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