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=="; |
Ezhilarasi | 1b2d71d | 2019-04-06 00:21:57 +0530 | [diff] [blame] | 54 | const REST_BLUEPRINT_PROCESSOR_BASE_URL= "http://localhost:8081/api/v1"; |
Arundathi Patil | e679ac7 | 2019-01-21 21:43:47 +0530 | [diff] [blame] | 55 | |
| 56 | export class BlueprintRestController { |
| 57 | constructor( |
Ezhilarasi | e4b82f7 | 2019-04-04 21:06:09 +0530 | [diff] [blame] | 58 | // @repository(BlueprintRepository) |
| 59 | // public blueprintRepository : BlueprintRepository, |
| 60 | @inject('services.BlueprintService') |
| 61 | public bpservice: BlueprintService, |
Arundathi Patil | e679ac7 | 2019-01-21 21:43:47 +0530 | [diff] [blame] | 62 | ) {} |
| 63 | |
Arundathi Patil | e679ac7 | 2019-01-21 21:43:47 +0530 | [diff] [blame] | 64 | @get('/blueprints', { |
| 65 | responses: { |
| 66 | '200': { |
Ezhilarasi | e4b82f7 | 2019-04-04 21:06:09 +0530 | [diff] [blame] | 67 | 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 Patil | e679ac7 | 2019-01-21 21:43:47 +0530 | [diff] [blame] | 87 | }, |
| 88 | }, |
Ezhilarasi | e4b82f7 | 2019-04-04 21:06:09 +0530 | [diff] [blame] | 89 | }) |
| 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 Patil | e679ac7 | 2019-01-21 21:43:47 +0530 | [diff] [blame] | 105 | } |
| 106 | |
Ezhilarasi | e4b82f7 | 2019-04-04 21:06:09 +0530 | [diff] [blame] | 107 | @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 Patil | e679ac7 | 2019-01-21 21:43:47 +0530 | [diff] [blame] | 118 | }, |
Ezhilarasi | e4b82f7 | 2019-04-04 21:06:09 +0530 | [diff] [blame] | 119 | }) |
| 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 Patil | e679ac7 | 2019-01-21 21:43:47 +0530 | [diff] [blame] | 136 | } |
| 137 | |
Ezhilarasi | a33ed51 | 2019-04-08 20:57:54 +0530 | [diff] [blame] | 138 | @get('/download-blueprint/{name}/{version}') |
Ezhilarasi | e4b82f7 | 2019-04-04 21:06:09 +0530 | [diff] [blame] | 139 | async download( |
Ezhilarasi | a33ed51 | 2019-04-08 20:57:54 +0530 | [diff] [blame] | 140 | @param.path.string('name') name: string, |
| 141 | @param.path.string('version') version:string, |
| 142 | // @inject(RestBindings.Http.REQUEST) request: Request, |
Ezhilarasi | e4b82f7 | 2019-04-04 21:06:09 +0530 | [diff] [blame] | 143 | @inject(RestBindings.Http.RESPONSE) response: Response, |
| 144 | ): Promise<any> { |
| 145 | return new Promise((resolve, reject) => { |
Ezhilarasi | a33ed51 | 2019-04-08 20:57:54 +0530 | [diff] [blame] | 146 | this.downloadFileFromBlueprintController("/blueprint-model/download/by-name/"+name+"/version/"+version).then(resp=>{ |
Ezhilarasi | e4b82f7 | 2019-04-04 21:06:09 +0530 | [diff] [blame] | 147 | 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 Patil | e679ac7 | 2019-01-21 21:43:47 +0530 | [diff] [blame] | 154 | } |
| 155 | |
Ezhilarasi | e4b82f7 | 2019-04-04 21:06:09 +0530 | [diff] [blame] | 156 | 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 Patil | e679ac7 | 2019-01-21 21:43:47 +0530 | [diff] [blame] | 172 | } |
| 173 | |
Ezhilarasi | e4b82f7 | 2019-04-04 21:06:09 +0530 | [diff] [blame] | 174 | 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 Patil | e679ac7 | 2019-01-21 21:43:47 +0530 | [diff] [blame] | 180 | }, |
Ezhilarasi | e4b82f7 | 2019-04-04 21:06:09 +0530 | [diff] [blame] | 181 | 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 Patil | e679ac7 | 2019-01-21 21:43:47 +0530 | [diff] [blame] | 208 | } |
| 209 | |
Ezhilarasi | e4b82f7 | 2019-04-04 21:06:09 +0530 | [diff] [blame] | 210 | 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 Patil | e679ac7 | 2019-01-21 21:43:47 +0530 | [diff] [blame] | 227 | } |
Ezhilarasi | e4b82f7 | 2019-04-04 21:06:09 +0530 | [diff] [blame] | 228 | } |