Consolidate PolicyRestAdapter setup
[policy/engine.git] / ONAP-PAP-REST / src / main / java / org / onap / policy / pap / xacml / rest / components / PolicyDBDao.java
index ba80571..25c9003 100644 (file)
@@ -24,6 +24,7 @@ package org.onap.policy.pap.xacml.rest.components;
 import com.att.research.xacml.api.pap.PAPException;
 import com.att.research.xacml.api.pap.PDPPolicy;
 import com.att.research.xacml.util.XACMLProperties;
+
 import java.io.ByteArrayInputStream;
 import java.io.InputStream;
 import java.net.URI;
@@ -36,7 +37,9 @@ import java.util.HashSet;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Set;
+
 import javax.persistence.PersistenceException;
+
 import org.apache.commons.io.FilenameUtils;
 import org.hibernate.Criteria;
 import org.hibernate.LockMode;
@@ -58,7 +61,7 @@ import org.onap.policy.rest.jpa.GroupEntity;
 import org.onap.policy.rest.jpa.PdpEntity;
 import org.onap.policy.rest.jpa.PolicyDBDaoEntity;
 import org.onap.policy.rest.jpa.PolicyEntity;
-import org.onap.policy.utils.CryptoUtils;
+import org.onap.policy.utils.PeCryptoUtils;
 import org.onap.policy.xacml.api.XACMLErrorConstants;
 import org.onap.policy.xacml.api.pap.OnapPDP;
 import org.onap.policy.xacml.api.pap.OnapPDPGroup;
