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
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.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
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";
41 selector: 'tosca-function',
42 templateUrl: './tosca-function.component.html',
43 styleUrls: ['./tosca-function.component.less'],
45 export class ToscaFunctionComponent implements OnInit, OnChanges {
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>();
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,
65 isLoading: boolean = false;
66 toscaFunction: ToscaFunction;
67 toscaFunctions: Array<string> = [];
68 toscaCustomFunctions: Array<String> = [];
70 private isInitialized: boolean = false;
71 private componentMetadata: ComponentMetadata;
73 constructor(private topologyTemplateService: TopologyTemplateService,
74 private workspaceService: WorkspaceService) {
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) {
85 if (this.formGroup.valid) {
86 this.onValidFunction.emit(this.toscaFunctionForm.value);
89 this.initToscaFunction();
90 this.emitValidityChange();
91 this.isInitialized = true;
94 ngOnChanges(changes: SimpleChanges): void {
95 if (changes.property) {
97 this.toscaFunction = this.inToscaFunction ? this.inToscaFunction : this.property.toscaFunction ? this.property.toscaFunction : undefined;
98 this.initToscaFunction();
99 this.loadToscaFunctions();
100 this.emitValidityChange();
104 private validate(): boolean {
105 return (!this.toscaFunctionForm.value && !this.toscaFunctionTypeForm.value) || this.formGroup.valid;
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));
113 if (subPropertyToscaFunction){
114 this.toscaFunction = subPropertyToscaFunction.toscaFunction;
115 this.toscaFunctionForm.setValue(this.toscaFunction);
116 this.toscaFunctionTypeForm.setValue(this.toscaFunction.type);
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)));
126 if (subPropertyToscaFunction){
127 this.toscaFunction = subPropertyToscaFunction.toscaFunction;
128 this.toscaFunctionForm.setValue(this.toscaFunction);
129 this.toscaFunctionTypeForm.setValue(this.toscaFunction.type);
134 if (!this.property.isToscaFunction()) {
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);
146 this.toscaFunctionTypeForm.setValue("other");
149 this.toscaFunctionTypeForm.setValue(this.inToscaFunction ? this.inToscaFunction.type : type);
153 private areEqual(array1: string[], array2: string[]): boolean {
154 return array1.length === array2.length && array1.every(function(value, index) { return value === array2[index]})
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);
165 this.loadCustomToscaFunctions();
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);
175 this.toscaCustomFunctions = [];
176 for (let func of this.customToscaFunctions) {
177 this.toscaCustomFunctions.push(func.name);
181 getCustomToscaFunction(): CustomToscaFunction {
182 let funcName = this.formGroup.get('toscaFunctionType').value;
183 return this.customToscaFunctions.find(custToscFunc => _.isEqual(custToscFunc.name, funcName));
186 getCustomFunctionName():string {
187 let toscaFunctionType: CustomToscaFunction = this.getCustomToscaFunction();
188 let name = toscaFunctionType.name;
189 return name == 'other' ? '' : name;
192 getCustomFunctionType():string {
193 let toscaFunctionType: CustomToscaFunction = this.getCustomToscaFunction();
194 return toscaFunctionType.type;
197 isDefaultCustomFunction(): boolean {
198 let toscaFunctionType: CustomToscaFunction = this.getCustomToscaFunction();
199 if (toscaFunctionType.name === "other") {
202 return this.customToscaFunctions.filter(e => e.name === toscaFunctionType.name).length > 0;
205 private resetForm(): void {
206 this.formGroup.reset();
207 this.toscaFunction = undefined;
210 private isGetPropertySelected(): boolean {
211 return this.formGroup.get('toscaFunctionType').value === ToscaGetFunctionType.GET_PROPERTY;
214 private isGetAttributeSelected(): boolean {
215 return this.formGroup.get('toscaFunctionType').value === ToscaGetFunctionType.GET_ATTRIBUTE;
218 private isGetInputSelected(): boolean {
219 return this.formGroup.get('toscaFunctionType').value === ToscaGetFunctionType.GET_INPUT;
222 isConcatSelected(): boolean {
223 return this.formGroup.get('toscaFunctionType').value === ToscaFunctionType.CONCAT;
226 isCustomSelected(): boolean {
227 let toscaFunctionType: CustomToscaFunction = this.getCustomToscaFunction();
228 return toscaFunctionType && (toscaFunctionType.type === ToscaFunctionType.CUSTOM || toscaFunctionType.type === ToscaFunctionType.GET_INPUT);
231 isGetFunctionSelected(): boolean {
232 return this.isGetInputSelected() || this.isGetPropertySelected() || this.isGetAttributeSelected();
235 isYamlFunctionSelected(): boolean {
236 return this.formGroup.get('toscaFunctionType').value === ToscaFunctionType.YAML;
239 onClearValues(): void {
243 showClearButton(): boolean {
244 return this.allowClear && this.toscaFunctionTypeForm.value;
247 onConcatFunctionValidityChange(validationEvent: ToscaConcatFunctionValidationEvent): void {
248 if (validationEvent.isValid) {
249 this.toscaFunctionForm.setValue(validationEvent.toscaConcatFunction);
251 this.toscaFunctionForm.setValue(undefined);
255 onCustomFunctionValidityChange(validationEvent: ToscaCustomFunctionValidationEvent): void {
256 if (validationEvent.isValid) {
257 this.toscaFunctionForm.setValue(validationEvent.toscaCustomFunction);
259 this.toscaFunctionForm.setValue(undefined);
263 onGetFunctionValidityChange(validationEvent: ToscaGetFunctionValidationEvent): void {
264 if (validationEvent.isValid) {
265 this.toscaFunctionForm.setValue(validationEvent.toscaGetFunction);
267 this.toscaFunctionForm.setValue(undefined);
269 this.emitValidityChange();
272 onYamlFunctionValidityChange(validationEvent: YamlFunctionValidationEvent): void {
273 if (validationEvent.isValid) {
274 this.toscaFunctionForm.setValue(validationEvent.value);
276 this.toscaFunctionForm.setValue(undefined);
280 onFunctionTypeChange(): void {
281 this.toscaFunction = undefined;
282 this.toscaFunctionForm.reset();
285 private emitValidityChange(): void {
286 const isValid: boolean = this.validate();
287 this.onValidityChange.emit({
289 toscaFunction: isValid ? this.buildFunctionFromForm() : undefined
293 private buildFunctionFromForm(): ToscaFunction {
294 if (!this.toscaFunctionTypeForm.value) {
297 if (this.isConcatSelected()) {
298 return new ToscaConcatFunction(this.toscaFunctionForm.value);
300 if (this.isCustomSelected()) {
301 return new ToscaCustomFunction(this.toscaFunctionForm.value);
303 if (this.isGetFunctionSelected()) {
304 return new ToscaGetFunction(this.toscaFunctionForm.value);
306 if (this.isYamlFunctionSelected()) {
307 return new YamlFunction(this.toscaFunctionForm.value);
310 console.error(`Function ${this.toscaFunctionTypeForm.value} not supported`);
314 export class ToscaFunctionValidationEvent {
316 toscaFunction: ToscaFunction;