1c7c6b60b4bf5f7fbb74c73db49ae551ea58248f
[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 {FileUploadModel} from "../../directives/file-upload/file-upload";
31 import {ComponentMetadata} from "../component-metadata";
32 import {ForwardingPath} from "app/models/forwarding-path";
33
34 export class Service extends Component {
35
36     public serviceApiArtifacts:ArtifactGroupModel;
37     public componentService:IServiceService;
38     public ecompGeneratedNaming:boolean;
39     public namingPolicy:string;
40     public serviceType:string;
41     public serviceRole:string;
42     public serviceFunction:string;
43     public environmentContext:string;
44     public instantiationType:string;
45     public forwardingPaths:{ [key:string]:ForwardingPath } = {};
46     public payloadData: string;
47     public payloadName: string;
48     public importedFile: FileUploadModel;
49
50     // Onboarding parameters
51     public csarUUID: string;
52     public csarVersion: string;
53     public csarPackageType: string;
54     public packageId: string;
55
56     constructor(componentService:IServiceService, $q:ng.IQService, component?:Service) {
57         super(componentService, $q, component);
58         this.ecompGeneratedNaming = true;
59         if (component) {
60             this.serviceApiArtifacts = new ArtifactGroupModel(component.serviceApiArtifacts);
61             this.filterTerm = this.name + ' ' + this.description + ' ' + (this.tags ? this.tags.toString() : '') + ' ' + this.version;
62             this.ecompGeneratedNaming = component.ecompGeneratedNaming;
63             this.namingPolicy = component.namingPolicy;
64             this.serviceType = component.serviceType;
65             this.serviceRole = component.serviceRole;
66             this.serviceFunction = component.serviceFunction;
67             this.instantiationType = component.instantiationType;
68             this.environmentContext = component.environmentContext;
69             this.payloadData = component.payloadData ? component.payloadData : undefined;
70             this.payloadName = component.payloadName ? component.payloadName : undefined;
71             this.csarUUID = component.csarUUID;
72             this.csarVersion = component.csarVersion;
73             if (component.categories && component.categories[0]) {
74                 this.mainCategory = component.categories[0].name;
75                 this.selectedCategory = this.mainCategory;
76                 this.importedFile = component.importedFile;
77             }
78         }
79         this.componentService = componentService;
80         this.iconSprite = "sprite-services-icons";
81     }
82
83     public importComponentOnServer = (): ng.IPromise<Component> => {
84         let deferred = this.$q.defer<Component>();
85         let onSuccess = (component: Service): void => {
86             this.payloadData = undefined;
87             this.payloadName = undefined;
88             deferred.resolve(component);
89         };
90         let onError = (error: any): void => {
91             deferred.reject(error);
92         };
93
94         this.handleTags();
95         if (this.importedFile) {
96             this.payloadData = this.importedFile.base64;
97             this.payloadName = this.importedFile.filename;
98         }
99         this.componentService.importComponent(this).then(onSuccess, onError);
100         return deferred.promise;
101     };
102
103     public getDistributionsList = ():ng.IPromise<Array<Distribution>> => {
104         return this.componentService.getDistributionsList(this.uuid);
105     };
106
107     public getDistributionsComponent = (distributionId:string):ng.IPromise<Array<DistributionComponent>> => {
108         return this.componentService.getDistributionComponents(distributionId);
109     };
110
111     public markAsDeployed = (distributionId:string):ng.IPromise<any> => {
112         return this.componentService.markAsDeployed(this.uniqueId, distributionId);
113     };
114
115     /* 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
116      *  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
117      */
118     public createInputsFormInstances = (instancesInputsMap:InstancesInputsOrPropertiesMapData, instancePropertiesMap:InstancesInputsOrPropertiesMapData):ng.IPromise<Array<InputModel>> => {
119
120         let deferred = this.$q.defer<Array<InputModel>>();
121         let onSuccess = (inputsCreated:Array<InputModel>):void => {
122             this.inputs = inputsCreated.concat(this.inputs);
123             deferred.resolve(inputsCreated);
124         };
125         let onFailed = (error:any):void => {
126             deferred.reject(error);
127         };
128
129         let propertiesAndInputsMap:InstancesInputsPropertiesMap = new InstancesInputsPropertiesMap(instancesInputsMap, instancePropertiesMap);
130         propertiesAndInputsMap = propertiesAndInputsMap.cleanUnnecessaryDataBeforeSending(); // We need to create a copy of the map, without the already selected inputs / properties, and to send the clean map
131         this.componentService.createInputsFromInstancesInputs(this.uniqueId, propertiesAndInputsMap).then(onSuccess, onFailed);
132         return deferred.promise;
133     };
134
135     // 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
136     public getServiceInputInputsAndProperties = (inputId:string):ng.IPromise<InputsAndProperties> => {
137         let deferred = this.$q.defer<InputsAndProperties>();
138         let onSuccess = (inputsAndProperties:InputsAndProperties):void => {
139             let input:InputModel = _.find(this.inputs, (input:InputModel) => {
140                 return input.uniqueId === inputId;
141             });
142             input.inputs = inputsAndProperties.inputs;
143             input.properties = inputsAndProperties.properties;
144             deferred.resolve(inputsAndProperties);
145         };
146         let onFailed = (error:any):void => {
147             deferred.reject(error);
148         };
149         this.componentService.getComponentInputInputsAndProperties(this.uniqueId, inputId).then(onSuccess, onFailed);
150         return deferred.promise;
151     };
152
153     public deleteServiceInput = (inputId:string):ng.IPromise<InputModel> => {
154         let deferred = this.$q.defer<InputModel>();
155
156         let onSuccess = (deletedInput:InputModel):void => {
157             delete _.remove(this.inputs, {uniqueId: deletedInput.uniqueId})[0];
158             deferred.resolve(deletedInput);
159         };
160
161         let onFailed = (error:any):void => {
162             deferred.reject(error);
163         };
164
165         this.componentService.deleteComponentInput(this.uniqueId, inputId).then(onSuccess, onFailed);
166         return deferred.promise;
167     };
168
169     public getArtifactsByType = (artifactGroupType:string):ArtifactGroupModel => {
170         switch (artifactGroupType) {
171             case ArtifactGroupType.DEPLOYMENT:
172                 return this.deploymentArtifacts;
173             case ArtifactGroupType.INFORMATION:
174                 return this.artifacts;
175             case ArtifactGroupType.SERVICE_API:
176                 return this.serviceApiArtifacts;
177         }
178     };
179
180     public updateGroupInstanceProperties = (resourceInstanceId:string, group:DisplayModule, properties:Array<PropertyModel>):ng.IPromise<Array<PropertyModel>> => {
181
182         let deferred = this.$q.defer<Array<PropertyModel>>();
183         let onSuccess = (updatedProperties:Array<PropertyModel>):void => {
184             _.forEach(updatedProperties, (property:PropertyModel) => { // Replace all updated properties on the we needed to update
185                 _.extend(_.find(group.properties, {uniqueId: property.uniqueId}), property);
186             });
187             deferred.resolve(updatedProperties);
188         };
189         let onError = (error:any):void => {
190             deferred.reject(error);
191         };
192
193         this.componentService.updateGroupInstanceProperties(this.uniqueId, resourceInstanceId, group.groupInstanceUniqueId, properties).then(onSuccess, onError);
194         return deferred.promise;
195     };
196
197     getTypeUrl():string {
198         return 'services/';
199     }
200
201
202     public setComponentMetadata(componentMetadata: ComponentMetadata) {
203         super.setComponentMetadata(componentMetadata);
204         this.ecompGeneratedNaming = componentMetadata.ecompGeneratedNaming;
205         this.namingPolicy = componentMetadata.namingPolicy;
206         this.serviceType = componentMetadata.serviceType;
207         this.serviceRole = componentMetadata.serviceRole;
208         this.serviceFunction = componentMetadata.serviceFunction;
209         this.environmentContext = componentMetadata.environmentContext;
210         this.instantiationType = componentMetadata.instantiationType;
211         this.setComponentDisplayData();
212     }
213
214     setComponentDisplayData():void {
215         this.filterTerm = this.name + ' ' + this.description + ' ' + (this.tags ? this.tags.toString() : '') + ' ' + this.version;
216         if (this.categories && this.categories[0]) {
217             this.mainCategory = this.categories[0].name;
218             this.selectedCategory = this.mainCategory;
219         }
220         this.iconSprite = "sprite-services-icons";
221     }
222
223     public isSubstituteCandidate(): boolean {
224         return !!this.derivedFromGenericType;
225     }
226
227     public toJSON = ():any => {
228         let temp = angular.copy(this);
229         temp.componentService = undefined;
230         temp.filterTerm = undefined;
231         temp.iconSprite = undefined;
232         temp.mainCategory = undefined;
233         temp.subCategory = undefined;
234         temp.selectedInstance = undefined;
235         temp.showMenu = undefined;
236         temp.$q = undefined;
237         temp.selectedCategory = undefined;
238         temp.modules = undefined;
239         temp.groupInstances = undefined;
240         temp.policies = undefined;
241         return temp;
242     };
243 }
244