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