Retrieve node_filter capabilities
[sdc.git] / catalog-ui / src / app / ng2 / pages / composition / panel / panel-tabs / service-dependencies-tab / service-dependencies-tab.component.ts
1 import {Component, Input} from '@angular/core';
2 import {Store} from '@ngxs/store';
3 import {
4     Component as TopologyTemplate,
5     FullComponentInstance,
6     PropertiesGroup,
7     PropertyBEModel,
8 } from 'app/models';
9 import {DEPENDENCY_EVENTS} from 'app/utils/constants';
10 import {ComponentMetadata} from '../../../../../../models/component-metadata';
11 import {ServiceInstanceObject} from '../../../../../../models/service-instance-properties-and-interfaces';
12 import {EventListenerService} from '../../../../../../services/event-listener-service';
13 import {ConstraintObject} from '../../../../../components/logic/service-dependencies/service-dependencies.component';
14 import {TopologyTemplateService} from '../../../../../services/component-services/topology-template.service';
15 import {ComponentGenericResponse} from '../../../../../services/responses/component-generic-response';
16 import {WorkspaceService} from '../../../../workspace/workspace.service';
17 import {SelectedComponentType} from '../../../common/store/graph.actions';
18 import {CompositionService} from '../../../composition.service';
19
20 @Component({
21     selector: 'service-dependencies-tab',
22     templateUrl: 'service-dependencies-tab.component.html',
23     styleUrls: ['service-dependencies-tab.component.less']
24 })
25 export class ServiceDependenciesTabComponent {
26     isComponentInstanceSelected: boolean;
27
28     selectedInstanceSiblings: ServiceInstanceObject[];
29     componentInstancesConstraints: any[];
30     selectedInstanceConstraints: ConstraintObject[];
31     selectedInstanceProperties: PropertyBEModel[];
32     componentInstanceProperties: PropertiesGroup;
33     metaData: ComponentMetadata;
34
35     @Input() isViewOnly: boolean;
36     @Input() componentType: SelectedComponentType;
37     @Input() component: FullComponentInstance | TopologyTemplate;
38     @Input() input: any;
39
40     constructor(private store: Store,
41                 private topologyTemplateService: TopologyTemplateService,
42                 private workspaceService: WorkspaceService,
43                 private compositionService: CompositionService,
44                 private eventListenerService: EventListenerService) {
45     }
46
47     ngOnInit() {
48         this.metaData = this.workspaceService.metadata;
49         this.isComponentInstanceSelected = this.componentType === SelectedComponentType.COMPONENT_INSTANCE;
50         this.initInstancesWithProperties();
51         this.loadConstraints();
52         this.initInstancesWithProperties();
53     }
54
55     public loadConstraints = (): void => {
56         this.topologyTemplateService.getServiceFilterConstraints(this.metaData.componentType, this.metaData.uniqueId).subscribe((response) => {
57             this.componentInstancesConstraints = response.nodeFilterforNode;
58         });
59     }
60
61     public notifyDependencyEventsObserver = (isChecked: boolean): void => {
62         this.eventListenerService.notifyObservers(DEPENDENCY_EVENTS.ON_DEPENDENCY_CHANGE, isChecked);
63     }
64
65     public updateSelectedInstanceConstraints = (constraintsList:Array<ConstraintObject>):void => {
66         this.componentInstancesConstraints[this.component.uniqueId].properties = constraintsList;
67         this.selectedInstanceConstraints = this.componentInstancesConstraints[this.component.uniqueId].properties;
68     }
69
70     public updateSelectedInstanceCapabilitiesConstraints = (constraintsList:Array<ConstraintObject>):void => {
71         this.componentInstancesConstraints[this.component.uniqueId].capabilities = constraintsList;
72         this.selectedInstanceConstraints = this.componentInstancesConstraints[this.component.uniqueId].capabilities;
73     }
74
75     private initInstancesWithProperties = (): void => {
76         this.topologyTemplateService.getComponentInstanceProperties(this.metaData.componentType, this.metaData.uniqueId).subscribe((genericResponse: ComponentGenericResponse) => {
77             this.componentInstanceProperties = genericResponse.componentInstancesProperties;
78             this.updateInstanceAttributes();
79         });
80     }
81
82     private updateInstanceAttributes = (): void => {
83         if (this.isComponentInstanceSelected && this.componentInstanceProperties) {
84             const instancesMappedList = this.compositionService.componentInstances.map((coInstance) => new ServiceInstanceObject({
85                 id: coInstance.uniqueId,
86                 name: coInstance.name,
87                 properties: this.componentInstanceProperties[coInstance.uniqueId] || []
88             }));
89             this.selectedInstanceProperties = this.componentInstanceProperties[this.component.uniqueId];
90             this.selectedInstanceSiblings = instancesMappedList.filter((coInstance) => coInstance.id !== this.component.uniqueId);
91         }
92     }
93 }