@Test public void testNegativeCase2() throws IOException { thrown.expect(NullPointerException.class); apController.pushPolicyToPDPGroup(null, null); }
@RequestMapping(value = {"/auto_Push/PushPolicyToPDP.htm"}, method = {RequestMethod.POST}) public ModelAndView pushPolicyToPDPGroup(HttpServletRequest request, HttpServletResponse response) throws IOException { try { response.setCharacterEncoding(PolicyUtils.CHARACTER_ENCODING); request.setCharacterEncoding(PolicyUtils.CHARACTER_ENCODING); ArrayList<Object> selectedPdps = new ArrayList<>(); ArrayList<String> selectedPoliciesInUI = new ArrayList<>(); PolicyController controller = getPolicyControllerInstance(); this.groups.addAll(controller.getPapEngine().getOnapPDPGroups()); ObjectMapper mapper = new ObjectMapper(); this.container = new PDPGroupContainer(controller.getPapEngine()); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); JsonNode root = mapper.readTree(request.getReader()); String userId = UserUtils.getUserSession(request).getOrgUserId(); logger.info( "**********************Logging UserID while Pushing Policy to PDP Group***********************"); logger.info("UserId: " + userId + "Push Policy Data: " + root.get("pushTabData").toString()); logger.info( "**********************************************************************************************"); AutoPushTabAdapter adapter = mapper.readValue(root.get("pushTabData").toString(), AutoPushTabAdapter.class); for (Object pdpGroupId : adapter.getPdpDatas()) { LinkedHashMap<?, ?> selectedPdp = (LinkedHashMap<?, ?>) pdpGroupId; for (OnapPDPGroup pdpGroup : this.groups) { if (pdpGroup.getId().equals(selectedPdp.get("id"))) { selectedPdps.add(pdpGroup); } } } for (Object policyId : adapter.getPolicyDatas()) { LinkedHashMap<?, ?> selected = (LinkedHashMap<?, ?>) policyId; String policyName = selected.get("policyName").toString() + "." + selected.get("activeVersion").toString() + ".xml"; selectedPoliciesInUI.add(policyName); } for (Object pdpDestinationGroupId : selectedPdps) { Set<PDPPolicy> currentPoliciesInGroup = new HashSet<>(); Set<PDPPolicy> selectedPolicies = new HashSet<>(); for (String policyId : selectedPoliciesInUI) { logger.debug("Handlepolicies..." + pdpDestinationGroupId + policyId); assert policyId != null; String name = policyId.replace(File.separator, "."); String id = name; if (id.endsWith(".xml")) { id = id.replace(".xml", ""); id = id.substring(0, id.lastIndexOf('.')); } StdPDPPolicy selectedPolicy = null; String dbCheckName = name; 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[] split = dbCheckName.split(":"); String query = "FROM PolicyEntity where policyName = :split_1 and scope = :split_0"; SimpleBindings policyParams = new SimpleBindings(); policyParams.put("split_1", split[1]); policyParams.put("split_0", split[0]); List<Object> queryData = controller.getDataByQuery(query, policyParams); PolicyEntity policyEntity = (PolicyEntity) queryData.get(0); File temp = new File(name); BufferedWriter bw = new BufferedWriter(new FileWriter(temp)); bw.write(policyEntity.getPolicyData()); bw.close(); try { selectedPolicy = new StdPDPPolicy(name, true, id, temp.toURI()); } catch (IOException e) { logger.error("Unable to create policy '" + name + "': " + e.getMessage(), e); } StdPDPGroup selectedGroup = (StdPDPGroup) pdpDestinationGroupId; if (selectedPolicy != null) { for (OnapPDPGroup group : container.getGroups()) { if (group.getId().equals(selectedGroup.getId())) { currentPoliciesInGroup.addAll(group.getPolicies()); } } try { controller.getPapEngine().copyPolicy(selectedPolicy, (StdPDPGroup) pdpDestinationGroupId, userId); } catch (PAPException e) { logger.error("Exception Occured" + e); return null; } selectedPolicies.add(selectedPolicy); } temp.delete(); } StdPDPGroup pdpGroup = (StdPDPGroup) pdpDestinationGroupId; StdPDPGroup updatedGroupObject = new StdPDPGroup(pdpGroup.getId(), pdpGroup.isDefaultGroup(), pdpGroup.getName(), pdpGroup.getDescription(), pdpGroup.getDirectory()); updatedGroupObject.setOnapPdps(pdpGroup.getOnapPdps()); updatedGroupObject.setPipConfigs(pdpGroup.getPipConfigs()); updatedGroupObject.setStatus(pdpGroup.getStatus()); updatedGroupObject.setOperation("push"); Iterator<PDPPolicy> policyIterator = currentPoliciesInGroup.iterator(); logger.debug("policyIterator....." + selectedPolicies); while (policyIterator.hasNext()) { PDPPolicy existingPolicy = policyIterator.next(); for (PDPPolicy selPolicy : selectedPolicies) { if (selPolicy.getName().equals(existingPolicy.getName())) { if (selPolicy.getVersion().equals(existingPolicy.getVersion())) { if (selPolicy.getId().equals(existingPolicy.getId())) { policyIterator.remove(); logger.debug("Removing policy: " + selPolicy); break; } } else { policyIterator.remove(); logger.debug("Removing Old Policy version: " + selPolicy); break; } } } } currentPoliciesInGroup.addAll(selectedPolicies); updatedGroupObject.setPolicies(currentPoliciesInGroup); this.container.updateGroup(updatedGroupObject, userId); response.setContentType(PolicyUtils.APPLICATION_JSON); refreshGroups(); response.getWriter().write(new JSONObject( new JsonMessage(mapper.writeValueAsString(groups))).toString()); } } catch (Exception e) { logger.error(e); response.getWriter().write(PolicyUtils.CATCH_EXCEPTION); } return null; }
AutoPushController extends RestrictedBaseController { @RequestMapping(value = {"/auto_Push/PushPolicyToPDP.htm"}, method = {RequestMethod.POST}) public ModelAndView pushPolicyToPDPGroup(HttpServletRequest request, HttpServletResponse response) throws IOException { try { response.setCharacterEncoding(PolicyUtils.CHARACTER_ENCODING); request.setCharacterEncoding(PolicyUtils.CHARACTER_ENCODING); ArrayList<Object> selectedPdps = new ArrayList<>(); ArrayList<String> selectedPoliciesInUI = new ArrayList<>(); PolicyController controller = getPolicyControllerInstance(); this.groups.addAll(controller.getPapEngine().getOnapPDPGroups()); ObjectMapper mapper = new ObjectMapper(); this.container = new PDPGroupContainer(controller.getPapEngine()); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); JsonNode root = mapper.readTree(request.getReader()); String userId = UserUtils.getUserSession(request).getOrgUserId(); logger.info( "**********************Logging UserID while Pushing Policy to PDP Group***********************"); logger.info("UserId: " + userId + "Push Policy Data: " + root.get("pushTabData").toString()); logger.info( "**********************************************************************************************"); AutoPushTabAdapter adapter = mapper.readValue(root.get("pushTabData").toString(), AutoPushTabAdapter.class); for (Object pdpGroupId : adapter.getPdpDatas()) { LinkedHashMap<?, ?> selectedPdp = (LinkedHashMap<?, ?>) pdpGroupId; for (OnapPDPGroup pdpGroup : this.groups) { if (pdpGroup.getId().equals(selectedPdp.get("id"))) { selectedPdps.add(pdpGroup); } } } for (Object policyId : adapter.getPolicyDatas()) { LinkedHashMap<?, ?> selected = (LinkedHashMap<?, ?>) policyId; String policyName = selected.get("policyName").toString() + "." + selected.get("activeVersion").toString() + ".xml"; selectedPoliciesInUI.add(policyName); } for (Object pdpDestinationGroupId : selectedPdps) { Set<PDPPolicy> currentPoliciesInGroup = new HashSet<>(); Set<PDPPolicy> selectedPolicies = new HashSet<>(); for (String policyId : selectedPoliciesInUI) { logger.debug("Handlepolicies..." + pdpDestinationGroupId + policyId); assert policyId != null; String name = policyId.replace(File.separator, "."); String id = name; if (id.endsWith(".xml")) { id = id.replace(".xml", ""); id = id.substring(0, id.lastIndexOf('.')); } StdPDPPolicy selectedPolicy = null; String dbCheckName = name; 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[] split = dbCheckName.split(":"); String query = "FROM PolicyEntity where policyName = :split_1 and scope = :split_0"; SimpleBindings policyParams = new SimpleBindings(); policyParams.put("split_1", split[1]); policyParams.put("split_0", split[0]); List<Object> queryData = controller.getDataByQuery(query, policyParams); PolicyEntity policyEntity = (PolicyEntity) queryData.get(0); File temp = new File(name); BufferedWriter bw = new BufferedWriter(new FileWriter(temp)); bw.write(policyEntity.getPolicyData()); bw.close(); try { selectedPolicy = new StdPDPPolicy(name, true, id, temp.toURI()); } catch (IOException e) { logger.error("Unable to create policy '" + name + "': " + e.getMessage(), e); } StdPDPGroup selectedGroup = (StdPDPGroup) pdpDestinationGroupId; if (selectedPolicy != null) { for (OnapPDPGroup group : container.getGroups()) { if (group.getId().equals(selectedGroup.getId())) { currentPoliciesInGroup.addAll(group.getPolicies()); } } try { controller.getPapEngine().copyPolicy(selectedPolicy, (StdPDPGroup) pdpDestinationGroupId, userId); } catch (PAPException e) { logger.error("Exception Occured" + e); return null; } selectedPolicies.add(selectedPolicy); } temp.delete(); } StdPDPGroup pdpGroup = (StdPDPGroup) pdpDestinationGroupId; StdPDPGroup updatedGroupObject = new StdPDPGroup(pdpGroup.getId(), pdpGroup.isDefaultGroup(), pdpGroup.getName(), pdpGroup.getDescription(), pdpGroup.getDirectory()); updatedGroupObject.setOnapPdps(pdpGroup.getOnapPdps()); updatedGroupObject.setPipConfigs(pdpGroup.getPipConfigs()); updatedGroupObject.setStatus(pdpGroup.getStatus()); updatedGroupObject.setOperation("push"); Iterator<PDPPolicy> policyIterator = currentPoliciesInGroup.iterator(); logger.debug("policyIterator....." + selectedPolicies); while (policyIterator.hasNext()) { PDPPolicy existingPolicy = policyIterator.next(); for (PDPPolicy selPolicy : selectedPolicies) { if (selPolicy.getName().equals(existingPolicy.getName())) { if (selPolicy.getVersion().equals(existingPolicy.getVersion())) { if (selPolicy.getId().equals(existingPolicy.getId())) { policyIterator.remove(); logger.debug("Removing policy: " + selPolicy); break; } } else { policyIterator.remove(); logger.debug("Removing Old Policy version: " + selPolicy); break; } } } } currentPoliciesInGroup.addAll(selectedPolicies); updatedGroupObject.setPolicies(currentPoliciesInGroup); this.container.updateGroup(updatedGroupObject, userId); response.setContentType(PolicyUtils.APPLICATION_JSON); refreshGroups(); response.getWriter().write(new JSONObject( new JsonMessage(mapper.writeValueAsString(groups))).toString()); } } catch (Exception e) { logger.error(e); response.getWriter().write(PolicyUtils.CATCH_EXCEPTION); } return null; } }
AutoPushController extends RestrictedBaseController { @RequestMapping(value = {"/auto_Push/PushPolicyToPDP.htm"}, method = {RequestMethod.POST}) public ModelAndView pushPolicyToPDPGroup(HttpServletRequest request, HttpServletResponse response) throws IOException { try { response.setCharacterEncoding(PolicyUtils.CHARACTER_ENCODING); request.setCharacterEncoding(PolicyUtils.CHARACTER_ENCODING); ArrayList<Object> selectedPdps = new ArrayList<>(); ArrayList<String> selectedPoliciesInUI = new ArrayList<>(); PolicyController controller = getPolicyControllerInstance(); this.groups.addAll(controller.getPapEngine().getOnapPDPGroups()); ObjectMapper mapper = new ObjectMapper(); this.container = new PDPGroupContainer(controller.getPapEngine()); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); JsonNode root = mapper.readTree(request.getReader()); String userId = UserUtils.getUserSession(request).getOrgUserId(); logger.info( "**********************Logging UserID while Pushing Policy to PDP Group***********************"); logger.info("UserId: " + userId + "Push Policy Data: " + root.get("pushTabData").toString()); logger.info( "**********************************************************************************************"); AutoPushTabAdapter adapter = mapper.readValue(root.get("pushTabData").toString(), AutoPushTabAdapter.class); for (Object pdpGroupId : adapter.getPdpDatas()) { LinkedHashMap<?, ?> selectedPdp = (LinkedHashMap<?, ?>) pdpGroupId; for (OnapPDPGroup pdpGroup : this.groups) { if (pdpGroup.getId().equals(selectedPdp.get("id"))) { selectedPdps.add(pdpGroup); } } } for (Object policyId : adapter.getPolicyDatas()) { LinkedHashMap<?, ?> selected = (LinkedHashMap<?, ?>) policyId; String policyName = selected.get("policyName").toString() + "." + selected.get("activeVersion").toString() + ".xml"; selectedPoliciesInUI.add(policyName); } for (Object pdpDestinationGroupId : selectedPdps) { Set<PDPPolicy> currentPoliciesInGroup = new HashSet<>(); Set<PDPPolicy> selectedPolicies = new HashSet<>(); for (String policyId : selectedPoliciesInUI) { logger.debug("Handlepolicies..." + pdpDestinationGroupId + policyId); assert policyId != null; String name = policyId.replace(File.separator, "."); String id = name; if (id.endsWith(".xml")) { id = id.replace(".xml", ""); id = id.substring(0, id.lastIndexOf('.')); } StdPDPPolicy selectedPolicy = null; String dbCheckName = name; 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[] split = dbCheckName.split(":"); String query = "FROM PolicyEntity where policyName = :split_1 and scope = :split_0"; SimpleBindings policyParams = new SimpleBindings(); policyParams.put("split_1", split[1]); policyParams.put("split_0", split[0]); List<Object> queryData = controller.getDataByQuery(query, policyParams); PolicyEntity policyEntity = (PolicyEntity) queryData.get(0); File temp = new File(name); BufferedWriter bw = new BufferedWriter(new FileWriter(temp)); bw.write(policyEntity.getPolicyData()); bw.close(); try { selectedPolicy = new StdPDPPolicy(name, true, id, temp.toURI()); } catch (IOException e) { logger.error("Unable to create policy '" + name + "': " + e.getMessage(), e); } StdPDPGroup selectedGroup = (StdPDPGroup) pdpDestinationGroupId; if (selectedPolicy != null) { for (OnapPDPGroup group : container.getGroups()) { if (group.getId().equals(selectedGroup.getId())) { currentPoliciesInGroup.addAll(group.getPolicies()); } } try { controller.getPapEngine().copyPolicy(selectedPolicy, (StdPDPGroup) pdpDestinationGroupId, userId); } catch (PAPException e) { logger.error("Exception Occured" + e); return null; } selectedPolicies.add(selectedPolicy); } temp.delete(); } StdPDPGroup pdpGroup = (StdPDPGroup) pdpDestinationGroupId; StdPDPGroup updatedGroupObject = new StdPDPGroup(pdpGroup.getId(), pdpGroup.isDefaultGroup(), pdpGroup.getName(), pdpGroup.getDescription(), pdpGroup.getDirectory()); updatedGroupObject.setOnapPdps(pdpGroup.getOnapPdps()); updatedGroupObject.setPipConfigs(pdpGroup.getPipConfigs()); updatedGroupObject.setStatus(pdpGroup.getStatus()); updatedGroupObject.setOperation("push"); Iterator<PDPPolicy> policyIterator = currentPoliciesInGroup.iterator(); logger.debug("policyIterator....." + selectedPolicies); while (policyIterator.hasNext()) { PDPPolicy existingPolicy = policyIterator.next(); for (PDPPolicy selPolicy : selectedPolicies) { if (selPolicy.getName().equals(existingPolicy.getName())) { if (selPolicy.getVersion().equals(existingPolicy.getVersion())) { if (selPolicy.getId().equals(existingPolicy.getId())) { policyIterator.remove(); logger.debug("Removing policy: " + selPolicy); break; } } else { policyIterator.remove(); logger.debug("Removing Old Policy version: " + selPolicy); break; } } } } currentPoliciesInGroup.addAll(selectedPolicies); updatedGroupObject.setPolicies(currentPoliciesInGroup); this.container.updateGroup(updatedGroupObject, userId); response.setContentType(PolicyUtils.APPLICATION_JSON); refreshGroups(); response.getWriter().write(new JSONObject( new JsonMessage(mapper.writeValueAsString(groups))).toString()); } } catch (Exception e) { logger.error(e); response.getWriter().write(PolicyUtils.CATCH_EXCEPTION); } return null; }  }
AutoPushController extends RestrictedBaseController { @RequestMapping(value = {"/auto_Push/PushPolicyToPDP.htm"}, method = {RequestMethod.POST}) public ModelAndView pushPolicyToPDPGroup(HttpServletRequest request, HttpServletResponse response) throws IOException { try { response.setCharacterEncoding(PolicyUtils.CHARACTER_ENCODING); request.setCharacterEncoding(PolicyUtils.CHARACTER_ENCODING); ArrayList<Object> selectedPdps = new ArrayList<>(); ArrayList<String> selectedPoliciesInUI = new ArrayList<>(); PolicyController controller = getPolicyControllerInstance(); this.groups.addAll(controller.getPapEngine().getOnapPDPGroups()); ObjectMapper mapper = new ObjectMapper(); this.container = new PDPGroupContainer(controller.getPapEngine()); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); JsonNode root = mapper.readTree(request.getReader()); String userId = UserUtils.getUserSession(request).getOrgUserId(); logger.info( "**********************Logging UserID while Pushing Policy to PDP Group***********************"); logger.info("UserId: " + userId + "Push Policy Data: " + root.get("pushTabData").toString()); logger.info( "**********************************************************************************************"); AutoPushTabAdapter adapter = mapper.readValue(root.get("pushTabData").toString(), AutoPushTabAdapter.class); for (Object pdpGroupId : adapter.getPdpDatas()) { LinkedHashMap<?, ?> selectedPdp = (LinkedHashMap<?, ?>) pdpGroupId; for (OnapPDPGroup pdpGroup : this.groups) { if (pdpGroup.getId().equals(selectedPdp.get("id"))) { selectedPdps.add(pdpGroup); } } } for (Object policyId : adapter.getPolicyDatas()) { LinkedHashMap<?, ?> selected = (LinkedHashMap<?, ?>) policyId; String policyName = selected.get("policyName").toString() + "." + selected.get("activeVersion").toString() + ".xml"; selectedPoliciesInUI.add(policyName); } for (Object pdpDestinationGroupId : selectedPdps) { Set<PDPPolicy> currentPoliciesInGroup = new HashSet<>(); Set<PDPPolicy> selectedPolicies = new HashSet<>(); for (String policyId : selectedPoliciesInUI) { logger.debug("Handlepolicies..." + pdpDestinationGroupId + policyId); assert policyId != null; String name = policyId.replace(File.separator, "."); String id = name; if (id.endsWith(".xml")) { id = id.replace(".xml", ""); id = id.substring(0, id.lastIndexOf('.')); } StdPDPPolicy selectedPolicy = null; String dbCheckName = name; 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[] split = dbCheckName.split(":"); String query = "FROM PolicyEntity where policyName = :split_1 and scope = :split_0"; SimpleBindings policyParams = new SimpleBindings(); policyParams.put("split_1", split[1]); policyParams.put("split_0", split[0]); List<Object> queryData = controller.getDataByQuery(query, policyParams); PolicyEntity policyEntity = (PolicyEntity) queryData.get(0); File temp = new File(name); BufferedWriter bw = new BufferedWriter(new FileWriter(temp)); bw.write(policyEntity.getPolicyData()); bw.close(); try { selectedPolicy = new StdPDPPolicy(name, true, id, temp.toURI()); } catch (IOException e) { logger.error("Unable to create policy '" + name + "': " + e.getMessage(), e); } StdPDPGroup selectedGroup = (StdPDPGroup) pdpDestinationGroupId; if (selectedPolicy != null) { for (OnapPDPGroup group : container.getGroups()) { if (group.getId().equals(selectedGroup.getId())) { currentPoliciesInGroup.addAll(group.getPolicies()); } } try { controller.getPapEngine().copyPolicy(selectedPolicy, (StdPDPGroup) pdpDestinationGroupId, userId); } catch (PAPException e) { logger.error("Exception Occured" + e); return null; } selectedPolicies.add(selectedPolicy); } temp.delete(); } StdPDPGroup pdpGroup = (StdPDPGroup) pdpDestinationGroupId; StdPDPGroup updatedGroupObject = new StdPDPGroup(pdpGroup.getId(), pdpGroup.isDefaultGroup(), pdpGroup.getName(), pdpGroup.getDescription(), pdpGroup.getDirectory()); updatedGroupObject.setOnapPdps(pdpGroup.getOnapPdps()); updatedGroupObject.setPipConfigs(pdpGroup.getPipConfigs()); updatedGroupObject.setStatus(pdpGroup.getStatus()); updatedGroupObject.setOperation("push"); Iterator<PDPPolicy> policyIterator = currentPoliciesInGroup.iterator(); logger.debug("policyIterator....." + selectedPolicies); while (policyIterator.hasNext()) { PDPPolicy existingPolicy = policyIterator.next(); for (PDPPolicy selPolicy : selectedPolicies) { if (selPolicy.getName().equals(existingPolicy.getName())) { if (selPolicy.getVersion().equals(existingPolicy.getVersion())) { if (selPolicy.getId().equals(existingPolicy.getId())) { policyIterator.remove(); logger.debug("Removing policy: " + selPolicy); break; } } else { policyIterator.remove(); logger.debug("Removing Old Policy version: " + selPolicy); break; } } } } currentPoliciesInGroup.addAll(selectedPolicies); updatedGroupObject.setPolicies(currentPoliciesInGroup); this.container.updateGroup(updatedGroupObject, userId); response.setContentType(PolicyUtils.APPLICATION_JSON); refreshGroups(); response.getWriter().write(new JSONObject( new JsonMessage(mapper.writeValueAsString(groups))).toString()); } } catch (Exception e) { logger.error(e); response.getWriter().write(PolicyUtils.CATCH_EXCEPTION); } return null; }  PolicyController getPolicyController(); void setPolicyController(PolicyController policyController); synchronized void refreshGroups(); @RequestMapping( value = {"/get_AutoPushPoliciesContainerData"}, method = {RequestMethod.GET}, produces = MediaType.APPLICATION_JSON_VALUE) void getPolicyGroupContainerData(HttpServletRequest request, HttpServletResponse response); @RequestMapping(value = {"/auto_Push/PushPolicyToPDP.htm"}, method = {RequestMethod.POST}) ModelAndView pushPolicyToPDPGroup(HttpServletRequest request, HttpServletResponse response); @SuppressWarnings("unchecked") @RequestMapping(value = {"/auto_Push/remove_GroupPolicies.htm"}, method = {RequestMethod.POST}) ModelAndView removePDPGroup(HttpServletRequest request, HttpServletResponse response); }
AutoPushController extends RestrictedBaseController { @RequestMapping(value = {"/auto_Push/PushPolicyToPDP.htm"}, method = {RequestMethod.POST}) public ModelAndView pushPolicyToPDPGroup(HttpServletRequest request, HttpServletResponse response) throws IOException { try { response.setCharacterEncoding(PolicyUtils.CHARACTER_ENCODING); request.setCharacterEncoding(PolicyUtils.CHARACTER_ENCODING); ArrayList<Object> selectedPdps = new ArrayList<>(); ArrayList<String> selectedPoliciesInUI = new ArrayList<>(); PolicyController controller = getPolicyControllerInstance(); this.groups.addAll(controller.getPapEngine().getOnapPDPGroups()); ObjectMapper mapper = new ObjectMapper(); this.container = new PDPGroupContainer(controller.getPapEngine()); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); JsonNode root = mapper.readTree(request.getReader()); String userId = UserUtils.getUserSession(request).getOrgUserId(); logger.info( "**********************Logging UserID while Pushing Policy to PDP Group***********************"); logger.info("UserId: " + userId + "Push Policy Data: " + root.get("pushTabData").toString()); logger.info( "**********************************************************************************************"); AutoPushTabAdapter adapter = mapper.readValue(root.get("pushTabData").toString(), AutoPushTabAdapter.class); for (Object pdpGroupId : adapter.getPdpDatas()) { LinkedHashMap<?, ?> selectedPdp = (LinkedHashMap<?, ?>) pdpGroupId; for (OnapPDPGroup pdpGroup : this.groups) { if (pdpGroup.getId().equals(selectedPdp.get("id"))) { selectedPdps.add(pdpGroup); } } } for (Object policyId : adapter.getPolicyDatas()) { LinkedHashMap<?, ?> selected = (LinkedHashMap<?, ?>) policyId; String policyName = selected.get("policyName").toString() + "." + selected.get("activeVersion").toString() + ".xml"; selectedPoliciesInUI.add(policyName); } for (Object pdpDestinationGroupId : selectedPdps) { Set<PDPPolicy> currentPoliciesInGroup = new HashSet<>(); Set<PDPPolicy> selectedPolicies = new HashSet<>(); for (String policyId : selectedPoliciesInUI) { logger.debug("Handlepolicies..." + pdpDestinationGroupId + policyId); assert policyId != null; String name = policyId.replace(File.separator, "."); String id = name; if (id.endsWith(".xml")) { id = id.replace(".xml", ""); id = id.substring(0, id.lastIndexOf('.')); } StdPDPPolicy selectedPolicy = null; String dbCheckName = name; 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[] split = dbCheckName.split(":"); String query = "FROM PolicyEntity where policyName = :split_1 and scope = :split_0"; SimpleBindings policyParams = new SimpleBindings(); policyParams.put("split_1", split[1]); policyParams.put("split_0", split[0]); List<Object> queryData = controller.getDataByQuery(query, policyParams); PolicyEntity policyEntity = (PolicyEntity) queryData.get(0); File temp = new File(name); BufferedWriter bw = new BufferedWriter(new FileWriter(temp)); bw.write(policyEntity.getPolicyData()); bw.close(); try { selectedPolicy = new StdPDPPolicy(name, true, id, temp.toURI()); } catch (IOException e) { logger.error("Unable to create policy '" + name + "': " + e.getMessage(), e); } StdPDPGroup selectedGroup = (StdPDPGroup) pdpDestinationGroupId; if (selectedPolicy != null) { for (OnapPDPGroup group : container.getGroups()) { if (group.getId().equals(selectedGroup.getId())) { currentPoliciesInGroup.addAll(group.getPolicies()); } } try { controller.getPapEngine().copyPolicy(selectedPolicy, (StdPDPGroup) pdpDestinationGroupId, userId); } catch (PAPException e) { logger.error("Exception Occured" + e); return null; } selectedPolicies.add(selectedPolicy); } temp.delete(); } StdPDPGroup pdpGroup = (StdPDPGroup) pdpDestinationGroupId; StdPDPGroup updatedGroupObject = new StdPDPGroup(pdpGroup.getId(), pdpGroup.isDefaultGroup(), pdpGroup.getName(), pdpGroup.getDescription(), pdpGroup.getDirectory()); updatedGroupObject.setOnapPdps(pdpGroup.getOnapPdps()); updatedGroupObject.setPipConfigs(pdpGroup.getPipConfigs()); updatedGroupObject.setStatus(pdpGroup.getStatus()); updatedGroupObject.setOperation("push"); Iterator<PDPPolicy> policyIterator = currentPoliciesInGroup.iterator(); logger.debug("policyIterator....." + selectedPolicies); while (policyIterator.hasNext()) { PDPPolicy existingPolicy = policyIterator.next(); for (PDPPolicy selPolicy : selectedPolicies) { if (selPolicy.getName().equals(existingPolicy.getName())) { if (selPolicy.getVersion().equals(existingPolicy.getVersion())) { if (selPolicy.getId().equals(existingPolicy.getId())) { policyIterator.remove(); logger.debug("Removing policy: " + selPolicy); break; } } else { policyIterator.remove(); logger.debug("Removing Old Policy version: " + selPolicy); break; } } } } currentPoliciesInGroup.addAll(selectedPolicies); updatedGroupObject.setPolicies(currentPoliciesInGroup); this.container.updateGroup(updatedGroupObject, userId); response.setContentType(PolicyUtils.APPLICATION_JSON); refreshGroups(); response.getWriter().write(new JSONObject( new JsonMessage(mapper.writeValueAsString(groups))).toString()); } } catch (Exception e) { logger.error(e); response.getWriter().write(PolicyUtils.CATCH_EXCEPTION); } return null; }  PolicyController getPolicyController(); void setPolicyController(PolicyController policyController); synchronized void refreshGroups(); @RequestMapping( value = {"/get_AutoPushPoliciesContainerData"}, method = {RequestMethod.GET}, produces = MediaType.APPLICATION_JSON_VALUE) void getPolicyGroupContainerData(HttpServletRequest request, HttpServletResponse response); @RequestMapping(value = {"/auto_Push/PushPolicyToPDP.htm"}, method = {RequestMethod.POST}) ModelAndView pushPolicyToPDPGroup(HttpServletRequest request, HttpServletResponse response); @SuppressWarnings("unchecked") @RequestMapping(value = {"/auto_Push/remove_GroupPolicies.htm"}, method = {RequestMethod.POST}) ModelAndView removePDPGroup(HttpServletRequest request, HttpServletResponse response);  }