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