Support TOSCA functions in Node Filters
[sdc.git] / catalog-ui / src / app / ng2 / components / logic / substitution-filter / substitution-filter.component.ts
1 /*
2 * ============LICENSE_START=======================================================
3 *  Copyright (C) 2020 Nordix Foundation. All rights reserved.
4 *  ================================================================================
5 *  Licensed under the Apache License, Version 2.0 (the "License");
6 *  you may not use this file except in compliance with the License.
7 *  You may obtain a copy of the License at
8 *
9 *        http://www.apache.org/licenses/LICENSE-2.0
10 *  Unless required by applicable law or agreed to in writing, software
11 *  distributed under the License is distributed on an "AS IS" BASIS,
12 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 *  See the License for the specific language governing permissions and
14 *  limitations under the License.
15 *
16 *  SPDX-License-Identifier: Apache-2.0
17 *  ============LICENSE_END=========================================================
18 */
19
20 import {Component, ComponentRef, EventEmitter, Input, OnChanges, OnInit, Output} from '@angular/core';
21 import {ButtonModel, ComponentInstance, InputBEModel, ModalModel, PropertyBEModel,} from 'app/models';
22 import {ModalComponent} from 'app/ng2/components/ui/modal/modal.component';
23 import {ServiceDependenciesEditorComponent} from 'app/ng2/pages/service-dependencies-editor/service-dependencies-editor.component';
24 import {ModalService} from 'app/ng2/services/modal.service';
25 import {TranslateService} from 'app/ng2/shared/translator/translate.service';
26 import {ComponentMetadata} from '../../../../models/component-metadata';
27 import {TopologyTemplateService} from '../../../services/component-services/topology-template.service';
28 import {ToscaFilterConstraintType} from "../../../../models/tosca-filter-constraint-type.enum";
29 import {FilterConstraint} from "app/models/filter-constraint";
30 import {ConstraintObjectUI} from "../../../../models/ui-models/constraint-object-ui";
31 import {FilterConstraintHelper, OPERATOR_TYPES} from "../../../../utils/filter-constraint-helper";
32
33 class I18nTexts {
34   static addSubstitutionFilterTxt: string;
35   static updateSubstitutionFilterTxt: string;
36   static deleteSubstitutionFilterTxt: string;
37   static deleteSubstitutionFilterMsg: string;
38   static modalCancel: string;
39   static modalCreate: string;
40   static modalSave: string;
41   static modalDelete: string;
42
43   public static translateTexts(translateService) {
44     I18nTexts.modalCancel = translateService.translate('MODAL_CANCEL');
45     I18nTexts.modalCreate = translateService.translate('MODAL_CREATE');
46     I18nTexts.modalSave = translateService.translate('MODAL_SAVE');
47     I18nTexts.modalDelete = translateService.translate('MODAL_DELETE');
48
49     I18nTexts.addSubstitutionFilterTxt = translateService.translate('ADD_SUBSTITUTION_FILTER');
50     I18nTexts.updateSubstitutionFilterTxt = translateService.translate('UPDATE_SUBSTITUTION_FILTER');
51     I18nTexts.deleteSubstitutionFilterTxt = translateService.translate('DELETE_SUBSTITUTION_FILTER');
52     I18nTexts.deleteSubstitutionFilterMsg = translateService.translate('DELETE_SUBSTITUTION_FILTER_MSG');
53   }
54 }
55
56 @Component({
57   selector: 'substitution-filter',
58   templateUrl: './substitution-filter.component.html',
59   styleUrls: ['substitution-filter.component.less'],
60   providers: [ModalService, TranslateService]
61 })
62
63 export class SubstitutionFilterComponent implements OnInit, OnChanges {
64   modalInstance: ComponentRef<ModalComponent>;
65   isLoading: boolean;
66   operatorTypes: any[];
67   constraintProperties: FilterConstraint[] = [];
68   constraintPropertyLabels: string[] = [];
69   PROPERTIES: string = ToscaFilterConstraintType.PROPERTIES;
70
71   @Input() readonly: boolean;
72   @Input() compositeService: ComponentMetadata;
73   @Input() currentServiceInstance: ComponentInstance;
74   @Input() selectedInstanceConstraints: FilterConstraint[] = [];
75   @Input() selectedInstanceProperties: PropertyBEModel[] = [];
76   @Input() parentServiceProperties: PropertyBEModel[] = [];
77   @Input() parentServiceInputs: InputBEModel[] = [];
78   @Output() updateSubstitutionFilterProperties: EventEmitter<FilterConstraint[]> = new EventEmitter<FilterConstraint[]>();
79   @Output() updateConstraintListEvent: EventEmitter<FilterConstraint[]> = new EventEmitter<FilterConstraint[]>();
80   @Output() loadConstraintListEvent: EventEmitter<any> = new EventEmitter();
81   @Output() hasSubstitutionFilter = new EventEmitter<boolean>();
82
83   constructor(private topologyTemplateService: TopologyTemplateService, private modalServiceNg2: ModalService, private translateService: TranslateService) {
84   }
85
86   ngOnInit(): void {
87     this.isLoading = false;
88     this.operatorTypes = [
89       {label: FilterConstraintHelper.convertToSymbol(OPERATOR_TYPES.GREATER_THAN), value: OPERATOR_TYPES.GREATER_THAN},
90       {label: FilterConstraintHelper.convertToSymbol(OPERATOR_TYPES.LESS_THAN), value: OPERATOR_TYPES.LESS_THAN},
91       {label: FilterConstraintHelper.convertToSymbol(OPERATOR_TYPES.EQUAL), value: OPERATOR_TYPES.EQUAL},
92       {label: FilterConstraintHelper.convertToSymbol(OPERATOR_TYPES.GREATER_OR_EQUAL), value: OPERATOR_TYPES.GREATER_OR_EQUAL},
93       {label: FilterConstraintHelper.convertToSymbol(OPERATOR_TYPES.LESS_OR_EQUAL), value: OPERATOR_TYPES.LESS_OR_EQUAL}
94     ];
95     this.loadSubstitutionFilter();
96     this.translateService.languageChangedObservable.subscribe((lang) => {
97       I18nTexts.translateTexts(this.translateService);
98     });
99   }
100
101   ngOnChanges(changes): void {
102     if (changes.compositeService) {
103       this.compositeService = changes.compositeService.currentValue;
104     }
105     if (changes.selectedInstanceConstraints && changes.selectedInstanceConstraints.currentValue !== changes.selectedInstanceConstraints.previousValue) {
106       this.selectedInstanceConstraints = changes.selectedInstanceConstraints.currentValue;
107       this.loadSubstitutionFilter();
108     }
109   }
110
111   private loadSubstitutionFilter = (): void => {
112     this.topologyTemplateService.getSubstitutionFilterConstraints(this.compositeService.componentType, this.compositeService.uniqueId)
113     .subscribe((response) => {
114       if(response.substitutionFilters) {
115         this.constraintProperties = response.substitutionFilters.properties;
116         this.buildConstraintPropertyLabels();
117       }
118     });
119   }
120
121   onAddSubstitutionFilter = (constraintType: string): void => {
122     const cancelButton: ButtonModel = new ButtonModel(I18nTexts.modalCancel, 'outline white', this.modalServiceNg2.closeCurrentModal);
123     const saveButton: ButtonModel = new ButtonModel(I18nTexts.modalCreate, 'blue', () => this.createSubstitutionFilter(constraintType), this.getDisabled);
124     const modalModel: ModalModel = new ModalModel('l', I18nTexts.addSubstitutionFilterTxt, '', [saveButton, cancelButton], 'standard');
125     this.modalInstance = this.modalServiceNg2.createCustomModal(modalModel);
126     this.modalServiceNg2.addDynamicContentToModal(
127         this.modalInstance,
128         ServiceDependenciesEditorComponent,
129         {
130           currentServiceName: this.currentServiceInstance.name,
131           operatorTypes: this.operatorTypes,
132           compositeServiceName: this.compositeService.name,
133           parentServiceInputs: this.parentServiceInputs,
134           parentServiceProperties: this.parentServiceProperties,
135           selectedInstanceProperties: this.parentServiceProperties,
136         }
137     );
138     this.modalInstance.instance.open();
139   }
140
141   createSubstitutionFilter = (constraintType: string): void => {
142     const newSubstitutionFilter: FilterConstraint = new FilterConstraint(this.modalInstance.instance.dynamicContent.instance.currentRule);
143     this.isLoading = true;
144     this.topologyTemplateService.createSubstitutionFilterConstraints(
145         this.compositeService.uniqueId,
146         newSubstitutionFilter,
147         this.compositeService.componentType,
148         constraintType
149     ).subscribe((response) => {
150       this.emitEventOnChanges(constraintType, response);
151       this.isLoading = false;
152     }, (err) => {
153       console.error(`Failed to Create Substitution Filter on the component with id: ${this.compositeService.uniqueId}`, err);
154       this.isLoading = false;
155     });
156     this.modalServiceNg2.closeCurrentModal();
157   }
158
159   onSelectSubstitutionFilter(constraintType: string, index: number) {
160     const cancelButton: ButtonModel = new ButtonModel(I18nTexts.modalCancel, 'outline white', this.modalServiceNg2.closeCurrentModal);
161     const updateButton: ButtonModel = new ButtonModel(I18nTexts.modalSave, 'blue', () => this.updateSubstitutionFilter(constraintType, index), this.getDisabled);
162     const modalModel: ModalModel = new ModalModel('l', I18nTexts.updateSubstitutionFilterTxt, '', [updateButton, cancelButton], 'standard');
163     this.modalInstance = this.modalServiceNg2.createCustomModal(modalModel);
164     this.modalServiceNg2.addDynamicContentToModal(
165         this.modalInstance,
166         ServiceDependenciesEditorComponent,
167         {
168           serviceRuleIndex: index,
169           serviceRules: _.map(this.constraintProperties, (constraint) => new ConstraintObjectUI(constraint)),
170           currentServiceName: this.currentServiceInstance.name,
171           operatorTypes: this.operatorTypes,
172           compositeServiceName: this.compositeService.name,
173           parentServiceInputs: this.parentServiceInputs,
174           parentServiceProperties: this.parentServiceProperties,
175           selectedInstanceProperties: this.parentServiceProperties,
176         }
177     );
178     this.modalInstance.instance.open();
179   }
180
181   updateSubstitutionFilter(constraintType: string, index: number): void {
182     const constraintToUpdate: FilterConstraint = this.modalInstance.instance.dynamicContent.instance.currentRule;
183     this.isLoading = true;
184     this.topologyTemplateService.updateSubstitutionFilterConstraint(
185         this.compositeService.uniqueId,
186         constraintToUpdate,
187         this.compositeService.componentType,
188         constraintType,
189         index
190     ).subscribe((response) => {
191       this.emitEventOnChanges(constraintType, response);
192       this.isLoading = false;
193     }, (error) => {
194       console.error("Failed to Update Substitution Filter on the component with id: ", this.compositeService.uniqueId, error);
195       this.isLoading = false;
196     });
197     this.modalServiceNg2.closeCurrentModal();
198   }
199
200   onDeleteSubstitutionFilter = (constraintType: string, index: number): void => {
201     this.isLoading = true;
202     this.topologyTemplateService.deleteSubstitutionFilterConstraints(
203         this.compositeService.uniqueId,
204         index,
205         this.compositeService.componentType,
206         constraintType
207     ).subscribe((response) => {
208       this.emitEventOnChanges(constraintType, response);
209       this.isLoading = false;
210     }, (error) => {
211       console.error("Failed to Delete Substitution Filter on the component with id: ",
212           this.compositeService.uniqueId, error);
213       this.isLoading = false;
214     });
215     this.modalServiceNg2.closeCurrentModal();
216   }
217
218   getDisabled = (): boolean => {
219     return !this.modalInstance.instance.dynamicContent.instance.checkFormValidForSubmit();
220   }
221
222   openDeleteModal = (constraintType: string, index: number): void => {
223     this.modalServiceNg2.createActionModal(I18nTexts.deleteSubstitutionFilterTxt, I18nTexts.deleteSubstitutionFilterMsg,
224         I18nTexts.modalDelete, () => this.onDeleteSubstitutionFilter(constraintType, index), I18nTexts.modalCancel).instance.open();
225   }
226
227   private emitEventOnChanges(constraintType: string, response): void {
228     if (ToscaFilterConstraintType.PROPERTIES === constraintType) {
229       this.updateSubstitutionFilterProperties.emit(response.properties);
230       this.constraintProperties = response.properties;
231       this.buildConstraintPropertyLabels();
232     }
233   }
234
235   private buildConstraintPropertyLabels(): void {
236     this.constraintPropertyLabels = [];
237     if (!this.constraintProperties) {
238       return;
239     }
240     this.constraintProperties.forEach(
241         constraint => this.constraintPropertyLabels.push(FilterConstraintHelper.buildFilterConstraintLabel(constraint))
242     )
243   }
244
245 }