aboutsummaryrefslogtreecommitdiffstats
path: root/sdc-workflow-designer-ui/src/app/components/property/rest-task/rest-task.component.ts
blob: 3def276fa27807e6ac68b27df7116e6de8febc7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*******************************************************************************
 * Copyright (c) 2017 ZTE Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and the Apache License 2.0 which both accompany this distribution,
 * and are available at http://www.eclipse.org/legal/epl-v10.html
 * and http://www.apache.org/licenses/LICENSE-2.0
 *
 * Contributors:
 *     ZTE - initial API and implementation and/or initial documentation
 *******************************************************************************/
import { Component, Input, OnInit } from '@angular/core';
import { Subscription } from 'rxjs/Subscription';
import { TranslateService } from '@ngx-translate/core';

import { PlanTreeviewItem } from '../../../model/plan-treeview-item';
import { ValueSource } from '../../../model/value-source.enum';
import { ValueType } from '../../../model/value-type.enum';
import { RestParameter } from '../../../model/workflow/rest-parameter';
import { RestTask } from '../../../model/workflow/rest-task';
import { BroadcastService } from '../../../services/broadcast.service';
import { NoticeService } from '../../../services/notice.service';
import { RestService } from '../../../services/rest.service';
import { WorkflowUtil } from '../../../util/workflow-util';
import { NodeTypeService } from '../../../services/node-type.service';
import { NodeDataType } from '../../../model/node-data-type/node-data-type';
import { SwaggerBaseParameter } from "../../../model/workflow/swagger/swagger-base-parameter";
import { SwaggerResponse } from "../../../model/workflow/swagger/swagger-response";
import { Swagger, SwaggerMethod } from '../../../model/swagger';
import { SwaggerBodyParameter } from '../../../model/workflow/swagger/swagger-body-parameter';
import { SwaggerNormalParameter } from '../../../model/workflow/swagger/swagger-normal-parameter';
import { SwaggerSchema } from '../../../model/workflow/swagger/swagger-schema';
import { SwaggerIn } from '../../../model/workflow/swagger/swagger-in.enum';

@Component({
    selector: 'wfm-rest-task',
    templateUrl: 'rest-task.component.html',
})
export class RestTaskComponent implements OnInit {
    @Input() public node: RestTask;
    @Input() public planItems: PlanTreeviewItem[];

    public swaggerJson: any = {};
    public restInterfaces = [];
    public restOperations = [];
    public loadSwaggerByMSB = true;
    // public dataTypeInput: SwaggerBaseParameter[] = [];
    public dataTypeOutput: SwaggerResponse[] = [];
    public definitions: SwaggerSchema;
    private swagger: Swagger;

    constructor(private nodeTypeService: NodeTypeService, private broadcastService: BroadcastService, public restService: RestService,
        private noticeService: NoticeService, private translate: TranslateService) { }

    public ngOnInit() {
        const nodeDataType = this.nodeTypeService.getNodeDataTypeById(this.node.typeId);
        // if (nodeDataType.content && nodeDataType.content.baseUrl && nodeDataType.content.serviceName && nodeDataType.content.version
        //     && nodeDataType.content.path && nodeDataType.content.method && nodeDataType.content.consumes) {
        if (nodeDataType && nodeDataType.content && nodeDataType.content.baseUrl && nodeDataType.content.serviceName
            && nodeDataType.content.serviceVersion && nodeDataType.content.path && nodeDataType.content.method) {
            this.loadSwaggerByMSB = false;
        }
        if (this.loadSwaggerByMSB) {
            this.loadInterfaces();
        } else {
            this.setParametersByDataType(nodeDataType);
        }
    }

    public serviceChanged(configId: string) {
        this.node.restConfigId = configId;
        this.pathChanged('');
        this.loadInterfaces();
    }

    public pathChanged(path: string) {
        this.node.path = path;
        this.node.consumes = [];
        this.node.produces = [];
        this.methodChanged('');
        this.loadOperations();
    }

    public methodChanged(method: string) {
        this.node.method = method;
        this.node.parameters = [];
        this.node.responses = [];
        this.updateMethodInfo();
    }

    private loadInterfaces(): void {
        if (this.node.restConfigId) {
            // this.swagger = this.restService.getSwaggerInfo(this.node.restConfigId);
            let restConfig = this.restService.getRestConfig(this.node.restConfigId);
            this.node.baseUrl = restConfig.url;
            this.node.serviceName = restConfig.name;
            this.node.serviceVersion = restConfig.version;

            this.swagger = restConfig.swagger;
            if (this.swagger) {
                this.restInterfaces = [];
                for (const key of Object.keys(this.swagger.paths)) {
                    this.restInterfaces.push(key);
                }
                this.loadOperations();
            } else {
                this.translate.get('WORKFLOW.MSG.SWAGGER_NOT_EXISTS').subscribe((res: string) => {
                    this.noticeService.error(res);
                });
            }
        }
    }

    private loadOperations(): void {
        if (this.node.path) {
            const swaggerPath: any = this.swagger.paths[this.node.path];

            this.restOperations = [];
            for (const key of Object.keys(swaggerPath)) {
                this.restOperations.push(key);
            }
        }
    }

