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