No properties found when trying to add a node filter to a VF
[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, ComponentInstance,
30 } from 'app/models';
31 import {ResourceType} from "app/utils";
32 import {DEPENDENCY_EVENTS} from 'app/utils/constants';
33 import {ComponentMetadata} from '../../../../../../models/component-metadata';
34 import {ServiceInstanceObject} from '../../../../../../models/service-instance-properties-and-interfaces';
35 import {EventListenerService} from '../../../../../../services/event-listener-service';
36 import {TopologyTemplateService} from '../../../../../services/component-services/topology-template.service';
37 import {ComponentInstanceServiceNg2} from '../../../../../services/component-instance-services/component-instance.service';
38 import {ComponentGenericResponse} from '../../../../../services/responses/component-generic-response';
39 import {WorkspaceService} from '../../../../workspace/workspace.service';
40 import {SelectedComponentType} from '../../../common/store/graph.actions';
41 import {CompositionService} from '../../../composition.service';
42 import {FilterConstraint} from "../../../../../../models/filter-constraint";
43
44 @Component({
45     selector: 'service-dependencies-tab',
46     templateUrl: 'service-dependencies-tab.component.html',
47     styleUrls: ['service-dependencies-tab.component.less']
48 })
49 export class ServiceDependenciesTabComponent implements OnInit {
50     isComponentInstanceSelected: boolean;
51
52     selectedInstanceSiblings: ServiceInstanceObject[];
53     componentInstancesConstraints: any[];
54     selectedInstanceConstraints: FilterConstraint[];
55     selectedInstanceProperties: PropertyBEModel[];
56     componentInstanceProperties: PropertiesGroup;
57     componentInstanceCapabilityProperties: CapabilitiesGroup;
58     metaData: ComponentMetadata;
59     componentInstanceCapabilitiesMap : Map<string, PropertyModel[]> = new Map();
60
61     @Input() isViewOnly: boolean;
62     @Input() componentType: SelectedComponentType;
63     @Input() component: FullComponentInstance | TopologyTemplate;
64     @Input() input: any;
65
66     constructor(private store: Store,
67                 private topologyTemplateService: TopologyTemplateService,
68                 private componentInstanceServiceNg2: ComponentInstanceServiceNg2,
69                 private workspaceService: WorkspaceService,
70                 private compositionService: CompositionService,
71                 private eventListenerService: EventListenerService) {
72     }
73
74     ngOnInit(): void {
75         this.metaData = this.workspaceService.metadata;
76         this.isComponentInstanceSelected = this.componentType === SelectedComponentType.COMPONENT_INSTANCE;
77         this.initInstancesWithProperties();
78         this.loadConstraints();
79         this.initInstancesWithProperties();
80         this.initInstancesWithCapabilityProperties()
81     }
82
83     public loadConstraints = (): void => {
84         this.topologyTemplateService.getServiceFilterConstraints(this.metaData.componentType, this.metaData.uniqueId).subscribe((response) => {
85             this.componentInstancesConstraints = response.nodeFilterforNode;
86         });
87     }
88
89     public notifyDependencyEventsObserver = (isChecked: boolean): void => {
90         this.eventListenerService.notifyObservers(DEPENDENCY_EVENTS.ON_DEPENDENCY_CHANGE, isChecked);
91     }
92
93     public updateSelectedInstanceConstraints = (constraintsList:Array<FilterConstraint>):void => {
94         this.componentInstancesConstraints[this.component.uniqueId].properties = constraintsList;
95         this.selectedInstanceConstraints = this.componentInstancesConstraints[this.component.uniqueId].properties;
96     }
97
98     public updateSelectedInstanceCapabilitiesConstraints = (constraintsList:Array<FilterConstraint>):void => {
99         this.componentInstancesConstraints[this.component.uniqueId].capabilities = constraintsList;
100         this.selectedInstanceConstraints = this.componentInstancesConstraints[this.component.uniqueId].capabilities;
101     }
102
103     private initInstancesWithProperties = (): void => {
104         if (this.component instanceof FullComponentInstance && this.isInput(this.component.resourceType)) {
105             this.componentInstanceServiceNg2
106             .getComponentInstanceInputsByIdAndType(this.metaData.uniqueId, this.metaData.componentType, this.component as ComponentInstance)
107             .subscribe(response => {
108                 this.selectedInstanceProperties = response;
109             });
110         } else {
111             this.topologyTemplateService.getComponentInstanceProperties(this.metaData.componentType, this.metaData.uniqueId).subscribe((genericResponse: ComponentGenericResponse) => {
112                 this.componentInstanceProperties = genericResponse.componentInstancesProperties;
113                 this.updateInstanceAttributes();
114             });
115         }
116     }
117
118     private isInput = (instanceType:string):boolean =>{
119         return instanceType === ResourceType.VF || instanceType === ResourceType.PNF || instanceType === ResourceType.CVFC || instanceType === ResourceType.CR;
120     }
121
122     private updateInstanceAttributes = (): void => {
123         if (this.isComponentInstanceSelected && this.componentInstanceProperties) {
124             const instancesMappedList = this.compositionService.componentInstances.map((coInstance) => new ServiceInstanceObject({
125                 id: coInstance.uniqueId,
126                 name: coInstance.name,
127                 properties: this.componentInstanceProperties[coInstance.uniqueId] || []
128             }));
129             this.selectedInstanceProperties = this.componentInstanceProperties[this.component.uniqueId];
130             this.selectedInstanceSiblings = instancesMappedList.filter((coInstance) => coInstance.id !== this.component.uniqueId);
131         }
132     }
133
134     private initInstancesWithCapabilityProperties(): void {
135         this.componentInstanceCapabilityProperties = this.component.capabilities;
136         this.updateComponentInstanceCapabilities();
137     }
138
139     private updateComponentInstanceCapabilities = (): void => {
140         if (this.isComponentInstanceSelected && this.componentInstanceCapabilityProperties) {
141             _.forEach(_.flatten(_.values(this.componentInstanceCapabilityProperties)), (capability: Capability) => {
142                 if (capability.properties) {
143                     this.componentInstanceCapabilitiesMap.set(capability.name, capability.properties);
144                 }
145             });
146         }
147     }
148
149 }