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