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 {AttributeBEModel, 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, PROPERTY_TYPES} 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 {FormControl, FormGroup, Validators} from "@angular/forms";
35 import {ToscaGetFunctionTypeConverter} from "../../../../../models/tosca-get-function-type-converter";
38 selector: 'app-tosca-get-function',
39 templateUrl: './tosca-get-function.component.html',
40 styleUrls: ['./tosca-get-function.component.less']
42 export class ToscaGetFunctionComponent implements OnInit, OnChanges {
44 @Input() property: PropertyBEModel;
45 @Input() toscaGetFunction: ToscaGetFunction;
46 @Input() componentInstanceMap: Map<string, InstanceFeDetails> = new Map<string, InstanceFeDetails>();
47 @Input() functionType: ToscaGetFunctionType;
48 @Output() onValidFunction: EventEmitter<ToscaGetFunction> = new EventEmitter<ToscaGetFunction>();
49 @Output() onValidityChange: EventEmitter<ToscaGetFunctionValidationEvent> = new EventEmitter<ToscaGetFunctionValidationEvent>();
51 formGroup: FormGroup = new FormGroup({
52 'selectedProperty': new FormControl(undefined, Validators.required),
53 'propertySource': new FormControl(undefined, Validators.required)
56 isLoading: boolean = false;
57 propertyDropdownList: Array<PropertyDropdownValue> = [];
58 propertySourceList: Array<string> = [];
59 instanceNameAndIdMap: Map<string, string> = new Map<string, string>();
60 dropdownValuesLabel: string;
61 dropDownErrorMsg: string;
63 private isInitialized: boolean = false;
64 private componentMetadata: ComponentMetadata;
66 constructor(private topologyTemplateService: TopologyTemplateService,
67 private workspaceService: WorkspaceService,
68 private propertiesService: PropertiesService,
69 private dataTypeService: DataTypeService,
70 private translateService: TranslateService) {
74 this.componentMetadata = this.workspaceService.metadata;
75 this.formGroup.valueChanges.subscribe(() => {
76 if (!this.isInitialized) {
79 this.onValidityChange.emit({
80 isValid: this.formGroup.valid,
81 toscaGetFunction: this.formGroup.valid ? this.buildGetFunctionFromForm() : undefined
83 if (this.formGroup.valid) {
84 this.onValidFunction.emit(this.buildGetFunctionFromForm());
87 this.loadPropertySourceDropdown();
88 this.loadPropertyDropdownLabel();
89 this.initToscaGetFunction().subscribe(() => {
90 this.isInitialized = true;
95 ngOnChanges(_changes: SimpleChanges): void {
96 if (!this.isInitialized) {
99 this.isInitialized = false;
101 this.loadPropertySourceDropdown();
102 this.loadPropertyDropdownLabel();
103 this.initToscaGetFunction().subscribe(() => {
104 this.isInitialized = true;
108 private initToscaGetFunction(): Observable<void> {
109 return new Observable(subscriber => {
110 if (!this.toscaGetFunction) {
111 if (this.isGetInput()) {
112 this.setSelfPropertySource();
113 this.loadPropertyDropdown();
118 if (this.toscaGetFunction.propertySource == PropertySource.SELF) {
119 this.propertySource.setValue(PropertySource.SELF);
120 } else if (this.toscaGetFunction.propertySource == PropertySource.INSTANCE) {
122 .setValue(this.propertySourceList.find(source => this.toscaGetFunction.sourceName === source));
124 if (this.propertySource.valid) {
125 this.loadPropertyDropdown(() => {
126 this.selectedProperty
127 .setValue(this.propertyDropdownList.find(property => property.propertyName === this.toscaGetFunction.propertyName));
136 private buildGetFunctionFromForm() {
137 const toscaGetFunction = new ToscaGetFunction();
138 toscaGetFunction.type = ToscaGetFunctionTypeConverter.convertToToscaFunctionType(this.functionType);
139 toscaGetFunction.functionType = this.functionType;
140 const propertySource = this.propertySource.value;
141 if (this.isPropertySourceSelf()) {
142 toscaGetFunction.propertySource = propertySource
143 toscaGetFunction.sourceName = this.componentMetadata.name;
144 toscaGetFunction.sourceUniqueId = this.componentMetadata.uniqueId;
146 toscaGetFunction.propertySource = PropertySource.INSTANCE;
147 toscaGetFunction.sourceName = propertySource;
148 toscaGetFunction.sourceUniqueId = this.instanceNameAndIdMap.get(propertySource);
151 const selectedProperty: PropertyDropdownValue = this.selectedProperty.value;
152 toscaGetFunction.propertyUniqueId = selectedProperty.propertyId;
153 toscaGetFunction.propertyName = selectedProperty.propertyName;
154 toscaGetFunction.propertyPathFromSource = selectedProperty.propertyPath;
156 return toscaGetFunction;
159 private loadPropertySourceDropdown(): void {
160 if (this.isGetInput()) {
163 this.propertySourceList = [];
164 this.propertySourceList.push(PropertySource.SELF);
165 this.componentInstanceMap.forEach((value, key) => {
166 const instanceName = value.name;
167 this.instanceNameAndIdMap.set(instanceName, key);
168 if (instanceName !== PropertySource.SELF) {
169 this.addToPropertySource(instanceName);
174 private addToPropertySource(source: string): void {
175 this.propertySourceList.push(source);
176 this.propertySourceList.sort((a, b) => {
177 if (a === PropertySource.SELF) {
179 } else if (b === PropertySource.SELF) {
183 return a.localeCompare(b);
187 private loadPropertyDropdown(onComplete?: () => any): void {
188 this.loadPropertyDropdownLabel();
189 this.loadPropertyDropdownValues(onComplete);
192 private resetForm(): void {
193 this.formGroup.reset();
196 private loadPropertyDropdownLabel(): void {
197 if (!this.functionType) {
200 if (this.isGetInput()) {
201 this.dropdownValuesLabel = this.translateService.translate('INPUT_DROPDOWN_LABEL');
202 } else if (this.isGetProperty()) {
203 this.dropdownValuesLabel = this.translateService.translate('TOSCA_FUNCTION_PROPERTY_DROPDOWN_LABEL');
204 } else if (this.isGetAttribute()) {
205 this.dropdownValuesLabel = this.translateService.translate('TOSCA_FUNCTION_ATTRIBUTE_DROPDOWN_LABEL');
209 private loadPropertyDropdownValues(onComplete?: () => any): void {
210 if (!this.functionType) {
213 this.resetPropertyDropdown();
214 this.fillPropertyDropdownValues(onComplete);
217 private resetPropertyDropdown(): void {
218 this.dropDownErrorMsg = undefined;
219 this.selectedProperty.reset();
220 this.propertyDropdownList = [];
223 private fillPropertyDropdownValues(onComplete?: () => any): void {
225 const propertiesObservable: Observable<ComponentGenericResponse> = this.getPropertyObservable();
226 propertiesObservable.subscribe( (response: ComponentGenericResponse) => {
227 const properties: Array<PropertyBEModel | AttributeBEModel> = this.extractProperties(response);
228 if (!properties || properties.length === 0) {
229 const msgCode = this.getNotFoundMsgCode();
230 this.dropDownErrorMsg = this.translateService.translate(msgCode, {type: this.propertyTypeToString()});
233 this.addPropertiesToDropdown(properties);
234 if (this.propertyDropdownList.length == 0) {
235 const msgCode = this.getNotFoundMsgCode();
236 this.dropDownErrorMsg = this.translateService.translate(msgCode, {type: this.propertyTypeToString()});
239 console.error('An error occurred while loading properties.', error);
249 private getNotFoundMsgCode(): string {
250 if (this.isGetInput()) {
251 return 'TOSCA_FUNCTION_NO_INPUT_FOUND';
253 if (this.isGetAttribute()) {
254 return 'TOSCA_FUNCTION_NO_ATTRIBUTE_FOUND';
256 if (this.isGetProperty()) {
257 return 'TOSCA_FUNCTION_NO_PROPERTY_FOUND';
263 private propertyTypeToString() {
264 if (this.property.schemaType) {
265 return `${this.property.type} of ${this.property.schemaType}`;
267 return this.property.type;
270 private extractProperties(componentGenericResponse: ComponentGenericResponse): Array<PropertyBEModel | AttributeBEModel> {
271 if (this.isGetInput()) {
272 return componentGenericResponse.inputs;
274 const instanceId = this.instanceNameAndIdMap.get(this.propertySource.value);
275 if (this.isGetProperty()) {
276 if (this.isPropertySourceSelf()) {
277 return componentGenericResponse.properties;
279 return this.removeSelectedProperty(componentGenericResponse.componentInstancesProperties[instanceId]);
281 if (this.isPropertySourceSelf()) {
282 return [...(componentGenericResponse.attributes || []), ...(componentGenericResponse.properties || [])];
284 return [...(componentGenericResponse.componentInstancesAttributes[instanceId] || []),
285 ...(componentGenericResponse.componentInstancesProperties[instanceId] || [])];
288 private isPropertySourceSelf() {
289 return this.propertySource.value === PropertySource.SELF;
292 private getPropertyObservable(): Observable<ComponentGenericResponse> {
293 if (this.isGetInput()) {
294 return this.topologyTemplateService.getComponentInputsValues(this.componentMetadata.componentType, this.componentMetadata.uniqueId);
296 if (this.isGetProperty()) {
297 if (this.isPropertySourceSelf()) {
298 return this.topologyTemplateService.findAllComponentProperties(this.componentMetadata.componentType, this.componentMetadata.uniqueId);
300 return this.topologyTemplateService.getComponentInstanceProperties(this.componentMetadata.componentType, this.componentMetadata.uniqueId);
302 if (this.isGetAttribute()) {
303 if (this.isPropertySourceSelf()) {
304 return this.topologyTemplateService.findAllComponentAttributesAndProperties(this.componentMetadata.componentType, this.componentMetadata.uniqueId);
306 return this.topologyTemplateService.getComponentInstanceAttributesAndProperties(this.componentMetadata.uniqueId, this.componentMetadata.componentType);
310 private removeSelectedProperty(componentInstanceProperties: PropertyModel[]): PropertyModel[] {
311 if (!componentInstanceProperties) {
314 return componentInstanceProperties.filter(property =>
315 (property.uniqueId !== this.property.uniqueId) ||
316 (property.uniqueId === this.property.uniqueId && property.resourceInstanceUniqueId !== this.property.parentUniqueId)
320 private addPropertyToDropdown(propertyDropdownValue: PropertyDropdownValue): void {
321 this.propertyDropdownList.push(propertyDropdownValue);
322 this.propertyDropdownList.sort((a, b) => a.propertyLabel.localeCompare(b.propertyLabel));
325 private addPropertiesToDropdown(properties: Array<PropertyBEModel | AttributeBEModel>): void {
326 for (const property of properties) {
327 if (this.hasSameType(property)) {
328 this.addPropertyToDropdown({
329 propertyName: property.name,
330 propertyId: property.uniqueId,
331 propertyLabel: property.name,
332 propertyPath: [property.name]
334 } else if (this.isComplexType(property.type)) {
335 this.fillPropertyDropdownWithMatchingChildProperties(property);
340 private fillPropertyDropdownWithMatchingChildProperties(inputProperty: PropertyBEModel | AttributeBEModel,
341 parentPropertyList: Array<PropertyBEModel | AttributeBEModel> = []): void {
342 const dataTypeFound: DataTypeModel = this.dataTypeService.getDataTypeByModelAndTypeName(this.componentMetadata.model, inputProperty.type);
343 if (!dataTypeFound || !dataTypeFound.properties) {
346 parentPropertyList.push(inputProperty);
347 dataTypeFound.properties.forEach(dataTypeProperty => {
348 if (this.hasSameType(dataTypeProperty)) {
349 this.addPropertyToDropdown({
350 propertyName: dataTypeProperty.name,
351 propertyId: parentPropertyList[0].uniqueId,
352 propertyLabel: parentPropertyList.map(property => property.name).join('->') + '->' + dataTypeProperty.name,
353 propertyPath: [...parentPropertyList.map(property => property.name), dataTypeProperty.name]
355 } else if (this.isComplexType(dataTypeProperty.type)) {
356 this.fillPropertyDropdownWithMatchingChildProperties(dataTypeProperty, [...parentPropertyList])
361 private hasSameType(property: PropertyBEModel | AttributeBEModel) {
362 if (this.typeHasSchema(this.property.type)) {
363 if (!property.schema || !property.schema.property) {
366 return property.type === this.property.type && this.property.schema.property.type === property.schema.property.type;
369 return property.type === this.property.type;
372 private isGetProperty(): boolean {
373 return this.functionType === ToscaGetFunctionType.GET_PROPERTY;
376 private isGetAttribute(): boolean {
377 return this.functionType === ToscaGetFunctionType.GET_ATTRIBUTE;
380 private isGetInput(): boolean {
381 return this.functionType === ToscaGetFunctionType.GET_INPUT;
384 private isComplexType(propertyType: string): boolean {
385 return PROPERTY_DATA.SIMPLE_TYPES.indexOf(propertyType) === -1;
388 private typeHasSchema(propertyType: string): boolean {
389 return PROPERTY_TYPES.MAP === propertyType || PROPERTY_TYPES.LIST === propertyType;
392 private stopLoading(): void {
393 this.isLoading = false;
396 private startLoading(): void {
397 this.isLoading = true;
400 showPropertyDropdown(): boolean {
401 if (this.isGetProperty() || this.isGetAttribute()) {
402 return this.propertySource.valid && !this.isLoading && !this.dropDownErrorMsg;
405 return this.functionType && !this.isLoading && !this.dropDownErrorMsg;
408 onPropertySourceChange(): void {
409 if (!this.functionType || !this.propertySource.valid) {
412 this.loadPropertyDropdown();
415 showPropertySourceDropdown(): boolean {
416 return this.isGetProperty() || this.isGetAttribute();
419 private setSelfPropertySource(): void {
420 this.propertySource.setValue(PropertySource.SELF);
423 private get propertySource(): FormControl {
424 return this.formGroup.get('propertySource') as FormControl;
427 private get selectedProperty(): FormControl {
428 return this.formGroup.get('selectedProperty') as FormControl;
433 export interface PropertyDropdownValue {
434 propertyName: string;
436 propertyLabel: string;
437 propertyPath: Array<string>;
440 export interface ToscaGetFunctionValidationEvent {
442 toscaGetFunction: ToscaGetFunction,