UI support for default custom function names with get_input structure
[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             this.emitValidityChange();
86             if (this.formGroup.valid) {
87                 this.onValidFunction.emit(this.toscaFunctionForm.value);
88             }
89         });
90         this.initToscaFunction();
91         this.emitValidityChange();
92         this.isInitialized = true;
93     }
94
95     ngOnChanges(changes: SimpleChanges): void {
96         if (changes.property) {
97             this.resetForm();
98             this.toscaFunction = this.inToscaFunction ? this.inToscaFunction : this.property.toscaFunction ? this.property.toscaFunction : undefined;
99             this.initToscaFunction();
100             this.loadToscaFunctions();
101             this.emitValidityChange();
102         }
103     }
104
105     private validate(): boolean {
106         return (!this.toscaFunctionForm.value && !this.toscaFunctionTypeForm.value) || this.formGroup.valid;
107     }
108
109     private initToscaFunction(): void {
110         if (this.compositionMap && this.property.subPropertyToscaFunctions) {
111             let keyToFind = [this.compositionMapKey];
112             let subPropertyToscaFunction = this.property.subPropertyToscaFunctions.find(subPropertyToscaFunction => this.areEqual(subPropertyToscaFunction.subPropertyPath, keyToFind));
113
114                 if (subPropertyToscaFunction){
115                         this.toscaFunction = subPropertyToscaFunction.toscaFunction;
116                     this.toscaFunctionForm.setValue(this.toscaFunction);
117                     this.toscaFunctionTypeForm.setValue(this.toscaFunction.type);
118                 }
119                 return;
120         }
121             if (this.property instanceof PropertyDeclareAPIModel && this.property.subPropertyToscaFunctions && (<PropertyDeclareAPIModel> this.property).propertiesName){
122                 let propertiesPath = (<PropertyDeclareAPIModel> this.property).propertiesName.split("#");
123             if (propertiesPath.length > 1){
124                 let keyToFind = (<DerivedFEProperty>this.property.input).toscaPath;
125                 let subPropertyToscaFunction = this.property.subPropertyToscaFunctions.find(subPropertyToscaFunction => this.areEqual(subPropertyToscaFunction.subPropertyPath, keyToFind.length > 0 ? keyToFind : propertiesPath.slice(1)));
126
127                 if (subPropertyToscaFunction){
128                         this.toscaFunction = subPropertyToscaFunction.toscaFunction;
129                     this.toscaFunctionForm.setValue(this.toscaFunction);
130                     this.toscaFunctionTypeForm.setValue(this.toscaFunction.type);
131                 }
132                 return;
133             }
134         }
135         if (!this.property.isToscaFunction()) {
136             return;
137         }
138
139         this.toscaFunctionForm.setValue(this.inToscaFunction ? this.inToscaFunction : this.property.toscaFunction);
140         let type = this.property.toscaFunction.type;
141         if (type == ToscaFunctionType.CUSTOM) {
142             let name = (this.property.toscaFunction as ToscaCustomFunction).name;
143             let test = this.customToscaFunctions.find(custToscFunc => _.isEqual(custToscFunc.name, name))
144             if (test) {
145                 this.toscaFunctionTypeForm.setValue(name);
146             } else {
147                 this.toscaFunctionTypeForm.setValue("other");
148             }
149         } else {
150             this.toscaFunctionTypeForm.setValue(this.inToscaFunction ? this.inToscaFunction.type : type);
151         }
152     }
153
154     private areEqual(array1: string[], array2: string[]): boolean {
155             return array1.length === array2.length && array1.every(function(value, index) { return value === array2[index]})
156     }
157
158     private loadToscaFunctions(): void {
159         this.toscaFunctions = [];
160         this.toscaFunctions.push(ToscaFunctionType.GET_ATTRIBUTE);
161         this.toscaFunctions.push(ToscaFunctionType.GET_INPUT);
162         this.toscaFunctions.push(ToscaFunctionType.GET_PROPERTY);
163         if (this.property.type === PROPERTY_TYPES.STRING || this.property.type === PROPERTY_TYPES.ANY) {
164             this.toscaFunctions.push(ToscaFunctionType.CUSTOM);
165         }
166         if ((this.property.type === PROPERTY_TYPES.STRING || this.property.type === PROPERTY_TYPES.ANY) && this.overridingType === undefined) {
167             this.toscaFunctions.push(ToscaFunctionType.CONCAT);
168         }
169         this.loadCustomToscaFunctions();
170     }
171
172     private loadCustomToscaFunctions(): void {
173         if (!this.customToscaFunctions.find(custToscFunc => _.isEqual(custToscFunc.name, "other"))) {
174             let other = new CustomToscaFunction();
175             other.name = "other";
176             other.type = ToscaFunctionType.CUSTOM;
177             this.customToscaFunctions.push(other);
178         }
179         this.toscaCustomFunctions = [];
180         for (let func of this.customToscaFunctions) {
181             this.toscaCustomFunctions.push(func.name);
182         }
183     }
184
185     getCustomToscaFunction(): CustomToscaFunction {
186         let funcName = this.formGroup.get('toscaFunctionType').value;
187         return this.customToscaFunctions.find(custToscFunc => _.isEqual(custToscFunc.name, funcName));
188     }
189
190     getCustomFunctionName():string {
191         let toscaFunctionType: CustomToscaFunction = this.getCustomToscaFunction();
192         return toscaFunctionType.name;
193     }
194
195     getCustomFunctionType():string {
196         let toscaFunctionType: CustomToscaFunction = this.getCustomToscaFunction();
197         return toscaFunctionType.type;
198     }
199
200     isDefaultCustomFunction(): boolean {
201         let toscaFunctionType: CustomToscaFunction = this.getCustomToscaFunction();
202         if (toscaFunctionType.name === "other") {
203             return false;
204         }
205         return this.customToscaFunctions.filter(e => e.name === toscaFunctionType.name).length > 0;
206     }
207
208     private resetForm(): void {
209         this.formGroup.reset();
210         this.toscaFunction = undefined;
211     }
212
213     private isGetPropertySelected(): boolean {
214         return this.formGroup.get('toscaFunctionType').value === ToscaGetFunctionType.GET_PROPERTY;
215     }
216
217     private isGetAttributeSelected(): boolean {
218         return this.formGroup.get('toscaFunctionType').value === ToscaGetFunctionType.GET_ATTRIBUTE;
219     }
220
221     private isGetInputSelected(): boolean {
222         return this.formGroup.get('toscaFunctionType').value === ToscaGetFunctionType.GET_INPUT;
223     }
224
225     isConcatSelected(): boolean {
226         return this.formGroup.get('toscaFunctionType').value === ToscaFunctionType.CONCAT;
227     }
228
229     isCustomSelected(): boolean {
230         let toscaFunctionType: CustomToscaFunction = this.getCustomToscaFunction();
231         return toscaFunctionType && (toscaFunctionType.type === ToscaFunctionType.CUSTOM || toscaFunctionType.type === ToscaFunctionType.GET_INPUT);
232     }
233
234     isGetFunctionSelected(): boolean {
235         return this.isGetInputSelected() || this.isGetPropertySelected() || this.isGetAttributeSelected();
236     }
237
238     isYamlFunctionSelected(): boolean {
239         return this.formGroup.get('toscaFunctionType').value === ToscaFunctionType.YAML;
240     }
241
242     onClearValues(): void {
243         this.resetForm();
244     }
245
246     showClearButton(): boolean {
247         return this.allowClear && this.toscaFunctionTypeForm.value;
248     }
249
250     onConcatFunctionValidityChange(validationEvent: ToscaConcatFunctionValidationEvent): void {
251         if (validationEvent.isValid) {
252             this.toscaFunctionForm.setValue(validationEvent.toscaConcatFunction);
253         } else {
254             this.toscaFunctionForm.setValue(undefined);
255         }
256     }
257
258     onCustomFunctionValidityChange(validationEvent: ToscaCustomFunctionValidationEvent): void {
259         if (validationEvent.isValid) {
260             this.toscaFunctionForm.setValue(validationEvent.toscaCustomFunction);
261         } else {
262             this.toscaFunctionForm.setValue(undefined);
263         }
264     }
265
266     onGetFunctionValidityChange(validationEvent: ToscaGetFunctionValidationEvent): void {
267         if (validationEvent.isValid) {
268             this.toscaFunctionForm.setValue(validationEvent.toscaGetFunction);
269         } else {
270             this.toscaFunctionForm.setValue(undefined);
271         }
272     }
273
274     onYamlFunctionValidityChange(validationEvent: YamlFunctionValidationEvent): void {
275         if (validationEvent.isValid) {
276             this.toscaFunctionForm.setValue(validationEvent.value);
277         } else {
278             this.toscaFunctionForm.setValue(undefined);
279         }
280     }
281
282     onFunctionTypeChange(): void {
283         this.toscaFunction = undefined;
284         this.toscaFunctionForm.reset();
285     }
286
287     private emitValidityChange(): void {
288         const isValid: boolean = this.validate();
289         this.onValidityChange.emit({
290             isValid: isValid,
291             toscaFunction: isValid ? this.buildFunctionFromForm() : undefined
292         });
293     }
294
295     private buildFunctionFromForm(): ToscaFunction {
296         if (!this.toscaFunctionTypeForm.value) {
297             return undefined;
298         }
299         if (this.isConcatSelected()) {
300             return new ToscaConcatFunction(this.toscaFunctionForm.value);
301         }
302         if (this.isCustomSelected()) {
303             return new ToscaCustomFunction(this.toscaFunctionForm.value);
304         }
305         if (this.isGetFunctionSelected()) {
306             return new ToscaGetFunction(this.toscaFunctionForm.value);
307         }
308         if (this.isYamlFunctionSelected()) {
309             return new YamlFunction(this.toscaFunctionForm.value);
310         }
311
312         console.error(`Function ${this.toscaFunctionTypeForm.value} not supported`);
313     }
314 }
315
316 export class ToscaFunctionValidationEvent {
317     isValid: boolean;
318     toscaFunction: ToscaFunction;
319 }