From 4eb4059162269fea685136572257a48345873ec7 Mon Sep 17 00:00:00 2001 From: "Sindhuri.A" Date: Wed, 17 Oct 2018 13:25:27 +0530 Subject: Batch delete keyboard shorcut on canvas Keyboard Shortcut for batch delete (multiple select nodes and delete) on Composition page canvas Issue-ID: SDC-1734 Change-Id: I7de5d95a1ca6ea27cdd257b20bfdc1ed9b0ce102 Signed-off-by: Sindhuri.A --- .../impl/ComponentInstanceBusinessLogic.java | 227 ++++++++++++++++++--- .../sdc/be/servlets/ComponentInstanceServlet.java | 131 +++++++++++- 2 files changed, 323 insertions(+), 35 deletions(-) (limited to 'catalog-be/src/main/java/org/openecomp') diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentInstanceBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentInstanceBusinessLogic.java index 1f4eb539d6..e5e8486d94 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentInstanceBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentInstanceBusinessLogic.java @@ -1094,10 +1094,72 @@ public class ComponentInstanceBusinessLogic extends BaseBusinessLogic { } - public Either dissociateRIFromRI(String componentId, String userId, RequirementCapabilityRelDef requirementDef, ComponentTypeEnum componentTypeEnum) { + /** + * @param componentId + * @param userId + * @param requirementDefList + * @param componentTypeEnum + * @return + */ + public List batchDissociateRIFromRI( + String componentId, + String userId, + List requirementDefList, + ComponentTypeEnum componentTypeEnum) { + + List delOkResult = new ArrayList<>(); + Either validateResponse = validateDissociateRI(componentId, userId, componentTypeEnum); + if (validateResponse.isRight()) { + + return delOkResult; + } + Component containerComponent = validateResponse.left().value(); + Either lockComponent = lockComponent(containerComponent, "associateRIToRI"); + if (lockComponent.isRight()) { + return delOkResult; + } + try { + for (RequirementCapabilityRelDef requirementDef : requirementDefList) { + Either actionResponse = dissociateRIFromRI( + componentId, requirementDef, containerComponent); + + if (actionResponse.isLeft()) { + delOkResult.add(actionResponse.left().value()); + } + } + } finally { + unlockComponent(validateResponse, containerComponent); + } + return delOkResult; + } + + public Either dissociateRIFromRI( + String componentId, String userId, RequirementCapabilityRelDef requirementDef, ComponentTypeEnum componentTypeEnum) { + Either validateResponse = validateDissociateRI(componentId, userId, componentTypeEnum); + if(validateResponse.isRight()) + { + return Either.right(validateResponse.right().value()); + } + Either actionResponse = null; + Component containerComponent = validateResponse.left().value(); + Either lockComponent = lockComponent(containerComponent, "associateRIToRI"); + if (lockComponent.isRight()) { + return Either.right(lockComponent.right().value()); + } + try { + actionResponse = dissociateRIFromRI( + componentId, requirementDef,containerComponent); + } finally { + unlockComponent(validateResponse, containerComponent); + } + return actionResponse; + } + + private Either validateDissociateRI( + String componentId, String userId, ComponentTypeEnum componentTypeEnum) { validateUserExists(userId, "dissociate RI From RI", false); - Either resultOp = null; + Either validateComponentExists = validateComponentExists(componentId, componentTypeEnum, null); if (validateComponentExists.isRight()) { return Either.right(validateComponentExists.right().value()); @@ -1108,45 +1170,49 @@ public class ComponentInstanceBusinessLogic extends BaseBusinessLogic { if (validateCanWorkOnComponent.isRight()) { return Either.right(validateCanWorkOnComponent.right().value()); } - Either lockComponent = lockComponent(containerComponent, "associateRIToRI"); + return Either.left(containerComponent); - if (lockComponent.isRight()) { - return Either.right(lockComponent.right().value()); - } - try { - log.debug("Try to create entry on graph"); - Either result = toscaOperationFacade.dissociateResourceInstances(componentId, requirementDef); - if (result.isLeft()) { - log.debug("Enty on graph is created."); - RequirementCapabilityRelDef requirementCapabilityRelDef = result.left().value(); - resultOp = Either.left(requirementCapabilityRelDef); - return resultOp; + } + private Either dissociateRIFromRI( + String componentId, RequirementCapabilityRelDef requirementDef, Component containerComponent) { - } else { + Either resultOp = null; + log.debug("Try to create entry on graph"); + Either result = toscaOperationFacade.dissociateResourceInstances( + componentId, requirementDef); + if (result.isLeft()) { + log.debug("Enty on graph is created."); + RequirementCapabilityRelDef requirementCapabilityRelDef = result.left().value(); + resultOp = Either.left(requirementCapabilityRelDef); + return resultOp; - log.debug("Failed to dissocaite node {} from node {}", requirementDef.getFromNode(), requirementDef.getToNode()); - String fromNameOrId = ""; - String toNameOrId = ""; - Either fromResult = getResourceInstanceById(containerComponent, requirementDef.getFromNode()); - Either toResult = getResourceInstanceById(containerComponent, requirementDef.getToNode()); + } else { - toNameOrId = requirementDef.getFromNode(); - fromNameOrId = requirementDef.getFromNode(); - if (fromResult.isLeft()) { - fromNameOrId = fromResult.left().value().getName(); - } - if (toResult.isLeft()) { - toNameOrId = toResult.left().value().getName(); - } + log.debug("Failed to dissocaite node {} from node {}", requirementDef.getFromNode(), requirementDef.getToNode()); + String fromNameOrId = ""; + String toNameOrId = ""; + Either fromResult = getResourceInstanceById( + containerComponent, requirementDef.getFromNode()); + Either toResult = getResourceInstanceById( + containerComponent, requirementDef.getToNode()); - resultOp = Either - .right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponseForResourceInstance(result.right().value(), true), fromNameOrId, toNameOrId, requirementDef.getRelationships().get(0).getRelation().getRequirement())); - return resultOp; + toNameOrId = requirementDef.getFromNode(); + fromNameOrId = requirementDef.getFromNode(); + if (fromResult.isLeft()) { + fromNameOrId = fromResult.left().value().getName(); } - } finally { - unlockComponent(resultOp, containerComponent); + if (toResult.isLeft()) { + toNameOrId = toResult.left().value().getName(); + } + + resultOp = Either + .right(componentsUtils.getResponseFormat( + componentsUtils.convertFromStorageResponseForResourceInstance( + result.right().value(), true), fromNameOrId, toNameOrId, requirementDef.getRelationships().get(0).getRelation().getRequirement())); + return resultOp; } } + /** * Allows to get relation contained in specified component according to received Id * @param componentId @@ -2820,7 +2886,7 @@ public class ComponentInstanceBusinessLogic extends BaseBusinessLogic { sourceAttribute.setUniqueId( UniqueIdBuilder.buildResourceInstanceUniuqeId( - "attribute" , destComponentInstanceId.split("\\.")[1] , sourceAttributeName)); + "attribute" , destComponentInstanceId.split("\\.")[1] , sourceAttributeName)); Either updateAttributeValueEither = createOrUpdateAttributeValueForCopyPaste(ComponentTypeEnum.SERVICE, @@ -2989,4 +3055,97 @@ public class ComponentInstanceBusinessLogic extends BaseBusinessLogic { return Either.right(componentsUtils.getResponseFormat( ActionStatus.USER_DEFINED, "Failed to paste component instance to the canvas")); } + + /** + * Method to delete selected nodes and edges on composition page + * @param containerComponentType + * @param componentId + * @param componentInstanceIdList + * @param userId + * @return + */ + public Map> batchDeleteComponentInstance(String containerComponentType, + String componentId, + List componentInstanceIdList, + String userId) { + + List deleteErrorIds = new ArrayList<>(); + Map> deleteErrorMap = new HashMap<>(); + Either validateResponse = validateUser(containerComponentType, componentId, userId); + if (validateResponse.isRight()) { + deleteErrorMap.put("deleteFailedIds", componentInstanceIdList); + return deleteErrorMap; + } + Component containerComponent = validateResponse.left().value(); + + Either lockComponent = lockComponent( + containerComponent, "batchDeleteComponentInstance"); + if (lockComponent.isRight()) { + log.error("Failed to lockComponent containerComponent"); + deleteErrorMap.put("deleteFailedIds", componentInstanceIdList); + return deleteErrorMap; + } + + try { + for (String eachInstanceId : componentInstanceIdList) { + Either actionResponse = batchDeleteComponentInstance( + containerComponent, containerComponentType, componentId, eachInstanceId); + log.debug("batchDeleteResourceInstances actionResponse is {}", actionResponse); + if (actionResponse.isRight()) { + log.error("Failed to delete ComponentInstance [{}]", eachInstanceId); + deleteErrorIds.add(eachInstanceId); + } + } + //sending the ids of the error nodes that were not deleted to UI + deleteErrorMap.put("deleteFailedIds", deleteErrorIds); + return deleteErrorMap; + } finally { + unlockComponent(validateResponse, containerComponent); + } + } + + private Either validateUser(String containerComponentParam, + String containerComponentId, + String userId) { + validateUserExists(userId, "delete Component Instance", false); + Either validateComponentType = validateComponentType(containerComponentParam); + if (validateComponentType.isRight()) { + log.error("ComponentType[{}] doesn't support", containerComponentParam); + return Either.right(validateComponentType.right().value()); + } + + final ComponentTypeEnum containerComponentType = validateComponentType.left().value(); + Either validateComponentExists = validateComponentExists( + containerComponentId, containerComponentType, null); + if (validateComponentExists.isRight()) { + log.error("Component Id[{}] doesn't exist", containerComponentId); + return Either.right(validateComponentExists.right().value()); + } + + Component containerComponent = validateComponentExists.left().value(); + Either validateCanWorkOnComponent = validateCanWorkOnComponent(containerComponent, userId); + if (validateCanWorkOnComponent.isRight()) { + return Either.right(validateCanWorkOnComponent.right().value()); + } + return Either.left(containerComponent); + } + + private Either batchDeleteComponentInstance(Component containerComponent, + String containerComponentType, + String containerComponentId, + String componentInstanceId) { + + Either resultOp; + final ComponentTypeEnum containerComponentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType); + + resultOp = deleteComponentInstance(containerComponent, componentInstanceId, containerComponentTypeEnum); + + if (resultOp.isRight()) { + log.error("Failed to deleteComponentInstance with instanceId[{}]", componentInstanceId); + return Either.right(resultOp.right().value()); + } + + log.info("Successfully deleted instance with id {}", componentInstanceId); + return Either.left(resultOp.left().value()); + } } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ComponentInstanceServlet.java b/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ComponentInstanceServlet.java index 3195727a68..042303e2c3 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ComponentInstanceServlet.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ComponentInstanceServlet.java @@ -1244,9 +1244,138 @@ public class ComponentInstanceServlet extends AbstractValidationsServlet { return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), copyComponentInstance.left().value()); } catch (Exception e) { - log.error("Failed to convert json to Map { }, error: { }", data, e); + log.error("Failed to convert json to Map { }", data, e); return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.USER_DEFINED, "Failed to get the copied component instance information")); } } + + @POST + @Path("/{containerComponentType}/{componentId}/batchDeleteResourceInstances/") + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.APPLICATION_JSON) + @ApiOperation(value = "Batch Delete ResourceInstances", httpMethod = "POST") + @ApiResponses(value = { + @ApiResponse(code = 203, message = "ResourceInstances deleted"), + @ApiResponse(code = 403, message = "Restricted Operation"), + @ApiResponse(code = 400, message = "Invalid Content / Missing Content") + }) + public Response batchDeleteResourceInstances( + @ApiParam(value = "valid values: resources / services / products", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME + "," + + ComponentTypeEnum.PRODUCT_PARAM_NAME) + @PathParam("containerComponentType") final String containerComponentType, + @PathParam("componentId") final String componentId, + @Context final HttpServletRequest request, + @ApiParam(value = "Component Instance Id List", required = true) final String componentInstanceIdLisStr) { + ServletContext context = request.getSession().getServletContext(); + try { + if (componentInstanceIdLisStr == null || componentInstanceIdLisStr.isEmpty()) { + log.error("Empty JSON List was sent",componentInstanceIdLisStr); + return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT)); + } + + + ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context); + if (componentInstanceLogic == null) { + log.error("Unsupported component type {}", containerComponentType); + return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType)); + } + + Either, ResponseFormat> convertResponse = convertToStringList(componentInstanceIdLisStr); + + if (convertResponse.isRight()) { + BeEcompErrorManager.getInstance().logBeSystemError("Resource Instance - batchDeleteResourceInstances"); + log.error("Failed to convert received data to BE format."); + return buildErrorResponse(convertResponse.right().value()); + } + + String userId = request.getHeader(Constants.USER_ID_HEADER); + List componentInstanceIdList = convertResponse.left().value(); + log.debug("batchDeleteResourceInstances componentInstanceIdList is {}", componentInstanceIdList); + Map> deleteErrorMap = componentInstanceLogic.batchDeleteComponentInstance(containerComponentType, + componentId, componentInstanceIdList, userId); + + return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), deleteErrorMap); + } catch (Exception e) { + BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Batch Delete ResourceInstances"); + log.error("batch delete resource instances with exception" , e); + return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR)); + } + + } + + @PUT + @Path("/{containerComponentType}/{componentId}/resourceInstance/batchDissociate") + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.APPLICATION_JSON) + @ApiOperation(value = "Batch Dissociate RI from RI", httpMethod = "PUT", notes = "Returns deleted RelationShip Info", response = Response.class) + @ApiResponses(value = { + @ApiResponse(code = 201, message = "Relationship deleted"), + @ApiResponse(code = 403, message = "Missing Information"), + @ApiResponse(code = 400, message = "Invalid Content / Missing Content") + }) + public Response batchDissociateRIFromRI( + @ApiParam(value = "allowed values are resources/services/products", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME + "," + ComponentTypeEnum.PRODUCT_PARAM_NAME, required = true) + @PathParam("containerComponentType") final String containerComponentType, + @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, + @Context final HttpServletRequest request) { + ServletContext context = request.getSession().getServletContext(); + + try { + if (data == null || data.length() == 0) { + log.info("Empty JSON list was sent"); + return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT)); + } + + ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType); + ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context); + + if (componentInstanceLogic == null) { + log.debug("Unsupported component type {}", containerComponentType); + return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType)); + } + + Either, ResponseFormat> regInfoWs = convertToRequirementCapabilityRelDefList(data); + + if (regInfoWs.isRight()) { + BeEcompErrorManager.getInstance().logBeSystemError("Resource Instance - batch dissociateRIFromRI"); + log.debug("Failed to convert received data to BE format"); + return buildErrorResponse(regInfoWs.right().value()); + } + + List requirementDefList = regInfoWs.left().value(); + List delOkResult = componentInstanceLogic.batchDissociateRIFromRI( + componentId, userId, requirementDefList, componentTypeEnum); + + return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), delOkResult); + } catch (Exception e) { + BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Batch Dissociate Resource Instance"); + log.debug("batch dissociate resource instance from service failed with exception", e); + return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR)); + } + } + + private Either, ResponseFormat> convertToStringList(String datalist) { + Either convertStatus = getComponentsUtils().convertJsonToObjectUsingObjectMapper(datalist, new User(), String[].class, null, null); + + if (convertStatus.isRight()) { + return Either.right(convertStatus.right().value()); + } + + return Either.left(Arrays.asList(convertStatus.left().value())); + } + + private Either, ResponseFormat> convertToRequirementCapabilityRelDefList(String data) { + Either convertStatus = getComponentsUtils().convertJsonToObjectUsingObjectMapper(data, new User(), RequirementCapabilityRelDef[].class, null, null); + + if (convertStatus.isRight()) { + return Either.right(convertStatus.right().value()); + } + + return Either.left(Arrays.asList(convertStatus.left().value())); + } + } -- cgit 1.2.3-korg