[SDC] rebase code
[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.Arrays;
26 import java.util.List;
27
28 import javax.inject.Singleton;
29 import javax.servlet.ServletContext;
30 import javax.servlet.http.HttpServletRequest;
31 import javax.ws.rs.Consumes;
32 import javax.ws.rs.DELETE;
33 import javax.ws.rs.GET;
34 import javax.ws.rs.HeaderParam;
35 import javax.ws.rs.POST;
36 import javax.ws.rs.PUT;
37 import javax.ws.rs.Path;
38 import javax.ws.rs.PathParam;
39 import javax.ws.rs.Produces;
40 import javax.ws.rs.core.Context;
41 import javax.ws.rs.core.MediaType;
42 import javax.ws.rs.core.Response;
43
44 import org.apache.commons.io.IOUtils;
45 import org.codehaus.jackson.map.ObjectMapper;
46 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
47 import org.openecomp.sdc.be.components.impl.GroupBusinessLogic;
48 import org.openecomp.sdc.be.config.BeEcompErrorManager;
49 import org.openecomp.sdc.be.dao.api.ActionStatus;
50 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
51 import org.openecomp.sdc.be.info.CreateAndAssotiateInfo;
52 import org.openecomp.sdc.be.info.GroupDefinitionInfo;
53 import org.openecomp.sdc.be.model.ComponentInstance;
54 import org.openecomp.sdc.be.model.ComponentInstanceInput;
55 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
56 import org.openecomp.sdc.be.model.InputDefinition;
57 import org.openecomp.sdc.be.model.PropertyConstraint;
58 import org.openecomp.sdc.be.model.RequirementCapabilityRelDef;
59 import org.openecomp.sdc.be.model.Resource;
60 import org.openecomp.sdc.be.model.User;
61 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation.PropertyConstraintDeserialiser;
62 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
63 import org.openecomp.sdc.common.api.Constants;
64 import org.openecomp.sdc.common.datastructure.Wrapper;
65 import org.openecomp.sdc.exception.ResponseFormat;
66 import org.slf4j.Logger;
67 import org.slf4j.LoggerFactory;
68
69 import com.google.gson.Gson;
70 import com.google.gson.GsonBuilder;
71 import com.google.gson.reflect.TypeToken;
72 import com.jcabi.aspects.Loggable;
73 import io.swagger.annotations.Api;
74 import io.swagger.annotations.ApiOperation;
75 import io.swagger.annotations.ApiParam;
76 import io.swagger.annotations.ApiResponse;
77 import io.swagger.annotations.ApiResponses;
78
79 import fj.data.Either;
80
81 /**
82  * Root resource (exposed at "/" path)
83  * .json
84  */
85 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
86 @Path("/v1/catalog")
87 @Api(value = "Resource Instance Servlet", description = "Resource Instance Servlet")
88 @Singleton
89 public class ComponentInstanceServlet extends AbstractValidationsServlet {
90
91         private static Logger log = LoggerFactory.getLogger(ComponentInstanceServlet.class.getName());
92
93         Type constraintType = new TypeToken<PropertyConstraint>() {
94         }.getType();
95
96         Gson gson = new GsonBuilder().registerTypeAdapter(constraintType, new PropertyConstraintDeserialiser()).create();
97
98         @POST
99         @Path("/{containerComponentType}/{componentId}/resourceInstance")
100         @Consumes(MediaType.APPLICATION_JSON)
101         @Produces(MediaType.APPLICATION_JSON)
102         @ApiOperation(value = "Create ComponentInstance", httpMethod = "POST", notes = "Returns created ComponentInstance", response = Response.class)
103         @ApiResponses(value = { @ApiResponse(code = 201, message = "Component created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
104                         @ApiResponse(code = 409, message = "Component instance already exist") })
105         public Response createComponentInstance(@ApiParam(value = "RI object to be created", required = true) String data, @PathParam("componentId") final String containerComponentId,
106                         @ApiParam(value = "valid values: resources / services", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
107                         @HeaderParam(value = Constants.USER_ID_HEADER) @ApiParam(value = "USER_ID of modifier user", required = true) String userId, @Context final HttpServletRequest request) {
108                 ServletContext context = request.getSession().getServletContext();
109
110                 try {
111
112                         ComponentInstance componentInstance = RepresentationUtils.fromRepresentation(data, ComponentInstance.class);
113                         componentInstance.setInvariantName(null);
114                         ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
115                         ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
116                         if (componentInstanceLogic == null) {
117                                 log.debug("Unsupported component type {}", containerComponentType);
118                                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
119                         }
120                         Either<ComponentInstance, ResponseFormat> actionResponse = componentInstanceLogic.createComponentInstance(containerComponentType, containerComponentId, userId, componentInstance);
121
122                         if (actionResponse.isRight()) {
123                                 return buildErrorResponse(actionResponse.right().value());
124                         }
125                         return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.CREATED), actionResponse.left().value());
126
127                 } catch (Exception e) {
128                         BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Create Component Instance");
129                         log.debug("create component instance failed with exception", e);
130                         return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
131                 }
132         }
133
134         @POST
135         @Path("/{containerComponentType}/{componentId}/resourceInstance/{componentInstanceId}")
136         @Consumes(MediaType.APPLICATION_JSON)
137         @Produces(MediaType.APPLICATION_JSON)
138         @ApiOperation(value = "Update resource instance", httpMethod = "POST", notes = "Returns updated resource instance", response = Response.class)
139         @ApiResponses(value = { @ApiResponse(code = 200, message = "Resource instance updated"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
140         public Response updateComponentInstanceMetadata(@PathParam("componentId") final String componentId, @PathParam("componentInstanceId") final String componentInstanceId,
141                         @ApiParam(value = "valid values: resources / services / products", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME + ","
142                                         + ComponentTypeEnum.PRODUCT_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
143                         @Context final HttpServletRequest request) {
144                 ServletContext context = request.getSession().getServletContext();
145
146                 String url = request.getMethod() + " " + request.getRequestURI();
147                 log.debug("Start handle request of {}", url);
148                 try {
149
150                         log.debug("Start handle request of {}", url);
151
152                         InputStream inputStream = request.getInputStream();
153
154                         byte[] bytes = IOUtils.toByteArray(inputStream);
155
156                         if (bytes == null || bytes.length == 0) {
157                                 log.info("Empty body was sent.");
158                                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT));
159                         }
160
161                         String userId = request.getHeader(Constants.USER_ID_HEADER);
162
163                         String data = new String(bytes);
164                         ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
165                         ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
166                         if (componentInstanceLogic == null) {
167                                 log.debug("Unsupported component type {}", containerComponentType);
168                                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
169                         }
170                         Either<ComponentInstance, ResponseFormat> convertResponse = convertToResourceInstance(data);
171
172                         if (convertResponse.isRight()) {
173                                 BeEcompErrorManager.getInstance().logBeSystemError("Resource Instance - updateResourceInstance");
174                                 log.debug("Failed to convert received data to BE format.");
175                                 return buildErrorResponse(convertResponse.right().value());
176                         }
177
178                         ComponentInstance resourceInstance = convertResponse.left().value();
179                         Either<ComponentInstance, ResponseFormat> actionResponse = componentInstanceLogic.updateComponentInstanceMetadata(containerComponentType, componentId, componentInstanceId, userId, resourceInstance);
180
181                         if (actionResponse.isRight()) {
182                                 return buildErrorResponse(actionResponse.right().value());
183                         }
184                         return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse.left().value());
185
186                 } catch (Exception e) {
187                         BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Update Resource Instance");
188                         log.debug("update resource instance with exception", e);
189                         return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
190                 }
191
192         }
193
194         @POST
195         @Path("/{containerComponentType}/{componentId}/resourceInstance/multipleComponentInstance")
196         @Consumes(MediaType.APPLICATION_JSON)
197         @Produces(MediaType.APPLICATION_JSON)
198         @ApiOperation(value = "Update resource instance multiple component", httpMethod = "POST", notes = "Returns updated resource instance", response = Response.class)
199         @ApiResponses(value = { @ApiResponse(code = 200, message = "Resource instance updated"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
200         public Response updateMultipleComponentInstance(@PathParam("componentId") final String componentId,
201                         @ApiParam(value = "valid values: resources / services / products", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME + ","
202                                         + ComponentTypeEnum.PRODUCT_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
203                         @Context final HttpServletRequest request, @ApiParam(value = "Component Instance JSON Array", required = true) final String componentInstanceJsonArray) {
204
205                 ServletContext context = request.getSession().getServletContext();
206                 String url = request.getMethod() + " " + request.getRequestURI();
207                 log.debug("Start handle request of {}", url);
208
209                 try {
210                         log.debug("Start handle request of {}", url);
211
212                         if (componentInstanceJsonArray == null || componentInstanceJsonArray.length() == 0) {
213                                 log.info("Empty JSON list was sent.");
214                                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT));
215                         }
216
217                         String userId = request.getHeader(Constants.USER_ID_HEADER);
218
219                         ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
220                         ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
221                         if (componentInstanceLogic == null) {
222                                 log.debug("Unsupported component type {}", containerComponentType);
223                                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
224                         }
225
226                         Either<List<ComponentInstance>, ResponseFormat> convertResponse = convertToMultipleResourceInstance(componentInstanceJsonArray);
227
228                         if (convertResponse.isRight()) {
229                                 // Using both ECOMP error methods, show to Sofer
230                                 BeEcompErrorManager.getInstance().logBeSystemError("Resource Instance - updateResourceInstance");
231                                 /*
232                                  * BeEcompErrorManager.getInstance().processEcompError( EcompErrorName.BeSystemError, "Resource Instance - updateResourceInstance");
233                                  */
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                         /*
250                          * BeEcompErrorManager.getInstance().processEcompError( EcompErrorName.BeRestApiGeneralError, "Update Resource Instance" );
251                          */
252                         BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Update Resource Instance");
253                         log.debug("update resource instance with exception", e);
254                         return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
255                 }
256
257         }
258
259         @DELETE
260         @Path("/{containerComponentType}/{componentId}/resourceInstance/{resourceInstanceId}")
261         @Consumes(MediaType.APPLICATION_JSON)
262         @Produces(MediaType.APPLICATION_JSON)
263         @ApiOperation(value = "Delete ResourceInstance", httpMethod = "DELETE", notes = "Returns delete resourceInstance", response = Response.class)
264         @ApiResponses(value = { @ApiResponse(code = 201, message = "ResourceInstance deleted"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
265         public Response deleteResourceInstance(@PathParam("componentId") final String componentId, @PathParam("resourceInstanceId") final String resourceInstanceId,
266                         @ApiParam(value = "valid values: resources / services / products", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME + ","
267                                         + ComponentTypeEnum.PRODUCT_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
268                         @Context final HttpServletRequest request) {
269                 ServletContext context = request.getSession().getServletContext();
270                 String url = request.getMethod() + " " + request.getRequestURI();
271                 Response response = null;
272                 try {
273                         log.debug("Start handle request of {}", url);
274                         ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
275                         ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
276                         if (componentInstanceLogic == null) {
277                                 log.debug("Unsupported component type {}", containerComponentType);
278                                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
279                         }
280                         String userId = request.getHeader(Constants.USER_ID_HEADER);
281                         Either<ComponentInstance, ResponseFormat> actionResponse = componentInstanceLogic.deleteComponentInstance(containerComponentType, componentId, resourceInstanceId, userId);
282
283                         if (actionResponse.isRight()) {
284                                 response = buildErrorResponse(actionResponse.right().value());
285                         } else {
286                                 response = buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.NO_CONTENT), null);
287                         }
288                         return response;
289                 } catch (Exception e) {
290                         BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Delete Resource Instance");
291                         log.debug("delete resource instance with exception", e);
292                         return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
293                 }
294         }
295
296         @ApiParam(value = "allowed values are resources /services / products", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME + "," + ComponentTypeEnum.PRODUCT_PARAM_NAME, required = true)
297         @POST
298         @Path("/{containerComponentType}/{componentId}/resourceInstance/associate")
299         @Consumes(MediaType.APPLICATION_JSON)
300         @Produces(MediaType.APPLICATION_JSON)
301         @ApiOperation(value = "Associate RI to RI", httpMethod = "POST", notes = "Returns created RelationshipInfo", response = Response.class)
302         @ApiResponses(value = { @ApiResponse(code = 201, message = "Relationship created"), @ApiResponse(code = 403, message = "Missing information"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
303                         @ApiResponse(code = 409, message = "Relationship already exist") })
304         public Response associateRIToRI(@ApiParam(value = "unique id of the container component") @PathParam("componentId") final String componentId,
305                         @ApiParam(value = "allowed values are resources /services / products", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME + ","
306                                         + ComponentTypeEnum.PRODUCT_PARAM_NAME, required = true) @PathParam("containerComponentType") final String containerComponentType,
307                         @HeaderParam(value = Constants.USER_ID_HEADER) String userId, @ApiParam(value = "RelationshipInfo", required = true) String data, @Context final HttpServletRequest request) {
308                 ServletContext context = request.getSession().getServletContext();
309
310                 String url = request.getMethod() + " " + request.getRequestURI();
311                 log.debug("Start handle request of {}", url);
312                 Response response = null;
313
314                 try {
315
316                         log.debug("Start handle request of {}", url);
317
318                         ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
319                         ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
320                         if (componentInstanceLogic == null) {
321                                 log.debug("Unsupported component type {}", containerComponentType);
322                                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
323                         }
324
325                         Either<RequirementCapabilityRelDef, ResponseFormat> regInfoW = convertToRequirementCapabilityRelDef(data);
326
327                         Either<RequirementCapabilityRelDef, ResponseFormat> resultOp;
328                         if (regInfoW.isRight()) {
329                                 BeEcompErrorManager.getInstance().logBeSystemError("Resource Instance - associateRIToRI");
330                                 log.debug("Failed to convert received data to BE format.");
331                                 resultOp = Either.right(regInfoW.right().value());
332                         } else {
333                                 RequirementCapabilityRelDef requirementDef = regInfoW.left().value();
334                                 resultOp = componentInstanceLogic.associateRIToRI(componentId, userId, requirementDef, componentTypeEnum);
335                         }
336
337                         Either<RequirementCapabilityRelDef, ResponseFormat> actionResponse = resultOp;
338
339                         if (actionResponse.isRight()) {
340                                 response = buildErrorResponse(actionResponse.right().value());
341                         } else {
342                                 response = buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse.left().value());
343                         }
344                         return response;
345
346                 } catch (Exception e) {
347                         BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Associate Resource Instance");
348                         log.debug("associate resource instance to another RI with exception", e);
349                         return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
350                 }
351         }
352
353         @PUT
354         @Path("/{containerComponentType}/{componentId}/resourceInstance/dissociate")
355         @Consumes(MediaType.APPLICATION_JSON)
356         @Produces(MediaType.APPLICATION_JSON)
357         @ApiOperation(value = "Dissociate RI from RI", httpMethod = "PUT", notes = "Returns deleted RelationshipInfo", response = Response.class)
358         @ApiResponses(value = { @ApiResponse(code = 201, message = "Relationship deleted"), @ApiResponse(code = 403, message = "Missing information"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
359         public Response dissociateRIFromRI(
360                         @ApiParam(value = "allowed values are resources /services / products", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME + ","
361                                         + ComponentTypeEnum.PRODUCT_PARAM_NAME, required = true) @PathParam("containerComponentType") final String containerComponentType,
362                         @ApiParam(value = "unique id of the container component") @PathParam("componentId") final String componentId, @HeaderParam(value = Constants.USER_ID_HEADER) String userId, @ApiParam(value = "RelationshipInfo", required = true) String data,
363                         @Context final HttpServletRequest request) {
364                 ServletContext context = request.getSession().getServletContext();
365
366                 String url = request.getMethod() + " " + request.getRequestURI();
367                 log.debug("Start handle request of {}", url);
368
369                 try {
370
371                         log.debug("Start handle request of {}", url);
372
373                         ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
374                         ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
375                         if (componentInstanceLogic == null) {
376                                 log.debug("Unsupported component type {}", containerComponentType);
377                                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
378                         }
379
380                         Either<RequirementCapabilityRelDef, ResponseFormat> regInfoW = convertToRequirementCapabilityRelDef(data);
381                         if (regInfoW.isRight()) {
382                                 BeEcompErrorManager.getInstance().logBeSystemError("Resource Instance - dissociateRIFromRI");
383                                 log.debug("Failed to convert received data to BE format.");
384                                 return buildErrorResponse(regInfoW.right().value());
385                         }
386
387                         RequirementCapabilityRelDef requirementDef = regInfoW.left().value();
388                         Either<RequirementCapabilityRelDef, ResponseFormat> actionResponse = componentInstanceLogic.dissociateRIFromRI(componentId, userId, requirementDef, componentTypeEnum);
389
390                         if (actionResponse.isRight()) {
391                                 return buildErrorResponse(actionResponse.right().value());
392                         }
393                         return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse.left().value());
394
395                 } catch (Exception e) {
396                         BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Dissociate Resource Instance");
397                         log.debug("dissociate resource instance from service failed with exception", e);
398                         return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
399                 }
400         }
401
402         @POST
403         @Path("/{containerComponentType}/{componentId}/resourceInstance/createAndAssociate")
404         @Consumes(MediaType.APPLICATION_JSON)
405         @Produces(MediaType.APPLICATION_JSON)
406         @ApiOperation(value = "Create RI and associate RI to RI", httpMethod = "POST", notes = "Returns created RI and RelationshipInfo", response = Response.class)
407         @ApiResponses(value = { @ApiResponse(code = 201, message = "RI created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
408                         @ApiResponse(code = 409, message = "Relationship already exist") })
409         public Response createAndAssociateRIToRI(@PathParam("componentId") final String componentId,
410                         @ApiParam(value = "valid values: resources / services", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
411                         @Context final HttpServletRequest request) {
412                 ServletContext context = request.getSession().getServletContext();
413
414                 String url = request.getMethod() + " " + request.getRequestURI();
415                 log.debug("Start handle request of {}", url);
416                 try {
417
418                         log.debug("Start handle request of {}", url);
419
420                         InputStream inputStream = request.getInputStream();
421
422                         byte[] bytes = IOUtils.toByteArray(inputStream);
423
424                         if (bytes == null || bytes.length == 0) {
425                                 log.info("Empty body was sent.");
426                                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT));
427                         }
428
429                         String userId = request.getHeader(Constants.USER_ID_HEADER);
430
431                         String data = new String(bytes);
432
433                         ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
434                         ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
435                         if (componentInstanceLogic == null) {
436                                 log.debug("Unsupported component type {}", containerComponentType);
437                                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
438                         }
439
440                         Either<CreateAndAssotiateInfo, ActionStatus> convertStatus = convertJsonToObject(data, CreateAndAssotiateInfo.class);
441                         if (convertStatus.isRight()) {
442                                 BeEcompErrorManager.getInstance().logBeSystemError("Resource Instance - createAndAssociateRIToRI");
443                                 log.debug("Failed to convert received data to BE format.");
444                                 Either<Object, ResponseFormat> formattedResponse = Either.right(getComponentsUtils().getResponseFormat(convertStatus.right().value()));
445                                 return buildErrorResponse(formattedResponse.right().value());
446                         }
447
448                         CreateAndAssotiateInfo createAndAssotiateInfo = convertStatus.left().value();
449                         Either<CreateAndAssotiateInfo, ResponseFormat> actionResponse = componentInstanceLogic.createAndAssociateRIToRI(containerComponentType, componentId, userId, createAndAssotiateInfo);
450
451                         if (actionResponse.isRight()) {
452                                 return buildErrorResponse(actionResponse.right().value());
453                         }
454                         return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.CREATED), actionResponse.left().value());
455                 } catch (Exception e) {
456                         BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Create and Associate Resource Instance");
457                         log.debug("create and associate RI failed with exception", e);
458                         return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
459                 }
460         }
461
462         @POST
463         @Path("/{containerComponentType}/{componentId}/resourceInstance/{componentInstanceId}/property")
464         @Consumes(MediaType.APPLICATION_JSON)
465         @Produces(MediaType.APPLICATION_JSON)
466         @ApiOperation(value = "Update resource instance property", httpMethod = "POST", notes = "Returns updated resource instance property", response = Response.class)
467         @ApiResponses(value = { @ApiResponse(code = 201, message = "Resource instance created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
468         public Response updateResourceInstanceProperty(@ApiParam(value = "service id") @PathParam("componentId") final String componentId,
469                         @ApiParam(value = "valid values: resources / services", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
470                         @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,
471                         @Context final HttpServletRequest request) {
472
473                 String url = request.getMethod() + " " + request.getRequestURI();
474                 log.debug("Start handle request of {}", url);
475
476                 try {
477                         Wrapper<String> dataWrapper = new Wrapper<>();
478                         Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
479                         Wrapper<ComponentInstanceProperty> propertyWrapper = new Wrapper<>();
480
481                         validateInputStream(request, dataWrapper, errorWrapper);
482
483                         if (errorWrapper.isEmpty()) {
484                                 validateClassParse(dataWrapper.getInnerElement(), propertyWrapper, () -> ComponentInstanceProperty.class, errorWrapper);
485                         }
486
487                         if (!errorWrapper.isEmpty()) {
488                                 return buildErrorResponse(errorWrapper.getInnerElement());
489                         }
490
491                         ComponentInstanceProperty property = propertyWrapper.getInnerElement();
492
493                         log.debug("Start handle request of updateResourceInstanceProperty. Received property is {}", property);
494
495                         ServletContext context = request.getSession().getServletContext();
496
497                         ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
498                         ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
499                         if (componentInstanceLogic == null) {
500                                 log.debug("Unsupported component type {}", containerComponentType);
501                                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
502                         }
503
504                         Either<ComponentInstanceProperty, ResponseFormat> actionResponse = componentInstanceLogic.createOrUpdatePropertyValue(componentTypeEnum, componentId, componentInstanceId, property, userId);
505
506                         if (actionResponse.isRight()) {
507                                 return buildErrorResponse(actionResponse.right().value());
508                         }
509
510                         ComponentInstanceProperty resourceInstanceProperty = actionResponse.left().value();
511                         ObjectMapper mapper = new ObjectMapper();
512                         String result = mapper.writeValueAsString(resourceInstanceProperty);
513                         return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), result);
514
515                 } catch (Exception e) {
516                         log.error("create and associate RI failed with exception: {}", e.getMessage(), e);
517                         return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
518                 }
519
520         }
521         
522         @POST
523         @Path("/{containerComponentType}/{componentId}/resourceInstance/{componentInstanceId}/input")
524         @Consumes(MediaType.APPLICATION_JSON)
525         @Produces(MediaType.APPLICATION_JSON)
526         @ApiOperation(value = "Update resource instance property", httpMethod = "POST", notes = "Returns updated resource instance property", response = Response.class)
527         @ApiResponses(value = { @ApiResponse(code = 201, message = "Resource instance created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
528         public Response updateResourceInstanceInput(@ApiParam(value = "service id") @PathParam("componentId") final String componentId,
529                         @ApiParam(value = "valid values: resources / services", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
530                         @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,
531                         @Context final HttpServletRequest request) {
532
533                 String url = request.getMethod() + " " + request.getRequestURI();
534                 log.debug("Start handle request of {}", url);
535
536                 try {
537                         Wrapper<String> dataWrapper = new Wrapper<>();
538                         Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
539                         Wrapper<ComponentInstanceInput> propertyWrapper = new Wrapper<>();
540                         
541                         validateInputStream(request, dataWrapper, errorWrapper);
542                         ComponentInstanceInput property = null;
543
544                         if (errorWrapper.isEmpty()) {
545                                 User modifier = new User();
546                                 modifier.setUserId(userId);
547                                 log.debug("modifier id is {}", userId);
548                                 
549                                 Either<ComponentInstanceInput, ResponseFormat> inputEither = getComponentsUtils().convertJsonToObjectUsingObjectMapper(dataWrapper.getInnerElement(), modifier, ComponentInstanceInput.class, AuditingActionEnum.UPDATE_RESOURCE_METADATA, ComponentTypeEnum.SERVICE);;
550                                 if(inputEither.isRight()){
551                                         log.debug("Failed to convert data to input definition. Status is {}", inputEither.right().value());
552                                         return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT));
553                                 }
554                                 property = inputEither.left().value();
555                                 
556                         }
557
558                         if (property == null) {
559                                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT));
560                         }
561
562                         
563                         log.debug("Start handle request of updateResourceInstanceProperty. Received property is {}", property);
564
565                         ServletContext context = request.getSession().getServletContext();
566
567                         ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
568                         ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
569                         if (componentInstanceLogic == null) {
570                                 log.debug("Unsupported component type {}", containerComponentType);
571                                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
572                         }
573
574                         Either<ComponentInstanceInput, ResponseFormat> actionResponse = componentInstanceLogic.createOrUpdateInstanceInputValue(componentTypeEnum, componentId, componentInstanceId, property, userId);
575
576                         if (actionResponse.isRight()) {
577                                 return buildErrorResponse(actionResponse.right().value());
578                         }
579
580                         ComponentInstanceInput resourceInstanceProperty = actionResponse.left().value();
581                         ObjectMapper mapper = new ObjectMapper();
582                         String result = mapper.writeValueAsString(resourceInstanceProperty);
583                         return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), result);
584
585                 } catch (Exception e) {
586                         log.error("create and associate RI failed with exception: {}", e.getMessage(), e);
587                         return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
588                 }
589
590         }
591
592         /**
593          * Updates ResourceInstance Attribute
594          * 
595          * @param componentId
596          * @param containerComponentType
597          * @param componentInstanceId
598          * @param userId
599          * @param request
600          * @return
601          */
602         @POST
603         @Path("/{containerComponentType}/{componentId}/resourceInstance/{componentInstanceId}/attribute")
604         @Consumes(MediaType.APPLICATION_JSON)
605         @Produces(MediaType.APPLICATION_JSON)
606         @ApiOperation(value = "Update resource instance attribute", httpMethod = "POST", notes = "Returns updated resource instance attribute", response = Response.class)
607         @ApiResponses(value = { @ApiResponse(code = 201, message = "Resource instance created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
608         public Response updateResourceInstanceAttribute(@ApiParam(value = "service id") @PathParam("componentId") final String componentId,
609                         @ApiParam(value = "valid values: resources / services", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
610                         @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,
611                         @Context final HttpServletRequest request) {
612
613                 String url = request.getMethod() + " " + request.getRequestURI();
614                 log.debug("Start handle request of {}", url);
615
616                 try {
617
618                         Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
619                         Wrapper<String> dataWrapper = new Wrapper<>();
620                         Wrapper<ComponentInstanceProperty> attributeWrapper = new Wrapper<>();
621                         Wrapper<ComponentInstanceBusinessLogic> blWrapper = new Wrapper<>();
622
623                         validateInputStream(request, dataWrapper, errorWrapper);
624
625                         if (errorWrapper.isEmpty()) {
626                                 validateClassParse(dataWrapper.getInnerElement(), attributeWrapper, () -> ComponentInstanceProperty.class, errorWrapper);
627                         }
628
629                         if (errorWrapper.isEmpty()) {
630                                 validateComponentInstanceBusinessLogic(request, containerComponentType, blWrapper, errorWrapper);
631                         }
632
633                         if (errorWrapper.isEmpty()) {
634                                 ComponentInstanceBusinessLogic componentInstanceLogic = blWrapper.getInnerElement();
635                                 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
636                                 log.debug("Start handle request of ComponentInstanceAttribute. Received attribute is {}", attributeWrapper.getInnerElement());
637                                 Either<ComponentInstanceProperty, ResponseFormat> eitherAttribute = componentInstanceLogic.createOrUpdateAttributeValue(componentTypeEnum, componentId, componentInstanceId, attributeWrapper.getInnerElement(), userId);
638                                 if (eitherAttribute.isRight()) {
639                                         errorWrapper.setInnerElement(eitherAttribute.right().value());
640                                 } else {
641                                         attributeWrapper.setInnerElement(eitherAttribute.left().value());
642                                 }
643                         }
644
645                         return buildResponseFromElement(errorWrapper, attributeWrapper);
646
647                 } catch (Exception e) {
648                         log.error("create and associate RI failed with exception: {}", e.getMessage(), e);
649                         return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
650                 }
651
652         }
653
654         @DELETE
655         @Path("/{containerComponentType}/{componentId}/resourceInstance/{componentInstanceId}/property/{propertyId}")
656         @Consumes(MediaType.APPLICATION_JSON)
657         @Produces(MediaType.APPLICATION_JSON)
658         @ApiOperation(value = "Update resource instance", httpMethod = "DELETE", notes = "Returns deleted resource instance property", response = Response.class)
659         @ApiResponses(value = { @ApiResponse(code = 201, message = "Resource instance created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
660         public Response deleteResourceInstanceProperty(@ApiParam(value = "service id") @PathParam("componentId") final String componentId,
661                         @ApiParam(value = "valid values: resources / services", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
662                         @ApiParam(value = "resource instance id") @PathParam("componentInstanceId") final String componentInstanceId, @ApiParam(value = "property id") @PathParam("propertyId") final String propertyId,
663                         @ApiParam(value = "id of user initiating the operation") @HeaderParam(value = Constants.USER_ID_HEADER) String userId, @Context final HttpServletRequest request) {
664
665                 ServletContext context = request.getSession().getServletContext();
666
667                 String url = request.getMethod() + " " + request.getRequestURI();
668                 log.debug("Start handle request of {}", url);
669                 try {
670
671                         ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
672                         ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
673                         if (componentInstanceLogic == null) {
674                                 log.debug("Unsupported component type {}", containerComponentType);
675                                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
676                         }
677
678                         Either<ComponentInstanceProperty, ResponseFormat> actionResponse = componentInstanceLogic.deletePropertyValue(componentTypeEnum, componentId, componentInstanceId, propertyId, userId);
679                         if (actionResponse.isRight()) {
680                                 return buildErrorResponse(actionResponse.right().value());
681                         }
682                         return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.NO_CONTENT), null);
683                 } catch (Exception e) {
684                         log.error("create and associate RI failed with exception: {}", e.getMessage(), e);
685                         return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
686                 }
687
688         }
689
690         @POST
691         @Path("/{containerComponentType}/{componentId}/resourceInstance/{componentInstanceId}/changeVersion")
692         @Consumes(MediaType.APPLICATION_JSON)
693         @Produces(MediaType.APPLICATION_JSON)
694         @ApiOperation(value = "Update resource instance", httpMethod = "POST", notes = "Returns updated resource instance", response = Response.class)
695         @ApiResponses(value = { @ApiResponse(code = 201, message = "Resource instance created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
696         public Response changeResourceInstanceVersion(@PathParam("componentId") final String componentId, @PathParam("componentInstanceId") final String componentInstanceId,
697                         @ApiParam(value = "valid values: resources / services", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
698                         @Context final HttpServletRequest request) {
699                 ServletContext context = request.getSession().getServletContext();
700
701                 String url = request.getMethod() + " " + request.getRequestURI();
702                 log.debug("Start handle request of {}", url);
703                 try {
704                         InputStream inputStream = request.getInputStream();
705
706                         byte[] bytes = IOUtils.toByteArray(inputStream);
707
708                         if (bytes == null || bytes.length == 0) {
709                                 log.info("Empty body was sent.");
710                                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT));
711                         }
712
713                         String userId = request.getHeader(Constants.USER_ID_HEADER);
714
715                         String data = new String(bytes);
716
717                         ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
718                         ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
719                         if (componentInstanceLogic == null) {
720                                 log.debug("Unsupported component type {}", containerComponentType);
721                                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
722                         }
723
724                         Either<ComponentInstance, ResponseFormat> convertResponse = convertToResourceInstance(data);
725
726                         if (convertResponse.isRight()) {
727                                 BeEcompErrorManager.getInstance().logBeSystemError("Resource Instance - updateResourceInstance");
728                                 log.debug("Failed to convert received data to BE format.");
729                                 return buildErrorResponse(convertResponse.right().value());
730                         }
731
732                         ComponentInstance newResourceInstance = convertResponse.left().value();
733                         Either<ComponentInstance, ResponseFormat> actionResponse = componentInstanceLogic.changeComponentInstanceVersion(containerComponentType, componentId, componentInstanceId, userId, newResourceInstance);
734
735                         if (actionResponse.isRight()) {
736                                 return buildErrorResponse(actionResponse.right().value());
737                         }
738                         return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse.left().value());
739
740                 } catch (Exception e) {
741                         BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Update Resource Instance");
742                         log.debug("update resource instance with exception", e);
743                         return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
744                 }
745
746         }
747         
748         @POST
749         @Path("/{containerComponentType}/{componentId}/resourceInstance/{componentInstanceId}/groupInstance/{groupInstanceId}/property")
750         @Consumes(MediaType.APPLICATION_JSON)
751         @Produces(MediaType.APPLICATION_JSON)
752         @ApiOperation(value = "Update resource instance property", httpMethod = "POST", notes = "Returns updated resource instance property", response = Response.class)
753         @ApiResponses(value = { @ApiResponse(code = 201, message = "Resource instance created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
754         public Response updateGroupInstanceProperty(@ApiParam(value = "service id") @PathParam("componentId") final String componentId,
755                         @ApiParam(value = "valid values: resources / services", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
756                         @ApiParam(value = "resource instance id") @PathParam("componentInstanceId") final String componentInstanceId, @ApiParam(value = "group instance id") @PathParam("groupInstanceId") final String groupInstanceId,  @ApiParam(value = "id of user initiating the operation") @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
757                         @Context final HttpServletRequest request) {
758
759                 String url = request.getMethod() + " " + request.getRequestURI();
760                 log.debug("Start handle request of {}", url);
761
762                 try {
763                         Wrapper<String> dataWrapper = new Wrapper<>();
764                         Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
765                         Wrapper<ComponentInstanceProperty> propertyWrapper = new Wrapper<>();
766
767                         validateInputStream(request, dataWrapper, errorWrapper);
768
769                         if (errorWrapper.isEmpty()) {
770                                 validateClassParse(dataWrapper.getInnerElement(), propertyWrapper, () -> ComponentInstanceProperty.class, errorWrapper);
771                         }
772
773                         if (!errorWrapper.isEmpty()) {
774                                 return buildErrorResponse(errorWrapper.getInnerElement());
775                         }
776
777                         ComponentInstanceProperty property = propertyWrapper.getInnerElement();
778
779                         log.debug("Start handle request of updateResourceInstanceProperty. Received property is {}", property);
780
781                         ServletContext context = request.getSession().getServletContext();
782
783                         ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
784                         ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
785                         if (componentInstanceLogic == null) {
786                                 log.debug("Unsupported component type {}", containerComponentType);
787                                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
788                         }
789
790                         Either<ComponentInstanceProperty, ResponseFormat> actionResponse = componentInstanceLogic.createOrUpdateGroupInstancePropertyValue(componentTypeEnum, componentId, componentInstanceId, groupInstanceId, property, userId);
791
792                         if (actionResponse.isRight()) {
793                                 return buildErrorResponse(actionResponse.right().value());
794                         }
795
796                         ComponentInstanceProperty resourceInstanceProperty = actionResponse.left().value();
797                         ObjectMapper mapper = new ObjectMapper();
798                         String result = mapper.writeValueAsString(resourceInstanceProperty);
799                         return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), result);
800
801                 } catch (Exception e) {
802                         log.error("create and associate RI failed with exception: {}", e.getMessage(), e);
803                         return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
804                 }
805
806         }
807         
808         @GET
809         @Path("/{containerComponentType}/{componentId}/resourceInstance/{componentInstanceId}/groupInstance/{groupInstId}")
810         @Consumes(MediaType.APPLICATION_JSON)
811         @Produces(MediaType.APPLICATION_JSON)
812         @ApiOperation(value = "Get group artifacts ", httpMethod = "GET", notes = "Returns artifacts metadata according to groupInstId", response = Resource.class)
813         @ApiResponses(value = { @ApiResponse(code = 200, message = "group found"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 404, message = "Group not found") })
814         public Response getGroupArtifactById(@PathParam("containerComponentType") final String containerComponentType, @PathParam("componentId") final String componentId, @PathParam("componentInstanceId") final String componentInstanceId, @PathParam("groupInstId") final String groupInstId,
815                         @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
816                 ServletContext context = request.getSession().getServletContext();
817                 String url = request.getMethod() + " " + request.getRequestURI();
818                 log.debug("(GET) Start handle request of {}", url);
819
820                 try {
821
822                         GroupBusinessLogic businessLogic = this.getGroupBL(context);
823                         ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
824                         Either<GroupDefinitionInfo, ResponseFormat> actionResponse = businessLogic.getGroupInstWithArtifactsById(componentTypeEnum, componentId, componentInstanceId, groupInstId, userId, false);
825
826                         if (actionResponse.isRight()) {
827                                 log.debug("failed to get all non abstract {}", containerComponentType);
828                                 return buildErrorResponse(actionResponse.right().value());
829                         }
830
831                         return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse.left().value());
832
833                 } catch (Exception e) {
834                         BeEcompErrorManager.getInstance().logBeRestApiGeneralError("getGroupArtifactById");
835                         log.debug("getGroupArtifactById unexpected exception", e);
836                         return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
837                 }
838
839         }
840         
841         //US831698
842         @GET
843         @Path("/{containerComponentType}/{containerComponentId}/componentInstances/{componentInstanceUniqueId}/properties")
844         @Consumes(MediaType.APPLICATION_JSON)
845         @Produces(MediaType.APPLICATION_JSON)
846         @ApiOperation(value = "Get component instance properties", httpMethod = "GET", 
847         notes = "Returns component instance properties", response = Response.class)
848         @ApiResponses(value = { @ApiResponse(code = 200, message = "Properties found"), 
849                         @ApiResponse(code = 403, message = "Restricted operation"), 
850                         @ApiResponse(code = 404, message = "Component/Component Instance - not found") })
851         public Response getInstancePropertiesById(@PathParam("containerComponentType") final String containerComponentType, 
852                         @PathParam("containerComponentId") final String containerComponentId, 
853                         @PathParam("componentInstanceUniqueId") final String componentInstanceUniqueId,
854                         @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
855                 
856                 ServletContext context = request.getSession().getServletContext();
857                 String url = request.getMethod() + " " + request.getRequestURI();
858                 log.debug("(GET) Start handle request of {}", url);
859
860                 try {
861                         ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
862                         ComponentInstanceBusinessLogic componentInstanceBL = getComponentInstanceBL(context, componentTypeEnum);
863                         
864                         Either<List<ComponentInstanceProperty>, ResponseFormat> componentInstancePropertiesById = componentInstanceBL
865                                         .getComponentInstancePropertiesById(containerComponentType, containerComponentId, 
866                                                         componentInstanceUniqueId, userId);
867
868                         if (componentInstancePropertiesById.isRight()) {
869                                 log.debug("Failed to get properties of component instance ID: {} in {} with ID: {}", 
870                                                 componentInstanceUniqueId, containerComponentType, containerComponentId);
871                                 return buildErrorResponse(componentInstancePropertiesById.right().value());
872                         }
873
874                         return buildOkResponse(getComponentsUtils().
875                                         getResponseFormat(ActionStatus.OK), componentInstancePropertiesById.left().value());
876                 } catch (Exception e) {
877                         BeEcompErrorManager.getInstance().logBeRestApiGeneralError("getGroupArtifactById");
878                         log.debug("getGroupArtifactById unexpected exception", e);
879                         return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
880                 }
881
882         }
883         
884         private Either<ComponentInstance, ResponseFormat> convertToResourceInstance(String data) {
885
886                 // Either<ComponentInstance, ActionStatus> convertStatus =
887                 // convertJsonToObject(data, ComponentInstance.class);
888                 Either<ComponentInstance, ResponseFormat> convertStatus = getComponentsUtils().convertJsonToObjectUsingObjectMapper(data, new User(), ComponentInstance.class, null, ComponentTypeEnum.RESOURCE_INSTANCE);
889                 if (convertStatus.isRight()) {
890                         return Either.right(convertStatus.right().value());
891                 }
892                 ComponentInstance resourceInstanceInfo = convertStatus.left().value();
893
894                 return Either.left(resourceInstanceInfo);
895         }
896
897         private Either<List<ComponentInstance>, ResponseFormat> convertToMultipleResourceInstance(String dataList) {
898
899                 Either<ComponentInstance[], ResponseFormat> convertStatus = getComponentsUtils().convertJsonToObjectUsingObjectMapper(dataList, new User(), ComponentInstance[].class, null, ComponentTypeEnum.RESOURCE_INSTANCE);
900                 if (convertStatus.isRight()) {
901                         return Either.right(convertStatus.right().value());
902                 }
903
904                 return Either.left(Arrays.asList(convertStatus.left().value()));
905         }
906
907         private Either<RequirementCapabilityRelDef, ResponseFormat> convertToRequirementCapabilityRelDef(String data) {
908
909                 Either<RequirementCapabilityRelDef, ActionStatus> convertStatus = convertJsonToObject(data, RequirementCapabilityRelDef.class);
910                 if (convertStatus.isRight()) {
911                         return Either.right(getComponentsUtils().getResponseFormat(convertStatus.right().value()));
912                 }
913                 RequirementCapabilityRelDef requirementCapabilityRelDef = convertStatus.left().value();
914                 return Either.left(requirementCapabilityRelDef);
915         }
916
917         private <T> Either<T, ActionStatus> convertJsonToObject(String data, Class<T> clazz) {
918                 try {
919                         log.trace("convert json to object. json=\n {}", data);
920                         T t = null;
921                         t = gson.fromJson(data, clazz);
922                         if (t == null) {
923                                 BeEcompErrorManager.getInstance().logBeInvalidJsonInput("convertJsonToObject");
924                                 log.debug("object is null after converting from json");
925                                 return Either.right(ActionStatus.INVALID_CONTENT);
926                         }
927                         return Either.left(t);
928                 } catch (Exception e) {
929                         // INVALID JSON
930                         BeEcompErrorManager.getInstance().logBeInvalidJsonInput("convertJsonToObject");
931                         log.debug("failed to convert from json", e);
932                         return Either.right(ActionStatus.INVALID_CONTENT);
933                 }
934         }
935 }