2 * Copyright © 2016-2018 European Support Limited
3 * Modification Copyright (C) 2022 Nordix Foundation.
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
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.
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";
37 selector: 'service-dependencies-editor',
38 templateUrl: './service-dependencies-editor.component.html',
39 styleUrls: ['./service-dependencies-editor.component.less'],
40 providers: [ServiceServiceNg2]
42 export class ServiceDependenciesEditorComponent implements OnInit {
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
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,
73 @Input() capabilityNameAndPropertiesMap: Map<string, PropertyModel[]>;
74 @Input() filterType: FilterType;
75 @Input() filterConstraint: PropertyFilterConstraintUi;
76 @Input() customToscaFunctions: Array<CustomToscaFunction>;
78 currentRule: PropertyFilterConstraintUi;
80 FILTER_TYPE_CAPABILITY: FilterType = FilterType.CAPABILITY
82 listAllowedOperators: ConstraintOperatorType[] = [
83 ConstraintOperatorType.EQUAL,
84 ConstraintOperatorType.LENGTH,
85 ConstraintOperatorType.MIN_LENGTH,
86 ConstraintOperatorType.MAX_LENGTH
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}
96 lengthArray: string[] = [ConstraintOperatorType.LENGTH,
97 ConstraintOperatorType.MIN_LENGTH,
98 ConstraintOperatorType.MAX_LENGTH];
100 servicePropertyDropdownList: DropdownValue[];
102 selectedProperty: PropertyFEModel;
103 selectedSourceType: string;
104 componentInstanceMap: Map<string, InstanceFeDetails> = new Map<string, InstanceFeDetails>();
106 capabilityDropdownList: DropdownValue[] = [];
107 validValuesToscaFunctionList: ToscaFunction[];
108 rangeToscaFunctionList: ToscaFunction[];
109 overridingType = PROPERTY_TYPES.INTEGER;
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}
117 constructor(private propertiesUtils: PropertiesUtils, private compositionService: CompositionService, private topologyTemplateService: TopologyTemplateService) {
121 if (this.compositionService.componentInstances) {
122 this.compositionService.componentInstances.forEach(value => {
123 this.componentInstanceMap.set(value.uniqueId, <InstanceFeDetails>{
128 this.initCustomToscaFunctions();
129 this.initCapabilityDropdown();
130 this.initCurrentRule();
131 this.initConstraintOperatorOptions();
132 this.initSelectedSourceType();
133 this.initPropertyDropdown();
135 this.generateRangeToscaFunctionList();
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));
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))
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);
165 propertyList = this.selectedInstanceProperties;
168 if (this.filterType == FilterType.CAPABILITY) {
169 selectLabel = this.currentRule.capabilityName ? 'Select' : 'Select a Capability';
171 selectLabel = 'Select';
173 this.servicePropertyDropdownList = [new DropdownValue(undefined, selectLabel), ...propertyList.map(prop => new DropdownValue(prop.name, prop.name)).sort((prop1, prop2) => prop1.value.localeCompare(prop2.value))];
176 private initConstraintOperatorOptions(): void {
177 if (!this.selectedProperty) {
178 this.operatorTypes = [this.setOperatorDropdownValue(undefined)];
181 const operatorList: DropdownValue[] = [];
183 case this.selectedProperty.type === PROPERTY_TYPES.RANGE:
184 if (this.currentRule.constraintOperator !== ConstraintOperatorType.IN_RANGE) {
185 this.currentRule.constraintOperator = ConstraintOperatorType.IN_RANGE;
187 this.operatorTypes = [this.setOperatorDropdownValue(ConstraintOperatorType.IN_RANGE)];
189 case this.selectedProperty.type === PROPERTY_TYPES.STRING:
190 this.allowedOperators.forEach(constraintOperatorType =>
191 operatorList.push(this.setOperatorDropdownValue(constraintOperatorType))
193 this.operatorTypes = operatorList;
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))
201 this.operatorTypes = operatorList;
203 case this.selectedProperty.type === PROPERTY_TYPES.LIST:
204 this.listAllowedOperators.forEach(constraintOperatorType =>
205 operatorList.push(this.setOperatorDropdownValue(constraintOperatorType))
207 this.operatorTypes = operatorList;
210 if (this.currentRule.constraintOperator !== ConstraintOperatorType.EQUAL) {
211 this.currentRule.constraintOperator = ConstraintOperatorType.EQUAL;
213 this.operatorTypes = [this.setOperatorDropdownValue(ConstraintOperatorType.EQUAL)];
218 private setOperatorDropdownValue(constraintOperatorType: ConstraintOperatorType) {
219 if (constraintOperatorType === undefined) {
220 return new DropdownValue(undefined, 'Select a Property');
222 return new DropdownValue(constraintOperatorType, FilterConstraintHelper.convertToSymbol(constraintOperatorType));
225 private initSelectedSourceType(): void {
226 if (!this.currentRule.sourceType || this.currentRule.sourceType === SourceType.STATIC) {
227 this.selectedSourceType = SourceType.STATIC;
229 if (!this.isValidValuesOperator() && !this.isRangeType() && !this.isInRangeOperator()) {
230 this.selectedSourceType = SourceType.TOSCA_FUNCTION;
232 this.selectedSourceType = SourceType.TOSCA_FUNCTION_LIST;
237 private initCurrentRule(): void {
238 if (this.filterConstraint) {
239 this.currentRule = new PropertyFilterConstraintUi(this.filterConstraint);
241 this.currentRule = new PropertyFilterConstraintUi({
242 sourceName: SourceType.STATIC,
243 sourceType: SourceType.STATIC,
244 constraintOperator: ConstraintOperatorType.EQUAL,
246 originalType: undefined
249 let propertyList: PropertyBEModel[] = [];
250 if (this.filterType == FilterType.CAPABILITY) {
251 if (this.currentRule.capabilityName) {
252 propertyList = this.capabilityNameAndPropertiesMap.get(this.currentRule.capabilityName);
255 propertyList = this.selectedInstanceProperties;
257 if (this.filterConstraint) {
258 this.filterConstraint.originalType = propertyList.find(prop => prop.name == this.filterConstraint.servicePropertyName).type;
262 onCapabilityChange(): void {
263 this.initPropertyDropdown();
264 this.resetSelectedProperty();
267 onPropertyChange(): void {
268 this.currentRule.value = undefined;
269 this.onValueChange(false);
270 this.updateSelectedProperty();
271 this.initConstraintOperatorOptions();
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;
279 this.initSelectedProperty();
280 this.initConstraintOperatorOptions();
283 onValueChange(isValidValue: any): void {
284 this.currentRule.updateValidity(isValidValue);
287 checkFormValidForSubmit(): boolean {
288 return this.currentRule.isValidRule();
291 initSelectedProperty(): void {
292 if (!this.currentRule.servicePropertyName) {
293 this.selectedProperty = undefined;
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);
302 newProperty = new PropertyFEModel(this.selectedInstanceProperties.find(property => property.name === this.currentRule.servicePropertyName));
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;
318 newProperty.value = JSON.stringify(this.currentRule.value);
319 this.propertiesUtils.initValueObjectRef(newProperty);
322 this.selectedProperty = newProperty;
323 this.currentRule.originalType = this.selectedProperty.type;
326 updateSelectedProperty(): void {
327 this.selectedProperty = undefined;
328 if (!this.currentRule.servicePropertyName) {
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);
338 newProperty = new PropertyFEModel(this.selectedInstanceProperties.find(property => property.name === this.currentRule.servicePropertyName));
340 newProperty.value = undefined;
341 newProperty.toscaFunction = undefined;
343 this.propertiesUtils.initValueObjectRef(newProperty);
344 this.selectedProperty = newProperty;
345 this.currentRule.originalType = this.selectedProperty.type;
348 isStaticSource(): boolean {
349 return this.selectedSourceType === SourceType.STATIC
352 isToscaFunctionSource(): boolean {
353 return this.selectedSourceType === SourceType.TOSCA_FUNCTION
356 isToscaFunctionListSource(): boolean {
357 return this.selectedSourceType === SourceType.TOSCA_FUNCTION_LIST
360 isComplexListMapType(): boolean {
361 return this.selectedProperty && this.selectedProperty.derivedDataType > 0;
364 isRangeType(): boolean {
365 return this.selectedProperty && this.selectedProperty.derivedDataType == 4;
368 isLengthOperator(): boolean {
369 return this.lengthArray.indexOf(this.currentRule.constraintOperator) > -1;
372 isInRangeOperator(): boolean {
373 return this.currentRule.constraintOperator && this.currentRule.constraintOperator === ConstraintOperatorType.IN_RANGE;
376 isValidValuesOperator(): boolean {
377 return this.currentRule.constraintOperator && this.currentRule.constraintOperator === ConstraintOperatorType.VALID_VALUES;
380 updateComplexListMapTypeRuleValue(): void {
381 this.currentRule.value = PropertyFEModel.cleanValueObj(this.selectedProperty.valueObj);
382 this.onValueChange(this.selectedProperty.valueObjIsValid);
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;
393 else if (validationEvent.toscaFunction instanceof ToscaCustomFunction) {
394 this.currentRule.sourceName = SourceType.TOSCA_FUNCTION_LIST;
395 this.currentRule.sourceType = SourceType.TOSCA_FUNCTION_LIST;
398 if (this.isLengthOperator()) {
399 this.overridingType = PROPERTY_TYPES.INTEGER;
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;
408 this.currentRule.updateValidity(false);
409 this.currentRule.value = undefined;
410 this.currentRule.sourceType = undefined;
411 this.currentRule.sourceName = undefined;
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;
424 this.currentRule.updateValidity(false);
425 this.currentRule.value = undefined;
426 this.currentRule.sourceType = undefined;
427 this.currentRule.sourceName = undefined;
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;
440 this.currentRule.updateValidity(false);
441 this.currentRule.value = undefined;
442 this.currentRule.sourceType = undefined;
443 this.currentRule.sourceName = undefined;
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;
452 this.currentRule.sourceType = this.selectedSourceType;
453 if (this.isStaticSource()) {
454 this.currentRule.sourceName = SourceType.STATIC;
456 if (this.isToscaFunctionListSource()) {
457 this.currentRule.sourceName = SourceType.TOSCA_FUNCTION_LIST;
459 this.updateSelectedProperty();
462 private resetSelectedProperty(): void {
463 this.currentRule.servicePropertyName = undefined;
464 this.selectedProperty = undefined;
465 this.onPropertyChange();
469 if (!this.validValuesToscaFunctionList) {
470 this.validValuesToscaFunctionList = new Array();
472 this.validValuesToscaFunctionList.push(ToscaFunctionHelper.convertObjectToToscaFunction(undefined));
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));
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;
497 private doesArrayContainsEmptyValues(arr) {
498 for (const element of arr) {
499 if (element === undefined) return true;
505 export enum FilterType {