792d9cba6050722bdcfe4e3064b5c30a310750c8
[sdc.git] / catalog-ui / src / app / ng2 / pages / properties-assignment / tosca-function / tosca-function.component.ts
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 import {Component, EventEmitter, Input, OnChanges, OnInit, Output, SimpleChanges} from '@angular/core';
21 import {ComponentMetadata, PropertyBEModel, PropertyDeclareAPIModel, DerivedFEProperty} from 'app/models';
22 import {TopologyTemplateService} from "../../../services/component-services/topology-template.service";
23 import {WorkspaceService} from "../../workspace/workspace.service";
24 import {ToscaGetFunctionType} from "../../../../models/tosca-get-function-type";
25 import {InstanceFeDetails} from "../../../../models/instance-fe-details";
26 import {ToscaGetFunction} from "../../../../models/tosca-get-function";
27 import {FormControl, FormGroup, Validators} from "@angular/forms";
28 import {ToscaFunctionType} from "../../../../models/tosca-function-type.enum";
29 import {ToscaGetFunctionValidationEvent} from "./tosca-get-function/tosca-get-function.component";
30 import {ToscaFunction} from "../../../../models/tosca-function";
31 import {ToscaConcatFunctionValidationEvent} from "./tosca-concat-function/tosca-concat-function.component";
32 import {ToscaCustomFunctionValidationEvent} from "./tosca-custom-function/tosca-custom-function.component";
33 import {PROPERTY_TYPES} from "../../../../utils/constants";
34 import {YamlFunctionValidationEvent} from "./yaml-function/yaml-function.component";
35 import {ToscaConcatFunction} from "../../../../models/tosca-concat-function";
36 import {ToscaCustomFunction} from "../../../../models/tosca-custom-function";
37 import {YamlFunction} from "../../../../models/yaml-function";
38 import {CustomToscaFunction} from "../../../../models/default-custom-functions";
39
40 @Component({
41     selector: 'tosca-function',
42     templateUrl: './tosca-function.component.html',
43     styleUrls: ['./tosca-function.component.less'],
44 })
45 export class ToscaFunctionComponent implements OnInit, OnChanges {
46
47     @Input() property: PropertyBEModel;
48     @Input() overridingType: PROPERTY_TYPES;
49     @Input() inToscaFunction: ToscaFunction;
50     @Input() componentInstanceMap: Map<string, InstanceFeDetails> = new Map<string, InstanceFeDetails>();
51     @Input() customToscaFunctions: Array<CustomToscaFunction> = [];
52     @Input() allowClear: boolean = true;
53     @Input() compositionMap: boolean = false;
54     @Input() compositionMapKey: string = "";
55     @Output() onValidFunction: EventEmitter<ToscaGetFunction> = new EventEmitter<ToscaGetFunction>();
56     @Output() onValidityChange: EventEmitter<ToscaFunctionValidationEvent> = new EventEmitter<ToscaFunctionValidationEvent>();
57
58     toscaFunctionForm: FormControl = new FormControl(undefined, [Validators.required]);
59     toscaFunctionTypeForm: FormControl = new FormControl(undefined, Validators.required);
60     formGroup: FormGroup = new FormGroup({
61         'toscaFunction': this.toscaFunctionForm,
62         'toscaFunctionType': this.toscaFunctionTypeForm,
63     });
64
65     isLoading: boolean = false;
66     toscaFunction: ToscaFunction;
67     toscaFunctions: Array<string> = [];
68     toscaCustomFunctions: Array<String> = [];
69
70     private isInitialized: boolean = false;
71     private componentMetadata: ComponentMetadata;
72
73     constructor(private topologyTemplateService: TopologyTemplateService,
74                 private workspaceService: WorkspaceService) {
75     }
76
77     ngOnInit(): void {
78         this.componentMetadata = this.workspaceService.metadata;
79         this.toscaFunction = this.inToscaFunction ? this.inToscaFunction : this.property.toscaFunction ? this.property.toscaFunction : undefined;
80         this.loadToscaFunctions();
81         this.formGroup.valueChanges.subscribe(() => {
82             if (!this.isInitialized) {
83                 return;
84             }
85             if (this.formGroup.valid) {
86                 this.onValidFunction.emit(this.toscaFunctionForm.value);
87             }
88         });
89         this.initToscaFunction();
90         this.emitValidityChange();
91         this.isInitialized = true;
92     }
93
94     ngOnChanges(changes: SimpleChanges): void {
95         if (changes.property) {
96             this.resetForm();
97             this.toscaFunction = this.inToscaFunction ? this.inToscaFunction : this.property.toscaFunction ? this.property.toscaFunction : undefined;
98             this.initToscaFunction();
99             this.loadToscaFunctions();
100             this.emitValidityChange();
101         }
102     }
103
104     private validate(): boolean {
105         return (!this.toscaFunctionForm.value && !this.toscaFunctionTypeForm.value) || this.formGroup.valid;
106     }
107
108     private initToscaFunction(): void {
109         if (this.compositionMap && this.property.subPropertyToscaFunctions) {
110             let keyToFind = [this.compositionMapKey];
111             let subPropertyToscaFunction = this.property.subPropertyToscaFunctions.find(subPropertyToscaFunction => this.areEqual(subPropertyToscaFunction.subPropertyPath, keyToFind));
112
113             if (subPropertyToscaFunction){
114                 this.toscaFunction = subPropertyToscaFunction.toscaFunction;
115                 this.toscaFunctionForm.setValue(this.toscaFunction);
116                 this.toscaFunctionTypeForm.setValue(this.toscaFunction.type);
117             }
118             return;
119         }
120             if (this.property instanceof PropertyDeclareAPIModel && this.property.subPropertyToscaFunctions && (<PropertyDeclareAPIModel> this.property).propertiesName){
121                 let propertiesPath = (<PropertyDeclareAPIModel> this.property).propertiesName.split("#");
122             if (propertiesPath.length > 1){
123                 let keyToFind = (<DerivedFEProperty>this.property.input).toscaPath;
124                 let subPropertyToscaFunction = this.property.subPropertyToscaFunctions.find(subPropertyToscaFunction => this.areEqual(subPropertyToscaFunction.subPropertyPath, keyToFind.length > 0 ? keyToFind : propertiesPath.slice(1)));
125
126                 if (subPropertyToscaFunction){
127                         this.toscaFunction = subPropertyToscaFunction.toscaFunction;
128                     this.toscaFunctionForm.setValue(this.toscaFunction);
129                     this.toscaFunctionTypeForm.setValue(this.toscaFunction.type);
130                 }
131                 return;
132             }
133         }
134         if (!this.property.isToscaFunction()) {
135             return;
136         }
137
138         this.toscaFunctionForm.setValue(this.inToscaFunction ? this.inToscaFunction : this.property.toscaFunction);
139         let type = this.property.toscaFunction.type;
140         if (type == ToscaFunctionType.CUSTOM) {
141             let name = (this.property.toscaFunction as ToscaCustomFunction).name;
142             let customToscaFunc = this.customToscaFunctions.find(custToscFunc => _.isEqual(custToscFunc.name, name))
143             if (customToscaFunc) {
144                 this.toscaFunctionTypeForm.setValue(name);
145             } else {
146                 this.toscaFunctionTypeForm.setValue("other");
147             }
148         } else {
149             this.toscaFunctionTypeForm.setValue(this.inToscaFunction ? this.inToscaFunction.type : type);
150         }
151     }
152
153     private areEqual(array1: string[], array2: string[]): boolean {
154             return array1.length === array2.length && array1.every(function(value, index) { return value === array2[index]})
155     }
156
157     private loadToscaFunctions(): void {
158         this.toscaFunctions = [];
159         this.toscaFunctions.push(ToscaFunctionType.GET_ATTRIBUTE);
160         this.toscaFunctions.push(ToscaFunctionType.GET_INPUT);
161         this.toscaFunctions.push(ToscaFunctionType.GET_PROPERTY);
162         if ((this.property.type === PROPERTY_TYPES.STRING || this.property.type === PROPERTY_TYPES.ANY) && this.overridingType === undefined) {
163             this.toscaFunctions.push(ToscaFunctionType.CONCAT);
164         }
165         this.loadCustomToscaFunctions();
166     }
167
168     private loadCustomToscaFunctions(): void {
169         if (!this.customToscaFunctions.find(custToscFunc => _.isEqual(custToscFunc.name, "other"))) {
170             let other = new CustomToscaFunction();
171             other.name = "other";
172             other.type = ToscaFunctionType.CUSTOM;
173             this.customToscaFunctions.push(other);
174         }
175         this.toscaCustomFunctions = [];
176         for (let func of this.customToscaFunctions) {
177             this.toscaCustomFunctions.push(func.name);
178         }
179     }
180
181     getCustomToscaFunction(): CustomToscaFunction {
182         let funcName = this.formGroup.get('toscaFunctionType').value;
183         return this.customToscaFunctions.find(custToscFunc => _.isEqual(custToscFunc.name, funcName));
184     }
185
186     getCustomFunctionName():string {
187         let toscaFunctionType: CustomToscaFunction = this.getCustomToscaFunction();
188         let name = toscaFunctionType.name;
189         return name == 'other' ? '' : name;
190     }
191
192     getCustomFunctionType():string {
193         let toscaFunctionType: CustomToscaFunction = this.getCustomToscaFunction();
194         return toscaFunctionType.type;
195     }
196
197     isDefaultCustomFunction(): boolean {
198         let toscaFunctionType: CustomToscaFunction = this.getCustomToscaFunction();
199         if (toscaFunctionType.name === "other") {
200             return false;
201         }
202         return this.customToscaFunctions.filter(e => e.name === toscaFunctionType.name).length > 0;
203     }
204
205     private resetForm(): void {
206         this.formGroup.reset();
207         this.toscaFunction = undefined;
208     }
209
210     private isGetPropertySelected(): boolean {
211         return this.formGroup.get('toscaFunctionType').value === ToscaGetFunctionType.GET_PROPERTY;
212     }
213
214     private isGetAttributeSelected(): boolean {
215         return this.formGroup.get('toscaFunctionType').value === ToscaGetFunctionType.GET_ATTRIBUTE;
216     }
217
218     private isGetInputSelected(): boolean {
219         return this.formGroup.get('toscaFunctionType').value === ToscaGetFunctionType.GET_INPUT;
220     }
221
222     isConcatSelected(): boolean {
223         return this.formGroup.get('toscaFunctionType').value === ToscaFunctionType.CONCAT;
224     }
225
226     isCustomSelected(): boolean {
227         let toscaFunctionType: CustomToscaFunction = this.getCustomToscaFunction();
228         return toscaFunctionType && (toscaFunctionType.type === ToscaFunctionType.CUSTOM || toscaFunctionType.type === ToscaFunctionType.GET_INPUT);
229     }
230
231     isGetFunctionSelected(): boolean {
232         return this.isGetInputSelected() || this.isGetPropertySelected() || this.isGetAttributeSelected();
233     }
234
235     isYamlFunctionSelected(): boolean {
236         return this.formGroup.get('toscaFunctionType').value === ToscaFunctionType.YAML;
237     }
238
239     onClearValues(): void {
240         this.resetForm();
241     }
242
243     showClearButton(): boolean {
244         return this.allowClear && this.toscaFunctionTypeForm.value;
245     }
246
247     onConcatFunctionValidityChange(validationEvent: ToscaConcatFunctionValidationEvent): void {
248         if (validationEvent.isValid) {
249             this.toscaFunctionForm.setValue(validationEvent.toscaConcatFunction);
250         } else {
251             this.toscaFunctionForm.setValue(undefined);
252         }
253     }
254
255     onCustomFunctionValidityChange(validationEvent: ToscaCustomFunctionValidationEvent): void {
256         if (validationEvent.isValid) {
257             this.toscaFunctionForm.setValue(validationEvent.toscaCustomFunction);
258         } else {
259             this.toscaFunctionForm.setValue(undefined);
260         }
261     }
262
263     onGetFunctionValidityChange(validationEvent: ToscaGetFunctionValidationEvent): void {
264         if (validationEvent.isValid) {
265             this.toscaFunctionForm.setValue(validationEvent.toscaGetFunction);
266         } else {
267             this.toscaFunctionForm.setValue(undefined);
268         }
269         this.emitValidityChange();
270     }
271
272     onYamlFunctionValidityChange(validationEvent: YamlFunctionValidationEvent): void {
273         if (validationEvent.isValid) {
274             this.toscaFunctionForm.setValue(validationEvent.value);
275         } else {
276             this.toscaFunctionForm.setValue(undefined);
277         }
278     }
279
280     onFunctionTypeChange(): void {
281         this.toscaFunction = undefined;
282         this.toscaFunctionForm.reset();
283     }
284
285     private emitValidityChange(): void {
286         const isValid: boolean = this.validate();
287         this.onValidityChange.emit({
288             isValid: isValid,
289             toscaFunction: isValid ? this.buildFunctionFromForm() : undefined
290         });
291     }
292
293     private buildFunctionFromForm(): ToscaFunction {
294         if (!this.toscaFunctionTypeForm.value) {
295             return undefined;
296         }
297         if (this.isConcatSelected()) {
298             return new ToscaConcatFunction(this.toscaFunctionForm.value);
299         }
300         if (this.isCustomSelected()) {
301             return new ToscaCustomFunction(this.toscaFunctionForm.value);
302         }
303         if (this.isGetFunctionSelected()) {
304             return new ToscaGetFunction(this.toscaFunctionForm.value);
305         }
306         if (this.isYamlFunctionSelected()) {
307             return new YamlFunction(this.toscaFunctionForm.value);
308         }
309
310         console.error(`Function ${this.toscaFunctionTypeForm.value} not supported`);
311     }
312 }
313
314 export class ToscaFunctionValidationEvent {
315     isValid: boolean;
316     toscaFunction: ToscaFunction;
317 }