Raise JUnit coverage common-be
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / servlets / ComponentInstanceServlet.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.servlets;
22
23 import java.io.InputStream;
24 import java.lang.reflect.Type;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.List;
28 import java.util.Set;
29
30 import javax.inject.Singleton;
31 import javax.servlet.ServletContext;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.ws.rs.Consumes;
34 import javax.ws.rs.DELETE;
35 import javax.ws.rs.GET;
36 import javax.ws.rs.HeaderParam;
37 import javax.ws.rs.POST;
38 import javax.ws.rs.PUT;
39 import javax.ws.rs.Path;
40 import javax.ws.rs.PathParam;
41 import javax.ws.rs.Produces;
42 import javax.ws.rs.QueryParam;
43 import javax.ws.rs.core.Context;
44 import javax.ws.rs.core.MediaType;
45 import javax.ws.rs.core.Response;
46
47 import org.apache.commons.io.IOUtils;
48 import org.apache.commons.lang.StringUtils;
49 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
50 import org.openecomp.sdc.be.components.impl.GroupBusinessLogic;
51 import org.openecomp.sdc.be.config.BeEcompErrorManager;
52 import org.openecomp.sdc.be.dao.api.ActionStatus;
53 import org.openecomp.sdc.be.datamodel.ForwardingPaths;
54 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
55 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
56 import org.openecomp.sdc.be.info.CreateAndAssotiateInfo;
57 import org.openecomp.sdc.be.info.GroupDefinitionInfo;
58 import org.openecomp.sdc.be.model.ComponentInstance;
59 import org.openecomp.sdc.be.model.ComponentInstanceInput;
60 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
61 import org.openecomp.sdc.be.model.PropertyConstraint;
62 import org.openecomp.sdc.be.model.RequirementCapabilityRelDef;
63 import org.openecomp.sdc.be.model.Resource;
64 import org.openecomp.sdc.be.model.User;
65 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation.PropertyConstraintDeserialiser;
66 import org.openecomp.sdc.common.api.Constants;
67 import org.openecomp.sdc.common.datastructure.Wrapper;
68 import org.openecomp.sdc.exception.ResponseFormat;
69 import org.slf4j.Logger;
70 import org.slf4j.LoggerFactory;
71
72 import com.fasterxml.jackson.databind.ObjectMapper;
73 import com.google.gson.Gson;
74 import com.google.gson.GsonBuilder;
75 import com.google.gson.reflect.TypeToken;
76 import com.jcabi.aspects.Loggable;
77
78 import fj.data.Either;
79 import io.swagger.annotations.Api;
80 import io.swagger.annotations.ApiOperation;
81 import io.swagger.annotations.ApiParam;
82 import io.swagger.annotations.ApiResponse;
83 import io.swagger.annotations.ApiResponses;
84
85 /**
86  * Root resource (exposed at "/" path) .json
87  */
88 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
89 @Path("/v1/catalog")
90 @Api(value = "Resource Instance Servlet")
91 @Singleton
92 public class ComponentInstanceServlet extends AbstractValidationsServlet {
93
94     private static final Logger log = LoggerFactory.getLogger(ComponentInstanceServlet.class);
95
96     Type constraintType = new TypeToken<PropertyConstraint>() {
97     }.getType();
98
99     Gson gson = new GsonBuilder().registerTypeAdapter(constraintType, new PropertyConstraintDeserialiser()).create();
100
101     @POST
102     @Path("/{containerComponentType}/{componentId}/resourceInstance")
103     @Consumes(MediaType.APPLICATION_JSON)
104     @Produces(MediaType.APPLICATION_JSON)
105     @ApiOperation(value = "Create ComponentInstance", httpMethod = "POST", notes = "Returns created ComponentInstance", response = Response.class)
106     @ApiResponses(value = { @ApiResponse(code = 201, message = "Component created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
107             @ApiResponse(code = 409, message = "Component instance already exist") })
108     public Response createComponentInstance(@ApiParam(value = "RI object to be created", required = true) String data, @PathParam("componentId") final String containerComponentId,
109             @ApiParam(value = "valid values: resources / services", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
110             @HeaderParam(value = Constants.USER_ID_HEADER) @ApiParam(value = "USER_ID of modifier user", required = true) String userId, @Context final HttpServletRequest request) {
111         ServletContext context = request.getSession().getServletContext();
112
113         try {
114
115             ComponentInstance componentInstance = RepresentationUtils.fromRepresentation(data, ComponentInstance.class);
116             componentInstance.setInvariantName(null);
117             ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
118             ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
119             if (componentInstanceLogic == null) {
120                 log.debug("Unsupported component type {}", containerComponentType);
121                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
122             }
123             Either<ComponentInstance, ResponseFormat> actionResponse = componentInstanceLogic.createComponentInstance(containerComponentType, containerComponentId, userId, componentInstance);
124
125             if (actionResponse.isRight()) {
126                 return buildErrorResponse(actionResponse.right().value());
127             }
128             return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.CREATED), actionResponse.left().value());
129
130         } catch (Exception e) {
131             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Create Component Instance");
132             log.debug("create component instance failed with exception", e);
133             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
134         }
135     }
136
137     @POST
138     @Path("/{containerComponentType}/{componentId}/resourceInstance/{componentInstanceId}")
139     @Consumes(MediaType.APPLICATION_JSON)
140     @Produces(MediaType.APPLICATION_JSON)
141     @ApiOperation(value = "Update resource instance", httpMethod = "POST", notes = "Returns updated resource instance", response = Response.class)
142     @ApiResponses(value = { @ApiResponse(code = 200, message = "Resource instance updated"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
143     public Response updateComponentInstanceMetadata(@PathParam("componentId") final String componentId, @PathParam("componentInstanceId") final String componentInstanceId,
144             @ApiParam(value = "valid values: resources / services / products", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME + ","
145                     + ComponentTypeEnum.PRODUCT_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
146             @Context final HttpServletRequest request) {
147         ServletContext context = request.getSession().getServletContext();
148
149         String url = request.getMethod() + " " + request.getRequestURI();
150         log.debug("Start handle request of {}", url);
151         try {
152
153             log.debug("Start handle request of {}", url);
154
155             InputStream inputStream = request.getInputStream();
156
157             byte[] bytes = IOUtils.toByteArray(inputStream);
158
159             if (bytes == null || bytes.length == 0) {
160                 log.info("Empty body was sent.");
161                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT));
162             }
163
164             String userId = request.getHeader(Constants.USER_ID_HEADER);
165
166             String data = new String(bytes);
167             ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
168             ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
169             if (componentInstanceLogic == null) {
170                 log.debug("Unsupported component type {}", containerComponentType);
171                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
172             }
173             Either<ComponentInstance, ResponseFormat> convertResponse = convertToResourceInstance(data);
174
175             if (convertResponse.isRight()) {
176                 BeEcompErrorManager.getInstance().logBeSystemError("Resource Instance - updateResourceInstance");
177                 log.debug("Failed to convert received data to BE format.");
178                 return buildErrorResponse(convertResponse.right().value());
179             }
180
181             ComponentInstance resourceInstance = convertResponse.left().value();
182             Either<ComponentInstance, ResponseFormat> actionResponse = componentInstanceLogic.updateComponentInstanceMetadata(containerComponentType, componentId, componentInstanceId, userId, resourceInstance);
183
184             if (actionResponse.isRight()) {
185                 return buildErrorResponse(actionResponse.right().value());
186             }
187             return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse.left().value());
188
189         } catch (Exception e) {
190             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Update Resource Instance");
191             log.debug("update resource instance with exception", e);
192             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
193         }
194
195     }
196
197     @POST
198     @Path("/{containerComponentType}/{componentId}/resourceInstance/multipleComponentInstance")
199     @Consumes(MediaType.APPLICATION_JSON)
200     @Produces(MediaType.APPLICATION_JSON)
201     @ApiOperation(value = "Update resource instance multiple component", httpMethod = "POST", notes = "Returns updated resource instance", response = Response.class)
202     @ApiResponses(value = { @ApiResponse(code = 200, message = "Resource instance updated"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
203     public Response updateMultipleComponentInstance(@PathParam("componentId") final String componentId,
204             @ApiParam(value = "valid values: resources / services / products", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME + ","
205                     + ComponentTypeEnum.PRODUCT_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
206             @Context final HttpServletRequest request, @ApiParam(value = "Component Instance JSON Array", required = true) final String componentInstanceJsonArray) {
207
208         ServletContext context = request.getSession().getServletContext();
209         String url = request.getMethod() + " " + request.getRequestURI();
210         log.debug("Start handle request of {}", url);
211
212         try {
213             log.debug("Start handle request of {}", url);
214
215             if (componentInstanceJsonArray == null || componentInstanceJsonArray.length() == 0) {
216                 log.info("Empty JSON list was sent.");
217                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT));
218             }
219
220             String userId = request.getHeader(Constants.USER_ID_HEADER);
221
222             ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
223             ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
224             if (componentInstanceLogic == null) {
225                 log.debug("Unsupported component type {}", containerComponentType);
226                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
227             }
228
229             Either<List<ComponentInstance>, ResponseFormat> convertResponse = convertToMultipleResourceInstance(componentInstanceJsonArray);
230
231             if (convertResponse.isRight()) {
232                 // Using both ECOMP error methods, show to Sofer
233                 BeEcompErrorManager.getInstance().logBeSystemError("Resource Instance - updateResourceInstance");
234                 log.debug("Failed to convert received data to BE format.");
235                 return buildErrorResponse(convertResponse.right().value());
236             }
237
238             List<ComponentInstance> componentInstanceList = convertResponse.left().value();
239
240             Either<List<ComponentInstance>, ResponseFormat> actionResponse = componentInstanceLogic.updateComponentInstance(containerComponentType, componentId, userId, componentInstanceList, true, true);
241
242             if (actionResponse.isRight()) {
243                 return buildErrorResponse(actionResponse.right().value());
244             }
245
246             return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse.left().value());
247
248         } catch (Exception e) {
249             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Update Resource Instance");
250             log.debug("update resource instance with exception", e);
251             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
252         }
253
254     }
255
256     @DELETE
257     @Path("/{containerComponentType}/{componentId}/resourceInstance/{resourceInstanceId}")
258     @Consumes(MediaType.APPLICATION_JSON)
259     @Produces(MediaType.APPLICATION_JSON)
260     @ApiOperation(value = "Delete ResourceInstance", httpMethod = "DELETE", notes = "Returns delete resourceInstance", response = Response.class)
261     @ApiResponses(value = { @ApiResponse(code = 201, message = "ResourceInstance deleted"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
262     public Response deleteResourceInstance(@PathParam("componentId") final String componentId, @PathParam("resourceInstanceId") final String resourceInstanceId,
263             @ApiParam(value = "valid values: resources / services / products", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME + ","
264                     + ComponentTypeEnum.PRODUCT_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
265             @Context final HttpServletRequest request) {
266         ServletContext context = request.getSession().getServletContext();
267         String url = request.getMethod() + " " + request.getRequestURI();
268         Response response = null;
269         try {
270             log.debug("Start handle request of {}", url);
271             ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
272             ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
273             if (componentInstanceLogic == null) {
274                 log.debug("Unsupported component type {}", containerComponentType);
275                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
276             }
277             String userId = request.getHeader(Constants.USER_ID_HEADER);
278             Either<ComponentInstance, ResponseFormat> actionResponse = componentInstanceLogic.deleteComponentInstance(containerComponentType, componentId, resourceInstanceId, userId);
279
280             if (actionResponse.isRight()) {
281                 response = buildErrorResponse(actionResponse.right().value());
282             } else {
283                 response = buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.NO_CONTENT), null);
284             }
285             return response;
286         } catch (Exception e) {
287             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Delete Resource Instance");
288             log.debug("delete resource instance with exception", e);
289             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
290         }
291     }
292
293     @ApiParam(value = "allowed values are resources /services / products", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME + "," + ComponentTypeEnum.PRODUCT_PARAM_NAME, required = true)
294     @POST
295     @Path("/{containerComponentType}/{componentId}/resourceInstance/associate")
296     @Consumes(MediaType.APPLICATION_JSON)
297     @Produces(MediaType.APPLICATION_JSON)
298     @ApiOperation(value = "Associate RI to RI", httpMethod = "POST", notes = "Returns created RelationshipInfo", response = Response.class)
299     @ApiResponses(value = { @ApiResponse(code = 201, message = "Relationship created"), @ApiResponse(code = 403, message = "Missing information"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
300             @ApiResponse(code = 409, message = "Relationship already exist") })
301     public Response associateRIToRI(@ApiParam(value = "unique id of the container component") @PathParam("componentId") final String componentId,
302             @ApiParam(value = "allowed values are resources /services / products", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME + ","
303                     + ComponentTypeEnum.PRODUCT_PARAM_NAME, required = true) @PathParam("containerComponentType") final String containerComponentType,
304             @HeaderParam(value = Constants.USER_ID_HEADER) String userId, @ApiParam(value = "RelationshipInfo", required = true) String data, @Context final HttpServletRequest request) {
305         ServletContext context = request.getSession().getServletContext();
306
307         String url = request.getMethod() + " " + request.getRequestURI();
308         log.debug("Start handle request of {}", url);
309         Response response = null;
310
311         try {
312
313             log.debug("Start handle request of {}", url);
314
315             ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
316             ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
317             if (componentInstanceLogic == null) {
318                 log.debug("Unsupported component type {}", containerComponentType);
319                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
320             }
321
322             Either<RequirementCapabilityRelDef, ResponseFormat> regInfoW = convertToRequirementCapabilityRelDef(data);
323
324             Either<RequirementCapabilityRelDef, ResponseFormat> resultOp;
325             if (regInfoW.isRight()) {
326                 BeEcompErrorManager.getInstance().logBeSystemError("Resource Instance - associateRIToRI");
327                 log.debug("Failed to convert received data to BE format.");
328                 resultOp = Either.right(regInfoW.right().value());
329             } else {
330                 RequirementCapabilityRelDef requirementDef = regInfoW.left().value();
331                 resultOp = componentInstanceLogic.associateRIToRI(componentId, userId, requirementDef, componentTypeEnum);
332             }
333
334             Either<RequirementCapabilityRelDef, ResponseFormat> actionResponse = resultOp;
335
336             if (actionResponse.isRight()) {
337                 response = buildErrorResponse(actionResponse.right().value());
338             } else {
339                 response = buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse.left().value());
340             }
341             return response;
342
343         } catch (Exception e) {
344             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Associate Resource Instance");
345             log.debug("associate resource instance to another RI with exception", e);
346             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
347         }
348     }
349
350     @PUT
351     @Path("/{containerComponentType}/{componentId}/resourceInstance/dissociate")
352     @Consumes(MediaType.APPLICATION_JSON)
353     @Produces(MediaType.APPLICATION_JSON)
354     @ApiOperation(value = "Dissociate RI from RI", httpMethod = "PUT", notes = "Returns deleted RelationshipInfo", response = Response.class)
355     @ApiResponses(value = { @ApiResponse(code = 201, message = "Relationship deleted"), @ApiResponse(code = 403, message = "Missing information"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
356     public Response dissociateRIFromRI(
357             @ApiParam(value = "allowed values are resources /services / products", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME + ","
358                     + ComponentTypeEnum.PRODUCT_PARAM_NAME, required = true) @PathParam("containerComponentType") final String containerComponentType,
359             @ApiParam(value = "unique id of the container component") @PathParam("componentId") final String componentId, @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
360             @ApiParam(value = "RelationshipInfo", required = true) String data, @Context final HttpServletRequest request) {
361         ServletContext context = request.getSession().getServletContext();
362
363         String url = request.getMethod() + " " + request.getRequestURI();
364         log.debug("Start handle request of {}", url);
365
366         try {
367
368             log.debug("Start handle request of {}", url);
369
370             ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
371             ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
372             if (componentInstanceLogic == null) {
373                 log.debug("Unsupported component type {}", containerComponentType);
374                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
375             }
376
377             Either<RequirementCapabilityRelDef, ResponseFormat> regInfoW = convertToRequirementCapabilityRelDef(data);
378             if (regInfoW.isRight()) {
379                 BeEcompErrorManager.getInstance().logBeSystemError("Resource Instance - dissociateRIFromRI");
380                 log.debug("Failed to convert received data to BE format.");
381                 return buildErrorResponse(regInfoW.right().value());
382             }
383
384             RequirementCapabilityRelDef requirementDef = regInfoW.left().value();
385             Either<RequirementCapabilityRelDef, ResponseFormat> actionResponse = componentInstanceLogic.dissociateRIFromRI(componentId, userId, requirementDef, componentTypeEnum);
386
387             if (actionResponse.isRight()) {
388                 return buildErrorResponse(actionResponse.right().value());
389             }
390             return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse.left().value());
391
392         } catch (Exception e) {
393             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Dissociate Resource Instance");
394             log.debug("dissociate resource instance from service failed with exception", e);
395             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
396         }
397     }
398
399     @POST
400     @Path("/{containerComponentType}/{componentId}/resourceInstance/createAndAssociate")
401     @Consumes(MediaType.APPLICATION_JSON)
402     @Produces(MediaType.APPLICATION_JSON)
403     @ApiOperation(value = "Create RI and associate RI to RI", httpMethod = "POST", notes = "Returns created RI and RelationshipInfo", response = Response.class)
404     @ApiResponses(value = { @ApiResponse(code = 201, message = "RI created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
405             @ApiResponse(code = 409, message = "Relationship already exist") })
406     public Response createAndAssociateRIToRI(@PathParam("componentId") final String componentId,
407             @ApiParam(value = "valid values: resources / services", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
408             @Context final HttpServletRequest request) {
409         ServletContext context = request.getSession().getServletContext();
410
411         String url = request.getMethod() + " " + request.getRequestURI();
412         log.debug("Start handle request of {}", url);
413         try {
414
415             log.debug("Start handle request of {}", url);
416
417             InputStream inputStream = request.getInputStream();
418
419             byte[] bytes = IOUtils.toByteArray(inputStream);
420
421             if (bytes == null || bytes.length == 0) {
422                 log.info("Empty body was sent.");
423                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT));
424             }
425
426             String userId = request.getHeader(Constants.USER_ID_HEADER);
427
428             String data = new String(bytes);
429
430             ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
431             ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
432             if (componentInstanceLogic == null) {
433                 log.debug("Unsupported component type {}", containerComponentType);
434                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
435             }
436
437             Either<CreateAndAssotiateInfo, ActionStatus> convertStatus = convertJsonToObject(data, CreateAndAssotiateInfo.class);
438             if (convertStatus.isRight()) {
439                 BeEcompErrorManager.getInstance().logBeSystemError("Resource Instance - createAndAssociateRIToRI");
440                 log.debug("Failed to convert received data to BE format.");
441                 Either<Object, ResponseFormat> formattedResponse = Either.right(getComponentsUtils().getResponseFormat(convertStatus.right().value()));
442                 return buildErrorResponse(formattedResponse.right().value());
443             }
444
445             CreateAndAssotiateInfo createAndAssotiateInfo = convertStatus.left().value();
446             Either<CreateAndAssotiateInfo, ResponseFormat> actionResponse = componentInstanceLogic.createAndAssociateRIToRI(containerComponentType, componentId, userId, createAndAssotiateInfo);
447
448             if (actionResponse.isRight()) {
449                 return buildErrorResponse(actionResponse.right().value());
450             }
451             return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.CREATED), actionResponse.left().value());
452         } catch (Exception e) {
453             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Create and Associate Resource Instance");
454             log.debug("create and associate RI failed with exception", e);
455             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
456         }
457     }
458
459     @POST
460     @Path("/{containerComponentType}/{componentId}/resourceInstance/{componentInstanceId}/properties")
461     @Consumes(MediaType.APPLICATION_JSON)
462     @Produces(MediaType.APPLICATION_JSON)
463     @ApiOperation(value = "Update resource instance property", httpMethod = "POST", notes = "Returns updated resource instance property", response = Response.class)
464     @ApiResponses(value = { @ApiResponse(code = 201, message = "Resource instance created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
465     public Response updateResourceInstanceProperties(@ApiParam(value = "service id") @PathParam("componentId") final String componentId,
466             @ApiParam(value = "valid values: resources / services", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
467             @ApiParam(value = "resource instance id") @PathParam("componentInstanceId") final String componentInstanceId, @ApiParam(value = "id of user initiating the operation") @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
468             @Context final HttpServletRequest request, @ApiParam(value = "Component Instance Properties JSON Array", required = true) final String componentInstancePropertiesJsonArray) {
469
470         String url = request.getMethod() + " " + request.getRequestURI();
471         log.debug("Start handle request of {}", url);
472
473         try {
474             Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
475             List<ComponentInstanceProperty> propertiesToUpdate = new ArrayList<>();
476             if (errorWrapper.isEmpty()) {
477                 Either<List<ComponentInstanceProperty>, ResponseFormat> propertiesToUpdateEither = convertMultipleProperties(componentInstancePropertiesJsonArray);
478                 if (propertiesToUpdateEither.isRight()) {
479                     errorWrapper.setInnerElement(propertiesToUpdateEither.right().value());
480                 } else {
481                     propertiesToUpdate = propertiesToUpdateEither.left().value();
482                 }
483             }
484
485             if (!errorWrapper.isEmpty()) {
486                 return buildErrorResponse(errorWrapper.getInnerElement());
487             }
488
489             log.debug("Start handle request of updateResourceInstanceProperty. Received property is {}", propertiesToUpdate);
490
491             ServletContext context = request.getSession().getServletContext();
492
493             ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
494             ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
495             if (componentInstanceLogic == null) {
496                 log.debug("Unsupported component type {}", containerComponentType);
497                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
498             }
499
500             Either<List<ComponentInstanceProperty>, ResponseFormat> actionResponse = componentInstanceLogic.createOrUpdatePropertiesValues(componentTypeEnum, componentId, componentInstanceId, propertiesToUpdate, userId);
501
502             if (actionResponse.isRight()) {
503                 return buildErrorResponse(actionResponse.right().value());
504             }
505
506             List<ComponentInstanceProperty> resourceInstanceProperties = actionResponse.left().value();
507             ObjectMapper mapper = new ObjectMapper();
508             String result = mapper.writeValueAsString(resourceInstanceProperties);
509             return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), result);
510
511         } catch (Exception e) {
512             log.error("create and associate RI failed with exception: {}", e.getMessage(), e);
513             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
514         }
515
516     }
517
518     @POST
519     @Path("/{containerComponentType}/{componentId}/resourceInstance/{componentInstanceId}/inputs")
520     @Consumes(MediaType.APPLICATION_JSON)
521     @Produces(MediaType.APPLICATION_JSON)
522     @ApiOperation(value = "Update resource instance property", httpMethod = "POST", notes = "Returns updated resource instance property", response = Response.class)
523     @ApiResponses(value = { @ApiResponse(code = 201, message = "Resource instance created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
524     public Response updateResourceInstanceInput(@ApiParam(value = "service id") @PathParam("componentId") final String componentId,
525             @ApiParam(value = "valid values: resources / services", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
526             @ApiParam(value = "resource instance id") @PathParam("componentInstanceId") final String componentInstanceId, @ApiParam(value = "id of user initiating the operation") @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
527             @Context final HttpServletRequest request, @ApiParam(value = "Component Instance Properties JSON Array", required = true) final String componentInstanceInputsJsonArray) {
528
529         String url = request.getMethod() + " " + request.getRequestURI();
530         log.debug("Start handle request of {}", url);
531
532         try {
533             Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
534             List<ComponentInstanceInput> inputsToUpdate = new ArrayList<>();
535             if (errorWrapper.isEmpty()) {
536                 Either<List<ComponentInstanceInput>, ResponseFormat> inputsToUpdateEither = convertMultipleInputs(componentInstanceInputsJsonArray);
537                 if (inputsToUpdateEither.isRight()) {
538                     errorWrapper.setInnerElement(inputsToUpdateEither.right().value());
539                 } else {
540                     inputsToUpdate = inputsToUpdateEither.left().value();
541                 }
542             }
543             if (!errorWrapper.isEmpty()) {
544                 return buildErrorResponse(errorWrapper.getInnerElement());
545             }
546
547             log.debug("Start handle request of updateResourceInstanceProperty. Received property is {}", inputsToUpdate);
548
549             ServletContext context = request.getSession().getServletContext();
550
551             ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
552             ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
553             if (componentInstanceLogic == null) {
554                 log.debug("Unsupported component type {}", containerComponentType);
555                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
556             }
557
558             Either<List<ComponentInstanceInput>, ResponseFormat> actionResponse =
559                     componentInstanceLogic.createOrUpdateInstanceInputValues(componentTypeEnum, componentId, componentInstanceId, inputsToUpdate, userId);
560
561             if (actionResponse.isRight()) {
562                 return buildErrorResponse(actionResponse.right().value());
563             }
564
565             List<ComponentInstanceInput> resourceInstanceInput = actionResponse.left().value();
566             ObjectMapper mapper = new ObjectMapper();
567             String result = mapper.writeValueAsString(resourceInstanceInput);
568             return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), result);
569
570         } catch (Exception e) {
571             log.error("create and associate RI failed with exception: {}", e.getMessage(), e);
572             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
573         }
574
575     }
576
577     /**
578      * Updates ResourceInstance Attribute
579      *
580      * @param componentId
581      * @param containerComponentType
582      * @param componentInstanceId
583      * @param userId
584      * @param request
585      * @return
586      */
587     @POST
588     @Path("/{containerComponentType}/{componentId}/resourceInstance/{componentInstanceId}/attribute")
589     @Consumes(MediaType.APPLICATION_JSON)
590     @Produces(MediaType.APPLICATION_JSON)
591     @ApiOperation(value = "Update resource instance attribute", httpMethod = "POST", notes = "Returns updated resource instance attribute", response = Response.class)
592     @ApiResponses(value = { @ApiResponse(code = 201, message = "Resource instance created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
593     public Response updateResourceInstanceAttribute(@ApiParam(value = "service id") @PathParam("componentId") final String componentId,
594             @ApiParam(value = "valid values: resources / services", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
595             @ApiParam(value = "resource instance id") @PathParam("componentInstanceId") final String componentInstanceId, @ApiParam(value = "id of user initiating the operation") @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
596             @Context final HttpServletRequest request) {
597
598         String url = request.getMethod() + " " + request.getRequestURI();
599         log.debug("Start handle request of {}", url);
600
601         try {
602
603             Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
604             Wrapper<String> dataWrapper = new Wrapper<>();
605             Wrapper<ComponentInstanceProperty> attributeWrapper = new Wrapper<>();
606             Wrapper<ComponentInstanceBusinessLogic> blWrapper = new Wrapper<>();
607
608             validateInputStream(request, dataWrapper, errorWrapper);
609
610             if (errorWrapper.isEmpty()) {
611                 validateClassParse(dataWrapper.getInnerElement(), attributeWrapper, () -> ComponentInstanceProperty.class, errorWrapper);
612             }
613
614             if (errorWrapper.isEmpty()) {
615                 validateComponentInstanceBusinessLogic(request, containerComponentType, blWrapper, errorWrapper);
616             }
617
618             if (errorWrapper.isEmpty()) {
619                 ComponentInstanceBusinessLogic componentInstanceLogic = blWrapper.getInnerElement();
620                 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
621                 log.debug("Start handle request of ComponentInstanceAttribute. Received attribute is {}", attributeWrapper.getInnerElement());
622                 Either<ComponentInstanceProperty, ResponseFormat> eitherAttribute = componentInstanceLogic.createOrUpdateAttributeValue(componentTypeEnum, componentId, componentInstanceId, attributeWrapper.getInnerElement(), userId);
623                 if (eitherAttribute.isRight()) {
624                     errorWrapper.setInnerElement(eitherAttribute.right().value());
625                 } else {
626                     attributeWrapper.setInnerElement(eitherAttribute.left().value());
627                 }
628             }
629
630             return buildResponseFromElement(errorWrapper, attributeWrapper);
631
632         } catch (Exception e) {
633             log.error("create and associate RI failed with exception: {}", e.getMessage(), e);
634             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
635         }
636
637     }
638
639     @DELETE
640     @Path("/{containerComponentType}/{componentId}/resourceInstance/{componentInstanceId}/property/{propertyId}")
641     @Consumes(MediaType.APPLICATION_JSON)
642     @Produces(MediaType.APPLICATION_JSON)
643     @ApiOperation(value = "Update resource instance", httpMethod = "DELETE", notes = "Returns deleted resource instance property", response = Response.class)
644     @ApiResponses(value = { @ApiResponse(code = 201, message = "Resource instance created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
645     public Response deleteResourceInstanceProperty(@ApiParam(value = "service id") @PathParam("componentId") final String componentId,
646             @ApiParam(value = "valid values: resources / services", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
647             @ApiParam(value = "resource instance id") @PathParam("componentInstanceId") final String componentInstanceId, @ApiParam(value = "property id") @PathParam("propertyId") final String propertyId,
648             @ApiParam(value = "id of user initiating the operation") @HeaderParam(value = Constants.USER_ID_HEADER) String userId, @Context final HttpServletRequest request) {
649
650         ServletContext context = request.getSession().getServletContext();
651
652         String url = request.getMethod() + " " + request.getRequestURI();
653         log.debug("Start handle request of {}", url);
654         try {
655
656             ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
657             ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
658             if (componentInstanceLogic == null) {
659                 log.debug("Unsupported component type {}", containerComponentType);
660                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
661             }
662
663             Either<ComponentInstanceProperty, ResponseFormat> actionResponse = componentInstanceLogic.deletePropertyValue(componentTypeEnum, componentId, componentInstanceId, propertyId, userId);
664             if (actionResponse.isRight()) {
665                 return buildErrorResponse(actionResponse.right().value());
666             }
667             return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.NO_CONTENT), null);
668         } catch (Exception e) {
669             log.error("create and associate RI failed with exception: {}", e.getMessage(), e);
670             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
671         }
672
673     }
674
675     @POST
676     @Path("/{containerComponentType}/{componentId}/resourceInstance/{componentInstanceId}/changeVersion")
677     @Consumes(MediaType.APPLICATION_JSON)
678     @Produces(MediaType.APPLICATION_JSON)
679     @ApiOperation(value = "Update resource instance", httpMethod = "POST", notes = "Returns updated resource instance", response = Response.class)
680     @ApiResponses(value = { @ApiResponse(code = 201, message = "Resource instance created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
681     public Response changeResourceInstanceVersion(@PathParam("componentId") final String componentId, @PathParam("componentInstanceId") final String componentInstanceId,
682             @ApiParam(value = "valid values: resources / services", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
683             @Context final HttpServletRequest request) {
684         ServletContext context = request.getSession().getServletContext();
685
686         String url = request.getMethod() + " " + request.getRequestURI();
687         log.debug("Start handle request of {}", url);
688         try (    InputStream inputStream = request.getInputStream()) {
689
690             byte[] bytes = IOUtils.toByteArray(inputStream);
691
692             if (bytes == null || bytes.length == 0) {
693                 log.info("Empty body was sent.");
694                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT));
695             }
696
697             String userId = request.getHeader(Constants.USER_ID_HEADER);
698
699             String data = new String(bytes);
700
701             ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
702             ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
703             if (componentInstanceLogic == null) {
704                 log.debug("Unsupported component type {}", containerComponentType);
705                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
706             }
707
708             Either<ComponentInstance, ResponseFormat> convertResponse = convertToResourceInstance(data);
709
710             if (convertResponse.isRight()) {
711                 BeEcompErrorManager.getInstance().logBeSystemError("Resource Instance - updateResourceInstance");
712                 log.debug("Failed to convert received data to BE format.");
713                 return buildErrorResponse(convertResponse.right().value());
714             }
715
716             ComponentInstance newResourceInstance = convertResponse.left().value();
717             Either<ComponentInstance, ResponseFormat> actionResponse = componentInstanceLogic.changeComponentInstanceVersion(containerComponentType, componentId, componentInstanceId, userId, newResourceInstance);
718
719             if (actionResponse.isRight()) {
720                 return buildErrorResponse(actionResponse.right().value());
721             }
722             return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse.left().value());
723
724         } catch (Exception e) {
725             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Update Resource Instance");
726             log.debug("update resource instance with exception", e);
727             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
728         }
729
730     }
731
732     @POST
733     @Path("/{containerComponentType}/{componentId}/resourceInstance/{componentInstanceId}/groupInstance/{groupInstanceId}/property")
734     @Consumes(MediaType.APPLICATION_JSON)
735     @Produces(MediaType.APPLICATION_JSON)
736     @ApiOperation(value = "Update resource instance property", httpMethod = "POST", notes = "Returns updated resource instance property", response = Response.class)
737     @ApiResponses(value = { @ApiResponse(code = 201, message = "Resource instance created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
738     public Response updateGroupInstanceProperty(@ApiParam(value = "service id") @PathParam("componentId") final String componentId,
739             @ApiParam(value = "valid values: resources / services", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
740             @ApiParam(value = "resource instance id") @PathParam("componentInstanceId") final String componentInstanceId, @ApiParam(value = "group instance id") @PathParam("groupInstanceId") final String groupInstanceId,
741             @ApiParam(value = "id of user initiating the operation") @HeaderParam(value = Constants.USER_ID_HEADER) String userId, @Context final HttpServletRequest request) {
742
743         String url = request.getMethod() + " " + request.getRequestURI();
744         log.debug("Start handle request of {}", url);
745
746         try {
747             Wrapper<String> dataWrapper = new Wrapper<>();
748             Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
749             Wrapper<ComponentInstanceProperty> propertyWrapper = new Wrapper<>();
750
751             validateInputStream(request, dataWrapper, errorWrapper);
752
753             if (errorWrapper.isEmpty()) {
754                 validateClassParse(dataWrapper.getInnerElement(), propertyWrapper, () -> ComponentInstanceProperty.class, errorWrapper);
755             }
756
757             if (!errorWrapper.isEmpty()) {
758                 return buildErrorResponse(errorWrapper.getInnerElement());
759             }
760
761             ComponentInstanceProperty property = propertyWrapper.getInnerElement();
762
763             log.debug("Start handle request of updateResourceInstanceProperty. Received property is {}", property);
764
765             ServletContext context = request.getSession().getServletContext();
766
767             ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
768             ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
769             if (componentInstanceLogic == null) {
770                 log.debug("Unsupported component type {}", containerComponentType);
771                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
772             }
773
774             Either<ComponentInstanceProperty, ResponseFormat> actionResponse = componentInstanceLogic.createOrUpdateGroupInstancePropertyValue(componentTypeEnum, componentId, componentInstanceId, groupInstanceId, property, userId);
775
776             if (actionResponse.isRight()) {
777                 return buildErrorResponse(actionResponse.right().value());
778             }
779
780             ComponentInstanceProperty resourceInstanceProperty = actionResponse.left().value();
781             ObjectMapper mapper = new ObjectMapper();
782             String result = mapper.writeValueAsString(resourceInstanceProperty);
783             return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), result);
784
785         } catch (Exception e) {
786             log.error("create and associate RI failed with exception: {}", e.getMessage(), e);
787             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
788         }
789
790     }
791
792     @GET
793     @Path("/{containerComponentType}/{componentId}/resourceInstance/{componentInstanceId}/groupInstance/{groupInstId}")
794     @Consumes(MediaType.APPLICATION_JSON)
795     @Produces(MediaType.APPLICATION_JSON)
796     @ApiOperation(value = "Get group artifacts ", httpMethod = "GET", notes = "Returns artifacts metadata according to groupInstId", response = Resource.class)
797     @ApiResponses(value = { @ApiResponse(code = 200, message = "group found"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 404, message = "Group not found") })
798     public Response getGroupArtifactById(@PathParam("containerComponentType") final String containerComponentType, @PathParam("componentId") final String componentId, @PathParam("componentInstanceId") final String componentInstanceId,
799             @PathParam("groupInstId") final String groupInstId, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
800         ServletContext context = request.getSession().getServletContext();
801         String url = request.getMethod() + " " + request.getRequestURI();
802         log.debug("(GET) Start handle request of {}", url);
803
804         try {
805
806             GroupBusinessLogic businessLogic = this.getGroupBL(context);
807             ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
808             Either<GroupDefinitionInfo, ResponseFormat> actionResponse = businessLogic.getGroupInstWithArtifactsById(componentTypeEnum, componentId, componentInstanceId, groupInstId, userId, false);
809
810             if (actionResponse.isRight()) {
811                 log.debug("failed to get all non abstract {}", containerComponentType);
812                 return buildErrorResponse(actionResponse.right().value());
813             }
814
815             return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse.left().value());
816
817         } catch (Exception e) {
818             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("getGroupArtifactById");
819             log.debug("getGroupArtifactById unexpected exception", e);
820             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
821         }
822
823     }
824
825     // US831698
826     @GET
827     @Path("/{containerComponentType}/{containerComponentId}/componentInstances/{componentInstanceUniqueId}/properties")
828     @Consumes(MediaType.APPLICATION_JSON)
829     @Produces(MediaType.APPLICATION_JSON)
830     @ApiOperation(value = "Get component instance properties", httpMethod = "GET", notes = "Returns component instance properties", response = Response.class)
831     @ApiResponses(value = { @ApiResponse(code = 200, message = "Properties found"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 404, message = "Component/Component Instance - not found") })
832     public Response getInstancePropertiesById(@PathParam("containerComponentType") final String containerComponentType, @PathParam("containerComponentId") final String containerComponentId,
833             @PathParam("componentInstanceUniqueId") final String componentInstanceUniqueId, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
834
835         ServletContext context = request.getSession().getServletContext();
836         String url = request.getMethod() + " " + request.getRequestURI();
837         log.debug("(GET) Start handle request of {}", url);
838
839         try {
840             ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
841             ComponentInstanceBusinessLogic componentInstanceBL = getComponentInstanceBL(context, componentTypeEnum);
842
843             Either<List<ComponentInstanceProperty>, ResponseFormat> componentInstancePropertiesById = componentInstanceBL.getComponentInstancePropertiesById(containerComponentType, containerComponentId, componentInstanceUniqueId, userId);
844
845             if (componentInstancePropertiesById.isRight()) {
846                 log.debug("Failed to get properties of component instance ID: {} in {} with ID: {}", componentInstanceUniqueId, containerComponentType, containerComponentId);
847                 return buildErrorResponse(componentInstancePropertiesById.right().value());
848             }
849
850             return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), componentInstancePropertiesById.left().value());
851         } catch (Exception e) {
852             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("getGroupArtifactById");
853             log.debug("getGroupArtifactById unexpected exception", e);
854             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
855         }
856
857     }
858
859     // US330353
860     @GET
861     @Path("/{containerComponentType}/{containerComponentId}/componentInstances/{componentInstanceUniqueId}/capability/{capabilityType}/capabilityName/{capabilityName}/ownerId/{ownerId}/properties")
862     @Consumes(MediaType.APPLICATION_JSON)
863     @Produces(MediaType.APPLICATION_JSON)
864     @ApiOperation(value = "Get component instance capability properties", httpMethod = "GET", notes = "Returns component instance capability properties", response = Response.class)
865     @ApiResponses(value = { @ApiResponse(code = 200, message = "Properties found"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 404, message = "Component/Component Instance/Capability - not found") })
866     public Response getInstanceCapabilityPropertiesById(@PathParam("containerComponentType") final String containerComponentType, @PathParam("containerComponentId") final String containerComponentId,
867             @PathParam("componentInstanceUniqueId") final String componentInstanceUniqueId, @PathParam("capabilityType") final String capabilityType, @PathParam("capabilityName") final String capabilityName,  @PathParam("ownerId") final String ownerId, @Context final HttpServletRequest request,
868             @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
869
870         ServletContext context = request.getSession().getServletContext();
871         String url = request.getMethod() + " " + request.getRequestURI();
872         log.debug("(GET) Start handle request of {}", url);
873
874         try {
875             ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
876             ComponentInstanceBusinessLogic componentInstanceBL = getComponentInstanceBL(context, componentTypeEnum);
877
878             Either<List<ComponentInstanceProperty>, ResponseFormat> componentInstancePropertiesById = componentInstanceBL.getComponentInstanceCapabilityPropertiesById(containerComponentType, containerComponentId, componentInstanceUniqueId,
879                     capabilityType, capabilityName, ownerId, userId);
880
881             if (componentInstancePropertiesById.isRight()) {
882                 log.debug("Failed to get properties of component instance ID: {} in {} with ID: {}", componentInstanceUniqueId, containerComponentType, containerComponentId);
883                 return buildErrorResponse(componentInstancePropertiesById.right().value());
884             }
885
886             return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), componentInstancePropertiesById.left().value());
887         } catch (Exception e) {
888             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("getGroupArtifactById");
889             log.debug("getGroupArtifactById unexpected exception", e);
890             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
891         }
892
893     }
894
895     //US 331281
896     @PUT
897     @Path("/{containerComponentType}/{containerComponentId}/componentInstances/{componentInstanceUniqueId}/capability/{capabilityType}/capabilityName/{capabilityName}/ownerId/{ownerId}/properties")
898     @Consumes(MediaType.APPLICATION_JSON)
899     @Produces(MediaType.APPLICATION_JSON)
900     @ApiOperation(value = "Update Instance Capabilty  Property", httpMethod = "PUT", notes = "Returns updated property", response = Response.class)
901     @ApiResponses(value = { @ApiResponse(code = 200, message = "Resource instance capabilty property updated"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
902             @ApiResponse(code = 404, message = "Component/Component Instance/Capability - not found") })
903     public Response updateInstanceCapabilityProperty(@PathParam("containerComponentType") final String containerComponentType, @PathParam("containerComponentId") final String containerComponentId,
904             @PathParam("componentInstanceUniqueId") final String componentInstanceUniqueId, @PathParam("capabilityType") final String capabilityType, @PathParam("capabilityName") final String capabilityName, @PathParam("ownerId") final String ownerId,
905             @ApiParam(value = "Instance capabilty property to update", required = true) String data, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
906         ServletContext context = request.getSession().getServletContext();
907         String url = request.getMethod() + " " + request.getRequestURI();
908         log.debug("(PUT) Start handle request of {}", url);
909         try {
910             Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
911             List<ComponentInstanceProperty> propertiesToUpdate = new ArrayList<>();
912             if (errorWrapper.isEmpty()) {
913                 Either<List<ComponentInstanceProperty>, ResponseFormat> propertiesToUpdateEither = convertMultipleProperties(data);
914                 if (propertiesToUpdateEither.isRight()) {
915                     errorWrapper.setInnerElement(propertiesToUpdateEither.right().value());
916                 } else {
917                     propertiesToUpdate = propertiesToUpdateEither.left().value();
918                 }
919             }
920
921             if (!errorWrapper.isEmpty()) {
922                 return buildErrorResponse(errorWrapper.getInnerElement());
923             }
924
925             ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
926             ComponentInstanceBusinessLogic componentInstanceBL = getComponentInstanceBL(context, componentTypeEnum);
927
928             Either<List<ComponentInstanceProperty>, ResponseFormat> updateCICapProperty = componentInstanceBL.updateInstanceCapabilityProperties(componentTypeEnum, containerComponentId, componentInstanceUniqueId, capabilityType, capabilityName, propertiesToUpdate, userId);
929
930             if (updateCICapProperty.isRight()) {
931                 log.debug("Failed to get properties of component instance ID: {} in {} with ID: {}", componentInstanceUniqueId, containerComponentType, containerComponentId);
932                 return buildErrorResponse(updateCICapProperty.right().value());
933             }
934
935             return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), updateCICapProperty.left().value());
936         } catch (Exception e) {
937             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("getGroupArtifactById");
938             log.debug("getGroupArtifactById unexpected exception", e);
939             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
940         }
941     }
942
943     @POST
944     @Path("/{containerComponentType}/{containerComponentId}/serviceProxy")
945     @Consumes(MediaType.APPLICATION_JSON)
946     @Produces(MediaType.APPLICATION_JSON)
947     @ApiOperation(value = "Create service proxy", httpMethod = "POST", notes = "Returns created service proxy", response = Response.class)
948     @ApiResponses(value = { @ApiResponse(code = 201, message = "Service proxy created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
949             @ApiResponse(code = 409, message = "Service proxy already exist") })
950     public Response createServiceProxy(@ApiParam(value = "RI object to be created", required = true) String data, @PathParam("containerComponentId") final String containerComponentId,
951             @ApiParam(value = "valid values: resources / services", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
952             @HeaderParam(value = Constants.USER_ID_HEADER) @ApiParam(value = "USER_ID of modifier user", required = true) String userId, @Context final HttpServletRequest request) {
953         ServletContext context = request.getSession().getServletContext();
954
955         try {
956
957             ComponentInstance componentInstance = RepresentationUtils.fromRepresentation(data, ComponentInstance.class);
958             componentInstance.setInvariantName(null);
959             ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
960             if (componentTypeEnum != ComponentTypeEnum.SERVICE) {
961                 log.debug("Unsupported container component type {}", containerComponentType);
962                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
963             }
964             ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
965             if (componentInstanceLogic == null) {
966                 log.debug("Unsupported component type {}", containerComponentType);
967                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
968             }
969             Either<ComponentInstance, ResponseFormat> actionResponse = componentInstanceLogic.createServiceProxy(containerComponentType, containerComponentId, userId, componentInstance);
970
971             if (actionResponse.isRight()) {
972                 return buildErrorResponse(actionResponse.right().value());
973             }
974             return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.CREATED), actionResponse.left().value());
975
976         } catch (Exception e) {
977             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Create service proxy");
978             log.debug("Create service proxy failed with exception", e);
979             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
980         }
981     }
982
983     @DELETE
984     @Path("/{containerComponentType}/{containerComponentId}/serviceProxy/{serviceProxyId}")
985     @Consumes(MediaType.APPLICATION_JSON)
986     @Produces(MediaType.APPLICATION_JSON)
987     @ApiOperation(value = "Delete service proxy", httpMethod = "DELETE", notes = "Returns delete service proxy", response = Response.class)
988     @ApiResponses(value = { @ApiResponse(code = 201, message = "Service proxy deleted"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
989     public Response deleteServiceProxy(@PathParam("containerComponentId") final String containerComponentId, @PathParam("serviceProxyId") final String serviceProxyId,
990             @ApiParam(value = "valid values: resources / services / products", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME + ","
991                     + ComponentTypeEnum.PRODUCT_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
992             @Context final HttpServletRequest request) {
993         ServletContext context = request.getSession().getServletContext();
994         String url = request.getMethod() + " " + request.getRequestURI();
995         Response response = null;
996         try {
997             log.debug("Start handle request of {}", url);
998             ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
999             ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
1000             if (componentInstanceLogic == null) {
1001                 log.debug("Unsupported component type {}", containerComponentType);
1002                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
1003             }
1004             String userId = request.getHeader(Constants.USER_ID_HEADER);
1005             Either<ComponentInstance, ResponseFormat> actionResponse = componentInstanceLogic.deleteServiceProxy(containerComponentType, containerComponentId, serviceProxyId, userId);
1006
1007             if (actionResponse.isRight()) {
1008                 response = buildErrorResponse(actionResponse.right().value());
1009             } else {
1010                 response = buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.NO_CONTENT), null);
1011             }
1012             return response;
1013         } catch (Exception e) {
1014             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Delete service proxy");
1015             log.debug("Delete service proxy failed with exception", e);
1016             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
1017         }
1018     }
1019
1020     @POST
1021     @Path("/{containerComponentType}/{containerComponentId}/serviceProxy/{serviceProxyId}/changeVersion/{newServiceId}")
1022     @Consumes(MediaType.APPLICATION_JSON)
1023     @Produces(MediaType.APPLICATION_JSON)
1024     @ApiOperation(value = "Update service proxy with new version", httpMethod = "POST", notes = "Returns updated service proxy", response = Response.class)
1025     @ApiResponses(value = { @ApiResponse(code = 201, message = "Service proxy created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
1026     public Response changeServiceProxyVersion(@PathParam("containerComponentId") final String containerComponentId, @PathParam("serviceProxyId") final String serviceProxyId,
1027             @ApiParam(value = "valid values: resources / services", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
1028             @Context final HttpServletRequest request) {
1029         ServletContext context = request.getSession().getServletContext();
1030
1031         String url = request.getMethod() + " " + request.getRequestURI();
1032         log.debug("Start handle request of {}", url);
1033         try {
1034
1035             String userId = request.getHeader(Constants.USER_ID_HEADER);
1036
1037             ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
1038             ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
1039             if (componentInstanceLogic == null) {
1040                 log.debug("Unsupported component type {}", containerComponentType);
1041                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
1042             }
1043             Either<ComponentInstance, ResponseFormat> actionResponse = componentInstanceLogic.changeServiceProxyVersion(containerComponentType, containerComponentId, serviceProxyId, userId);
1044
1045             if (actionResponse.isRight()) {
1046                 return buildErrorResponse(actionResponse.right().value());
1047             }
1048             return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse.left().value());
1049
1050         } catch (Exception e) {
1051             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Update service proxy with new version");
1052             log.debug("Update service proxy with new version failed with exception", e);
1053             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
1054         }
1055     }
1056     /**
1057      * REST API GET relation by Id
1058      * Allows to get relation contained in specified component according to received Id
1059      * @param containerComponentType
1060      * @param componentId
1061      * @param relationId
1062      * @param request
1063      * @param userId
1064      * @return Response
1065      */
1066     @GET
1067     @Path("/{containerComponentType}/{componentId}/relationId/{relationId}")
1068     @Consumes(MediaType.APPLICATION_JSON)
1069     @Produces(MediaType.APPLICATION_JSON)
1070     @ApiOperation(value = "Get relation", httpMethod = "GET", notes = "Returns relation metadata according to relationId", response = Resource.class)
1071     @ApiResponses(value = { @ApiResponse(code = 200, message = "relation found"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 404, message = "Relation not found") })
1072     public Response getRelationById(@PathParam("containerComponentType") final String containerComponentType, @PathParam("componentId") final String componentId,
1073             @PathParam("relationId") final String relationId, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
1074
1075         ServletContext context = request.getSession().getServletContext();
1076         String url = request.getMethod() + " " + request.getRequestURI();
1077         log.debug("(GET) Start handle request of {}", url);
1078         try {
1079             ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
1080             if (componentTypeEnum == null) {
1081                 log.debug("Unsupported component type {}", containerComponentType);
1082                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
1083             }
1084             ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
1085
1086             Either<RequirementCapabilityRelDef, ResponseFormat> actionResponse = componentInstanceLogic.getRelationById(componentId, relationId, userId, componentTypeEnum);
1087             if (actionResponse.isRight()) {
1088                 return buildErrorResponse(actionResponse.right().value());
1089             }
1090             return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse.left().value());
1091         } catch (Exception e) {
1092             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("getRelationById");
1093             log.debug("getRelationById unexpected exception", e);
1094             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
1095         }
1096     }
1097
1098     private Either<ComponentInstance, ResponseFormat> convertToResourceInstance(String data) {
1099
1100         // Either<ComponentInstance, ActionStatus> convertStatus =
1101         // convertJsonToObject(data, ComponentInstance.class);
1102         Either<ComponentInstance, ResponseFormat> convertStatus = getComponentsUtils().convertJsonToObjectUsingObjectMapper(data, new User(), ComponentInstance.class, null, ComponentTypeEnum.RESOURCE_INSTANCE);
1103         if (convertStatus.isRight()) {
1104             return Either.right(convertStatus.right().value());
1105         }
1106         ComponentInstance resourceInstanceInfo = convertStatus.left().value();
1107
1108         return Either.left(resourceInstanceInfo);
1109     }
1110
1111     private Either<List<ComponentInstance>, ResponseFormat> convertToMultipleResourceInstance(String dataList) {
1112
1113         Either<ComponentInstance[], ResponseFormat> convertStatus = getComponentsUtils().convertJsonToObjectUsingObjectMapper(dataList, new User(), ComponentInstance[].class, null, ComponentTypeEnum.RESOURCE_INSTANCE);
1114         if (convertStatus.isRight()) {
1115             return Either.right(convertStatus.right().value());
1116         }
1117
1118         return Either.left(Arrays.asList(convertStatus.left().value()));
1119     }
1120
1121     private Either<List<ComponentInstanceProperty>, ResponseFormat> convertMultipleProperties(String dataList) {
1122         if (StringUtils.isEmpty(dataList)) {
1123             return Either.right(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT));
1124         }
1125         Either<ComponentInstanceProperty[], ResponseFormat> convertStatus = getComponentsUtils().convertJsonToObjectUsingObjectMapper(dataList, new User(), ComponentInstanceProperty[].class, null, ComponentTypeEnum.RESOURCE_INSTANCE);
1126         if (convertStatus.isRight()) {
1127             return Either.right(convertStatus.right().value());
1128         }
1129         return Either.left(Arrays.asList(convertStatus.left().value()));
1130     }
1131
1132     private Either<List<ComponentInstanceInput>, ResponseFormat> convertMultipleInputs(String dataList) {
1133         if (StringUtils.isEmpty(dataList)) {
1134             return Either.right(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT));
1135         }
1136         Either<ComponentInstanceInput[], ResponseFormat> convertStatus = getComponentsUtils().convertJsonToObjectUsingObjectMapper(dataList, new User(), ComponentInstanceInput[].class, null, ComponentTypeEnum.RESOURCE_INSTANCE);
1137         if (convertStatus.isRight()) {
1138             return Either.right(convertStatus.right().value());
1139         }
1140         return Either.left(Arrays.asList(convertStatus.left().value()));
1141     }
1142
1143
1144     private Either<RequirementCapabilityRelDef, ResponseFormat> convertToRequirementCapabilityRelDef(String data) {
1145
1146         Either<RequirementCapabilityRelDef, ActionStatus> convertStatus = convertJsonToObject(data, RequirementCapabilityRelDef.class);
1147         if (convertStatus.isRight()) {
1148             return Either.right(getComponentsUtils().getResponseFormat(convertStatus.right().value()));
1149         }
1150         RequirementCapabilityRelDef requirementCapabilityRelDef = convertStatus.left().value();
1151         return Either.left(requirementCapabilityRelDef);
1152     }
1153
1154     private <T> Either<T, ActionStatus> convertJsonToObject(String data, Class<T> clazz) {
1155         try {
1156             log.trace("convert json to object. json=\n {}", data);
1157             T t;
1158             t = gson.fromJson(data, clazz);
1159             if (t == null) {
1160                 BeEcompErrorManager.getInstance().logBeInvalidJsonInput("convertJsonToObject");
1161                 log.debug("object is null after converting from json");
1162                 return Either.right(ActionStatus.INVALID_CONTENT);
1163             }
1164             return Either.left(t);
1165         } catch (Exception e) {
1166             // INVALID JSON
1167             BeEcompErrorManager.getInstance().logBeInvalidJsonInput("convertJsonToObject");
1168             log.debug("failed to convert from json", e);
1169             return Either.right(ActionStatus.INVALID_CONTENT);
1170         }
1171     }
1172
1173
1174     @GET
1175     @Path("/{containerComponentType}/{componentId}/paths-to-delete")
1176     @Produces(MediaType.APPLICATION_JSON)
1177     @ApiOperation(value = "Check if forwarding path to delete on version change", httpMethod = "GET", notes = "Returns forwarding paths to delete",
1178         response = Response.class)
1179     public Response changeResourceInstanceVersion( @PathParam("componentId") String componentId,
1180         @QueryParam("componentInstanceId") final String oldComponentInstanceId,
1181         @QueryParam("newComponentInstanceId") final String newComponentInstanceId,
1182         @ApiParam(value = "valid values: resources / services",
1183             allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME)
1184         @PathParam("containerComponentType") final String containerComponentType,
1185         @Context final HttpServletRequest request) {
1186         if (oldComponentInstanceId == null){
1187             return  buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.MISSING_OLD_COMPONENT_INSTANCE));
1188         }
1189         if (newComponentInstanceId == null){
1190             return  buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.MISSING_NEW_COMPONENT_INSTANCE));
1191         }
1192         ServletContext context = request.getSession().getServletContext();
1193
1194         String url = request.getMethod() + " " + request.getRequestURI();
1195         log.debug("Start handle request of {}", url);
1196         ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
1197         ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
1198         if (componentInstanceLogic == null) {
1199             log.debug("Unsupported component type {}", containerComponentType);
1200             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
1201         }
1202         ComponentInstance newComponentInstance;
1203         if(StringUtils.isNotEmpty(newComponentInstanceId)){
1204             newComponentInstance=new ComponentInstance();
1205             newComponentInstance.setToscaPresentationValue(JsonPresentationFields.CI_COMPONENT_UID,newComponentInstanceId);
1206         }else{
1207             log.error("missing component id");
1208             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.MISSING_DATA));
1209         }
1210         Either<Set<String>,ResponseFormat> actionResponse= componentInstanceLogic.forwardingPathOnVersionChange(
1211             containerComponentType,componentId,oldComponentInstanceId,newComponentInstance);
1212         if (actionResponse.isRight()) {
1213             return buildErrorResponse(actionResponse.right().value());
1214         }
1215         ForwardingPaths forwardingPaths=new ForwardingPaths();
1216         forwardingPaths.setForwardingPathToDelete(actionResponse.left().value());
1217         return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), forwardingPaths);
1218
1219     }
1220
1221 }