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