Sync Integ to Master
[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} from "app/models";
28 import {COMPONENT_FIELDS} from "app/utils";
29 import {ComponentGenericResponse} from "../responses/component-generic-response";
30 import {InstanceBePropertiesMap} from "../../../models/properties-inputs/property-fe-map";
31 import {API_QUERY_PARAMS} from "app/utils";
32 import { ComponentType, ServerTypeUrl } from "../../../utils/constants";
33 import { HttpService } from '../http.service';
34 import {SdcConfigToken, ISdcConfig} from "../../config/sdc-config.config";
35
36 declare var angular:angular.IAngularStatic;
37
38 @Injectable()
39 export class ComponentServiceNg2 {
40
41     protected baseUrl;
42
43     constructor(protected http:HttpService, @Inject(SdcConfigToken) sdcConfig:ISdcConfig) {
44         this.baseUrl = sdcConfig.api.root + sdcConfig.api.component_api_root;
45     }
46
47     protected getComponentDataByFieldsName(componentType:string, componentId: string, fields:Array<string>):Observable<ComponentGenericResponse> {
48
49         let params:URLSearchParams = new URLSearchParams();
50         _.forEach(fields, (field:string):void => {
51             params.append(API_QUERY_PARAMS.INCLUDE, field);
52         });
53
54         return this.http.get(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/filteredDataByParams', {search: params})
55             .map((res:Response) => {
56                 return this.analyzeComponentDataResponse(res);
57             });
58     }
59
60     protected analyzeComponentDataResponse(res: Response):ComponentGenericResponse {
61         return new ComponentGenericResponse().deserialize(res.json());
62     }
63
64     private getServerTypeUrl = (componentType:string):string => {
65         switch (componentType) {
66             case ComponentType.SERVICE:
67                 return ServerTypeUrl.SERVICES;
68             default:
69                 return ServerTypeUrl.RESOURCES;
70         }
71     }
72
73     getComponentMetadata(component:Component):Observable<ComponentGenericResponse> {
74         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_METADATA]);
75     }
76
77     getComponentInstanceAttributesAndProperties(component:Component):Observable<ComponentGenericResponse> {
78         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES_PROPERTIES, COMPONENT_FIELDS.COMPONENT_INSTANCES_ATTRIBUTES]);
79     }
80
81     getComponentAttributes(component:Component):Observable<ComponentGenericResponse> {
82         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_ATTRIBUTES]);
83     }
84
85     getComponentCompositionData(component:Component):Observable<ComponentGenericResponse> {
86         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES_RELATION, COMPONENT_FIELDS.COMPONENT_INSTANCES, COMPONENT_FIELDS.COMPONENT_POLICIES, COMPONENT_FIELDS.COMPONENT_GROUPS]);
87     }
88
89     getComponentResourceInstances(component:Component):Observable<ComponentGenericResponse> {
90         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES]);
91     }
92
93     getComponentInputs(component:Component):Observable<ComponentGenericResponse> {
94         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INPUTS]);
95     }
96
97     getComponentDeploymentArtifacts(component:Component):Observable<ComponentGenericResponse> {
98         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_DEPLOYMENT_ARTIFACTS]);
99     }
100
101     getComponentInformationalArtifacts(component:Component):Observable<ComponentGenericResponse> {
102         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INFORMATIONAL_ARTIFACTS]);
103     }
104
105     getComponentInformationalArtifactsAndInstances(component:Component):Observable<ComponentGenericResponse> {
106         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INFORMATIONAL_ARTIFACTS, COMPONENT_FIELDS.COMPONENT_INSTANCES]);
107     }
108
109     getComponentToscaArtifacts(component:Component):Observable<ComponentGenericResponse> {
110         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_TOSCA_ARTIFACTS]);
111     }
112
113     getComponentProperties(component:Component):Observable<ComponentGenericResponse> {
114         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_PROPERTIES]);
115     }
116
117     getCapabilitiesAndRequirements(componentType: string, componentId:string):Observable<ComponentGenericResponse> {
118         return this.getComponentDataByFieldsName(componentType, componentId, [COMPONENT_FIELDS.COMPONENT_REQUIREMENTS, COMPONENT_FIELDS.COMPONENT_CAPABILITIES]);
119     }
120
121     getDeploymentGraphData(component:Component):Observable<ComponentGenericResponse> {
122         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES_RELATION, COMPONENT_FIELDS.COMPONENT_INSTANCES, COMPONENT_FIELDS.COMPONENT_GROUPS]);
123     }
124
125     createInput(component:Component, inputsToCreate:InstancePropertiesAPIMap):Observable<any> {
126         return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/create/inputs', inputsToCreate)
127             .map(res => {
128                 return res.json();
129             })
130     }
131
132     deleteInput(component:Component, input:InputBEModel):Observable<InputBEModel> {
133
134         return this.http.delete(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/delete/' + input.uniqueId + '/input')
135             .map((res:Response) => {
136                 return new InputBEModel(res.json());
137             })
138     }
139
140     updateComponentInputs(component:Component, inputs:InputBEModel[]):Observable<InputBEModel[]> {
141
142         return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/update/inputs', inputs)
143             .map((res:Response) => {
144                 return res.json().map((input) => new InputBEModel(input));
145             })
146     }
147
148     filterComponentInstanceProperties(component: Component, filterData:FilterPropertiesAssignmentData): Observable<InstanceBePropertiesMap> {//instance-property-be-map
149         let params: URLSearchParams = new URLSearchParams();
150         _.forEach(filterData.selectedTypes, (type:string) => {
151             params.append('resourceType', type);
152         });
153
154         return this.http.get(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/filteredproperties/' + filterData.propertyName, {search: params})
155             .map((res: Response) => {
156                 return res.json();
157             });
158     }
159 }
160