Allow to select properties in the get_attribute function
[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  * ================================================================================
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  *      http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ============LICENSE_END=========================================================
22  */
23
24 import * as _ from "lodash";
25 import {Inject, Injectable} from '@angular/core';
26 import {Observable} from 'rxjs/Observable';
27 import 'rxjs/add/operator/map';
28 import 'rxjs/add/operator/toPromise';
29 import {
30     ArtifactModel,
31     AttributeModel,
32     Capability,
33     Component,
34     FilterPropertiesAssignmentData,
35     IFileDownload,
36     InputBEModel,
37     InstancePropertiesAPIMap,
38     OperationModel,
39     PropertyModel,
40     Requirement
41 } from "app/models";
42 import {API_QUERY_PARAMS, ArtifactGroupType, COMPONENT_FIELDS} from "app/utils";
43 import {ComponentGenericResponse} from "../responses/component-generic-response";
44 import {InstanceBePropertiesMap} from "../../../models/properties-inputs/property-fe-map";
45 import {ComponentType, ServerTypeUrl, SERVICE_FIELDS} from "../../../utils/constants";
46 import {ISdcConfig, SdcConfigToken} from "../../config/sdc-config.config";
47 import {IDependenciesServerResponse} from "../responses/dependencies-server-response";
48 import {AutomatedUpgradeGenericResponse} from "../responses/automated-upgrade-response";
49 import {IAutomatedUpgradeRequestObj} from "../../pages/automated-upgrade/automated-upgrade.service";
50 import {ComponentInstance} from "../../../models/componentsInstances/componentInstance";
51 import {CommonUtils} from "../../../utils/common-utils";
52 import {RelationshipModel} from "../../../models/graph/relationship";
53 import {ServiceGenericResponse} from "../responses/service-generic-response";
54 import {HttpClient, HttpHeaders, HttpParams} from "@angular/common/http";
55 import {HttpHelperService} from "../http-hepler.service";
56 import {ConsumptionInput} from "../../components/logic/service-consumption/service-consumption.component";
57 import {ConstraintObject} from "../../components/logic/service-dependencies/service-dependencies.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 {CapabilitiesConstraintObject} from "../../components/logic/capabilities-constraint/capabilities-constraint.component";
62 import {BEInterfaceOperationModel, InterfaceOperationModel} from "../../../models/interfaceOperation";
63 import {AttributeBEModel} from "../../../models/attributes-outputs/attribute-be-model";
64 import {InstanceAttributesAPIMap} from "../../../models/attributes-outputs/attribute-fe-map";
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: ConstraintObject, 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: CapabilitiesConstraintObject, 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, constraints: ConstraintObject, 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', constraints)
490     }
491
492     updateServiceFilterCapabilitiesConstraint(componentMetaDataId: string, componentInstanceId: string, constraints: CapabilitiesConstraintObject, 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: ConstraintObject, componentType: string, constraintType: string): Observable<any> {
505         return this.http.post<any>(this.baseUrl + this.getServerTypeUrl(componentType) + componentMetaDataId + '/substitutionFilter/' + constraintType, constraint);
506     }
507
508     updateSubstitutionFilterConstraints(componentMetaDataId: string, constraint: ConstraintObject[], componentType: string, constraintType: string): Observable<any>{
509         return this.http.put<any>(this.baseUrl + this.getServerTypeUrl(componentType) + componentMetaDataId + '/substitutionFilter/' + constraintType, constraint);
510     }
511
512     deleteSubstitutionFilterConstraints(componentMetaDataId: string, constraintIndex: number, componentType: string, constraintType: string): Observable<any>{
513         return this.http.delete<any>(this.baseUrl + this.getServerTypeUrl(componentType) + componentMetaDataId + '/substitutionFilter/' + constraintType + "/" + constraintIndex)
514     }
515
516     deletePolicy(component: Component, policy: PolicyInstance): Observable<PolicyInstance> {
517         return this.http.put<PolicyInstance>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/policies/' + policy.uniqueId + '/undeclare', policy)
518     }
519
520     createListInput(component: Component, input: any, isSelf: boolean): Observable<any> {
521         let inputs: any;
522         if (isSelf) {
523             // change componentInstanceProperties -> serviceProperties
524             inputs = {
525                 componentInstInputsMap: {
526                     serviceProperties: input.componentInstInputsMap.componentInstanceProperties
527                 },
528                 listInput: input.listInput
529             };
530         } else {
531             inputs = input;
532         }
533         return this.http.post<any>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/create/listInput', inputs);
534     }
535
536     createPolicy(component: Component, policiesToCreate: InstancePropertiesAPIMap, isSelf: boolean): Observable<any> {
537         const policiesList =
538             isSelf ?
539                 // tslint:disable-next-line:object-literal-key-quotes
540                 {'componentPropertiesToPolicies': {
541                         ...policiesToCreate.componentInstanceProperties
542                     }
543                 } :
544                 // tslint:disable-next-line:object-literal-key-quotes
545                 {'componentInstancePropertiesToPolicies': {
546                         ...policiesToCreate.componentInstanceProperties
547                     }
548                 };
549         return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/create/policies', policiesList);
550     }
551
552     protected getComponentDataByFieldsName(componentType: string, componentId: string, fields: string[]): Observable<ComponentGenericResponse> {
553         let params: HttpParams = new HttpParams();
554         _.forEach(fields, (field: string): void => {
555             params = params.append(API_QUERY_PARAMS.INCLUDE, field);
556         });
557         // tslint:disable-next-line:object-literal-shorthand
558         return this.http.get<ComponentGenericResponse>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/filteredDataByParams', {params: params})
559             .map((res) => {
560                 return componentType === ComponentType.SERVICE ? new ServiceGenericResponse().deserialize(res) :
561                         new ComponentGenericResponse().deserialize(res);
562             });
563     }
564
565     private getServerTypeUrl = (componentType: string): string => {
566         switch (componentType) {
567             case ComponentType.SERVICE:
568             case ComponentType.SERVICE_PROXY:
569             case ComponentType.SERVICE_SUBSTITUTION:
570                 return ServerTypeUrl.SERVICES;
571             default:
572                 return ServerTypeUrl.RESOURCES;
573         }
574     }
575
576     private convertArtifactTypeToUrl = (artifactType: ArtifactGroupType): string => {
577         switch (artifactType) {
578             case ArtifactGroupType.TOSCA:
579                 return COMPONENT_FIELDS.COMPONENT_TOSCA_ARTIFACTS;
580             case ArtifactGroupType.INFORMATION:
581                 return COMPONENT_FIELDS.COMPONENT_INFORMATIONAL_ARTIFACTS;
582             case ArtifactGroupType.DEPLOYMENT:
583                 return COMPONENT_FIELDS.COMPONENT_DEPLOYMENT_ARTIFACTS;
584             case ArtifactGroupType.SERVICE_API:
585                 return COMPONENT_FIELDS.SERVICE_API_ARTIFACT;
586         }
587     }
588
589     // createCapability(component: Component, capabilityData: Capability): Observable<Capability[]> {
590     createCapability(type: string, uniqueId: string, capabilityData: Capability): Observable<Capability[]> {
591         let capBEObj = {
592             'capabilities': {
593                 [capabilityData.type]: [capabilityData]
594             }
595         };
596         return this.http.post<any>(this.baseUrl + type + uniqueId + '/capabilities', capBEObj);
597     }
598
599     updateCapability(type: string, uniqueId: string, capabilityData: Capability): Observable<Capability[]> {
600         let capBEObj = {
601             'capabilities': {
602                 [capabilityData.type]: [capabilityData]
603             }
604         };
605         return this.http.put<any>(this.baseUrl + type + uniqueId + '/capabilities', capBEObj);
606     }
607
608     deleteCapability(component: Component, capId: string): Observable<Capability> {
609         return this.http.delete<Capability>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/capabilities/' + capId);
610     }
611
612     createRequirement(type: string, uniqueId: string, requirementData: Requirement): Observable<any> {
613         let reqBEObj = {
614             'requirements': {
615                 [requirementData.capability]: [requirementData]
616             }
617         };
618         return this.http.post(this.baseUrl + type + uniqueId + '/requirements', reqBEObj);
619     }
620
621     updateRequirement(type: string, uniqueId: string, requirementData: Requirement): Observable<any> {
622         let reqBEObj = {
623             'requirements': {
624                 [requirementData.capability]: [requirementData]
625             }
626         };
627         return this.http.put(this.baseUrl + type + uniqueId + '/requirements', reqBEObj);
628     }
629
630     deleteRequirement(component: Component, reqId: string): Observable<Requirement> {
631         return this.http.delete<Requirement>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/requirements/' + reqId);
632     }
633
634     getDirectiveList(): Observable<string[]> {
635         return this.http.get<ListDirectiveResponse>(this.baseUrl + "directives")
636         .pipe(map(response => response.directives));
637     }
638
639     updateComponentInstanceInterfaceOperation(componentMetaDataId: string,
640                                               componentMetaDataType: string,
641                                               componentInstanceId: string,
642                                               operation: InterfaceOperationModel): Observable<ComponentInstance> {
643         const operationList = {
644             interfaces: {
645                 [operation.interfaceType]: {
646                     type: operation.interfaceType,
647                     operations: {
648                         [operation.name]: new BEInterfaceOperationModel(operation)
649                     }
650                 }
651             }
652         };
653         return this.http.put<ComponentInstance>(this.baseUrl + this
654         .getServerTypeUrl(componentMetaDataType) + componentMetaDataId + '/componentInstance/' + componentInstanceId + '/interfaceOperation', operationList);
655     }
656
657 }