Arundathi Patil | e679ac7 | 2019-01-21 21:43:47 +0530 | [diff] [blame] | 1 | /* |
| 2 | ============LICENSE_START========================================== |
| 3 | =================================================================== |
| 4 | Copyright (C) 2018-19 IBM Intellectual Property. All rights reserved. |
| 5 | =================================================================== |
| 6 | |
| 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 |
| 11 | |
| 12 | http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | |
| 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============================================ |
| 20 | */ |
| 21 | |
| 22 | |
| 23 | |
| 24 | import { |
| 25 | Count, |
| 26 | CountSchema, |
| 27 | Filter, |
| 28 | repository, |
| 29 | Where, |
| 30 | } from '@loopback/repository'; |
| 31 | import { |
| 32 | post, |
| 33 | param, |
| 34 | get, |
| 35 | getFilterSchemaFor, |
| 36 | getWhereSchemaFor, |
| 37 | patch, |
| 38 | put, |
| 39 | del, |
| 40 | requestBody, |
Ezhilarasi | e4b82f7 | 2019-04-04 21:06:09 +0530 | [diff] [blame^] | 41 | Request, |
| 42 | Response, |
| 43 | RestBindings, |
Arundathi Patil | e679ac7 | 2019-01-21 21:43:47 +0530 | [diff] [blame] | 44 | } from '@loopback/rest'; |
| 45 | import {Blueprint} from '../models'; |
Ezhilarasi | e4b82f7 | 2019-04-04 21:06:09 +0530 | [diff] [blame^] | 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 | |
| 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=="; |
Arundathi Patil | e679ac7 | 2019-01-21 21:43:47 +0530 | [diff] [blame] | 54 | |
| 55 | export class BlueprintRestController { |
| 56 | constructor( |
Ezhilarasi | e4b82f7 | 2019-04-04 21:06:09 +0530 | [diff] [blame^] | 57 | // @repository(BlueprintRepository) |
| 58 | // public blueprintRepository : BlueprintRepository, |
| 59 | @inject('services.BlueprintService') |
| 60 | public bpservice: BlueprintService, |
Arundathi Patil | e679ac7 | 2019-01-21 21:43:47 +0530 | [diff] [blame] | 61 | ) {} |
| 62 | |
Arundathi Patil | e679ac7 | 2019-01-21 21:43:47 +0530 | [diff] [blame] | 63 | @get('/blueprints', { |
| 64 | responses: { |
| 65 | '200': { |
Ezhilarasi | e4b82f7 | 2019-04-04 21:06:09 +0530 | [diff] [blame^] | 66 | 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 Patil | e679ac7 | 2019-01-21 21:43:47 +0530 | [diff] [blame] | 86 | }, |
| 87 | }, |
Ezhilarasi | e4b82f7 | 2019-04-04 21:06:09 +0530 | [diff] [blame^] | 88 | }) |
| 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 Patil | e679ac7 | 2019-01-21 21:43:47 +0530 | [diff] [blame] | 104 | } |
| 105 | |
Ezhilarasi | e4b82f7 | 2019-04-04 21:06:09 +0530 | [diff] [blame^] | 106 | @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 Patil | e679ac7 | 2019-01-21 21:43:47 +0530 | [diff] [blame] | 117 | }, |
Ezhilarasi | e4b82f7 | 2019-04-04 21:06:09 +0530 | [diff] [blame^] | 118 | }) |
| 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 Patil | e679ac7 | 2019-01-21 21:43:47 +0530 | [diff] [blame] | 135 | } |
| 136 | |
Ezhilarasi | e4b82f7 | 2019-04-04 21:06:09 +0530 | [diff] [blame^] | 137 | @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 Patil | e679ac7 | 2019-01-21 21:43:47 +0530 | [diff] [blame] | 152 | } |
| 153 | |
Ezhilarasi | e4b82f7 | 2019-04-04 21:06:09 +0530 | [diff] [blame^] | 154 | 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 Patil | e679ac7 | 2019-01-21 21:43:47 +0530 | [diff] [blame] | 170 | } |
| 171 | |
Ezhilarasi | e4b82f7 | 2019-04-04 21:06:09 +0530 | [diff] [blame^] | 172 | 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 Patil | e679ac7 | 2019-01-21 21:43:47 +0530 | [diff] [blame] | 178 | }, |
Ezhilarasi | e4b82f7 | 2019-04-04 21:06:09 +0530 | [diff] [blame^] | 179 | 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 Patil | e679ac7 | 2019-01-21 21:43:47 +0530 | [diff] [blame] | 206 | } |
| 207 | |
Ezhilarasi | e4b82f7 | 2019-04-04 21:06:09 +0530 | [diff] [blame^] | 208 | 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 Patil | e679ac7 | 2019-01-21 21:43:47 +0530 | [diff] [blame] | 225 | } |
Ezhilarasi | e4b82f7 | 2019-04-04 21:06:09 +0530 | [diff] [blame^] | 226 | } |