support swagger for microservice definition
[sdc/sdc-workflow-designer.git] / sdc-workflow-designer-ui / src / app / components / menu / microservice / microservice-detail / microservice-detail.component.ts
1 /**
2  * Copyright (c) 2017 ZTE Corporation.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * and the Apache License 2.0 which both accompany this distribution,
6  * and are available at http://www.eclipse.org/legal/epl-v10.html
7  * and http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Contributors:
10  *     ZTE - initial API and implementation and/or initial documentation
11  */
12
13 import { Component, Input, OnChanges, ViewChild } from '@angular/core';
14 import { ModalDirective } from 'ngx-bootstrap/modal';
15
16 import { Microservice } from '../../../../model/workflow/microservice';
17 import { WorkflowConfigService } from '../../../../services/workflow-config.service';
18 import { Swagger } from "../../../../model/swagger";
19
20 /**
21  * toolbar component contains some basic operations(save) and all of the supported workflow nodes.
22  * The supported nodes can be dragged to container component. which will add a new node to the workflow.
23  */
24 @Component({
25     selector: 'b4t-microservice-detail',
26     templateUrl: 'microservice-detail.component.html',
27 })
28 export class MicroserviceDetailComponent implements OnChanges {
29     @Input() microservice: Microservice;
30
31     public detail: string;
32     public dynamic = false;
33
34     constructor(private configService: WorkflowConfigService) {
35     }
36
37     public ngOnChanges() {
38         if(this.microservice == null) {
39             this.microservice = new Microservice('', '', null, '');
40         }
41         this.dynamic = this.microservice.definition !== '';
42         this.parseSwagger2String();
43     }
44
45     private parseSwagger2String() {
46         if (this.microservice.swagger) {
47             this.detail = JSON.stringify(this.microservice.swagger);
48         } else {
49             this.detail = '';
50         }
51     }
52
53     public onDetailChanged(detail: string) {
54         try {
55             if(detail) {
56                 const swagger = new Swagger(JSON.parse(detail));
57                 this.detail = detail;
58                 console.log(swagger);
59                 this.microservice.swagger = swagger;
60             } else {
61                 this.detail = '';
62                 this.microservice.swagger = null;
63             }
64         } catch (e) {
65             // if detail is not a json object, then not change the swagger
66         }
67     }
68
69     public toggleDynamic(dynamic: boolean) {
70         this.dynamic = dynamic;
71         this.onDetailChanged(null);
72
73         if(!dynamic) {
74             this.microservice.definition = null;
75         }
76     }
77
78     private loadDynamicInfo() {
79         this.configService.loadDynamicInfo(this.microservice.definition)
80         .subscribe(response => {
81             try {
82                 this.microservice.swagger = new Swagger(response);
83                 this.parseSwagger2String();
84             } catch (e) {
85                 console.log('detail transfer error');
86                 console.error(e);
87             }
88         });
89     }
90 }