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