X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=ECOMP-PAP-REST%2Fsrc%2Fmain%2Fjava%2Forg%2Fopenecomp%2Fpolicy%2Fpap%2Fxacml%2Frest%2Fhandler%2FDeleteHandler.java;h=69319651776548358949589722590e7aa5b343c4;hb=eb654437ee2815028602daa943d0a939a5bee3cd;hp=15e1faa0414b0c903475d805c0f66fe126288503;hpb=a330af579866dacbe595e2e4ad1dd29cd3c96945;p=policy%2Fengine.git diff --git a/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/handler/DeleteHandler.java b/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/handler/DeleteHandler.java index 15e1faa04..693196517 100644 --- a/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/handler/DeleteHandler.java +++ b/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/handler/DeleteHandler.java @@ -123,7 +123,14 @@ public class DeleteHandler { }else if(policyName.contains("Decision_")){ splitPolicyName = removeVersionExtension.replace(".Decision_", ":Decision_"); } - split = splitPolicyName.split(":"); + if(splitPolicyName != null){ + split = splitPolicyName.split(":"); + }else{ + PolicyLogger.error(MessageCodes.ERROR_UNKNOWN + "Failed to delete the policy. Please, provide the valid policyname."); + response.addHeader("error", "unknown"); + response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR ); + return; + } policyEntityQuery = em.createQuery("SELECT p FROM PolicyEntity p WHERE p.policyName LIKE :pName and p.scope=:pScope"); }else if(policy.getDeleteCondition().equalsIgnoreCase("Current Version")) { if(policyName.contains("Config_")){ @@ -151,7 +158,7 @@ public class DeleteHandler { getPolicyVersion.setParameter("pname", removeVersionExtension.replace(".", File.separator)); List pvResult = getPolicyVersion.getResultList(); PolicyVersion pVersion = (PolicyVersion) pvResult.get(0); - int highestVersion = 0; + int newVersion = 0; em.getTransaction().begin(); Class.forName(papDbDriver); con = DriverManager.getConnection(papDbUrl,papDbUser,papDbPassword); @@ -173,7 +180,7 @@ public class DeleteHandler { return; }else if(status.equals("PolicyInPDP")){ PolicyLogger.error(MessageCodes.GENERAL_WARNING + "Policy can't be deleted, it is active in PDP Groups."); - response.addHeader("error", "unknown"); + response.addHeader("error", "PolicyInPDP"); response.setStatus(HttpServletResponse.SC_CONFLICT); return; }else{ @@ -210,13 +217,13 @@ public class DeleteHandler { policyEntity = (PolicyEntity) object; String policyEntityName = policyEntity.getPolicyName().replace(".xml", ""); int policyEntityVersion = Integer.parseInt(policyEntityName.substring(policyEntityName.lastIndexOf(".")+1)); - if(policyEntityVersion > highestVersion){ - highestVersion = policyEntityVersion; + if(policyEntityVersion > newVersion){ + newVersion = policyEntityVersion-1; } } } - pVersion.setActiveVersion(highestVersion); - pVersion.setHigherVersion(highestVersion); + pVersion.setActiveVersion(newVersion); + pVersion.setHigherVersion(newVersion); try{ policyVersionDeleted = true; em.persist(pVersion); @@ -249,7 +256,9 @@ public class DeleteHandler { return; } finally { em.close(); - con.close(); + if(con != null){ + con.close(); + } } if (policyVersionDeleted) { @@ -290,12 +299,18 @@ public class DeleteHandler { PolicyEntity policyEntity = (PolicyEntity) peData; Statement st = null; ResultSet rs = null; - st = con.createStatement(); - rs = st.executeQuery("Select * from PolicyGroupEntity where policyid = '"+policyEntity.getPolicyId()+"'"); - boolean gEntityList = rs.next(); - rs.close(); - if(gEntityList){ - return true; + try{ + st = con.createStatement(); + 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; @@ -392,8 +407,12 @@ public class DeleteHandler { } catch (PAPException e1) { PolicyLogger.error("Exception occured While Deleting Policy From PDP Group"+e1); } - if (group == null || ! (group instanceof StdPDPGroup) || ! (group.getId().equals(existingGroup.getId()))) { - PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE + " Group update had bad input. id=" + existingGroup.getId() + " objectFromJSON="+group); + if (group == null || ! (group instanceof StdPDPGroup) || existingGroup == null || ! (group.getId().equals(existingGroup.getId()))) { + String existingID = null; + if(existingGroup != null){ + existingID = existingGroup.getId(); + } + PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE + " Group update had bad input. id=" + existingID + " objectFromJSON="+group); loggingContext.transactionEnded(); PolicyLogger.audit("Transaction Failed - See Error.log"); response = "No Group";