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;
35 constructor(input?: any) {
37 const value = input.value || '';
38 const label = input.label || '';
40 this.options = input.options;
41 this.assignedLabel = input.assignedLabel;
42 this.type = input.type;
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]
53 export class CapabilitiesFilterPropertiesEditorComponent {
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[]>,
69 currentServiceName: string;
70 selectedServiceProperties: PropertyBEModel[];
71 operatorTypes: DropdownValue[];
72 sourceTypes: UIDropDownSourceTypesElement[] = [];
73 currentRule: CapabilitiesConstraintObjectUI;
75 listOfValuesToAssign: DropdownValue[];
76 listOfSourceOptions: PropertyBEModel[];
77 assignedValueLabel: string;
78 serviceRulesList: CapabilitiesConstraintObjectUI[];
80 capabilitiesNames: string[];
81 selectedPropertiesByCapabilityName: Array<PropertyModel>;
82 selectedCapabilityName: string;
83 capabilityProperties: DropdownValue[];
85 selectedCapabilitiesPropertyObject: PropertyBEModel;
88 STATIC: {label: 'Static', value: 'static'},
89 SERVICE_PROPERTY: {label: 'Service Property', value: 'property'},
90 CAPABILITY_NAME: {label: 'Name', value: 'name'}
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;
107 this.initSourceTypes();
109 this.updateSourceTypesRelatedValues();
110 this.onCapabilityNameChanged(this.currentRule.capabilityName)
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
123 _.forEach(this.input.selectedInstanceSiblings, (sib) =>
124 this.sourceTypes.push({
127 options: sib.properties || [],
128 assignedLabel: this.SOURCE_TYPES.SERVICE_PROPERTY.label,
129 type: this.SOURCE_TYPES.SERVICE_PROPERTY.value
135 if (!this.currentRule.sourceName && this.currentRule.sourceType === this.SOURCE_TYPES.STATIC.value) {
136 this.currentRule.sourceName = this.SOURCE_TYPES.STATIC.value;
138 this.selectedCapabilitiesPropertyObject = Array.from(this.input.componentInstanceCapabilitiesMap
139 .get(this.currentRule.capabilityName))
140 .find(property => property.name == this.currentRule.servicePropertyName);
141 this.updateOperatorTypesList();
142 this.updateSourceTypesRelatedValues();
145 updateOperatorTypesList() {
146 if (this.selectedCapabilitiesPropertyObject && PROPERTY_DATA.SIMPLE_TYPES_COMPARABLE.indexOf(this.selectedCapabilitiesPropertyObject.type) === -1) {
147 this.operatorTypes = [{label: '=', value: OPERATOR_TYPES.EQUAL}];
148 this.currentRule.constraintOperator = OPERATOR_TYPES.EQUAL;
150 this.operatorTypes = this.input.operatorTypes;
154 updateSourceTypesRelatedValues() {
155 if (this.currentRule.sourceName) {
156 const selectedSourceType: UIDropDownSourceTypesElement = this.sourceTypes.find(
157 (t) => t.value === this.currentRule.sourceName && t.type === this.currentRule.sourceType
159 if(selectedSourceType) {
160 this.listOfSourceOptions = selectedSourceType.options || [];
161 this.assignedValueLabel = selectedSourceType.assignedLabel || this.SOURCE_TYPES.STATIC.label;
162 this.filterOptionsByType();
167 onCapabilityNameChanged= (value: any): void => {
168 this.selectedPropertiesByCapabilityName = this.input.componentInstanceCapabilitiesMap.get(value);
169 this.capabilityProperties = _.map(this.selectedPropertiesByCapabilityName, (prop) => new DropdownValue(prop.name, prop.name));
170 this.selectedCapabilityName = value;
171 this.updateOperatorTypesList();
172 this.filterOptionsByType();
175 onServicePropertyChanged() {
176 this.updateOperatorTypesList();
177 this.filterOptionsByType();
178 this.currentRule.value = '';
181 onSelectSourceType() {
182 this.currentRule.sourceType = this.currentRule.sourceName === this.SOURCE_TYPES.STATIC.value ?
183 this.SOURCE_TYPES.STATIC.value :
184 this.SOURCE_TYPES.SERVICE_PROPERTY.value;
185 this.updateSourceTypesRelatedValues();
186 this.currentRule.value = '';
189 filterOptionsByType() {
190 if (!this.selectedCapabilitiesPropertyObject) {
191 this.listOfValuesToAssign = [];
194 this.listOfValuesToAssign = this.listOfSourceOptions.reduce((result, op: PropertyModel) => {
195 if (op.type === this.selectedCapabilitiesPropertyObject.type && (!op.schemaType || op.schemaType === this.selectedCapabilitiesPropertyObject.schemaType)) {
196 result.push(new DropdownValue(op.name, op.name));
202 onValueChange(isValidValue) {
203 this.currentRule.updateValidity(isValidValue);
206 checkFormValidForSubmit() {
207 if (!this.serviceRulesList) {
208 const isStatic = this.currentRule.sourceName === this.SOURCE_TYPES.STATIC.value;
209 return this.currentRule.isValidRule(isStatic);
211 return this.serviceRulesList.every((rule) => rule.isValidRule(rule.sourceName === this.SOURCE_TYPES.STATIC.value));