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