Catalog alignment
[sdc.git] / catalog-ui / src / app / ng2 / pages / composition / panel / panel-tabs / service-consumption-tab / service-consumption-tab.component.ts
1
2 import { Component, Input } from '@angular/core';
3 import { Store } from '@ngxs/store';
4 import {
5     CapabilitiesGroup,
6     Capability,
7     Component as TopologyTemplate,
8     ComponentInstance,
9     FullComponentInstance,
10     InputBEModel,
11     InputsGroup,
12     InterfaceModel,
13     PropertiesGroup
14 } from 'app/models';
15 import { ComponentMetadata } from '../../../../../../models/component-metadata';
16 import { ServiceInstanceObject } from '../../../../../../models/service-instance-properties-and-interfaces';
17 import { EventListenerService } from '../../../../../../services/event-listener-service';
18 import { TopologyTemplateService } from '../../../../../services/component-services/topology-template.service';
19 import { ComponentGenericResponse } from '../../../../../services/responses/component-generic-response';
20 import { WorkspaceService } from '../../../../workspace/workspace.service';
21 import { SelectedComponentType } from '../../../common/store/graph.actions';
22 import { CompositionService } from '../../../composition.service';
23
24 @Component({
25     selector: 'service-consumption-tab',
26     templateUrl: './service-consumption-tab.component.html',
27     styleUrls: ['./service-consumption-tab.component.less'],
28 })
29 export class ServiceConsumptionTabComponent {
30     isComponentInstanceSelected: boolean;
31
32     instancesMappedList: ServiceInstanceObject[];
33     componentInstancesProperties: PropertiesGroup;
34     componentInstancesInputs: InputsGroup;
35     componentInstancesInterfaces: Map<string, InterfaceModel[]>;
36     componentInputs: InputBEModel[];
37     componentCapabilities: Capability[];
38     instancesCapabilitiesMap: Map<string, Capability[]>;
39     metadata: ComponentMetadata;
40
41     @Input() isViewOnly: boolean;
42     @Input() componentType: SelectedComponentType;
43     @Input() component: TopologyTemplate | FullComponentInstance;
44     @Input() input: any;
45
46     constructor(private store: Store,
47                 private topologyTemplateService: TopologyTemplateService,
48                 private workspaceService: WorkspaceService,
49                 private compositionService: CompositionService,
50                 private eventListenerService: EventListenerService ) {}
51     ngOnInit() {
52         this.metadata = this.workspaceService.metadata;
53         this.isComponentInstanceSelected = this.componentType === SelectedComponentType.COMPONENT_INSTANCE;
54         this.initInstances();
55     }
56
57     private initInstances = (): void => {
58         this.topologyTemplateService.getServiceConsumptionData(this.metadata.componentType, this.metadata.uniqueId).subscribe((genericResponse: ComponentGenericResponse) => {
59             this.componentInstancesProperties = genericResponse.componentInstancesProperties;
60             this.componentInstancesInputs = genericResponse.componentInstancesInputs;
61             this.componentInstancesInterfaces = genericResponse.componentInstancesInterfaces;
62             this.componentInputs = genericResponse.inputs;
63             this.buildInstancesCapabilitiesMap(genericResponse.componentInstances);
64             this.updateInstanceAttributes();
65         });
66     }
67
68     private buildInstancesCapabilitiesMap = (componentInstances: Array<ComponentInstance>): void => {
69         this.instancesCapabilitiesMap = new Map();
70         let flattenCapabilities = [];
71         _.forEach(componentInstances, (componentInstance) => {
72             flattenCapabilities = CapabilitiesGroup.getFlattenedCapabilities(componentInstance.capabilities);
73             this.instancesCapabilitiesMap[componentInstance.uniqueId] = _.filter(flattenCapabilities, cap => cap.properties && cap.ownerId === componentInstance.uniqueId);
74         });
75     }
76
77     private updateInstanceAttributes = (): void => {
78         if (this.isComponentInstanceSelected && this.componentInstancesProperties) {
79             this.instancesMappedList = this.compositionService.componentInstances.map((coInstance) => new ServiceInstanceObject({
80                 id: coInstance.uniqueId,
81                 name: coInstance.name,
82                 properties: this.componentInstancesProperties[coInstance.uniqueId] || [],
83                 inputs: this.componentInstancesInputs[coInstance.uniqueId] || [],
84                 interfaces: this.componentInstancesInterfaces[coInstance.uniqueId] || []
85             }));
86         }
87     }
88
89 }