7ca46040042f8515937ca1cf79a6d19a29997d32
[sdc.git] / catalog-ui / src / app / ng2 / pages / composition / panel / panel-tabs / service-dependencies-tab / service-dependencies-tab.component.ts
1 /*
2  * -
3  *  ============LICENSE_START=======================================================
4  *  Copyright (C) 2022 Nordix Foundation.
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *       http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 import {Component, Input, OnInit} from '@angular/core';
23 import {Store} from '@ngxs/store';
24 import {
25     CapabilitiesGroup, Capability,
26     Component as TopologyTemplate,
27     FullComponentInstance,
28     PropertiesGroup,
29     PropertyBEModel, PropertyModel,
30 } from 'app/models';
31 import {DEPENDENCY_EVENTS} from 'app/utils/constants';
32 import {ComponentMetadata} from '../../../../../../models/component-metadata';
33 import {ServiceInstanceObject} from '../../../../../../models/service-instance-properties-and-interfaces';
34 import {EventListenerService} from '../../../../../../services/event-listener-service';
35 import {TopologyTemplateService} from '../../../../../services/component-services/topology-template.service';
36 import {ComponentGenericResponse} from '../../../../../services/responses/component-generic-response';
37 import {WorkspaceService} from '../../../../workspace/workspace.service';
38 import {SelectedComponentType} from '../../../common/store/graph.actions';
39 import {CompositionService} from '../../../composition.service';
40 import {FilterConstraint} from "../../../../../../models/filter-constraint";
41
42 @Component({
43     selector: 'service-dependencies-tab',
44     templateUrl: 'service-dependencies-tab.component.html',
45     styleUrls: ['service-dependencies-tab.component.less']
46 })
47 export class ServiceDependenciesTabComponent implements OnInit {
48     isComponentInstanceSelected: boolean;
49
50     selectedInstanceSiblings: ServiceInstanceObject[];
51     componentInstancesConstraints: any[];
52     selectedInstanceConstraints: FilterConstraint[];
53     selectedInstanceProperties: PropertyBEModel[];
54     componentInstanceProperties: PropertiesGroup;
55     componentInstanceCapabilityProperties: CapabilitiesGroup;
56     metaData: ComponentMetadata;
57     componentInstanceCapabilitiesMap : Map<string, PropertyModel[]> = new Map();
58
59     @Input() isViewOnly: boolean;
60     @Input() componentType: SelectedComponentType;
61     @Input() component: FullComponentInstance | TopologyTemplate;
62     @Input() input: any;
63
64     constructor(private store: Store,
65                 private topologyTemplateService: TopologyTemplateService,
66                 private workspaceService: WorkspaceService,
67                 private compositionService: CompositionService,
68                 private eventListenerService: EventListenerService) {
69     }
70
71     ngOnInit(): void {
72         this.metaData = this.workspaceService.metadata;
73         this.isComponentInstanceSelected = this.componentType === SelectedComponentType.COMPONENT_INSTANCE;
74         this.initInstancesWithProperties();
75         this.loadConstraints();
76         this.initInstancesWithProperties();
77         this.initInstancesWithCapabilityProperties()
78     }
79
80     public loadConstraints = (): void => {
81         this.topologyTemplateService.getServiceFilterConstraints(this.metaData.componentType, this.metaData.uniqueId).subscribe((response) => {
82             this.componentInstancesConstraints = response.nodeFilterforNode;
83         });
84     }
85
86     public notifyDependencyEventsObserver = (isChecked: boolean): void => {
87         this.eventListenerService.notifyObservers(DEPENDENCY_EVENTS.ON_DEPENDENCY_CHANGE, isChecked);
88     }
89
90     public updateSelectedInstanceConstraints = (constraintsList:Array<FilterConstraint>):void => {
91         this.componentInstancesConstraints[this.component.uniqueId].properties = constraintsList;
92         this.selectedInstanceConstraints = this.componentInstancesConstraints[this.component.uniqueId].properties;
93     }
94
95     public updateSelectedInstanceCapabilitiesConstraints = (constraintsList:Array<FilterConstraint>):void => {
96         this.componentInstancesConstraints[this.component.uniqueId].capabilities = constraintsList;
97         this.selectedInstanceConstraints = this.componentInstancesConstraints[this.component.uniqueId].capabilities;
98     }
99
100     private initInstancesWithProperties = (): void => {
101         this.topologyTemplateService.getComponentInstanceProperties(this.metaData.componentType, this.metaData.uniqueId).subscribe((genericResponse: ComponentGenericResponse) => {
102             this.componentInstanceProperties = genericResponse.componentInstancesProperties;
103             this.updateInstanceAttributes();
104         });
105     }
106
107     private updateInstanceAttributes = (): void => {
108         if (this.isComponentInstanceSelected && this.componentInstanceProperties) {
109             const instancesMappedList = this.compositionService.componentInstances.map((coInstance) => new ServiceInstanceObject({
110                 id: coInstance.uniqueId,
111                 name: coInstance.name,
112                 properties: this.componentInstanceProperties[coInstance.uniqueId] || []
113             }));
114             this.selectedInstanceProperties = this.componentInstanceProperties[this.component.uniqueId];
115             this.selectedInstanceSiblings = instancesMappedList.filter((coInstance) => coInstance.id !== this.component.uniqueId);
116         }
117     }
118
119     private initInstancesWithCapabilityProperties(): void {
120         this.componentInstanceCapabilityProperties = this.component.capabilities;
121         this.updateComponentInstanceCapabilities();
122     }
123
124     private updateComponentInstanceCapabilities = (): void => {
125         if (this.isComponentInstanceSelected && this.componentInstanceCapabilityProperties) {
126             _.forEach(_.flatten(_.values(this.componentInstanceCapabilityProperties)), (capability: Capability) => {
127                 if (capability.properties) {
128                     this.componentInstanceCapabilitiesMap.set(capability.name, capability.properties);
129                 }
130             });
131         }
132     }
133
134 }