Catalog alignment
[sdc.git] / catalog-ui / src / app / models / components / service.ts
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 /**
22  * Created by obarda on 2/4/2016.
23  */
24 'use strict';
25 import * as _ from "lodash";
26 import {IServiceService} from "../../services/components/service-service";
27 import {Component, PropertyModel, DisplayModule, InputsAndProperties, InputModel, InstancesInputsOrPropertiesMapData, InstancesInputsPropertiesMap,
28     Distribution, DistributionComponent, ArtifactGroupModel} from "../../models";
29 import {ArtifactGroupType} from "../../utils/constants";
30 import {ComponentMetadata} from "../component-metadata";
31 import {ForwardingPath} from "app/models/forwarding-path";
32
33 export class Service extends Component {
34
35     public serviceApiArtifacts:ArtifactGroupModel;
36     public componentService:IServiceService;
37     public ecompGeneratedNaming:boolean;
38     public namingPolicy:string;
39     public serviceType:string;
40     public serviceRole:string;
41     public serviceFunction:string;
42     public environmentContext:string;
43     public instantiationType:string;
44     public forwardingPaths:{ [key:string]:ForwardingPath } = {};
45
46     constructor(componentService:IServiceService, $q:ng.IQService, component?:Service) {
47         super(componentService, $q, component);
48         this.ecompGeneratedNaming = true;
49         if (component) {
50             this.serviceApiArtifacts = new ArtifactGroupModel(component.serviceApiArtifacts);
51             this.filterTerm = this.name + ' ' + this.description + ' ' + (this.tags ? this.tags.toString() : '') + ' ' + this.version;
52             this.ecompGeneratedNaming = component.ecompGeneratedNaming;
53             this.namingPolicy = component.namingPolicy;
54             this.serviceType = component.serviceType;
55             this.serviceRole = component.serviceRole;
56             this.serviceFunction = component.serviceFunction;
57             this.instantiationType = component.instantiationType;
58             this.environmentContext = component.environmentContext;
59             if (component.categories && component.categories[0]) {
60                 this.mainCategory = component.categories[0].name;
61                 this.selectedCategory = this.mainCategory;
62             }
63         }
64         this.componentService = componentService;
65         this.iconSprite = "sprite-services-icons";
66     }
67
68     public getDistributionsList = ():ng.IPromise<Array<Distribution>> => {
69         return this.componentService.getDistributionsList(this.uuid);
70     };
71
72     public getDistributionsComponent = (distributionId:string):ng.IPromise<Array<DistributionComponent>> => {
73         return this.componentService.getDistributionComponents(distributionId);
74     };
75
76     public markAsDeployed = (distributionId:string):ng.IPromise<any> => {
77         return this.componentService.markAsDeployed(this.uniqueId, distributionId);
78     };
79
80     /* we need to change the name of the input to vfInstanceName + input name before sending to server in order to create the inputs on the service
81      *  we also need to remove already selected inputs (the inputs that already create on server, and disabled in the view - but they are selected so they are still in the view model
82      */
83     public createInputsFormInstances = (instancesInputsMap:InstancesInputsOrPropertiesMapData, instancePropertiesMap:InstancesInputsOrPropertiesMapData):ng.IPromise<Array<InputModel>> => {
84
85         let deferred = this.$q.defer<Array<InputModel>>();
86         let onSuccess = (inputsCreated:Array<InputModel>):void => {
87             this.inputs = inputsCreated.concat(this.inputs);
88             deferred.resolve(inputsCreated);
89         };
90         let onFailed = (error:any):void => {
91             deferred.reject(error);
92         };
93
94         let propertiesAndInputsMap:InstancesInputsPropertiesMap = new InstancesInputsPropertiesMap(instancesInputsMap, instancePropertiesMap);
95         propertiesAndInputsMap = propertiesAndInputsMap.cleanUnnecessaryDataBeforeSending(); // We need to create a copy of the map, without the already selected inputs / properties, and to send the clean map
96         this.componentService.createInputsFromInstancesInputs(this.uniqueId, propertiesAndInputsMap).then(onSuccess, onFailed);
97         return deferred.promise;
98     };
99
100     // we need to change the name of the input to vfInstanceName + input name before sending to server in order to create the inputs on the service
101     public getServiceInputInputsAndProperties = (inputId:string):ng.IPromise<InputsAndProperties> => {
102         let deferred = this.$q.defer<InputsAndProperties>();
103         let onSuccess = (inputsAndProperties:InputsAndProperties):void => {
104             let input:InputModel = _.find(this.inputs, (input:InputModel) => {
105                 return input.uniqueId === inputId;
106             });
107             input.inputs = inputsAndProperties.inputs;
108             input.properties = inputsAndProperties.properties;
109             deferred.resolve(inputsAndProperties);
110         };
111         let onFailed = (error:any):void => {
112             deferred.reject(error);
113         };
114         this.componentService.getComponentInputInputsAndProperties(this.uniqueId, inputId).then(onSuccess, onFailed);
115         return deferred.promise;
116     };
117
118     public deleteServiceInput = (inputId:string):ng.IPromise<InputModel> => {
119         let deferred = this.$q.defer<InputModel>();
120
121         let onSuccess = (deletedInput:InputModel):void => {
122             delete _.remove(this.inputs, {uniqueId: deletedInput.uniqueId})[0];
123             deferred.resolve(deletedInput);
124         };
125
126         let onFailed = (error:any):void => {
127             deferred.reject(error);
128         };
129
130         this.componentService.deleteComponentInput(this.uniqueId, inputId).then(onSuccess, onFailed);
131         return deferred.promise;
132     };
133
134     public getArtifactsByType = (artifactGroupType:string):ArtifactGroupModel => {
135         switch (artifactGroupType) {
136             case ArtifactGroupType.DEPLOYMENT:
137                 return this.deploymentArtifacts;
138             case ArtifactGroupType.INFORMATION:
139                 return this.artifacts;
140             case ArtifactGroupType.SERVICE_API:
141                 return this.serviceApiArtifacts;
142         }
143     };
144
145     public updateGroupInstanceProperties = (resourceInstanceId:string, group:DisplayModule, properties:Array<PropertyModel>):ng.IPromise<Array<PropertyModel>> => {
146
147         let deferred = this.$q.defer<Array<PropertyModel>>();
148         let onSuccess = (updatedProperties:Array<PropertyModel>):void => {
149             _.forEach(updatedProperties, (property:PropertyModel) => { // Replace all updated properties on the we needed to update
150                 _.extend(_.find(group.properties, {uniqueId: property.uniqueId}), property);
151             });
152             deferred.resolve(updatedProperties);
153         };
154         let onError = (error:any):void => {
155             deferred.reject(error);
156         };
157
158         this.componentService.updateGroupInstanceProperties(this.uniqueId, resourceInstanceId, group.groupInstanceUniqueId, properties).then(onSuccess, onError);
159         return deferred.promise;
160     };
161
162     getTypeUrl():string {
163         return 'services/';
164     }
165
166
167     public setComponentMetadata(componentMetadata: ComponentMetadata) {
168         super.setComponentMetadata(componentMetadata);
169         this.ecompGeneratedNaming = componentMetadata.ecompGeneratedNaming;
170         this.namingPolicy = componentMetadata.namingPolicy;
171         this.serviceType = componentMetadata.serviceType;
172         this.serviceRole = componentMetadata.serviceRole;
173         this.serviceFunction = componentMetadata.serviceFunction;
174         this.environmentContext = componentMetadata.environmentContext;
175         this.instantiationType = componentMetadata.instantiationType;
176         this.setComponentDisplayData();
177     }
178
179     setComponentDisplayData():void {
180         this.filterTerm = this.name + ' ' + this.description + ' ' + (this.tags ? this.tags.toString() : '') + ' ' + this.version;
181         if (this.categories && this.categories[0]) {
182             this.mainCategory = this.categories[0].name;
183             this.selectedCategory = this.mainCategory;
184         }
185         this.iconSprite = "sprite-services-icons";
186     }
187
188     public toJSON = ():any => {
189         let temp = angular.copy(this);
190         temp.componentService = undefined;
191         temp.filterTerm = undefined;
192         temp.iconSprite = undefined;
193         temp.mainCategory = undefined;
194         temp.subCategory = undefined;
195         temp.selectedInstance = undefined;
196         temp.showMenu = undefined;
197         temp.$q = undefined;
198         temp.selectedCategory = undefined;
199         temp.modules = undefined;
200         temp.groupInstances = undefined;
201         temp.policies = undefined;
202         return temp;
203     };
204 }
205