a25fb75a41fe315d097a7b68ad81831cb047803a
[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, ComponentInstance, InputBEModel, InstancePropertiesAPIMap, FilterPropertiesAssignmentData,
28     PropertyBEModel, OperationModel, BEOperationModel, Capability, Requirement} from "app/models";
29 import {COMPONENT_FIELDS, CommonUtils, SERVICE_FIELDS} from "app/utils";
30 import {downgradeInjectable} from '@angular/upgrade/static';
31 import {ComponentGenericResponse} from "../responses/component-generic-response";
32 import {InstanceBePropertiesMap} from "../../../models/properties-inputs/property-fe-map";
33 import {API_QUERY_PARAMS} from "app/utils";
34 import { ComponentType, ServerTypeUrl } from "../../../utils/constants";
35 import { HttpService } from '../http.service';
36 import {SdcConfigToken, ISdcConfig} from "../../config/sdc-config.config";
37 import {ConstraintObject} from 'app/ng2/components/logic/service-dependencies/service-dependencies.component';
38 import {IDependenciesServerResponse} from "../responses/dependencies-server-response";
39 import {AutomatedUpgradeGenericResponse} from "../responses/automated-upgrade-response";
40 import {IAutomatedUpgradeRequestObj} from "../../pages/automated-upgrade/automated-upgrade.service";
41
42 declare var angular:angular.IAngularStatic;
43
44 @Injectable()
45 export class ComponentServiceNg2 {
46
47     protected baseUrl;
48
49     constructor(protected http:HttpService, @Inject(SdcConfigToken) sdcConfig:ISdcConfig) {
50         this.baseUrl = sdcConfig.api.root + sdcConfig.api.component_api_root;
51     }
52
53     protected getComponentDataByFieldsName(componentType:string, componentId: string, fields:Array<string>):Observable<ComponentGenericResponse> {
54
55         let params:URLSearchParams = new URLSearchParams();
56         _.forEach(fields, (field:string):void => {
57             params.append(API_QUERY_PARAMS.INCLUDE, field);
58         });
59
60         return this.http.get(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/filteredDataByParams', {search: params})
61             .map((res:Response) => {
62                 return this.analyzeComponentDataResponse(res);
63             });
64     }
65
66     protected analyzeComponentDataResponse(res: Response):ComponentGenericResponse {
67         return new ComponentGenericResponse().deserialize(res.json());
68     }
69
70     private getServerTypeUrl = (componentType:string):string => {
71         switch (componentType) {
72             case ComponentType.SERVICE:
73                 return ServerTypeUrl.SERVICES;
74             default:
75                 return ServerTypeUrl.RESOURCES;
76         }
77     }
78
79     getComponentMetadata(component:Component):Observable<ComponentGenericResponse> {
80         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_METADATA]);
81     }
82
83     getComponentInstanceAttributesAndProperties(component:Component):Observable<ComponentGenericResponse> {
84         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES_PROPERTIES, COMPONENT_FIELDS.COMPONENT_INSTANCES_ATTRIBUTES]);
85     }
86
87     getComponentInstanceProperties(component:Component):Observable<ComponentGenericResponse> {
88         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES_PROPERTIES]);
89     }
90
91     getComponentAttributes(component:Component):Observable<ComponentGenericResponse> {
92         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_ATTRIBUTES]);
93     }
94
95     getComponentCompositionData(component:Component):Observable<ComponentGenericResponse> {
96         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]);
97     }
98
99     getComponentResourcePropertiesData(component:Component):Observable<ComponentGenericResponse> {
100         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES, COMPONENT_FIELDS.COMPONENT_POLICIES, COMPONENT_FIELDS.COMPONENT_NON_EXCLUDED_GROUPS]);
101     }
102
103     getComponentResourceInstances(component:Component):Observable<ComponentGenericResponse> {
104         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES]);
105     }
106
107     getComponentInputs(component:Component):Observable<ComponentGenericResponse> {
108         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INPUTS]);
109     }
110
111     getComponentDeploymentArtifacts(component:Component):Observable<ComponentGenericResponse> {
112         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_DEPLOYMENT_ARTIFACTS]);
113     }
114
115     getComponentInformationalArtifacts(component:Component):Observable<ComponentGenericResponse> {
116         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INFORMATIONAL_ARTIFACTS]);
117     }
118
119     getComponentInformationalArtifactsAndInstances(component:Component):Observable<ComponentGenericResponse> {
120         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INFORMATIONAL_ARTIFACTS, COMPONENT_FIELDS.COMPONENT_INSTANCES]);
121     }
122
123     getComponentToscaArtifacts(component:Component):Observable<ComponentGenericResponse> {
124         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_TOSCA_ARTIFACTS]);
125     }
126
127     getComponentProperties(component:Component):Observable<ComponentGenericResponse> {
128         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_PROPERTIES]);
129     }
130
131     getInterfaces(component:Component):Observable<ComponentGenericResponse> {
132         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INTERFACE_OPERATIONS]);
133     }
134
135     getInterfaceOperation(component:Component, operation:OperationModel):Observable<OperationModel> {
136         return this.http.get(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/interfaces/' + operation.interfaceId + '/operations/' + operation.uniqueId)
137             .map((res:Response) => {
138                 return res.json();
139             });
140     }
141
142     createInterfaceOperation(component:Component, operation:OperationModel):Observable<OperationModel> {
143         const operationList = {
144             'interfaces': {
145                 [operation.interfaceType]: {
146                     'type': operation.interfaceType,
147                     'operations': {
148                         [operation.name]: new BEOperationModel(operation)
149                     }
150                 }
151             }
152         };
153         return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/interfaceOperations', operationList)
154             .map((res:Response) => {
155                 const interf = _.find(res.json().interfaces, (interf: any) => interf.type === operation.interfaceType);
156                 const newOperation = _.find(interf.operations, (op:OperationModel) => op.name === operation.name);
157                 return new OperationModel({
158                     ...newOperation,
159                     interfaceType: interf.type,
160                     interfaceId: interf.uniqueId
161                 });
162             });
163     }
164
165     updateInterfaceOperation(component:Component, operation:OperationModel):Observable<OperationModel> {
166         const operationList = {
167             'interfaces': {
168                 [operation.interfaceType]: {
169                     'type': operation.interfaceType,
170                     'operations': {
171                         [operation.name]: new BEOperationModel(operation)
172                     }
173                 }
174             }
175         };
176         return this.http.put(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/interfaceOperations', operationList)
177             .map((res:Response) => {
178                 const interf = _.find(res.json().interfaces, (interf: any) => interf.type === operation.interfaceType);
179                 const newOperation = _.find(interf.operations, (op:OperationModel) => op.name === operation.name);
180                 return new OperationModel({
181                     ...newOperation,
182                     interfaceType: interf.type,
183                     interfaceId: interf.uniqueId
184                 });
185             });
186     }
187
188     deleteInterfaceOperation(component:Component, operation:OperationModel):Observable<OperationModel> {
189         return this.http.delete(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/interfaces/' + operation.interfaceId + '/operations/' + operation.uniqueId)
190             .map((res:Response) => {
191                 return res.json();
192             });
193     }
194
195     getInterfaceTypes(component:Component):Observable<{[id:string]: Array<string>}> {
196         return this.http.get(this.baseUrl + 'interfaceLifecycleTypes')
197             .map((res:Response) => {
198                 const interfaceMap = {};
199                 _.forEach(res.json(), (interf:any) => {
200                     interfaceMap[interf.toscaPresentation.type] = _.keys(interf.toscaPresentation.operations);
201                 });
202                 return interfaceMap;
203             });
204     }
205
206     getCapabilitiesAndRequirements(componentType: string, componentId:string):Observable<ComponentGenericResponse> {
207         return this.getComponentDataByFieldsName(componentType, componentId, [COMPONENT_FIELDS.COMPONENT_REQUIREMENTS, COMPONENT_FIELDS.COMPONENT_CAPABILITIES]);
208     }
209
210     createCapability(component: Component, capabilityData: Capability): Observable<Array<Capability>> {
211         let capBEObj = {
212             'capabilities': {
213                 [capabilityData.type]: [capabilityData]
214             }
215         };
216         return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/capabilities', capBEObj)
217             .map((res: Response) => {
218                 return res.json();
219             });
220     }
221
222     updateCapability(component: Component, capabilityData: Capability): Observable<Array<Capability>> {
223         let capBEObj = {
224             'capabilities': {
225                 [capabilityData.type]: [capabilityData]
226             }
227         };
228         return this.http.put(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/capabilities', capBEObj)
229             .map((res: Response) => {
230                 return res.json();
231             });
232     }
233
234     deleteCapability(component: Component, capId: string): Observable<Capability> {
235         return this.http.delete(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/capabilities/' + capId)
236             .map((res: Response) => {
237                 return res.json();
238             });
239     }
240
241     createRequirement(component: Component, requirementData: Requirement): Observable<any> {
242         let reqBEObj = {
243             'requirements': {
244                 [requirementData.capability]: [requirementData]
245             }
246         };
247         return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/requirements', reqBEObj)
248             .map((res: Response) => {
249                 return res.json();
250             });
251     }
252
253     updateRequirement(component: Component, requirementData: Requirement): Observable<any> {
254         let reqBEObj = {
255             'requirements': {
256                 [requirementData.capability]: [requirementData]
257             }
258         };
259         return this.http.put(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/requirements', reqBEObj)
260             .map((res: Response) => {
261                 return res.json();
262             });
263     }
264
265     deleteRequirement(component: Component, reqId: string): Observable<Requirement> {
266         return this.http.delete(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/requirements/' + reqId)
267             .map((res: Response) => {
268                 return res.json();
269             });
270     }
271
272     getDeploymentGraphData(component:Component):Observable<ComponentGenericResponse> {
273         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES_RELATION, COMPONENT_FIELDS.COMPONENT_INSTANCES, COMPONENT_FIELDS.COMPONENT_GROUPS]);
274     }
275
276     createInput(component:Component, inputsToCreate:InstancePropertiesAPIMap, isSelf:boolean):Observable<any> {
277         let inputs = isSelf ? { serviceProperties: inputsToCreate.componentInstanceProperties } : inputsToCreate;
278         return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/create/inputs', inputs)
279             .map(res => {
280                 return res.json();
281             })
282     }
283
284     restoreComponent(componentType:string, componentId:string){
285         return this.http.post(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/restore', {})
286     }
287
288     archiveComponent(componentType:string, componentId:string){
289         return this.http.post(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/archive', {})
290     }
291
292
293     deleteInput(component:Component, input:InputBEModel):Observable<InputBEModel> {
294         return this.http.delete(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/delete/' + input.uniqueId + '/input')
295             .map((res:Response) => {
296                 return new InputBEModel(res.json());
297             });
298     }
299
300     updateComponentInputs(component:Component, inputs:InputBEModel[]):Observable<InputBEModel[]> {
301         return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/update/inputs', inputs)
302             .map((res:Response) => {
303                 return res.json().map((input) => new InputBEModel(input));
304             })
305     }
306
307     filterComponentInstanceProperties(component: Component, filterData:FilterPropertiesAssignmentData): Observable<InstanceBePropertiesMap> {//instance-property-be-map
308         let params: URLSearchParams = new URLSearchParams();
309         _.forEach(filterData.selectedTypes, (type:string) => {
310             params.append('resourceType', type);
311         });
312
313         return this.http.get(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/filteredproperties/' + filterData.propertyName, {search: params})
314             .map((res: Response) => {
315                 return res.json();
316             });
317     }
318
319     createServiceProperty(component: Component, propertyModel:PropertyBEModel): Observable<PropertyBEModel> {
320         let serverObject = {};
321         serverObject[propertyModel.name] = propertyModel;
322         return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/properties', serverObject)
323             .map(res => {
324                 let property:PropertyBEModel = new PropertyBEModel(res.json());
325                 return property;
326             })
327     }
328
329     getServiceProperties(component: Component): Observable<Array<PropertyBEModel>> {
330         return this.http.get(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/properties')
331             .map((res: Response) => {
332                 if (!res.text()){
333                     return new Array<PropertyBEModel>();
334                 }
335                 return CommonUtils.initBeProperties(res.json());
336             });
337     }
338
339     updateServiceProperties(component: Component, properties: PropertyBEModel[]) {
340         return this.http.put( this.baseUrl + component.getTypeUrl() + component.uniqueId + '/properties', properties)
341             .map((res: Response) => {
342                 const resJson = res.json();
343                 return _.map(resJson,
344                     (resValue:PropertyBEModel) => new PropertyBEModel(resValue));
345             });
346     }
347
348     deleteServiceProperty(component:Component, property:PropertyBEModel):Observable<string> {
349         return this.http.delete(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/properties/' + property.uniqueId )
350             .map((res:Response) => {
351                 return property.uniqueId;
352             })
353     }
354
355     getDependencies(componentType:string, componentId: string):Observable<Array<IDependenciesServerResponse>> {
356         return this.http.get(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/dependencies')
357             .map((res:Response) => {
358                 return res.json();
359             });
360     }
361
362     automatedUpgrade(componentType:string, componentId: string, componentsIdsToUpgrade:Array<IAutomatedUpgradeRequestObj>):Observable<AutomatedUpgradeGenericResponse> {
363         return this.http.post(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/automatedupgrade', componentsIdsToUpgrade)
364             .map((res:Response) => {
365                 return res.json();
366             });
367     }
368
369     updateComponentInstance(component:Component, componentInstance:ComponentInstance):Observable<ComponentInstance> {
370         return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/resourceInstance/' + componentInstance.uniqueId, componentInstance)
371             .map((res:Response) => {
372                 return res.json();
373             });
374     }
375
376     getServiceFilterConstraints(component:Component):Observable<ComponentGenericResponse> {
377         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [SERVICE_FIELDS.NODE_FILTER]);
378     }
379
380     createServiceFilterConstraints(component:Component, componentInstance:ComponentInstance, constraint:ConstraintObject):Observable<any> {
381         return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/resourceInstances/' + componentInstance.uniqueId + '/nodeFilter', constraint)
382             .map((res:Response) => {
383                 return res.json();
384             });
385     }
386
387     updateServiceFilterConstraints(component:Component, componentInstance:ComponentInstance, constraints:Array<ConstraintObject>):Observable<any> {
388         return this.http.put(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/resourceInstances/' + componentInstance.uniqueId + '/nodeFilter/', constraints)
389             .map((res:Response) => {
390                 return res.json();
391             });
392     }
393
394     deleteServiceFilterConstraints(component:Component, componentInstance:ComponentInstance, constraintIndex:number) {
395         return this.http.delete(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/resourceInstances/' + componentInstance.uniqueId + '/nodeFilter/' + constraintIndex)
396             .map((res:Response) => {
397                 return res.json();
398             });
399     }
400 }
401