2 * ============LICENSE_START=======================================================
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
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
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";
31 export class UIDropDownSourceTypesElement extends DropdownValue {
33 assignedLabel: string;
36 constructor(input?: any) {
37 super(input ? input.value || '' : "", input ? input.label || '' : "");
39 this.options = input.options;
40 this.assignedLabel = input.assignedLabel;
41 this.type = input.type;
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]
52 export class CapabilitiesFilterPropertiesEditorComponent {
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[]>,
65 currentServiceName: string;
66 selectedServiceProperties: PropertyBEModel[];
67 operatorTypes: DropdownValue[];
68 sourceTypes: UIDropDownSourceTypesElement[] = [];
69 currentRule: CapabilitiesConstraintObjectUI;
71 listOfValuesToAssign: DropdownValue[];
72 listOfSourceOptions: PropertyBEModel[];
73 assignedValueLabel: string;
74 serviceRulesList: CapabilitiesConstraintObjectUI[];
76 capabilitiesNames: string[];
77 selectedPropertiesByCapabilityName: Array<PropertyModel>;
78 selectedCapabilityName: string;
79 capabilityProperties: DropdownValue[];
81 selectedCapabilitiesPropertyObject: PropertyBEModel;
84 STATIC: {label: 'Static', value: 'static'},
85 SERVICE_PROPERTY: {label: 'Service Property', value: 'property'},
86 CAPABILITY_NAME: {label: 'Name', value: 'name'}
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
101 this.currentServiceName = this.input.currentServiceName;
102 this.operatorTypes = this.input.operatorTypes;
104 this.initSourceTypes();
106 this.updateSourceTypesRelatedValues();
107 this.onCapabilityNameChanged(this.currentRule.capabilityName)
111 this.sourceTypes.push({
112 label: this.SOURCE_TYPES.STATIC.label,
113 value: this.SOURCE_TYPES.STATIC.value,
115 assignedLabel: this.SOURCE_TYPES.STATIC.label,
116 type: this.SOURCE_TYPES.STATIC.value
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
125 _.forEach(this.input.selectedInstanceSiblings, (sib) =>
126 this.sourceTypes.push({
129 options: sib.properties || [],
130 assignedLabel: this.SOURCE_TYPES.SERVICE_PROPERTY.label,
131 type: this.SOURCE_TYPES.SERVICE_PROPERTY.value
137 if (!this.currentRule.sourceName && this.currentRule.sourceType === this.SOURCE_TYPES.STATIC.value) {
138 this.currentRule.sourceName = this.SOURCE_TYPES.STATIC.value;
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);
145 this.updateOperatorTypesList();
146 this.updateSourceTypesRelatedValues();
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;
154 this.operatorTypes = this.input.operatorTypes;
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
163 if (selectedSourceType) {
164 this.listOfSourceOptions = selectedSourceType.options || [];
165 this.assignedValueLabel = selectedSourceType.assignedLabel || this.SOURCE_TYPES.STATIC.label;
166 this.filterOptionsByType();
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();
179 onServicePropertyChanged() {
180 this.updateOperatorTypesList();
181 this.filterOptionsByType();
182 this.currentRule.value = '';
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 = '';
193 filterOptionsByType() {
194 if (!this.selectedCapabilitiesPropertyObject) {
195 this.listOfValuesToAssign = [];
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));
206 onValueChange(isValidValue) {
207 this.currentRule.updateValidity(isValidValue);
210 checkFormValidForSubmit() {
211 if (!this.serviceRulesList) {
212 const isStatic = this.currentRule.sourceName === this.SOURCE_TYPES.STATIC.value;
213 return this.currentRule.isValidRule(isStatic);
215 return this.serviceRulesList.every((rule) => rule.isValidRule(rule.sourceName === this.SOURCE_TYPES.STATIC.value));