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