Support a custom yaml value in tosca function
[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, 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";
34
35 @Component({
36     selector: 'tosca-function',
37     templateUrl: './tosca-function.component.html',
38     styleUrls: ['./tosca-function.component.less'],
39 })
40 export class ToscaFunctionComponent implements OnInit {
41
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>();
47
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,
53     });
54
55     isLoading: boolean = false;
56     toscaFunction: ToscaFunction;
57     toscaFunctions: Array<string> = [];
58
59     private isInitialized: boolean = false;
60     private componentMetadata: ComponentMetadata;
61
62     constructor(private topologyTemplateService: TopologyTemplateService,
63                 private workspaceService: WorkspaceService) {
64     }
65
66     ngOnInit(): void {
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) {
72                 return;
73             }
74             this.emitValidityChange();
75             if (this.formGroup.valid) {
76                 this.onValidFunction.emit(this.toscaFunctionForm.value);
77             }
78         });
79         this.initToscaFunction();
80         this.emitValidityChange();
81         this.isInitialized = true;
82     }
83
84     private validate() {
85         return (!this.toscaFunctionForm.value && !this.toscaFunctionTypeForm.value) || this.formGroup.valid;
86     }
87
88     private initToscaFunction() {
89         if (!this.property.isToscaFunction()) {
90             return;
91         }
92         this.toscaFunctionForm.setValue(this.property.toscaFunction);
93         this.toscaFunctionTypeForm.setValue(this.property.toscaFunction.type);
94     }
95
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);
102         }
103         this.toscaFunctions.push(ToscaFunctionType.YAML);
104     }
105
106     private resetForm(): void {
107         this.formGroup.reset();
108         this.toscaFunction = undefined;
109     }
110
111     private isGetPropertySelected(): boolean {
112         return this.formGroup.get('toscaFunctionType').value === ToscaGetFunctionType.GET_PROPERTY;
113     }
114
115     private isGetAttributeSelected(): boolean {
116         return this.formGroup.get('toscaFunctionType').value === ToscaGetFunctionType.GET_ATTRIBUTE;
117     }
118
119     private isGetInputSelected(): boolean {
120         return this.formGroup.get('toscaFunctionType').value === ToscaGetFunctionType.GET_INPUT;
121     }
122
123     isConcatSelected(): boolean {
124         return this.formGroup.get('toscaFunctionType').value === ToscaFunctionType.CONCAT;
125     }
126
127     isGetFunctionSelected(): boolean {
128         return this.isGetInputSelected() || this.isGetPropertySelected() || this.isGetAttributeSelected();
129     }
130
131     isYamlFunctionSelected(): boolean {
132         return this.formGroup.get('toscaFunctionType').value === ToscaFunctionType.YAML;
133     }
134
135     onClearValues() {
136         this.resetForm();
137     }
138
139     showClearButton(): boolean {
140         return this.allowClear && this.toscaFunctionTypeForm.value;
141     }
142
143     onConcatFunctionValidityChange(validationEvent: ToscaConcatFunctionValidationEvent) {
144         if (validationEvent.isValid) {
145             this.toscaFunctionForm.setValue(validationEvent.toscaConcatFunction);
146         } else {
147             this.toscaFunctionForm.setValue(undefined);
148         }
149     }
150
151     onGetFunctionValidityChange(validationEvent: ToscaGetFunctionValidationEvent) {
152         if (validationEvent.isValid) {
153             this.toscaFunctionForm.setValue(validationEvent.toscaGetFunction);
154         } else {
155             this.toscaFunctionForm.setValue(undefined);
156         }
157     }
158
159     onYamlFunctionValidityChange(validationEvent: YamlFunctionValidationEvent) {
160         if (validationEvent.isValid) {
161             this.toscaFunctionForm.setValue(validationEvent.value);
162         } else {
163             this.toscaFunctionForm.setValue(undefined);
164         }
165     }
166
167     private emitValidityChange() {
168         const isValid = this.validate();
169         this.onValidityChange.emit({
170             isValid: isValid,
171             toscaFunction: isValid ? this.toscaFunctionForm.value : undefined
172         });
173     }
174
175 }
176
177 export class ToscaFunctionValidationEvent {
178     isValid: boolean;
179     toscaFunction: ToscaFunction;
180 }