Nirvan Ramjuttun | 2ac64f2 | 2019-05-30 14:07:23 -0400 | [diff] [blame] | 1 | /** |
| 2 | ~ Copyright © 2019 Bell Canada. |
| 3 | ~ |
| 4 | ~ Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | ~ you may not use this file except in compliance with the License. |
| 6 | ~ You may obtain a copy of the License at |
| 7 | ~ |
| 8 | ~ http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | ~ |
| 10 | ~ Unless required by applicable law or agreed to in writing, software |
| 11 | ~ distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | ~ See the License for the specific language governing permissions and |
| 14 | ~ limitations under the License. |
| 15 | */ |
| 16 | import * as fs from 'fs'; |
| 17 | import * as uuidv1 from 'uuid/v1'; |
| 18 | const grpc = require('grpc'); |
| 19 | import * as protoLoader from '@grpc/proto-loader'; |
| 20 | import {processorApiConfig} from '../config/app-config'; |
| 21 | |
| 22 | const PROTO_PATH = processorApiConfig.grpc.bluePrintManagement.protoPath; |
| 23 | |
| 24 | // Suggested options for similarity to existing grpc.load behavior |
| 25 | const packageDefinition: protoLoader.PackageDefinition = protoLoader.loadSync( |
| 26 | PROTO_PATH, |
| 27 | { |
| 28 | keepCase: true, |
| 29 | longs: String, |
| 30 | enums: String, |
| 31 | defaults: true, |
| 32 | oneofs: true |
| 33 | }); |
| 34 | |
| 35 | const protoDescriptor = grpc.loadPackageDefinition(packageDefinition); |
| 36 | // The protoDescriptor object has the full package hierarchy |
| 37 | |
| 38 | const stub = new protoDescriptor.org.onap.ccsdk.cds.controllerblueprints.management.api.BluePrintManagementService( |
| 39 | "" + processorApiConfig.grpc.host + ":" + processorApiConfig.grpc.port + "", |
| 40 | grpc.credentials.createInsecure()); |
| 41 | |
| 42 | const metadata = new grpc.Metadata(); |
| 43 | metadata.add('Authorization', processorApiConfig.grpc.authToken); |
| 44 | |
| 45 | class BluePrintManagementServiceGrpcClient { |
| 46 | |
| 47 | async uploadBlueprint(filePath: string): Promise<any> { |
| 48 | |
| 49 | let input = { |
| 50 | commonHeader: { |
| 51 | timestamp: new Date(), |
| 52 | originatorId: "cds-ui", |
| 53 | requestId: uuidv1(), |
| 54 | subRequestId: "1234-56", |
| 55 | }, |
| 56 | fileChunk: { |
| 57 | chunk: fs.readFileSync(filePath) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | let removeTempFile = () => { |
| 62 | fs.unlink(filePath, (err: any) => { |
| 63 | if (err) { |
| 64 | console.error(err); |
| 65 | } |
| 66 | }); |
| 67 | } |
| 68 | |
| 69 | return new Promise<any>((resolve, reject) => { |
| 70 | stub.uploadBlueprint(input, metadata, (err: any, output: any) => { |
| 71 | if (err) { |
| 72 | removeTempFile(); |
| 73 | reject(err); |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | removeTempFile(); |
| 78 | resolve(output); |
| 79 | }); |
| 80 | }); |
| 81 | |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | export const bluePrintManagementServiceGrpcClient = new BluePrintManagementServiceGrpcClient(); |
| 86 | |