2cd4f4d1c72f7a77a659d73247ea7896c6364cb9
[sdc.git] /
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   constructor(input?: any) {
36     if (input) {
37       const value = input.value || '';
38       const label = input.label || '';
39       super(value, label);
40       this.options = input.options;
41       this.assignedLabel = input.assignedLabel;
42       this.type = input.type;
43     }
44   }
45 }
46
47 @Component({
48   selector: 'app-capabilities-filter-properties-editor',
49   templateUrl: './capabilities-filter-properties-editor.component.html',
50   styleUrls: ['./capabilities-filter-properties-editor.component.less'],
51   providers: [ServiceServiceNg2]
52 })
53 export class CapabilitiesFilterPropertiesEditorComponent {
54
55   constructor() {
56   }
57
58   input: {
59     serviceRuleIndex: number,
60     serviceRules: CapabilitiesConstraintObjectUI[],
61     compositeServiceName: string,
62     currentServiceName: string,
63     parentServiceInputs: InputBEModel[],
64     selectedInstanceProperties: PropertyBEModel[],
65     operatorTypes: DropdownValue[],
66     selectedInstanceSiblings: ServiceInstanceObject[],
67     componentInstanceCapabilitiesMap: Map<string, PropertyModel[]>,
68   };
69   currentServiceName: string;
70   selectedServiceProperties: PropertyBEModel[];
71   operatorTypes: DropdownValue[];
72   sourceTypes: UIDropDownSourceTypesElement[] = [];
73   currentRule: CapabilitiesConstraintObjectUI;
74   currentIndex: number;
75   listOfValuesToAssign: DropdownValue[];
76   listOfSourceOptions: PropertyBEModel[];
77   assignedValueLabel: string;
78   serviceRulesList: CapabilitiesConstraintObjectUI[];
79
80   capabilitiesNames: string[];
81   selectedPropertiesByCapabilityName: Array<PropertyModel>;
82   selectedCapabilityName: string;
83   capabilityProperties: DropdownValue[];
84
85   selectedCapabilitiesPropertyObject: PropertyBEModel;
86
87   SOURCE_TYPES = {
88     STATIC: {label: 'Static', value: 'static'},
89     SERVICE_PROPERTY: {label: 'Service Property', value: 'property'},
90     CAPABILITY_NAME: {label: 'Name', value: 'name'}
91   };
92
93   ngOnInit() {
94     this.capabilitiesNames = Array.from(this.input.componentInstanceCapabilitiesMap.keys());
95     this.currentIndex = this.input.serviceRuleIndex;
96     this.serviceRulesList = this.input.serviceRules;
97     this.currentRule = this.serviceRulesList && this.input.serviceRuleIndex >= 0 ?
98         this.serviceRulesList[this.input.serviceRuleIndex] :
99         new CapabilitiesConstraintObjectUI({
100           capabilityName: this.SOURCE_TYPES.CAPABILITY_NAME.value,
101           sourceName: this.SOURCE_TYPES.STATIC.value,
102           sourceType: this.SOURCE_TYPES.STATIC.value, value: '',
103           constraintOperator: OPERATOR_TYPES.EQUAL});
104     this.currentServiceName = this.input.currentServiceName;
105     this.operatorTypes = this.input.operatorTypes;
106
107     this.initSourceTypes();
108     this.syncRuleData();
109     this.updateSourceTypesRelatedValues();
110     this.onCapabilityNameChanged(this.currentRule.capabilityName)
111   }
112
113   initSourceTypes() {
114     this.sourceTypes.push({label: this.SOURCE_TYPES.STATIC.label, value: this.SOURCE_TYPES.STATIC.value,
115       options: [], assignedLabel: this.SOURCE_TYPES.STATIC.label, type: this.SOURCE_TYPES.STATIC.value});
116     this.sourceTypes.push({
117       label: this.input.compositeServiceName,
118       value: this.input.compositeServiceName,
119       assignedLabel: this.SOURCE_TYPES.SERVICE_PROPERTY.label,
120       type: this.SOURCE_TYPES.SERVICE_PROPERTY.value,
121       options: this.input.parentServiceInputs
122     });
123     _.forEach(this.input.selectedInstanceSiblings, (sib) =>
124         this.sourceTypes.push({
125           label: sib.name,
126           value: sib.name,
127           options: sib.properties || [],
128           assignedLabel: this.SOURCE_TYPES.SERVICE_PROPERTY.label,
129           type: this.SOURCE_TYPES.SERVICE_PROPERTY.value
130         })
131     );
132   }
133
134   syncRuleData() {
135     if (!this.currentRule.sourceName && this.currentRule.sourceType === this.SOURCE_TYPES.STATIC.value) {
136       this.currentRule.sourceName = this.SOURCE_TYPES.STATIC.value;
137     }
138     if (!this.input.componentInstanceCapabilitiesMap){
139       this.selectedCapabilitiesPropertyObject = Array.from(this.input.componentInstanceCapabilitiesMap
140       .get(this.currentRule.capabilityName))
141       .find(property => property.name == this.currentRule.servicePropertyName);
142     }
143     this.updateOperatorTypesList();
144     this.updateSourceTypesRelatedValues();
145   }
146
147   updateOperatorTypesList() {
148     if (this.selectedCapabilitiesPropertyObject && PROPERTY_DATA.SIMPLE_TYPES_COMPARABLE.indexOf(this.selectedCapabilitiesPropertyObject.type) === -1) {
149       this.operatorTypes = [{label: '=', value: OPERATOR_TYPES.EQUAL}];
150       this.currentRule.constraintOperator = OPERATOR_TYPES.EQUAL;
151     } else {
152       this.operatorTypes = this.input.operatorTypes;
153     }
154   }
155
156   updateSourceTypesRelatedValues() {
157     if (this.currentRule.sourceName) {
158       const selectedSourceType: UIDropDownSourceTypesElement = this.sourceTypes.find(
159           (t) => t.value === this.currentRule.sourceName && t.type === this.currentRule.sourceType
160       );
161       if(selectedSourceType) {
162         this.listOfSourceOptions = selectedSourceType.options || [];
163         this.assignedValueLabel = selectedSourceType.assignedLabel || this.SOURCE_TYPES.STATIC.label;
164         this.filterOptionsByType();
165       }
166     }
167   }
168
169   onCapabilityNameChanged= (value: any): void => {
170     this.selectedPropertiesByCapabilityName = this.input.componentInstanceCapabilitiesMap.get(value);
171     this.capabilityProperties = _.map(this.selectedPropertiesByCapabilityName, (prop) => new DropdownValue(prop.name, prop.name));
172     this.selectedCapabilityName = value;
173     this.updateOperatorTypesList();
174     this.filterOptionsByType();
175   }
176
177   onServicePropertyChanged() {
178     this.updateOperatorTypesList();
179     this.filterOptionsByType();
180     this.currentRule.value = '';
181   }
182
183   onSelectSourceType() {
184     this.currentRule.sourceType = this.currentRule.sourceName === this.SOURCE_TYPES.STATIC.value ?
185         this.SOURCE_TYPES.STATIC.value :
186         this.SOURCE_TYPES.SERVICE_PROPERTY.value;
187     this.updateSourceTypesRelatedValues();
188     this.currentRule.value = '';
189   }
190
191   filterOptionsByType() {
192     if (!this.selectedCapabilitiesPropertyObject) {
193       this.listOfValuesToAssign = [];
194       return;
195     }
196     this.listOfValuesToAssign =  this.listOfSourceOptions.reduce((result, op: PropertyModel) => {
197       if (op.type === this.selectedCapabilitiesPropertyObject.type && (!op.schemaType || op.schemaType === this.selectedCapabilitiesPropertyObject.schemaType)) {
198         result.push(new DropdownValue(op.name, op.name));
199       }
200       return result;
201     }, []);
202   }
203
204   onValueChange(isValidValue) {
205     this.currentRule.updateValidity(isValidValue);
206   }
207
208   checkFormValidForSubmit() {
209     if (!this.serviceRulesList) {
210       const isStatic = this.currentRule.sourceName === this.SOURCE_TYPES.STATIC.value;
211       return this.currentRule.isValidRule(isStatic);
212     }
213     return this.serviceRulesList.every((rule) => rule.isValidRule(rule.sourceName === this.SOURCE_TYPES.STATIC.value));
214   }
215
216 }