Support for concat 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
34 @Component({
35     selector: 'tosca-function',
36     templateUrl: './tosca-function.component.html',
37     styleUrls: ['./tosca-function.component.less'],
38 })
39 export class ToscaFunctionComponent implements OnInit {
40
41     @Input() property: PropertyBEModel;
42     @Input() componentInstanceMap: Map<string, InstanceFeDetails> = new Map<string, InstanceFeDetails>();
43     @Input() allowClear: boolean = true;
44     @Output() onValidFunction: EventEmitter<ToscaGetFunction> = new EventEmitter<ToscaGetFunction>();
45     @Output() onValidityChange: EventEmitter<ToscaFunctionValidationEvent> = new EventEmitter<ToscaFunctionValidationEvent>();
46
47     toscaFunctionForm: FormControl = new FormControl(undefined, [Validators.required]);
48     toscaFunctionTypeForm: FormControl = new FormControl(undefined, Validators.required);
49     formGroup: FormGroup = new FormGroup({
50         'toscaFunction': this.toscaFunctionForm,
51         'toscaFunctionType': this.toscaFunctionTypeForm,
52     });
53
54     isLoading: boolean = false;
55     toscaFunction: ToscaFunction;
56     toscaFunctions: Array<string> = [];
57
58     private isInitialized: boolean = false;
59     private componentMetadata: ComponentMetadata;
60
61     constructor(private topologyTemplateService: TopologyTemplateService,
62                 private workspaceService: WorkspaceService) {
63     }
64
65     ngOnInit(): void {
66         this.componentMetadata = this.workspaceService.metadata;
67         this.toscaFunction = this.property.toscaFunction ? this.property.toscaFunction : undefined;
68         this.loadToscaFunctions();
69         this.formGroup.valueChanges.subscribe(() => {
70             if (!this.isInitialized) {
71                 return;
72             }
73             this.emitValidityChange();
74             if (this.formGroup.valid) {
75                 this.onValidFunction.emit(this.toscaFunctionForm.value);
76             }
77         });
78         this.initToscaGetFunction();
79         this.emitValidityChange();
80         this.isInitialized = true;
81     }
82
83     private validate() {
84         return (!this.toscaFunctionForm.value && !this.toscaFunctionTypeForm.value) || this.formGroup.valid;
85     }
86
87     private initToscaGetFunction() {
88         if (!this.property.isToscaGetFunction()) {
89             return;
90         }
91         this.toscaFunctionForm.setValue(this.property.toscaFunction);
92         this.toscaFunctionTypeForm.setValue(this.property.toscaFunction.type);
93     }
94
95     private loadToscaFunctions(): void {
96         this.toscaFunctions.push(ToscaFunctionType.GET_ATTRIBUTE);
97         this.toscaFunctions.push(ToscaFunctionType.GET_INPUT);
98         this.toscaFunctions.push(ToscaFunctionType.GET_PROPERTY);
99         if (this.property.type === PROPERTY_TYPES.STRING) {
100             this.toscaFunctions.push(ToscaFunctionType.CONCAT);
101         }
102     }
103
104     private resetForm(): void {
105         this.formGroup.reset();
106         this.toscaFunction = undefined;
107     }
108
109     private isGetPropertySelected(): boolean {
110         return this.formGroup.get('toscaFunctionType').value === ToscaGetFunctionType.GET_PROPERTY;
111     }
112
113     private isGetAttributeSelected(): boolean {
114         return this.formGroup.get('toscaFunctionType').value === ToscaGetFunctionType.GET_ATTRIBUTE;
115     }
116
117     private isGetInputSelected(): boolean {
118         return this.formGroup.get('toscaFunctionType').value === ToscaGetFunctionType.GET_INPUT;
119     }
120
121     isConcatSelected(): boolean {
122         return this.formGroup.get('toscaFunctionType').value === ToscaFunctionType.CONCAT;
123     }
124
125     isGetFunctionSelected(): boolean {
126         return this.isGetInputSelected() || this.isGetPropertySelected() || this.isGetAttributeSelected();
127     }
128
129     onClearValues() {
130         this.resetForm();
131     }
132
133     showClearButton(): boolean {
134         return this.allowClear && this.toscaFunctionTypeForm.value;
135     }
136
137     onConcatFunctionValidityChange(validationEvent: ToscaConcatFunctionValidationEvent) {
138         if (validationEvent.isValid) {
139             this.toscaFunctionForm.setValue(validationEvent.toscaConcatFunction);
140         } else {
141             this.toscaFunctionForm.setValue(undefined);
142         }
143     }
144
145     onGetFunctionValidityChange(validationEvent: ToscaGetFunctionValidationEvent) {
146         if (validationEvent.isValid) {
147             this.toscaFunctionForm.setValue(validationEvent.toscaGetFunction);
148         } else {
149             this.toscaFunctionForm.setValue(undefined);
150         }
151     }
152
153     private emitValidityChange() {
154         const isValid = this.validate();
155         this.onValidityChange.emit({
156             isValid: isValid,
157             toscaFunction: isValid ? this.toscaFunctionForm.value : undefined
158         });
159     }
160 }
161
162 export class ToscaFunctionValidationEvent {
163     isValid: boolean;
164     toscaFunction: ToscaFunction;
165 }