Fail VF download of csar when VF has operation defined with workflow.
[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 { Component, InputBEModel, InstancePropertiesAPIMap, FilterPropertiesAssignmentData, OperationModel, CreateOperationResponse, ArtifactModel} from "app/models";
27 import {COMPONENT_FIELDS} from "app/utils";
28 import {ComponentGenericResponse} from "../responses/component-generic-response";
29 import {InstanceBePropertiesMap} from "../../../models/properties-inputs/property-fe-map";
30 import {API_QUERY_PARAMS} from "app/utils";
31 import {ComponentType, ServerTypeUrl, SERVICE_FIELDS} from "../../../utils/constants";
32 import {SdcConfigToken, ISdcConfig} from "../../config/sdc-config.config";
33 import {IDependenciesServerResponse} from "../responses/dependencies-server-response";
34 import {AutomatedUpgradeGenericResponse} from "../responses/automated-upgrade-response";
35 import {IAutomatedUpgradeRequestObj} from "../../pages/automated-upgrade/automated-upgrade.service";
36 import {ComponentInstance} from "../../../models/componentsInstances/componentInstance";
37 import {CommonUtils} from "../../../utils/common-utils";
38 import {RelationshipModel} from "../../../models/graph/relationship";
39 import { HttpClient, HttpParams, HttpHeaders } from "@angular/common/http";
40 import { BEOperationModel, InterfaceModel } from "../../../models/operation";
41 import { PropertyBEModel } from "../../../models/properties-inputs/property-be-model";
42 import { PolicyInstance } from "../../../models/graph/zones/policy-instance";
43 import { ConstraintObject } from "../../components/logic/service-dependencies/service-dependencies.component";
44 import { Requirement } from "../../../models/requirement";
45 import { Capability } from "../../../models/capability";
46 import { OutputBEModel } from "app/models/attributes-outputs/output-be-model";
47 import { HttpHelperService } from '../http-hepler.service';
48
49 /*
50 PLEASE DO NOT USE THIS SERVICE IN ANGULAR2! Use the topology-template.service instead
51  */
52 @Injectable()
53 export class ComponentServiceNg2 {
54
55     protected baseUrl;
56
57     constructor(protected http: HttpClient, @Inject(SdcConfigToken) sdcConfig:ISdcConfig) {
58         this.baseUrl = sdcConfig.api.root + sdcConfig.api.component_api_root;
59     }
60
61     protected getComponentDataByFieldsName(componentType:string, componentId:string, fields:Array<string>):Observable<ComponentGenericResponse> {
62
63         let params: HttpParams = new HttpParams();
64         fields.forEach((field:string):void => {
65             params = params.append(API_QUERY_PARAMS.INCLUDE, field);
66         });
67
68         return this.http.get<ComponentGenericResponse>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/filteredDataByParams', {params: params})
69             .map((res) => {
70                 return new ComponentGenericResponse().deserialize(res);
71             });
72     }
73     protected getServerTypeUrl = (componentType:string):string => {
74         switch (componentType) {
75             case ComponentType.SERVICE:
76                 return ServerTypeUrl.SERVICES;
77             default:
78                 return ServerTypeUrl.RESOURCES;
79         }
80     }
81
82     getFullComponent(uniqueId:string):Observable<ComponentGenericResponse> {
83         return this.http.get<ComponentGenericResponse>(this.baseUrl + uniqueId)
84             .map((res) => {
85                 return new ComponentGenericResponse().deserialize(res);
86             });
87     }
88
89     getComponentMetadata(uniqueId:string, type:string):Observable<ComponentGenericResponse> {
90         return this.getComponentDataByFieldsName(type, uniqueId, [COMPONENT_FIELDS.COMPONENT_METADATA]);
91     }
92
93
94     getComponentInstanceAttributesAndProperties(component:Component):Observable<ComponentGenericResponse> {
95         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES_PROPERTIES, COMPONENT_FIELDS.COMPONENT_INSTANCES_ATTRIBUTES]);
96     }
97
98     getComponentInstanceProperties(component:Component): Observable<ComponentGenericResponse> {
99         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES_PROPERTIES]);
100     }
101
102     getComponentAttributes(component:Component):Observable<ComponentGenericResponse> {
103         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_ATTRIBUTES]);
104     }
105
106     getComponentCompositionData(component:Component):Observable<ComponentGenericResponse> {
107         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]);
108     }
109
110     getComponentResourcePropertiesData(component:Component):Observable<ComponentGenericResponse> {
111         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES, COMPONENT_FIELDS.COMPONENT_POLICIES, COMPONENT_FIELDS.COMPONENT_NON_EXCLUDED_GROUPS]);
112     }
113
114     getComponentResourceAttributesData(component:Component):Observable<ComponentGenericResponse> {
115         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES, COMPONENT_FIELDS.COMPONENT_NON_EXCLUDED_GROUPS]);
116     }
117
118     getComponentResourceInstances(component:Component):Observable<ComponentGenericResponse> {
119         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES]);
120     }
121
122     getComponentInputs(component:Component):Observable<ComponentGenericResponse> {
123         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INPUTS]);
124     }
125
126     getComponentInputsWithProperties(component:Component):Observable<ComponentGenericResponse> {
127         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INPUTS, COMPONENT_FIELDS.COMPONENT_INSTANCES, COMPONENT_FIELDS.COMPONENT_INSTANCES_PROPERTIES, COMPONENT_FIELDS.COMPONENT_PROPERTIES]);
128     }
129
130     getComponentDeploymentArtifacts(component:Component):Observable<ComponentGenericResponse> {
131         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_DEPLOYMENT_ARTIFACTS]);
132     }
133
134     getComponentInformationalArtifacts(component:Component):Observable<ComponentGenericResponse> {
135         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INFORMATIONAL_ARTIFACTS]);
136     }
137
138     getComponentInformationalArtifactsAndInstances(component:Component):Observable<ComponentGenericResponse> {
139         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INFORMATIONAL_ARTIFACTS, COMPONENT_FIELDS.COMPONENT_INSTANCES]);
140     }
141
142     getComponentToscaArtifacts(component:Component):Observable<ComponentGenericResponse> {
143         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_TOSCA_ARTIFACTS]);
144     }
145
146     getComponentProperties(component:Component):Observable<ComponentGenericResponse> {
147         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_PROPERTIES]);
148     }
149
150     getInterfaceOperations(component:Component):Observable<ComponentGenericResponse> {
151         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INTERFACE_OPERATIONS]);
152     }
153
154     getInterfaceOperation(component:Component, operation:OperationModel):Observable<OperationModel> {
155         return this.http.get<OperationModel>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/interfaceOperations/' + operation.uniqueId);
156     }
157
158     // tslint:disable-next-line:member-ordering
159     createInterfaceOperation(component:Component, operation:OperationModel):Observable<OperationModel> {
160         const operationList = {
161             'interfaces': {
162                 [operation.interfaceType]: {
163                     'type': operation.interfaceType,
164                     'operations': {
165                         [operation.name]: new BEOperationModel(operation)
166                     }
167                 }
168             }
169         };
170         return this.http.post<any>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/interfaceOperations', operationList)
171             .map((res:any) => {
172                 const interf:InterfaceModel = _.find(res.interfaces, interf => interf.type === operation.interfaceType);
173                 const newOperation:OperationModel = _.find(interf.operations, op => op.name === operation.name);
174                 return new OperationModel({
175                     ...newOperation,
176                     interfaceType: interf.type,
177                     interfaceId: interf.uniqueId,
178                     artifactFileName: operation.artifactFileName
179                 });
180             });
181     }
182
183     // tslint:disable-next-line:member-ordering
184     updateInterfaceOperation(component:Component, operation:OperationModel):Observable<OperationModel> {
185         const operationList = {
186             interfaces: {
187                 [operation.interfaceType]: {
188                     type: operation.interfaceType,
189                     operations: {
190                         [operation.name]: new BEOperationModel(operation)
191                     }
192                 }
193             }
194         };
195         return this.http.put<any>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/interfaceOperations', operationList)
196             .map((res: any) => {
197                 const interf: InterfaceModel = _.find(res.interfaces, interf => interf.type === operation.interfaceType);
198                 const newOperation: OperationModel = _.find(interf.operations, op => op.name === operation.name);
199                 return new OperationModel({
200                     ...newOperation,
201                     interfaceType: interf.type,
202                     interfaceId: interf.uniqueId,
203                     artifactFileName: operation.artifactFileName
204                 });
205             });
206     }
207
208
209     deleteInterfaceOperation(component: Component, operation:OperationModel):Observable<OperationModel> {
210         return this.http.delete<OperationModel>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/interfaces/' + operation.interfaceId + '/operations/' + operation.uniqueId);
211     }
212
213     getInterfaceTypes(component:Component):Observable<{[id:string]: Array<string>}> {
214         return this.http.get<any>(this.baseUrl + 'interfaceLifecycleTypes' + ((component && component.model) ? '?model=' + component.model : ''))
215             .map((res: any) => {
216                 const interfaceMap = {};
217                 if (!res) {
218                     return interfaceMap;
219                 }
220                 Object.keys(res).forEach(interfaceName => {
221                     const interface1 = res[interfaceName];
222                     if (!interface1.toscaPresentation.operations) {
223                         return;
224                     }
225                     interfaceMap[interface1.toscaPresentation.type] = Object.keys(interface1.toscaPresentation.operations);
226                 });
227                 return interfaceMap;
228             });
229     }
230
231     uploadInterfaceOperationArtifact(component:Component, newOperation:OperationModel, oldOperation:OperationModel) {
232         const payload = {
233             artifactType: "WORKFLOW",
234             artifactName: oldOperation.artifactFileName,
235             description: "Workflow Artifact Description",
236             payloadData: oldOperation.artifactData
237         };
238
239         const headers = new HttpHeaders().append('Content-MD5', HttpHelperService.getHeaderMd5(payload));
240
241         return this.http.post(this.baseUrl + component.getTypeUrl() + component.uuid + '/interfaces/' + newOperation.interfaceId + '/operations/' + newOperation.uniqueId + '/artifacts/' + newOperation.implementation.artifactUUID,
242                 payload, {headers: headers}
243             ).map((res: any) => {
244                 const fileName = res.artifactDisplayName || res.artifactName;
245                 newOperation.artifactFileName = fileName;
246                 return res;
247             });
248     }
249
250     getCapabilitiesAndRequirements(componentType: string, componentId:string):Observable<ComponentGenericResponse> {
251         return this.getComponentDataByFieldsName(componentType, componentId, [COMPONENT_FIELDS.COMPONENT_REQUIREMENTS, COMPONENT_FIELDS.COMPONENT_CAPABILITIES]);
252     }
253
254
255
256
257
258     getDeploymentGraphData(component:Component):Observable<ComponentGenericResponse> {
259         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES_RELATION, COMPONENT_FIELDS.COMPONENT_INSTANCES, COMPONENT_FIELDS.COMPONENT_GROUPS]);
260     }
261
262     createInput(component:Component, inputsToCreate:InstancePropertiesAPIMap, isSelf:boolean):Observable<any> {
263         const inputs = isSelf ? { serviceProperties: inputsToCreate.componentInstanceProperties } : inputsToCreate;
264         return this.http.post<any>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/create/inputs', inputs);
265     }
266
267     createListInput(component:Component, input:any, isSelf:boolean):Observable<any> {
268         let inputs: any;
269         if(isSelf) {
270             // change componentInstanceProperties -> serviceProperties
271             inputs = {
272                 componentInstInputsMap: {
273                     serviceProperties: input.componentInstInputsMap.componentInstanceProperties
274                 },
275                 listInput: input.listInput
276             };
277         } else {
278             inputs = input;
279         }
280         return this.http.post<any>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/create/listInput', inputs);
281     }
282
283     createPolicy(component:Component, policiesToCreate:InstancePropertiesAPIMap, isSelf:boolean):Observable<any> {
284         const policiesList =
285             isSelf ?
286                 {'componentPropertiesToPolicies': {
287                         ...policiesToCreate.componentInstanceProperties
288                     }
289                 } :
290                 {'componentInstancePropertiesToPolicies': {
291                         ...policiesToCreate.componentInstanceProperties
292                     }
293                 };
294         return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/create/policies', policiesList);
295     }
296
297     deletePolicy(component:Component, policy: PolicyInstance) {
298         return this.http.put<PolicyInstance>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/policies/' + policy.uniqueId + '/undeclare', policy);
299     }
300
301     restoreComponent(componentType:string, componentId:string) {
302         return this.http.post(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/restore', {})
303     }
304
305     archiveComponent(componentType:string, componentId:string) {
306         return this.http.post(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/archive', {})
307     }
308
309     deleteInput(component:Component, input:InputBEModel):Observable<InputBEModel> {
310
311         return this.http.delete<InputBEModel>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/delete/' + input.uniqueId + '/input')
312             .map((res) => {
313                 return new InputBEModel(res);
314             })
315     }
316
317     deleteOutput(component:Component, output:OutputBEModel):Observable<OutputBEModel> {
318
319         return this.http.delete<OutputBEModel>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/delete/' + output.uniqueId + '/output')
320             .map((res) => {
321                 return new OutputBEModel(res);
322             })
323     }
324
325     updateComponentInputs(component:Component, inputs:InputBEModel[]):Observable<InputBEModel[]> {
326
327         return this.http.post<InputBEModel[]>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/update/inputs', inputs)
328             .map((res) => {
329                 return res.map((input) => new InputBEModel(input));
330             })
331     }
332
333     updateComponentOutputs(component:Component, outputs:OutputBEModel[]):Observable<OutputBEModel[]> {
334
335         return this.http.post<OutputBEModel[]>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/update/outputs', outputs)
336             .map((res) => {
337                 return res.map((output) => new OutputBEModel(output));
338             })
339     }
340
341     filterComponentInstanceProperties(component:Component, filterData:FilterPropertiesAssignmentData):Observable<InstanceBePropertiesMap> {//instance-property-be-map
342         let params: HttpParams = new HttpParams();
343         filterData.selectedTypes.forEach((type:string) => {
344             params = params.append('resourceType', type);
345         });
346
347         return this.http.get<InstanceBePropertiesMap>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/filteredproperties/' + filterData.propertyName, {params: params});
348     }
349
350     filterComponentInstanceAttributes(component:Component, filterData:FilterPropertiesAssignmentData):Observable<InstanceBePropertiesMap> {//instance-property-be-map
351         let params: HttpParams = new HttpParams();
352         filterData.selectedTypes.forEach((type:string) => {
353             params = params.append('resourceType', type);
354         });
355
356         return this.http.get<InstanceBePropertiesMap>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/filteredproperties/' + filterData.propertyName, {params: params});
357     }
358
359     createServiceProperty(component: Component, propertyModel:PropertyBEModel): Observable<PropertyBEModel> {
360         let serverObject = {};
361         serverObject[propertyModel.name] = propertyModel;
362         return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/properties', serverObject)
363             .map((res: PropertyBEModel) => {
364                 const property: PropertyBEModel = new PropertyBEModel(res);
365                 return property;
366             })
367     }
368
369     getServiceProperties(component: Component): Observable<PropertyBEModel[]> {
370         return this.http.get(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/properties')
371             .map((res: PropertyBEModel[]) => {
372                 if (!res) {
373                     return new Array<PropertyBEModel>();
374                 }
375                 return CommonUtils.initBeProperties(res);
376             });
377     }
378
379     updateServiceProperties(component: Component, properties: PropertyBEModel[]) {
380         return this.http.put<PropertyBEModel[]>( this.baseUrl + component.getTypeUrl() + component.uniqueId + '/properties', properties)
381             .map((res) => {
382                 const resJson = res;
383                 return _.map(resJson,
384                     (resValue:PropertyBEModel) => new PropertyBEModel(resValue));
385             });
386     }
387
388     deleteServiceProperty(component:Component, property:PropertyBEModel):Observable<string> {
389         return this.http.delete(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/properties/' + property.uniqueId )
390             .map((res:Response) => {
391                 return property.uniqueId;
392             })
393     }
394
395     getDependencies(componentType:string, componentId: string):Observable<IDependenciesServerResponse[]> {
396         return this.http.get<IDependenciesServerResponse[]>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/dependencies');
397     }
398
399     automatedUpgrade(componentType:string, componentId:string, componentsIdsToUpgrade:IAutomatedUpgradeRequestObj[]):Observable<AutomatedUpgradeGenericResponse> {
400         return this.http.post<AutomatedUpgradeGenericResponse>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/automatedupgrade', componentsIdsToUpgrade);
401     }
402
403     updateMultipleComponentInstances(componentId:string, instances:ComponentInstance[]):Observable<ComponentInstance[]> {
404         return this.http.post<ComponentInstance[]>(this.baseUrl + componentId + '/resourceInstance/multipleComponentInstance', instances);
405     }
406
407     getServiceFilterConstraints(component:Component):Observable<ComponentGenericResponse> {
408         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [SERVICE_FIELDS.NODE_FILTER]);
409     }
410
411     createServiceFilterConstraints(component:Component, componentInstance:ComponentInstance, constraint:ConstraintObject):Observable<any> {
412         return this.http.post<any>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/resourceInstances/' + componentInstance.uniqueId + '/nodeFilter', constraint);
413     }
414
415     updateServiceFilterConstraints(component:Component, componentInstance:ComponentInstance, constraints:ConstraintObject[]):Observable<any> {
416         return this.http.put<any>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/resourceInstances/' + componentInstance.uniqueId + '/nodeFilter/', constraints);
417     }
418
419     deleteServiceFilterConstraints(component:Component, componentInstance:ComponentInstance, constraintIndex:number) {
420         return this.http.delete<any>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/resourceInstances/' + componentInstance.uniqueId + '/nodeFilter/' + constraintIndex);
421     }
422
423     protected analyzeComponentDataResponse(res: Response):ComponentGenericResponse {
424         return new ComponentGenericResponse().deserialize(res);
425     }
426 }