0af8c737de9342bf9fd3b38c610b3b07dbac6be8
[sdc.git] / catalog-ui / src / app / ng2 / services / component-services / topology-template.service.ts
1 /**
2  * Created by ob0695 on 6/26/2018.
3  */
4 /*-
5  * ============LICENSE_START=======================================================
6  * SDC
7  * ================================================================================
8  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
9  * Modification Copyright (C) 2022 Nordix Foundation.
10  * ================================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  * ============LICENSE_END=========================================================
23  */
24
25 import * as _ from "lodash";
26 import {Inject, Injectable} from '@angular/core';
27 import {Observable} from 'rxjs/Observable';
28 import 'rxjs/add/operator/map';
29 import 'rxjs/add/operator/toPromise';
30 import {
31     ArtifactModel,
32     AttributeModel,
33     Capability,
34     Component,
35     FilterPropertiesAssignmentData,
36     IFileDownload,
37     InputBEModel,
38     InstancePropertiesAPIMap,
39     OperationModel,
40     PropertyModel,
41     Requirement
42 } from "app/models";
43 import {API_QUERY_PARAMS, ArtifactGroupType, COMPONENT_FIELDS} from "app/utils";
44 import {ComponentGenericResponse} from "../responses/component-generic-response";
45 import {InstanceBePropertiesMap} from "../../../models/properties-inputs/property-fe-map";
46 import {ComponentType, ServerTypeUrl, SERVICE_FIELDS} from "../../../utils/constants";
47 import {ISdcConfig, SdcConfigToken} from "../../config/sdc-config.config";
48 import {IDependenciesServerResponse} from "../responses/dependencies-server-response";
49 import {AutomatedUpgradeGenericResponse} from "../responses/automated-upgrade-response";
50 import {IAutomatedUpgradeRequestObj} from "../../pages/automated-upgrade/automated-upgrade.service";
51 import {ComponentInstance} from "../../../models/componentsInstances/componentInstance";
52 import {CommonUtils} from "../../../utils/common-utils";
53 import {RelationshipModel} from "../../../models/graph/relationship";
54 import {ServiceGenericResponse} from "../responses/service-generic-response";
55 import {HttpClient, HttpHeaders, HttpParams} from "@angular/common/http";
56 import {HttpHelperService} from "../http-hepler.service";
57 import {ConsumptionInput} from "../../components/logic/service-consumption/service-consumption.component";
58 import {PolicyInstance} from "../../../models/graph/zones/policy-instance";
59 import {PropertyBEModel} from "../../../models/properties-inputs/property-be-model";
60 import {map} from "rxjs/operators";
61 import {BEInterfaceOperationModel, InterfaceOperationModel} from "../../../models/interfaceOperation";
62 import {AttributeBEModel} from "../../../models/attributes-outputs/attribute-be-model";
63 import {InstanceAttributesAPIMap} from "../../../models/attributes-outputs/attribute-fe-map";
64 import {FilterConstraint} from "../../../models/filter-constraint";
65 import {CustomToscaFunction, DefaultCustomFunctions} from "../../../models/default-custom-functions";
66
67 /* we need to use this service from now, we will remove component.service when we finish remove the angular1.
68  The service is duplicated since we can not use downgrades service with NGXS*/
69
70 @Injectable()
71 export class TopologyTemplateService {
72
73     protected baseUrl;
74
75     constructor(protected http: HttpClient, @Inject(SdcConfigToken) sdcConfig: ISdcConfig) {
76         this.baseUrl = sdcConfig.api.root + sdcConfig.api.component_api_root;
77     }
78
79     putServiceToscaTemplate(componentId: string, componentType: string, file) {
80         return this.http.put<any>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/toscaTemplate', file)
81     }
82
83     putServiceToscaModel(componentId: string, componentType: string, file) {
84         let uploadData:FormData = new FormData();
85         uploadData.append('upload', file);
86         return this.http.put<any>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/toscaModel', uploadData);
87     }
88
89     getFullComponent(componentType: string, uniqueId: string): Observable<Component> {
90         return this.http.get<Component>(this.baseUrl + this.getServerTypeUrl(componentType) + uniqueId);
91     }
92
93     getComponentMetadata(uniqueId: string, type: string): Observable<ComponentGenericResponse> {
94         return this.getComponentDataByFieldsName(type, uniqueId, [COMPONENT_FIELDS.COMPONENT_METADATA]);
95     }
96
97     getComponentInstanceAttributesAndProperties(uniqueId: string, type: string): Observable<ComponentGenericResponse> {
98         return this.getComponentDataByFieldsName(type, uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES_PROPERTIES, COMPONENT_FIELDS.COMPONENT_INSTANCES_ATTRIBUTES]);
99     }
100
101     getComponentInstanceAttributesAndPropertiesAndInputs(uniqueId: string, type: string): Observable<ComponentGenericResponse> {
102         return this.getComponentDataByFieldsName(type, uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES_PROPERTIES, COMPONENT_FIELDS.COMPONENT_INSTANCES_ATTRIBUTES, COMPONENT_FIELDS.COMPONENT_INPUTS]);
103     }
104
105     async getComponentAttributes(componentType: string, componentId: string): Promise<ComponentGenericResponse> {
106         return this.getComponentDataByFieldsName(componentType, componentId, [COMPONENT_FIELDS.COMPONENT_ATTRIBUTES]).toPromise();
107     }
108
109     getComponentCompositionData(componentUniqueId: string, componentType: string): Observable<ComponentGenericResponse> {
110         const params: string[] = [COMPONENT_FIELDS.COMPONENT_INSTANCES_RELATION, COMPONENT_FIELDS.COMPONENT_INSTANCES,
111             COMPONENT_FIELDS.COMPONENT_NON_EXCLUDED_POLICIES, COMPONENT_FIELDS.COMPONENT_NON_EXCLUDED_GROUPS];
112         if (componentType === ComponentType.SERVICE) {
113             params.push(COMPONENT_FIELDS.FORWARDING_PATHS);
114         }
115         return this.getComponentDataByFieldsName(componentType, componentUniqueId, params);
116     }
117
118     getComponentResourcePropertiesData(component: Component): Observable<ComponentGenericResponse> {
119         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId,
120             [COMPONENT_FIELDS.COMPONENT_INSTANCES, COMPONENT_FIELDS.COMPONENT_POLICIES, COMPONENT_FIELDS.COMPONENT_NON_EXCLUDED_GROUPS]);
121     }
122
123     getComponentInstances(componentType: string, componentId: string): Observable<ComponentGenericResponse> {
124         return this.getComponentDataByFieldsName(componentType, componentId, [COMPONENT_FIELDS.COMPONENT_INSTANCES]);
125     }
126
127     getComponentInputs(component: Component): Observable<ComponentGenericResponse> {
128         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INPUTS]);
129     }
130
131     getComponentInputsValues(componentType: string, componentId: string): Observable<ComponentGenericResponse> {
132         return this.getComponentDataByFieldsName(componentType, componentId, [COMPONENT_FIELDS.COMPONENT_INPUTS]);
133     }
134
135     getComponentInputsWithProperties(componentType: string, componentId: string): Observable<ComponentGenericResponse> {
136         return this.getComponentDataByFieldsName(componentType, componentId,
137             [COMPONENT_FIELDS.COMPONENT_INPUTS, COMPONENT_FIELDS.COMPONENT_INSTANCES, COMPONENT_FIELDS.COMPONENT_INSTANCES_PROPERTIES, COMPONENT_FIELDS.COMPONENT_PROPERTIES]);
138     }
139
140     getComponentOutputsWithAttributes(componentType: string, componentId: string): Observable<ComponentGenericResponse> {
141         return this.getComponentDataByFieldsName(componentType, componentId,
142             [COMPONENT_FIELDS.COMPONENT_OUTPUTS, COMPONENT_FIELDS.COMPONENT_INSTANCES, COMPONENT_FIELDS.COMPONENT_INSTANCES_ATTRIBUTES, COMPONENT_FIELDS.COMPONENT_ATTRIBUTES,COMPONENT_FIELDS.COMPONENT_INSTANCES_OUTPUTS]);
143     }
144
145     getComponentDeploymentArtifacts(component: Component): Observable<ComponentGenericResponse> {
146         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_DEPLOYMENT_ARTIFACTS]);
147     }
148
149     getComponentInformationalArtifacts(component: Component): Observable<ComponentGenericResponse> {
150         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INFORMATIONAL_ARTIFACTS]);
151     }
152
153     getComponentInterfaceOperations(componentType: string, componentId: string): Observable<ComponentGenericResponse> {
154         return this.getComponentDataByFieldsName(componentType, componentId, [COMPONENT_FIELDS.COMPONENT_INTERFACE_OPERATIONS]);
155     }
156
157     getComponentInformationalArtifactsAndInstances(component: Component): Observable<ComponentGenericResponse> {
158         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INFORMATIONAL_ARTIFACTS, COMPONENT_FIELDS.COMPONENT_INSTANCES]);
159     }
160
161     getComponentToscaArtifacts(componentType: string, componentId: string): Observable<ComponentGenericResponse> {
162         return this.getComponentDataByFieldsName(componentType, componentId, [COMPONENT_FIELDS.COMPONENT_TOSCA_ARTIFACTS]);
163     }
164
165     getComponentProperties(component: Component): Observable<ComponentGenericResponse> {
166         return this.findAllComponentProperties(component.componentType, component.uniqueId);
167     }
168
169     findAllComponentProperties(componentType: string, componentUniqueId: string): Observable<ComponentGenericResponse> {
170         return this.getComponentDataByFieldsName(componentType, componentUniqueId, [COMPONENT_FIELDS.COMPONENT_PROPERTIES]);
171     }
172
173     findAllComponentAttributes(componentType: string, componentUniqueId: string): Observable<ComponentGenericResponse> {
174         return this.getComponentDataByFieldsName(componentType, componentUniqueId, [COMPONENT_FIELDS.COMPONENT_ATTRIBUTES]);
175     }
176
177     findAllComponentAttributesAndProperties(componentType: string, componentUniqueId: string): Observable<ComponentGenericResponse> {
178         return this.getComponentDataByFieldsName(componentType, componentUniqueId, [COMPONENT_FIELDS.COMPONENT_ATTRIBUTES, COMPONENT_FIELDS.COMPONENT_PROPERTIES]);
179     }
180
181     getCapabilitiesAndRequirements(componentType: string, componentId: string): Observable<ComponentGenericResponse> {
182         return this.getComponentDataByFieldsName(componentType, componentId, [COMPONENT_FIELDS.COMPONENT_REQUIREMENTS, COMPONENT_FIELDS.COMPONENT_CAPABILITIES]);
183     }
184
185     getRequirementsAndCapabilitiesWithProperties(componentType: string, componentId: string): Observable<ComponentGenericResponse> {
186         return this.getComponentDataByFieldsName(componentType, componentId,
187             [COMPONENT_FIELDS.COMPONENT_REQUIREMENTS, COMPONENT_FIELDS.COMPONENT_CAPABILITIES, COMPONENT_FIELDS.COMPONENT_CAPABILITIES_PROPERTIES]);
188     }
189
190     getDeploymentGraphData(componentType: string, componentId: string): Observable<ComponentGenericResponse> {
191         return this.getComponentDataByFieldsName(componentType, componentId, [COMPONENT_FIELDS.COMPONENT_INSTANCES_RELATION, COMPONENT_FIELDS.COMPONENT_INSTANCES, COMPONENT_FIELDS.COMPONENT_GROUPS]);
192     }
193
194     createInput(component: Component, inputsToCreate: InstancePropertiesAPIMap, isSelf: boolean): Observable<any> {
195         const inputs = isSelf ? { serviceProperties: inputsToCreate.componentInstanceProperties } : inputsToCreate;
196         return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/create/inputs', inputs);
197     }
198
199     createOutput(component: Component, outputsToCreate: InstanceAttributesAPIMap, isSelf: boolean): Observable<any> {
200         const outputs = isSelf ? { serviceProperties: outputsToCreate.componentInstanceAttributes } : outputsToCreate;
201         return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/create/outputs', outputs);
202     }
203
204     restoreComponent(componentType: string, componentId: string) {
205         return this.http.post(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/restore', {});
206     }
207
208     archiveComponent(componentType: string, componentId: string) {
209         return this.http.post(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/archive', {});
210     }
211
212     deleteInput(component: Component, input: InputBEModel): Observable<InputBEModel> {
213         return this.http.delete<InputBEModel>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/delete/' + input.uniqueId + '/input')
214             .map((res) => {
215                 return new InputBEModel(res);
216             });
217     }
218
219     updateComponentInputs(component: Component, inputs: InputBEModel[]): Observable<InputBEModel[]> {
220         return this.http.post<InputBEModel[]>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/update/inputs', inputs)
221             .map((res) => {
222                 return res.map((input) => new InputBEModel(input));
223             });
224     }
225
226     filterComponentInstanceProperties(component: Component, filterData: FilterPropertiesAssignmentData): Observable<InstanceBePropertiesMap> {// instance-property-be-map
227         let params: HttpParams = new HttpParams();
228         _.forEach(filterData.selectedTypes, (type: string) => {
229             params = params.append('resourceType', type);
230         });
231
232         // tslint:disable-next-line:object-literal-shorthand
233         return this.http.get<InstanceBePropertiesMap>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/filteredproperties/' + filterData.propertyName, {params: params});
234     }
235
236      createServiceProperty(componentId: string, propertyModel: PropertyBEModel): Observable<PropertyBEModel> {
237         const serverObject = {};
238         serverObject[propertyModel.name] = propertyModel;
239         return this.http.post<PropertyBEModel>(this.baseUrl + 'services/' + componentId + '/properties', serverObject)
240             .map((res) => {
241                 const property: PropertyBEModel = new PropertyBEModel(res);
242                 return property;
243             });
244     }
245
246     createServiceAttribute(componentId: string, attributeModel: AttributeBEModel): Observable<AttributeBEModel> {
247         const serverObject = {};
248         serverObject[attributeModel.name] = attributeModel;
249         return this.http.post<AttributeBEModel>(this.baseUrl + 'services/' + componentId + '/attributes', serverObject)
250             .map((res) => {
251                 const attribute: AttributeBEModel = new AttributeBEModel(res);
252                 return attribute;
253             });
254     }
255
256     getServiceProperties(componentId: string): Observable<PropertyBEModel[]> {
257         return this.http.get<any>(this.baseUrl + 'services/' + componentId + '/properties')
258             .map((res) => {
259                 if (!res) {
260                     return new Array<PropertyBEModel>();
261                 }
262                 return CommonUtils.initBeProperties(res);
263             });
264     }
265
266     getServiceAttributes(componentId: string): Observable<AttributeBEModel[]> {
267         return this.http.get<any>(this.baseUrl + 'services/' + componentId + '/attributes')
268             .map((res) => {
269                 if (!res) {
270                     return new Array<AttributeBEModel>();
271                 }
272                 return CommonUtils.initAttributes(res);
273             });
274     }
275
276     updateServiceProperties(componentId: string, properties: PropertyBEModel[]) {
277         return this.http.put<any>( this.baseUrl + 'services/' + componentId + '/properties', properties)
278             .map((res) => {
279                 const resJson = res;
280                 return _.map(resJson,
281                     (resValue: PropertyBEModel) => new PropertyBEModel(resValue));
282             });
283     }
284
285     updateServiceAttributes(componentId: string, attributes: AttributeBEModel[]) {
286         return this.http.put<any>( this.baseUrl + 'services/' + componentId + '/attributes', attributes)
287             .map((res) => {
288                 const resJson = res;
289                 return _.map(resJson,
290                     (resValue: AttributeBEModel) => new AttributeBEModel(resValue));
291             });
292     }
293
294     deleteServiceProperty(componentId: string, property: PropertyBEModel): Observable<string> {
295         return this.http.delete(this.baseUrl + 'services/' + componentId + '/properties/' + property.uniqueId )
296             .map((res: Response) => {
297                 return property.uniqueId;
298             });
299     }
300
301     createServiceInput(componentId: string, inputModel: InputBEModel): Observable<InputBEModel> {
302         const serverObject = {};
303         serverObject[inputModel.name] = inputModel;
304         return this.http.post<InputBEModel>(this.baseUrl + 'services/' + componentId + '/create/input', serverObject)
305             .map((res) => {
306                 const input: InputBEModel = new InputBEModel(res);
307                 return input;
308             });
309     }
310
311     deleteServiceAttribute(componentId: string, attribute: AttributeBEModel): Observable<string> {
312         return this.http.delete(this.baseUrl + 'services/' + componentId + '/attributes/' + attribute.uniqueId )
313             .map((res: Response) => {
314                 return attribute.uniqueId;
315             });
316     }
317
318     getDependencies(componentType: string, componentId: string): Observable<IDependenciesServerResponse[]> {
319         return this.http.get<IDependenciesServerResponse[]>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/dependencies');
320     }
321
322     automatedUpgrade(componentType: string, componentId: string, componentsIdsToUpgrade: IAutomatedUpgradeRequestObj[]): Observable<AutomatedUpgradeGenericResponse> {
323         return this.http.post<AutomatedUpgradeGenericResponse>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/automatedupgrade', componentsIdsToUpgrade);
324     }
325
326     updateComponentInstance(componentMetaDataId: string, componentType: string, componentInstance:ComponentInstance): Observable<ComponentInstance> {
327         return this.http.post<ComponentInstance>(this.baseUrl + this.getServerTypeUrl(componentType) + componentMetaDataId + '/resourceInstance/' + componentInstance.uniqueId, componentInstance);
328     }
329
330     updateMultipleComponentInstances(componentId: string, componentType: string, instances: ComponentInstance[]): Observable<ComponentInstance[]> {
331         return this.http.post<ComponentInstance[]>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/resourceInstance/multipleComponentInstance', instances)
332             .map((res) => {
333                 return CommonUtils.initComponentInstances(res);
334             });
335     }
336
337     createRelation(componentId: string, componentType: string, link: RelationshipModel): Observable<RelationshipModel> {
338         return this.http.post<RelationshipModel>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/resourceInstance/associate', link)
339             .map((res) => {
340                 return new RelationshipModel(res);
341             });
342     }
343
344     deleteRelation(componentId: string, componentType: string, link: RelationshipModel): Observable<RelationshipModel> {
345         return this.http.put<RelationshipModel>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/resourceInstance/dissociate', link)
346             .map((res) => {
347                 return new RelationshipModel(res);
348             });
349     }
350
351     createComponentInstance(componentType: string, componentId: string, componentInstance: ComponentInstance): Observable<ComponentInstance> {
352         return this.http.post<ComponentInstance>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/resourceInstance', componentInstance)
353             .map((res) => {
354                 return new ComponentInstance(res);
355             });
356     }
357
358     deleteComponentInstance(componentType: string, componentId: string, componentInstanceId: string): Observable<ComponentInstance> {
359         return this.http.delete<ComponentInstance>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/resourceInstance/' + componentInstanceId)
360             .map((res) => {
361                 return new ComponentInstance(res);
362             });
363     }
364
365     fetchRelation(componentType: string, componentId: string, linkId: string): Observable<RelationshipModel> {
366         return this.http.get<RelationshipModel>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/relationId/' + linkId)
367             .map((res) => {
368                 return new RelationshipModel(res);
369             });
370     }
371
372     addOrUpdateArtifact = (componentType: string, componentId: string, artifact: ArtifactModel): Observable<ArtifactModel> => {
373         let headerObj: HttpHeaders = new HttpHeaders();
374         if (artifact.payloadData) {
375             headerObj = headerObj.append('Content-MD5', HttpHelperService.getHeaderMd5(artifact));
376         }
377
378         let artifactID: string = '';
379         if (artifact.uniqueId) {
380             artifactID = '/' + artifact.uniqueId;
381         }
382         return this.http.post<ArtifactModel>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/artifacts' + artifactID, JSON.stringify(artifact), {headers: headerObj}).map(
383             (res) => new ArtifactModel(res)
384         );
385     }
386
387     deleteArtifact = (componentId: string, componentType: string, artifactId: string, artifactLabel: string): Observable<ArtifactModel> => {
388         return this.http.delete<ArtifactModel>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/artifacts/' + artifactId + '?operation=' + artifactLabel)
389             .map((res) => new ArtifactModel(res));
390     }
391
392     downloadArtifact = (componentType: string, componentId: string, artifactId: string): Observable<IFileDownload> => {
393         return this.http.get<IFileDownload>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/artifacts/' + artifactId);
394     }
395
396     // ------------------------------------------------ Properties API --------------------------------------------------//
397     addProperty = (componentType: string, componentId: string, property: PropertyModel):Observable<PropertyModel> => {
398         return this.http.post<PropertyModel>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/properties', property.convertToServerObject())
399         .map((response) => {
400             return new PropertyModel(response);
401         });
402     }
403
404     updateProperty = (componentType: string, componentId: string, property: PropertyModel): Observable<PropertyModel> => {
405         const propertiesList: PropertyBEModel[] = [property];
406         return this.http.put<PropertyModel>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/properties', propertiesList)
407         .map((response) => {
408             return new PropertyModel(response[Object.keys(response)[0]]);
409         });
410     }
411
412     deleteProperty = (componentType: string, componentId: string, propertyId: string): Observable<PropertyModel> => {
413         return this.http.delete<PropertyModel>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/properties/' + propertyId);
414     }
415
416     // ------------------------------------------------ Attributes API --------------------------------------------------//
417     addAttribute = (componentType: string, componentId: string, attribute: AttributeModel): Observable<AttributeModel> => {
418         return this.http.post<AttributeModel>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/attributes', attribute.convertToServerObject())
419             .map((response) => {
420                 return new AttributeModel(response);
421             });
422     }
423
424     updateAttribute = (componentType: string, componentId: string, attribute: AttributeModel): Observable<AttributeModel> => {
425         const payload = attribute.convertToServerObject();
426
427         return this.http.put<AttributeModel>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/attributes/' + attribute.uniqueId, payload)
428             .map((response) => {
429                 return new AttributeModel(response);
430             });
431     }
432
433     // Async Methods
434     addAttributeAsync = async (componentType: string, componentId: string, attribute: AttributeModel): Promise<AttributeModel> => {
435         return this.addAttribute(componentType, componentId, attribute).toPromise();
436     }
437
438     updateAttributeAsync = async (componentType: string, componentId: string, attribute: AttributeModel): Promise<AttributeModel> => {
439         return this.updateAttribute(componentType, componentId, attribute).toPromise();
440     }
441
442     deleteAttributeAsync = async (componentType: string, componentId: string, attribute: AttributeModel): Promise<any> => {
443         return this.http.delete<any>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/attributes/' + attribute.uniqueId, {}).toPromise();
444     }
445
446     getArtifactsByType(componentType: string, componentId: string, artifactsType: ArtifactGroupType) {
447         return this.getComponentDataByFieldsName(componentType, componentId, [this.convertArtifactTypeToUrl(artifactsType)]);
448     }
449
450     getServiceConsumptionData(componentType: string, componentId: string): Observable<ComponentGenericResponse> {
451         return this.getComponentDataByFieldsName(componentType, componentId, [
452             // COMPONENT_FIELDS.COMPONENT_INSTANCES_INTERFACES,
453             COMPONENT_FIELDS.COMPONENT_INSTANCES_PROPERTIES,
454             // COMPONENT_FIELDS.COMPONENT_INSTANCES_INPUTS,
455             COMPONENT_FIELDS.COMPONENT_INPUTS,
456             COMPONENT_FIELDS.COMPONENT_INSTANCES,
457             COMPONENT_FIELDS.COMPONENT_CAPABILITIES
458         ]);
459     }
460
461     getServiceConsumptionInputs(componentMetaDataId: string, serviceInstanceId: string, interfaceId: string, operation: OperationModel): Observable<ConsumptionInput[]> {
462         return this.http.get<ConsumptionInput[]>
463         (this.baseUrl + 'services/' + componentMetaDataId + '/consumption/' + serviceInstanceId + '/interfaces/' + interfaceId + '/operations/' + operation.uniqueId + '/inputs');
464     }
465
466     createOrUpdateServiceConsumptionInputs(componentMetaDataId: string, serviceInstanceId: string, consumptionInputsList: Array<{[id: string]: ConsumptionInput[]}>): Observable<any> {
467         return this.http.post(this.baseUrl + 'services/' + componentMetaDataId + '/consumption/' + serviceInstanceId, consumptionInputsList);
468     }
469
470     getServiceFilterConstraints(componentType: string, componentId: string): Observable<ComponentGenericResponse> {
471         return this.getComponentDataByFieldsName(componentType, componentId, [SERVICE_FIELDS.NODE_FILTER]);
472     }
473
474     getSubstitutionFilterConstraints(componentType: string, componentId: string): Observable<ComponentGenericResponse> {
475         return this.getComponentDataByFieldsName(componentType, componentId, [SERVICE_FIELDS.SUBSTITUTION_FILTER]);
476     }
477
478     getComponentInstanceProperties(componentType: string, componentId: string): Observable<ComponentGenericResponse> {
479         return this.getComponentDataByFieldsName(componentType, componentId, [COMPONENT_FIELDS.COMPONENT_INSTANCES_PROPERTIES]);
480     }
481
482     findAllComponentInstanceAttributes(componentType: string, componentId: string): Observable<ComponentGenericResponse> {
483         return this.getComponentDataByFieldsName(componentType, componentId, [COMPONENT_FIELDS.COMPONENT_INSTANCES_ATTRIBUTES]);
484     }
485
486     getComponentInstanceCapabilityProperties(componentType: string, componentId: string): Observable<ComponentGenericResponse> {
487         return this.getComponentDataByFieldsName(componentType, componentId,
488             [COMPONENT_FIELDS.COMPONENT_CAPABILITIES, COMPONENT_FIELDS.COMPONENT_CAPABILITIES_PROPERTIES]);
489     }
490
491     createServiceFilterConstraints(componentMetaDataId: string, componentInstanceId: string, constraint: FilterConstraint, componentType: string, constraintType: string): Observable<any> {
492         return this.http.post<any>(this.baseUrl + this.getServerTypeUrl(componentType) + componentMetaDataId + '/componentInstance/' + componentInstanceId + '/' + constraintType + '/nodeFilter', constraint);
493     }
494
495     createServiceFilterCapabilitiesConstraints(componentMetaDataId: string, componentInstanceId: string, constraint: FilterConstraint, componentType: string, constraintType: string): Observable<any> {
496         return this.http.post<any>(this.baseUrl + this.getServerTypeUrl(componentType) + componentMetaDataId + '/componentInstance/' + componentInstanceId + '/' + constraintType + '/nodeFilter', constraint);
497     }
498
499     updateServiceFilterConstraints(componentMetaDataId: string, componentInstanceId: string, constraint: FilterConstraint, componentType: string, constraintType: string, constraintIndex: number):Observable<any>{
500         return this.http.put<any>(this.baseUrl + this.getServerTypeUrl(componentType) + componentMetaDataId + '/componentInstance/' + componentInstanceId + '/' + constraintType + '/' + constraintIndex + '/nodeFilter', constraint)
501     }
502
503     updateServiceFilterCapabilitiesConstraint(componentMetaDataId: string, componentInstanceId: string, constraints: FilterConstraint, componentType: string, constraintType: string, constraintIndex: number):Observable<any>{
504         return this.http.put<any>(this.baseUrl + this.getServerTypeUrl(componentType) + componentMetaDataId + '/componentInstance/' + componentInstanceId + '/' + constraintType + '/' + constraintIndex + '/nodeFilter', constraints)
505     }
506
507     deleteServiceFilterConstraints(componentMetaDataId: string, componentInstanceId: string, constraintIndex: number, componentType: string, constraintType: string): Observable<any>{
508         return this.http.delete<any>(this.baseUrl + this.getServerTypeUrl(componentType) + componentMetaDataId + '/componentInstance/' + componentInstanceId + '/' + constraintType + '/' + constraintIndex + '/nodeFilter')
509     }
510
511     getComponentPropertiesAndInputsForSubstitutionFilter(componentType: string, componentId: string): Observable<ComponentGenericResponse> {
512         return this.getComponentDataByFieldsName(componentType, componentId, [COMPONENT_FIELDS.COMPONENT_PROPERTIES, COMPONENT_FIELDS.COMPONENT_INPUTS]);
513     }
514
515     createSubstitutionFilterConstraints(componentMetaDataId: string, constraint: FilterConstraint, componentType: string, constraintType: string): Observable<any> {
516         return this.http.post<any>(this.baseUrl + this.getServerTypeUrl(componentType) + componentMetaDataId + '/substitutionFilter/' + constraintType, constraint);
517     }
518
519     updateSubstitutionFilterConstraint(componentId: string, constraint: FilterConstraint, componentType: string, constraintType: string,
520                                        index: number): Observable<any> {
521         const url = `${this.baseUrl}${this.getServerTypeUrl(componentType)}${componentId}/substitutionFilter/${constraintType}/${index}`;
522         return this.http.put<any>(url, constraint);
523     }
524
525     deleteSubstitutionFilterConstraints(componentMetaDataId: string, constraintIndex: number, componentType: string, constraintType: string): Observable<any>{
526         return this.http.delete<any>(this.baseUrl + this.getServerTypeUrl(componentType) + componentMetaDataId + '/substitutionFilter/' + constraintType + "/" + constraintIndex)
527     }
528
529     deletePolicy(component: Component, policy: PolicyInstance): Observable<PolicyInstance> {
530         return this.http.put<PolicyInstance>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/policies/' + policy.uniqueId + '/undeclare', policy)
531     }
532
533     createListInput(component: Component, input: any, isSelf: boolean): Observable<any> {
534         let inputs: any;
535         if (isSelf) {
536             // change componentInstanceProperties -> serviceProperties
537             inputs = {
538                 componentInstInputsMap: {
539                     serviceProperties: input.componentInstInputsMap.componentInstanceProperties
540                 },
541                 listInput: input.listInput
542             };
543         } else {
544             inputs = input;
545         }
546         return this.http.post<any>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/create/listInput', inputs);
547     }
548
549     createPolicy(component: Component, policiesToCreate: InstancePropertiesAPIMap, isSelf: boolean): Observable<any> {
550         const policiesList =
551             isSelf ?
552                 // tslint:disable-next-line:object-literal-key-quotes
553                 {'componentPropertiesToPolicies': {
554                         ...policiesToCreate.componentInstanceProperties
555                     }
556                 } :
557                 // tslint:disable-next-line:object-literal-key-quotes
558                 {'componentInstancePropertiesToPolicies': {
559                         ...policiesToCreate.componentInstanceProperties
560                     }
561                 };
562         return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/create/policies', policiesList);
563     }
564
565     protected getComponentDataByFieldsName(componentType: string, componentId: string, fields: string[]): Observable<ComponentGenericResponse> {
566         let params: HttpParams = new HttpParams();
567         _.forEach(fields, (field: string): void => {
568             params = params.append(API_QUERY_PARAMS.INCLUDE, field);
569         });
570         // tslint:disable-next-line:object-literal-shorthand
571         return this.http.get<ComponentGenericResponse>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/filteredDataByParams', {params: params})
572             .map((res) => {
573                 return componentType === ComponentType.SERVICE ? new ServiceGenericResponse().deserialize(res) :
574                         new ComponentGenericResponse().deserialize(res);
575             });
576     }
577
578     private getServerTypeUrl = (componentType: string): string => {
579         switch (componentType) {
580             case ComponentType.SERVICE:
581             case ComponentType.SERVICE_PROXY:
582             case ComponentType.SERVICE_SUBSTITUTION:
583                 return ServerTypeUrl.SERVICES;
584             default:
585                 return ServerTypeUrl.RESOURCES;
586         }
587     }
588
589     private convertArtifactTypeToUrl = (artifactType: ArtifactGroupType): string => {
590         switch (artifactType) {
591             case ArtifactGroupType.TOSCA:
592                 return COMPONENT_FIELDS.COMPONENT_TOSCA_ARTIFACTS;
593             case ArtifactGroupType.INFORMATION:
594                 return COMPONENT_FIELDS.COMPONENT_INFORMATIONAL_ARTIFACTS;
595             case ArtifactGroupType.DEPLOYMENT:
596                 return COMPONENT_FIELDS.COMPONENT_DEPLOYMENT_ARTIFACTS;
597             case ArtifactGroupType.SERVICE_API:
598                 return COMPONENT_FIELDS.SERVICE_API_ARTIFACT;
599         }
600     }
601
602     // createCapability(component: Component, capabilityData: Capability): Observable<Capability[]> {
603     createCapability(type: string, uniqueId: string, capabilityData: Capability): Observable<Capability[]> {
604         let capBEObj = {
605             'capabilities': {
606                 [capabilityData.type]: [capabilityData]
607             }
608         };
609         return this.http.post<any>(this.baseUrl + type + uniqueId + '/capabilities', capBEObj);
610     }
611
612     updateCapability(type: string, uniqueId: string, capabilityData: Capability): Observable<Capability[]> {
613         let capBEObj = {
614             'capabilities': {
615                 [capabilityData.type]: [capabilityData]
616             }
617         };
618         return this.http.put<any>(this.baseUrl + type + uniqueId + '/capabilities', capBEObj);
619     }
620
621     deleteCapability(component: Component, capId: string): Observable<Capability> {
622         return this.http.delete<Capability>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/capabilities/' + capId);
623     }
624
625     createRequirement(type: string, uniqueId: string, requirementData: Requirement): Observable<any> {
626         let reqBEObj = {
627             'requirements': {
628                 [requirementData.capability]: [requirementData]
629             }
630         };
631         return this.http.post(this.baseUrl + type + uniqueId + '/requirements', reqBEObj);
632     }
633
634     updateRequirement(type: string, uniqueId: string, requirementData: Requirement): Observable<any> {
635         let reqBEObj = {
636             'requirements': {
637                 [requirementData.capability]: [requirementData]
638             }
639         };
640         return this.http.put(this.baseUrl + type + uniqueId + '/requirements', reqBEObj);
641     }
642
643     deleteRequirement(component: Component, reqId: string): Observable<Requirement> {
644         return this.http.delete<Requirement>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/requirements/' + reqId);
645     }
646
647     getDirectiveList(): Observable<string[]> {
648         return this.http.get<ListDirectiveResponse>(this.baseUrl + "directives")
649         .pipe(map(response => response.directives));
650     }
651
652     updateComponentInstanceInterfaceOperation(componentMetaDataId: string,
653                                               componentMetaDataType: string,
654                                               componentInstanceId: string,
655                                               operation: InterfaceOperationModel): Observable<ComponentInstance> {
656         const operationList = {
657             interfaces: {
658                 [operation.interfaceType]: {
659                     type: operation.interfaceType,
660                     operations: {
661                         [operation.name]: new BEInterfaceOperationModel(operation)
662                     }
663                 }
664             }
665         };
666         return this.http.put<ComponentInstance>(this.baseUrl + this
667         .getServerTypeUrl(componentMetaDataType) + componentMetaDataId + '/componentInstance/' + componentInstanceId + '/interfaceOperation', operationList);
668     }
669
670     getDefaultCustomFunction(type='ALL'): Observable<CustomToscaFunction[]> {
671         return this.http.get<DefaultCustomFunctions>(this.baseUrl + "customToscaFunctions/" + type)
672         .pipe(map(response => response && response.defaultCustomToscaFunction  ? response.defaultCustomToscaFunction : []));
673     }
674
675 }