re base code
[sdc.git] / catalog-ui / src / app / ng2 / services / component-services / component.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 * as _ from "lodash";
22 import {Injectable, Inject} from '@angular/core';
23 import {Observable} from 'rxjs/Observable';
24 import 'rxjs/add/operator/map';
25 import 'rxjs/add/operator/toPromise';
26 import {Response, URLSearchParams} from '@angular/http';
27 import { Component, InputBEModel, InstancePropertiesAPIMap, FilterPropertiesAssignmentData, OperationModel, CreateOperationResponse} from "app/models";
28 import {downgradeInjectable} from '@angular/upgrade/static';
29 import {COMPONENT_FIELDS} from "app/utils";
30 import {ComponentGenericResponse} from "../responses/component-generic-response";
31 import {InstanceBePropertiesMap} from "../../../models/properties-inputs/property-fe-map";
32 import {API_QUERY_PARAMS} from "app/utils";
33 import { ComponentType, ServerTypeUrl } from "../../../utils/constants";
34 import { HttpService } from '../http.service';
35 import {SdcConfigToken, ISdcConfig} from "../../config/sdc-config.config";
36 import {IDependenciesServerResponse} from "../responses/dependencies-server-response";
37 import {AutomatedUpgradeGenericResponse} from "../responses/automated-upgrade-response";
38 import {IAutomatedUpgradeRequestObj} from "../../pages/automated-upgrade/automated-upgrade.service";
39
40 declare var angular:angular.IAngularStatic;
41
42 @Injectable()
43 export class ComponentServiceNg2 {
44
45     protected baseUrl;
46
47     constructor(protected http:HttpService, @Inject(SdcConfigToken) sdcConfig:ISdcConfig) {
48         this.baseUrl = sdcConfig.api.root + sdcConfig.api.component_api_root;
49     }
50
51     protected getComponentDataByFieldsName(componentType:string, componentId: string, fields:Array<string>):Observable<ComponentGenericResponse> {
52
53         let params:URLSearchParams = new URLSearchParams();
54         _.forEach(fields, (field:string):void => {
55             params.append(API_QUERY_PARAMS.INCLUDE, field);
56         });
57
58         return this.http.get(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/filteredDataByParams', {search: params})
59             .map((res:Response) => {
60                 return this.analyzeComponentDataResponse(res);
61             });
62     }
63
64     protected analyzeComponentDataResponse(res: Response):ComponentGenericResponse {
65         return new ComponentGenericResponse().deserialize(res.json());
66     }
67
68     private getServerTypeUrl = (componentType:string):string => {
69         switch (componentType) {
70             case ComponentType.SERVICE:
71                 return ServerTypeUrl.SERVICES;
72             default:
73                 return ServerTypeUrl.RESOURCES;
74         }
75     }
76
77     getComponentMetadata(component:Component):Observable<ComponentGenericResponse> {
78         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_METADATA]);
79     }
80
81     getComponentInstanceAttributesAndProperties(component:Component):Observable<ComponentGenericResponse> {
82         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES_PROPERTIES, COMPONENT_FIELDS.COMPONENT_INSTANCES_ATTRIBUTES]);
83     }
84
85     getComponentAttributes(component:Component):Observable<ComponentGenericResponse> {
86         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_ATTRIBUTES]);
87     }
88
89     getComponentCompositionData(component:Component):Observable<ComponentGenericResponse> {
90         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES_RELATION, COMPONENT_FIELDS.COMPONENT_INSTANCES, COMPONENT_FIELDS.COMPONENT_NON_EXCLUDED_POLICIES, COMPONENT_FIELDS.COMPONENT_NON_EXCLUDED_GROUPS]);
91     }
92
93     getComponentResourcePropertiesData(component:Component):Observable<ComponentGenericResponse> {
94         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES, COMPONENT_FIELDS.COMPONENT_POLICIES, COMPONENT_FIELDS.COMPONENT_NON_EXCLUDED_GROUPS]);
95     }
96
97     getComponentResourceInstances(component:Component):Observable<ComponentGenericResponse> {
98         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES]);
99     }
100
101     getComponentInputs(component:Component):Observable<ComponentGenericResponse> {
102         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INPUTS]);
103     }
104
105     getComponentDeploymentArtifacts(component:Component):Observable<ComponentGenericResponse> {
106         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_DEPLOYMENT_ARTIFACTS]);
107     }
108
109     getComponentInformationalArtifacts(component:Component):Observable<ComponentGenericResponse> {
110         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INFORMATIONAL_ARTIFACTS]);
111     }
112
113     getComponentInformationalArtifactsAndInstances(component:Component):Observable<ComponentGenericResponse> {
114         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INFORMATIONAL_ARTIFACTS, COMPONENT_FIELDS.COMPONENT_INSTANCES]);
115     }
116
117     getComponentToscaArtifacts(component:Component):Observable<ComponentGenericResponse> {
118         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_TOSCA_ARTIFACTS]);
119     }
120
121     getComponentProperties(component:Component):Observable<ComponentGenericResponse> {
122         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_PROPERTIES]);
123     }
124
125     getInterfaceOperations(component:Component):Observable<ComponentGenericResponse> {
126         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INTERFACE_OPERATIONS]);
127     }
128
129     getInterfaceOperation(component:Component, operation:OperationModel):Observable<OperationModel> {
130         return this.http.get(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/interfaceOperations/' + operation.uniqueId)
131             .map((res:Response) => {
132                 return res.json();
133             });
134     }
135
136     createInterfaceOperation(component:Component, operation:OperationModel):Observable<CreateOperationResponse> {
137         const operationList = {
138             'interfaceOperations': {
139                 [operation.operationType]: operation
140             }
141         };
142         return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/interfaceOperations', operationList)
143             .map((res:Response) => {
144                 return res.json();
145             });
146     }
147
148     updateInterfaceOperation(component:Component, operation:OperationModel):Observable<CreateOperationResponse> {
149         const operationList = {
150             'interfaceOperations': {
151                 [operation.operationType]: operation
152             }
153         };
154         return this.http.put(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/interfaceOperations', operationList)
155             .map((res:Response) => {
156                 return res.json();
157             });
158     }
159
160     deleteInterfaceOperation(component:Component, operation:OperationModel):Observable<OperationModel> {
161         return this.http.delete(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/interfaceOperations/' + operation.uniqueId)
162             .map((res:Response) => {
163                 return res.json();
164             });
165     }
166
167     getCapabilitiesAndRequirements(componentType: string, componentId:string):Observable<ComponentGenericResponse> {
168         return this.getComponentDataByFieldsName(componentType, componentId, [COMPONENT_FIELDS.COMPONENT_REQUIREMENTS, COMPONENT_FIELDS.COMPONENT_CAPABILITIES]);
169     }
170
171     getDeploymentGraphData(component:Component):Observable<ComponentGenericResponse> {
172         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES_RELATION, COMPONENT_FIELDS.COMPONENT_INSTANCES, COMPONENT_FIELDS.COMPONENT_GROUPS]);
173     }
174
175     createInput(component:Component, inputsToCreate:InstancePropertiesAPIMap):Observable<any> {
176         return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/create/inputs', inputsToCreate)
177             .map(res => {
178                 return res.json();
179             })
180     }
181
182     restoreComponent(componentType:string, componentId:string){ 
183         return this.http.post(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/restore', {})
184     }
185
186     archiveComponent(componentType:string, componentId:string){
187         return this.http.post(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/archive', {})
188     }
189
190
191     deleteInput(component:Component, input:InputBEModel):Observable<InputBEModel> {
192
193         return this.http.delete(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/delete/' + input.uniqueId + '/input')
194             .map((res:Response) => {
195                 return new InputBEModel(res.json());
196             })
197     }
198
199     updateComponentInputs(component:Component, inputs:InputBEModel[]):Observable<InputBEModel[]> {
200
201         return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/update/inputs', inputs)
202             .map((res:Response) => {
203                 return res.json().map((input) => new InputBEModel(input));
204             })
205     }
206
207     filterComponentInstanceProperties(component: Component, filterData:FilterPropertiesAssignmentData): Observable<InstanceBePropertiesMap> {//instance-property-be-map
208         let params: URLSearchParams = new URLSearchParams();
209         _.forEach(filterData.selectedTypes, (type:string) => {
210             params.append('resourceType', type);
211         });
212
213         return this.http.get(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/filteredproperties/' + filterData.propertyName, {search: params})
214             .map((res: Response) => {
215                 return res.json();
216             });
217     }
218
219     getDependencies(componentType:string, componentId: string):Observable<Array<IDependenciesServerResponse>> {
220         return this.http.get(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/dependencies')
221             .map((res:Response) => {
222                 return res.json();
223             });
224     }
225
226     automatedUpgrade(componentType:string, componentId: string, componentsIdsToUpgrade:Array<IAutomatedUpgradeRequestObj>):Observable<AutomatedUpgradeGenericResponse> {
227         return this.http.post(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/automatedupgrade', componentsIdsToUpgrade)
228             .map((res:Response) => {
229                 return res.json();
230             });
231     }
232 }
233