1e4ddda9c0e5b7334cfff70cdf229c5ebb386a0d
[sdc.git] / catalog-ui / src / app / ng2 / services / component-instance-services / component-instance.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 import {Inject, Injectable} from '@angular/core';
22 import {Observable} from 'rxjs/Observable';
23 import {
24     ArtifactGroupModel,
25     ArtifactModel,
26     AttributeModel,
27     Capability,
28     Component,
29     ComponentInstance,
30     IFileDownload,
31     PropertyBEModel,
32     PropertyModel,
33     Requirement
34 } from "app/models";
35 import {CommonUtils, ComponentInstanceFactory, ComponentType, ServerTypeUrl} from "app/utils";
36 import {ISdcConfig, SdcConfigToken} from "../../config/sdc-config.config";
37 import {HttpClient, HttpHeaders} from '@angular/common/http';
38 import {InputBEModel} from '../../../models/properties-inputs/input-be-model';
39 import {HttpHelperService} from '../http-hepler.service';
40 import {AttributeBEModel} from "../../../models/attributes-outputs/attribute-be-model";
41 import {OutputBEModel} from "../../../models/attributes-outputs/output-be-model";
42
43 @Injectable()
44 export class ComponentInstanceServiceNg2 {
45
46     protected baseUrl;
47
48     constructor(private http: HttpClient, @Inject(SdcConfigToken) sdcConfig:ISdcConfig) {
49         this.baseUrl = sdcConfig.api.root + sdcConfig.api.component_api_root;
50     }
51
52     private getServerTypeUrl = (componentType:string):string => {
53         switch (componentType) {
54             case ComponentType.SERVICE:
55                 return ServerTypeUrl.SERVICES;
56             default:
57                 return ServerTypeUrl.RESOURCES;
58         }
59     }
60     getComponentInstanceProperties(component: Component, componentInstanceId: string): Observable<Array<PropertyBEModel>> {
61         return this.http.get<Array<PropertyBEModel>>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/componentInstances/' + componentInstanceId + '/properties')
62             .map(res => {
63                 return CommonUtils.initBeProperties(res);
64         })
65     }
66
67     getComponentInstanceAttributes(component: Component, componentInstanceId: string): Observable<Array<AttributeBEModel>> {
68         return this.http.get<Array<AttributeBEModel>>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/componentInstances/' + componentInstanceId + '/attributes')
69             .map(res => {
70                 return CommonUtils.initBeAttributes(res);
71         })
72     }
73
74     getComponentInstanceInputs(component: Component, componentInstance: ComponentInstance): Observable<Array<PropertyBEModel>> {
75         return this.http.get<Array<InputBEModel>>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/componentInstances/' + componentInstance.uniqueId + '/' + componentInstance.componentUid + '/inputs')
76             .map(res => {
77                 return CommonUtils.initInputs(res);
78             })
79     }
80
81     getComponentInstanceOutputs(component: Component, componentInstance: ComponentInstance): Observable<Array<AttributeBEModel>> {
82         return this.http.get<Array<OutputBEModel>>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/componentInstances/' + componentInstance.uniqueId + '/' + componentInstance.componentUid + '/outputs')
83             .map(res => {
84                 return CommonUtils.initOutputs(res);
85             })
86     }
87
88     getComponentInstanceArtifactsByGroupType = (componentType:string, componentId:string, componentInstanceId:string, artifactGroupType:string):Observable<ArtifactGroupModel> => {
89
90         return this.http.get<ArtifactGroupModel>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + "/resourceInstances/" + componentInstanceId + "/artifactsByType/" + artifactGroupType)
91             .map(res => {
92                 return new ArtifactGroupModel(res);
93             });
94     };
95
96     getArtifactByGroupType = (componentType:string, componentId:string, artifactGroupType:string):Observable<ArtifactGroupModel> => {
97
98         return this.http.get<ArtifactGroupModel>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + "/artifactsByType/" + artifactGroupType)
99             .map(response => new ArtifactGroupModel(response));
100     };
101
102     changeResourceInstanceVersion = (componentType:string, componentId:string, componentInstanceId:string, componentUid:string):Observable<ComponentInstance> => {
103         return this.http.post<ComponentInstance>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/resourceInstance/' + componentInstanceId + '/changeVersion', {'componentUid': componentUid})
104         .map((res) => {
105             return ComponentInstanceFactory.createComponentInstance(res);
106         })
107     };
108
109     updateComponentInstance = (componentType:string, componentId:string, componentInstance:ComponentInstance):Observable<ComponentInstance> => {
110         return this.http.post<ComponentInstance>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/resourceInstance/' + componentInstance.uniqueId, componentInstance.toJSON())
111             .map((response) => {
112                 return ComponentInstanceFactory.createComponentInstance(response);
113             });
114     };
115
116     updateInstanceProperties(componentType:string, componentId:string, componentInstanceId: string, properties: PropertyBEModel[]) {
117
118         return this.http.post<Array<PropertyModel>>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/resourceInstance/' + componentInstanceId + '/properties', properties)
119             .map((res) => {
120                 return res.map((resProperty) => {
121                     let newProp = new PropertyModel(resProperty);
122                     newProp.resourceInstanceUniqueId = componentInstanceId;
123                     return newProp;
124                 });
125             });
126     }
127
128     updateInstanceAttributes(componentType:string, componentId:string, componentInstanceId: string, attributes: AttributeBEModel[]) {
129
130         return this.http.post<Array<AttributeModel>>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/resourceInstance/' + componentInstanceId + '/attributes', attributes)
131             .map((res) => {
132                 return res.map((resAttribute) => {
133                     let newAttrib = new AttributeModel(resAttribute);
134                     newAttrib.resourceInstanceUniqueId = componentInstanceId;
135                     return newAttrib;
136                 });
137             });
138     }
139
140     getInstanceCapabilityProperties(componentType: string, componentId: string, componentInstanceId: string, capability: Capability): Observable<Array<PropertyModel>> {
141
142         return this.http.get<Array<PropertyModel>>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/componentInstances/' + componentInstanceId + '/capability/' + capability.type +
143             '/capabilityName/' +  capability.name + '/ownerId/' + capability.ownerId + '/properties')
144             .map((res) => {
145                 capability.properties = res.map((capProp) => new PropertyModel(capProp));  // update capability properties
146                 return capability.properties;
147             })
148     }
149
150     updateInstanceCapabilityProperties(component: Component, componentInstanceId: string, capability: Capability, properties: PropertyBEModel[]): Observable<Array<PropertyModel>> {
151
152         return this.http.put<Array<PropertyModel>>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/componentInstances/' + componentInstanceId + '/capability/' +  capability.type +
153             '/capabilityName/' +  capability.name + '/ownerId/' + capability.ownerId + '/properties', properties)
154             .map((res) => {
155                 const savedProperties: PropertyModel[] = res.map((resProperty) => new PropertyModel(resProperty));
156                 savedProperties.forEach((savedProperty) => {
157                     const propIdx = capability.properties.findIndex((p) => p.uniqueId === savedProperty.uniqueId);
158                     if (propIdx !== -1) {
159                         capability.properties.splice(propIdx, 1, savedProperty);
160                     }
161                 });
162                 return savedProperties;
163             })
164     }
165
166     updateInstanceRequirement(componentType: string, componentId: string, componentInstanceId: string, requirement: Requirement): Observable<Requirement> {
167         return this.http.put<Requirement>(this.baseUrl + componentType + componentId + '/componentInstances/' + componentInstanceId + '/requirement/' +  requirement.capability +
168             '/requirementName/' +  requirement.name, requirement);
169     }
170
171     updateInstanceCapability(componentTypeUrl: string, componentId: string, componentInstanceId: string, capability: Capability): Observable<Capability> {
172         const url = `${this.baseUrl}${componentTypeUrl}${componentId}/componentInstances/${componentInstanceId}/capability/`;
173         return this.http.put<Capability>(url, capability);
174     }
175
176     updateInstanceInputs(component: Component, componentInstanceId: string, inputs: PropertyBEModel[]): Observable<PropertyBEModel[]> {
177
178         return this.http.post<Array<PropertyModel>>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/resourceInstance/' + componentInstanceId + '/inputs', inputs)
179             .map((res) => {
180                 return res.map((resInput) => new PropertyBEModel(resInput));
181             });
182     }
183
184     updateInstanceOutputs(component: Component, componentInstanceId: string, outputs: AttributeBEModel[]): Observable<AttributeBEModel[]> {
185
186         return this.http.post<Array<AttributeModel>>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/resourceInstance/' + componentInstanceId + '/outputs', outputs)
187             .map((res) => {
188                 return res.map((resOutput) => new AttributeBEModel(resOutput));
189             });
190     }
191
192     getComponentGroupInstanceProperties(component: Component, groupInstanceId: string): Observable<Array<PropertyBEModel>> {
193         return this.http.get<Array<PropertyBEModel>>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/groups/' + groupInstanceId + '/properties')
194             .map((res) => {
195                 return CommonUtils.initBeProperties(res);
196             });
197     }
198
199     updateComponentGroupInstanceProperties(componentType:string, componentId:string, groupInstanceId: string, properties: PropertyBEModel[]): Observable<Array<PropertyBEModel>> {
200         return this.http.put<Array<PropertyBEModel>>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/groups/' + groupInstanceId + '/properties', properties)
201             .map((res) => {
202                 return res.map((resProperty) => new PropertyBEModel(resProperty));
203             });
204     }
205
206     getComponentPolicyInstanceProperties(componentType:string, componentId:string, policyInstanceId: string): Observable<Array<PropertyBEModel>> {
207         return this.http.get<Array<PropertyBEModel>>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/policies/' + policyInstanceId + '/properties')
208             .map((res) => {
209                 return CommonUtils.initBeProperties(res);
210             });
211     }
212
213     updateComponentPolicyInstanceProperties(componentType:string, componentId:string, policyInstanceId: string, properties: PropertyBEModel[]): Observable<Array<PropertyBEModel>> {
214         return this.http.put<Array<PropertyBEModel>>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/policies/' + policyInstanceId + '/properties', properties)
215             .map((res) => {
216                 return res.map((resProperty) => new PropertyBEModel(resProperty));
217             });
218     }
219
220     addInstanceArtifact = (componentType:string, componentId:string, instanceId:string, artifact:ArtifactModel):Observable<ArtifactModel> => {
221         let headerObj = new HttpHeaders();
222         if (artifact.payloadData) {
223             headerObj = headerObj.set('Content-MD5', HttpHelperService.getHeaderMd5(artifact));
224         }
225         return this.http.post<ArtifactModel>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/resourceInstance/' + instanceId + '/artifacts', JSON.stringify(artifact), {headers: headerObj})
226         .map((res) => {
227             return new ArtifactModel(res);
228         });
229     };
230
231     updateInstanceArtifact = (componentType:string, componentId:string, instanceId:string, artifact:ArtifactModel):Observable<ArtifactModel> => {
232         return this.http.post<ArtifactModel>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/resourceInstance/' + instanceId + '/artifacts/' + artifact.uniqueId, artifact).map((res) => {
233             return new ArtifactModel(res);
234         });
235     };
236
237     deleteInstanceArtifact = (componentId:string, componentType:string, instanceId:string, artifactId:string, artifactLabel:string):Observable<ArtifactModel> => {
238         return this.http.delete<ArtifactModel>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + "/resourceInstance/" + instanceId + '/artifacts/' + artifactId + '?operation=' + artifactLabel)
239             .map((res) => {
240                 return new ArtifactModel(res);
241             });
242     }
243
244     downloadInstanceArtifact = (componentType:string, componentId:string, instanceId:string, artifactId:string):Observable<IFileDownload> => {
245         return this.http.get<IFileDownload>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + "/resourceInstances/" + instanceId + "/artifacts/" + artifactId);
246     };
247
248     uploadInstanceEnvFile = (componentType:string, componentId:string, instanceId:string, artifact:ArtifactModel):Observable<ArtifactModel> => {
249         let headerObj = new HttpHeaders();
250         if (artifact.payloadData) {
251             headerObj = headerObj.set('Content-MD5', HttpHelperService.getHeaderMd5(artifact));
252         }
253         return this.http.post<ArtifactModel>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/resourceInstance/' + instanceId + '/artifacts/' + artifact.uniqueId, JSON.stringify(artifact), {headers: headerObj});
254     };
255
256
257     updateInstanceAttribute = (componentType:string, componentId:string, attribute:AttributeModel):Observable<AttributeModel> => {
258         let instanceId = attribute.resourceInstanceUniqueId;
259         return this.http.post<AttributeModel>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + "/resourceInstance/" + instanceId + "/attribute", attribute)
260             .map((response) => {
261                 let newAttribute = new AttributeModel(response);
262                 newAttribute.readonly = true;
263                 newAttribute.resourceInstanceUniqueId = instanceId;
264                 return newAttribute;
265             });
266     };
267
268 }