Fix for substitution filter properties
[sdc.git] / catalog-ui / src / app / ng2 / pages / composition / panel / panel-tabs / substitution-filter-tab / substitution-filter-tab.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, Input } from '@angular/core';
21 import { Store } from '@ngxs/store';
22 import {
23     Component as TopologyTemplate,
24     FullComponentInstance,
25     PropertiesGroup,
26     PropertyBEModel,
27 } from 'app/models';
28 import { SUBSTITUTION_FILTER_EVENTS } from 'app/utils/constants';
29 import { ComponentMetadata } from '../../../../../../models/component-metadata';
30 import { ServiceInstanceObject } from '../../../../../../models/service-instance-properties-and-interfaces';
31 import { EventListenerService } from '../../../../../../services/event-listener-service';
32 import { ConstraintObject } from '../../../../../components/logic/service-dependencies/service-dependencies.component';
33 import { TopologyTemplateService } from '../../../../../services/component-services/topology-template.service';
34 import { ComponentGenericResponse } from '../../../../../services/responses/component-generic-response';
35 import { WorkspaceService } from '../../../../workspace/workspace.service';
36 import { SelectedComponentType } from '../../../common/store/graph.actions';
37 import { CompositionService } from '../../../composition.service';
38
39 @Component({
40     selector: 'substitution-filter-tab',
41     templateUrl: 'substitution-filter-tab.component.html',
42     styleUrls: ['substitution-filter-tab.component.less']
43 })
44 export class SubstitutionFilterTabComponent {
45     isComponentInstanceSelected: boolean;
46
47     selectedInstanceSiblings: ServiceInstanceObject[];
48     componentInstancesConstraints: any[];
49     selectedInstanceConstraints: ConstraintObject[];
50     selectedInstanceProperties: PropertyBEModel[];
51     componentInstanceProperties: PropertiesGroup;
52     metaData: ComponentMetadata;
53
54     @Input() isViewOnly: boolean;
55     @Input() componentType: SelectedComponentType;
56     @Input() component: FullComponentInstance | TopologyTemplate;
57     @Input() input: any;
58
59     constructor(private store: Store,
60                 private topologyTemplateService: TopologyTemplateService,
61                 private workspaceService: WorkspaceService,
62                 private compositionService: CompositionService,
63                 private eventListenerService: EventListenerService) {
64     }
65
66     ngOnInit() {
67         this.metaData = this.workspaceService.metadata;
68         this.isComponentInstanceSelected = this.componentType === SelectedComponentType.COMPONENT_INSTANCE;
69         this.initInstancesWithProperties();
70     }
71
72     public loadConstraints = (): void => {
73         this.topologyTemplateService.getSubstitutionFilterConstraints(this.metaData.componentType, this.metaData.uniqueId).subscribe((response) => {
74             this.componentInstancesConstraints = response.substitutionFilterForTopologyTemplate;
75         });
76     }
77
78     public notifyDependencyEventsObserver = (isChecked: boolean): void => {
79         this.eventListenerService.notifyObservers(SUBSTITUTION_FILTER_EVENTS.ON_SUBSTITUTION_FILTER_CHANGE, isChecked);
80     }
81
82     public updateSelectedInstanceConstraints = (constraintsList:Array<ConstraintObject>):void => {
83         this.componentInstancesConstraints[this.component.uniqueId].properties = constraintsList;
84         this.selectedInstanceConstraints = this.componentInstancesConstraints[this.component.uniqueId].properties;
85     }
86
87     private initInstancesWithProperties = (): void => {
88         this.topologyTemplateService.getComponentPropertiesSubstitutionFilter(this.metaData.componentType, this.metaData.uniqueId).subscribe((genericResponse: ComponentGenericResponse) => {
89             this.selectedInstanceProperties = genericResponse.properties;
90             this.updateInstanceAttributes();
91         });
92     }
93
94     private updateInstanceAttributes = (): void => {
95         if (this.isComponentInstanceSelected && this.componentInstanceProperties) {
96             const instancesMappedList = this.compositionService.componentInstances.map((coInstance) => new ServiceInstanceObject({
97                 id: coInstance.uniqueId,
98                 name: coInstance.name,
99                 properties: this.componentInstanceProperties[coInstance.uniqueId] || []
100             }));
101             this.selectedInstanceProperties = this.componentInstanceProperties[this.component.uniqueId];
102             this.selectedInstanceSiblings = instancesMappedList.filter((coInstance) => coInstance.id !== this.component.uniqueId);
103         }
104     }
105 }