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