diff options
Diffstat (limited to 'POLICY-SDK-APP/src/main/java/org/openecomp/policy/admin/PolicyManagerServlet.java')
-rw-r--r-- | POLICY-SDK-APP/src/main/java/org/openecomp/policy/admin/PolicyManagerServlet.java | 274 |
1 files changed, 151 insertions, 123 deletions
diff --git a/POLICY-SDK-APP/src/main/java/org/openecomp/policy/admin/PolicyManagerServlet.java b/POLICY-SDK-APP/src/main/java/org/openecomp/policy/admin/PolicyManagerServlet.java index aa8a227d3..a3f4ada0a 100644 --- a/POLICY-SDK-APP/src/main/java/org/openecomp/policy/admin/PolicyManagerServlet.java +++ b/POLICY-SDK-APP/src/main/java/org/openecomp/policy/admin/PolicyManagerServlet.java @@ -115,7 +115,7 @@ public class PolicyManagerServlet extends HttpServlet { PolicyManagerServlet.policyNames = policyNames; } - private static List<String> serviceTypeNamesList = new ArrayList<String>(); + private static List<String> serviceTypeNamesList = new ArrayList<>(); private List<Object> policyData; public static List<String> getServiceTypeNamesList() { @@ -142,20 +142,26 @@ public class PolicyManagerServlet extends HttpServlet { String location = closedLoopJsonLocation.toString(); try { inputStream = new FileInputStream(location); + if (location.endsWith("json")) { + JsonReader jsonReader = null; + jsonReader = Json.createReader(inputStream); + policyNames = jsonReader.readArray(); + serviceTypeNamesList = new ArrayList<>(); + for (int i = 0; i < policyNames.size(); i++) { + javax.json.JsonObject policyName = policyNames.getJsonObject(i); + String name = policyName.getJsonString("serviceTypePolicyName").getString(); + serviceTypeNamesList.add(name); + } + jsonReader.close(); + } } catch (FileNotFoundException e) { LOGGER.error("Exception Occured while initializing the JSONConfig file"+e); - } - if (location.endsWith("json")) { - JsonReader jsonReader = null; - jsonReader = Json.createReader(inputStream); - policyNames = jsonReader.readArray(); - serviceTypeNamesList = new ArrayList<String>(); - for (int i = 0; i < policyNames.size(); i++) { - javax.json.JsonObject policyName = policyNames.getJsonObject(i); - String name = policyName.getJsonString("serviceTypePolicyName").getString(); - serviceTypeNamesList.add(name); + }finally{ + try { + inputStream.close(); + } catch (IOException e) { + LOGGER.error("Exception Occured while closing the File InputStream"+e); } - jsonReader.close(); } } @@ -185,6 +191,7 @@ public class PolicyManagerServlet extends HttpServlet { out.print(responseJsonObject); out.flush(); } catch (Exception x) { + LOGGER.error("Exception Occured"+x); response.sendError(HttpStatus.SC_INTERNAL_SERVER_ERROR, x.getMessage()); } } @@ -193,7 +200,7 @@ public class PolicyManagerServlet extends HttpServlet { private void uploadFile(HttpServletRequest request, HttpServletResponse response) throws ServletException { try { String newFile; - Map<String, InputStream> files = new HashMap<String, InputStream>(); + Map<String, InputStream> files = new HashMap<>(); List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request); for (FileItem item : items) { @@ -218,7 +225,7 @@ public class PolicyManagerServlet extends HttpServlet { JSONObject responseJsonObject = null; responseJsonObject = this.success(); - response.setContentType("application/json"); + response.setContentType(CONTENTTYPE); PrintWriter out = response.getWriter(); out.print(responseJsonObject); out.flush(); @@ -286,7 +293,7 @@ public class PolicyManagerServlet extends HttpServlet { LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Exception Occured While doing File Operation" + e); responseJsonObject = error(e.getMessage()); } - response.setContentType("application/json"); + response.setContentType(CONTENTTYPE); PrintWriter out = response.getWriter(); out.print(responseJsonObject); out.flush(); @@ -295,20 +302,20 @@ public class PolicyManagerServlet extends HttpServlet { private JSONObject searchPolicyList(JSONObject params, HttpServletRequest request) { Set<String> scopes = null; List<String> roles = null; - policyData = new ArrayList<Object>(); + policyData = new ArrayList<>(); JSONArray policyList = null; if(params.has("policyList")){ policyList = (JSONArray) params.get("policyList"); } PolicyController controller = new PolicyController(); - List<JSONObject> resultList = new ArrayList<JSONObject>(); + List<JSONObject> resultList = new ArrayList<>(); try { //Get the Login Id of the User from Request String userId = UserUtils.getUserSession(request).getOrgUserId(); //Check if the Role and Scope Size are Null get the values from db. List<Object> userRoles = PolicyController.getRoles(userId); - roles = new ArrayList<String>(); - scopes = new HashSet<String>(); + roles = new ArrayList<>(); + scopes = new HashSet<>(); for(Object role: userRoles){ Roles userRole = (Roles) role; roles.add(userRole.getRole()); @@ -352,7 +359,7 @@ public class PolicyManagerServlet extends HttpServlet { } } }else{ - if (roles.contains("super-admin") || roles.contains("super-editor") || roles.contains("super-guest") ){ + if (roles.contains(SUPERADMIN) || roles.contains(SUPEREDITOR) || roles.contains(SUPERGUEST) ){ policyData = controller.getData(PolicyVersion.class); }else{ List<Object> filterdatas = controller.getData(PolicyVersion.class); @@ -494,7 +501,9 @@ public class PolicyManagerServlet extends HttpServlet { } catch (IOException e) { LOGGER.error("Exception Occured while Describing the Policy"+e); }finally{ - temp.delete(); + if(temp != null){ + temp.delete(); + } } }else{ return error("Error Occured while Describing the Policy"); @@ -512,8 +521,8 @@ public class PolicyManagerServlet extends HttpServlet { String userId = UserUtils.getUserSession(request).getOrgUserId(); //Check if the Role and Scope Size are Null get the values from db. List<Object> userRoles = PolicyController.getRoles(userId); - roles = new ArrayList<String>(); - scopes = new HashSet<String>(); + roles = new ArrayList<>(); + scopes = new HashSet<>(); for(Object role: userRoles){ Roles userRole = (Roles) role; roles.add(userRole.getRole()); @@ -534,7 +543,7 @@ public class PolicyManagerServlet extends HttpServlet { } } - List<JSONObject> resultList = new ArrayList<JSONObject>(); + List<JSONObject> resultList = new ArrayList<>(); boolean onlyFolders = params.getBoolean("onlyFolders"); String path = params.getString("path"); if(path.contains("..xml")){ @@ -680,7 +689,7 @@ public class PolicyManagerServlet extends HttpServlet { PolicyController controller = new PolicyController(); UserInfo userInfo = (UserInfo) controller.getEntityItem(UserInfo.class, "userLoginId", loginId); if(userInfo == null){ - return "super-admin"; + return SUPERADMIN; } return userInfo.getUserName(); } @@ -688,13 +697,19 @@ public class PolicyManagerServlet extends HttpServlet { //Rename Policy private JSONObject rename(JSONObject params, HttpServletRequest request) throws ServletException { try { + boolean isActive = false; + List<String> policyActiveInPDP = new ArrayList<>(); + Set<String> scopeOfPolicyActiveInPDP = new HashSet<>(); String userId = UserUtils.getUserSession(request).getOrgUserId(); String oldPath = params.getString("path"); String newPath = params.getString("newPath"); oldPath = oldPath.substring(oldPath.indexOf("/")+1); newPath = newPath.substring(newPath.indexOf("/")+1); if(oldPath.endsWith(".xml")){ - policyRename(oldPath, newPath, userId); + JSONObject result = policyRename(oldPath, newPath, userId); + if(!(Boolean)(result.getJSONObject("result").get("success"))){ + return result; + } }else{ String scopeName = oldPath; String newScopeName = newPath; @@ -715,18 +730,36 @@ public class PolicyManagerServlet extends HttpServlet { PolicyVersion activeVersion = (PolicyVersion) object; String policyOldPath = activeVersion.getPolicyName().replace(File.separator, "/") + "." + activeVersion.getActiveVersion() + ".xml"; String policyNewPath = policyOldPath.replace(oldPath, newPath); - policyRename(policyOldPath, policyNewPath, userId); + JSONObject result = policyRename(policyOldPath, policyNewPath, userId); + if(!(Boolean)(result.getJSONObject("result").get("success"))){ + isActive = true; + policyActiveInPDP.add(policyOldPath); + String scope = policyOldPath.substring(0, policyOldPath.lastIndexOf("/")); + scopeOfPolicyActiveInPDP.add(scope.replace("/", File.separator)); + } + } + boolean rename = false; + if(activePolicies.size() != policyActiveInPDP.size()){ + rename = true; } - for(Object object : scopesList){ - PolicyEditorScopes editorScopeEntity = (PolicyEditorScopes) object; - if(scopeName.contains("\\\\\\\\")){ - scopeName = scopeName.replace("\\\\\\\\", File.separator); - newScopeName = newScopeName.replace("\\\\\\\\", File.separator); + + UserInfo userInfo = new UserInfo(); + userInfo.setUserLoginId(userId); + if(policyActiveInPDP.size() == 0){ + renameScope(scopesList, scopeName, newScopeName, controller); + }else if(rename){ + renameScope(scopesList, scopeName, newScopeName, controller); + for(String scope : scopeOfPolicyActiveInPDP){ + PolicyEditorScopes editorScopeEntity = new PolicyEditorScopes(); + editorScopeEntity.setScopeName(scope.replace("\\", "\\\\\\\\")); + editorScopeEntity.setUserCreatedBy(userInfo); + editorScopeEntity.setUserModifiedBy(userInfo); + controller.saveData(editorScopeEntity); } - String scope = editorScopeEntity.getScopeName().replace(scopeName, newScopeName); - editorScopeEntity.setScopeName(scope); - controller.updateData(editorScopeEntity); } + if(isActive){ + return error("The Following policies rename failed. Since they are active in PDP Groups" +policyActiveInPDP); + } } return success(); } catch (Exception e) { @@ -735,11 +768,24 @@ public class PolicyManagerServlet extends HttpServlet { } } + private void renameScope(List<Object> scopesList, String scopeName, String newScopeName, PolicyController controller){ + for(Object object : scopesList){ + PolicyEditorScopes editorScopeEntity = (PolicyEditorScopes) object; + if(scopeName.contains("\\\\\\\\")){ + scopeName = scopeName.replace("\\\\\\\\", File.separator); + newScopeName = newScopeName.replace("\\\\\\\\", File.separator); + } + String scope = editorScopeEntity.getScopeName().replace(scopeName, newScopeName); + editorScopeEntity.setScopeName(scope); + controller.updateData(editorScopeEntity); + } + } + private JSONObject policyRename(String oldPath, String newPath, String userId) throws ServletException { try { PolicyEntity entity = null; PolicyController controller = new PolicyController(); - + String policyVersionName = newPath.replace(".xml", ""); String policyName = policyVersionName.substring(0, policyVersionName.lastIndexOf(".")).replace("/", File.separator); @@ -767,51 +813,42 @@ public class PolicyManagerServlet extends HttpServlet { oldPolicyCheck = oldPolicyCheck.replace(".Decision_", ":Decision_"); } String[] oldPolicySplit = oldPolicyCheck.split(":"); - + //Check PolicyEntity table with newPolicy Name String policyEntityquery = "FROM PolicyEntity where policyName = '"+newPolicySplit[1]+"' and scope ='"+newPolicySplit[0]+"'"; - System.out.println(policyEntityquery); List<Object> queryData = controller.getDataByQuery(policyEntityquery); if(!queryData.isEmpty()){ entity = (PolicyEntity) queryData.get(0); + return error("Policy rename failed. Since, the policy with same name already exists."); } - - if(entity != null){ - //if a policy exists with new name check if it is deleted or not - if(entity.isDeleted()){ - //Check Policy Group Entity table if policy has been pushed or not - String query = "from PolicyGroupEntity where policyid = '"+entity.getPolicyId()+"'"; - List<Object> object = controller.getDataByQuery(query); - if(object.isEmpty()){ - //if PolicyGroupEntity data is empty delete the entry from database - controller.deleteData(entity); - //Query the Policy Entity with oldPolicy Name - String oldpolicyEntityquery = "FROM PolicyEntity where policyName = '"+oldPolicySplit[1]+"' and scope ='"+oldPolicySplit[0]+"'"; - System.out.println(oldpolicyEntityquery); - List<Object> oldEntityData = controller.getDataByQuery(oldpolicyEntityquery); - if(!oldEntityData.isEmpty()){ - entity = (PolicyEntity) oldEntityData.get(0); - } - checkOldPolicyEntryAndUpdate(entity, newPolicySplit[0], newPolicySplit[1], oldPolicySplit[0], oldPolicySplit[1], policyName, newpolicyName, oldpolicyName, userId); + + //Query the Policy Entity with oldPolicy Name + String policyEntityCheck = oldPolicySplit[1].substring(0, oldPolicySplit[1].indexOf(".")); + String oldpolicyEntityquery = "FROM PolicyEntity where policyName like '"+policyEntityCheck+"%' and scope ='"+oldPolicySplit[0]+"'"; + List<Object> oldEntityData = controller.getDataByQuery(oldpolicyEntityquery); + if(!oldEntityData.isEmpty()){ + String groupQuery = "FROM PolicyGroupEntity where ("; + for(int i=0; i<oldEntityData.size(); i++){ + entity = (PolicyEntity) oldEntityData.get(i); + if(i == 0){ + groupQuery = groupQuery + "policyid =" + entity.getPolicyId(); }else{ - return error("Policy rename failed due to policy with new name existing in PDP Group."); + groupQuery = groupQuery + " or policyid =" + entity.getPolicyId(); } - }else{ - return error("Policy rename failed due to same name existing."); } - }else{ - //Query the Policy Entity with oldPolicy Name - String policyEntityCheck = oldPolicySplit[1].substring(0, oldPolicySplit[1].indexOf(".")); - String oldpolicyEntityquery = "FROM PolicyEntity where policyName like '"+policyEntityCheck+"%' and scope ='"+oldPolicySplit[0]+"'"; - List<Object> oldEntityData = controller.getDataByQuery(oldpolicyEntityquery); - if(!oldEntityData.isEmpty()){ - for(int i=0; i<oldEntityData.size(); i++){ - entity = (PolicyEntity) oldEntityData.get(i); - checkOldPolicyEntryAndUpdate(entity, newPolicySplit[0] , newPolicySplit[1], oldPolicySplit[0], oldPolicySplit[1], policyName, newpolicyName, oldpolicyName, userId); - } + groupQuery = groupQuery + ")"; + List<Object> groupEntityData = controller.getDataByQuery(groupQuery); + if(groupEntityData.size() > 0){ + return error("Policy rename failed. Since the policy or its version is active in PDP Groups."); } + for(int i=0; i<oldEntityData.size(); i++){ + entity = (PolicyEntity) oldEntityData.get(i); + checkOldPolicyEntryAndUpdate(entity, newPolicySplit[0] , newPolicySplit[1], oldPolicySplit[0], oldPolicySplit[1], policyName, newpolicyName, oldpolicyName, userId); + } + }else{ + return error("Policy rename failed due to policy not able to retrieve from database. Please, contact super-admin."); } - + return success(); } catch (Exception e) { LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Exception Occured While Renaming Policy"+e); @@ -822,60 +859,54 @@ public class PolicyManagerServlet extends HttpServlet { private JSONObject checkOldPolicyEntryAndUpdate(PolicyEntity entity, String newScope, String removenewPolicyExtension, String oldScope, String removeoldPolicyExtension, String policyName, String newpolicyName, String oldpolicyName, String userId) throws ServletException{ try { - ConfigurationDataEntity configEntity; - ActionBodyEntity actionEntity; + ConfigurationDataEntity configEntity = entity.getConfigurationData(); + ActionBodyEntity actionEntity = entity.getActionBodyEntity(); PolicyController controller = new PolicyController(); - configEntity = entity.getConfigurationData(); - actionEntity = entity.getActionBodyEntity(); - if(entity != null){ - //Check Policy Group Entity table if policy has been pushed or not - String query = "from PolicyGroupEntity where policyid = '"+entity.getPolicyId()+"'"; - List<Object> object = controller.getDataByQuery(query); - if(object.isEmpty()){ - String oldPolicyNameWithoutExtension = removeoldPolicyExtension; - String newPolicyNameWithoutExtension = removenewPolicyExtension; - if(removeoldPolicyExtension.endsWith(".xml")){ - oldPolicyNameWithoutExtension = oldPolicyNameWithoutExtension.substring(0, oldPolicyNameWithoutExtension.indexOf(".")); - newPolicyNameWithoutExtension = newPolicyNameWithoutExtension.substring(0, newPolicyNameWithoutExtension.indexOf(".")); - } - entity.setPolicyName(entity.getPolicyName().replace(oldPolicyNameWithoutExtension, newPolicyNameWithoutExtension)); - entity.setPolicyData(entity.getPolicyData().replace(oldScope +"."+oldPolicyNameWithoutExtension, newScope+"."+newPolicyNameWithoutExtension)); - entity.setScope(newScope); - entity.setModifiedBy(userId); - if(newpolicyName.contains("Config_")){ - configEntity.setConfigurationName(configEntity.getConfigurationName().replace(oldScope +"."+oldPolicyNameWithoutExtension, newScope+"."+newPolicyNameWithoutExtension)); - controller.updateData(configEntity); - }else if(newpolicyName.contains("Action_")){ - actionEntity.setActionBody(actionEntity.getActionBody().replace(oldScope +"."+oldPolicyNameWithoutExtension, newScope+"."+newPolicyNameWithoutExtension)); - controller.updateData(actionEntity); - } - controller.updateData(entity); - }else{ - //Mark as Deleted in PolicyEntiy table - entity.setDeleted(true); - controller.updateData(entity); - //Mark as Deleted in ConfigurationDataEntity table - configEntity.setDeleted(true); - controller.updateData(configEntity); - //Mark as Deleted in ActionDataEntity table - actionEntity.setDeleted(true); - controller.updateData(actionEntity); - //Clone New Copy - cloneRecord(newpolicyName, oldScope, removeoldPolicyExtension, newScope, removenewPolicyExtension, entity, userId); - } - PolicyVersion versionEntity = (PolicyVersion) controller.getEntityItem(PolicyVersion.class, "policyName", oldpolicyName); - versionEntity.setPolicyName(policyName); - versionEntity.setModifiedBy(userId); - controller.updateData(versionEntity); - String movePolicyCheck = policyName.substring(policyName.lastIndexOf(File.separator)+1); - String moveOldPolicyCheck = oldpolicyName.substring(oldpolicyName.lastIndexOf(File.separator)+1); - if(movePolicyCheck.equals(moveOldPolicyCheck)){ - controller.watchPolicyFunction(versionEntity, oldpolicyName, "Move"); - }else{ - controller.watchPolicyFunction(versionEntity, oldpolicyName, "Rename"); + String oldPolicyNameWithoutExtension = removeoldPolicyExtension; + String newPolicyNameWithoutExtension = removenewPolicyExtension; + if(removeoldPolicyExtension.endsWith(".xml")){ + oldPolicyNameWithoutExtension = oldPolicyNameWithoutExtension.substring(0, oldPolicyNameWithoutExtension.indexOf(".")); + newPolicyNameWithoutExtension = newPolicyNameWithoutExtension.substring(0, newPolicyNameWithoutExtension.indexOf(".")); + } + entity.setPolicyName(entity.getPolicyName().replace(oldPolicyNameWithoutExtension, newPolicyNameWithoutExtension)); + entity.setPolicyData(entity.getPolicyData().replace(oldScope +"."+oldPolicyNameWithoutExtension, newScope+"."+newPolicyNameWithoutExtension)); + entity.setScope(newScope); + entity.setModifiedBy(userId); + if(newpolicyName.contains("Config_")){ + String oldConfigurationName = configEntity.getConfigurationName(); + configEntity.setConfigurationName(configEntity.getConfigurationName().replace(oldScope +"."+oldPolicyNameWithoutExtension, newScope+"."+newPolicyNameWithoutExtension)); + controller.updateData(configEntity); + String newConfigurationName = configEntity.getConfigurationName(); + File file = new File(PolicyController.configHome + File.separator + oldConfigurationName); + if(file.exists()){ + File renamefile = new File(PolicyController.configHome + File.separator + newConfigurationName); + file.renameTo(renamefile); + } + }else if(newpolicyName.contains("Action_")){ + String oldConfigurationName = actionEntity.getActionBodyName(); + actionEntity.setActionBody(actionEntity.getActionBody().replace(oldScope +"."+oldPolicyNameWithoutExtension, newScope+"."+newPolicyNameWithoutExtension)); + controller.updateData(actionEntity); + String newConfigurationName = actionEntity.getActionBodyName(); + File file = new File(PolicyController.actionHome + File.separator + oldConfigurationName); + if(file.exists()){ + File renamefile = new File(PolicyController.actionHome + File.separator + newConfigurationName); + file.renameTo(renamefile); } } + controller.updateData(entity); + + PolicyVersion versionEntity = (PolicyVersion) controller.getEntityItem(PolicyVersion.class, "policyName", oldpolicyName); + versionEntity.setPolicyName(policyName); + versionEntity.setModifiedBy(userId); + controller.updateData(versionEntity); + String movePolicyCheck = policyName.substring(policyName.lastIndexOf(File.separator)+1); + String moveOldPolicyCheck = oldpolicyName.substring(oldpolicyName.lastIndexOf(File.separator)+1); + if(movePolicyCheck.equals(moveOldPolicyCheck)){ + controller.watchPolicyFunction(versionEntity, oldpolicyName, "Move"); + }else{ + controller.watchPolicyFunction(versionEntity, oldpolicyName, "Rename"); + } return success(); } catch (Exception e) { LOGGER.error("Exception Occured"+e); @@ -970,7 +1001,6 @@ public class PolicyManagerServlet extends HttpServlet { //Check PolicyEntity table with newPolicy Name String policyEntityquery = "FROM PolicyEntity where policyName = '"+newPolicySplit[1]+"' and scope ='"+newPolicySplit[0]+"'"; - System.out.println(policyEntityquery); List<Object> queryData = controller.getDataByQuery(policyEntityquery); if(!queryData.isEmpty()){ entity = (PolicyEntity) queryData.get(0); @@ -986,7 +1016,6 @@ public class PolicyManagerServlet extends HttpServlet { controller.deleteData(entity); //Query the Policy Entity with oldPolicy Name policyEntityquery = "FROM PolicyEntity where policyName = '"+oldPolicySplit[1]+"' and scope ='"+oldPolicySplit[0]+"'"; - System.out.println(policyEntityquery); queryData = controller.getDataByQuery(policyEntityquery); if(!queryData.isEmpty()){ entity = (PolicyEntity) queryData.get(0); @@ -1004,7 +1033,6 @@ public class PolicyManagerServlet extends HttpServlet { }else{ //Query the Policy Entity with oldPolicy Name policyEntityquery = "FROM PolicyEntity where policyName = '"+oldPolicySplit[1]+"' and scope ='"+oldPolicySplit[0]+"'"; - System.out.println(policyEntityquery); queryData = controller.getDataByQuery(policyEntityquery); if(!queryData.isEmpty()){ entity = (PolicyEntity) queryData.get(0); |