From e4b82f76f386a1aad395951e5c5d2cf2ba688c6b Mon Sep 17 00:00:00 2001 From: Ezhilarasi Date: Thu, 4 Apr 2019 21:06:09 +0530 Subject: Service for get and post Blueprint Change-Id: Ie99f2ae09e65754e5da5384501f4f8574399da42 Issue-ID: CCSDK-1068 Signed-off-by: Ezhilarasi --- .../src/controllers/blueprint-rest.controller.ts | 244 +++++++++++++-------- .../src/datasources/blueprint.datasource.json | 22 ++ .../server/src/datasources/blueprint.datasource.ts | 14 ++ cds-ui/server/src/datasources/index.ts | 1 + cds-ui/server/src/datasources/rest.config.json | 39 ---- cds-ui/server/src/datasources/rest.datasource.json | 39 ++++ cds-ui/server/src/datasources/rest.datasource.ts | 2 +- cds-ui/server/src/services/blueprint.service.ts | 19 ++ cds-ui/server/src/services/index.ts | 1 + 9 files changed, 252 insertions(+), 129 deletions(-) create mode 100644 cds-ui/server/src/datasources/blueprint.datasource.json create mode 100644 cds-ui/server/src/datasources/blueprint.datasource.ts delete mode 100644 cds-ui/server/src/datasources/rest.config.json create mode 100644 cds-ui/server/src/datasources/rest.datasource.json create mode 100644 cds-ui/server/src/services/blueprint.service.ts create mode 100644 cds-ui/server/src/services/index.ts (limited to 'cds-ui/server/src') diff --git a/cds-ui/server/src/controllers/blueprint-rest.controller.ts b/cds-ui/server/src/controllers/blueprint-rest.controller.ts index 960f09fbe..ae028afc3 100644 --- a/cds-ui/server/src/controllers/blueprint-rest.controller.ts +++ b/cds-ui/server/src/controllers/blueprint-rest.controller.ts @@ -38,123 +38,189 @@ import { put, del, requestBody, + Request, + Response, + RestBindings, } from '@loopback/rest'; import {Blueprint} from '../models'; -import {BlueprintRepository} from '../repositories'; +import { inject } from '@loopback/core'; +import { BlueprintService } from '../services'; +import * as fs from 'fs'; +import * as multiparty from 'multiparty'; +import * as request_lib from 'request'; + +const REST_BLUEPRINT_CONTROLLER_BASE_URL = process.env.REST_BLUEPRINT_CONTROLLER_BASE_URL || "http://localhost:8080/api/v1"; +const REST_BLUEPRINT_CONTROLLER_BASIC_AUTH_HEADER = process.env.REST_BLUEPRINT_CONTROLLER_BASIC_AUTH_HEADER || "Basic Y2NzZGthcHBzOmNjc2RrYXBwcw=="; export class BlueprintRestController { constructor( - @repository(BlueprintRepository) - public blueprintRepository : BlueprintRepository, + // @repository(BlueprintRepository) + // public blueprintRepository : BlueprintRepository, + @inject('services.BlueprintService') + public bpservice: BlueprintService, ) {} - @post('/blueprints', { + @get('/blueprints', { responses: { '200': { description: 'Blueprint model instance', - content: {'application/json': {schema: {'x-ts-type': Blueprint}}}, + content: { 'application/json': { schema: { 'x-ts-type': Blueprint } } }, }, }, }) - async create(@requestBody() blueprint: Blueprint): Promise { - return await this.blueprintRepository.create(blueprint); - } + async getall() { + return await this.bpservice.getAllblueprints(REST_BLUEPRINT_CONTROLLER_BASIC_AUTH_HEADER); - @get('/blueprints/count', { - responses: { - '200': { - description: 'Blueprint model count', - content: {'application/json': {schema: CountSchema}}, - }, - }, - }) - async count( - @param.query.object('where', getWhereSchemaFor(Blueprint)) where?: Where, - ): Promise { - return await this.blueprintRepository.count(where); } - @get('/blueprints', { - responses: { - '200': { - description: 'Array of Blueprint model instances', - content: { - 'application/json': { - schema: {type: 'array', items: {'x-ts-type': Blueprint}}, - }, + @post('/create-blueprint') + async upload( + @requestBody({ + description: 'multipart/form-data value.', + required: true, + content: { + 'multipart/form-data': { + // Skip body parsing + 'x-parser': 'stream', + schema: {type: 'object'}, }, }, - }, - }) - async find( - @param.query.object('filter', getFilterSchemaFor(Blueprint)) filter?: Filter, - ): Promise { - return await this.blueprintRepository.find(filter); + }) + request: Request, + @inject(RestBindings.Http.RESPONSE) response: Response, + ): Promise { + return new Promise((resolve, reject) => { + this.getFileFromMultiPartForm(request).then(file=>{ + this.uploadFileToBlueprintController(file, "/blueprint-model/").then(resp=>{ + response.setHeader("X-ONAP-RequestID", resp.headers['x-onap-requestid']); + resolve(JSON.parse(resp.body)); + }, err=>{ + reject(err); + }); + }, err=>{ + reject(err); + }); + }); } - @patch('/blueprints', { - responses: { - '200': { - description: 'Blueprint PATCH success count', - content: {'application/json': {schema: CountSchema}}, + @post('/enrich-blueprint') + async enrich( + @requestBody({ + description: 'multipart/form-data value.', + required: true, + content: { + 'multipart/form-data': { + // Skip body parsing + 'x-parser': 'stream', + schema: {type: 'object'}, + }, }, - }, - }) - async updateAll( - @requestBody() blueprint: Blueprint, - @param.query.object('where', getWhereSchemaFor(Blueprint)) where?: Where, - ): Promise { - return await this.blueprintRepository.updateAll(blueprint, where); + }) + request: Request, + @inject(RestBindings.Http.RESPONSE) response: Response, + ): Promise { + return new Promise((resolve, reject) => { + this.getFileFromMultiPartForm(request).then(file=>{ + this.uploadFileToBlueprintController(file, "/blueprint-model/enrich/").then(resp=>{ + response.setHeader("X-ONAP-RequestID", resp.headers['x-onap-requestid']); + response.setHeader("Content-Disposition", resp.headers['content-disposition']); + resolve(resp.body); + }, err=>{ + reject(err); + }); + }, err=>{ + reject(err); + }); + }); } - @get('/blueprints/{id}', { - responses: { - '200': { - description: 'Blueprint model instance', - content: {'application/json': {schema: {'x-ts-type': Blueprint}}}, - }, - }, - }) - async findById(@param.path.number('id') id: number): Promise { - return await this.blueprintRepository.findById(id); + @get('/download-blueprint/{id}') + async download( + @param.path.string('id') id: string, + @inject(RestBindings.Http.REQUEST) request: Request, + @inject(RestBindings.Http.RESPONSE) response: Response, + ): Promise { + return new Promise((resolve, reject) => { + this.downloadFileFromBlueprintController("/blueprint-model/download/" + id).then(resp=>{ + response.setHeader("X-ONAP-RequestID", resp.headers['x-onap-requestid']); + response.setHeader("Content-Disposition", resp.headers['content-disposition']); + resolve(resp.body); + }, err=>{ + reject(err); + }); + }); } - @patch('/blueprints/{id}', { - responses: { - '204': { - description: 'Blueprint PATCH success', - }, - }, - }) - async updateById( - @param.path.number('id') id: number, - @requestBody() blueprint: Blueprint, - ): Promise { - await this.blueprintRepository.updateById(id, blueprint); + async getFileFromMultiPartForm(request: Request): Promise{ + return new Promise((resolve, reject) => { + // let options = { + // uploadDir: MULTIPART_FORM_UPLOAD_DIR + // } + let form = new multiparty.Form(); + form.parse(request, (err: any, fields: any, files: { [x: string]: any[]; }) => { + if (err) reject(err); + let file = files['file'][0]; // get the file from the returned files object + if(!file){ + reject('File was not found in form data.'); + }else{ + resolve(file); + } + }); + }) } - @put('/blueprints/{id}', { - responses: { - '204': { - description: 'Blueprint PUT success', + async uploadFileToBlueprintController(file: any, uri: string): Promise{ + let options = { + url: REST_BLUEPRINT_CONTROLLER_BASE_URL + uri, + headers: { + Authorization: REST_BLUEPRINT_CONTROLLER_BASIC_AUTH_HEADER, + 'content-type': 'multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' }, - }, - }) - async replaceById( - @param.path.number('id') id: number, - @requestBody() blueprint: Blueprint, - ): Promise { - await this.blueprintRepository.replaceById(id, blueprint); - } + formData: { + file: { + value: fs.createReadStream(file.path), + options: { + filename: 'cba.zip', + contentType: 'application/zip' + } + } + } + }; - @del('/blueprints/{id}', { - responses: { - '204': { - description: 'Blueprint DELETE success', - }, - }, - }) - async deleteById(@param.path.number('id') id: number): Promise { - await this.blueprintRepository.deleteById(id); + return new Promise((resolve, reject) => { + request_lib.post(options, (err: any, resp: any, body: any) => { + if (err) { + //delete tmp file + fs.unlink(file.path, (err: any) => { + if (err) { + console.error(err); + return + } + }) + reject(err); + }else{ + resolve(resp); + } + }) + }) } + + async downloadFileFromBlueprintController(uri: string): Promise { + let options = { + url: REST_BLUEPRINT_CONTROLLER_BASE_URL + uri, + headers: { + Authorization: REST_BLUEPRINT_CONTROLLER_BASIC_AUTH_HEADER, + } + }; + + return new Promise((resolve, reject) => { + request_lib.get(options, (err: any, resp: any, body: any) => { + if (err) { + reject(err); + }else{ + resolve(resp); + } + }) + }) } +} \ No newline at end of file diff --git a/cds-ui/server/src/datasources/blueprint.datasource.json b/cds-ui/server/src/datasources/blueprint.datasource.json new file mode 100644 index 000000000..86b1c2c15 --- /dev/null +++ b/cds-ui/server/src/datasources/blueprint.datasource.json @@ -0,0 +1,22 @@ +{ + "name": "blueprint", + "connector": "rest", + "baseURL": "http://localhost:8080/api/v1/", + "crud": false, + "operations": [{ + "template": { + "method": "GET", + "url": "http://localhost:8080/api/v1/blueprint-model/", + "headers": { + "accepts": "application/json", + "content-type": "application/json", + "authorization": "{authtoken}" + }, + "responsePath": "$.*" + }, + "functions": { + "getAllblueprints": ["authtoken"] + + } + }] +} \ No newline at end of file diff --git a/cds-ui/server/src/datasources/blueprint.datasource.ts b/cds-ui/server/src/datasources/blueprint.datasource.ts new file mode 100644 index 000000000..008612693 --- /dev/null +++ b/cds-ui/server/src/datasources/blueprint.datasource.ts @@ -0,0 +1,14 @@ +import {inject} from '@loopback/core'; +import {juggler} from '@loopback/repository'; +import * as config from './blueprint.datasource.json'; + +export class BlueprintDataSource extends juggler.DataSource { + static dataSourceName = 'blueprint'; + + constructor( + @inject('datasources.config.blueprint', {optional: true}) + dsConfig: object = config, + ) { + super(dsConfig); + } +} diff --git a/cds-ui/server/src/datasources/index.ts b/cds-ui/server/src/datasources/index.ts index 1726b4515..f7a934cd8 100644 --- a/cds-ui/server/src/datasources/index.ts +++ b/cds-ui/server/src/datasources/index.ts @@ -21,3 +21,4 @@ limitations under the License. export * from './db.datasource'; +export * from './blueprint.datasource'; diff --git a/cds-ui/server/src/datasources/rest.config.json b/cds-ui/server/src/datasources/rest.config.json deleted file mode 100644 index 86f33fbd4..000000000 --- a/cds-ui/server/src/datasources/rest.config.json +++ /dev/null @@ -1,39 +0,0 @@ -{ - "name": "restConfig", - "connector": "rest", - "options": { - "headers": { - "accept": "application/json", - "content-type": "application/json" - } - }, - "operations": [ - { - "template": { - "method": "GET", - "url": "" - }, - "functions": { - "getEnricheddata": [""] - } - }, - { - "template": { - "method": "POST", - "url": "" - }, - "functions": { - "saveBlueprint": [""] - } - }, - { - "template": { - "method": "POST", - "url": "" - }, - "functions": { - "test": [""] - } - } -] -} \ No newline at end of file diff --git a/cds-ui/server/src/datasources/rest.datasource.json b/cds-ui/server/src/datasources/rest.datasource.json new file mode 100644 index 000000000..86f33fbd4 --- /dev/null +++ b/cds-ui/server/src/datasources/rest.datasource.json @@ -0,0 +1,39 @@ +{ + "name": "restConfig", + "connector": "rest", + "options": { + "headers": { + "accept": "application/json", + "content-type": "application/json" + } + }, + "operations": [ + { + "template": { + "method": "GET", + "url": "" + }, + "functions": { + "getEnricheddata": [""] + } + }, + { + "template": { + "method": "POST", + "url": "" + }, + "functions": { + "saveBlueprint": [""] + } + }, + { + "template": { + "method": "POST", + "url": "" + }, + "functions": { + "test": [""] + } + } +] +} \ No newline at end of file diff --git a/cds-ui/server/src/datasources/rest.datasource.ts b/cds-ui/server/src/datasources/rest.datasource.ts index 42cdb8018..1b5a44d79 100644 --- a/cds-ui/server/src/datasources/rest.datasource.ts +++ b/cds-ui/server/src/datasources/rest.datasource.ts @@ -21,7 +21,7 @@ limitations under the License. import { juggler } from '@loopback/service-proxy'; -import * as config from './rest.config.json'; +import * as config from './rest.datasource.json'; export class RestDataSource extends juggler.DataSource { diff --git a/cds-ui/server/src/services/blueprint.service.ts b/cds-ui/server/src/services/blueprint.service.ts new file mode 100644 index 000000000..f48253652 --- /dev/null +++ b/cds-ui/server/src/services/blueprint.service.ts @@ -0,0 +1,19 @@ +import {getService} from '@loopback/service-proxy'; +import {inject, Provider} from '@loopback/core'; +import {BlueprintDataSource} from '../datasources'; + +export interface BlueprintService { + getAllblueprints(authtoken: string): Promise; +} + +export class BlueprintServiceProvider implements Provider { + constructor( + // blueprint must match the name property in the datasource json file + @inject('datasources.blueprint') + protected dataSource: BlueprintDataSource = new BlueprintDataSource(), + ) {} + + value(): Promise { + return getService(this.dataSource); + } +} \ No newline at end of file diff --git a/cds-ui/server/src/services/index.ts b/cds-ui/server/src/services/index.ts new file mode 100644 index 000000000..26b01ed0b --- /dev/null +++ b/cds-ui/server/src/services/index.ts @@ -0,0 +1 @@ +export * from './blueprint.service'; -- cgit 1.2.3-korg