blob: c1f7f9640618482b8c2fb5cc7ca78233fb1185eb [file] [log] [blame]
Arundathi Patile679ac72019-01-21 21:43:47 +05301/*
2============LICENSE_START==========================================
3===================================================================
4Copyright (C) 2018-19 IBM Intellectual Property. All rights reserved.
5===================================================================
6
7Unless otherwise specified, all software contained herein is licensed
8under the Apache License, Version 2.0 (the License);
9you may not use this software except in compliance with the License.
10You may obtain a copy of the License at
11
12 http://www.apache.org/licenses/LICENSE-2.0
13
14Unless required by applicable law or agreed to in writing, software
15distributed under the License is distributed on an "AS IS" BASIS,
16WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17See the License for the specific language governing permissions and
18limitations under the License.
19============LICENSE_END============================================
20*/
21
22
23
24import {
25 Count,
26 CountSchema,
27 Filter,
28 repository,
29 Where,
30} from '@loopback/repository';
31import {
32 post,
33 param,
34 get,
35 getFilterSchemaFor,
36 getWhereSchemaFor,
37 patch,
38 put,
39 del,
40 requestBody,
Ezhilarasie4b82f72019-04-04 21:06:09 +053041 Request,
42 Response,
43 RestBindings,
Arundathi Patile679ac72019-01-21 21:43:47 +053044} from '@loopback/rest';
45import {Blueprint} from '../models';
Ezhilarasie4b82f72019-04-04 21:06:09 +053046import { inject } from '@loopback/core';
47import { BlueprintService } from '../services';
48import * as fs from 'fs';
49import * as multiparty from 'multiparty';
50import * as request_lib from 'request';
51
52const REST_BLUEPRINT_CONTROLLER_BASE_URL = process.env.REST_BLUEPRINT_CONTROLLER_BASE_URL || "http://localhost:8080/api/v1";
53const REST_BLUEPRINT_CONTROLLER_BASIC_AUTH_HEADER = process.env.REST_BLUEPRINT_CONTROLLER_BASIC_AUTH_HEADER || "Basic Y2NzZGthcHBzOmNjc2RrYXBwcw==";
Ezhilarasi1b2d71d2019-04-06 00:21:57 +053054const REST_BLUEPRINT_PROCESSOR_BASE_URL= "http://localhost:8081/api/v1";
Arundathi Patile679ac72019-01-21 21:43:47 +053055
56export class BlueprintRestController {
57 constructor(
Ezhilarasie4b82f72019-04-04 21:06:09 +053058 // @repository(BlueprintRepository)
59 // public blueprintRepository : BlueprintRepository,
60 @inject('services.BlueprintService')
61 public bpservice: BlueprintService,
Arundathi Patile679ac72019-01-21 21:43:47 +053062 ) {}
63
Arundathi Patile679ac72019-01-21 21:43:47 +053064 @get('/blueprints', {
65 responses: {
66 '200': {
Ezhilarasie4b82f72019-04-04 21:06:09 +053067 description: 'Blueprint model instance',
68 content: { 'application/json': { schema: { 'x-ts-type': Blueprint } } },
69 },
70 },
71 })
72 async getall() {
73 return await this.bpservice.getAllblueprints(REST_BLUEPRINT_CONTROLLER_BASIC_AUTH_HEADER);
74
75 }
76
77 @post('/create-blueprint')
78 async upload(
79 @requestBody({
80 description: 'multipart/form-data value.',
81 required: true,
82 content: {
83 'multipart/form-data': {
84 // Skip body parsing
85 'x-parser': 'stream',
86 schema: {type: 'object'},
Arundathi Patile679ac72019-01-21 21:43:47 +053087 },
88 },
Ezhilarasie4b82f72019-04-04 21:06:09 +053089 })
90 request: Request,
91 @inject(RestBindings.Http.RESPONSE) response: Response,
92 ): Promise<object> {
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));
98 }, err=>{
99 reject(err);
100 });
101 }, err=>{
102 reject(err);
103 });
104 });
Arundathi Patile679ac72019-01-21 21:43:47 +0530105 }
106
Ezhilarasie4b82f72019-04-04 21:06:09 +0530107 @post('/enrich-blueprint')
108 async enrich(
109 @requestBody({
110 description: 'multipart/form-data value.',
111 required: true,
112 content: {
113 'multipart/form-data': {
114 // Skip body parsing
115 'x-parser': 'stream',
116 schema: {type: 'object'},
117 },
Arundathi Patile679ac72019-01-21 21:43:47 +0530118 },
Ezhilarasie4b82f72019-04-04 21:06:09 +0530119 })
120 request: Request,
121 @inject(RestBindings.Http.RESPONSE) response: Response,
122 ): Promise<any> {
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']);
128 resolve(resp.body);
129 }, err=>{
130 reject(err);
131 });
132 }, err=>{
133 reject(err);
134 });
135 });
Arundathi Patile679ac72019-01-21 21:43:47 +0530136 }
137
Ezhilarasia33ed512019-04-08 20:57:54 +0530138 @get('/download-blueprint/{name}/{version}')
Ezhilarasie4b82f72019-04-04 21:06:09 +0530139 async download(
Ezhilarasia33ed512019-04-08 20:57:54 +0530140 @param.path.string('name') name: string,
141 @param.path.string('version') version:string,
142 // @inject(RestBindings.Http.REQUEST) request: Request,
Ezhilarasie4b82f72019-04-04 21:06:09 +0530143 @inject(RestBindings.Http.RESPONSE) response: Response,
144 ): Promise<any> {
145 return new Promise((resolve, reject) => {
Ezhilarasia33ed512019-04-08 20:57:54 +0530146 this.downloadFileFromBlueprintController("/blueprint-model/download/by-name/"+name+"/version/"+version).then(resp=>{
Ezhilarasie4b82f72019-04-04 21:06:09 +0530147 response.setHeader("X-ONAP-RequestID", resp.headers['x-onap-requestid']);
148 response.setHeader("Content-Disposition", resp.headers['content-disposition']);
149 resolve(resp.body);
150 }, err=>{
151 reject(err);
152 });
153 });
Arundathi Patile679ac72019-01-21 21:43:47 +0530154 }
155
Ezhilarasie4b82f72019-04-04 21:06:09 +0530156 async getFileFromMultiPartForm(request: Request): Promise<any>{
157 return new Promise((resolve, reject) => {
158 // let options = {
159 // uploadDir: MULTIPART_FORM_UPLOAD_DIR
160 // }
161 let form = new multiparty.Form();
162 form.parse(request, (err: any, fields: any, files: { [x: string]: any[]; }) => {
163 if (err) reject(err);
164 let file = files['file'][0]; // get the file from the returned files object
165 if(!file){
166 reject('File was not found in form data.');
167 }else{
168 resolve(file);
169 }
170 });
171 })
Arundathi Patile679ac72019-01-21 21:43:47 +0530172 }
173
Ezhilarasie4b82f72019-04-04 21:06:09 +0530174 async uploadFileToBlueprintController(file: any, uri: string): Promise<any>{
175 let options = {
176 url: REST_BLUEPRINT_CONTROLLER_BASE_URL + uri,
177 headers: {
178 Authorization: REST_BLUEPRINT_CONTROLLER_BASIC_AUTH_HEADER,
179 'content-type': 'multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW'
Arundathi Patile679ac72019-01-21 21:43:47 +0530180 },
Ezhilarasie4b82f72019-04-04 21:06:09 +0530181 formData: {
182 file: {
183 value: fs.createReadStream(file.path),
184 options: {
185 filename: 'cba.zip',
186 contentType: 'application/zip'
187 }
188 }
189 }
190 };
191
192 return new Promise((resolve, reject) => {
193 request_lib.post(options, (err: any, resp: any, body: any) => {
194 if (err) {
195 //delete tmp file
196 fs.unlink(file.path, (err: any) => {
197 if (err) {
198 console.error(err);
199 return
200 }
201 })
202 reject(err);
203 }else{
204 resolve(resp);
205 }
206 })
207 })
Arundathi Patile679ac72019-01-21 21:43:47 +0530208 }
209
Ezhilarasie4b82f72019-04-04 21:06:09 +0530210 async downloadFileFromBlueprintController(uri: string): Promise<any> {
211 let options = {
212 url: REST_BLUEPRINT_CONTROLLER_BASE_URL + uri,
213 headers: {
214 Authorization: REST_BLUEPRINT_CONTROLLER_BASIC_AUTH_HEADER,
215 }
216 };
217
218 return new Promise((resolve, reject) => {
219 request_lib.get(options, (err: any, resp: any, body: any) => {
220 if (err) {
221 reject(err);
222 }else{
223 resolve(resp);
224 }
225 })
226 })
Arundathi Patile679ac72019-01-21 21:43:47 +0530227}
Ezhilarasie4b82f72019-04-04 21:06:09 +0530228}