X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=ONAP-PAP-REST%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fpolicy%2Fpap%2Fxacml%2Frest%2Fhandler%2FDeleteHandler.java;h=85b6e24a194ff24afa3ad654f2e1cb400f889c3c;hb=c5d97e8a9a6bea71f3be329a2e44bdbe5fe50882;hp=a185054781d77ed1d2f9bf1b3729b916c2b2a63e;hpb=cbc5914ca2c1770797f6ad2a05ea9d7c30a55b63;p=policy%2Fengine.git diff --git a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/handler/DeleteHandler.java b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/handler/DeleteHandler.java index a18505478..85b6e24a1 100644 --- a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/handler/DeleteHandler.java +++ b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/handler/DeleteHandler.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP-PAP-REST * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -37,6 +37,7 @@ import org.onap.policy.common.logging.ONAPLoggingContext; import org.onap.policy.common.logging.eelf.MessageCodes; import org.onap.policy.common.logging.eelf.PolicyLogger; import org.onap.policy.common.logging.flexlogger.FlexLogger; +import org.onap.policy.common.logging.flexlogger.Logger; import org.onap.policy.pap.xacml.rest.XACMLPapServlet; import org.onap.policy.pap.xacml.rest.components.PolicyDBDaoTransaction; import org.onap.policy.pap.xacml.rest.elk.client.PolicyElasticSearchController; @@ -51,8 +52,7 @@ import org.onap.policy.xacml.api.XACMLErrorConstants; import org.onap.policy.xacml.api.pap.OnapPDPGroup; import org.onap.policy.xacml.std.pap.StdPAPPolicy; import org.onap.policy.xacml.std.pap.StdPDPGroup; -import org.onap.policy.common.logging.flexlogger.FlexLogger; -import org.onap.policy.common.logging.flexlogger.Logger; + import com.att.research.xacml.api.pap.PAPException; import com.att.research.xacml.api.pap.PDPPolicy; import com.att.research.xacml.util.XACMLProperties; @@ -61,20 +61,19 @@ public class DeleteHandler { private OnapPDPGroup newgroup; private static Logger logger = FlexLogger.getLogger(DeleteHandler.class); + public static final String POLICY_IN_PDP = "PolicyInPDP"; + public static final String ERROR = "error"; + public static final String UNKNOWN = "unknown"; - private static String papDbDriver = null; - private static String papDbUrl = null; - private static String papDbUser = null; - private static String papDbPassword = null; - public static void doAPIDeleteFromPAP(HttpServletRequest request, HttpServletResponse response, ONAPLoggingContext loggingContext) throws IOException, SQLException { + public void doAPIDeleteFromPAP(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException { // get the request content into a String String json = null; java.util.Scanner scanner = new java.util.Scanner(request.getInputStream()); scanner.useDelimiter("\\A"); json = scanner.hasNext() ? scanner.next() : ""; scanner.close(); - PolicyLogger.info("JSON request from API: " + json); + PolicyLogger.info("JSON request from API to Delete Policy from the PAP: " + json); // convert Object sent as JSON into local object StdPAPPolicy policy = PolicyUtils.jsonStringToObject(json, StdPAPPolicy.class); String policyName = policy.getPolicyName(); @@ -84,21 +83,21 @@ public class DeleteHandler { String removeVersionExtension; String splitPolicyName = null; String[] split = null; - String status = "error"; + String status = ERROR; PolicyEntity policyEntity = null; JPAUtils jpaUtils = null; - papDbDriver = XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_DB_DRIVER); - papDbUrl = XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_DB_URL); - papDbUser = XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_DB_USER); - papDbPassword = XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_DB_PASSWORD); + String papDbDriver = XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_DB_DRIVER); + String papDbUrl = XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_DB_URL); + String papDbUser = XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_DB_USER); + String papDbPassword = XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_DB_PASSWORD); Connection con = null; try { jpaUtils = JPAUtils.getJPAUtilsInstance(XACMLPapServlet.getEmf()); } catch (Exception e) { PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "XACMLPapServlet", " Could not create JPAUtils instance on the PAP"); - response.addHeader("error", "jpautils"); + response.addHeader(ERROR, "jpautils"); response.addHeader("operation", "delete"); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); return; @@ -110,13 +109,13 @@ public class DeleteHandler { response.setStatus(HttpServletResponse.SC_ACCEPTED); return; } - EntityManager em = (EntityManager) XACMLPapServlet.getEmf().createEntityManager(); + EntityManager em = XACMLPapServlet.getEmf().createEntityManager(); Query policyEntityQuery = null; try{ if(policyName.endsWith(".xml")){ removeXMLExtension = policyName.replace(".xml", ""); - currentVersion = Integer.parseInt(removeXMLExtension.substring(removeXMLExtension.lastIndexOf(".")+1)); - removeVersionExtension = removeXMLExtension.substring(0, removeXMLExtension.lastIndexOf(".")); + currentVersion = Integer.parseInt(removeXMLExtension.substring(removeXMLExtension.lastIndexOf('.')+1)); + removeVersionExtension = removeXMLExtension.substring(0, removeXMLExtension.lastIndexOf('.')); boolean queryCheck = true; if(policy.getDeleteCondition().equalsIgnoreCase("All Versions")){ if(policyName.contains("Config_")){ @@ -130,7 +129,7 @@ public class DeleteHandler { split = splitPolicyName.split(":"); }else{ PolicyLogger.error(MessageCodes.ERROR_UNKNOWN + "Failed to delete the policy. Please, provide the valid policyname."); - response.addHeader("error", "unknown"); + response.addHeader(ERROR, UNKNOWN); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR ); return; } @@ -167,23 +166,23 @@ public class DeleteHandler { con = DriverManager.getConnection(papDbUrl,papDbUser,papDbPassword); if(policy.getDeleteCondition().equalsIgnoreCase("All Versions")){ - boolean groupCheck = checkPolicyGroupEntity(em, con, peResult); + boolean groupCheck = checkPolicyGroupEntity(con, peResult); if(!groupCheck){ for(Object peData : peResult){ policyEntity = (PolicyEntity) peData; status = deletePolicyEntityData(em, policyEntity); } }else{ - status = "PolicyInPDP"; + status = POLICY_IN_PDP; } - if(status.equals("error")){ + if(status.equals(ERROR)){ PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE + "Exception Occured while deleting the Entity from Database."); - response.addHeader("error", "unknown"); + response.addHeader(ERROR, UNKNOWN); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR ); return; - }else if(status.equals("PolicyInPDP")){ + }else if(status.equals(POLICY_IN_PDP)){ PolicyLogger.error(MessageCodes.GENERAL_WARNING + "Policy can't be deleted, it is active in PDP Groups."); - response.addHeader("error", "PolicyInPDP"); + response.addHeader(ERROR, POLICY_IN_PDP); response.setStatus(HttpServletResponse.SC_CONFLICT); return; }else{ @@ -196,22 +195,22 @@ public class DeleteHandler { } } }else if(policy.getDeleteCondition().equalsIgnoreCase("Current Version")){ - boolean groupCheck = checkPolicyGroupEntity(em, con, peResult); + boolean groupCheck = checkPolicyGroupEntity(con, peResult); if(!groupCheck){ policyEntity = (PolicyEntity) peResult.get(0); status = deletePolicyEntityData(em, policyEntity); }else{ - status = "PolicyInPDP"; + status = POLICY_IN_PDP; } - if(status.equals("error")){ + if(ERROR.equals(status)){ PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE + "Exception Occured while deleting the Entity from Database."); - response.addHeader("error", "unknown"); + response.addHeader(ERROR, UNKNOWN); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR ); return; - }else if(status.equals("PolicyInPDP")){ + }else if(POLICY_IN_PDP.equals(status)){ PolicyLogger.error(MessageCodes.GENERAL_WARNING + "Policy can't be deleted, it is active in PDP Groups."); - response.addHeader("error", "unknown"); + response.addHeader(ERROR, POLICY_IN_PDP); response.setStatus(HttpServletResponse.SC_CONFLICT); return; }else{ @@ -220,7 +219,7 @@ public class DeleteHandler { for(Object object : peResult){ policyEntity = (PolicyEntity) object; String policyEntityName = policyEntity.getPolicyName().replace(".xml", ""); - int policyEntityVersion = Integer.parseInt(policyEntityName.substring(policyEntityName.lastIndexOf(".")+1)); + int policyEntityVersion = Integer.parseInt(policyEntityName.substring(policyEntityName.lastIndexOf('.')+1)); if(policyEntityVersion > newVersion){ newVersion = policyEntityVersion-1; } @@ -248,7 +247,7 @@ public class DeleteHandler { } }else{ PolicyLogger.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.addHeader(ERROR, UNKNOWN); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR ); return; } @@ -257,7 +256,7 @@ public class DeleteHandler { }catch(Exception e){ em.getTransaction().rollback(); PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "XACMLPapServlet", " ERROR"); - response.addHeader("error", "deleteDB"); + response.addHeader(ERROR, "deleteDB"); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); return; } finally { @@ -274,13 +273,13 @@ public class DeleteHandler { return; } else { PolicyLogger.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.addHeader(ERROR, UNKNOWN); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR ); return; } } - public static String deletePolicyEntityData(EntityManager em, PolicyEntity policyEntity) throws SQLException{ + public static String deletePolicyEntityData(EntityManager em, PolicyEntity policyEntity){ PolicyElasticSearchController controller = new PolicyElasticSearchController(); PolicyRestAdapter policyData = new PolicyRestAdapter(); String policyName = policyEntity.getPolicyName(); @@ -296,37 +295,33 @@ public class DeleteHandler { em.remove(policyEntity); }catch(Exception e){ logger.error(e.getMessage(),e); - return "error"; + return ERROR; } return "success"; } - public static boolean checkPolicyGroupEntity(EntityManager em, Connection con, List peResult) throws SQLException{ + public static boolean checkPolicyGroupEntity(Connection con, List peResult) throws SQLException{ for(Object peData : peResult){ PolicyEntity policyEntity = (PolicyEntity) peData; - Statement st = null; - ResultSet rs = null; - try{ - st = con.createStatement(); - rs = st.executeQuery("Select * from PolicyGroupEntity where policyid = '"+policyEntity.getPolicyId()+"'"); + try(Statement st = con.createStatement(); + ResultSet rs = st.executeQuery("Select * from PolicyGroupEntity where policyid = '"+policyEntity.getPolicyId()+"'")){ boolean gEntityList = rs.next(); - rs.close(); if(gEntityList){ return true; } - }finally{ - if(st != null){ - st.close(); - } } } return false; } public void doAPIDeleteFromPDP(HttpServletRequest request, HttpServletResponse response, ONAPLoggingContext loggingContext) throws IOException { + String policyName = request.getParameter("policyName"); String groupId = request.getParameter("groupId"); String responseString = null; + + PolicyLogger.info("JSON request from API to Delete Policy from the PDP: " + policyName); + // for PUT operations the group may or may not need to exist before the operation can be done OnapPDPGroup group = null; try { @@ -335,12 +330,13 @@ public class DeleteHandler { PolicyLogger.error("Exception occured While PUT operation is performing for PDP Group"+e); } if (group == null) { - String message = "Unknown groupId '" + groupId + "'"; + String message = "Unknown groupId '" + groupId + "'."; PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE + " " + message); loggingContext.transactionEnded(); PolicyLogger.audit("Transaction Failed - See Error.log"); - response.addHeader("error", "UnknownGroup"); - response.sendError(HttpServletResponse.SC_NOT_FOUND, message); + response.addHeader(ERROR, "UnknownGroup"); + response.addHeader("message", message); + response.setStatus(HttpServletResponse.SC_NOT_FOUND); return; } else { loggingContext.setServiceName("API:PAP.deletPolicyFromPDPGroup"); @@ -351,7 +347,7 @@ public class DeleteHandler { + "policyName must be the full name of the file to be deleted including version and extension"; PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE + " Invalid policyName... " + "policyName must be the full name of the file to be deleted including version and extension"); - response.addHeader("error", message); + response.addHeader(ERROR, message); response.setStatus(HttpServletResponse.SC_BAD_REQUEST); return; } @@ -364,7 +360,7 @@ public class DeleteHandler { PolicyLogger.debug("Precheck Successful."); } } - + PolicyLogger.info("Preparing to remove policy from group: " + group.getId()); removePolicy.prepareToRemove(policy); OnapPDPGroup updatedGroup = removePolicy.getUpdatedObject(); responseString = deletePolicyFromPDPGroup(updatedGroup, loggingContext); @@ -372,12 +368,13 @@ public class DeleteHandler { String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Policy does not exist on the PDP."; PolicyLogger.error(message); PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE + " Policy does not exist on the PDP."); - response.addHeader("error", message); + response.addHeader(ERROR, message); response.setStatus(HttpServletResponse.SC_BAD_REQUEST); return; } } if (responseString.equals("success")) { + loggingContext.transactionEnded(); PolicyLogger.info("Policy successfully deleted!"); PolicyLogger.audit("Policy successfully deleted!"); response.setStatus(HttpServletResponse.SC_OK); @@ -388,17 +385,17 @@ public class DeleteHandler { String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Group update had bad input."; PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE + " Group update had bad input."); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); - response.addHeader("error", "groupUpdate"); + response.addHeader(ERROR, "groupUpdate"); response.addHeader("message", message); return; } else if (responseString.equals("DB Error")) { PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW + " Error while updating group in the database"); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); - response.addHeader("error", "deleteDB"); + response.addHeader(ERROR, "deleteDB"); return; } else { PolicyLogger.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.addHeader(ERROR, UNKNOWN); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR ); return; } @@ -414,7 +411,7 @@ public class DeleteHandler { } catch (PAPException e1) { PolicyLogger.error("Exception occured While Deleting Policy From PDP Group"+e1); } - if (group == null || ! (group instanceof StdPDPGroup) || existingGroup == null || ! (group.getId().equals(existingGroup.getId()))) { + if (!(group instanceof StdPDPGroup) || existingGroup == null || !(group.getId().equals(existingGroup.getId()))) { String existingID = null; if(existingGroup != null){ existingID = existingGroup.getId(); @@ -430,7 +427,7 @@ public class DeleteHandler { // so we need to fill that in before submitting the group for update ((StdPDPGroup)group).setDirectory(((StdPDPGroup)existingGroup).getDirectory()); try{ - acPutTransaction.updateGroup(group, "XACMLPapServlet.doAPIDelete"); + acPutTransaction.updateGroup(group, "XACMLPapServlet.doDelete"); } catch(Exception e){ PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, "XACMLPapServlet", " Error while updating group in the database: " +"group="+existingGroup.getId()); @@ -454,8 +451,8 @@ public class DeleteHandler { } if (response==null){ response = "success"; + loggingContext.transactionEnded(); PolicyLogger.audit("Policy successfully deleted!"); - PolicyLogger.audit("Transaction Ended Successfully"); } loggingContext.transactionEnded(); PolicyLogger.audit("Transaction Ended"); @@ -473,8 +470,7 @@ public class DeleteHandler { public static DeleteHandler getInstance() { try { Class deleteHandler = Class.forName(XACMLProperties.getProperty("deletePolicy.impl.className", DeleteHandler.class.getName())); - DeleteHandler instance = (DeleteHandler) deleteHandler.newInstance(); - return instance; + return (DeleteHandler) deleteHandler.newInstance(); } catch (Exception e) { logger.error(e.getMessage(),e); }