blob: ae028afc3d088569d68281d1b2e799fa072b62ab [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==";
Arundathi Patile679ac72019-01-21 21:43:47 +053054
55export class BlueprintRestController {
56 constructor(
Ezhilarasie4b82f72019-04-04 21:06:09 +053057 // @repository(BlueprintRepository)
58 // public blueprintRepository : BlueprintRepository,
59 @inject('services.BlueprintService')
60 public bpservice: BlueprintService,
Arundathi Patile679ac72019-01-21 21:43:47 +053061 ) {}
62
Arundathi Patile679ac72019-01-21 21:43:47 +053063 @get('/blueprints', {
64 responses: {
65 '200': {
Ezhilarasie4b82f72019-04-04 21:06:09 +053066 description: 'Blueprint model instance',
67 content: { 'application/json': { schema: { 'x-ts-type': Blueprint } } },
68 },
69 },
70 })
71 async getall() {
72 return await this.bpservice.getAllblueprints(REST_BLUEPRINT_CONTROLLER_BASIC_AUTH_HEADER);
73
74 }
75
76 @post('/create-blueprint')
77 async upload(
78 @requestBody({
79 description: 'multipart/form-data value.',
80 required: true,
81 content: {
82 'multipart/form-data': {
83 // Skip body parsing
84 'x-parser': 'stream',
85 schema: {type: 'object'},
Arundathi Patile679ac72019-01-21 21:43:47 +053086 },
87 },
Ezhilarasie4b82f72019-04-04 21:06:09 +053088 })
89 request: Request,
90 @inject(RestBindings.Http.RESPONSE) response: Response,
91 ): Promise<object> {
92 return new Promise((resolve, reject) => {
93 this.getFileFromMultiPartForm(request).then(file=>{
94 this.uploadFileToBlueprintController(file, "/blueprint-model/").then(resp=>{
95 response.setHeader("X-ONAP-RequestID", resp.headers['x-onap-requestid']);
96 resolve(JSON.parse(resp.body));
97 }, err=>{
98 reject(err);
99 });
100 }, err=>{
101 reject(err);
102 });
103 });
Arundathi Patile679ac72019-01-21 21:43:47 +0530104 }
105
Ezhilarasie4b82f72019-04-04 21:06:09 +0530106 @post('/enrich-blueprint')
107 async enrich(
108 @requestBody({
109 description: 'multipart/form-data value.',
110 required: true,
111 content: {
112 'multipart/form-data': {
113 // Skip body parsing
114 'x-parser': 'stream',
115 schema: {type: 'object'},
116 },
Arundathi Patile679ac72019-01-21 21:43:47 +0530117 },
Ezhilarasie4b82f72019-04-04 21:06:09 +0530118 })
119 request: Request,
120 @inject(RestBindings.Http.RESPONSE) response: Response,
121 ): Promise<any> {
122 return new Promise((resolve, reject) => {
123 this.getFileFromMultiPartForm(request).then(file=>{
124 this.uploadFileToBlueprintController(file, "/blueprint-model/enrich/").then(resp=>{
125 response.setHeader("X-ONAP-RequestID", resp.headers['x-onap-requestid']);
126 response.setHeader("Content-Disposition", resp.headers['content-disposition']);
127 resolve(resp.body);
128 }, err=>{
129 reject(err);
130 });
131 }, err=>{
132 reject(err);
133 });
134 });
Arundathi Patile679ac72019-01-21 21:43:47 +0530135 }
136
Ezhilarasie4b82f72019-04-04 21:06:09 +0530137 @get('/download-blueprint/{id}')
138 async download(
139 @param.path.string('id') id: string,
140 @inject(RestBindings.Http.REQUEST) request: Request,
141 @inject(RestBindings.Http.RESPONSE) response: Response,
142 ): Promise<any> {
143 return new Promise((resolve, reject) => {
144 this.downloadFileFromBlueprintController("/blueprint-model/download/" + id).then(resp=>{
145 response.setHeader("X-ONAP-RequestID", resp.headers['x-onap-requestid']);
146 response.setHeader("Content-Disposition", resp.headers['content-disposition']);
147 resolve(resp.body);
148 }, err=>{
149 reject(err);
150 });
151 });
Arundathi Patile679ac72019-01-21 21:43:47 +0530152 }
153
Ezhilarasie4b82f72019-04-04 21:06:09 +0530154 async getFileFromMultiPartForm(request: Request): Promise<any>{
155 return new Promise((resolve, reject) => {
156 // let options = {
157 // uploadDir: MULTIPART_FORM_UPLOAD_DIR
158 // }
159 let form = new multiparty.Form();
160 form.parse(request, (err: any, fields: any, files: { [x: string]: any[]; }) => {
161 if (err) reject(err);
162 let file = files['file'][0]; // get the file from the returned files object
163 if(!file){
164 reject('File was not found in form data.');
165 }else{
166 resolve(file);
167 }
168 });
169 })
Arundathi Patile679ac72019-01-21 21:43:47 +0530170 }
171
Ezhilarasie4b82f72019-04-04 21:06:09 +0530172 async uploadFileToBlueprintController(file: any, uri: string): Promise<any>{
173 let options = {
174 url: REST_BLUEPRINT_CONTROLLER_BASE_URL + uri,
175 headers: {
176 Authorization: REST_BLUEPRINT_CONTROLLER_BASIC_AUTH_HEADER,
177 'content-type': 'multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW'
Arundathi Patile679ac72019-01-21 21:43:47 +0530178 },
Ezhilarasie4b82f72019-04-04 21:06:09 +0530179 formData: {
180 file: {
181 value: fs.createReadStream(file.path),
182 options: {
183 filename: 'cba.zip',
184 contentType: 'application/zip'
185 }
186 }
187 }
188 };
189
190 return new Promise((resolve, reject) => {
191 request_lib.post(options, (err: any, resp: any, body: any) => {
192 if (err) {
193 //delete tmp file
194 fs.unlink(file.path, (err: any) => {
195 if (err) {
196 console.error(err);
197 return
198 }
199 })
200 reject(err);
201 }else{
202 resolve(resp);
203 }
204 })
205 })
Arundathi Patile679ac72019-01-21 21:43:47 +0530206 }
207
Ezhilarasie4b82f72019-04-04 21:06:09 +0530208 async downloadFileFromBlueprintController(uri: string): Promise<any> {
209 let options = {
210 url: REST_BLUEPRINT_CONTROLLER_BASE_URL + uri,
211 headers: {
212 Authorization: REST_BLUEPRINT_CONTROLLER_BASIC_AUTH_HEADER,
213 }
214 };
215
216 return new Promise((resolve, reject) => {
217 request_lib.get(options, (err: any, resp: any, body: any) => {
218 if (err) {
219 reject(err);
220 }else{
221 resolve(resp);
222 }
223 })
224 })
Arundathi Patile679ac72019-01-21 21:43:47 +0530225}
Ezhilarasie4b82f72019-04-04 21:06:09 +0530226}