Renaming Files having BluePrint to have Blueprint
[ccsdk/cds.git] / cds-ui / server / src / clients / blueprint-management-service-grpc-client.ts
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, actionName: 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             actionIdentifiers: {
60                 mode: "sync",
61                 blueprintName: "cds.zip",
62                 actionName: actionName
63             }
64         }
65
66         let removeTempFile = () => {
67             fs.unlink(filePath, (err: any) => {
68                 if (err) {
69                     console.error(err);
70                 }
71             });
72         }
73
74         return new Promise<any>((resolve, reject) => {
75             stub.uploadBlueprint(input, metadata, (err: any, output: any) => {
76                 if (err) {
77                     removeTempFile();
78                     reject(err);
79                     return;
80                 }
81
82                 removeTempFile();
83                 resolve(output);
84             });
85         });
86
87     }
88
89     async downloadBlueprint(blueprintName: string,blueprintVersion: string): Promise<any> {
90
91         let input = {
92             commonHeader: {
93                 timestamp: new Date(),
94                 originatorId: "cds-ui",
95                 requestId: uuidv1(),
96                 subRequestId: "1234-56",
97             },
98             actionIdentifiers: {
99                 mode: "sync",
100                 blueprintName: blueprintName,
101                 blueprintVersion: blueprintVersion
102             }
103         }
104
105         return new Promise<any>((resolve, reject) => {
106             stub.downloadBlueprint(input, metadata, (err: any, output: any) => {
107                 if (err) {
108                     reject(err);
109                     return;
110                 }
111                 resolve(output);
112             });
113         });
114
115     }
116 }
117
118 export const bluePrintManagementServiceGrpcClient = new BlueprintManagementServiceGrpcClient();
119