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 this.emitValidityChange();
86 if (this.formGroup.valid) {
87 this.onValidFunction.emit(this.toscaFunctionForm.value);
90 this.initToscaFunction();
91 this.emitValidityChange();
92 this.isInitialized = true;
95 ngOnChanges(changes: SimpleChanges): void {
96 if (changes.property) {
98 this.toscaFunction = this.inToscaFunction ? this.inToscaFunction : this.property.toscaFunction ? this.property.toscaFunction : undefined;
99 this.initToscaFunction();
100 this.loadToscaFunctions();
101 this.emitValidityChange();
105 private validate(): boolean {
106 return (!this.toscaFunctionForm.value && !this.toscaFunctionTypeForm.value) || this.formGroup.valid;
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));
114 if (subPropertyToscaFunction){
115 this.toscaFunction = subPropertyToscaFunction.toscaFunction;
116 this.toscaFunctionForm.setValue(this.toscaFunction);
117 this.toscaFunctionTypeForm.setValue(this.toscaFunction.type);
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)));
127 if (subPropertyToscaFunction){
128 this.toscaFunction = subPropertyToscaFunction.toscaFunction;
129 this.toscaFunctionForm.setValue(this.toscaFunction);
130 this.toscaFunctionTypeForm.setValue(this.toscaFunction.type);
135 if (!this.property.isToscaFunction()) {
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))
145 this.toscaFunctionTypeForm.setValue(name);
147 this.toscaFunctionTypeForm.setValue("other");
150 this.toscaFunctionTypeForm.setValue(this.inToscaFunction ? this.inToscaFunction.type : type);
154 private areEqual(array1: string[], array2: string[]): boolean {
155 return array1.length === array2.length && array1.every(function(value, index) { return value === array2[index]})
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);
166 if ((this.property.type === PROPERTY_TYPES.STRING || this.property.type === PROPERTY_TYPES.ANY) && this.overridingType === undefined) {
167 this.toscaFunctions.push(ToscaFunctionType.CONCAT);
169 this.loadCustomToscaFunctions();
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);
179 this.toscaCustomFunctions = [];
180 for (let func of this.customToscaFunctions) {
181 this.toscaCustomFunctions.push(func.name);
185 getCustomToscaFunction(): CustomToscaFunction {
186 let funcName = this.formGroup.get('toscaFunctionType').value;
187 return this.customToscaFunctions.find(custToscFunc => _.isEqual(custToscFunc.name, funcName));
190 getCustomFunctionName():string {
191 let toscaFunctionType: CustomToscaFunction = this.getCustomToscaFunction();
192 return toscaFunctionType.name;
195 getCustomFunctionType():string {
196 let toscaFunctionType: CustomToscaFunction = this.getCustomToscaFunction();
197 return toscaFunctionType.type;
200 isDefaultCustomFunction(): boolean {
201 let toscaFunctionType: CustomToscaFunction = this.getCustomToscaFunction();
202 if (toscaFunctionType.name === "other") {
205 return this.customToscaFunctions.filter(e => e.name === toscaFunctionType.name).length > 0;
208 private resetForm(): void {
209 this.formGroup.reset();
210 this.toscaFunction = undefined;
213 private isGetPropertySelected(): boolean {
214 return this.formGroup.get('toscaFunctionType').value === ToscaGetFunctionType.GET_PROPERTY;
217 private isGetAttributeSelected(): boolean {
218 return this.formGroup.get('toscaFunctionType').value === ToscaGetFunctionType.GET_ATTRIBUTE;
221 private isGetInputSelected(): boolean {
222 return this.formGroup.get('toscaFunctionType').value === ToscaGetFunctionType.GET_INPUT;
225 isConcatSelected(): boolean {
226 return this.formGroup.get('toscaFunctionType').value === ToscaFunctionType.CONCAT;
229 isCustomSelected(): boolean {
230 let toscaFunctionType: CustomToscaFunction = this.getCustomToscaFunction();
231 return toscaFunctionType && (toscaFunctionType.type === ToscaFunctionType.CUSTOM || toscaFunctionType.type === ToscaFunctionType.GET_INPUT);
234 isGetFunctionSelected(): boolean {
235 return this.isGetInputSelected() || this.isGetPropertySelected() || this.isGetAttributeSelected();
238 isYamlFunctionSelected(): boolean {
239 return this.formGroup.get('toscaFunctionType').value === ToscaFunctionType.YAML;
242 onClearValues(): void {
246 showClearButton(): boolean {
247 return this.allowClear && this.toscaFunctionTypeForm.value;
250 onConcatFunctionValidityChange(validationEvent: ToscaConcatFunctionValidationEvent): void {
251 if (validationEvent.isValid) {
252 this.toscaFunctionForm.setValue(validationEvent.toscaConcatFunction);
254 this.toscaFunctionForm.setValue(undefined);
258 onCustomFunctionValidityChange(validationEvent: ToscaCustomFunctionValidationEvent): void {
259 if (validationEvent.isValid) {
260 this.toscaFunctionForm.setValue(validationEvent.toscaCustomFunction);
262 this.toscaFunctionForm.setValue(undefined);
266 onGetFunctionValidityChange(validationEvent: ToscaGetFunctionValidationEvent): void {
267 if (validationEvent.isValid) {
268 this.toscaFunctionForm.setValue(validationEvent.toscaGetFunction);
270 this.toscaFunctionForm.setValue(undefined);
274 onYamlFunctionValidityChange(validationEvent: YamlFunctionValidationEvent): void {
275 if (validationEvent.isValid) {
276 this.toscaFunctionForm.setValue(validationEvent.value);
278 this.toscaFunctionForm.setValue(undefined);
282 onFunctionTypeChange(): void {
283 this.toscaFunction = undefined;
284 this.toscaFunctionForm.reset();
287 private emitValidityChange(): void {
288 const isValid: boolean = this.validate();
289 this.onValidityChange.emit({
291 toscaFunction: isValid ? this.buildFunctionFromForm() : undefined
295 private buildFunctionFromForm(): ToscaFunction {
296 if (!this.toscaFunctionTypeForm.value) {
299 if (this.isConcatSelected()) {
300 return new ToscaConcatFunction(this.toscaFunctionForm.value);
302 if (this.isCustomSelected()) {
303 return new ToscaCustomFunction(this.toscaFunctionForm.value);
305 if (this.isGetFunctionSelected()) {
306 return new ToscaGetFunction(this.toscaFunctionForm.value);
308 if (this.isYamlFunctionSelected()) {
309 return new YamlFunction(this.toscaFunctionForm.value);
312 console.error(`Function ${this.toscaFunctionTypeForm.value} not supported`);
316 export class ToscaFunctionValidationEvent {
318 toscaFunction: ToscaFunction;