7fa9675f9b8767d2057b553572b983ed9b89614d
[sdc/sdc-workflow-designer.git] /
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 import { AfterViewInit, Component, Input } from '@angular/core';
13 import { Subscription } from '../../../../../node_modules/rxjs/Subscription.d';
14
15 import { PlanTreeviewItem } from '../../../model/plan-treeview-item';
16 import { NodeTemplate } from '../../../model/topology/node-template';
17 import { ValueSource } from '../../../model/value-source.enum';
18 import { Parameter } from '../../../model/workflow/parameter';
19 import { ToscaNodeTask } from '../../../model/workflow/tosca-node-task';
20 import { BroadcastService } from '../../../services/broadcast.service';
21 import { ToscaService } from '../../../services/tosca.service';
22
23 /**
24  * node template component provides operations about tosca modules which saved in winery.
25  * This component will be used in the property component while the corresponding workflow node is calling the node template's operation
26  */
27 @Component({
28     selector: 'wfm-node-template',
29     templateUrl: 'node-template.component.html',
30 })
31 export class NodeTemplateComponent implements AfterViewInit {
32     @Input() public node: ToscaNodeTask;
33     @Input() public planItems: PlanTreeviewItem[];
34
35     public inputSources: ValueSource[] = [ValueSource.string, ValueSource.Variable, ValueSource.Topology, ValueSource.Plan];
36     public outputSources: ValueSource[] = [ValueSource.Topology, ValueSource.Plan];
37     public nodeInterfaces: string[] = [];
38     public nodeOperations: any[] = [];
39     public nodeTemplates: NodeTemplate[] = [];
40
41     constructor(private toscaService: ToscaService) {
42     }
43
44     public ngAfterViewInit() {
45         this.nodeTemplates = this.toscaService.getNodeTemplate();
46
47         this.loadInterfaces();
48         this.loadOperations();
49     }
50
51     public nodeTemplateChanged() {
52         this.setTemplateNamespace();
53
54         this.nodeInterfaceChanged('');
55
56         this.loadInterfaces();
57     }
58
59     public nodeInterfaceChanged(newInterface: string) {
60         this.node.nodeInterface = newInterface;
61
62         this.nodeOperationChanged('');
63
64         this.loadOperations();
65     }
66
67     public nodeOperationChanged(operation: string) {
68         this.node.operation = operation;
69
70         this.node.input = [];
71         this.node.output = [];
72
73         this.loadParameters();
74     }
75
76     private setTemplateNamespace() {
77         const nodeTemplate = this.nodeTemplates.find(
78             tmpNodeTemplate => tmpNodeTemplate.id === this.node.template.id);
79
80         if (nodeTemplate) {
81             this.node.template.namespace = nodeTemplate.namespace;
82             this.node.template.type = nodeTemplate.type;
83         }
84     }
85
86     private loadInterfaces() {
87         if (this.node.template.id) {
88             this.toscaService.loadNodeTemplateInterfaces(this.node.template)
89                 .subscribe(interfaces => {
90                     this.nodeInterfaces = interfaces;
91                 });
92         } else {
93             this.nodeInterfaces = [];
94         }
95     }
96
97     private loadOperations() {
98         if (this.node.nodeInterface) {
99             this.nodeOperations = [];
100             this.toscaService.loadNodeTemplateOperations(
101                 this.node.template,
102                 this.node.nodeInterface)
103                 .subscribe(operations => this.nodeOperations = operations);
104         } else {
105             this.nodeOperations = [];
106         }
107     }
108
109     private loadParameters() {
110         if (this.node.operation) {
111             this.toscaService.loadNodeTemplateOperationParameter(
112                 this.node.template,
113                 this.node.nodeInterface,
114                 this.node.operation)
115                 .subscribe(params => {
116                     this.node.input = [];
117                     this.node.output = [];
118
119                     params.input.forEach(param => {
120                         const p = new Parameter(param, '', ValueSource[ValueSource.string]);
121                         this.node.input.push(p);
122                     });
123
124                     params.output.forEach(param => {
125                         const p = new Parameter(param, '', ValueSource[ValueSource.Definition]);
126                         this.node.output.push(p);
127                     });
128                 });
129         }
130     }
131 }