X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=POLICY-SDK-APP%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fpolicy%2Fadmin%2FPolicyManagerServlet.java;h=4d549fea2fc192310aafe2c6aa9f96b01e3df4b5;hb=51eb4945ce94e88c5f315ffe9469a9daa3512cd9;hp=54a14cb8dbb5456213ae0abe756b46b80cf320b3;hpb=9301fed7c290f52208922f780fdfe4b5a880b5aa;p=policy%2Fengine.git diff --git a/POLICY-SDK-APP/src/main/java/org/onap/policy/admin/PolicyManagerServlet.java b/POLICY-SDK-APP/src/main/java/org/onap/policy/admin/PolicyManagerServlet.java index 54a14cb8d..4d549fea2 100644 --- a/POLICY-SDK-APP/src/main/java/org/onap/policy/admin/PolicyManagerServlet.java +++ b/POLICY-SDK-APP/src/main/java/org/onap/policy/admin/PolicyManagerServlet.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP Policy Engine * ================================================================================ - * 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. @@ -430,60 +430,58 @@ public class PolicyManagerServlet extends HttpServlet { String activePolicy; PolicyController controller = getPolicyControllerInstance(); - if(params.toString().contains("activeVersion")){ - String activeVersion = params.getString("activeVersion"); - String highestVersion = params.get("highestVersion").toString(); - if(Integer.parseInt(activeVersion) > Integer.parseInt(highestVersion)){ - return error("The Version shouldn't be greater than Highest Value"); - }else{ - activePolicy = policyName + "." + activeVersion + ".xml"; - String dbCheckName = activePolicy.replace("/", "."); - if(dbCheckName.contains("Config_")){ - dbCheckName = dbCheckName.replace(".Config_", ":Config_"); - }else if(dbCheckName.contains("Action_")){ - dbCheckName = dbCheckName.replace(".Action_", ":Action_"); - }else if(dbCheckName.contains("Decision_")){ - dbCheckName = dbCheckName.replace(".Decision_", ":Decision_"); - } - String[] splitDBCheckName = dbCheckName.split(":"); - String peQuery = "FROM PolicyEntity where policyName = :splitDBCheckName_1 and scope = :splitDBCheckName_0"; - SimpleBindings policyParams = new SimpleBindings(); - policyParams.put("splitDBCheckName_1", splitDBCheckName[1]); - policyParams.put("splitDBCheckName_0", splitDBCheckName[0]); - List policyEntity = controller.getDataByQuery(peQuery, policyParams); - PolicyEntity pentity = (PolicyEntity) policyEntity.get(0); - if(pentity.isDeleted()){ - return error("The Policy is Not Existing in Workspace"); - }else{ - if(policyName.contains("/")){ - policyName = policyName.replace("/", File.separator); - } - policyName = policyName.substring(policyName.indexOf(File.separator)+1); - if(policyName.contains("\\")){ - policyName = policyName.replace(File.separator, "\\"); - } - policyName = splitDBCheckName[0].replace(".", File.separator)+File.separator+policyName; - String watchPolicyName = policyName; - if(policyName.contains("/")){ - policyName = policyName.replace("/", File.separator); - } - if(policyName.contains("\\")){ - policyName = policyName.replace("\\", "\\\\"); - } - String query = "update PolicyVersion set active_version='"+activeVersion+"' where policy_name ='"+policyName+"' and id >0"; - //query the database - controller.executeQuery(query); - //Policy Notification - PolicyVersion entity = new PolicyVersion(); - entity.setPolicyName(watchPolicyName); - entity.setActiveVersion(Integer.parseInt(activeVersion)); - entity.setModifiedBy(userId); - controller.watchPolicyFunction(entity, activePolicy, "SwitchVersion"); - return success(); - } - } + if(! params.toString().contains("activeVersion")){ + return controller.switchVersionPolicyContent(policyName); + } + String activeVersion = params.getString("activeVersion"); + String highestVersion = params.get("highestVersion").toString(); + if(Integer.parseInt(activeVersion) > Integer.parseInt(highestVersion)){ + return error("The Version shouldn't be greater than Highest Value"); + } + activePolicy = policyName + "." + activeVersion + ".xml"; + String dbCheckName = activePolicy.replace("/", "."); + if(dbCheckName.contains("Config_")){ + dbCheckName = dbCheckName.replace(".Config_", ":Config_"); + }else if(dbCheckName.contains("Action_")){ + dbCheckName = dbCheckName.replace(".Action_", ":Action_"); + }else if(dbCheckName.contains("Decision_")){ + dbCheckName = dbCheckName.replace(".Decision_", ":Decision_"); + } + String[] splitDBCheckName = dbCheckName.split(":"); + String peQuery = "FROM PolicyEntity where policyName = :splitDBCheckName_1 and scope = :splitDBCheckName_0"; + SimpleBindings policyParams = new SimpleBindings(); + policyParams.put("splitDBCheckName_1", splitDBCheckName[1]); + policyParams.put("splitDBCheckName_0", splitDBCheckName[0]); + List policyEntity = controller.getDataByQuery(peQuery, policyParams); + PolicyEntity pentity = (PolicyEntity) policyEntity.get(0); + if(pentity.isDeleted()){ + return error("The Policy is Not Existing in Workspace"); + } + if(policyName.contains("/")){ + policyName = policyName.replace("/", File.separator); + } + policyName = policyName.substring(policyName.indexOf(File.separator)+1); + if(policyName.contains("\\")){ + policyName = policyName.replace(File.separator, "\\"); } - return controller.switchVersionPolicyContent(policyName); + policyName = splitDBCheckName[0].replace(".", File.separator)+File.separator+policyName; + String watchPolicyName = policyName; + if(policyName.contains("/")){ + policyName = policyName.replace("/", File.separator); + } + if(policyName.contains("\\")){ + policyName = policyName.replace("\\", "\\\\"); + } + String query = "update PolicyVersion set active_version='"+activeVersion+"' where policy_name ='"+policyName+"' and id >0"; + //query the database + controller.executeQuery(query); + //Policy Notification + PolicyVersion entity = new PolicyVersion(); + entity.setPolicyName(watchPolicyName); + entity.setActiveVersion(Integer.parseInt(activeVersion)); + entity.setModifiedBy(userId); + controller.watchPolicyFunction(entity, activePolicy, "SwitchVersion"); + return success(); } //Describe Policy @@ -518,34 +516,32 @@ public class PolicyManagerServlet extends HttpServlet { }else{ queryData = controller.getDataByQuery(query, peParams); } - if(!queryData.isEmpty()){ - PolicyEntity entity = (PolicyEntity) queryData.get(0); - File temp = null; - BufferedWriter bw = null; - try { - temp = File.createTempFile(policyName, ".tmp"); - bw = new BufferedWriter(new FileWriter(temp)); - bw.write(entity.getPolicyData()); - bw.close(); - object = HumanPolicyComponent.DescribePolicy(temp); - } catch (IOException e) { - LOGGER.error("Exception Occured while Describing the Policy"+e); - }finally{ - if(temp != null){ - temp.delete(); - } - if(bw != null){ - try { - bw.close(); - } catch (IOException e) { - LOGGER.error("Exception Occured while Closing the File Writer"+e); - } + if(queryData.isEmpty()){ + return error("Error Occured while Describing the Policy - query is empty"); + } + PolicyEntity entity = (PolicyEntity) queryData.get(0); + File temp = null; + try { + temp = File.createTempFile(policyName, ".tmp"); + } catch (IOException e) { + String message = "Failed to create temp file " + policyName + ".tmp"; + LOGGER.error(message + e); + return error(message); + } + try (BufferedWriter bw = new BufferedWriter(new FileWriter(temp))) { + bw.write(entity.getPolicyData()); + object = HumanPolicyComponent.DescribePolicy(temp); + } catch (IOException e) { + LOGGER.error("Exception Occured while Describing the Policy"+e); + }finally{ + if(temp != null){ + try { + Files.delete(temp.toPath()); + } catch (IOException e) { + LOGGER.warn("Failed to delete " + temp.getName() + e); } } - }else{ - return error("Error Occured while Describing the Policy"); } - return object; } @@ -650,13 +646,14 @@ public class PolicyManagerServlet extends HttpServlet { } //Get Active Policy List based on Scope Selection form Policy Version table - private void activePolicyList(String scopeName, List resultList, List roles, Set scopes, boolean onlyFolders){ + private void activePolicyList(String inScopeName, List resultList, List roles, Set scopes, boolean onlyFolders){ PolicyController controller = getPolicyControllerInstance(); + String scopeName = inScopeName; if(scopeName.contains("/")){ scopeName = scopeName.replace("/", File.separator); } if(scopeName.contains("\\")){ - scopeName = scopeName.replace("\\", "\\\\\\\\"); + scopeName = scopeName.replace("\\", "\\\\"); } String query = "from PolicyVersion where POLICY_NAME like :scopeName"; String scopeNamequery = "from PolicyEditorScopes where SCOPENAME like :scopeName"; @@ -704,7 +701,7 @@ public class PolicyManagerServlet extends HttpServlet { PolicyVersion policy = (PolicyVersion) list; String scopeNameValue = policy.getPolicyName().substring(0, policy.getPolicyName().lastIndexOf(File.separator)); if(roles.contains(SUPERADMIN) || roles.contains(SUPEREDITOR) || roles.contains(SUPERGUEST)){ - if((scopeName.contains("\\\\"))){ + if(scopeName.contains("\\\\")){ scopeNameCheck = scopeName.replace("\\\\", File.separator); }else{ scopeNameCheck = scopeName; @@ -754,7 +751,14 @@ public class PolicyManagerServlet extends HttpServlet { String newPath = params.getString("newPath"); oldPath = oldPath.substring(oldPath.indexOf('/')+1); newPath = newPath.substring(newPath.indexOf('/')+1); + String checkValidation = null; if(oldPath.endsWith(".xml")){ + checkValidation = newPath.replace(".xml", ""); + checkValidation = checkValidation.substring(checkValidation.indexOf('_') + 1, checkValidation.lastIndexOf(".")); + checkValidation = checkValidation.substring(checkValidation.lastIndexOf("/")+1); + if(!PolicyUtils.policySpecialCharValidator(checkValidation).contains("success")){ + return error("Policy Rename Failed. The Name contains special characters."); + } JSONObject result = policyRename(oldPath, newPath, userId); if(!(Boolean)(result.getJSONObject("result").get("success"))){ return result; @@ -766,10 +770,14 @@ public class PolicyManagerServlet extends HttpServlet { scopeName = scopeName.replace("/", File.separator); newScopeName = newScopeName.replace("/", File.separator); } - if(scopeName.contains("\\")){ - scopeName = scopeName.replace("\\", "\\\\\\\\"); - newScopeName = newScopeName.replace("\\", "\\\\\\\\"); - } + checkValidation = newScopeName.substring(newScopeName.lastIndexOf(File.separator)+1); + if(scopeName.contains("\\")){ + scopeName = scopeName.replace("\\", "\\\\\\\\"); + newScopeName = newScopeName.replace("\\", "\\\\\\\\"); + } + if(!PolicyUtils.policySpecialCharValidator(checkValidation).contains("success")){ + return error("Scope Rename Failed. The Name contains special characters."); + } PolicyController controller = getPolicyControllerInstance(); String query = "from PolicyVersion where POLICY_NAME like :scopeName"; String scopeNamequery = "from PolicyEditorScopes where SCOPENAME like :scopeName"; @@ -819,9 +827,10 @@ public class PolicyManagerServlet extends HttpServlet { } } - private void renameScope(List scopesList, String scopeName, String newScopeName, PolicyController controller){ + private void renameScope(List scopesList, String inScopeName, String newScopeName, PolicyController controller){ for(Object object : scopesList){ PolicyEditorScopes editorScopeEntity = (PolicyEditorScopes) object; + String scopeName = inScopeName; if(scopeName.contains("\\\\\\\\")){ scopeName = scopeName.replace("\\\\\\\\", File.separator); newScopeName = newScopeName.replace("\\\\\\\\", File.separator); @@ -903,7 +912,12 @@ public class PolicyManagerServlet extends HttpServlet { } for(int i=0; i 1){ @@ -1275,7 +1306,12 @@ public class PolicyManagerServlet extends HttpServlet { entity.setModifiedBy(userId); controller.watchPolicyFunction(entity, policyNamewithExtension, "DeleteOne"); - String updatequery = "update PolicyVersion set active_version='"+highestVersion+"' , highest_version='"+highestVersion+"' where policy_name ='" +policyNamewithoutExtension.replace("\\", "\\\\")+"'"; + String updatequery = ""; + if(highestVersion != 0){ + updatequery = "update PolicyVersion set active_version='"+highestVersion+"' , highest_version='"+highestVersion+"' where policy_name ='" +policyNamewithoutExtension.replace("\\", "\\\\")+"'"; + }else{ + updatequery = "delete from PolicyVersion where policy_name ='" +policyNamewithoutExtension.replace("\\", "\\\\")+"' and id >0"; + } controller.executeQuery(updatequery); }else{ String policyVersionQuery = "delete from PolicyVersion where policy_name ='" +policyNamewithoutExtension.replace("\\", "\\\\")+"' and id >0"; @@ -1289,7 +1325,7 @@ public class PolicyManagerServlet extends HttpServlet { } } }else{ - List activePoliciesInPDP = new ArrayList(); + List activePoliciesInPDP = new ArrayList<>(); if(!policyEntityobjects.isEmpty()){ for(Object object : policyEntityobjects){ policyEntity = (PolicyEntity) object; @@ -1310,9 +1346,11 @@ public class PolicyManagerServlet extends HttpServlet { if(policyNamewithoutExtension.contains("Config_")){ Files.deleteIfExists(Paths.get(PolicyController.getConfigHome() + File.separator + policyEntity.getConfigurationData().getConfigurationName())); controller.deleteData(policyEntity.getConfigurationData()); + restController.notifyOtherPAPSToUpdateConfigurations("delete", null, policyEntity.getConfigurationData().getConfigurationName()); }else if(policyNamewithoutExtension.contains("Action_")){ Files.deleteIfExists(Paths.get(PolicyController.getActionHome() + File.separator + policyEntity.getActionBodyEntity().getActionBodyName())); controller.deleteData(policyEntity.getActionBodyEntity()); + restController.notifyOtherPAPSToUpdateConfigurations("delete", null, policyEntity.getActionBodyEntity().getActionBodyName()); } } }