    private updateMethodInfo(): void {
        if (this.node.method) {
            const path: any = this.swagger.paths[this.node.path];
            const method: SwaggerMethod = path[this.node.method];

            this.node.consumes = WorkflowUtil.deepClone(method.consumes);
            this.node.produces = WorkflowUtil.deepClone(method.produces);

            let tempParameters: RestParameter[] = [];
            method.parameters.forEach(param => {
                let defaultType = SwaggerIn[SwaggerIn.body] === param.position ? ValueType[ValueType.object] : ValueType[ValueType.string];
                const type = param.type ? param.type : defaultType;
                const nodeParam = new RestParameter(param.name, undefined, ValueSource[ValueSource.string],
                    type, param.position, param.schema, param.required);
                tempParameters.push(WorkflowUtil.deepClone(nodeParam));
            });
            this.node.parameters = tempParameters;

            const responseParams = this.restService.getResponseParameters(
                this.swagger, this.node.path, this.node.method);
            this.node.responses = responseParams.map(param => WorkflowUtil.deepClone(param));
        }
    }

    private setParametersByDataType(nodeDataType: NodeDataType): void {
        this.node.serviceName = nodeDataType.content.serviceName;
        this.node.serviceVersion = nodeDataType.content.serviceVersion;
        this.node.restConfigId = this.node.serviceName;
        if (this.node.serviceVersion) {
            this.node.restConfigId += ('.' + this.node.serviceVersion);
        }
        this.node.baseUrl = nodeDataType.content.baseUrl;
        this.node.path = nodeDataType.content.path;
        this.node.method = nodeDataType.content.method;
        this.node.consumes = nodeDataType.content.consumes;
        this.node.produces = nodeDataType.content.produces;
        this.definitions = nodeDataType.definitions;
        if (nodeDataType.content.inputs) {
            for (const key in nodeDataType.content.inputs) {
                if (nodeDataType.content.inputs.hasOwnProperty(key)) {
                    // Set default value of dataType
                    const element = nodeDataType.content.inputs[key];
                    let param: SwaggerBaseParameter = this.getParameterByDataType(element, key);
                    if (param) {
                        // Set exist value
                        let found = false;
                        if (this.node.parameters) {
                            for (let p = 0; p < this.node.parameters.length; p++) {
                                if (param.name === this.node.parameters[p].name) {
                                    found = true;
                                    let value = this.node.parameters[p].value;
                                    let valueSource = this.node.parameters[p].valueSource;
                                    param.value = value;
                                    param.valueSource = valueSource;
                                    this.node.parameters[p] = param;
                                    break;
                                }
                            }
                        } else {
                            this.node.parameters = [];
                        }
                        if (!found) {
                            this.node.parameters.push(param);
                        }
                    }
                }
            }
        }
        if (nodeDataType.content.outputs) {
            for (const key in nodeDataType.content.outputs) {
                if (nodeDataType.content.outputs.hasOwnProperty(key)) {
                    // Set default value of dataType
                    const element = nodeDataType.content.outputs[key];
                    this.dataTypeOutput.push(this.getResponseByDataType(element, key));
                }
            }
        }
    }

    private getParameterByDataType(dataTypeParameter: SwaggerBaseParameter, name: string): SwaggerBaseParameter {
        if (!dataTypeParameter.name) {
            dataTypeParameter.name = name;
        }
        if (SwaggerIn[SwaggerIn.body] === dataTypeParameter.in) {
            return this.initBodyParam(dataTypeParameter as SwaggerBodyParameter);
        } else {
            return this.initNormalParam(dataTypeParameter as SwaggerNormalParameter);
        }
    }

    private getResponseByDataType(dataTypeResponse: SwaggerResponse, name: string): SwaggerResponse {
        let responseParam = dataTypeResponse;
        if (!responseParam.name) {
            responseParam.name = name;
        }
        return this.initResponseParam(responseParam);
    }

    private initNormalParam(normalParam: SwaggerNormalParameter): SwaggerNormalParameter {
        let normal = WorkflowUtil.deepClone(normalParam);
        // Set default value of dataType
        if (undefined === normalParam.show) {
            normal.show = true;
        }
        if ('path' === normalParam.in) {
            normal.required = true;
        } else if (undefined === normalParam.required) {
            normal.required = false;
        }
        if (undefined === normalParam.allowEmptyValue) {
            normal.allowEmptyValue = false;
        }
        if (undefined === normalParam.collectionFormat) {
            normal.collectionFormat = 'csv';
        }
        if (undefined === normalParam.type) {
            normal.type == ValueType[ValueType.string];
        }
        // editable not support
        return normal;
    }

    private initBodyParam(bodyParam: SwaggerBodyParameter): SwaggerBodyParameter {
        let body = WorkflowUtil.deepClone(bodyParam);
        // Set default value of dataType
        if (undefined === bodyParam.show) {
            body.show = true;
        }
        if (undefined === bodyParam.required) {
            body.required = false;
        }
        if (undefined === bodyParam.valueSource) {
            body.valueSource = ValueSource[ValueSource.Definition];
        }
        if (undefined === bodyParam.schema.type) {
            body.schema.type == ValueType[ValueType.string];
        }
        // $ref not support
        if (bodyParam.$ref) {
            console.warn('Do not support body parameter $ref.');
        }
        return body;
    }

    private initResponseParam(responseParam: SwaggerResponse): SwaggerResponse {
        let response = responseParam;
        return response;
    }
}