[SDC] rebase code
[sdc.git] / catalog-ui / src / app / ng2 / services / component-services / component.service.ts
1 import {Injectable} from '@angular/core';
2 import {Observable} from 'rxjs/Observable';
3 import 'rxjs/add/operator/map';
4 import 'rxjs/add/operator/toPromise';
5 import {Response, URLSearchParams} from '@angular/http';
6 import { Component, PropertyBEModel, InstancePropertiesAPIMap, FilterPropertiesAssignmentData} from "app/models";
7 import {downgradeInjectable} from '@angular/upgrade/static';
8 import {COMPONENT_FIELDS} from "app/utils";
9 import {ComponentGenericResponse} from "../responses/component-generic-response";
10 import {sdc2Config} from "../../../../main";
11 import {InstanceBePropertiesMap} from "../../../models/properties-inputs/property-fe-map";
12 import {API_QUERY_PARAMS} from "app/utils";
13 import {ComponentType, ServerTypeUrl} from "../../../utils/constants";
14 import {InterceptorService} from "ng2-interceptors/index";
15
16 declare var angular:angular.IAngularStatic;
17
18 @Injectable()
19 export class ComponentServiceNg2 {
20
21     protected baseUrl;
22
23     constructor(private http:InterceptorService) {
24         this.baseUrl = sdc2Config.api.root + sdc2Config.api.component_api_root;
25     }
26
27     private getComponentDataByFieldsName(componentType:string, componentId: string, fields:Array<string>):Observable<ComponentGenericResponse> {
28
29         let params:URLSearchParams = new URLSearchParams();
30         _.forEach(fields, (field:string):void => {
31             params.append(API_QUERY_PARAMS.INCLUDE, field);
32         });
33
34         return this.http.get(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/filteredDataByParams', {search: params})
35             .map((res:Response) => {
36                 return new ComponentGenericResponse().deserialize(res.json());
37             }).do(error => console.log('server data:', error));
38     }
39
40     private getServerTypeUrl = (componentType:string):string => {
41         switch (componentType) {
42             case ComponentType.PRODUCT:
43                 return ServerTypeUrl.PRODUCTS;
44             case ComponentType.SERVICE:
45                 return ServerTypeUrl.SERVICES;
46             default:
47                 return ServerTypeUrl.RESOURCES;
48         }
49     }
50
51     getComponentMetadata(component:Component):Observable<ComponentGenericResponse> {
52         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_METADATA]);
53     }
54
55     getComponentInstanceAttributesAndProperties(component:Component):Observable<ComponentGenericResponse> {
56         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES_PROPERTIES, COMPONENT_FIELDS.COMPONENT_INSTANCES_ATTRIBUTES]);
57     }
58
59     getComponentAttributes(component:Component):Observable<ComponentGenericResponse> {
60         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_ATTRIBUTES]);
61     }
62
63     getComponentInstancesAndRelation(component:Component):Observable<ComponentGenericResponse> {
64         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES_RELATION, COMPONENT_FIELDS.COMPONENT_INSTANCES]);
65     }
66
67     getComponentResourceInstances(component:Component):Observable<ComponentGenericResponse> {
68         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES]);
69     }
70
71     getComponentInputs(component:Component):Observable<ComponentGenericResponse> {
72         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INPUTS]);
73     }
74
75     getComponentDeploymentArtifacts(component:Component):Observable<ComponentGenericResponse> {
76         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_DEPLOYMENT_ARTIFACTS]);
77     }
78
79     getComponentInformationalArtifacts(component:Component):Observable<ComponentGenericResponse> {
80         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INFORMATIONAL_ARTIFACTS]);
81     }
82
83     getComponentInformationalArtifactsAndInstances(component:Component):Observable<ComponentGenericResponse> {
84         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INFORMATIONAL_ARTIFACTS, COMPONENT_FIELDS.COMPONENT_INSTANCES]);
85     }
86
87     getComponentToscaArtifacts(component:Component):Observable<ComponentGenericResponse> {
88         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_TOSCA_ARTIFACTS]);
89     }
90
91     getComponentProperties(component:Component):Observable<ComponentGenericResponse> {
92         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_PROPERTIES]);
93     }
94
95     getCapabilitiesAndRequirements(componentType: string, componentId:string):Observable<ComponentGenericResponse> {
96         return this.getComponentDataByFieldsName(componentType, componentId, [COMPONENT_FIELDS.COMPONENT_REQUIREMENTS, COMPONENT_FIELDS.COMPONENT_CAPABILITIES]);
97     }
98
99     getDeploymentGraphData(component:Component):Observable<ComponentGenericResponse> {
100         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES_RELATION, COMPONENT_FIELDS.COMPONENT_INSTANCES, COMPONENT_FIELDS.COMPONENT_GROUPS]);
101     }
102
103     createInput(component:Component, inputsToCreate:InstancePropertiesAPIMap):Observable<any> {
104         return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/create/inputs', inputsToCreate)
105             .map(res => {
106                 return res.json();
107             })
108     }
109
110     deleteInput(component:Component, input:PropertyBEModel):Observable<PropertyBEModel> {
111
112         return this.http.delete(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/delete/' + input.uniqueId + '/input')
113             .map((res:Response) => {
114                 return new PropertyBEModel(res.json());
115             })
116     }
117
118     updateComponentInput(component:Component, input:PropertyBEModel):Observable<PropertyBEModel> {
119
120         return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/update/inputs', input)
121             .map((res:Response) => {
122                 return new PropertyBEModel(res.json())
123             })
124     }
125
126     filterComponentInstanceProperties(component: Component, filterData:FilterPropertiesAssignmentData): Observable<InstanceBePropertiesMap> {//instance-property-be-map
127         let params: URLSearchParams = new URLSearchParams();
128         _.forEach(filterData.selectedTypes, (type:string) => {
129             params.append('resourceType', type);
130         });
131
132         return this.http.get(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/filteredproperties/' + filterData.propertyName, {search: params})
133             .map((res: Response) => {
134                 return res.json();
135             });
136
137         // 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"},
138         //     {definition: false,name: "exVL_naming",password: false,required: true,type: "org.openecomp.datatypes.Naming",uniqueId: "623cca1c-d605-4c9c-9f2b-935ec85ebcf8.exVL_naming"},
139         //     {definition: false,name: "network_flows",password: false,required: false,type: "org.openecomp.datatypes.network.NetworkFlows",uniqueId: "623cca1c-d605-4c9c-9f2b-935ec85ebcf8.network_flows"},
140         //     {definition: false,name: "provider_network",password: false,required: true,type: "org.openecomp.datatypes.network.ProviderNetwork",uniqueId: "623cca1c-d605-4c9c-9f2b-935ec85ebcf8.provider_network"},
141         //     {definition: false,name: "network_homing",password: false,required: true,type: "org.openecomp.datatypes.EcompHoming",uniqueId: "623cca1c-d605-4c9c-9f2b-935ec85ebcf8.network_homing"}],
142         //     '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"}],
143         //     '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"}]};
144
145
146     }
147 }
148
149 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