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