2 ============LICENSE_START==========================================
3 ===================================================================
4 Copyright (C) 2018-19 IBM Intellectual Property. All rights reserved.
5 ===================================================================
7 Unless otherwise specified, all software contained herein is licensed
8 under the Apache License, Version 2.0 (the License);
9 you may not use this software except in compliance with the License.
10 You may obtain a copy of the License at
12 http://www.apache.org/licenses/LICENSE-2.0
14 Unless required by applicable law or agreed to in writing, software
15 distributed under the License is distributed on an "AS IS" BASIS,
16 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 See the License for the specific language governing permissions and
18 limitations under the License.
19 ============LICENSE_END============================================
30 } from '@loopback/repository';
44 } from '@loopback/rest';
45 import { Blueprint } from '../models';
46 import { inject } from '@loopback/core';
47 import { BlueprintService } from '../services';
48 import * as fs from 'fs';
49 import * as multiparty from 'multiparty';
50 import * as request_lib from 'request';
51 import { processorApiConfig, appConfig } from '../config/app-config';
52 import { bluePrintManagementServiceGrpcClient } from '../clients/blueprint-management-service-grpc-client';
54 export class BlueprintRestController {
56 @inject('services.BlueprintService')
57 public bpservice: BlueprintService,
60 @get('/controllerblueprint/all', {
63 description: 'Blueprint model instance',
64 content: { 'application/json': { schema: { 'x-ts-type': Blueprint } } },
69 return await this.bpservice.getAllblueprints();
72 @get('/controllerblueprint/meta-data/{keyword}', {
75 description: 'Blueprint model instance',
76 content: { 'application/json': { schema: { 'x-ts-type': Blueprint } } },
80 async getAllPacakgesByKeword(@param.path.string('keyword') keyword: string) {
81 return await this.bpservice.getBlueprintsByKeyword(keyword);
86 @get('/controllerblueprint/searchByTags/{tags}', {
89 content: { 'application/json': {} },
93 async getByTags(@param.path.string('tags') tags: string) {
94 return await this.bpservice.getByTags(tags);
97 @post('/controllerblueprint/create-blueprint')
100 description: 'multipart/form-data value.',
103 'multipart/form-data': {
105 'x-parser': 'stream',
106 schema: { type: 'object' },
111 @inject(RestBindings.Http.RESPONSE) response: Response,
112 ): Promise<Response> {
113 return new Promise((resolve, reject) => {
114 this.getFileFromMultiPartForm(request).then(file => {
115 // if (appConfig.action.deployBlueprint.grpcEnabled)
116 if (appConfig.action.grpcEnabled)
117 return this.uploadFileToBlueprintProcessorGrpc(file, "DRAFT", response);
119 return this.uploadFileToBlueprintController(file, "/blueprint-model/", response);
124 // return new Promise((resolve, reject) => {
125 // this.getFileFromMultiPartForm(request).then(file => {
126 // this.uploadFileToBlueprintController(file, "/blueprint-model/", response).then(resp => {
137 @post('/controllerblueprint/publish')
140 description: 'multipart/form-data value.',
143 'multipart/form-data': {
145 'x-parser': 'stream',
146 schema: { type: 'object' },
151 @inject(RestBindings.Http.RESPONSE) response: Response,
152 ): Promise<Response> {
153 return new Promise((resolve, reject) => {
154 this.getFileFromMultiPartForm(request).then(file => {
155 // if (appConfig.action.deployBlueprint.grpcEnabled)
156 if (appConfig.action.grpcEnabled)
157 return this.uploadFileToBlueprintProcessorGrpc(file, "PUBLISH", response);
159 return this.uploadFileToBlueprintController(file, "/blueprint-model/publish/", response);
164 // return new Promise((resolve, reject) => {
165 // this.getFileFromMultiPartForm(request).then(file => {
166 // this.uploadFileToBlueprintController(file, "/blueprint-model/publish/", response).then(resp => {
177 @post('/controllerblueprint/enrich-blueprint')
180 description: 'multipart/form-data value.',
183 'multipart/form-data': {
185 'x-parser': 'stream',
186 schema: { type: 'object' },
191 @inject(RestBindings.Http.RESPONSE) response: Response,
192 ): Promise<Response> {
193 return new Promise((resolve, reject) => {
194 this.getFileFromMultiPartForm(request).then(file => {
195 if (appConfig.action.grpcEnabled)
196 return this.uploadFileToBlueprintProcessorGrpc(file, "ENRICH", response);
198 return this.uploadFileToBlueprintController(file, "/blueprint-model/enrich/", response)
199 // this.uploadFileToBlueprintController(file, "/blueprint-model/enrich/", response).then(resp => {
210 @get('/controllerblueprint/download-blueprint/{name}/{version}')
212 @param.path.string('name') name: string,
213 @param.path.string('version') version: string,
214 @inject(RestBindings.Http.RESPONSE) response: Response,
215 ): Promise<Response> {
217 if (appConfig.action.grpcEnabled)
218 return this.downloadFileFromBlueprintProcessorGrpc(name, version, response);
220 return this.downloadFileFromBlueprintController("/blueprint-model/download/by-name/" + name + "/version/" + version, response);
224 async getFileFromMultiPartForm(request: Request): Promise<multiparty.File> {
225 return new Promise((resolve, reject) => {
226 let form = new multiparty.Form();
227 form.parse(request, (err: any, fields: any, files: { [x: string]: any[]; }) => {
228 if (err) reject(err);
229 let file = files['file'][0]; // get the file from the returned files object
231 reject('File was not found in form data.');
239 @post('/controllerblueprint/deploy-blueprint')
242 description: 'multipart/form-data value.',
245 'multipart/form-data': {
247 'x-parser': 'stream',
248 schema: { type: 'object' },
253 @inject(RestBindings.Http.RESPONSE) response: Response,
254 ): Promise<Response> {
255 return new Promise((resolve, reject) => {
256 this.getFileFromMultiPartForm(request).then(file => {
257 // if (appConfig.action.deployBlueprint.grpcEnabled)
258 if (appConfig.action.grpcEnabled)
259 return this.uploadFileToBlueprintProcessorGrpc(file, "PUBLISH", response);
261 return this.uploadFileToBlueprintProcessor(file, "/execution-service/upload/", response);
268 async uploadFileToBlueprintController(file: multiparty.File, uri: string, response: Response): Promise<Response> {
269 return this.uploadFileToBlueprintService(file, processorApiConfig.http.url + uri, processorApiConfig.http.authToken, response);
272 async uploadFileToBlueprintProcessor(file: multiparty.File, uri: string, response: Response): Promise<Response> {
273 return this.uploadFileToBlueprintService(file, processorApiConfig.http.url + uri, processorApiConfig.http.authToken, response);
276 async uploadFileToBlueprintService(file: multiparty.File, url: string, authToken: string, response: Response): Promise<Response> {
280 Authorization: authToken,
281 'content-type': 'multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW'
285 value: fs.createReadStream(file.path),
288 contentType: 'application/zip'
294 var removeTempFile = () => {
295 fs.unlink(file.path, (err: any) => {
302 return new Promise((resolve, reject) => {
303 request_lib.post(options)
304 .on("error", err => {
308 .once("finish", () => {
315 async downloadFileFromBlueprintController(uri: string, response: Response): Promise<Response> {
316 return this.downloadFileFromBlueprintService(processorApiConfig.http.url + uri, processorApiConfig.http.authToken, response);
319 async downloadFileFromBlueprintService(url: string, authToken: string, response: Response): Promise<Response> {
323 Authorization: authToken,
326 return new Promise((resolve, reject) => {
327 request_lib.get(options)
328 .on("error", err => {
332 .once("finish", () => {
338 async uploadFileToBlueprintProcessorGrpc(file: multiparty.File, actionName: string, response: Response): Promise<Response> {
339 return new Promise<Response>((resolve, reject) => {
340 bluePrintManagementServiceGrpcClient.uploadBlueprint(file.path, actionName).then(output => {
341 response.send(output.status.message);
344 response.status(500).send(err);
349 async downloadFileFromBlueprintProcessorGrpc(blueprintName: string, blueprintVersion: string, response: Response): Promise<Response> {
350 return new Promise<Response>((resolve, reject) => {
351 bluePrintManagementServiceGrpcClient.downloadBlueprint(blueprintName, blueprintVersion)
353 response.send(output.status.message);
356 response.status(500).send(err);