- private String deletePolicyFromPDPGroup (OnapPDPGroup group, ONAPLoggingContext loggingContext){
- PolicyDBDaoTransaction acPutTransaction = XACMLPapServlet.getDbDaoTransaction();
- String response = null;
- loggingContext.setServiceName("API:PAP.DeleteHandler");
- OnapPDPGroup existingGroup = null;
- try {
- existingGroup = XACMLPapServlet.getPAPEngine().getGroup(group.getId());
- } catch (PAPException e1) {
- PolicyLogger.error("Exception occured While Deleting Policy From PDP Group"+e1);
- }
- if (!(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";
- return response;
- }
- // The Path on the PAP side is not carried on the RESTful interface with the AC
- // (because it is local to the PAP)
- // so we need to fill that in before submitting the group for update
- ((StdPDPGroup)group).setDirectory(((StdPDPGroup)existingGroup).getDirectory());
- try{
- 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());
- response = "DB Error";
- return response;
- }
- try {
- XACMLPapServlet.getPAPEngine().updateGroup(group);
- } catch (PAPException e) {
- PolicyLogger.error("Exception occured While Updating PDP Groups"+e);
- response = "error in updateGroup method";
- }
- PolicyLogger.debug("Group '" + group.getId() + "' updated");
- acPutTransaction.commitTransaction();
- // Group changed, which might include changing the policies
- try {
- newgroup = existingGroup;
- } catch (Exception e) {
- PolicyLogger.error("Exception occured in Group Change Method"+e);
- response = "error in groupChanged method";
- }
- if (response==null){
- response = "success";
- loggingContext.transactionEnded();
- PolicyLogger.audit("Policy successfully deleted!");
- }
- loggingContext.transactionEnded();
- PolicyLogger.audit("Transaction Ended");
- return response;
- }
-
- public OnapPDPGroup getDeletedGroup(){
- return newgroup;
- }
-
- public boolean preSafetyCheck(PDPPolicy policy) {
- return true;
- }
-
- public static DeleteHandler getInstance() {
- try {
- Class<?> deleteHandler = Class.forName(XACMLProperties.getProperty("deletePolicy.impl.className", DeleteHandler.class.getName()));
- return (DeleteHandler) deleteHandler.newInstance();
- } catch (Exception e) {
- logger.error(e.getMessage(),e);
- }
- return null;
- }
+ if (ERROR.equals(status)) {
+ PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE +
+ "Exception Occured while deleting the Entity from Database.");
+ response.addHeader(ERROR, UNKNOWN);
+ response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
+ return;
+ } 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, POLICY_IN_PDP);
+ response.setStatus(HttpServletResponse.SC_CONFLICT);
+ return;
+ } else {
+ if (currentVersion > 1) {
+ if (!peResult.isEmpty()) {
+ for (Object object : peResult) {
+ policyEntity = (PolicyEntity) object;
+ String policyEntityName = policyEntity.getPolicyName().replace(".xml", "");
+ int policyEntityVersion = Integer.parseInt(
+ policyEntityName.substring(policyEntityName.lastIndexOf('.') + 1));
+ if (policyEntityVersion > newVersion) {
+ newVersion = policyEntityVersion - 1;
+ }
+ }
+ }
+ pVersion.setActiveVersion(newVersion);
+ pVersion.setHigherVersion(newVersion);
+ try {
+ policyVersionDeleted = true;
+ em.persist(pVersion);
+ } catch (Exception e) {
+ logger.error(e.getMessage(), e);
+ policyVersionDeleted = false;
+ }
+ } else {
+ try {
+ policyVersionDeleted = true;
+ em.remove(pVersion);
+ } catch (Exception e) {
+ logger.error(e.getMessage(), e);
+ policyVersionDeleted = false;
+ }
+ }
+ }
+ }
+ } 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.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
+ return;
+ }
+ }
+ em.getTransaction().commit();
+ } catch (Exception e) {
+ em.getTransaction().rollback();
+ PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "XACMLPapServlet", " ERROR");
+ response.addHeader(ERROR, "deleteDB");
+ response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
+ return;
+ } finally {
+ em.close();
+ if (con != null) {
+ con.close();
+ }
+ }
+
+ if (policyVersionDeleted) {
+ response.setStatus(HttpServletResponse.SC_OK);
+ response.addHeader("successMapKey", "success");
+ response.addHeader("operation", "delete");
+ } 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.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
+ }
+ }
+
+ public static String deletePolicyEntityData(EntityManager em, PolicyEntity policyEntity) {
+ PolicyElasticSearchController controller = new PolicyElasticSearchController();
+ PolicyRestAdapter policyData = new PolicyRestAdapter();
+ String policyName = policyEntity.getPolicyName();
+ try {
+ if (policyName.contains("Config_")) {
+ em.remove(policyEntity.getConfigurationData());
+ } else if (policyName.contains("Action_")) {
+ em.remove(policyEntity.getActionBodyEntity());
+ }
+ String searchPolicyName = policyEntity.getScope() + "." + policyEntity.getPolicyName();
+ policyData.setNewFileName(searchPolicyName);
+ controller.deleteElk(policyData);
+ em.remove(policyEntity);
+ } catch (Exception e) {
+ logger.error(e.getMessage(), e);
+ return ERROR;
+ }
+ return "success";
+ }
+
+ public static boolean checkPolicyGroupEntity(Connection con, List<?> peResult) throws SQLException {
+ for (Object peData : peResult) {
+ PolicyEntity policyEntity = (PolicyEntity) peData;
+ try (Statement st = con.createStatement();
+ ResultSet rs = st.executeQuery(
+ "Select * from PolicyGroupEntity where policyid = '" + policyEntity.getPolicyId() + "'")) {
+ boolean gEntityList = rs.next();
+ if (gEntityList) {
+ return true;
+ }
+ }
+ }
+ 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;
+
+ if (groupId != null && !groupId.matches(REGEX)) {
+ response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
+ response.addHeader("error", ERROR);
+ response.addHeader("message", "Group Id is not valid");
+ return;
+ }
+
+ 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 {
+ group = XACMLPapServlet.getPAPEngine().getGroup(groupId);
+ } catch (PAPException e) {
+ PolicyLogger.error("Exception occured While PUT operation is performing for PDP Group" + e);
+ }
+ if (group == null) {
+ 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.addHeader("message", message);
+ response.setStatus(HttpServletResponse.SC_NOT_FOUND);
+ return;
+ } else {
+ loggingContext.setServiceName("API:PAP.deletPolicyFromPDPGroup");
+ if (policyName.contains("xml")) {
+ PolicyLogger
+ .debug("The full file name including the extension was provided for policyName.. continue.");
+ } else {
+ String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Invalid policyName... "
+ + "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.setStatus(HttpServletResponse.SC_BAD_REQUEST);
+ return;
+ }
+ RemoveGroupPolicy removePolicy = new RemoveGroupPolicy((StdPDPGroup) group);
+ PDPPolicy policy = group.getPolicy(policyName);
+ if (policy != null) {
+
+ if ((policy.getId().contains("Config_MS_")) || (policy.getId().contains("BRMS_Param"))) {
+ if (preSafetyCheck(policy)) {
+ 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);
+ } else {
+ 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.setStatus(HttpServletResponse.SC_BAD_REQUEST);
+ return;
+ }
+ }
+ switch (responseString) {
+ case "success":
+ loggingContext.transactionEnded();
+ PolicyLogger.info("Policy successfully deleted!");
+ PolicyLogger.audit("Policy successfully deleted!");
+ response.setStatus(HttpServletResponse.SC_OK);
+ response.addHeader("successMapKey", "success");
+ response.addHeader("operation", "delete");
+ break;
+ case "No Group":
+ 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("message", message);
+ break;
+ case "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");
+ break;
+ default:
+ 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.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
+ break;
+ }
+ }
+
+ private String deletePolicyFromPDPGroup(OnapPDPGroup group, ONAPLoggingContext loggingContext) {
+ PolicyDBDaoTransaction acPutTransaction = XACMLPapServlet.getDbDaoTransaction();
+ String response = null;
+ loggingContext.setServiceName("API:PAP.DeleteHandler");
+ OnapPDPGroup existingGroup = null;
+ try {
+ existingGroup = XACMLPapServlet.getPAPEngine().getGroup(group.getId());
+ } catch (PAPException e1) {
+ PolicyLogger.error("Exception occured While Deleting Policy From PDP Group" + e1);
+ }
+ if (!(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";
+ return response;
+ }
+ // The Path on the PAP side is not carried on the RESTful interface with the AC
+ // (because it is local to the PAP)
+ // so we need to fill that in before submitting the group for update
+ ((StdPDPGroup) group).setDirectory(((StdPDPGroup) existingGroup).getDirectory());
+ try {
+ 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());
+ response = "DB Error";
+ return response;
+ }
+ try {
+ XACMLPapServlet.getPAPEngine().updateGroup(group);
+ } catch (PAPException e) {
+ PolicyLogger.error("Exception occured While Updating PDP Groups" + e);
+ response = "error in updateGroup method";
+ }
+ PolicyLogger.debug("Group '" + group.getId() + "' updated");
+ acPutTransaction.commitTransaction();
+ // Group changed, which might include changing the policies
+ try {
+ newgroup = existingGroup;
+ } catch (Exception e) {
+ PolicyLogger.error("Exception occured in Group Change Method" + e);
+ response = "error in groupChanged method";
+ }
+ if (response == null) {
+ response = "success";
+ loggingContext.transactionEnded();
+ PolicyLogger.audit("Policy successfully deleted!");
+ }
+ loggingContext.transactionEnded();
+ PolicyLogger.audit("Transaction Ended");
+ return response;
+ }
+
+ public OnapPDPGroup getDeletedGroup() {
+ return newgroup;
+ }
+
+ public boolean preSafetyCheck(PDPPolicy policy) {
+ return true;
+ }
+
+ public static DeleteHandler getInstance() {
+ try {
+ Class<?> deleteHandler = Class.forName(
+ XACMLProperties.getProperty("deletePolicy.impl.className", DeleteHandler.class.getName()));
+ return (DeleteHandler) deleteHandler.newInstance();
+ } catch (Exception e) {
+ logger.error(e.getMessage(), e);
+ }
+ return null;
+ }