fdaf2f3573161be8c86c839bcab185f92ab59a3c
[sdc.git] / catalog-ui / src / app / ng2 / pages / service-dependencies-editor / service-dependencies-editor.component.ts
1 /*!
2  * Copyright © 2016-2018 European Support Limited
3  * Modification Copyright (C) 2022 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  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14  * or implied. See the License for the specific language governing
15  * permissions and limitations under the License.
16  */
17 import {Component, Input, OnInit} from '@angular/core';
18 import {InputBEModel, PropertyBEModel, PropertyFEModel, PropertyModel} from 'app/models';
19 import {SourceType} from 'app/ng2/components/logic/service-dependencies/service-dependencies.component';
20 import {DropdownValue} from 'app/ng2/components/ui/form-components/dropdown/ui-element-dropdown.component';
21 import {ServiceServiceNg2} from 'app/ng2/services/component-services/service.service';
22 import {PROPERTY_DATA, PROPERTY_TYPES} from 'app/utils';
23 import {PropertiesUtils} from '../properties-assignment/services/properties.utils';
24 import {ToscaFunctionValidationEvent} from "../properties-assignment/tosca-function/tosca-function.component";
25 import {InstanceFeDetails} from "../../../models/instance-fe-details";
26 import {CompositionService} from "../composition/composition.service";
27 import {ToscaGetFunction} from "../../../models/tosca-get-function";
28 import {PropertyFilterConstraintUi} from "../../../models/ui-models/property-filter-constraint-ui";
29 import {ConstraintOperatorType, FilterConstraintHelper} from "../../../utils/filter-constraint-helper";
30 import {ToscaFunctionHelper} from "../../../utils/tosca-function-helper";
31 import {TopologyTemplateService} from "app/ng2/services/component-services/topology-template.service";
32 import {CustomToscaFunction} from "../../../models/default-custom-functions";
33 import {ToscaFunction} from "../../../models/tosca-function";
34 import {ToscaCustomFunction} from "../../../models/tosca-custom-function";
35
36 @Component({
37     selector: 'service-dependencies-editor',
38     templateUrl: './service-dependencies-editor.component.html',
39     styleUrls: ['./service-dependencies-editor.component.less'],
40     providers: [ServiceServiceNg2]
41 })
42 export class ServiceDependenciesEditorComponent implements OnInit {
43
44     @Input() serviceRuleIndex: number;
45     @Input() serviceRules: PropertyFilterConstraintUi[];
46     @Input() compositeServiceName: string;
47     @Input() currentServiceName: string;
48     @Input() parentServiceInputs: InputBEModel[];
49     @Input() parentServiceProperties: PropertyBEModel[];
50     @Input() selectedInstanceProperties: PropertyBEModel[];
51     @Input() allowedOperators: ConstraintOperatorType[] = [
52         ConstraintOperatorType.GREATER_THAN,
53         ConstraintOperatorType.LESS_THAN,
54         ConstraintOperatorType.EQUAL,
55         ConstraintOperatorType.GREATER_OR_EQUAL,
56         ConstraintOperatorType.LESS_OR_EQUAL,
57         ConstraintOperatorType.IN_RANGE,
58         ConstraintOperatorType.VALID_VALUES,
59         ConstraintOperatorType.LENGTH,
60         ConstraintOperatorType.MIN_LENGTH,
61         ConstraintOperatorType.MAX_LENGTH,
62         ConstraintOperatorType.PATTERN
63     ];
64     @Input() comparableAllowedOperators: ConstraintOperatorType[] = [
65         ConstraintOperatorType.GREATER_THAN,
66         ConstraintOperatorType.LESS_THAN,
67         ConstraintOperatorType.EQUAL,
68         ConstraintOperatorType.GREATER_OR_EQUAL,
69         ConstraintOperatorType.LESS_OR_EQUAL,
70         ConstraintOperatorType.IN_RANGE,
71         ConstraintOperatorType.VALID_VALUES,
72     ];
73     @Input() capabilityNameAndPropertiesMap: Map<string, PropertyModel[]>;
74     @Input() filterType: FilterType;
75     @Input() filterConstraint: PropertyFilterConstraintUi;
76   @Input() customToscaFunctions: Array<CustomToscaFunction>;
77     //output
78     currentRule: PropertyFilterConstraintUi;
79
80     FILTER_TYPE_CAPABILITY: FilterType = FilterType.CAPABILITY
81
82     listAllowedOperators: ConstraintOperatorType[] = [
83         ConstraintOperatorType.EQUAL,
84         ConstraintOperatorType.LENGTH,
85         ConstraintOperatorType.MIN_LENGTH,
86         ConstraintOperatorType.MAX_LENGTH
87     ];
88
89     operatorTypes: DropdownValue[] = [
90         {label: FilterConstraintHelper.convertToSymbol(ConstraintOperatorType.GREATER_THAN), value: ConstraintOperatorType.GREATER_THAN},
91         {label: FilterConstraintHelper.convertToSymbol(ConstraintOperatorType.LESS_THAN), value: ConstraintOperatorType.LESS_THAN},
92         {label: FilterConstraintHelper.convertToSymbol(ConstraintOperatorType.EQUAL), value: ConstraintOperatorType.EQUAL},
93         {label: FilterConstraintHelper.convertToSymbol(ConstraintOperatorType.GREATER_OR_EQUAL), value: ConstraintOperatorType.GREATER_OR_EQUAL},
94         {label: FilterConstraintHelper.convertToSymbol(ConstraintOperatorType.LESS_OR_EQUAL), value: ConstraintOperatorType.LESS_OR_EQUAL}
95     ];
96     lengthArray: string[] = [ConstraintOperatorType.LENGTH,
97         ConstraintOperatorType.MIN_LENGTH,
98         ConstraintOperatorType.MAX_LENGTH];
99
100     servicePropertyDropdownList: DropdownValue[];
101     isLoading: false;
102     selectedProperty: PropertyFEModel;
103     selectedSourceType: string;
104     componentInstanceMap: Map<string, InstanceFeDetails> = new Map<string, InstanceFeDetails>();
105
106     capabilityDropdownList: DropdownValue[] = [];
107     validValuesToscaFunctionList: ToscaFunction[];
108     rangeToscaFunctionList: ToscaFunction[];
109     overridingType = PROPERTY_TYPES.INTEGER;
110
111     SOURCE_TYPES = {
112         STATIC: {label: 'Static', value: SourceType.STATIC},
113         TOSCA_FUNCTION: {label: 'Tosca Function', value: SourceType.TOSCA_FUNCTION},
114         TOSCA_FUNCTION_LIST: {label: 'Tosca Function List', value: SourceType.TOSCA_FUNCTION_LIST}
115     };
116
117     constructor(private propertiesUtils: PropertiesUtils, private compositionService: CompositionService, private topologyTemplateService: TopologyTemplateService) {
118     }
119
120     ngOnInit(): void {
121         if (this.compositionService.componentInstances) {
122             this.compositionService.componentInstances.forEach(value => {
123                 this.componentInstanceMap.set(value.uniqueId, <InstanceFeDetails>{
124                     name: value.name
125                 });
126             });
127         }
128         this.initCustomToscaFunctions();
129         this.initCapabilityDropdown();
130         this.initCurrentRule();
131         this.initConstraintOperatorOptions();
132         this.initSelectedSourceType();
133         this.initPropertyDropdown();
134         this.syncRuleData();
135         this.generateRangeToscaFunctionList();
136     }
137
138     private initCustomToscaFunctions() {
139       if (!this.customToscaFunctions) {
140           this.customToscaFunctions = [];
141           this.topologyTemplateService.getDefaultCustomFunction().toPromise().then((data) => {
142               for (let customFunction of data) {
143                   this.customToscaFunctions.push(new CustomToscaFunction(customFunction));
144               }
145           });
146       }
147  }
148
149     private initCapabilityDropdown(): void {
150         if (this.filterType == FilterType.CAPABILITY) {
151             this.capabilityDropdownList = [
152                 new DropdownValue(undefined, 'Select'),
153                 ...Array.from(this.capabilityNameAndPropertiesMap.keys()).map(capabilityName => new DropdownValue(capabilityName, capabilityName))
154             ];
155         }
156     }
157
158     private initPropertyDropdown(): void {
159         let propertyList: PropertyBEModel[] = [];
160         if (this.filterType == FilterType.CAPABILITY) {
161             if (this.currentRule.capabilityName) {
162                 propertyList = this.capabilityNameAndPropertiesMap.get(this.currentRule.capabilityName);
163             }
164         } else {
165             propertyList = this.selectedInstanceProperties;
166         }
167         let selectLabel;
168         if (this.filterType == FilterType.CAPABILITY) {
169             selectLabel = this.currentRule.capabilityName ? 'Select' : 'Select a Capability';
170         } else {
171             selectLabel = 'Select';
172         }
173         this.servicePropertyDropdownList = [new DropdownValue(undefined, selectLabel), ...propertyList.map(prop => new DropdownValue(prop.name, prop.name)).sort((prop1, prop2) => prop1.value.localeCompare(prop2.value))];
174     }
175
176     private initConstraintOperatorOptions(): void {
177         if (!this.selectedProperty) {
178             this.operatorTypes = [this.setOperatorDropdownValue(undefined)];
179             return;
180         }
181         const operatorList: DropdownValue[] = [];
182         switch (true) {
183             case this.selectedProperty.type === PROPERTY_TYPES.RANGE:
184                 if (this.currentRule.constraintOperator !== ConstraintOperatorType.IN_RANGE) {
185                     this.currentRule.constraintOperator = ConstraintOperatorType.IN_RANGE;
186                 }
187                 this.operatorTypes = [this.setOperatorDropdownValue(ConstraintOperatorType.IN_RANGE)];
188                 break;
189             case this.selectedProperty.type === PROPERTY_TYPES.STRING:
190                 this.allowedOperators.forEach(constraintOperatorType =>
191                     operatorList.push(this.setOperatorDropdownValue(constraintOperatorType))
192                 );
193                 this.operatorTypes = operatorList;
194                 break;
195             case  this.selectedProperty.type != PROPERTY_TYPES.STRING &&
196             ((PROPERTY_DATA.SIMPLE_TYPES_COMPARABLE.indexOf(this.selectedProperty.type) > -1) ||
197                 (PROPERTY_DATA.COMPARABLE_TYPES.indexOf(this.selectedProperty.type) > -1)):
198                 this.comparableAllowedOperators.forEach(constraintOperatorType =>
199                     operatorList.push(this.setOperatorDropdownValue(constraintOperatorType))
200                 );
201                 this.operatorTypes = operatorList;
202                 break;
203             case this.selectedProperty.type === PROPERTY_TYPES.LIST:
204                 this.listAllowedOperators.forEach(constraintOperatorType =>
205                     operatorList.push(this.setOperatorDropdownValue(constraintOperatorType))
206                 );
207                 this.operatorTypes = operatorList;
208                 break;
209             default:
210                 if (this.currentRule.constraintOperator !== ConstraintOperatorType.EQUAL) {
211                     this.currentRule.constraintOperator = ConstraintOperatorType.EQUAL;
212                 }
213                 this.operatorTypes = [this.setOperatorDropdownValue(ConstraintOperatorType.EQUAL)];
214                 break;
215         }
216     }
217
218     private setOperatorDropdownValue(constraintOperatorType: ConstraintOperatorType) {
219         if (constraintOperatorType === undefined) {
220             return new DropdownValue(undefined, 'Select a Property');
221         }
222         return new DropdownValue(constraintOperatorType, FilterConstraintHelper.convertToSymbol(constraintOperatorType));
223     }
224
225     private initSelectedSourceType(): void {
226         if (!this.currentRule.sourceType || this.currentRule.sourceType === SourceType.STATIC) {
227             this.selectedSourceType = SourceType.STATIC;
228         } else {
229             if (!this.isValidValuesOperator() && !this.isRangeType() && !this.isInRangeOperator()) {
230                 this.selectedSourceType = SourceType.TOSCA_FUNCTION;
231             } else {
232                 this.selectedSourceType = SourceType.TOSCA_FUNCTION_LIST;
233             }
234         }
235     }
236
237     private initCurrentRule(): void {
238         if (this.filterConstraint) {
239             this.currentRule = new PropertyFilterConstraintUi(this.filterConstraint);
240         } else {
241             this.currentRule = new PropertyFilterConstraintUi({
242                 sourceName: SourceType.STATIC,
243                 sourceType: SourceType.STATIC,
244                 constraintOperator: ConstraintOperatorType.EQUAL,
245                 value: undefined,
246                 originalType: undefined
247             });
248         }
249         let propertyList: PropertyBEModel[] = [];
250         if (this.filterType == FilterType.CAPABILITY) {
251             if (this.currentRule.capabilityName) {
252                 propertyList = this.capabilityNameAndPropertiesMap.get(this.currentRule.capabilityName);
253             }
254         } else {
255             propertyList = this.selectedInstanceProperties;
256         }
257         if (this.filterConstraint) {
258             this.filterConstraint.originalType = propertyList.find(prop => prop.name == this.filterConstraint.servicePropertyName).type;
259         }
260     }
261
262     onCapabilityChange(): void {
263         this.initPropertyDropdown();
264         this.resetSelectedProperty();
265     }
266
267     onPropertyChange(): void {
268         this.currentRule.value = undefined;
269         this.onValueChange(false);
270         this.updateSelectedProperty();
271         this.initConstraintOperatorOptions();
272     }
273
274     syncRuleData(): void {
275         if (!this.currentRule.sourceName || this.currentRule.sourceType === SourceType.STATIC) {
276             this.currentRule.sourceName = SourceType.STATIC;
277             this.currentRule.sourceType = SourceType.STATIC;
278         }
279         this.initSelectedProperty();
280         this.initConstraintOperatorOptions();
281     }
282
283     onValueChange(isValidValue: any): void {
284         this.currentRule.updateValidity(isValidValue);
285     }
286
287     checkFormValidForSubmit(): boolean {
288         return this.currentRule.isValidRule();
289     }
290
291     initSelectedProperty(): void {
292         if (!this.currentRule.servicePropertyName) {
293             this.selectedProperty = undefined;
294             return;
295         }
296         let newProperty;
297         if (this.filterType === FilterType.CAPABILITY) {
298             const currentProperty = this.capabilityNameAndPropertiesMap.get(this.currentRule.capabilityName)
299                 .find(property => property.name === this.currentRule.servicePropertyName);
300             newProperty = new PropertyFEModel(currentProperty);
301         } else {
302             newProperty = new PropertyFEModel(this.selectedInstanceProperties.find(property => property.name === this.currentRule.servicePropertyName));
303         }
304         newProperty.value = undefined;
305         newProperty.toscaFunction = undefined;
306         if (typeof this.currentRule.value === 'string') {
307             newProperty.value = this.currentRule.value;
308             this.propertiesUtils.initValueObjectRef(newProperty);
309         } else if (ToscaFunctionHelper.isValueToscaFunction(this.currentRule.value)) {
310             newProperty.toscaFunction = ToscaFunctionHelper.convertObjectToToscaFunction(this.currentRule.value);
311             newProperty.value = newProperty.toscaFunction.buildValueString();
312         } else if (Array.isArray(this.currentRule.value) &&
313             typeof this.currentRule.value[0] === "object") {
314             this.validValuesToscaFunctionList = this.currentRule.value;
315             this.rangeToscaFunctionList = this.currentRule.value;
316             newProperty.toscaFunction = this.currentRule.value;
317         } else {
318             newProperty.value = JSON.stringify(this.currentRule.value);
319             this.propertiesUtils.initValueObjectRef(newProperty);
320         }
321
322         this.selectedProperty = newProperty;
323         this.currentRule.originalType = this.selectedProperty.type;
324     }
325
326     updateSelectedProperty(): void {
327         this.selectedProperty = undefined;
328         if (!this.currentRule.servicePropertyName) {
329             return;
330         }
331
332         let newProperty;
333         if (this.filterType === FilterType.CAPABILITY) {
334             const currentProperty = this.capabilityNameAndPropertiesMap.get(this.currentRule.capabilityName)
335                 .find(property => property.name === this.currentRule.servicePropertyName);
336             newProperty = new PropertyFEModel(currentProperty);
337         } else {
338             newProperty = new PropertyFEModel(this.selectedInstanceProperties.find(property => property.name === this.currentRule.servicePropertyName));
339         }
340         newProperty.value = undefined;
341         newProperty.toscaFunction = undefined;
342
343         this.propertiesUtils.initValueObjectRef(newProperty);
344         this.selectedProperty = newProperty;
345         this.currentRule.originalType = this.selectedProperty.type;
346     }
347
348     isStaticSource(): boolean {
349         return this.selectedSourceType === SourceType.STATIC
350     }
351
352     isToscaFunctionSource(): boolean {
353         return this.selectedSourceType === SourceType.TOSCA_FUNCTION
354     }
355
356     isToscaFunctionListSource(): boolean {
357         return this.selectedSourceType === SourceType.TOSCA_FUNCTION_LIST
358     }
359
360     isComplexListMapType(): boolean {
361         return this.selectedProperty && this.selectedProperty.derivedDataType > 0;
362     }
363
364     isRangeType(): boolean {
365         return this.selectedProperty && this.selectedProperty.derivedDataType == 4;
366     }
367
368     isLengthOperator(): boolean {
369         return this.lengthArray.indexOf(this.currentRule.constraintOperator) > -1;
370     }
371
372     isInRangeOperator(): boolean {
373         return this.currentRule.constraintOperator && this.currentRule.constraintOperator === ConstraintOperatorType.IN_RANGE;
374     }
375
376     isValidValuesOperator(): boolean {
377         return this.currentRule.constraintOperator && this.currentRule.constraintOperator === ConstraintOperatorType.VALID_VALUES;
378     }
379
380     updateComplexListMapTypeRuleValue(): void {
381         this.currentRule.value = PropertyFEModel.cleanValueObj(this.selectedProperty.valueObj);
382         this.onValueChange(this.selectedProperty.valueObjIsValid);
383     }
384
385     onToscaFunctionValidityChange(validationEvent: ToscaFunctionValidationEvent): void {
386         if (validationEvent.isValid && validationEvent.toscaFunction) {
387             if (this.isValidValuesOperator()) {
388                 this.currentRule.value = this.validValuesToscaFunctionList;
389                 this.currentRule.sourceType = SourceType.TOSCA_FUNCTION_LIST;
390                 if (validationEvent.toscaFunction instanceof ToscaGetFunction) {
391                     this.currentRule.sourceName = SourceType.TOSCA_FUNCTION_LIST;
392                 }
393                 else if (validationEvent.toscaFunction instanceof ToscaCustomFunction) {
394                     this.currentRule.sourceName = SourceType.TOSCA_FUNCTION_LIST;
395                     this.currentRule.sourceType = SourceType.TOSCA_FUNCTION_LIST;
396                 }
397             } else {
398                 if (this.isLengthOperator()) {
399                     this.overridingType = PROPERTY_TYPES.INTEGER;
400                 }
401                 this.currentRule.value = validationEvent.toscaFunction;
402                 this.currentRule.sourceType = validationEvent.toscaFunction.type
403                 if (validationEvent.toscaFunction instanceof ToscaGetFunction) {
404                     this.currentRule.sourceName = validationEvent.toscaFunction.sourceName;
405                 }
406             }
407         } else {
408             this.currentRule.updateValidity(false);
409             this.currentRule.value = undefined;
410             this.currentRule.sourceType = undefined;
411             this.currentRule.sourceName = undefined;
412         }
413     }
414
415     onToscaFunctionListValidityChange(validationEvent: ToscaFunctionValidationEvent, valueIndex: number): void {
416         if (validationEvent.isValid && validationEvent.toscaFunction) {
417             this.validValuesToscaFunctionList.splice(this.validValuesToscaFunctionList.length - 1, 1, validationEvent.toscaFunction);
418             this.currentRule.value = this.validValuesToscaFunctionList;
419             this.currentRule.sourceType = 'SEVERAL';
420             if (validationEvent.toscaFunction instanceof ToscaGetFunction) {
421                 this.currentRule.sourceName = validationEvent.toscaFunction.sourceName;
422             }
423         } else {
424             this.currentRule.updateValidity(false);
425             this.currentRule.value = undefined;
426             this.currentRule.sourceType = undefined;
427             this.currentRule.sourceName = undefined;
428         }
429     }
430
431     onToscaRangeFunctionListValidityChange(validationEvent: ToscaFunctionValidationEvent, valueIndex: number): void {
432         if (validationEvent.isValid && validationEvent.toscaFunction) {
433             this.rangeToscaFunctionList.splice(valueIndex, 1, validationEvent.toscaFunction);
434             this.currentRule.value = this.rangeToscaFunctionList;
435             this.currentRule.sourceType = 'SEVERAL';
436             if (validationEvent.toscaFunction instanceof ToscaGetFunction) {
437                 this.currentRule.sourceName = validationEvent.toscaFunction.sourceName;
438             }
439         } else {
440             this.currentRule.updateValidity(false);
441             this.currentRule.value = undefined;
442             this.currentRule.sourceType = undefined;
443             this.currentRule.sourceName = undefined;
444         }
445     }
446
447     onSourceTypeChange(): void {
448         this.currentRule.value = undefined;
449         if (!this.isStaticSource() && (this.isValidValuesOperator() || this.isRangeType() || this.isInRangeOperator())) {
450             this.selectedSourceType = SourceType.TOSCA_FUNCTION_LIST;
451         }
452         this.currentRule.sourceType = this.selectedSourceType;
453         if (this.isStaticSource()) {
454             this.currentRule.sourceName = SourceType.STATIC;
455         }
456         if (this.isToscaFunctionListSource()) {
457             this.currentRule.sourceName = SourceType.TOSCA_FUNCTION_LIST;
458         }
459         this.updateSelectedProperty();
460     }
461
462     private resetSelectedProperty(): void {
463         this.currentRule.servicePropertyName = undefined;
464         this.selectedProperty = undefined;
465         this.onPropertyChange();
466     }
467
468     addToList() {
469         if (!this.validValuesToscaFunctionList) {
470             this.validValuesToscaFunctionList = new Array();
471         }
472         this.validValuesToscaFunctionList.push(ToscaFunctionHelper.convertObjectToToscaFunction(undefined));
473     }
474
475     generateRangeToscaFunctionList() {
476         if (!this.rangeToscaFunctionList) {
477             this.rangeToscaFunctionList = new Array();
478             this.rangeToscaFunctionList.push(ToscaFunctionHelper.convertObjectToToscaFunction(undefined));
479             this.rangeToscaFunctionList.push(ToscaFunctionHelper.convertObjectToToscaFunction(undefined));
480         }
481     }
482
483     trackByFn(index) {
484         return index;
485     }
486
487     removeFromList(valueIndex: number) {
488         this.validValuesToscaFunctionList.splice(valueIndex, 1);
489         this.currentRule.updateValidity(!this.doesArrayContainsEmptyValues(this.validValuesToscaFunctionList) && !(this.validValuesToscaFunctionList.length === 0));
490         if (this.doesArrayContainsEmptyValues(this.validValuesToscaFunctionList) || (this.validValuesToscaFunctionList.length === 0)) {
491             this.currentRule.value = undefined;
492             this.currentRule.sourceType = undefined;
493             this.currentRule.sourceName = undefined;
494         }
495     }
496
497     private doesArrayContainsEmptyValues(arr) {
498         for (const element of arr) {
499             if (element === undefined) return true;
500         }
501         return false;
502     }
503 }
504
505 export enum FilterType {
506     CAPABILITY,
507     PROPERTY
508 }