[SDC] rebase 1710 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 {Injectable} from '@angular/core';
22 import {Observable} from 'rxjs/Observable';
23 import 'rxjs/add/operator/map';
24 import 'rxjs/add/operator/toPromise';
25 import {Response, URLSearchParams} from '@angular/http';
26 import { Component, PropertyBEModel, InstancePropertiesAPIMap, FilterPropertiesAssignmentData} from "app/models";
27 import {downgradeInjectable} from '@angular/upgrade/static';
28 import {COMPONENT_FIELDS} from "app/utils";
29 import {ComponentGenericResponse} from "../responses/component-generic-response";
30 import {sdc2Config} from "../../../../main";
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 {InterceptorService} from "ng2-interceptors/index";
35
36 declare var angular:angular.IAngularStatic;
37
38 @Injectable()
39 export class ComponentServiceNg2 {
40
41     protected baseUrl;
42
43     constructor(private http:InterceptorService) {
44         this.baseUrl = sdc2Config.api.root + sdc2Config.api.component_api_root;
45     }
46
47     private 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 new ComponentGenericResponse().deserialize(res.json());
57             }).do(error => console.log('server data:', error));
58     }
59
60     private getServerTypeUrl = (componentType:string):string => {
61         switch (componentType) {
62             case ComponentType.SERVICE:
63                 return ServerTypeUrl.SERVICES;
64             default:
65                 return ServerTypeUrl.RESOURCES;
66         }
67     }
68
69     getComponentMetadata(component:Component):Observable<ComponentGenericResponse> {
70         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_METADATA]);
71     }
72
73     getComponentInstanceAttributesAndProperties(component:Component):Observable<ComponentGenericResponse> {
74         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES_PROPERTIES, COMPONENT_FIELDS.COMPONENT_INSTANCES_ATTRIBUTES]);
75     }
76
77     getComponentAttributes(component:Component):Observable<ComponentGenericResponse> {
78         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_ATTRIBUTES]);
79     }
80
81     getComponentInstancesAndRelation(component:Component):Observable<ComponentGenericResponse> {
82         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES_RELATION, COMPONENT_FIELDS.COMPONENT_INSTANCES]);
83     }
84
85     getComponentResourceInstances(component:Component):Observable<ComponentGenericResponse> {
86         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES]);
87     }
88
89     getComponentInputs(component:Component):Observable<ComponentGenericResponse> {
90         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INPUTS]);
91     }
92
93     getComponentDeploymentArtifacts(component:Component):Observable<ComponentGenericResponse> {
94         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_DEPLOYMENT_ARTIFACTS]);
95     }
96
97     getComponentInformationalArtifacts(component:Component):Observable<ComponentGenericResponse> {
98         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INFORMATIONAL_ARTIFACTS]);
99     }
100
101     getComponentInformationalArtifactsAndInstances(component:Component):Observable<ComponentGenericResponse> {
102         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INFORMATIONAL_ARTIFACTS, COMPONENT_FIELDS.COMPONENT_INSTANCES]);
103     }
104
105     getComponentToscaArtifacts(component:Component):Observable<ComponentGenericResponse> {
106         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_TOSCA_ARTIFACTS]);
107     }
108
109     getComponentProperties(component:Component):Observable<ComponentGenericResponse> {
110         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_PROPERTIES]);
111     }
112
113     getCapabilitiesAndRequirements(componentType: string, componentId:string):Observable<ComponentGenericResponse> {
114         return this.getComponentDataByFieldsName(componentType, componentId, [COMPONENT_FIELDS.COMPONENT_REQUIREMENTS, COMPONENT_FIELDS.COMPONENT_CAPABILITIES]);
115     }
116
117     getDeploymentGraphData(component:Component):Observable<ComponentGenericResponse> {
118         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES_RELATION, COMPONENT_FIELDS.COMPONENT_INSTANCES, COMPONENT_FIELDS.COMPONENT_GROUPS]);
119     }
120
121     createInput(component:Component, inputsToCreate:InstancePropertiesAPIMap):Observable<any> {
122         return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/create/inputs', inputsToCreate)
123             .map(res => {
124                 return res.json();
125             })
126     }
127
128     deleteInput(component:Component, input:PropertyBEModel):Observable<PropertyBEModel> {
129
130         return this.http.delete(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/delete/' + input.uniqueId + '/input')
131             .map((res:Response) => {
132                 return new PropertyBEModel(res.json());
133             })
134     }
135
136     updateComponentInput(component:Component, input:PropertyBEModel):Observable<PropertyBEModel> {
137
138         return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/update/inputs', input)
139             .map((res:Response) => {
140                 return new PropertyBEModel(res.json())
141             })
142     }
143
144     filterComponentInstanceProperties(component: Component, filterData:FilterPropertiesAssignmentData): Observable<InstanceBePropertiesMap> {//instance-property-be-map
145         let params: URLSearchParams = new URLSearchParams();
146         _.forEach(filterData.selectedTypes, (type:string) => {
147             params.append('resourceType', type);
148         });
149
150         return this.http.get(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/filteredproperties/' + filterData.propertyName, {search: params})
151             .map((res: Response) => {
152                 return res.json();
153             });
154
155         // return {'ExtVL 0':[{definition: false,name:"network_assignments",password:false,required:true,type:"org.openecomp.datatypes.network.NetworkAssignments",uniqueId:"623cca1c-d605-4c9c-9f2b-935ec85ebcf8.network_assignments"},
156         //     {definition: false,name: "exVL_naming",password: false,required: true,type: "org.openecomp.datatypes.Naming",uniqueId: "623cca1c-d605-4c9c-9f2b-935ec85ebcf8.exVL_naming"},
157         //     {definition: false,name: "network_flows",password: false,required: false,type: "org.openecomp.datatypes.network.NetworkFlows",uniqueId: "623cca1c-d605-4c9c-9f2b-935ec85ebcf8.network_flows"},
158         //     {definition: false,name: "provider_network",password: false,required: true,type: "org.openecomp.datatypes.network.ProviderNetwork",uniqueId: "623cca1c-d605-4c9c-9f2b-935ec85ebcf8.provider_network"},
159         //     {definition: false,name: "network_homing",password: false,required: true,type: "org.openecomp.datatypes.EcompHoming",uniqueId: "623cca1c-d605-4c9c-9f2b-935ec85ebcf8.network_homing"}],
160         //     'NetworkCP 0':[{definition: false,description: "identifies MAC address assignments to the CP",name: "mac_requirements",password: false,required: false,type: "org.openecomp.datatypes.network.MacRequirements",uniqueId: "26ec2bfd-b904-46c7-87ed-b32775120f2c.mac_requirements"}],
161         //     'NetworkCP 1':[{definition: false,description: "identifies MAC address assignments to the CP",name: "mac_requirements",password: false,required: false,type: "org.openecomp.datatypes.network.MacRequirements",uniqueId: "26ec2bfd-b904-46c7-87ed-b32775120f2c.mac_requirements"}]};
162
163
164     }
165 }
166
167 angular.module('Sdc.Services').factory('ComponentServiceNg2', downgradeInjectable(ComponentServiceNg2)); // This is in order to use the service in angular1 till we finish remove all angular1 code