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