CSIT Fix for SDC-2585
[sdc.git] / catalog-ui / src / app / view-models / workspace / tabs / composition / tabs / service-consumption / service-consumption-view-model.ts
1 /*!
2 * Copyright © 2016-2018 European Support Limited
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13 * or implied. See the License for the specific language governing
14 * permissions and limitations under the License.
15 */
16
17
18 import {ICompositionViewModelScope} from "../../composition-view-model";
19 import {
20     Service,
21     PropertiesGroup,
22     InputsGroup,
23     ServiceInstanceObject,
24     InterfaceModel,
25     InputBEModel,
26     CapabilitiesGroup,
27     Capability,
28     ComponentInstance
29 } from 'app/models';
30 import {ComponentGenericResponse} from "app/ng2/services/responses/component-generic-response";
31 import {ServiceServiceNg2} from "app/ng2/services/component-services/service.service";
32
33 interface IServiceConsumptionViewModelScope extends ICompositionViewModelScope {
34     service: Service;
35     instancesMappedList: Array<ServiceInstanceObject>;
36     componentInstancesProperties: PropertiesGroup;
37     componentInstancesInputs: InputsGroup;
38     componentInstancesInterfaces: Map<string, Array<InterfaceModel>>;
39     componentInputs: Array<InputBEModel>;
40     componentCapabilities: Array<Capability>;
41     instancesCapabilitiesMap: Map<string, Array<Capability>>;
42 }
43
44
45 export class ServiceConsumptionViewModel {
46
47     static '$inject' = [
48         '$scope',
49         'ServiceServiceNg2'
50     ];
51
52     constructor(private $scope:IServiceConsumptionViewModelScope, private ServiceServiceNg2:ServiceServiceNg2) {
53         this.$scope.service = <Service>this.$scope.currentComponent;
54         this.initInstances();
55         this.initScope();
56     }
57
58     private initInstances = ():void => {
59         this.ServiceServiceNg2.getServiceConsumptionData(this.$scope.service).subscribe((genericResponse:ComponentGenericResponse) => {
60             this.$scope.componentInstancesProperties = genericResponse.componentInstancesProperties;
61             this.$scope.componentInstancesInputs = genericResponse.componentInstancesInputs;
62             this.$scope.componentInstancesInterfaces = genericResponse.componentInstancesInterfaces;
63             this.$scope.componentInputs = genericResponse.inputs;
64             this.buildInstancesCapabilitiesMap(genericResponse.componentInstances);
65             this.updateInstanceAttributes();
66         });
67     }
68
69     buildInstancesCapabilitiesMap = (componentInstances: Array<ComponentInstance>): void => {
70         this.$scope.instancesCapabilitiesMap = new Map();
71         let flattenCapabilities = [];
72         _.forEach(componentInstances, componentInstance => {
73             flattenCapabilities = CapabilitiesGroup.getFlattenedCapabilities(componentInstance.capabilities);
74             this.$scope.instancesCapabilitiesMap[componentInstance.uniqueId] = _.filter(flattenCapabilities, cap => cap.properties && cap.ownerId === componentInstance.uniqueId);
75         });
76     }
77
78     private updateInstanceAttributes = ():void => {
79         if (this.$scope.isComponentInstanceSelected() && this.$scope.componentInstancesProperties) {
80             this.$scope.instancesMappedList = this.$scope.service.componentInstances.map(coInstance => new ServiceInstanceObject({
81                 id: coInstance.uniqueId,
82                 name: coInstance.name,
83                 properties: this.$scope.componentInstancesProperties[coInstance.uniqueId] || [],
84                 inputs: this.$scope.componentInstancesInputs[coInstance.uniqueId] || [],
85                 interfaces: this.$scope.componentInstancesInterfaces[coInstance.uniqueId] || []
86             }));
87         }
88     }
89
90     private initScope = ():void => {
91         this.$scope.$watch('currentComponent.selectedInstance', ():void => {
92             this.updateInstanceAttributes();
93         });
94
95         this.$scope.registerCreateInstanceEvent(() => {
96             this.initInstances();
97         });
98
99         this.$scope.$on('$destroy', this.$scope.unregisterCreateInstanceEvent);
100     }
101 }