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';
52 const REST_BLUEPRINT_CONTROLLER_BASE_URL = process.env.REST_BLUEPRINT_CONTROLLER_BASE_URL || "http://localhost:8080/api/v1";
53 const REST_BLUEPRINT_CONTROLLER_BASIC_AUTH_HEADER = process.env.REST_BLUEPRINT_CONTROLLER_BASIC_AUTH_HEADER || "Basic Y2NzZGthcHBzOmNjc2RrYXBwcw==";
54 const REST_BLUEPRINT_PROCESSOR_BASE_URL= "http://localhost:8081/api/v1";
56 export class BlueprintRestController {
58 // @repository(BlueprintRepository)
59 // public blueprintRepository : BlueprintRepository,
60 @inject('services.BlueprintService')
61 public bpservice: BlueprintService,
67 description: 'Blueprint model instance',
68 content: { 'application/json': { schema: { 'x-ts-type': Blueprint } } },
73 return await this.bpservice.getAllblueprints(REST_BLUEPRINT_CONTROLLER_BASIC_AUTH_HEADER);
77 @post('/create-blueprint')
80 description: 'multipart/form-data value.',
83 'multipart/form-data': {
86 schema: {type: 'object'},
91 @inject(RestBindings.Http.RESPONSE) response: Response,
93 return new Promise((resolve, reject) => {
94 this.getFileFromMultiPartForm(request).then(file=>{
95 this.uploadFileToBlueprintController(file, "/blueprint-model/").then(resp=>{
96 response.setHeader("X-ONAP-RequestID", resp.headers['x-onap-requestid']);
97 resolve(JSON.parse(resp.body));
107 @post('/enrich-blueprint')
110 description: 'multipart/form-data value.',
113 'multipart/form-data': {
115 'x-parser': 'stream',
116 schema: {type: 'object'},
121 @inject(RestBindings.Http.RESPONSE) response: Response,
123 return new Promise((resolve, reject) => {
124 this.getFileFromMultiPartForm(request).then(file=>{
125 this.uploadFileToBlueprintController(file, "/blueprint-model/enrich/").then(resp=>{
126 response.setHeader("X-ONAP-RequestID", resp.headers['x-onap-requestid']);
127 response.setHeader("Content-Disposition", resp.headers['content-disposition']);
138 @get('/download-blueprint/{id}')
140 @param.path.string('id') id: string,
141 @inject(RestBindings.Http.REQUEST) request: Request,
142 @inject(RestBindings.Http.RESPONSE) response: Response,
144 return new Promise((resolve, reject) => {
145 this.downloadFileFromBlueprintController("/blueprint-model/download/" + id).then(resp=>{
146 response.setHeader("X-ONAP-RequestID", resp.headers['x-onap-requestid']);
147 response.setHeader("Content-Disposition", resp.headers['content-disposition']);
155 async getFileFromMultiPartForm(request: Request): Promise<any>{
156 return new Promise((resolve, reject) => {
158 // uploadDir: MULTIPART_FORM_UPLOAD_DIR
160 let form = new multiparty.Form();
161 form.parse(request, (err: any, fields: any, files: { [x: string]: any[]; }) => {
162 if (err) reject(err);
163 let file = files['file'][0]; // get the file from the returned files object
165 reject('File was not found in form data.');
173 async uploadFileToBlueprintController(file: any, uri: string): Promise<any>{
175 url: REST_BLUEPRINT_CONTROLLER_BASE_URL + uri,
177 Authorization: REST_BLUEPRINT_CONTROLLER_BASIC_AUTH_HEADER,
178 'content-type': 'multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW'
182 value: fs.createReadStream(file.path),
185 contentType: 'application/zip'
191 return new Promise((resolve, reject) => {
192 request_lib.post(options, (err: any, resp: any, body: any) => {
195 fs.unlink(file.path, (err: any) => {
209 async downloadFileFromBlueprintController(uri: string): Promise<any> {
211 url: REST_BLUEPRINT_CONTROLLER_BASE_URL + uri,
213 Authorization: REST_BLUEPRINT_CONTROLLER_BASIC_AUTH_HEADER,
217 return new Promise((resolve, reject) => {
218 request_lib.get(options, (err: any, resp: any, body: any) => {