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} 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";
33 selector: 'service-dependencies-editor',
34 templateUrl: './service-dependencies-editor.component.html',
35 styleUrls: ['./service-dependencies-editor.component.less'],
36 providers: [ServiceServiceNg2]
38 export class ServiceDependenciesEditorComponent implements OnInit {
40 @Input() serviceRuleIndex: number;
41 @Input() serviceRules: PropertyFilterConstraintUi[];
42 @Input() compositeServiceName: string;
43 @Input() currentServiceName: string;
44 @Input() parentServiceInputs: InputBEModel[];
45 @Input() parentServiceProperties: PropertyBEModel[];
46 @Input() selectedInstanceProperties: PropertyBEModel[];
47 @Input() allowedOperators: ConstraintOperatorType[] = [
48 ConstraintOperatorType.GREATER_THAN,
49 ConstraintOperatorType.LESS_THAN,
50 ConstraintOperatorType.EQUAL,
51 ConstraintOperatorType.GREATER_OR_EQUAL,
52 ConstraintOperatorType.LESS_OR_EQUAL
54 @Input() capabilityNameAndPropertiesMap: Map<string, PropertyModel[]>;
55 @Input() filterType: FilterType;
56 @Input() filterConstraint: PropertyFilterConstraintUi;
58 currentRule: PropertyFilterConstraintUi;
60 FILTER_TYPE_CAPABILITY: FilterType = FilterType.CAPABILITY
62 operatorTypes: DropdownValue[] = [
63 {label: FilterConstraintHelper.convertToSymbol(ConstraintOperatorType.GREATER_THAN), value: ConstraintOperatorType.GREATER_THAN},
64 {label: FilterConstraintHelper.convertToSymbol(ConstraintOperatorType.LESS_THAN), value: ConstraintOperatorType.LESS_THAN},
65 {label: FilterConstraintHelper.convertToSymbol(ConstraintOperatorType.EQUAL), value: ConstraintOperatorType.EQUAL},
66 {label: FilterConstraintHelper.convertToSymbol(ConstraintOperatorType.GREATER_OR_EQUAL), value: ConstraintOperatorType.GREATER_OR_EQUAL},
67 {label: FilterConstraintHelper.convertToSymbol(ConstraintOperatorType.LESS_OR_EQUAL), value: ConstraintOperatorType.LESS_OR_EQUAL}
70 servicePropertyDropdownList: DropdownValue[];
72 selectedProperty: PropertyFEModel;
73 selectedSourceType: string;
74 componentInstanceMap: Map<string, InstanceFeDetails> = new Map<string, InstanceFeDetails>();
75 capabilityDropdownList: DropdownValue[] = [];
78 STATIC: {label: 'Static', value: SourceType.STATIC},
79 TOSCA_FUNCTION: {label: 'Tosca Function', value: SourceType.TOSCA_FUNCTION}
82 constructor(private propertiesUtils: PropertiesUtils, private compositionService: CompositionService) {}
85 if (this.compositionService.componentInstances) {
86 this.compositionService.componentInstances.forEach(value => {
87 this.componentInstanceMap.set(value.uniqueId, <InstanceFeDetails>{
92 this.initCapabilityDropdown();
93 this.initCurrentRule();
94 this.initConstraintOperatorOptions();
95 this.initSelectedSourceType();
96 this.initPropertyDropdown();
101 private initCapabilityDropdown(): void {
102 if (this.filterType == FilterType.CAPABILITY) {
103 this.capabilityDropdownList = [
104 new DropdownValue(undefined, 'Select'),
105 ...Array.from(this.capabilityNameAndPropertiesMap.keys()).map(capabilityName => new DropdownValue(capabilityName, capabilityName))
110 private initPropertyDropdown(): void {
111 let propertyList: PropertyBEModel[] = [];
112 if (this.filterType == FilterType.CAPABILITY) {
113 if (this.currentRule.capabilityName) {
114 propertyList = this.capabilityNameAndPropertiesMap.get(this.currentRule.capabilityName);
117 propertyList = this.selectedInstanceProperties;
120 if (this.filterType == FilterType.CAPABILITY) {
121 selectLabel = this.currentRule.capabilityName ? 'Select' : 'Select a Capability';
123 selectLabel = 'Select';
125 this.servicePropertyDropdownList = [new DropdownValue(undefined, selectLabel), ...propertyList.map(prop => new DropdownValue(prop.name, prop.name))];
128 private initConstraintOperatorOptions(): void {
129 if (!this.selectedProperty) {
130 this.operatorTypes = [new DropdownValue(undefined, 'Select a Property')];
134 if (PROPERTY_DATA.SIMPLE_TYPES_COMPARABLE.indexOf(this.selectedProperty.type) === -1) {
135 if (this.currentRule.constraintOperator !== ConstraintOperatorType.EQUAL) {
136 this.currentRule.constraintOperator = ConstraintOperatorType.EQUAL;
138 this.operatorTypes = [new DropdownValue(ConstraintOperatorType.EQUAL, FilterConstraintHelper.convertToSymbol(ConstraintOperatorType.EQUAL))];
140 const operatorList: DropdownValue[] = [];
141 this.allowedOperators.forEach(constraintOperatorType =>
142 operatorList.push(new DropdownValue(constraintOperatorType, FilterConstraintHelper.convertToSymbol(constraintOperatorType)))
144 this.operatorTypes = operatorList;
148 private initSelectedSourceType(): void {
149 if (!this.currentRule.sourceType || this.currentRule.sourceType === SourceType.STATIC) {
150 this.selectedSourceType = SourceType.STATIC;
152 this.selectedSourceType = SourceType.TOSCA_FUNCTION;
156 private initCurrentRule(): void {
157 if (this.filterConstraint) {
158 this.currentRule = new PropertyFilterConstraintUi(this.filterConstraint);
160 this.currentRule = new PropertyFilterConstraintUi({
161 sourceName: SourceType.STATIC,
162 sourceType: SourceType.STATIC,
163 constraintOperator: ConstraintOperatorType.EQUAL,
169 onCapabilityChange(): void {
170 this.initPropertyDropdown();
171 this.resetSelectedProperty();
174 onPropertyChange(): void {
175 this.currentRule.value = undefined;
176 this.onValueChange(false);
177 this.updateSelectedProperty();
178 this.initConstraintOperatorOptions();
181 syncRuleData(): void {
182 if (!this.currentRule.sourceName || this.currentRule.sourceType === SourceType.STATIC) {
183 this.currentRule.sourceName = SourceType.STATIC;
184 this.currentRule.sourceType = SourceType.STATIC;
186 this.initSelectedProperty();
187 this.initConstraintOperatorOptions();
190 onValueChange(isValidValue:any): void {
191 this.currentRule.updateValidity(isValidValue);
194 checkFormValidForSubmit(): boolean {
195 return this.currentRule.isValidRule();
198 initSelectedProperty(): void {
199 if (!this.currentRule.servicePropertyName) {
200 this.selectedProperty = undefined;
204 if (this.filterType === FilterType.CAPABILITY) {
205 const currentProperty = this.capabilityNameAndPropertiesMap.get(this.currentRule.capabilityName)
206 .find(property => property.name === this.currentRule.servicePropertyName);
207 newProperty = new PropertyFEModel(currentProperty);
209 newProperty = new PropertyFEModel(this.selectedInstanceProperties.find(property => property.name === this.currentRule.servicePropertyName));
211 newProperty.value = undefined;
212 newProperty.toscaFunction = undefined;
213 if (typeof this.currentRule.value === 'string') {
214 newProperty.value = this.currentRule.value;
215 this.propertiesUtils.initValueObjectRef(newProperty);
216 } else if (ToscaFunctionHelper.isValueToscaFunction(this.currentRule.value)) {
217 newProperty.toscaFunction = ToscaFunctionHelper.convertObjectToToscaFunction(this.currentRule.value);
218 newProperty.value = newProperty.toscaFunction.buildValueString();
220 newProperty.value = JSON.stringify(this.currentRule.value);
221 this.propertiesUtils.initValueObjectRef(newProperty);
224 this.selectedProperty = newProperty;
227 updateSelectedProperty(): void {
228 this.selectedProperty = undefined;
229 if (!this.currentRule.servicePropertyName) {
234 if (this.filterType === FilterType.CAPABILITY) {
235 const currentProperty = this.capabilityNameAndPropertiesMap.get(this.currentRule.capabilityName)
236 .find(property => property.name === this.currentRule.servicePropertyName);
237 newProperty = new PropertyFEModel(currentProperty);
239 newProperty = new PropertyFEModel(this.selectedInstanceProperties.find(property => property.name === this.currentRule.servicePropertyName));
241 newProperty.value = undefined;
242 newProperty.toscaFunction = undefined;
244 this.propertiesUtils.initValueObjectRef(newProperty);
245 this.selectedProperty = newProperty;
248 isStaticSource(): boolean {
249 return this.selectedSourceType === SourceType.STATIC
252 isToscaFunctionSource(): boolean {
253 return this.selectedSourceType === SourceType.TOSCA_FUNCTION
256 isComplexListMapType(): boolean {
257 return this.selectedProperty && this.selectedProperty.derivedDataType > 0;
260 updateComplexListMapTypeRuleValue(): void {
261 this.currentRule.value = PropertyFEModel.cleanValueObj(this.selectedProperty.valueObj);
262 this.onValueChange(this.selectedProperty.valueObjIsValid);
265 onToscaFunctionValidityChange(validationEvent: ToscaFunctionValidationEvent): void {
266 if (validationEvent.isValid && validationEvent.toscaFunction) {
267 this.currentRule.value = validationEvent.toscaFunction;
268 this.currentRule.sourceType = validationEvent.toscaFunction.type
269 if (validationEvent.toscaFunction instanceof ToscaGetFunction) {
270 this.currentRule.sourceName = validationEvent.toscaFunction.sourceName;
273 this.currentRule.updateValidity(false);
274 this.currentRule.value = undefined;
275 this.currentRule.sourceType = undefined;
276 this.currentRule.sourceName = undefined;
280 onSourceTypeChange(): void {
281 this.currentRule.value = undefined;
282 this.currentRule.sourceType = this.selectedSourceType;
283 if (this.isStaticSource()) {
284 this.currentRule.sourceName = SourceType.STATIC;
286 this.updateSelectedProperty();
289 private resetSelectedProperty(): void {
290 this.currentRule.servicePropertyName = undefined;
291 this.selectedProperty = undefined;
292 this.onPropertyChange();
297 export enum FilterType {