X-Git-Url: https://gerrit.onap.org/r/gitweb?p=policy%2Fengine.git;a=blobdiff_plain;f=ONAP-PAP-REST%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fpolicy%2Fpap%2Fxacml%2Frest%2Fcomponents%2FPolicyDbDao.java;fp=ONAP-PAP-REST%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fpolicy%2Fpap%2Fxacml%2Frest%2Fcomponents%2FPolicyDBDao.java;h=e0db53e8b860dbb203853296e7b4808af4ea56f8;hp=87e684380f213de0af2dec248c07722787989614;hb=5a5842e77e4e0f0b6207192b374d99753db26c7e;hpb=fdfd5a00ed2805fe6138dbd0a78840c7210e8098 diff --git a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/PolicyDBDao.java b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/PolicyDbDao.java similarity index 80% rename from ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/PolicyDBDao.java rename to ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/PolicyDbDao.java index 87e684380..e0db53e8b 100644 --- a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/PolicyDBDao.java +++ b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/PolicyDbDao.java @@ -53,7 +53,7 @@ import org.onap.policy.common.logging.flexlogger.FlexLogger; import org.onap.policy.common.logging.flexlogger.Logger; import org.onap.policy.rest.XacmlRestProperties; import org.onap.policy.rest.adapter.PolicyRestAdapter; -import org.onap.policy.rest.dao.PolicyDBException; +import org.onap.policy.rest.dao.PolicyDbException; import org.onap.policy.rest.jpa.ActionBodyEntity; import org.onap.policy.rest.jpa.ConfigurationDataEntity; import org.onap.policy.rest.jpa.DatabaseLockEntity; @@ -71,9 +71,12 @@ import org.onap.policy.xacml.std.pap.StdPDPPolicy; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +/** + * The Class PolicyDbDao. + */ @Component -public class PolicyDBDao { - private static final Logger logger = FlexLogger.getLogger(PolicyDBDao.class); +public class PolicyDbDao { + private static final Logger logger = FlexLogger.getLogger(PolicyDbDao.class); public static final String JSON_CONFIG = "JSON"; public static final String XML_CONFIG = "XML"; public static final String PROPERTIES_CONFIG = "PROPERTIES"; @@ -100,7 +103,10 @@ 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"; - private static PolicyDBDao currentInstance = null; + + private static final String AUDIT_STR = "Audit"; + + private static PolicyDbDao currentInstance = null; private static boolean isJunit = false; private static SessionFactory sessionfactory; private List> otherServers; @@ -110,40 +116,58 @@ public class PolicyDBDao { * 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() { + public static PolicyDbDao getPolicyDbDaoInstance() { logger.debug("getPolicyDBDaoInstance() as getPolicyDBDaoInstance() called"); if (currentInstance != null) { return currentInstance; } else { - currentInstance = new PolicyDBDao("init"); + currentInstance = new PolicyDbDao("init"); } return currentInstance; } + /** + * Sets the pap engine. + * + * @param papEngine2 the new pap engine + */ public void setPapEngine(PAPPolicyEngine papEngine2) { this.papEngine = papEngine2; } + /** + * Instantiates a new policy db dao. + * + * @param sessionFactory the session factory + */ @Autowired - public PolicyDBDao(SessionFactory sessionFactory) { - PolicyDBDao.sessionfactory = sessionFactory; + public PolicyDbDao(SessionFactory sessionFactory) { + PolicyDbDao.sessionfactory = sessionFactory; } - public PolicyDBDao() { + /** + * Instantiates a new policy db dao. + */ + public PolicyDbDao() { // Default Constructor } - public PolicyDBDao(String init) { + /** + * Initialize the DAO. + * + * @param init initiation parameters + */ + public PolicyDbDao(String init) { // not needed in this release if (!register()) { PolicyLogger .error("This server's PolicyDBDao instance could not be registered and may not reveive updates"); } - otherServers = getRemotePolicyDBDaoList(); + otherServers = getRemotePolicyDbDaoList(); if (logger.isDebugEnabled()) { logger.debug("Number of remote PolicyDBDao instances: " + otherServers.size()); } @@ -152,8 +176,15 @@ 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) + /** + * Start a synchronized transaction. + * + *
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) + * + * @param session the session + * @param waitTime the wait time + */ @SuppressWarnings("deprecation") public void startTransactionSynced(Session session, int waitTime) { logger.debug("\n\nstartTransactionSynced(Hibernate Session,int waitTime) as " + "\n startTransactionSynced(" @@ -179,17 +210,17 @@ public class PolicyDBDao { } /** - * Gets the list of other registered PolicyDBDaos from the database + * Gets the list of other registered PolicyDBDaos from the database. * * @return List (type PolicyDBDaoEntity) of other PolicyDBDaos */ - private List> getRemotePolicyDBDaoList() { + private List> getRemotePolicyDbDaoList() { logger.debug("getRemotePolicyDBDaoList() as getRemotePolicyDBDaoList() called"); - List> policyDBDaoEntityList = new LinkedList<>(); + List> policyDbDaoEntityList = new LinkedList<>(); Session session = sessionfactory.openSession(); try { Criteria cr = session.createCriteria(PolicyDBDaoEntity.class); - policyDBDaoEntityList = cr.list(); + policyDbDaoEntityList = cr.list(); } catch (Exception e) { PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, POLICYDBDAO_VAR, "Exception querying for other registered PolicyDBDaos"); @@ -201,21 +232,29 @@ public class PolicyDBDao { logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement" + e); } } - return policyDBDaoEntityList; + return policyDbDaoEntityList; } - public PolicyDBDaoTransaction getNewTransaction() { + /** + * Gets the new transaction. + * + * @return the new transaction + */ + public PolicyDbDaoTransaction getNewTransaction() { logger.debug("getNewTransaction() as getNewTransaction() called"); return new PolicyDbDaoTransactionInstance("init"); } - /* - * Because the normal transactions are not used in audits, we can use the same transaction - * mechanism to get a transaction and obtain the emlock and the DB lock. We just need to provide - * different transaction timeout values in ms because the audit will run longer than normal - * transactions. + /** + * Get a new audit transaction. + * + *
Because the normal transactions are not used in audits, we can use the same transaction mechanism to get a + * transaction and obtain the emlock and the DB lock. We just need to provide different transaction timeout values + * in ms because the audit will run longer than normal transactions. + * + * @return the new audit transaction */ - public PolicyDBDaoTransaction getNewAuditTransaction() { + public PolicyDbDaoTransaction getNewAuditTransaction() { logger.debug("getNewAuditTransaction() as getNewAuditTransaction() called"); // Use the standard transaction wait time in ms int auditWaitMs = Integer.parseInt(XACMLProperties.getProperty(XacmlRestProperties.PROP_PAP_TRANS_WAIT)); @@ -229,6 +268,7 @@ public class PolicyDBDao { * * @param one A String or null to compare * @param two A String or null to compare + * @return true, if successful */ public static boolean stringEquals(String one, String two) { logger.debug("stringEquals(String one, String two) as stringEquals(" + one + ", " + two + ") called"); @@ -242,8 +282,7 @@ public class PolicyDBDao { } /** - * Returns the url of this local pap server, removing the username and password, if they are - * present + * Returns the url of this local pap server, removing the username and password, if they are present. * * @return The url of this local pap server */ @@ -256,6 +295,12 @@ public class PolicyDBDao { return splitPapUrlUserPass(url); } + /** + * Split the user and password of a PAP URL. + * + * @param url the URL + * @return the user and password + */ public String[] splitPapUrlUserPass(String url) { String[] urlUserPass = new String[3]; String[] commaSplit = url.split(","); @@ -283,7 +328,7 @@ public class PolicyDBDao { } /** - * Register the PolicyDBDao instance in the PolicyDBDaoEntity table + * Register the PolicyDBDao instance in the PolicyDBDaoEntity table. * * @return Boolean, were we able to register? */ @@ -325,24 +370,24 @@ public class PolicyDBDao { } } logger.debug("\nPolicyDBDao.register. Database locking and concurrency control is initialized\n"); - PolicyDBDaoEntity foundPolicyDBDaoEntity = null; + PolicyDBDaoEntity foundPolicyDbDaoEntity = null; Criteria cr = session.createCriteria(PolicyDBDaoEntity.class); cr.add(Restrictions.eq("policyDBDaoUrl", url[0])); List> data = cr.list(); if (!data.isEmpty()) { - foundPolicyDBDaoEntity = (PolicyDBDaoEntity) data.get(0); + foundPolicyDbDaoEntity = (PolicyDBDaoEntity) data.get(0); } // encrypt the password String txt = PeCryptoUtils.encrypt(url[2]); - if (foundPolicyDBDaoEntity == null) { - PolicyDBDaoEntity newPolicyDBDaoEntity = new PolicyDBDaoEntity(); - newPolicyDBDaoEntity.setPolicyDBDaoUrl(url[0]); - newPolicyDBDaoEntity.setDescription("PAP server at " + url[0]); - newPolicyDBDaoEntity.setUsername(url[1]); - newPolicyDBDaoEntity.setPassword(txt); + if (foundPolicyDbDaoEntity == null) { + PolicyDBDaoEntity newPolicyDbDaoEntity = new PolicyDBDaoEntity(); + newPolicyDbDaoEntity.setPolicyDBDaoUrl(url[0]); + newPolicyDbDaoEntity.setDescription("PAP server at " + url[0]); + newPolicyDbDaoEntity.setUsername(url[1]); + newPolicyDbDaoEntity.setPassword(txt); try { - session.persist(newPolicyDBDaoEntity); + session.persist(newPolicyDbDaoEntity); session.getTransaction().commit(); } catch (Exception e) { logger.debug(e); @@ -356,13 +401,13 @@ public class PolicyDBDao { } } else { // just want to update in order to change modified date - if (url[1] != null && !stringEquals(url[1], foundPolicyDBDaoEntity.getUsername())) { - foundPolicyDBDaoEntity.setUsername(url[1]); + if (url[1] != null && !stringEquals(url[1], foundPolicyDbDaoEntity.getUsername())) { + foundPolicyDbDaoEntity.setUsername(url[1]); } - if (txt != null && !stringEquals(txt, foundPolicyDBDaoEntity.getPassword())) { - foundPolicyDBDaoEntity.setPassword(txt); + if (txt != null && !stringEquals(txt, foundPolicyDbDaoEntity.getPassword())) { + foundPolicyDbDaoEntity.setPassword(txt); } - foundPolicyDBDaoEntity.preUpdate(); + foundPolicyDbDaoEntity.preUpdate(); try { session.getTransaction().commit(); } catch (Exception e) { @@ -381,12 +426,20 @@ public class PolicyDBDao { return true; } - /* - * This method is called during all pushPolicy transactions and makes sure the file system group - * is in sync with the database groupentity + /** + * Synchronize group policies in the fuile system. + * + *
This method is called during all pushPolicy transactions and makes sure the file system group is in sync with
+ * the database group entity
+ *
+ * @param pdpGroup the pdp group
+ * @param groupentity the groupentity
+ * @return the std PDP group
+ * @throws PAPException the PAP exception
+ * @throws PolicyDbException the policy db exception
*/
public StdPDPGroup synchronizeGroupPoliciesInFileSystem(StdPDPGroup pdpGroup, GroupEntity groupentity)
- throws PAPException, PolicyDBException {
+ throws PAPException, PolicyDbException {
HashMap