@Test(expected = NullPointerException.class) public void testDoDeletePdp() throws IOException { CommonClassDao dao = Mockito.mock(CommonClassDao.class); DeleteHandler handler = new DeleteHandler(dao); OnapLoggingContext loggingContext = new OnapLoggingContext(); MockHttpServletRequest request = new MockHttpServletRequest(); request.setBodyContent( "{\n\"PAPPolicyType\": \"StdPAPPolicy\", \"policyName\": \"foo.Config_name.1.xml\", \"deleteCondition\": \"All Versions\"\n}\n"); MockHttpServletResponse response = new MockHttpServletResponse(); handler.doApiDeleteFromPdp(request, response, loggingContext); }
public void doApiDeleteFromPdp(HttpServletRequest request, HttpServletResponse response, OnapLoggingContext loggingContext) throws IOException { String groupId = request.getParameter("groupId"); if (groupId != null && !groupId.matches(REGEX)) { response.setStatus(HttpServletResponse.SC_BAD_REQUEST); response.addHeader(ERROR, ERROR); response.addHeader(MESSAGE, "Group Id is not valid"); return; } String requestId = request.getHeader("X-ECOMP-RequestID"); String polName = request.getParameter("policyName"); LOGGER.info("JSON request from API to Delete Policy from the PDP - " + polName + " -RequestId - " + requestId); OnapPDPGroup group = null; try { group = XACMLPapServlet.getPAPEngine().getGroup(groupId); } catch (PAPException e) { LOGGER.error("Exception occured While PUT operation is performing for PDP Group" + " - RequestId - " + requestId + e); } if (group == null) { String message = "Unknown groupId '" + groupId + "'."; LOGGER.error(MessageCodes.ERROR_DATA_ISSUE + " - RequestId - " + requestId + " - " + message); loggingContext.transactionEnded(); PolicyLogger.audit("Transaction Failed - See Error.log"); response.addHeader(ERROR, "UnknownGroup"); response.addHeader(MESSAGE, message); response.setStatus(HttpServletResponse.SC_NOT_FOUND); return; } loggingContext.setServiceName("API:PAP.deletPolicyFromPDPGroup"); RemoveGroupPolicy removePolicy = new RemoveGroupPolicy((StdPDPGroup) group); removePolicy.prepareToRemove(); List<String> policyIdList = Arrays.asList(polName.split(",")); for (String policyName : policyIdList) { if (!policyName.endsWith("xml")) { String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Invalid policyName... " + "policyName must be the full name of the file to be deleted including version and extension"; LOGGER.error(message + " - RequestId - " + requestId); response.addHeader(ERROR, message); response.setStatus(HttpServletResponse.SC_BAD_REQUEST); return; } PDPPolicy policy = group.getPolicy(policyName); if (policy == null) { String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Policy does not exist on the PDP - " + policyName; LOGGER.error(message + " - RequestId - " + requestId); response.addHeader(ERROR, message); response.setStatus(HttpServletResponse.SC_BAD_REQUEST); return; } if (StringUtils.indexOfAny(policy.getId(), "Config_MS_", "BRMS_Param") >= 0) { preSafetyCheck(policy); } LOGGER.info("Preparing to remove policy from group: " + group.getId() + "- RequestId - " + requestId); removePolicy.removePolicy(policy); } OnapPDPGroup updatedGroup = removePolicy.getUpdatedObject(); String userId = request.getParameter("userId"); String responseString = deletePolicyFromPdpGroup(updatedGroup, loggingContext, userId); switch (responseString) { case SUCCESS: loggingContext.transactionEnded(); LOGGER.info("Policy successfully removed from PDP - " + polName + " - RequestId - " + requestId); PolicyLogger.audit("Policy successfully deleted!"); response.setStatus(HttpServletResponse.SC_OK); response.addHeader("successMapKey", SUCCESS); response.addHeader(OPERATION, DELETE); break; case "No Group": String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Group update had bad input."; LOGGER.error(message + " - RequestId - " + requestId); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); response.addHeader(ERROR, "groupUpdate"); response.addHeader(MESSAGE, message); break; case "DB Error": LOGGER.error(MessageCodes.ERROR_PROCESS_FLOW + " Error while updating group in the database" + " - RequestId - " + requestId); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); response.addHeader(ERROR, "deleteDB"); break; default: LOGGER.error(MessageCodes.ERROR_UNKNOWN + " Failed to delete the policy for an unknown reason. Check the file system and other logs " + "for " + "further information."); response.addHeader(ERROR, UNKNOWN); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); break; } }
DeleteHandler { public void doApiDeleteFromPdp(HttpServletRequest request, HttpServletResponse response, OnapLoggingContext loggingContext) throws IOException { String groupId = request.getParameter("groupId"); if (groupId != null && !groupId.matches(REGEX)) { response.setStatus(HttpServletResponse.SC_BAD_REQUEST); response.addHeader(ERROR, ERROR); response.addHeader(MESSAGE, "Group Id is not valid"); return; } String requestId = request.getHeader("X-ECOMP-RequestID"); String polName = request.getParameter("policyName"); LOGGER.info("JSON request from API to Delete Policy from the PDP - " + polName + " -RequestId - " + requestId); OnapPDPGroup group = null; try { group = XACMLPapServlet.getPAPEngine().getGroup(groupId); } catch (PAPException e) { LOGGER.error("Exception occured While PUT operation is performing for PDP Group" + " - RequestId - " + requestId + e); } if (group == null) { String message = "Unknown groupId '" + groupId + "'."; LOGGER.error(MessageCodes.ERROR_DATA_ISSUE + " - RequestId - " + requestId + " - " + message); loggingContext.transactionEnded(); PolicyLogger.audit("Transaction Failed - See Error.log"); response.addHeader(ERROR, "UnknownGroup"); response.addHeader(MESSAGE, message); response.setStatus(HttpServletResponse.SC_NOT_FOUND); return; } loggingContext.setServiceName("API:PAP.deletPolicyFromPDPGroup"); RemoveGroupPolicy removePolicy = new RemoveGroupPolicy((StdPDPGroup) group); removePolicy.prepareToRemove(); List<String> policyIdList = Arrays.asList(polName.split(",")); for (String policyName : policyIdList) { if (!policyName.endsWith("xml")) { String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Invalid policyName... " + "policyName must be the full name of the file to be deleted including version and extension"; LOGGER.error(message + " - RequestId - " + requestId); response.addHeader(ERROR, message); response.setStatus(HttpServletResponse.SC_BAD_REQUEST); return; } PDPPolicy policy = group.getPolicy(policyName); if (policy == null) { String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Policy does not exist on the PDP - " + policyName; LOGGER.error(message + " - RequestId - " + requestId); response.addHeader(ERROR, message); response.setStatus(HttpServletResponse.SC_BAD_REQUEST); return; } if (StringUtils.indexOfAny(policy.getId(), "Config_MS_", "BRMS_Param") >= 0) { preSafetyCheck(policy); } LOGGER.info("Preparing to remove policy from group: " + group.getId() + "- RequestId - " + requestId); removePolicy.removePolicy(policy); } OnapPDPGroup updatedGroup = removePolicy.getUpdatedObject(); String userId = request.getParameter("userId"); String responseString = deletePolicyFromPdpGroup(updatedGroup, loggingContext, userId); switch (responseString) { case SUCCESS: loggingContext.transactionEnded(); LOGGER.info("Policy successfully removed from PDP - " + polName + " - RequestId - " + requestId); PolicyLogger.audit("Policy successfully deleted!"); response.setStatus(HttpServletResponse.SC_OK); response.addHeader("successMapKey", SUCCESS); response.addHeader(OPERATION, DELETE); break; case "No Group": String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Group update had bad input."; LOGGER.error(message + " - RequestId - " + requestId); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); response.addHeader(ERROR, "groupUpdate"); response.addHeader(MESSAGE, message); break; case "DB Error": LOGGER.error(MessageCodes.ERROR_PROCESS_FLOW + " Error while updating group in the database" + " - RequestId - " + requestId); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); response.addHeader(ERROR, "deleteDB"); break; default: LOGGER.error(MessageCodes.ERROR_UNKNOWN + " Failed to delete the policy for an unknown reason. Check the file system and other logs " + "for " + "further information."); response.addHeader(ERROR, UNKNOWN); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); break; } } }
DeleteHandler { public void doApiDeleteFromPdp(HttpServletRequest request, HttpServletResponse response, OnapLoggingContext loggingContext) throws IOException { String groupId = request.getParameter("groupId"); if (groupId != null && !groupId.matches(REGEX)) { response.setStatus(HttpServletResponse.SC_BAD_REQUEST); response.addHeader(ERROR, ERROR); response.addHeader(MESSAGE, "Group Id is not valid"); return; } String requestId = request.getHeader("X-ECOMP-RequestID"); String polName = request.getParameter("policyName"); LOGGER.info("JSON request from API to Delete Policy from the PDP - " + polName + " -RequestId - " + requestId); OnapPDPGroup group = null; try { group = XACMLPapServlet.getPAPEngine().getGroup(groupId); } catch (PAPException e) { LOGGER.error("Exception occured While PUT operation is performing for PDP Group" + " - RequestId - " + requestId + e); } if (group == null) { String message = "Unknown groupId '" + groupId + "'."; LOGGER.error(MessageCodes.ERROR_DATA_ISSUE + " - RequestId - " + requestId + " - " + message); loggingContext.transactionEnded(); PolicyLogger.audit("Transaction Failed - See Error.log"); response.addHeader(ERROR, "UnknownGroup"); response.addHeader(MESSAGE, message); response.setStatus(HttpServletResponse.SC_NOT_FOUND); return; } loggingContext.setServiceName("API:PAP.deletPolicyFromPDPGroup"); RemoveGroupPolicy removePolicy = new RemoveGroupPolicy((StdPDPGroup) group); removePolicy.prepareToRemove(); List<String> policyIdList = Arrays.asList(polName.split(",")); for (String policyName : policyIdList) { if (!policyName.endsWith("xml")) { String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Invalid policyName... " + "policyName must be the full name of the file to be deleted including version and extension"; LOGGER.error(message + " - RequestId - " + requestId); response.addHeader(ERROR, message); response.setStatus(HttpServletResponse.SC_BAD_REQUEST); return; } PDPPolicy policy = group.getPolicy(policyName); if (policy == null) { String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Policy does not exist on the PDP - " + policyName; LOGGER.error(message + " - RequestId - " + requestId); response.addHeader(ERROR, message); response.setStatus(HttpServletResponse.SC_BAD_REQUEST); return; } if (StringUtils.indexOfAny(policy.getId(), "Config_MS_", "BRMS_Param") >= 0) { preSafetyCheck(policy); } LOGGER.info("Preparing to remove policy from group: " + group.getId() + "- RequestId - " + requestId); removePolicy.removePolicy(policy); } OnapPDPGroup updatedGroup = removePolicy.getUpdatedObject(); String userId = request.getParameter("userId"); String responseString = deletePolicyFromPdpGroup(updatedGroup, loggingContext, userId); switch (responseString) { case SUCCESS: loggingContext.transactionEnded(); LOGGER.info("Policy successfully removed from PDP - " + polName + " - RequestId - " + requestId); PolicyLogger.audit("Policy successfully deleted!"); response.setStatus(HttpServletResponse.SC_OK); response.addHeader("successMapKey", SUCCESS); response.addHeader(OPERATION, DELETE); break; case "No Group": String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Group update had bad input."; LOGGER.error(message + " - RequestId - " + requestId); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); response.addHeader(ERROR, "groupUpdate"); response.addHeader(MESSAGE, message); break; case "DB Error": LOGGER.error(MessageCodes.ERROR_PROCESS_FLOW + " Error while updating group in the database" + " - RequestId - " + requestId); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); response.addHeader(ERROR, "deleteDB"); break; default: LOGGER.error(MessageCodes.ERROR_UNKNOWN + " Failed to delete the policy for an unknown reason. Check the file system and other logs " + "for " + "further information."); response.addHeader(ERROR, UNKNOWN); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); break; } } @Autowired  DeleteHandler(CommonClassDao commonClassDao);  DeleteHandler(); }
DeleteHandler { public void doApiDeleteFromPdp(HttpServletRequest request, HttpServletResponse response, OnapLoggingContext loggingContext) throws IOException { String groupId = request.getParameter("groupId"); if (groupId != null && !groupId.matches(REGEX)) { response.setStatus(HttpServletResponse.SC_BAD_REQUEST); response.addHeader(ERROR, ERROR); response.addHeader(MESSAGE, "Group Id is not valid"); return; } String requestId = request.getHeader("X-ECOMP-RequestID"); String polName = request.getParameter("policyName"); LOGGER.info("JSON request from API to Delete Policy from the PDP - " + polName + " -RequestId - " + requestId); OnapPDPGroup group = null; try { group = XACMLPapServlet.getPAPEngine().getGroup(groupId); } catch (PAPException e) { LOGGER.error("Exception occured While PUT operation is performing for PDP Group" + " - RequestId - " + requestId + e); } if (group == null) { String message = "Unknown groupId '" + groupId + "'."; LOGGER.error(MessageCodes.ERROR_DATA_ISSUE + " - RequestId - " + requestId + " - " + message); loggingContext.transactionEnded(); PolicyLogger.audit("Transaction Failed - See Error.log"); response.addHeader(ERROR, "UnknownGroup"); response.addHeader(MESSAGE, message); response.setStatus(HttpServletResponse.SC_NOT_FOUND); return; } loggingContext.setServiceName("API:PAP.deletPolicyFromPDPGroup"); RemoveGroupPolicy removePolicy = new RemoveGroupPolicy((StdPDPGroup) group); removePolicy.prepareToRemove(); List<String> policyIdList = Arrays.asList(polName.split(",")); for (String policyName : policyIdList) { if (!policyName.endsWith("xml")) { String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Invalid policyName... " + "policyName must be the full name of the file to be deleted including version and extension"; LOGGER.error(message + " - RequestId - " + requestId); response.addHeader(ERROR, message); response.setStatus(HttpServletResponse.SC_BAD_REQUEST); return; } PDPPolicy policy = group.getPolicy(policyName); if (policy == null) { String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Policy does not exist on the PDP - " + policyName; LOGGER.error(message + " - RequestId - " + requestId); response.addHeader(ERROR, message); response.setStatus(HttpServletResponse.SC_BAD_REQUEST); return; } if (StringUtils.indexOfAny(policy.getId(), "Config_MS_", "BRMS_Param") >= 0) { preSafetyCheck(policy); } LOGGER.info("Preparing to remove policy from group: " + group.getId() + "- RequestId - " + requestId); removePolicy.removePolicy(policy); } OnapPDPGroup updatedGroup = removePolicy.getUpdatedObject(); String userId = request.getParameter("userId"); String responseString = deletePolicyFromPdpGroup(updatedGroup, loggingContext, userId); switch (responseString) { case SUCCESS: loggingContext.transactionEnded(); LOGGER.info("Policy successfully removed from PDP - " + polName + " - RequestId - " + requestId); PolicyLogger.audit("Policy successfully deleted!"); response.setStatus(HttpServletResponse.SC_OK); response.addHeader("successMapKey", SUCCESS); response.addHeader(OPERATION, DELETE); break; case "No Group": String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Group update had bad input."; LOGGER.error(message + " - RequestId - " + requestId); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); response.addHeader(ERROR, "groupUpdate"); response.addHeader(MESSAGE, message); break; case "DB Error": LOGGER.error(MessageCodes.ERROR_PROCESS_FLOW + " Error while updating group in the database" + " - RequestId - " + requestId); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); response.addHeader(ERROR, "deleteDB"); break; default: LOGGER.error(MessageCodes.ERROR_UNKNOWN + " Failed to delete the policy for an unknown reason. Check the file system and other logs " + "for " + "further information."); response.addHeader(ERROR, UNKNOWN); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); break; } } @Autowired  DeleteHandler(CommonClassDao commonClassDao);  DeleteHandler(); void doApiDeleteFromPap(HttpServletRequest request, HttpServletResponse response); static String deletePolicyEntityData(PolicyEntity policyEntity); static boolean checkPolicyGroupEntity(List<?> peResult); void doApiDeleteFromPdp(HttpServletRequest request, HttpServletResponse response,
            OnapLoggingContext loggingContext); OnapPDPGroup getDeletedGroup(); boolean preSafetyCheck(PDPPolicy policy); static DeleteHandler getInstance(); }
DeleteHandler { public void doApiDeleteFromPdp(HttpServletRequest request, HttpServletResponse response, OnapLoggingContext loggingContext) throws IOException { String groupId = request.getParameter("groupId"); if (groupId != null && !groupId.matches(REGEX)) { response.setStatus(HttpServletResponse.SC_BAD_REQUEST); response.addHeader(ERROR, ERROR); response.addHeader(MESSAGE, "Group Id is not valid"); return; } String requestId = request.getHeader("X-ECOMP-RequestID"); String polName = request.getParameter("policyName"); LOGGER.info("JSON request from API to Delete Policy from the PDP - " + polName + " -RequestId - " + requestId); OnapPDPGroup group = null; try { group = XACMLPapServlet.getPAPEngine().getGroup(groupId); } catch (PAPException e) { LOGGER.error("Exception occured While PUT operation is performing for PDP Group" + " - RequestId - " + requestId + e); } if (group == null) { String message = "Unknown groupId '" + groupId + "'."; LOGGER.error(MessageCodes.ERROR_DATA_ISSUE + " - RequestId - " + requestId + " - " + message); loggingContext.transactionEnded(); PolicyLogger.audit("Transaction Failed - See Error.log"); response.addHeader(ERROR, "UnknownGroup"); response.addHeader(MESSAGE, message); response.setStatus(HttpServletResponse.SC_NOT_FOUND); return; } loggingContext.setServiceName("API:PAP.deletPolicyFromPDPGroup"); RemoveGroupPolicy removePolicy = new RemoveGroupPolicy((StdPDPGroup) group); removePolicy.prepareToRemove(); List<String> policyIdList = Arrays.asList(polName.split(",")); for (String policyName : policyIdList) { if (!policyName.endsWith("xml")) { String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Invalid policyName... " + "policyName must be the full name of the file to be deleted including version and extension"; LOGGER.error(message + " - RequestId - " + requestId); response.addHeader(ERROR, message); response.setStatus(HttpServletResponse.SC_BAD_REQUEST); return; } PDPPolicy policy = group.getPolicy(policyName); if (policy == null) { String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Policy does not exist on the PDP - " + policyName; LOGGER.error(message + " - RequestId - " + requestId); response.addHeader(ERROR, message); response.setStatus(HttpServletResponse.SC_BAD_REQUEST); return; } if (StringUtils.indexOfAny(policy.getId(), "Config_MS_", "BRMS_Param") >= 0) { preSafetyCheck(policy); } LOGGER.info("Preparing to remove policy from group: " + group.getId() + "- RequestId - " + requestId); removePolicy.removePolicy(policy); } OnapPDPGroup updatedGroup = removePolicy.getUpdatedObject(); String userId = request.getParameter("userId"); String responseString = deletePolicyFromPdpGroup(updatedGroup, loggingContext, userId); switch (responseString) { case SUCCESS: loggingContext.transactionEnded(); LOGGER.info("Policy successfully removed from PDP - " + polName + " - RequestId - " + requestId); PolicyLogger.audit("Policy successfully deleted!"); response.setStatus(HttpServletResponse.SC_OK); response.addHeader("successMapKey", SUCCESS); response.addHeader(OPERATION, DELETE); break; case "No Group": String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Group update had bad input."; LOGGER.error(message + " - RequestId - " + requestId); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); response.addHeader(ERROR, "groupUpdate"); response.addHeader(MESSAGE, message); break; case "DB Error": LOGGER.error(MessageCodes.ERROR_PROCESS_FLOW + " Error while updating group in the database" + " - RequestId - " + requestId); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); response.addHeader(ERROR, "deleteDB"); break; default: LOGGER.error(MessageCodes.ERROR_UNKNOWN + " Failed to delete the policy for an unknown reason. Check the file system and other logs " + "for " + "further information."); response.addHeader(ERROR, UNKNOWN); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); break; } } @Autowired  DeleteHandler(CommonClassDao commonClassDao);  DeleteHandler(); void doApiDeleteFromPap(HttpServletRequest request, HttpServletResponse response); static String deletePolicyEntityData(PolicyEntity policyEntity); static boolean checkPolicyGroupEntity(List<?> peResult); void doApiDeleteFromPdp(HttpServletRequest request, HttpServletResponse response,
            OnapLoggingContext loggingContext); OnapPDPGroup getDeletedGroup(); boolean preSafetyCheck(PDPPolicy policy); static DeleteHandler getInstance();  }