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/paged', {
75 description: 'Blueprint model instance with pagination',
76 content: { 'application/json': { schema: { 'x-ts-type': Blueprint } } },
80 async getPagedBlueprints(
81 @param.query.number('limit') limit: number,
82 @param.query.number('offset') offset: number,
83 @param.query.string('sort') sort: string) {
84 return await this.bpservice.getPagedBueprints(limit, offset, sort);
87 @get('/controllerblueprint/metadata/paged/{keyword}', {
90 description: 'Blueprint model instance with pagination',
91 content: { 'application/json': { schema: { 'x-ts-type': Blueprint } } },
95 async getMetaDataPagedBlueprints(
96 @param.path.string('keyword') keyword: string,
97 @param.query.number('limit') limit: number,
98 @param.query.number('offset') offset: number,
99 @param.query.string('sort') sort: string) {
100 return await this.bpservice.getMetaDataPagedBlueprints(limit, offset, sort,keyword);
103 @get('/controllerblueprint/meta-data/{keyword}', {
106 description: 'Blueprint model instance',
107 content: { 'application/json': { schema: { 'x-ts-type': Blueprint } } },
111 async getAllPacakgesByKeword(@param.path.string('keyword') keyword: string) {
112 return await this.bpservice.getBlueprintsByKeyword(keyword);
117 @get('/controllerblueprint/searchByTags/{tags}', {
120 content: { 'application/json': {} },
124 async getByTags(@param.path.string('tags') tags: string) {
125 return await this.bpservice.getByTags(tags);
128 @post('/controllerblueprint/create-blueprint')
131 description: 'multipart/form-data value.',
134 'multipart/form-data': {
136 'x-parser': 'stream',
137 schema: { type: 'object' },
142 @inject(RestBindings.Http.RESPONSE) response: Response,
143 ): Promise<Response> {
144 return new Promise((resolve, reject) => {
145 this.getFileFromMultiPartForm(request).then(file => {
146 // if (appConfig.action.deployBlueprint.grpcEnabled)
147 if (appConfig.action.grpcEnabled)
148 return this.uploadFileToBlueprintProcessorGrpc(file, "DRAFT", response);
150 return this.uploadFileToBlueprintController(file, "/blueprint-model/", response);
155 // return new Promise((resolve, reject) => {
156 // this.getFileFromMultiPartForm(request).then(file => {
157 // this.uploadFileToBlueprintController(file, "/blueprint-model/", response).then(resp => {
168 @post('/controllerblueprint/publish')
171 description: 'multipart/form-data value.',
174 'multipart/form-data': {
176 'x-parser': 'stream',
177 schema: { type: 'object' },
182 @inject(RestBindings.Http.RESPONSE) response: Response,
183 ): Promise<Response> {
184 return new Promise((resolve, reject) => {
185 this.getFileFromMultiPartForm(request).then(file => {
186 // if (appConfig.action.deployBlueprint.grpcEnabled)
187 if (appConfig.action.grpcEnabled)
188 return this.uploadFileToBlueprintProcessorGrpc(file, "PUBLISH", response);
190 return this.uploadFileToBlueprintController(file, "/blueprint-model/publish/", response);
195 // return new Promise((resolve, reject) => {
196 // this.getFileFromMultiPartForm(request).then(file => {
197 // this.uploadFileToBlueprintController(file, "/blueprint-model/publish/", response).then(resp => {
208 @post('/controllerblueprint/enrich-blueprint')
211 description: 'multipart/form-data value.',
214 'multipart/form-data': {
216 'x-parser': 'stream',
217 schema: { type: 'object' },
222 @inject(RestBindings.Http.RESPONSE) response: Response,
223 ): Promise<Response> {
224 return new Promise((resolve, reject) => {
225 this.getFileFromMultiPartForm(request).then(file => {
226 if (appConfig.action.grpcEnabled)
227 return this.uploadFileToBlueprintProcessorGrpc(file, "ENRICH", response);
229 return this.uploadFileToBlueprintController(file, "/blueprint-model/enrich/", response)
230 // this.uploadFileToBlueprintController(file, "/blueprint-model/enrich/", response).then(resp => {
241 @get('/controllerblueprint/download-blueprint/{name}/{version}')
243 @param.path.string('name') name: string,
244 @param.path.string('version') version: string,
245 @inject(RestBindings.Http.RESPONSE) response: Response,
246 ): Promise<Response> {
248 if (appConfig.action.grpcEnabled)
249 return this.downloadFileFromBlueprintProcessorGrpc(name, version, response);
251 return this.downloadFileFromBlueprintController("/blueprint-model/download/by-name/" + name + "/version/" + version, response);
255 async getFileFromMultiPartForm(request: Request): Promise<multiparty.File> {
256 return new Promise((resolve, reject) => {
257 let form = new multiparty.Form();
258 form.parse(request, (err: any, fields: any, files: { [x: string]: any[]; }) => {
259 if (err) reject(err);
260 let file = files['file'][0]; // get the file from the returned files object
262 reject('File was not found in form data.');
270 @post('/controllerblueprint/deploy-blueprint')
273 description: 'multipart/form-data value.',
276 'multipart/form-data': {
278 'x-parser': 'stream',
279 schema: { type: 'object' },
284 @inject(RestBindings.Http.RESPONSE) response: Response,
285 ): Promise<Response> {
286 return new Promise((resolve, reject) => {
287 this.getFileFromMultiPartForm(request).then(file => {
288 // if (appConfig.action.deployBlueprint.grpcEnabled)
289 if (appConfig.action.grpcEnabled)
290 return this.uploadFileToBlueprintProcessorGrpc(file, "PUBLISH", response);
292 return this.uploadFileToBlueprintProcessor(file, "/execution-service/upload/", response);
299 async uploadFileToBlueprintController(file: multiparty.File, uri: string, response: Response): Promise<Response> {
300 return this.uploadFileToBlueprintService(file, processorApiConfig.http.url + uri, processorApiConfig.http.authToken, response);
303 async uploadFileToBlueprintProcessor(file: multiparty.File, uri: string, response: Response): Promise<Response> {
304 return this.uploadFileToBlueprintService(file, processorApiConfig.http.url + uri, processorApiConfig.http.authToken, response);
307 async uploadFileToBlueprintService(file: multiparty.File, url: string, authToken: string, response: Response): Promise<Response> {
311 Authorization: authToken,
312 'content-type': 'multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW'
316 value: fs.createReadStream(file.path),
319 contentType: 'application/zip'
325 var removeTempFile = () => {
326 fs.unlink(file.path, (err: any) => {
333 return new Promise((resolve, reject) => {
334 request_lib.post(options)
335 .on("error", err => {
339 .once("finish", () => {
346 async downloadFileFromBlueprintController(uri: string, response: Response): Promise<Response> {
347 return this.downloadFileFromBlueprintService(processorApiConfig.http.url + uri, processorApiConfig.http.authToken, response);
350 async downloadFileFromBlueprintService(url: string, authToken: string, response: Response): Promise<Response> {
354 Authorization: authToken,
357 return new Promise((resolve, reject) => {
358 request_lib.get(options)
359 .on("error", err => {
363 .once("finish", () => {
369 async uploadFileToBlueprintProcessorGrpc(file: multiparty.File, actionName: string, response: Response): Promise<Response> {
370 return new Promise<Response>((resolve, reject) => {
371 bluePrintManagementServiceGrpcClient.uploadBlueprint(file.path, actionName).then(output => {
372 response.send(output.status.message);
375 response.status(500).send(err);
380 async downloadFileFromBlueprintProcessorGrpc(blueprintName: string, blueprintVersion: string, response: Response): Promise<Response> {
381 return new Promise<Response>((resolve, reject) => {
382 bluePrintManagementServiceGrpcClient.downloadBlueprint(blueprintName, blueprintVersion)
384 response.send(output.status.message);
387 response.status(500).send(err);