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, PROPERTY_DATA} 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";
40 selector: 'tosca-function',
41 templateUrl: './tosca-function.component.html',
42 styleUrls: ['./tosca-function.component.less'],
44 export class ToscaFunctionComponent implements OnInit, OnChanges {
46 @Input() property: PropertyBEModel;
47 @Input() componentInstanceMap: Map<string, InstanceFeDetails> = new Map<string, InstanceFeDetails>();
48 @Input() allowClear: boolean = true;
49 @Input() compositionMap: boolean = false;
50 @Input() compositionMapKey: string = "";
51 @Output() onValidFunction: EventEmitter<ToscaGetFunction> = new EventEmitter<ToscaGetFunction>();
52 @Output() onValidityChange: EventEmitter<ToscaFunctionValidationEvent> = new EventEmitter<ToscaFunctionValidationEvent>();
54 toscaFunctionForm: FormControl = new FormControl(undefined, [Validators.required]);
55 toscaFunctionTypeForm: FormControl = new FormControl(undefined, Validators.required);
56 formGroup: FormGroup = new FormGroup({
57 'toscaFunction': this.toscaFunctionForm,
58 'toscaFunctionType': this.toscaFunctionTypeForm,
61 isLoading: boolean = false;
62 toscaFunction: ToscaFunction;
63 toscaFunctions: Array<string> = [];
65 private isInitialized: boolean = false;
66 private componentMetadata: ComponentMetadata;
68 constructor(private topologyTemplateService: TopologyTemplateService,
69 private workspaceService: WorkspaceService) {
73 this.componentMetadata = this.workspaceService.metadata;
74 this.toscaFunction = this.property.toscaFunction ? this.property.toscaFunction : undefined;
75 this.loadToscaFunctions();
76 this.formGroup.valueChanges.subscribe(() => {
77 if (!this.isInitialized) {
80 this.emitValidityChange();
81 if (this.formGroup.valid) {
82 this.onValidFunction.emit(this.toscaFunctionForm.value);
85 this.initToscaFunction();
86 this.emitValidityChange();
87 this.isInitialized = true;
90 ngOnChanges(changes: SimpleChanges): void {
91 if (changes.property) {
93 this.toscaFunction = this.property.toscaFunction ? this.property.toscaFunction : undefined;
94 this.initToscaFunction();
95 this.loadToscaFunctions();
96 this.emitValidityChange();
100 private validate(): boolean {
101 return (!this.toscaFunctionForm.value && !this.toscaFunctionTypeForm.value) || this.formGroup.valid;
104 private initToscaFunction(): void {
105 if (this.compositionMap && this.property.subPropertyToscaFunctions) {
106 let keyToFind = [this.compositionMapKey];
107 let subPropertyToscaFunction = this.property.subPropertyToscaFunctions.find(subPropertyToscaFunction => this.areEqual(subPropertyToscaFunction.subPropertyPath, keyToFind));
109 if (subPropertyToscaFunction){
110 this.toscaFunction = subPropertyToscaFunction.toscaFunction;
111 this.toscaFunctionForm.setValue(this.toscaFunction);
112 this.toscaFunctionTypeForm.setValue(this.toscaFunction.type);
116 if (this.property instanceof PropertyDeclareAPIModel && this.property.subPropertyToscaFunctions && (<PropertyDeclareAPIModel> this.property).propertiesName){
117 let propertiesPath = (<PropertyDeclareAPIModel> this.property).propertiesName.split("#");
118 if (propertiesPath.length > 1){
119 let keyToFind = (<DerivedFEProperty>this.property.input).toscaPath;
120 let subPropertyToscaFunction = this.property.subPropertyToscaFunctions.find(subPropertyToscaFunction => this.areEqual(subPropertyToscaFunction.subPropertyPath, keyToFind.length > 0 ? keyToFind : propertiesPath.slice(1)));
122 if (subPropertyToscaFunction){
123 this.toscaFunction = subPropertyToscaFunction.toscaFunction;
124 this.toscaFunctionForm.setValue(this.toscaFunction);
125 this.toscaFunctionTypeForm.setValue(this.toscaFunction.type);
131 if (!this.property.isToscaFunction()) {
134 this.toscaFunctionForm.setValue(this.property.toscaFunction);
135 this.toscaFunctionTypeForm.setValue(this.property.toscaFunction.type);
138 private areEqual(array1: string[], array2: string[]): boolean {
139 return array1.length === array2.length && array1.every(function(value, index) { return value === array2[index]})
142 private loadToscaFunctions(): void {
143 this.toscaFunctions = [];
144 this.toscaFunctions.push(ToscaFunctionType.GET_ATTRIBUTE);
145 this.toscaFunctions.push(ToscaFunctionType.GET_INPUT);
146 this.toscaFunctions.push(ToscaFunctionType.GET_PROPERTY);
147 this.toscaFunctions.push(ToscaFunctionType.CUSTOM);
148 if (this.property.type === PROPERTY_TYPES.STRING || this.property.type === PROPERTY_TYPES.ANY) {
149 this.toscaFunctions.push(ToscaFunctionType.CONCAT);
151 this.toscaFunctions.push(ToscaFunctionType.YAML);
154 private resetForm(): void {
155 this.formGroup.reset();
156 this.toscaFunction = undefined;
159 private isGetPropertySelected(): boolean {
160 return this.formGroup.get('toscaFunctionType').value === ToscaGetFunctionType.GET_PROPERTY;
163 private isGetAttributeSelected(): boolean {
164 return this.formGroup.get('toscaFunctionType').value === ToscaGetFunctionType.GET_ATTRIBUTE;
167 private isGetInputSelected(): boolean {
168 return this.formGroup.get('toscaFunctionType').value === ToscaGetFunctionType.GET_INPUT;
171 isConcatSelected(): boolean {
172 return this.formGroup.get('toscaFunctionType').value === ToscaFunctionType.CONCAT;
175 isCustomSelected(): boolean {
176 return this.formGroup.get('toscaFunctionType').value === ToscaFunctionType.CUSTOM;
179 isGetFunctionSelected(): boolean {
180 return this.isGetInputSelected() || this.isGetPropertySelected() || this.isGetAttributeSelected();
183 isYamlFunctionSelected(): boolean {
184 return this.formGroup.get('toscaFunctionType').value === ToscaFunctionType.YAML;
187 onClearValues(): void {
191 showClearButton(): boolean {
192 return this.allowClear && this.toscaFunctionTypeForm.value;
195 onConcatFunctionValidityChange(validationEvent: ToscaConcatFunctionValidationEvent): void {
196 if (validationEvent.isValid) {
197 this.toscaFunctionForm.setValue(validationEvent.toscaConcatFunction);
199 this.toscaFunctionForm.setValue(undefined);
203 onCustomFunctionValidityChange(validationEvent: ToscaCustomFunctionValidationEvent): void {
204 if (validationEvent.isValid) {
205 this.toscaFunctionForm.setValue(validationEvent.toscaCustomFunction);
207 this.toscaFunctionForm.setValue(undefined);
211 onGetFunctionValidityChange(validationEvent: ToscaGetFunctionValidationEvent): void {
212 if (validationEvent.isValid) {
213 this.toscaFunctionForm.setValue(validationEvent.toscaGetFunction);
215 this.toscaFunctionForm.setValue(undefined);
219 onYamlFunctionValidityChange(validationEvent: YamlFunctionValidationEvent): void {
220 if (validationEvent.isValid) {
221 this.toscaFunctionForm.setValue(validationEvent.value);
223 this.toscaFunctionForm.setValue(undefined);
227 onFunctionTypeChange(): void {
228 this.toscaFunction = undefined;
229 this.toscaFunctionForm.reset();
232 private emitValidityChange(): void {
233 const isValid: boolean = this.validate();
234 this.onValidityChange.emit({
236 toscaFunction: isValid ? this.buildFunctionFromForm() : undefined
240 private buildFunctionFromForm(): ToscaFunction {
241 if (!this.toscaFunctionTypeForm.value) {
244 if (this.isConcatSelected()) {
245 return new ToscaConcatFunction(this.toscaFunctionForm.value);
247 if (this.isCustomSelected()) {
248 return new ToscaCustomFunction(this.toscaFunctionForm.value);
250 if (this.isGetFunctionSelected()) {
251 return new ToscaGetFunction(this.toscaFunctionForm.value);
253 if (this.isYamlFunctionSelected()) {
254 return new YamlFunction(this.toscaFunctionForm.value);
257 console.error(`Function ${this.toscaFunctionTypeForm.value} not supported`);
261 export class ToscaFunctionValidationEvent {
263 toscaFunction: ToscaFunction;