diff options
Diffstat (limited to 'catalog-be/src/main/java')
2 files changed, 323 insertions, 35 deletions
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<RequirementCapabilityRelDef, ResponseFormat> dissociateRIFromRI(String componentId, String userId, RequirementCapabilityRelDef requirementDef, ComponentTypeEnum componentTypeEnum) { + /** + * @param componentId + * @param userId + * @param requirementDefList + * @param componentTypeEnum + * @return + */ + public List<RequirementCapabilityRelDef> batchDissociateRIFromRI( + String componentId, + String userId, + List<RequirementCapabilityRelDef> requirementDefList, + ComponentTypeEnum componentTypeEnum) { + + List<RequirementCapabilityRelDef> delOkResult = new ArrayList<>(); + Either<Component, ResponseFormat> validateResponse = validateDissociateRI(componentId, userId, componentTypeEnum); + if (validateResponse.isRight()) { + + return delOkResult; + } + Component containerComponent = validateResponse.left().value(); + Either<Boolean, ResponseFormat> lockComponent = lockComponent(containerComponent, "associateRIToRI"); + if (lockComponent.isRight()) { + return delOkResult; + } + try { + for (RequirementCapabilityRelDef requirementDef : requirementDefList) { + Either<RequirementCapabilityRelDef, ResponseFormat> actionResponse = dissociateRIFromRI( + componentId, requirementDef, containerComponent); + + if (actionResponse.isLeft()) { + delOkResult.add(actionResponse.left().value()); + } + } + } finally { + unlockComponent(validateResponse, containerComponent); + } + return delOkResult; + } + + public Either<RequirementCapabilityRelDef, ResponseFormat> dissociateRIFromRI( + String componentId, String userId, RequirementCapabilityRelDef requirementDef, ComponentTypeEnum componentTypeEnum) { + Either<Component, ResponseFormat> validateResponse = validateDissociateRI(componentId, userId, componentTypeEnum); + if(validateResponse.isRight()) + { + return Either.right(validateResponse.right().value()); + } + Either<RequirementCapabilityRelDef, ResponseFormat> actionResponse = null; + Component containerComponent = validateResponse.left().value(); + Either<Boolean, ResponseFormat> 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<Component, ResponseFormat> validateDissociateRI( + String componentId, String userId, ComponentTypeEnum componentTypeEnum) { validateUserExists(userId, "dissociate RI From RI", false); - Either<RequirementCapabilityRelDef, ResponseFormat> resultOp = null; + Either<org.openecomp.sdc.be.model.Component, ResponseFormat> 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<Boolean, ResponseFormat> 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<RequirementCapabilityRelDef, StorageOperationStatus> 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<RequirementCapabilityRelDef, ResponseFormat> dissociateRIFromRI( + String componentId, RequirementCapabilityRelDef requirementDef, Component containerComponent) { - } else { + Either<RequirementCapabilityRelDef, ResponseFormat> resultOp = null; + log.debug("Try to create entry on graph"); + Either<RequirementCapabilityRelDef, StorageOperationStatus> 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<ComponentInstance, StorageOperationStatus> fromResult = getResourceInstanceById(containerComponent, requirementDef.getFromNode()); - Either<ComponentInstance, StorageOperationStatus> 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<ComponentInstance, StorageOperationStatus> fromResult = getResourceInstanceById( + containerComponent, requirementDef.getFromNode()); + Either<ComponentInstance, StorageOperationStatus> 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<ComponentInstanceProperty, ResponseFormat> 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<String, List<String>> batchDeleteComponentInstance(String containerComponentType, + String componentId, + List<String> componentInstanceIdList, + String userId) { + + List<String> deleteErrorIds = new ArrayList<>(); + Map<String, List<String>> deleteErrorMap = new HashMap<>(); + Either<Component, ResponseFormat> validateResponse = validateUser(containerComponentType, componentId, userId); + if (validateResponse.isRight()) { + deleteErrorMap.put("deleteFailedIds", componentInstanceIdList); + return deleteErrorMap; + } + Component containerComponent = validateResponse.left().value(); + + Either<Boolean, ResponseFormat> 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<ComponentInstance, ResponseFormat> 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<Component, ResponseFormat> validateUser(String containerComponentParam, + String containerComponentId, + String userId) { + validateUserExists(userId, "delete Component Instance", false); + Either<ComponentTypeEnum, ResponseFormat> 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<Component, ResponseFormat> 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<Boolean, ResponseFormat> validateCanWorkOnComponent = validateCanWorkOnComponent(containerComponent, userId); + if (validateCanWorkOnComponent.isRight()) { + return Either.right(validateCanWorkOnComponent.right().value()); + } + return Either.left(containerComponent); + } + + private Either<ComponentInstance, ResponseFormat> batchDeleteComponentInstance(Component containerComponent, + String containerComponentType, + String containerComponentId, + String componentInstanceId) { + + Either<ComponentInstance, ResponseFormat> 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<List<String>, 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<String> componentInstanceIdList = convertResponse.left().value(); + log.debug("batchDeleteResourceInstances componentInstanceIdList is {}", componentInstanceIdList); + Map<String, List<String>> 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<List<RequirementCapabilityRelDef>, 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<RequirementCapabilityRelDef> requirementDefList = regInfoWs.left().value(); + List<RequirementCapabilityRelDef> 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<List<String>, ResponseFormat> convertToStringList(String datalist) { + Either<String[], ResponseFormat> 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<List<RequirementCapabilityRelDef>, ResponseFormat> convertToRequirementCapabilityRelDefList(String data) { + Either<RequirementCapabilityRelDef[], ResponseFormat> 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())); + } + } |