No properties found when trying to add a node filter to a VF
[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.getComponentInstanceInputsByIdAndType(component.uniqueId, component.componentType, componentInstance);
76     }
77
78
79     getComponentInstanceInputsByIdAndType(componentId: string, componentType:string, componentInstance: ComponentInstance): Observable<Array<PropertyBEModel>> {
80         return this.http.get<Array<InputBEModel>>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/componentInstances/' + componentInstance.uniqueId + '/' + componentInstance.componentUid + '/inputs')
81             .map(res => {
82                 return CommonUtils.initInputs(res);
83             })
84     }
85
86     getComponentInstanceOutputs(component: Component, componentInstance: ComponentInstance): Observable<Array<AttributeBEModel>> {
87         return this.http.get<Array<OutputBEModel>>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/componentInstances/' + componentInstance.uniqueId + '/' + componentInstance.componentUid + '/outputs')
88             .map(res => {
89                 return CommonUtils.initOutputs(res);
90             })
91     }
92
93     getComponentInstanceArtifactsByGroupType = (componentType:string, componentId:string, componentInstanceId:string, artifactGroupType:string):Observable<ArtifactGroupModel> => {
94
95         return this.http.get<ArtifactGroupModel>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + "/resourceInstances/" + componentInstanceId + "/artifactsByType/" + artifactGroupType)
96             .map(res => {
97                 return new ArtifactGroupModel(res);
98             });
99     };
100
101     getArtifactByGroupType = (componentType:string, componentId:string, artifactGroupType:string):Observable<ArtifactGroupModel> => {
102
103         return this.http.get<ArtifactGroupModel>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + "/artifactsByType/" + artifactGroupType)
104             .map(response => new ArtifactGroupModel(response));
105     };
106
107     changeResourceInstanceVersion = (componentType:string, componentId:string, componentInstanceId:string, componentUid:string):Observable<ComponentInstance> => {
108         return this.http.post<ComponentInstance>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/resourceInstance/' + componentInstanceId + '/changeVersion', {'componentUid': componentUid})
109         .map((res) => {
110             return ComponentInstanceFactory.createComponentInstance(res);
111         })
112     };
113
114     updateComponentInstance = (componentType:string, componentId:string, componentInstance:ComponentInstance):Observable<ComponentInstance> => {
115         return this.http.post<ComponentInstance>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/resourceInstance/' + componentInstance.uniqueId, componentInstance.toJSON())
116             .map((response) => {
117                 return ComponentInstanceFactory.createComponentInstance(response);
118             });
119     };
120
121     updateInstanceProperties(componentType:string, componentId:string, componentInstanceId: string, properties: PropertyBEModel[]) {
122
123         return this.http.post<Array<PropertyModel>>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/resourceInstance/' + componentInstanceId + '/properties', properties)
124             .map((res) => {
125                 return res.map((resProperty) => {
126                     let newProp = new PropertyModel(resProperty);
127                     newProp.resourceInstanceUniqueId = componentInstanceId;
128                     return newProp;
129                 });
130             });
131     }
132
133     updateInstanceAttributes(componentType:string, componentId:string, componentInstanceId: string, attributes: AttributeBEModel[]) {
134
135         return this.http.post<Array<AttributeModel>>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/resourceInstance/' + componentInstanceId + '/attributes', attributes)
136             .map((res) => {
137                 return res.map((resAttribute) => {
138                     let newAttrib = new AttributeModel(resAttribute);
139                     newAttrib.resourceInstanceUniqueId = componentInstanceId;
140                     return newAttrib;
141                 });
142             });
143     }
144
145     getInstanceCapabilityProperties(componentType: string, componentId: string, componentInstanceId: string, capability: Capability): Observable<Array<PropertyModel>> {
146
147         return this.http.get<Array<PropertyModel>>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/componentInstances/' + componentInstanceId + '/capability/' + capability.type +
148             '/capabilityName/' +  capability.name + '/ownerId/' + capability.ownerId + '/properties')
149             .map((res) => {
150                 capability.properties = res.map((capProp) => new PropertyModel(capProp));  // update capability properties
151                 return capability.properties;
152             })
153     }
154
155     updateInstanceCapabilityProperties(component: Component, componentInstanceId: string, capability: Capability, properties: PropertyBEModel[]): Observable<Array<PropertyModel>> {
156
157         return this.http.put<Array<PropertyModel>>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/componentInstances/' + componentInstanceId + '/capability/' +  capability.type +
158             '/capabilityName/' +  capability.name + '/ownerId/' + capability.ownerId + '/properties', properties)
159             .map((res) => {
160                 const savedProperties: PropertyModel[] = res.map((resProperty) => new PropertyModel(resProperty));
161                 savedProperties.forEach((savedProperty) => {
162                     const propIdx = capability.properties.findIndex((p) => p.uniqueId === savedProperty.uniqueId);
163                     if (propIdx !== -1) {
164                         capability.properties.splice(propIdx, 1, savedProperty);
165                     }
166                 });
167                 return savedProperties;
168             })
169     }
170
171     updateInstanceRequirement(componentType: string, componentId: string, componentInstanceId: string, requirement: Requirement): Observable<Requirement> {
172         return this.http.put<Requirement>(this.baseUrl + componentType + componentId + '/componentInstances/' + componentInstanceId + '/requirement/' +  requirement.capability +
173             '/requirementName/' +  requirement.name, requirement);
174     }
175
176     updateInstanceCapability(componentTypeUrl: string, componentId: string, componentInstanceId: string, capability: Capability): Observable<Capability> {
177         const url = `${this.baseUrl}${componentTypeUrl}${componentId}/componentInstances/${componentInstanceId}/capability/`;
178         return this.http.put<Capability>(url, capability);
179     }
180
181     updateInstanceInputs(component: Component, componentInstanceId: string, inputs: PropertyBEModel[]): Observable<PropertyBEModel[]> {
182
183         return this.http.post<Array<PropertyModel>>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/resourceInstance/' + componentInstanceId + '/inputs', inputs)
184             .map((res) => {
185                 return res.map((resInput) => new PropertyBEModel(resInput));
186             });
187     }
188
189     updateInstanceOutputs(component: Component, componentInstanceId: string, outputs: AttributeBEModel[]): Observable<AttributeBEModel[]> {
190
191         return this.http.post<Array<AttributeModel>>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/resourceInstance/' + componentInstanceId + '/outputs', outputs)
192             .map((res) => {
193                 return res.map((resOutput) => new AttributeBEModel(resOutput));
194             });
195     }
196
197     getComponentGroupInstanceProperties(component: Component, groupInstanceId: string): Observable<Array<PropertyBEModel>> {
198         return this.http.get<Array<PropertyBEModel>>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/groups/' + groupInstanceId + '/properties')
199             .map((res) => {
200                 return CommonUtils.initBeProperties(res);
201             });
202     }
203
204     updateComponentGroupInstanceProperties(componentType:string, componentId:string, groupInstanceId: string, properties: PropertyBEModel[]): Observable<Array<PropertyBEModel>> {
205         return this.http.put<Array<PropertyBEModel>>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/groups/' + groupInstanceId + '/properties', properties)
206             .map((res) => {
207                 return res.map((resProperty) => new PropertyBEModel(resProperty));
208             });
209     }
210
211     getComponentPolicyInstanceProperties(componentType:string, componentId:string, policyInstanceId: string): Observable<Array<PropertyBEModel>> {
212         return this.http.get<Array<PropertyBEModel>>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/policies/' + policyInstanceId + '/properties')
213             .map((res) => {
214                 return CommonUtils.initBeProperties(res);
215             });
216     }
217
218     updateComponentPolicyInstanceProperties(componentType:string, componentId:string, policyInstanceId: string, properties: PropertyBEModel[]): Observable<Array<PropertyBEModel>> {
219         return this.http.put<Array<PropertyBEModel>>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/policies/' + policyInstanceId + '/properties', properties)
220             .map((res) => {
221                 return res.map((resProperty) => new PropertyBEModel(resProperty));
222             });
223     }
224
225     addInstanceArtifact = (componentType:string, componentId:string, instanceId:string, artifact:ArtifactModel):Observable<ArtifactModel> => {
226         let headerObj = new HttpHeaders();
227         if (artifact.payloadData) {
228             headerObj = headerObj.set('Content-MD5', HttpHelperService.getHeaderMd5(artifact));
229         }
230         return this.http.post<ArtifactModel>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/resourceInstance/' + instanceId + '/artifacts', JSON.stringify(artifact), {headers: headerObj})
231         .map((res) => {
232             return new ArtifactModel(res);
233         });
234     };
235
236     updateInstanceArtifact = (componentType:string, componentId:string, instanceId:string, artifact:ArtifactModel):Observable<ArtifactModel> => {
237         return this.http.post<ArtifactModel>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/resourceInstance/' + instanceId + '/artifacts/' + artifact.uniqueId, artifact).map((res) => {
238             return new ArtifactModel(res);
239         });
240     };
241
242     deleteInstanceArtifact = (componentId:string, componentType:string, instanceId:string, artifactId:string, artifactLabel:string):Observable<ArtifactModel> => {
243         return this.http.delete<ArtifactModel>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + "/resourceInstance/" + instanceId + '/artifacts/' + artifactId + '?operation=' + artifactLabel)
244             .map((res) => {
245                 return new ArtifactModel(res);
246             });
247     }
248
249     downloadInstanceArtifact = (componentType:string, componentId:string, instanceId:string, artifactId:string):Observable<IFileDownload> => {
250         return this.http.get<IFileDownload>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + "/resourceInstances/" + instanceId + "/artifacts/" + artifactId);
251     };
252
253     uploadInstanceEnvFile = (componentType:string, componentId:string, instanceId:string, artifact:ArtifactModel):Observable<ArtifactModel> => {
254         let headerObj = new HttpHeaders();
255         if (artifact.payloadData) {
256             headerObj = headerObj.set('Content-MD5', HttpHelperService.getHeaderMd5(artifact));
257         }
258         return this.http.post<ArtifactModel>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/resourceInstance/' + instanceId + '/artifacts/' + artifact.uniqueId, JSON.stringify(artifact), {headers: headerObj});
259     };
260
261
262     updateInstanceAttribute = (componentType:string, componentId:string, attribute:AttributeModel):Observable<AttributeModel> => {
263         let instanceId = attribute.resourceInstanceUniqueId;
264         return this.http.post<AttributeModel>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + "/resourceInstance/" + instanceId + "/attribute", attribute)
265             .map((response) => {
266                 let newAttribute = new AttributeModel(response);
267                 newAttribute.readonly = true;
268                 newAttribute.resourceInstanceUniqueId = instanceId;
269                 return newAttribute;
270             });
271     };
272
273 }