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, DataTypeModel, PropertyBEModel, PropertyModel} from 'app/models';
22 import {TopologyTemplateService} from "../../../services/component-services/topology-template.service";
23 import {WorkspaceService} from "../../workspace/workspace.service";
24 import {PropertiesService} from "../../../services/properties.service";
25 import {PROPERTY_DATA} from "../../../../utils/constants";
26 import {DataTypeService} from "../../../services/data-type.service";
27 import {ToscaGetFunctionType} from "../../../../models/tosca-get-function-type";
28 import {TranslateService} from "../../../shared/translator/translate.service";
29 import {ComponentGenericResponse} from '../../../services/responses/component-generic-response';
30 import {Observable} from 'rxjs/Observable';
31 import {PropertySource} from "../../../../models/property-source";
32 import {InstanceFeDetails} from "../../../../models/instance-fe-details";
33 import {ToscaGetFunction} from "../../../../models/tosca-get-function";
34 import {AbstractControl, FormControl, FormGroup, ValidationErrors, ValidatorFn} from "@angular/forms";
37 selector: 'tosca-function',
38 templateUrl: './tosca-function.component.html',
39 styleUrls: ['./tosca-function.component.less'],
41 export class ToscaFunctionComponent implements OnInit {
43 @Input() property: PropertyBEModel;
44 @Input() componentInstanceMap: Map<string, InstanceFeDetails> = new Map<string, InstanceFeDetails>();
45 @Input() allowClear: boolean = true;
46 @Output() onValidFunction: EventEmitter<ToscaGetFunction> = new EventEmitter<ToscaGetFunction>();
47 @Output() onValidityChange: EventEmitter<boolean> = new EventEmitter<boolean>();
49 toscaGetFunctionValidator: ValidatorFn = (control: AbstractControl): ValidationErrors | null => {
50 const toscaGetFunction: ToscaGetFunction = control.value;
51 const errors: ValidationErrors = {};
52 if (!toscaGetFunction.sourceName) {
53 errors.sourceName = { required: true };
55 if (!toscaGetFunction.functionType) {
56 errors.functionType = { required: true };
58 if (!toscaGetFunction.sourceUniqueId) {
59 errors.sourceUniqueId = { required: true };
61 if (!toscaGetFunction.sourceName) {
62 errors.sourceName = { required: true };
64 if (!toscaGetFunction.propertyPathFromSource) {
65 errors.propertyPathFromSource = { required: true };
67 if (!toscaGetFunction.propertyName) {
68 errors.propertyName = { required: true };
70 if (!toscaGetFunction.propertySource) {
71 errors.propertySource = { required: true };
73 return errors ? errors : null;
76 toscaGetFunctionForm: FormControl = new FormControl(new ToscaGetFunction(undefined), [this.toscaGetFunctionValidator]);
77 formGroup: FormGroup = new FormGroup({
78 'toscaGetFunction': this.toscaGetFunctionForm
81 TOSCA_FUNCTION_GET_PROPERTY = ToscaGetFunctionType.GET_PROPERTY;
83 selectedProperty: PropertyDropdownValue;
84 isLoading: boolean = false;
85 propertyDropdownList: Array<PropertyDropdownValue> = [];
86 toscaFunctions: Array<string> = [];
87 propertySourceList: Array<string> = [];
88 instanceNameAndIdMap: Map<string, string> = new Map<string, string>();
89 dropdownValuesLabel: string;
90 dropDownErrorMsg: string;
91 propertySource: string
92 toscaGetFunction: ToscaGetFunction = new ToscaGetFunction(undefined);
94 private componentMetadata: ComponentMetadata;
96 constructor(private topologyTemplateService: TopologyTemplateService,
97 private workspaceService: WorkspaceService,
98 private propertiesService: PropertiesService,
99 private dataTypeService: DataTypeService,
100 private translateService: TranslateService) {
104 this.componentMetadata = this.workspaceService.metadata;
105 this.loadToscaFunctions();
106 this.loadPropertySourceDropdown();
107 this.initToscaGetFunction();
108 this.toscaGetFunctionForm.valueChanges.subscribe(toscaGetFunction => {
109 this.onValidityChange.emit(this.toscaGetFunctionForm.valid);
110 if (this.toscaGetFunctionForm.valid) {
111 this.onValidFunction.emit(toscaGetFunction);
116 private initToscaGetFunction(): void {
117 if (!this.property.isToscaGetFunction()) {
120 this.toscaGetFunction = new ToscaGetFunction(this.property.toscaGetFunction);
121 this.toscaGetFunctionForm.setValue(this.toscaGetFunction);
122 if (this.toscaGetFunction.functionType === ToscaGetFunctionType.GET_PROPERTY) {
123 if (this.toscaGetFunction.propertySource === PropertySource.SELF) {
124 this.propertySource = PropertySource.SELF;
126 this.propertySource = this.toscaGetFunction.sourceName;
129 if (this.toscaGetFunction.propertyName) {
130 this.loadPropertyDropdown(() => {
131 this.selectedProperty = this.propertyDropdownList.find(property => property.propertyName === this.toscaGetFunction.propertyName)
136 private loadToscaFunctions(): void {
137 this.toscaFunctions.push(ToscaGetFunctionType.GET_INPUT);
138 this.toscaFunctions.push(ToscaGetFunctionType.GET_PROPERTY);
141 private loadPropertySourceDropdown(): void {
142 this.propertySourceList.push(PropertySource.SELF);
143 this.componentInstanceMap.forEach((value, key) => {
144 const instanceName = value.name;
145 this.instanceNameAndIdMap.set(instanceName, key);
146 if (instanceName !== PropertySource.SELF) {
147 this.addToPropertySource(instanceName);
152 private addToPropertySource(source: string): void {
153 this.propertySourceList.push(source);
154 this.propertySourceList.sort((a, b) => {
155 if (a === PropertySource.SELF) {
157 } else if (b === PropertySource.SELF) {
161 return a.localeCompare(b);
165 onToscaFunctionChange(): void {
166 this.resetPropertySource();
167 this.resetPropertyDropdown();
168 if (this.isGetInputSelected()) {
169 this.setSelfPropertySource();
170 this.loadPropertyDropdown();
174 private loadPropertyDropdown(onComplete?: () => any): void {
175 this.loadPropertyDropdownLabel();
176 this.loadPropertyDropdownValues(onComplete);
179 private resetForm(): void {
180 this.toscaGetFunction = new ToscaGetFunction(undefined);
181 this.toscaGetFunctionForm.setValue(new ToscaGetFunction(undefined));
182 this.propertySource = undefined;
183 this.selectedProperty = undefined;
186 private resetPropertySource(): void {
187 this.toscaGetFunction.propertyUniqueId = undefined;
188 this.toscaGetFunction.propertyName = undefined;
189 this.toscaGetFunction.propertySource = undefined;
190 this.toscaGetFunction.sourceUniqueId = undefined;
191 this.toscaGetFunction.sourceName = undefined;
192 this.toscaGetFunction.propertyPathFromSource = undefined;
193 this.propertySource = undefined;
194 this.selectedProperty = undefined;
196 const toscaGetFunction1 = new ToscaGetFunction(undefined);
197 toscaGetFunction1.functionType = this.toscaGetFunction.functionType;
198 this.toscaGetFunctionForm.setValue(toscaGetFunction1);
201 private loadPropertyDropdownLabel(): void {
202 if (!this.toscaGetFunction.functionType) {
205 if (this.isGetInputSelected()) {
206 this.dropdownValuesLabel = this.translateService.translate('INPUT_DROPDOWN_LABEL');
207 } else if (this.isGetPropertySelected()) {
208 this.dropdownValuesLabel = this.translateService.translate('TOSCA_FUNCTION_PROPERTY_DROPDOWN_LABEL');
212 private loadPropertyDropdownValues(onComplete?: () => any): void {
213 if (!this.toscaGetFunction.functionType) {
216 this.resetPropertyDropdown();
217 this.fillPropertyDropdownValues(onComplete);
220 private resetPropertyDropdown(): void {
221 this.dropDownErrorMsg = undefined;
222 this.selectedProperty = undefined;
223 this.propertyDropdownList = [];
226 private fillPropertyDropdownValues(onComplete?: () => any): void {
228 const propertiesObservable: Observable<ComponentGenericResponse> = this.getPropertyObservable();
229 propertiesObservable.subscribe( (response: ComponentGenericResponse) => {
230 const properties: PropertyBEModel[] = this.extractProperties(response);
231 if (!properties || properties.length === 0) {
232 const msgCode = this.isGetInputSelected() ? 'TOSCA_FUNCTION_NO_INPUT_FOUND' : 'TOSCA_FUNCTION_NO_PROPERTY_FOUND';
233 this.dropDownErrorMsg = this.translateService.translate(msgCode, {type: this.propertyTypeToString()});
236 this.addPropertiesToDropdown(properties);
237 if (this.propertyDropdownList.length == 0) {
238 const msgCode = this.isGetInputSelected() ? 'TOSCA_FUNCTION_NO_INPUT_FOUND' : 'TOSCA_FUNCTION_NO_PROPERTY_FOUND';
239 this.dropDownErrorMsg = this.translateService.translate(msgCode, {type: this.propertyTypeToString()});
242 console.error('An error occurred while loading properties.', error);
251 private propertyTypeToString() {
252 if (this.property.schemaType) {
253 return `${this.property.type} of ${this.property.schemaType}`;
255 return this.property.type;
258 private extractProperties(componentGenericResponse: ComponentGenericResponse): PropertyBEModel[] {
259 if (this.isGetInputSelected()) {
260 return componentGenericResponse.inputs;
262 if (this.isGetPropertySelected()) {
263 if (this.propertySource === PropertySource.SELF) {
264 return componentGenericResponse.properties;
266 const componentInstanceProperties: PropertyModel[] = componentGenericResponse.componentInstancesProperties[this.instanceNameAndIdMap.get(this.propertySource)];
267 return this.removeSelectedProperty(componentInstanceProperties);
271 private getPropertyObservable(): Observable<ComponentGenericResponse> {
272 if (this.isGetInputSelected()) {
273 return this.topologyTemplateService.getComponentInputsValues(this.componentMetadata.componentType, this.componentMetadata.uniqueId);
275 if (this.isGetPropertySelected()) {
276 if (this.propertySource === PropertySource.SELF) {
277 return this.topologyTemplateService.findAllComponentProperties(this.componentMetadata.componentType, this.componentMetadata.uniqueId);
279 return this.topologyTemplateService.getComponentInstanceProperties(this.componentMetadata.componentType, this.componentMetadata.uniqueId);
283 private removeSelectedProperty(componentInstanceProperties: PropertyModel[]): PropertyModel[] {
284 if (!componentInstanceProperties) {
287 return componentInstanceProperties.filter(property =>
288 (property.uniqueId !== this.property.uniqueId) ||
289 (property.uniqueId === this.property.uniqueId && property.resourceInstanceUniqueId !== this.property.parentUniqueId)
293 private addPropertyToDropdown(propertyDropdownValue: PropertyDropdownValue): void {
294 this.propertyDropdownList.push(propertyDropdownValue);
295 this.propertyDropdownList.sort((a, b) => a.propertyLabel.localeCompare(b.propertyLabel));
298 private addPropertiesToDropdown(properties: PropertyBEModel[]): void {
299 for (const property of properties) {
300 if (this.hasSameType(property)) {
301 this.addPropertyToDropdown({
302 propertyName: property.name,
303 propertyId: property.uniqueId,
304 propertyLabel: property.name,
305 propertyPath: [property.name]
307 } else if (this.isComplexType(property.type)) {
308 this.fillPropertyDropdownWithMatchingChildProperties(property);
313 private fillPropertyDropdownWithMatchingChildProperties(inputProperty: PropertyBEModel, parentPropertyList: Array<PropertyBEModel> = []): void {
314 const dataTypeFound: DataTypeModel = this.dataTypeService.getDataTypeByModelAndTypeName(this.componentMetadata.model, inputProperty.type);
315 if (!dataTypeFound || !dataTypeFound.properties) {
318 parentPropertyList.push(inputProperty);
319 dataTypeFound.properties.forEach(dataTypeProperty => {
320 if (this.hasSameType(dataTypeProperty)) {
321 this.addPropertyToDropdown({
322 propertyName: dataTypeProperty.name,
323 propertyId: parentPropertyList[0].uniqueId,
324 propertyLabel: parentPropertyList.map(property => property.name).join('->') + '->' + dataTypeProperty.name,
325 propertyPath: [...parentPropertyList.map(property => property.name), dataTypeProperty.name]
327 } else if (this.isComplexType(dataTypeProperty.type)) {
328 this.fillPropertyDropdownWithMatchingChildProperties(dataTypeProperty, [...parentPropertyList])
333 private hasSameType(property: PropertyBEModel) {
334 if (this.property.schema && this.property.schema.property) {
335 if (!property.schema || !property.schema.property) {
338 return property.type === this.property.type && this.property.schema.property.type === property.schema.property.type;
340 return property.type === this.property.type;
343 private isGetPropertySelected(): boolean {
344 return this.toscaGetFunction.functionType === ToscaGetFunctionType.GET_PROPERTY;
347 private isGetInputSelected(): boolean {
348 return this.toscaGetFunction.functionType === ToscaGetFunctionType.GET_INPUT;
351 private isComplexType(propertyType: string): boolean {
352 return PROPERTY_DATA.SIMPLE_TYPES.indexOf(propertyType) === -1;
355 private stopLoading(): void {
356 this.isLoading = false;
359 private startLoading(): void {
360 this.isLoading = true;
363 showDropdown(): boolean {
364 if (this.toscaGetFunction.functionType === ToscaGetFunctionType.GET_PROPERTY) {
365 return this.toscaGetFunction.propertySource && !this.isLoading && !this.dropDownErrorMsg;
368 return this.toscaGetFunction.functionType && !this.isLoading && !this.dropDownErrorMsg;
371 onPropertySourceChange(): void {
372 if (!this.toscaGetFunction.functionType || !this.propertySource) {
375 this.toscaGetFunction.propertyUniqueId = undefined;
376 this.toscaGetFunction.propertyName = undefined;
377 this.toscaGetFunction.propertyPathFromSource = undefined;
378 if (this.propertySource === PropertySource.SELF) {
379 this.setSelfPropertySource();
381 this.toscaGetFunction.propertySource = PropertySource.INSTANCE;
382 this.toscaGetFunction.sourceName = this.propertySource;
383 this.toscaGetFunction.sourceUniqueId = this.instanceNameAndIdMap.get(this.propertySource);
385 this.toscaGetFunctionForm.setValue(this.toscaGetFunction);
386 this.loadPropertyDropdown();
389 private setSelfPropertySource(): void {
390 this.toscaGetFunction.propertySource = PropertySource.SELF;
391 this.toscaGetFunction.sourceName = this.componentMetadata.name;
392 this.toscaGetFunction.sourceUniqueId = this.componentMetadata.uniqueId;
393 this.toscaGetFunctionForm.setValue(this.toscaGetFunction);
396 onPropertyChange(): void {
397 this.toscaGetFunction.propertyUniqueId = this.selectedProperty.propertyId;
398 this.toscaGetFunction.propertyName = this.selectedProperty.propertyName;
399 this.toscaGetFunction.propertyPathFromSource = this.selectedProperty.propertyPath;
400 this.toscaGetFunctionForm.setValue(this.toscaGetFunction);
407 showClearButton(): boolean {
408 return this.allowClear && this.toscaGetFunction.functionType !== undefined;
412 export interface PropertyDropdownValue {
413 propertyName: string;
415 propertyLabel: string;
416 propertyPath: Array<string>;