cc382a3df03f55ccec0b452d764d04b15443a3d5
[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} 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
31 @Injectable()
32 export class ComponentInstanceServiceNg2 {
33
34     protected baseUrl;
35
36     constructor(private http: HttpClient, @Inject(SdcConfigToken) sdcConfig:ISdcConfig) {
37         this.baseUrl = sdcConfig.api.root + sdcConfig.api.component_api_root;
38     }
39
40     private getServerTypeUrl = (componentType:string):string => {
41         switch (componentType) {
42             case ComponentType.SERVICE:
43                 return ServerTypeUrl.SERVICES;
44             default:
45                 return ServerTypeUrl.RESOURCES;
46         }
47     }
48     getComponentInstanceProperties(component: Component, componentInstanceId: string): Observable<Array<PropertyBEModel>> {
49         return this.http.get<Array<PropertyBEModel>>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/componentInstances/' + componentInstanceId + '/properties')
50             .map(res => {
51                 return CommonUtils.initBeProperties(res);
52         })
53     }
54
55     getComponentInstanceInputs(component: Component, componentInstance: ComponentInstance): Observable<Array<PropertyBEModel>> {
56         return this.http.get<Array<InputBEModel>>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/componentInstances/' + componentInstance.uniqueId + '/' + componentInstance.componentUid + '/inputs')
57             .map(res => {
58                 return CommonUtils.initInputs(res);
59             })
60     }
61
62     getComponentInstanceArtifactsByGroupType = (componentType:string, componentId:string, componentInstanceId:string, artifactGroupType:string):Observable<ArtifactGroupModel> => {
63
64         return this.http.get<ArtifactGroupModel>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + "/resourceInstances/" + componentInstanceId + "/artifactsByType/" + artifactGroupType)
65             .map(res => {
66                 return new ArtifactGroupModel(res);
67             });
68     };
69
70     getArtifactByGroupType = (componentType:string, componentId:string, artifactGroupType:string):Observable<ArtifactGroupModel> => {
71         
72         return this.http.get<ArtifactGroupModel>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + "/artifactsByType/" + artifactGroupType)
73             .map(response => new ArtifactGroupModel(response));
74     };
75
76     changeResourceInstanceVersion = (componentType:string, componentId:string, componentInstanceId:string, componentUid:string):Observable<ComponentInstance> => {
77         return this.http.post<ComponentInstance>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/resourceInstance/' + componentInstanceId + '/changeVersion', {'componentUid': componentUid})
78         .map((res) => {
79             return ComponentInstanceFactory.createComponentInstance(res);
80         })
81     };
82
83     updateComponentInstance = (componentType:string, componentId:string, componentInstance:ComponentInstance):Observable<ComponentInstance> => {
84         return this.http.post<ComponentInstance>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/resourceInstance/' + componentInstance.uniqueId, componentInstance.toJSON())
85             .map((response) => {
86                 return ComponentInstanceFactory.createComponentInstance(response);
87             });
88     };
89
90     updateInstanceProperties(componentType:string, componentId:string, componentInstanceId: string, properties: PropertyBEModel[]) {
91
92         return this.http.post<Array<PropertyModel>>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/resourceInstance/' + componentInstanceId + '/properties', properties)
93             .map((res) => {
94                 return res.map((resProperty) => {
95                     let newProp = new PropertyModel(resProperty);
96                     newProp.resourceInstanceUniqueId = componentInstanceId
97                     return newProp;
98                 });
99             });
100     }
101
102     getInstanceCapabilityProperties(componentType: string, componentId: string, componentInstanceId: string, capability: Capability): Observable<Array<PropertyModel>> {
103
104         return this.http.get<Array<PropertyModel>>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/componentInstances/' + componentInstanceId + '/capability/' + capability.type +
105             '/capabilityName/' +  capability.name + '/ownerId/' + capability.ownerId + '/properties')
106             .map((res) => {
107                 capability.properties = res.map((capProp) => new PropertyModel(capProp));  // update capability properties
108                 return capability.properties;
109             })
110     }
111
112     updateInstanceCapabilityProperties(component: Component, componentInstanceId: string, capability: Capability, properties: PropertyBEModel[]): Observable<Array<PropertyModel>> {
113
114         return this.http.put<Array<PropertyModel>>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/componentInstances/' + componentInstanceId + '/capability/' +  capability.type +
115             '/capabilityName/' +  capability.name + '/ownerId/' + capability.ownerId + '/properties', properties)
116             .map((res) => {
117                 const savedProperties: PropertyModel[] = res.map((resProperty) => new PropertyModel(resProperty));
118                 savedProperties.forEach((savedProperty) => {
119                     const propIdx = capability.properties.findIndex((p) => p.uniqueId === savedProperty.uniqueId);
120                     if (propIdx !== -1) {
121                         capability.properties.splice(propIdx, 1, savedProperty);
122                     }
123                 });
124                 return savedProperties;
125             })
126     }
127
128     updateInstanceInputs(component: Component, componentInstanceId: string, inputs: PropertyBEModel[]): Observable<PropertyBEModel[]> {
129
130         return this.http.post<Array<PropertyModel>>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/resourceInstance/' + componentInstanceId + '/inputs', inputs)
131             .map((res) => {
132                 return res.map((resInput) => new PropertyBEModel(resInput));
133             });
134     }
135
136     getComponentGroupInstanceProperties(component: Component, groupInstanceId: string): Observable<Array<PropertyBEModel>> {
137         return this.http.get<Array<PropertyBEModel>>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/groups/' + groupInstanceId + '/properties')
138             .map((res) => {
139                 return CommonUtils.initBeProperties(res);
140             });
141     }
142
143     updateComponentGroupInstanceProperties(componentType:string, componentId:string, groupInstanceId: string, properties: PropertyBEModel[]): Observable<Array<PropertyBEModel>> {
144         return this.http.put<Array<PropertyBEModel>>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/groups/' + groupInstanceId + '/properties', properties)
145             .map((res) => {
146                 return res.map((resProperty) => new PropertyBEModel(resProperty));
147             });
148     }
149
150     getComponentPolicyInstanceProperties(componentType:string, componentId:string, policyInstanceId: string): Observable<Array<PropertyBEModel>> {
151         return this.http.get<Array<PropertyBEModel>>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/policies/' + policyInstanceId + '/properties')
152             .map((res) => {
153                 return CommonUtils.initBeProperties(res);
154             });
155     }
156
157     updateComponentPolicyInstanceProperties(componentType:string, componentId:string, policyInstanceId: string, properties: PropertyBEModel[]): Observable<Array<PropertyBEModel>> {
158         return this.http.put<Array<PropertyBEModel>>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/policies/' + policyInstanceId + '/properties', properties)
159             .map((res) => {
160                 return res.map((resProperty) => new PropertyBEModel(resProperty));
161             });
162     }
163
164     addInstanceArtifact = (componentType:string, componentId:string, instanceId:string, artifact:ArtifactModel):Observable<ArtifactModel> => {
165         // let deferred = this.$q.defer<ArtifactModel>();
166         let headerObj = new HttpHeaders();
167         if (artifact.payloadData) {
168             headerObj = headerObj.set('Content-MD5', HttpHelperService.getHeaderMd5(artifact));
169         }
170         return this.http.post<ArtifactModel>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/resourceInstance/' + instanceId + '/artifacts', JSON.stringify(artifact), {headers: headerObj})
171         .map((res) => {
172             return new ArtifactModel(res);
173         });
174     };
175
176     updateInstanceArtifact = (componentType:string, componentId:string, instanceId:string, artifact:ArtifactModel):Observable<ArtifactModel> => {
177         return this.http.post<ArtifactModel>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/resourceInstance/' + instanceId + '/artifacts/' + artifact.uniqueId, artifact).map((res) => {
178             return new ArtifactModel(res);
179         });;
180     };
181
182     deleteInstanceArtifact = (componentId:string, componentType:string, instanceId:string, artifactId:string, artifactLabel:string):Observable<ArtifactModel> => {
183         return this.http.delete<ArtifactModel>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + "/resourceInstance/" + instanceId + '/artifacts/' + artifactId + '?operation=' + artifactLabel)
184             .map((res) => {
185                 return new ArtifactModel(res);
186             });
187     }
188
189     downloadInstanceArtifact = (componentType:string, componentId:string, instanceId:string, artifactId:string):Observable<IFileDownload> => {
190         return this.http.get<IFileDownload>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + "/resourceInstances/" + instanceId + "/artifacts/" + artifactId);
191     };
192
193     uploadInstanceEnvFile = (componentType:string, componentId:string, instanceId:string, artifact:ArtifactModel):Observable<ArtifactModel> => {
194         let headerObj = new HttpHeaders();
195         if (artifact.payloadData) {
196             headerObj = headerObj.set('Content-MD5', HttpHelperService.getHeaderMd5(artifact));
197         }
198         return this.http.post<ArtifactModel>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/resourceInstance/' + instanceId + '/artifacts/' + artifact.uniqueId, JSON.stringify(artifact), {headers: headerObj});
199     };
200
201
202     updateInstanceAttribute = (componentType:string, componentId:string, attribute:AttributeModel):Observable<AttributeModel> => {
203         let instanceId = attribute.resourceInstanceUniqueId;
204         return this.http.post<AttributeModel>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + "/resourceInstance/" + instanceId + "/attribute", attribute)
205             .map((response) => {
206                 let newAttribute = new AttributeModel(response);
207                 newAttribute.readonly = true;
208                 newAttribute.resourceInstanceUniqueId = instanceId;
209                 return newAttribute;
210             });
211     };
212
213 }