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, OnInit, Output} from '@angular/core';
21 import {ComponentMetadata, PropertyBEModel} 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} from "../../../../utils/constants";
33 import {YamlFunctionValidationEvent} from "./yaml-function/yaml-function.component";
36 selector: 'tosca-function',
37 templateUrl: './tosca-function.component.html',
38 styleUrls: ['./tosca-function.component.less'],
40 export class ToscaFunctionComponent implements OnInit {
42 @Input() property: PropertyBEModel;
43 @Input() componentInstanceMap: Map<string, InstanceFeDetails> = new Map<string, InstanceFeDetails>();
44 @Input() allowClear: boolean = true;
45 @Output() onValidFunction: EventEmitter<ToscaGetFunction> = new EventEmitter<ToscaGetFunction>();
46 @Output() onValidityChange: EventEmitter<ToscaFunctionValidationEvent> = new EventEmitter<ToscaFunctionValidationEvent>();
48 toscaFunctionForm: FormControl = new FormControl(undefined, [Validators.required]);
49 toscaFunctionTypeForm: FormControl = new FormControl(undefined, Validators.required);
50 formGroup: FormGroup = new FormGroup({
51 'toscaFunction': this.toscaFunctionForm,
52 'toscaFunctionType': this.toscaFunctionTypeForm,
55 isLoading: boolean = false;
56 toscaFunction: ToscaFunction;
57 toscaFunctions: Array<string> = [];
59 private isInitialized: boolean = false;
60 private componentMetadata: ComponentMetadata;
62 constructor(private topologyTemplateService: TopologyTemplateService,
63 private workspaceService: WorkspaceService) {
67 this.componentMetadata = this.workspaceService.metadata;
68 this.toscaFunction = this.property.toscaFunction ? this.property.toscaFunction : undefined;
69 this.loadToscaFunctions();
70 this.formGroup.valueChanges.subscribe(() => {
71 if (!this.isInitialized) {
74 this.emitValidityChange();
75 if (this.formGroup.valid) {
76 this.onValidFunction.emit(this.toscaFunctionForm.value);
79 this.initToscaFunction();
80 this.emitValidityChange();
81 this.isInitialized = true;
85 return (!this.toscaFunctionForm.value && !this.toscaFunctionTypeForm.value) || this.formGroup.valid;
88 private initToscaFunction() {
89 if (!this.property.isToscaFunction()) {
92 this.toscaFunctionForm.setValue(this.property.toscaFunction);
93 this.toscaFunctionTypeForm.setValue(this.property.toscaFunction.type);
96 private loadToscaFunctions(): void {
97 this.toscaFunctions.push(ToscaFunctionType.GET_ATTRIBUTE);
98 this.toscaFunctions.push(ToscaFunctionType.GET_INPUT);
99 this.toscaFunctions.push(ToscaFunctionType.GET_PROPERTY);
100 if (this.property.type === PROPERTY_TYPES.STRING) {
101 this.toscaFunctions.push(ToscaFunctionType.CONCAT);
103 this.toscaFunctions.push(ToscaFunctionType.YAML);
106 private resetForm(): void {
107 this.formGroup.reset();
108 this.toscaFunction = undefined;
111 private isGetPropertySelected(): boolean {
112 return this.formGroup.get('toscaFunctionType').value === ToscaGetFunctionType.GET_PROPERTY;
115 private isGetAttributeSelected(): boolean {
116 return this.formGroup.get('toscaFunctionType').value === ToscaGetFunctionType.GET_ATTRIBUTE;
119 private isGetInputSelected(): boolean {
120 return this.formGroup.get('toscaFunctionType').value === ToscaGetFunctionType.GET_INPUT;
123 isConcatSelected(): boolean {
124 return this.formGroup.get('toscaFunctionType').value === ToscaFunctionType.CONCAT;
127 isGetFunctionSelected(): boolean {
128 return this.isGetInputSelected() || this.isGetPropertySelected() || this.isGetAttributeSelected();
131 isYamlFunctionSelected(): boolean {
132 return this.formGroup.get('toscaFunctionType').value === ToscaFunctionType.YAML;
139 showClearButton(): boolean {
140 return this.allowClear && this.toscaFunctionTypeForm.value;
143 onConcatFunctionValidityChange(validationEvent: ToscaConcatFunctionValidationEvent) {
144 if (validationEvent.isValid) {
145 this.toscaFunctionForm.setValue(validationEvent.toscaConcatFunction);
147 this.toscaFunctionForm.setValue(undefined);
151 onGetFunctionValidityChange(validationEvent: ToscaGetFunctionValidationEvent) {
152 if (validationEvent.isValid) {
153 this.toscaFunctionForm.setValue(validationEvent.toscaGetFunction);
155 this.toscaFunctionForm.setValue(undefined);
159 onYamlFunctionValidityChange(validationEvent: YamlFunctionValidationEvent) {
160 if (validationEvent.isValid) {
161 this.toscaFunctionForm.setValue(validationEvent.value);
163 this.toscaFunctionForm.setValue(undefined);
167 private emitValidityChange() {
168 const isValid = this.validate();
169 this.onValidityChange.emit({
171 toscaFunction: isValid ? this.toscaFunctionForm.value : undefined
177 export class ToscaFunctionValidationEvent {
179 toscaFunction: ToscaFunction;