Provide tosca function to custom datatypes of list and map
[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 {PROPERTY_TYPES, PROPERTY_DATA} from "../../../../utils/constants";
33 import {YamlFunctionValidationEvent} from "./yaml-function/yaml-function.component";
34 import {ToscaConcatFunction} from "../../../../models/tosca-concat-function";
35 import {YamlFunction} from "../../../../models/yaml-function";
36
37 @Component({
38     selector: 'tosca-function',
39     templateUrl: './tosca-function.component.html',
40     styleUrls: ['./tosca-function.component.less'],
41 })
42 export class ToscaFunctionComponent implements OnInit, OnChanges {
43
44     @Input() property: PropertyBEModel;
45     @Input() componentInstanceMap: Map<string, InstanceFeDetails> = new Map<string, InstanceFeDetails>();
46     @Input() allowClear: boolean = true;
47     @Output() onValidFunction: EventEmitter<ToscaGetFunction> = new EventEmitter<ToscaGetFunction>();
48     @Output() onValidityChange: EventEmitter<ToscaFunctionValidationEvent> = new EventEmitter<ToscaFunctionValidationEvent>();
49
50     toscaFunctionForm: FormControl = new FormControl(undefined, [Validators.required]);
51     toscaFunctionTypeForm: FormControl = new FormControl(undefined, Validators.required);
52     formGroup: FormGroup = new FormGroup({
53         'toscaFunction': this.toscaFunctionForm,
54         'toscaFunctionType': this.toscaFunctionTypeForm,
55     });
56
57     isLoading: boolean = false;
58     toscaFunction: ToscaFunction;
59     toscaFunctions: Array<string> = [];
60
61     private isInitialized: boolean = false;
62     private componentMetadata: ComponentMetadata;
63
64     constructor(private topologyTemplateService: TopologyTemplateService,
65                 private workspaceService: WorkspaceService) {
66     }
67
68     ngOnInit(): void {
69         this.componentMetadata = this.workspaceService.metadata;
70         this.toscaFunction = this.property.toscaFunction ? this.property.toscaFunction : undefined;
71         this.loadToscaFunctions();
72         this.formGroup.valueChanges.subscribe(() => {
73             if (!this.isInitialized) {
74                 return;
75             }
76             this.emitValidityChange();
77             if (this.formGroup.valid) {
78                 this.onValidFunction.emit(this.toscaFunctionForm.value);
79             }
80         });
81         this.initToscaFunction();
82         this.emitValidityChange();
83         this.isInitialized = true;
84     }
85
86     ngOnChanges(changes: SimpleChanges): void {
87         if (changes.property) {
88             this.resetForm();
89             this.toscaFunction = this.property.toscaFunction ? this.property.toscaFunction : undefined;
90             this.initToscaFunction();
91             this.loadToscaFunctions();
92             this.emitValidityChange();
93         }
94     }
95
96     private validate(): boolean {
97         return (!this.toscaFunctionForm.value && !this.toscaFunctionTypeForm.value) || this.formGroup.valid;
98     }
99
100     private initToscaFunction(): void {
101             if (this.property instanceof PropertyDeclareAPIModel && this.property.subPropertyToscaFunctions && (<PropertyDeclareAPIModel> this.property).propertiesName){
102                 let propertiesPath = (<PropertyDeclareAPIModel> this.property).propertiesName.split("#");
103             if (propertiesPath.length > 1){
104                 let keyToFind = [];
105                 if (this.property.type == PROPERTY_TYPES.MAP || this.property.type == PROPERTY_TYPES.LIST) {
106                     keyToFind.push((<DerivedFEProperty>this.property.input).mapKey);
107                     if (PROPERTY_DATA.SIMPLE_TYPES.indexOf(this.property.schemaType) === -1) {
108                         keyToFind.push(propertiesPath.reverse()[0]);
109                     }
110                 }
111                 let subPropertyToscaFunction = this.property.subPropertyToscaFunctions.find(subPropertyToscaFunction => this.areEqual(subPropertyToscaFunction.subPropertyPath, keyToFind.length > 0 ? keyToFind : propertiesPath.slice(1)));
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         }
121
122         if (!this.property.isToscaFunction()) {
123             return;
124         }
125         this.toscaFunctionForm.setValue(this.property.toscaFunction);
126         this.toscaFunctionTypeForm.setValue(this.property.toscaFunction.type);
127     }
128
129     private areEqual(array1: string[], array2: string[]): boolean {
130             return array1.length === array2.length && array1.every(function(value, index) { return value === array2[index]})
131     }
132
133     private loadToscaFunctions(): void {
134         this.toscaFunctions = [];
135         this.toscaFunctions.push(ToscaFunctionType.GET_ATTRIBUTE);
136         this.toscaFunctions.push(ToscaFunctionType.GET_INPUT);
137         this.toscaFunctions.push(ToscaFunctionType.GET_PROPERTY);
138         if (this.property.type === PROPERTY_TYPES.STRING) {
139             this.toscaFunctions.push(ToscaFunctionType.CONCAT);
140         }
141         this.toscaFunctions.push(ToscaFunctionType.YAML);
142     }
143
144     private resetForm(): void {
145         this.formGroup.reset();
146         this.toscaFunction = undefined;
147     }
148
149     private isGetPropertySelected(): boolean {
150         return this.formGroup.get('toscaFunctionType').value === ToscaGetFunctionType.GET_PROPERTY;
151     }
152
153     private isGetAttributeSelected(): boolean {
154         return this.formGroup.get('toscaFunctionType').value === ToscaGetFunctionType.GET_ATTRIBUTE;
155     }
156
157     private isGetInputSelected(): boolean {
158         return this.formGroup.get('toscaFunctionType').value === ToscaGetFunctionType.GET_INPUT;
159     }
160
161     isConcatSelected(): boolean {
162         return this.formGroup.get('toscaFunctionType').value === ToscaFunctionType.CONCAT;
163     }
164
165     isGetFunctionSelected(): boolean {
166         return this.isGetInputSelected() || this.isGetPropertySelected() || this.isGetAttributeSelected();
167     }
168
169     isYamlFunctionSelected(): boolean {
170         return this.formGroup.get('toscaFunctionType').value === ToscaFunctionType.YAML;
171     }
172
173     onClearValues(): void {
174         this.resetForm();
175     }
176
177     showClearButton(): boolean {
178         return this.allowClear && this.toscaFunctionTypeForm.value;
179     }
180
181     onConcatFunctionValidityChange(validationEvent: ToscaConcatFunctionValidationEvent): void {
182         if (validationEvent.isValid) {
183             this.toscaFunctionForm.setValue(validationEvent.toscaConcatFunction);
184         } else {
185             this.toscaFunctionForm.setValue(undefined);
186         }
187     }
188
189     onGetFunctionValidityChange(validationEvent: ToscaGetFunctionValidationEvent): void {
190         if (validationEvent.isValid) {
191             this.toscaFunctionForm.setValue(validationEvent.toscaGetFunction);
192         } else {
193             this.toscaFunctionForm.setValue(undefined);
194         }
195     }
196
197     onYamlFunctionValidityChange(validationEvent: YamlFunctionValidationEvent): void {
198         if (validationEvent.isValid) {
199             this.toscaFunctionForm.setValue(validationEvent.value);
200         } else {
201             this.toscaFunctionForm.setValue(undefined);
202         }
203     }
204
205     onFunctionTypeChange(): void {
206         this.toscaFunction = undefined;
207         this.toscaFunctionForm.reset();
208     }
209
210     private emitValidityChange(): void {
211         const isValid: boolean = this.validate();
212         this.onValidityChange.emit({
213             isValid: isValid,
214             toscaFunction: isValid ? this.buildFunctionFromForm() : undefined
215         });
216     }
217
218     private buildFunctionFromForm(): ToscaFunction {
219         if (!this.toscaFunctionTypeForm.value) {
220             return undefined;
221         }
222         if (this.isConcatSelected()) {
223             return new ToscaConcatFunction(this.toscaFunctionForm.value);
224         }
225         if (this.isGetFunctionSelected()) {
226             return new ToscaGetFunction(this.toscaFunctionForm.value);
227         }
228         if (this.isYamlFunctionSelected()) {
229             return new YamlFunction(this.toscaFunctionForm.value);
230         }
231
232         console.error(`Function ${this.toscaFunctionTypeForm.value} not supported`);
233     }
234 }
235
236 export class ToscaFunctionValidationEvent {
237     isValid: boolean;
238     toscaFunction: ToscaFunction;
239 }