+ private OnapPDPGroup newgroup;
+ private static Logger logger = FlexLogger.getLogger(DeleteHandler.class);
+ public static final String POLICY_IN_PDP = "PolicyInPDP";
+ public static final String ERROR = "error";
+ public static final String UNKNOWN = "unknown";
+ private static final String REGEX = "[0-9a-zA-Z._]*";
+
+ public void doAPIDeleteFromPAP(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException {
+ // get the request content into a String
+ String json = null;
+ java.util.Scanner scanner = new java.util.Scanner(request.getInputStream());
+ scanner.useDelimiter("\\A");
+ json = scanner.hasNext() ? scanner.next() : "";
+ scanner.close();
+ PolicyLogger.info("JSON request from API to Delete Policy from the PAP: " + json);
+ // convert Object sent as JSON into local object
+ StdPAPPolicy policy = PolicyUtils.jsonStringToObject(json, StdPAPPolicy.class);
+ String policyName = policy.getPolicyName();
+ Boolean policyVersionDeleted = false;
+ String removeXMLExtension;
+ int currentVersion;
+ String removeVersionExtension;
+ String splitPolicyName = null;
+ String[] split = null;
+ String status = ERROR;
+ PolicyEntity policyEntity = null;
+ JPAUtils jpaUtils = null;
+
+ String papDbDriver = XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_DB_DRIVER);
+ String papDbUrl = XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_DB_URL);
+ String papDbUser = XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_DB_USER);
+ String papDbPassword = XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_DB_PASSWORD);
+ Connection con = null;
+
+ try {
+ jpaUtils = JPAUtils.getJPAUtilsInstance(XACMLPapServlet.getEmf());
+ } catch (Exception e) {
+ PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "XACMLPapServlet", " Could not create JPAUtils instance on the PAP");
+ response.addHeader(ERROR, "jpautils");
+ response.addHeader("operation", "delete");
+ response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
+ return;
+ }
+ if (jpaUtils.dbLockdownIgnoreErrors()) {
+ PolicyLogger.warn("Policies are locked down");
+ response.addHeader("operation", "delete");
+ response.addHeader("lockdown", "true");
+ response.setStatus(HttpServletResponse.SC_ACCEPTED);
+ return;
+ }
+ EntityManager em = XACMLPapServlet.getEmf().createEntityManager();
+ Query policyEntityQuery = null;
+ try{
+ if(policyName.endsWith(".xml")){
+ removeXMLExtension = policyName.replace(".xml", "");
+ currentVersion = Integer.parseInt(removeXMLExtension.substring(removeXMLExtension.lastIndexOf('.')+1));
+ removeVersionExtension = removeXMLExtension.substring(0, removeXMLExtension.lastIndexOf('.'));
+ boolean queryCheck = true;
+ if(policy.getDeleteCondition().equalsIgnoreCase("All Versions")){
+ if(policyName.contains("Config_")){
+ splitPolicyName = removeVersionExtension.replace(".Config_", ":Config_");
+ }else if(policyName.contains("Action_")){
+ splitPolicyName = removeVersionExtension.replace(".Action_", ":Action_");
+ }else if(policyName.contains("Decision_")){
+ splitPolicyName = removeVersionExtension.replace(".Decision_", ":Decision_");
+ }
+ 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_")){
+ splitPolicyName = policyName.replace(".Config_", ":Config_");
+ }else if(policyName.contains("Action_")){
+ splitPolicyName = policyName.replace(".Action_", ":Action_");
+ }else if(policyName.contains("Decision_")){
+ splitPolicyName = policyName.replace(".Decision_", ":Decision_");
+ }
+ split = splitPolicyName.split(":");
+ queryCheck = false;
+ policyEntityQuery = em.createQuery("SELECT p FROM PolicyEntity p WHERE p.policyName=:pName and p.scope=:pScope");
+ }
+
+ if(queryCheck){
+ policyEntityQuery.setParameter("pName", "%"+split[1]+"%");
+ }else{
+ policyEntityQuery.setParameter("pName", split[1]);
+ }
+
+ policyEntityQuery.setParameter("pScope", split[0]);
+ List<?> peResult = policyEntityQuery.getResultList();
+ if(!peResult.isEmpty()){
+ Query getPolicyVersion = em.createQuery("Select p from PolicyVersion p where p.policyName=:pname");
+ getPolicyVersion.setParameter("pname", removeVersionExtension.replace(".", File.separator));
+ List<?> pvResult = getPolicyVersion.getResultList();
+ PolicyVersion pVersion = (PolicyVersion) pvResult.get(0);
+ int newVersion = 0;
+ em.getTransaction().begin();
+ Class.forName(papDbDriver);
+ con = DriverManager.getConnection(papDbUrl,papDbUser,papDbPassword);
+
+ if(policy.getDeleteCondition().equalsIgnoreCase("All Versions")){
+ boolean groupCheck = checkPolicyGroupEntity(con, peResult);
+ if(!groupCheck){
+ for(Object peData : peResult){
+ policyEntity = (PolicyEntity) peData;
+ status = deletePolicyEntityData(em, policyEntity);
+ }
+ }else{
+ status = POLICY_IN_PDP;
+ }
+ if(status.equals(ERROR)){
+ 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(status.equals(POLICY_IN_PDP)){
+ 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{
+ try{
+ policyVersionDeleted = true;
+ em.remove(pVersion);
+ }catch(Exception e){
+ logger.error(e.getMessage(),e);
+ policyVersionDeleted = false;
+ }
+ }
+ }else if(policy.getDeleteCondition().equalsIgnoreCase("Current Version")){
+ boolean groupCheck = checkPolicyGroupEntity(con, peResult);
+ if(!groupCheck){
+ policyEntity = (PolicyEntity) peResult.get(0);
+ status = deletePolicyEntityData(em, policyEntity);
+ }else{
+ status = POLICY_IN_PDP;
+ }
+
+ 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");
+ return;
+ } 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;
+ }
+ }
+
+ 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);