Add property mapping feature to ONAP
[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, PropertyBEModel, OperationModel, BEOperationModel, CreateOperationResponse} from "app/models";
28 import {downgradeInjectable} from '@angular/upgrade/static';
29 import {COMPONENT_FIELDS, CommonUtils} 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     getInterfaces(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 + '/interfaces/' + operation.interfaceId + '/operations/' + 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             'interfaces': {
139                 [operation.interfaceType]: {
140                     'type': operation.interfaceType,
141                     'operations': {
142                         [operation.name]: new BEOperationModel(operation)
143                     }
144                 }
145             }
146         };
147         return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/interfaceOperations', operationList)
148             .map((res:Response) => {
149                 const interf = _.find(res.json().interfaces, (interf: any) => interf.type === operation.interfaceType);
150                 const newOperation = _.find(interf.operations, (op:OperationModel) => op.name === operation.name);
151                 return new CreateOperationResponse({
152                     ...newOperation,
153                     interfaceType: interf.type,
154                     interfaceId: interf.uniqueId
155                 });
156             });
157     }
158
159     updateInterfaceOperation(component:Component, operation:OperationModel):Observable<CreateOperationResponse> {
160         const operationList = {
161             'interfaces': {
162                 [operation.interfaceType]: {
163                     'type': operation.interfaceType,
164                     'operations': {
165                         [operation.name]: new BEOperationModel(operation)
166                     }
167                 }
168             }
169         };
170         return this.http.put(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/interfaceOperations', operationList)
171             .map((res:Response) => {
172                 const interf = _.find(res.json().interfaces, (interf: any) => interf.type === operation.interfaceType);
173                 const newOperation = _.find(interf.operations, (op:OperationModel) => op.name === operation.name);
174                 return new CreateOperationResponse({
175                     ...newOperation,
176                     interfaceType: interf.type,
177                     interfaceId: interf.uniqueId
178                 });
179             });
180     }
181
182     deleteInterfaceOperation(component:Component, operation:OperationModel):Observable<OperationModel> {
183         return this.http.delete(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/interfaces/' + operation.interfaceId + '/operations/' + operation.uniqueId)
184             .map((res:Response) => {
185                 return res.json();
186             });
187     }
188
189     getInterfaceTypes(component:Component):Observable<{[id:string]: Array<string>}> {
190         return this.http.get(this.baseUrl + 'interfaceLifecycleTypes')
191             .map((res:Response) => {
192                 const interfaceMap = {};
193                 _.forEach(res.json(), (interf:any) => {
194                     interfaceMap[interf.toscaPresentation.type] = _.keys(interf.toscaPresentation.operations);
195                 });
196                 return interfaceMap;
197             });
198     }
199
200     getCapabilitiesAndRequirements(componentType: string, componentId:string):Observable<ComponentGenericResponse> {
201         return this.getComponentDataByFieldsName(componentType, componentId, [COMPONENT_FIELDS.COMPONENT_REQUIREMENTS, COMPONENT_FIELDS.COMPONENT_CAPABILITIES]);
202     }
203
204     getDeploymentGraphData(component:Component):Observable<ComponentGenericResponse> {
205         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES_RELATION, COMPONENT_FIELDS.COMPONENT_INSTANCES, COMPONENT_FIELDS.COMPONENT_GROUPS]);
206     }
207
208     createInput(component:Component, inputsToCreate:InstancePropertiesAPIMap, isSelf:boolean):Observable<any> {
209         let inputs = isSelf ? { serviceProperties: inputsToCreate.componentInstanceProperties } : inputsToCreate;
210         return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/create/inputs', inputs)
211             .map(res => {
212                 return res.json();
213             })
214     }
215
216     restoreComponent(componentType:string, componentId:string){ 
217         return this.http.post(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/restore', {})
218     }
219
220     archiveComponent(componentType:string, componentId:string){
221         return this.http.post(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/archive', {})
222     }
223
224
225     deleteInput(component:Component, input:InputBEModel):Observable<InputBEModel> {
226         return this.http.delete(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/delete/' + input.uniqueId + '/input')
227             .map((res:Response) => {
228                 return new InputBEModel(res.json());
229             })
230     }
231
232     updateComponentInputs(component:Component, inputs:InputBEModel[]):Observable<InputBEModel[]> {
233         return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/update/inputs', inputs)
234             .map((res:Response) => {
235                 return res.json().map((input) => new InputBEModel(input));
236             })
237     }
238
239     filterComponentInstanceProperties(component: Component, filterData:FilterPropertiesAssignmentData): Observable<InstanceBePropertiesMap> {//instance-property-be-map
240         let params: URLSearchParams = new URLSearchParams();
241         _.forEach(filterData.selectedTypes, (type:string) => {
242             params.append('resourceType', type);
243         });
244
245         return this.http.get(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/filteredproperties/' + filterData.propertyName, {search: params})
246             .map((res: Response) => {
247                 return res.json();
248             });
249     }
250
251     createServiceProperty(component: Component, propertyModel:PropertyBEModel): Observable<PropertyBEModel> {
252         let serverObject = {};
253         serverObject[propertyModel.name] = propertyModel;
254         return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/properties', serverObject)
255             .map(res => {
256                 let property:PropertyBEModel = new PropertyBEModel(res.json());
257                 return property;
258             })
259     }
260
261     getServiceProperties(component: Component): Observable<Array<PropertyBEModel>> {
262         return this.http.get(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/properties')
263             .map((res: Response) => {
264                 if (!res.text()){
265                     return new Array<PropertyBEModel>();
266                 }
267                 return CommonUtils.initBeProperties(res.json());
268             });
269     }
270
271     updateServiceProperties(component: Component, properties: PropertyBEModel[]) {
272         return this.http.put( this.baseUrl + component.getTypeUrl() + component.uniqueId + '/properties', properties)
273             .map((res: Response) => {
274                 const resJson = res.json();
275                 return _.map(resJson,
276                     (resValue:PropertyBEModel) => new PropertyBEModel(resValue));
277             });
278     }
279
280     deleteServiceProperty(component:Component, property:PropertyBEModel):Observable<string> {
281         return this.http.delete(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/properties/' + property.uniqueId )
282             .map((res:Response) => {
283                 return property.uniqueId;
284             })
285     }
286
287     getDependencies(componentType:string, componentId: string):Observable<Array<IDependenciesServerResponse>> {
288         return this.http.get(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/dependencies')
289             .map((res:Response) => {
290                 return res.json();
291             });
292     }
293
294     automatedUpgrade(componentType:string, componentId: string, componentsIdsToUpgrade:Array<IAutomatedUpgradeRequestObj>):Observable<AutomatedUpgradeGenericResponse> {
295         return this.http.post(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/automatedupgrade', componentsIdsToUpgrade)
296             .map((res:Response) => {
297                 return res.json();
298             });
299     }
300 }
301