re base code
[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 {Response, RequestOptions, Headers} from '@angular/http';
23 import { Observable } from 'rxjs/Observable';
24 import {PropertyFEModel, PropertyBEModel} from "app/models";
25 import {CommonUtils} from "app/utils";
26 import {Component, ComponentInstance, Capability, PropertyModel} from "app/models";
27 import { HttpService } from '../http.service';
28 import {SdcConfigToken, ISdcConfig} from "../../config/sdc-config.config";
29
30 @Injectable()
31 export class ComponentInstanceServiceNg2 {
32
33     protected baseUrl;
34
35     constructor(private http: HttpService, @Inject(SdcConfigToken) sdcConfig:ISdcConfig) {
36         this.baseUrl = sdcConfig.api.root + sdcConfig.api.component_api_root;
37     }
38
39     getComponentInstanceProperties(component: Component, componentInstanceId: string): Observable<Array<PropertyBEModel>> {
40
41         return this.http.get(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/componentInstances/' + componentInstanceId + '/properties')
42             .map((res: Response) => {
43                 return CommonUtils.initBeProperties(res.json());
44         })
45     }
46
47     getComponentInstanceInputs(component: Component, componentInstance: ComponentInstance): Observable<Array<PropertyBEModel>> {
48         return this.http.get(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/componentInstances/' + componentInstance.uniqueId + '/' + componentInstance.componentUid + '/inputs')
49             .map((res: Response) => {
50                 return CommonUtils.initInputs(res.json());
51             })
52     }
53
54     updateInstanceProperties(component: Component, componentInstanceId: string, properties: PropertyBEModel[]) {
55
56         return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/resourceInstance/' + componentInstanceId + '/properties', properties)
57             .map((res: Response) => {
58                 return res.json().map((resProperty) => new PropertyBEModel(resProperty));
59             });
60     }
61
62     getInstanceCapabilityProperties(component: Component, componentInstanceId: string, capability: Capability): Observable<Array<PropertyModel>> {
63
64         return this.http.get(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/componentInstances/' + componentInstanceId + '/capability/' + capability.type +
65             '/capabilityName/' +  capability.name + '/ownerId/' + capability.ownerId + '/properties')
66             .map((res: Response) => {
67                 capability.properties = res.json().map((capProp) => new PropertyModel(capProp));  // update capability properties
68                 return capability.properties;
69             })
70     }
71
72     updateInstanceCapabilityProperties(component: Component, componentInstanceId: string, capability: Capability, properties: PropertyBEModel[]): Observable<Array<PropertyModel>> {
73
74         return this.http.put(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/componentInstances/' + componentInstanceId + '/capability/' +  capability.type +
75             '/capabilityName/' +  capability.name + '/ownerId/' + capability.ownerId + '/properties', properties)
76             .map((res: Response) => {
77                 const savedProperties: PropertyModel[] = res.json().map((resProperty) => new PropertyModel(resProperty));
78                 savedProperties.forEach((savedProperty) => {
79                     const propIdx = capability.properties.findIndex((p) => p.uniqueId === savedProperty.uniqueId);
80                     if (propIdx !== -1) {
81                         capability.properties.splice(propIdx, 1, savedProperty);
82                     }
83                 });
84                 return savedProperties;
85             })
86     }
87
88     updateInstanceInputs(component: Component, componentInstanceId: string, inputs: PropertyBEModel[]): Observable<PropertyBEModel[]> {
89
90         return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/resourceInstance/' + componentInstanceId + '/inputs', inputs)
91             .map((res: Response) => {
92                 return res.json().map((resInput) => new PropertyBEModel(resInput));
93             });
94     }
95
96     getComponentGroupInstanceProperties(component: Component, groupInstanceId: string): Observable<Array<PropertyBEModel>> {
97         return this.http.get(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/groups/' + groupInstanceId + '/properties')
98             .map((res: Response) => {
99                 return CommonUtils.initBeProperties(res.json());
100             });
101     }
102
103     updateComponentGroupInstanceProperties(component: Component, groupInstanceId: string, properties: PropertyBEModel[]): Observable<Array<PropertyBEModel>> {
104         return this.http.put(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/groups/' + groupInstanceId + '/properties', properties)
105             .map((res: Response) => {
106                 return res.json().map((resProperty) => new PropertyBEModel(resProperty));
107             });
108     }
109
110     getComponentPolicyInstanceProperties(component: Component, policyInstanceId: string): Observable<Array<PropertyBEModel>> {
111         return this.http.get(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/policies/' + policyInstanceId + '/properties')
112             .map((res: Response) => {
113                 return CommonUtils.initBeProperties(res.json());
114             });
115     }
116
117     updateComponentPolicyInstanceProperties(component: Component, policyInstanceId: string, properties: PropertyBEModel[]): Observable<Array<PropertyBEModel>> {
118         return this.http.put(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/policies/' + policyInstanceId + '/properties', properties)
119             .map((res: Response) => {
120                 return res.json().map((resProperty) => new PropertyBEModel(resProperty));
121             });
122     }
123 }