f844dfae4c9ef61b8a3048df1a6f9ff405cd65ee
[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     CapabilitiesGroup, Capability,
5     Component as TopologyTemplate,
6     FullComponentInstance,
7     PropertiesGroup,
8     PropertyBEModel, PropertyModel,
9 } from 'app/models';
10 import {DEPENDENCY_EVENTS} from 'app/utils/constants';
11 import {ComponentMetadata} from '../../../../../../models/component-metadata';
12 import {ServiceInstanceObject} from '../../../../../../models/service-instance-properties-and-interfaces';
13 import {EventListenerService} from '../../../../../../services/event-listener-service';
14 import {ConstraintObject} from '../../../../../components/logic/service-dependencies/service-dependencies.component';
15 import {TopologyTemplateService} from '../../../../../services/component-services/topology-template.service';
16 import {ComponentGenericResponse} from '../../../../../services/responses/component-generic-response';
17 import {WorkspaceService} from '../../../../workspace/workspace.service';
18 import {SelectedComponentType} from '../../../common/store/graph.actions';
19 import {CompositionService} from '../../../composition.service';
20 import {CapabilitiesConstraintObject} from "../../../../../components/logic/capabilities-constraint/capabilities-constraint.component";
21
22 @Component({
23     selector: 'service-dependencies-tab',
24     templateUrl: 'service-dependencies-tab.component.html',
25     styleUrls: ['service-dependencies-tab.component.less']
26 })
27 export class ServiceDependenciesTabComponent {
28     isComponentInstanceSelected: boolean;
29
30     selectedInstanceSiblings: ServiceInstanceObject[];
31     componentInstancesConstraints: any[];
32     selectedInstanceConstraints: ConstraintObject[];
33     selectedInstanceProperties: PropertyBEModel[];
34     componentInstanceProperties: PropertiesGroup;
35     componentInstanceCapabilityProperties: CapabilitiesGroup;
36     metaData: ComponentMetadata;
37     componentInstanceCapabilitiesMap : Map<string, PropertyModel[]> = new Map();
38     componentInstanceCapabilitiesNames: string[];
39
40     @Input() isViewOnly: boolean;
41     @Input() componentType: SelectedComponentType;
42     @Input() component: FullComponentInstance | TopologyTemplate;
43     @Input() input: any;
44
45     constructor(private store: Store,
46                 private topologyTemplateService: TopologyTemplateService,
47                 private workspaceService: WorkspaceService,
48                 private compositionService: CompositionService,
49                 private eventListenerService: EventListenerService) {
50     }
51
52     ngOnInit() {
53         this.metaData = this.workspaceService.metadata;
54         this.isComponentInstanceSelected = this.componentType === SelectedComponentType.COMPONENT_INSTANCE;
55         this.initInstancesWithProperties();
56         this.loadConstraints();
57         this.initInstancesWithProperties();
58         this.initInstancesWithCapabilityProperties()
59     }
60
61     public loadConstraints = (): void => {
62         this.topologyTemplateService.getServiceFilterConstraints(this.metaData.componentType, this.metaData.uniqueId).subscribe((response) => {
63             this.componentInstancesConstraints = response.nodeFilterforNode;
64         });
65     }
66
67     public notifyDependencyEventsObserver = (isChecked: boolean): void => {
68         this.eventListenerService.notifyObservers(DEPENDENCY_EVENTS.ON_DEPENDENCY_CHANGE, isChecked);
69     }
70
71     public updateSelectedInstanceConstraints = (constraintsList:Array<ConstraintObject>):void => {
72         this.componentInstancesConstraints[this.component.uniqueId].properties = constraintsList;
73         this.selectedInstanceConstraints = this.componentInstancesConstraints[this.component.uniqueId].properties;
74     }
75
76     public updateSelectedInstanceCapabilitiesConstraints = (constraintsList:Array<CapabilitiesConstraintObject>):void => {
77         this.componentInstancesConstraints[this.component.uniqueId].capabilities = constraintsList;
78         this.selectedInstanceConstraints = this.componentInstancesConstraints[this.component.uniqueId].capabilities;
79     }
80
81     private initInstancesWithProperties = (): void => {
82         this.topologyTemplateService.getComponentInstanceProperties(this.metaData.componentType, this.metaData.uniqueId).subscribe((genericResponse: ComponentGenericResponse) => {
83             this.componentInstanceProperties = genericResponse.componentInstancesProperties;
84             this.updateInstanceAttributes();
85         });
86     }
87
88     private updateInstanceAttributes = (): void => {
89         if (this.isComponentInstanceSelected && this.componentInstanceProperties) {
90             const instancesMappedList = this.compositionService.componentInstances.map((coInstance) => new ServiceInstanceObject({
91                 id: coInstance.uniqueId,
92                 name: coInstance.name,
93                 properties: this.componentInstanceProperties[coInstance.uniqueId] || []
94             }));
95             this.selectedInstanceProperties = this.componentInstanceProperties[this.component.uniqueId];
96             this.selectedInstanceSiblings = instancesMappedList.filter((coInstance) => coInstance.id !== this.component.uniqueId);
97         }
98     }
99
100     private initInstancesWithCapabilityProperties = (): void => {
101         this.componentInstanceCapabilityProperties = this.component.capabilities;
102         this.updateComponentInstanceCapabilities();
103     }
104
105     private updateComponentInstanceCapabilities = (): void => {
106         if (this.isComponentInstanceSelected && this.componentInstanceCapabilityProperties) {
107             _.forEach(_.flatten(_.values(this.componentInstanceCapabilityProperties)), (capability: Capability) => {
108                 if (capability.properties) {
109                     this.componentInstanceCapabilitiesMap.set(capability.name, capability.properties);
110                 }
111             });
112         }
113     }
114
115 }