bug fixes to operation screen and External workflowartifact completion
[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, Headers} from '@angular/http';
27 import { Component, ComponentInstance, InputBEModel, InstancePropertiesAPIMap, FilterPropertiesAssignmentData,
28     PropertyBEModel, OperationModel, BEOperationModel, Capability, Requirement, PolicyInstance} 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     getComponentInputsWithProperties(component:Component):Observable<ComponentGenericResponse> {
112         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INPUTS, COMPONENT_FIELDS.COMPONENT_INSTANCES, COMPONENT_FIELDS.COMPONENT_INSTANCES_PROPERTIES, COMPONENT_FIELDS.COMPONENT_PROPERTIES]);
113     }
114
115     getComponentDeploymentArtifacts(component:Component):Observable<ComponentGenericResponse> {
116         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_DEPLOYMENT_ARTIFACTS]);
117     }
118
119     getComponentInformationalArtifacts(component:Component):Observable<ComponentGenericResponse> {
120         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INFORMATIONAL_ARTIFACTS]);
121     }
122
123     getComponentInformationalArtifactsAndInstances(component:Component):Observable<ComponentGenericResponse> {
124         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INFORMATIONAL_ARTIFACTS, COMPONENT_FIELDS.COMPONENT_INSTANCES]);
125     }
126
127     getComponentToscaArtifacts(component:Component):Observable<ComponentGenericResponse> {
128         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_TOSCA_ARTIFACTS]);
129     }
130
131     getComponentProperties(component:Component):Observable<ComponentGenericResponse> {
132         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_PROPERTIES]);
133     }
134
135     getInterfaces(component:Component):Observable<ComponentGenericResponse> {
136         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INTERFACE_OPERATIONS]);
137     }
138
139     getInterfaceOperation(component:Component, operation:OperationModel):Observable<OperationModel> {
140         return this.http.get(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/interfaces/' + operation.interfaceId + '/operations/' + operation.uniqueId)
141             .map((res:Response) => {
142                 return res.json();
143             });
144     }
145
146     createInterfaceOperation(component:Component, operation:OperationModel):Observable<OperationModel> {
147         const operationList = {
148             'interfaces': {
149                 [operation.interfaceType]: {
150                     'type': operation.interfaceType,
151                     'operations': {
152                         [operation.name]: new BEOperationModel(operation)
153                     }
154                 }
155             }
156         };
157         return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/interfaceOperations', operationList)
158             .map((res:Response) => {
159                 const interf = _.find(res.json().interfaces, (interf: any) => interf.type === operation.interfaceType);
160                 const newOperation = _.find(interf.operations, (op:OperationModel) => op.name === operation.name);
161                 return new OperationModel({
162                     ...newOperation,
163                     interfaceType: interf.type,
164                     interfaceId: interf.uniqueId
165                 });
166             });
167     }
168
169     updateInterfaceOperation(component:Component, operation:OperationModel):Observable<OperationModel> {
170         const operationList = {
171             'interfaces': {
172                 [operation.interfaceType]: {
173                     'type': operation.interfaceType,
174                     'operations': {
175                         [operation.name]: new BEOperationModel(operation)
176                     }
177                 }
178             }
179         };
180         return this.http.put(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/interfaceOperations', operationList)
181             .map((res:Response) => {
182                 const interf = _.find(res.json().interfaces, (interf: any) => interf.type === operation.interfaceType);
183                 const newOperation = _.find(interf.operations, (op:OperationModel) => op.name === operation.name);
184                 return new OperationModel({
185                     ...newOperation,
186                     interfaceType: interf.type,
187                     interfaceId: interf.uniqueId
188                 });
189             });
190     }
191
192     deleteInterfaceOperation(component:Component, operation:OperationModel):Observable<OperationModel> {
193         return this.http.delete(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/interfaces/' + operation.interfaceId + '/operations/' + operation.uniqueId)
194             .map((res:Response) => {
195                 return res.json();
196             });
197     }
198
199     getInterfaceTypes(component:Component):Observable<{[id:string]: Array<string>}> {
200         return this.http.get(this.baseUrl + 'interfaceLifecycleTypes')
201             .map((res:Response) => {
202                 const interfaceMap = {};
203                 _.forEach(res.json(), (interf:any) => {
204                     interfaceMap[interf.toscaPresentation.type] = _.keys(interf.toscaPresentation.operations);
205                 });
206                 return interfaceMap;
207             });
208     }
209
210     uploadInterfaceOperationArtifact(component:Component, response:OperationModel, UIOperation:OperationModel) {
211         const payload = {
212             artifactType: "WORKFLOW",
213             artifactName: UIOperation.artifactFile.name,
214             description: "Workflow Artifact Description",
215             payloadData: UIOperation.artifactData
216         };
217
218         const headers = new Headers();
219         JSON.stringify(payload);
220         const payloadString = JSON.stringify(payload, null, '  ');
221         const md5Result = md5(payloadString).toLowerCase();
222         headers.append('Content-MD5', btoa(md5Result));
223
224         return this.http.post(this.baseUrl + component.getTypeUrl() + component.uuid + '/interfaces/' + response.interfaceId + '/operations/' + response.uniqueId + '/artifacts/' + response.implementation.artifactUUID,
225                 payload,
226                 {headers}
227             ).map((res: Response) => {
228                 return res.json();
229             });
230     }
231
232     getCapabilitiesAndRequirements(componentType: string, componentId:string):Observable<ComponentGenericResponse> {
233         return this.getComponentDataByFieldsName(componentType, componentId, [COMPONENT_FIELDS.COMPONENT_REQUIREMENTS, COMPONENT_FIELDS.COMPONENT_CAPABILITIES]);
234     }
235
236     createCapability(component: Component, capabilityData: Capability): Observable<Array<Capability>> {
237         let capBEObj = {
238             'capabilities': {
239                 [capabilityData.type]: [capabilityData]
240             }
241         };
242         return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/capabilities', capBEObj)
243             .map((res: Response) => {
244                 return res.json();
245             });
246     }
247
248     updateCapability(component: Component, capabilityData: Capability): Observable<Array<Capability>> {
249         let capBEObj = {
250             'capabilities': {
251                 [capabilityData.type]: [capabilityData]
252             }
253         };
254         return this.http.put(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/capabilities', capBEObj)
255             .map((res: Response) => {
256                 return res.json();
257             });
258     }
259
260     deleteCapability(component: Component, capId: string): Observable<Capability> {
261         return this.http.delete(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/capabilities/' + capId)
262             .map((res: Response) => {
263                 return res.json();
264             });
265     }
266
267     createRequirement(component: Component, requirementData: Requirement): Observable<any> {
268         let reqBEObj = {
269             'requirements': {
270                 [requirementData.capability]: [requirementData]
271             }
272         };
273         return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/requirements', reqBEObj)
274             .map((res: Response) => {
275                 return res.json();
276             });
277     }
278
279     updateRequirement(component: Component, requirementData: Requirement): Observable<any> {
280         let reqBEObj = {
281             'requirements': {
282                 [requirementData.capability]: [requirementData]
283             }
284         };
285         return this.http.put(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/requirements', reqBEObj)
286             .map((res: Response) => {
287                 return res.json();
288             });
289     }
290
291     deleteRequirement(component: Component, reqId: string): Observable<Requirement> {
292         return this.http.delete(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/requirements/' + reqId)
293             .map((res: Response) => {
294                 return res.json();
295             });
296     }
297
298     getDeploymentGraphData(component:Component):Observable<ComponentGenericResponse> {
299         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES_RELATION, COMPONENT_FIELDS.COMPONENT_INSTANCES, COMPONENT_FIELDS.COMPONENT_GROUPS]);
300     }
301
302     createInput(component:Component, inputsToCreate:InstancePropertiesAPIMap, isSelf:boolean):Observable<any> {
303         let inputs = isSelf ? { serviceProperties: inputsToCreate.componentInstanceProperties } : inputsToCreate;
304         return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/create/inputs', inputs)
305             .map(res => {
306                 return res.json();
307             })
308     }
309
310     createPolicy(component:Component, policiesToCreate:InstancePropertiesAPIMap, isSelf:boolean):Observable<any> {
311         const policiesList =
312             isSelf ?
313                 {'componentPropertiesToPolicies': {
314                         ...policiesToCreate.componentInstanceProperties
315                     }
316                 } :
317                 {'componentInstancePropertiesToPolicies': {
318                         ...policiesToCreate.componentInstanceProperties
319                     }
320                 };
321         return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/create/policies', policiesList)
322             .map(res => {
323                 return res.json();
324             });
325     }
326
327     deletePolicy(component:Component, policy: PolicyInstance) {
328         return this.http.put(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/policies/' + policy.uniqueId + '/undeclare', policy)
329             .map(res => {
330                 return res.json();
331             });
332     }
333
334     restoreComponent(componentType:string, componentId:string){
335         return this.http.post(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/restore', {})
336     }
337
338     archiveComponent(componentType:string, componentId:string){
339         return this.http.post(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/archive', {})
340     }
341
342
343     deleteInput(component:Component, input:InputBEModel):Observable<InputBEModel> {
344         return this.http.delete(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/delete/' + input.uniqueId + '/input')
345             .map((res:Response) => {
346                 return new InputBEModel(res.json());
347             });
348     }
349
350     updateComponentInputs(component:Component, inputs:InputBEModel[]):Observable<InputBEModel[]> {
351         return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/update/inputs', inputs)
352             .map((res:Response) => {
353                 return res.json().map((input) => new InputBEModel(input));
354             })
355     }
356
357     filterComponentInstanceProperties(component: Component, filterData:FilterPropertiesAssignmentData): Observable<InstanceBePropertiesMap> {//instance-property-be-map
358         let params: URLSearchParams = new URLSearchParams();
359         _.forEach(filterData.selectedTypes, (type:string) => {
360             params.append('resourceType', type);
361         });
362
363         return this.http.get(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/filteredproperties/' + filterData.propertyName, {search: params})
364             .map((res: Response) => {
365                 return res.json();
366             });
367     }
368
369     createServiceProperty(component: Component, propertyModel:PropertyBEModel): Observable<PropertyBEModel> {
370         let serverObject = {};
371         serverObject[propertyModel.name] = propertyModel;
372         return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/properties', serverObject)
373             .map(res => {
374                 let property:PropertyBEModel = new PropertyBEModel(res.json());
375                 return property;
376             })
377     }
378
379     getServiceProperties(component: Component): Observable<Array<PropertyBEModel>> {
380         return this.http.get(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/properties')
381             .map((res: Response) => {
382                 if (!res.text()){
383                     return new Array<PropertyBEModel>();
384                 }
385                 return CommonUtils.initBeProperties(res.json());
386             });
387     }
388
389     updateServiceProperties(component: Component, properties: PropertyBEModel[]) {
390         return this.http.put( this.baseUrl + component.getTypeUrl() + component.uniqueId + '/properties', properties)
391             .map((res: Response) => {
392                 const resJson = res.json();
393                 return _.map(resJson,
394                     (resValue:PropertyBEModel) => new PropertyBEModel(resValue));
395             });
396     }
397
398     deleteServiceProperty(component:Component, property:PropertyBEModel):Observable<string> {
399         return this.http.delete(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/properties/' + property.uniqueId )
400             .map((res:Response) => {
401                 return property.uniqueId;
402             })
403     }
404
405     getDependencies(componentType:string, componentId: string):Observable<Array<IDependenciesServerResponse>> {
406         return this.http.get(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/dependencies')
407             .map((res:Response) => {
408                 return res.json();
409             });
410     }
411
412     automatedUpgrade(componentType:string, componentId: string, componentsIdsToUpgrade:Array<IAutomatedUpgradeRequestObj>):Observable<AutomatedUpgradeGenericResponse> {
413         return this.http.post(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/automatedupgrade', componentsIdsToUpgrade)
414             .map((res:Response) => {
415                 return res.json();
416             });
417     }
418
419     updateComponentInstance(component:Component, componentInstance:ComponentInstance):Observable<ComponentInstance> {
420         return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/resourceInstance/' + componentInstance.uniqueId, componentInstance)
421             .map((res:Response) => {
422                 return res.json();
423             });
424     }
425
426     getServiceFilterConstraints(component:Component):Observable<ComponentGenericResponse> {
427         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [SERVICE_FIELDS.NODE_FILTER]);
428     }
429
430     createServiceFilterConstraints(component:Component, componentInstance:ComponentInstance, constraint:ConstraintObject):Observable<any> {
431         return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/resourceInstances/' + componentInstance.uniqueId + '/nodeFilter', constraint)
432             .map((res:Response) => {
433                 return res.json();
434             });
435     }
436
437     updateServiceFilterConstraints(component:Component, componentInstance:ComponentInstance, constraints:Array<ConstraintObject>):Observable<any> {
438         return this.http.put(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/resourceInstances/' + componentInstance.uniqueId + '/nodeFilter/', constraints)
439             .map((res:Response) => {
440                 return res.json();
441             });
442     }
443
444     deleteServiceFilterConstraints(component:Component, componentInstance:ComponentInstance, constraintIndex:number) {
445         return this.http.delete(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/resourceInstances/' + componentInstance.uniqueId + '/nodeFilter/' + constraintIndex)
446             .map((res:Response) => {
447                 return res.json();
448             });
449     }
450 }
451