re base code
[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 environmentContext:string;
42     public instantiationType:string;
43     public forwardingPaths:{ [key:string]:ForwardingPath } = {};
44
45     constructor(componentService:IServiceService, $q:ng.IQService, component?:Service) {
46         super(componentService, $q, component);
47         this.ecompGeneratedNaming = true;
48         if (component) {
49             this.serviceApiArtifacts = new ArtifactGroupModel(component.serviceApiArtifacts);
50             this.filterTerm = this.name + ' ' + this.description + ' ' + (this.tags ? this.tags.toString() : '') + ' ' + this.version;
51             this.ecompGeneratedNaming = component.ecompGeneratedNaming;
52             this.namingPolicy = component.namingPolicy;
53             this.serviceType = component.serviceType;
54             this.serviceRole = component.serviceRole;
55             this.instantiationType = component.instantiationType;
56             this.environmentContext = component.environmentContext;
57             if (component.categories && component.categories[0]) {
58                 this.mainCategory = component.categories[0].name;
59                 this.selectedCategory = this.mainCategory;
60             }
61         }
62         this.componentService = componentService;
63         this.iconSprite = "sprite-services-icons";
64     }
65
66     public getDistributionsList = ():ng.IPromise<Array<Distribution>> => {
67         return this.componentService.getDistributionsList(this.uuid);
68     };
69
70     public getDistributionsComponent = (distributionId:string):ng.IPromise<Array<DistributionComponent>> => {
71         return this.componentService.getDistributionComponents(distributionId);
72     };
73
74     public markAsDeployed = (distributionId:string):ng.IPromise<any> => {
75         return this.componentService.markAsDeployed(this.uniqueId, distributionId);
76     };
77
78     /* 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
79      *  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
80      */
81     public createInputsFormInstances = (instancesInputsMap:InstancesInputsOrPropertiesMapData, instancePropertiesMap:InstancesInputsOrPropertiesMapData):ng.IPromise<Array<InputModel>> => {
82
83         let deferred = this.$q.defer<Array<InputModel>>();
84         let onSuccess = (inputsCreated:Array<InputModel>):void => {
85             this.inputs = inputsCreated.concat(this.inputs);
86             deferred.resolve(inputsCreated);
87         };
88         let onFailed = (error:any):void => {
89             deferred.reject(error);
90         };
91
92         let propertiesAndInputsMap:InstancesInputsPropertiesMap = new InstancesInputsPropertiesMap(instancesInputsMap, instancePropertiesMap);
93         propertiesAndInputsMap = propertiesAndInputsMap.cleanUnnecessaryDataBeforeSending(); // We need to create a copy of the map, without the already selected inputs / properties, and to send the clean map
94         this.componentService.createInputsFromInstancesInputs(this.uniqueId, propertiesAndInputsMap).then(onSuccess, onFailed);
95         return deferred.promise;
96     };
97
98     // 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
99     public getServiceInputInputsAndProperties = (inputId:string):ng.IPromise<InputsAndProperties> => {
100         let deferred = this.$q.defer<InputsAndProperties>();
101         let onSuccess = (inputsAndProperties:InputsAndProperties):void => {
102             let input:InputModel = _.find(this.inputs, (input:InputModel) => {
103                 return input.uniqueId === inputId;
104             });
105             input.inputs = inputsAndProperties.inputs;
106             input.properties = inputsAndProperties.properties;
107             deferred.resolve(inputsAndProperties);
108         };
109         let onFailed = (error:any):void => {
110             deferred.reject(error);
111         };
112         this.componentService.getComponentInputInputsAndProperties(this.uniqueId, inputId).then(onSuccess, onFailed);
113         return deferred.promise;
114     };
115
116     public deleteServiceInput = (inputId:string):ng.IPromise<InputModel> => {
117         let deferred = this.$q.defer<InputModel>();
118
119         let onSuccess = (deletedInput:InputModel):void => {
120             delete _.remove(this.inputs, {uniqueId: deletedInput.uniqueId})[0];
121             deferred.resolve(deletedInput);
122         };
123
124         let onFailed = (error:any):void => {
125             deferred.reject(error);
126         };
127
128         this.componentService.deleteComponentInput(this.uniqueId, inputId).then(onSuccess, onFailed);
129         return deferred.promise;
130     };
131
132     public getArtifactsByType = (artifactGroupType:string):ArtifactGroupModel => {
133         switch (artifactGroupType) {
134             case ArtifactGroupType.DEPLOYMENT:
135                 return this.deploymentArtifacts;
136             case ArtifactGroupType.INFORMATION:
137                 return this.artifacts;
138             case ArtifactGroupType.SERVICE_API:
139                 return this.serviceApiArtifacts;
140         }
141     };
142
143     public updateGroupInstanceProperties = (resourceInstanceId:string, group:DisplayModule, properties:Array<PropertyModel>):ng.IPromise<Array<PropertyModel>> => {
144
145         let deferred = this.$q.defer<Array<PropertyModel>>();
146         let onSuccess = (updatedProperties:Array<PropertyModel>):void => {
147             _.forEach(updatedProperties, (property:PropertyModel) => { // Replace all updated properties on the we needed to update
148                 _.extend(_.find(group.properties, {uniqueId: property.uniqueId}), property);
149             });
150             deferred.resolve(updatedProperties);
151         };
152         let onError = (error:any):void => {
153             deferred.reject(error);
154         };
155
156         this.componentService.updateGroupInstanceProperties(this.uniqueId, resourceInstanceId, group.groupInstanceUniqueId, properties).then(onSuccess, onError);
157         return deferred.promise;
158     };
159
160     getTypeUrl():string {
161         return 'services/';
162     }
163
164
165     public setComponentMetadata(componentMetadata: ComponentMetadata) {
166         super.setComponentMetadata(componentMetadata);
167         this.ecompGeneratedNaming = componentMetadata.ecompGeneratedNaming;
168         this.namingPolicy = componentMetadata.namingPolicy;
169         this.serviceType = componentMetadata.serviceType;
170         this.serviceRole = componentMetadata.serviceRole;
171         this.environmentContext = componentMetadata.environmentContext;
172         this.instantiationType = componentMetadata.instantiationType;
173         this.setComponentDisplayData();
174     }
175
176     setComponentDisplayData():void {
177         this.filterTerm = this.name + ' ' + this.description + ' ' + (this.tags ? this.tags.toString() : '') + ' ' + this.version;
178         if (this.categories && this.categories[0]) {
179             this.mainCategory = this.categories[0].name;
180             this.selectedCategory = this.mainCategory;
181         }
182         this.iconSprite = "sprite-services-icons";
183     }
184
185     public toJSON = ():any => {
186         let temp = angular.copy(this);
187         temp.componentService = undefined;
188         temp.filterTerm = undefined;
189         temp.iconSprite = undefined;
190         temp.mainCategory = undefined;
191         temp.subCategory = undefined;
192         temp.selectedInstance = undefined;
193         temp.showMenu = undefined;
194         temp.$q = undefined;
195         temp.selectedCategory = undefined;
196         temp.modules = undefined;
197         temp.groupInstances = undefined;
198         return temp;
199     };
200 }
201