@@ -71,23 +74,6 @@ import org.springframework.stereotype.Component;
 @Component
 public class PolicyDBDao {
     private static final Logger logger = FlexLogger.getLogger(PolicyDBDao.class);
-    private List<?> otherServers;
-
-    public List<?> getOtherServers() {
-        return otherServers;
-    }
-
-    public void setOtherServers(List<?> otherServers) {
-        this.otherServers = otherServers;
-    }
-
-    private static PolicyDBDao currentInstance = null;
-    private PAPPolicyEngine papEngine;
-
-    public PAPPolicyEngine getPapEngine() {
-        return papEngine;
-    }
-
     public static final String JSON_CONFIG = "JSON";
     public static final String XML_CONFIG = "XML";
     public static final String PROPERTIES_CONFIG = "PROPERTIES";
@@ -114,22 +100,18 @@ public class PolicyDBDao {
     public static final String POLICYDBDAO_VAR = "PolicyDBDao";
     public static final String DUP_POLICYID = "Somehow, more than one policy with the id ";
     public static final String FOUND_IN_DB = " were found in the database";
-
-
-    public static boolean isJunit = false;
-
-    public static void setJunit(boolean isJunit) {
-        PolicyDBDao.isJunit = isJunit;
-    }
-
+    private static PolicyDBDao currentInstance = null;
+    private static boolean isJunit = false;
     private static SessionFactory sessionfactory;
+    private List<?> otherServers;
+    private PAPPolicyEngine papEngine;
 
     /**
      * Gets the current instance of PolicyDBDao.
      *
      * @return The instance of PolicyDBDao or throws exception if the given instance is null.
-     * @throws IllegalStateException if a PolicyDBDao instance is null. Call createPolicyDBDaoInstance
-     *         (EntityManagerFactory emf) to get this.
+     * @throws IllegalStateException if a PolicyDBDao instance is null. Call
+     *         createPolicyDBDaoInstance (EntityManagerFactory emf) to get this.
      */
     public static PolicyDBDao getPolicyDBDaoInstance() {
         logger.debug("getPolicyDBDaoInstance() as getPolicyDBDaoInstance() called");
@@ -173,7 +155,7 @@ public class PolicyDBDao {
     // not static because we are going to be using the instance's emf
     // waitTime in ms to wait for lock, or -1 to wait forever (no)
     @SuppressWarnings("deprecation")
-    public void startTransactionSynced(Session session, int waitTime) throws InterruptedException {
+    public void startTransactionSynced(Session session, int waitTime) {
         logger.debug("\n\nstartTransactionSynced(Hibernate Session,int waitTime) as " + "\n   startTransactionSynced("
                 + session + "," + waitTime + ") called\n\n");
         DatabaseLockEntity lock = null;
@@ -289,7 +271,8 @@ public class PolicyDBDao {
             }
         }
         if (urlUserPass[2] == null || "".equals(urlUserPass[2])) {
-            String passwordPropertyValue = XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_PASS);
+            String passwordPropertyValue =
+                    PeCryptoUtils.decrypt(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_PASS));
             if (passwordPropertyValue != null) {
                 urlUserPass[2] = passwordPropertyValue;
             }
@@ -314,7 +297,7 @@ public class PolicyDBDao {
         Session session = sessionfactory.openSession();
         try {
             startTransactionSynced(session, 1000);
-        } catch (InterruptedException | IllegalStateException e) {
+        } catch (IllegalStateException e) {
             logger.debug("\nPolicyDBDao.register() caught an IllegalStateException: \n" + e + "\n");
             DatabaseLockEntity lock;
             lock = (DatabaseLockEntity) session.get(DatabaseLockEntity.class, 1);
@@ -351,13 +334,7 @@ public class PolicyDBDao {
         }
 
         // encrypt the password
-        String txt = null;
-        try {
-            txt = CryptoUtils.encryptTxt(url[2].getBytes(StandardCharsets.UTF_8));
-        } catch (Exception e) {
-            logger.debug(e);
-            PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, POLICYDBDAO_VAR, "Could not encrypt PAP password");
-        }
+        String txt = PeCryptoUtils.encrypt(url[2]);
         if (foundPolicyDBDaoEntity == null) {
             PolicyDBDaoEntity newPolicyDBDaoEntity = new PolicyDBDaoEntity();
             newPolicyDBDaoEntity.setPolicyDBDaoUrl(url[0]);
@@ -453,8 +430,8 @@ public class PolicyDBDao {
                     Files.delete(Paths.get(currentPolicyMap.get(id).getLocation()));
                 } catch (Exception e) {
                     logger.debug(e);
-                    PolicyLogger
-                            .error("PolicyDBDao: Exception occurred while attempting to delete the old version of the policy file from the group. "
+                    PolicyLogger.error(
+                            "PolicyDBDao: Exception occurred while attempting to delete the old version of the policy file from the group. "
                                     + e.getMessage());
                 }
             }
@@ -583,10 +560,11 @@ public class PolicyDBDao {
                 try {
                     if (CONFIG.equalsIgnoreCase(type)) {
                         configName = ((ConfigurationDataEntity) configData).getConfigurationName();
-                        configBody = (((ConfigurationDataEntity) configData).getConfigBody() != null)
-                                ? ((ConfigurationDataEntity) configData).getConfigBody()
-                                        .getBytes(StandardCharsets.UTF_8)
-                                : "".getBytes();
+                        configBody =
+                                (((ConfigurationDataEntity) configData).getConfigBody() != null)
+                                        ? ((ConfigurationDataEntity) configData).getConfigBody()
+                                                .getBytes(StandardCharsets.UTF_8)
+                                        : "".getBytes();
                     } else {
                         configName = ((ActionBodyEntity) configData).getActionBodyName();
                         configBody = (((ActionBodyEntity) configData).getActionBody() != null)
@@ -797,6 +775,26 @@ public class PolicyDBDao {
         return false;
     }
 
+    public List<?> getOtherServers() {
+        return otherServers;
+    }
+
+    public void setOtherServers(List<?> otherServers) {
+        this.otherServers = otherServers;
+    }
+
+    public PAPPolicyEngine getPapEngine() {
+        return papEngine;
+    }
+
+    public static boolean isJunit() {
+        return isJunit;
+    }
+
+    public static void setJunit(boolean isJunit) {
+        PolicyDBDao.isJunit = isJunit;
+    }
+
     public static PolicyDBDaoTestClass getPolicyDBDaoTestClass() {
         return new PolicyDBDao().new PolicyDBDaoTestClass();
     }