3781a5163d35ae5ee924876e79c7a09eff67dc54
[sdc.git] / catalog-ui / src / app / ng2 / pages / composition / capabilities-filter-properties-editor / capabilities-filter-properties-editor.component.ts
1 /*
2 * ============LICENSE_START=======================================================
3 * SDC
4 * ================================================================================
5 *  Copyright (C) 2020 Nordix Foundation. All rights reserved.
6 *  ================================================================================
7 *  Licensed under the Apache License, Version 2.0 (the "License");
8 *  you may not use this file except in compliance with the License.
9 *  You may obtain a copy of the License at
10 *
11 *        http://www.apache.org/licenses/LICENSE-2.0
12 *  Unless required by applicable law or agreed to in writing, software
13 *  distributed under the License is distributed on an "AS IS" BASIS,
14 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 *  See the License for the specific language governing permissions and
16 *  limitations under the License.
17 *
18 *  SPDX-License-Identifier: Apache-2.0
19 *  ============LICENSE_END=========================================================
20 */
21
22 import {Component} from '@angular/core';
23 import {InputBEModel, PropertyBEModel, PropertyModel} from 'app/models';
24 import {OPERATOR_TYPES} from 'app/ng2/components/logic/service-dependencies/service-dependencies.component';
25 import {DropdownValue} from 'app/ng2/components/ui/form-components/dropdown/ui-element-dropdown.component';
26 import {ServiceServiceNg2} from 'app/ng2/services/component-services/service.service';
27 import {PROPERTY_DATA} from 'app/utils';
28 import {ServiceInstanceObject} from '../../../../models/service-instance-properties-and-interfaces';
29 import {CapabilitiesConstraintObjectUI} from "../../../components/logic/capabilities-constraint/capabilities-constraint.component";
30
31 export class UIDropDownSourceTypesElement extends DropdownValue {
32   options: any[];
33   assignedLabel: string;
34   type: string;
35
36   constructor(input?: any) {
37     super(input ? input.value || '' : "", input ? input.label || '' : "");
38     if (input) {
39       this.options = input.options;
40       this.assignedLabel = input.assignedLabel;
41       this.type = input.type;
42     }
43   }
44 }
45
46 @Component({
47   selector: 'app-capabilities-filter-properties-editor',
48   templateUrl: './capabilities-filter-properties-editor.component.html',
49   styleUrls: ['./capabilities-filter-properties-editor.component.less'],
50   providers: [ServiceServiceNg2]
51 })
52 export class CapabilitiesFilterPropertiesEditorComponent {
53
54   input: {
55     serviceRuleIndex: number,
56     serviceRules: CapabilitiesConstraintObjectUI[],
57     compositeServiceName: string,
58     currentServiceName: string,
59     parentServiceInputs: InputBEModel[],
60     selectedInstanceProperties: PropertyBEModel[],
61     operatorTypes: DropdownValue[],
62     selectedInstanceSiblings: ServiceInstanceObject[],
63     componentInstanceCapabilitiesMap: Map<string, PropertyModel[]>,
64   };
65   currentServiceName: string;
66   selectedServiceProperties: PropertyBEModel[];
67   operatorTypes: DropdownValue[];
68   sourceTypes: UIDropDownSourceTypesElement[] = [];
69   currentRule: CapabilitiesConstraintObjectUI;
70   currentIndex: number;
71   listOfValuesToAssign: DropdownValue[];
72   listOfSourceOptions: PropertyBEModel[];
73   assignedValueLabel: string;
74   serviceRulesList: CapabilitiesConstraintObjectUI[];
75
76   capabilitiesNames: string[];
77   selectedPropertiesByCapabilityName: Array<PropertyModel>;
78   selectedCapabilityName: string;
79   capabilityProperties: DropdownValue[];
80
81   selectedCapabilitiesPropertyObject: PropertyBEModel;
82
83   SOURCE_TYPES = {
84     STATIC: {label: 'Static', value: 'static'},
85     SERVICE_PROPERTY: {label: 'Service Property', value: 'property'},
86     CAPABILITY_NAME: {label: 'Name', value: 'name'}
87   };
88
89   ngOnInit() {
90     this.capabilitiesNames = Array.from(this.input.componentInstanceCapabilitiesMap.keys());
91     this.currentIndex = this.input.serviceRuleIndex;
92     this.serviceRulesList = this.input.serviceRules;
93     this.currentRule = this.serviceRulesList && this.input.serviceRuleIndex >= 0 ?
94         this.serviceRulesList[this.input.serviceRuleIndex] :
95         new CapabilitiesConstraintObjectUI({
96           capabilityName: this.SOURCE_TYPES.CAPABILITY_NAME.value,
97           sourceName: this.SOURCE_TYPES.STATIC.value,
98           sourceType: this.SOURCE_TYPES.STATIC.value, value: '',
99           constraintOperator: OPERATOR_TYPES.EQUAL
100         });
101     this.currentServiceName = this.input.currentServiceName;
102     this.operatorTypes = this.input.operatorTypes;
103
104     this.initSourceTypes();
105     this.syncRuleData();
106     this.updateSourceTypesRelatedValues();
107     this.onCapabilityNameChanged(this.currentRule.capabilityName)
108   }
109
110   initSourceTypes() {
111     this.sourceTypes.push({
112       label: this.SOURCE_TYPES.STATIC.label,
113       value: this.SOURCE_TYPES.STATIC.value,
114       options: [],
115       assignedLabel: this.SOURCE_TYPES.STATIC.label,
116       type: this.SOURCE_TYPES.STATIC.value
117     });
118     this.sourceTypes.push({
119       label: this.input.compositeServiceName,
120       value: this.input.compositeServiceName,
121       assignedLabel: this.SOURCE_TYPES.SERVICE_PROPERTY.label,
122       type: this.SOURCE_TYPES.SERVICE_PROPERTY.value,
123       options: this.input.parentServiceInputs
124     });
125     _.forEach(this.input.selectedInstanceSiblings, (sib) =>
126         this.sourceTypes.push({
127           label: sib.name,
128           value: sib.name,
129           options: sib.properties || [],
130           assignedLabel: this.SOURCE_TYPES.SERVICE_PROPERTY.label,
131           type: this.SOURCE_TYPES.SERVICE_PROPERTY.value
132         })
133     );
134   }
135
136   syncRuleData() {
137     if (!this.currentRule.sourceName && this.currentRule.sourceType === this.SOURCE_TYPES.STATIC.value) {
138       this.currentRule.sourceName = this.SOURCE_TYPES.STATIC.value;
139     }
140     if (!this.input.componentInstanceCapabilitiesMap) {
141       this.selectedCapabilitiesPropertyObject = Array.from(this.input.componentInstanceCapabilitiesMap
142       .get(this.currentRule.capabilityName))
143       .find(property => property.name == this.currentRule.servicePropertyName);
144     }
145     this.updateOperatorTypesList();
146     this.updateSourceTypesRelatedValues();
147   }
148
149   updateOperatorTypesList() {
150     if (this.selectedCapabilitiesPropertyObject && PROPERTY_DATA.SIMPLE_TYPES_COMPARABLE.indexOf(this.selectedCapabilitiesPropertyObject.type) === -1) {
151       this.operatorTypes = [{label: '=', value: OPERATOR_TYPES.EQUAL}];
152       this.currentRule.constraintOperator = OPERATOR_TYPES.EQUAL;
153     } else {
154       this.operatorTypes = this.input.operatorTypes;
155     }
156   }
157
158   updateSourceTypesRelatedValues() {
159     if (this.currentRule.sourceName) {
160       const selectedSourceType: UIDropDownSourceTypesElement = this.sourceTypes.find(
161           (t) => t.value === this.currentRule.sourceName && t.type === this.currentRule.sourceType
162       );
163       if (selectedSourceType) {
164         this.listOfSourceOptions = selectedSourceType.options || [];
165         this.assignedValueLabel = selectedSourceType.assignedLabel || this.SOURCE_TYPES.STATIC.label;
166         this.filterOptionsByType();
167       }
168     }
169   }
170
171   onCapabilityNameChanged = (value: any): void => {
172     this.selectedPropertiesByCapabilityName = this.input.componentInstanceCapabilitiesMap.get(value);
173     this.capabilityProperties = _.map(this.selectedPropertiesByCapabilityName, (prop) => new DropdownValue(prop.name, prop.name));
174     this.selectedCapabilityName = value;
175     this.updateOperatorTypesList();
176     this.filterOptionsByType();
177   }
178
179   onServicePropertyChanged() {
180     this.updateOperatorTypesList();
181     this.filterOptionsByType();
182     this.currentRule.value = '';
183   }
184
185   onSelectSourceType() {
186     this.currentRule.sourceType = this.currentRule.sourceName === this.SOURCE_TYPES.STATIC.value ?
187         this.SOURCE_TYPES.STATIC.value :
188         this.SOURCE_TYPES.SERVICE_PROPERTY.value;
189     this.updateSourceTypesRelatedValues();
190     this.currentRule.value = '';
191   }
192
193   filterOptionsByType() {
194     if (!this.selectedCapabilitiesPropertyObject) {
195       this.listOfValuesToAssign = [];
196       return;
197     }
198     this.listOfValuesToAssign = this.listOfSourceOptions.reduce((result, op: PropertyModel) => {
199       if (op.type === this.selectedCapabilitiesPropertyObject.type && (!op.schemaType || op.schemaType === this.selectedCapabilitiesPropertyObject.schemaType)) {
200         result.push(new DropdownValue(op.name, op.name));
201       }
202       return result;
203     }, []);
204   }
205
206   onValueChange(isValidValue) {
207     this.currentRule.updateValidity(isValidValue);
208   }
209
210   checkFormValidForSubmit() {
211     if (!this.serviceRulesList) {
212       const isStatic = this.currentRule.sourceName === this.SOURCE_TYPES.STATIC.value;
213       return this.currentRule.isValidRule(isStatic);
214     }
215     return this.serviceRulesList.every((rule) => rule.isValidRule(rule.sourceName === this.SOURCE_TYPES.STATIC.value));
216   }
217
218 }