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 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);
143 this.updateOperatorTypesList();
144 this.updateSourceTypesRelatedValues();
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;
152 this.operatorTypes = this.input.operatorTypes;
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
161 if(selectedSourceType) {
162 this.listOfSourceOptions = selectedSourceType.options || [];
163 this.assignedValueLabel = selectedSourceType.assignedLabel || this.SOURCE_TYPES.STATIC.label;
164 this.filterOptionsByType();
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();
177 onServicePropertyChanged() {
178 this.updateOperatorTypesList();
179 this.filterOptionsByType();
180 this.currentRule.value = '';
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 = '';
191 filterOptionsByType() {
192 if (!this.selectedCapabilitiesPropertyObject) {
193 this.listOfValuesToAssign = [];
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));
204 onValueChange(isValidValue) {
205 this.currentRule.updateValidity(isValidValue);
208 checkFormValidForSubmit() {
209 if (!this.serviceRulesList) {
210 const isStatic = this.currentRule.sourceName === this.SOURCE_TYPES.STATIC.value;
211 return this.currentRule.isValidRule(isStatic);
213 return this.serviceRulesList.every((rule) => rule.isValidRule(rule.sourceName === this.SOURCE_TYPES.STATIC.value));