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