From f8a620d1ff2b0d33b08a22279058f3e0253bdde1 Mon Sep 17 00:00:00 2001 From: Guo Ruijing Date: Fri, 28 Jul 2017 08:21:14 +0000 Subject: [POLICY-71] replace openecomp for policy-common Change-Id: I3241f5d1f0234043b4dff718eda1ffdc48052276 Signed-off-by: Guo Ruijing Signed-off-by: Pamela Dragosh --- .../org/onap/policy/common/ia/AuditThread.java | 769 ++++++++++++++ .../java/org/onap/policy/common/ia/DbAudit.java | 462 ++++++++ .../onap/policy/common/ia/DbAuditException.java | 37 + .../main/java/org/onap/policy/common/ia/DbDAO.java | 739 +++++++++++++ .../common/ia/DbDaoTransactionException.java | 37 + .../org/onap/policy/common/ia/IntegrityAudit.java | 241 +++++ .../policy/common/ia/IntegrityAuditProperties.java | 54 + .../ia/IntegrityAuditPropertiesException.java | 37 + .../policy/common/ia/jpa/IntegrityAuditEntity.java | 203 ++++ .../openecomp/policy/common/ia/AuditThread.java | 769 -------------- .../org/openecomp/policy/common/ia/DbAudit.java | 462 -------- .../policy/common/ia/DbAuditException.java | 37 - .../java/org/openecomp/policy/common/ia/DbDAO.java | 739 ------------- .../common/ia/DbDaoTransactionException.java | 37 - .../openecomp/policy/common/ia/IntegrityAudit.java | 241 ----- .../policy/common/ia/IntegrityAuditProperties.java | 54 - .../ia/IntegrityAuditPropertiesException.java | 37 - .../policy/common/ia/jpa/IntegrityAuditEntity.java | 203 ---- .../src/main/resources/META-INF/persistence.xml | 6 +- integrity-audit/src/main/resources/logback.xml | 2 +- .../policy/common/ia/test/AuditPeriodTest.java | 480 +++++++++ .../common/ia/test/DbAuditCompareEntriesTest.java | 617 +++++++++++ .../onap/policy/common/ia/test/DbAuditTest.java | 753 +++++++++++++ .../org/onap/policy/common/ia/test/DbDAOTest.java | 735 +++++++++++++ .../ia/test/IntegrityAuditDesignationTest.java | 1109 ++++++++++++++++++++ .../policy/common/ia/test/jpa/IaTestEntity.java | 157 +++ .../onap/policy/common/ia/test/jpa/PersonTest.java | 64 ++ .../policy/common/ia/test/AuditPeriodTest.java | 480 --------- .../common/ia/test/DbAuditCompareEntriesTest.java | 617 ----------- .../policy/common/ia/test/DbAuditTest.java | 753 ------------- .../openecomp/policy/common/ia/test/DbDAOTest.java | 735 ------------- .../ia/test/IntegrityAuditDesignationTest.java | 1109 -------------------- .../policy/common/ia/test/jpa/IaTestEntity.java | 157 --- .../policy/common/ia/test/jpa/PersonTest.java | 64 -- integrity-audit/src/test/resources/logback.xml | 2 +- 35 files changed, 6499 insertions(+), 6499 deletions(-) create mode 100644 integrity-audit/src/main/java/org/onap/policy/common/ia/AuditThread.java create mode 100644 integrity-audit/src/main/java/org/onap/policy/common/ia/DbAudit.java create mode 100644 integrity-audit/src/main/java/org/onap/policy/common/ia/DbAuditException.java create mode 100644 integrity-audit/src/main/java/org/onap/policy/common/ia/DbDAO.java create mode 100644 integrity-audit/src/main/java/org/onap/policy/common/ia/DbDaoTransactionException.java create mode 100644 integrity-audit/src/main/java/org/onap/policy/common/ia/IntegrityAudit.java create mode 100644 integrity-audit/src/main/java/org/onap/policy/common/ia/IntegrityAuditProperties.java create mode 100644 integrity-audit/src/main/java/org/onap/policy/common/ia/IntegrityAuditPropertiesException.java create mode 100644 integrity-audit/src/main/java/org/onap/policy/common/ia/jpa/IntegrityAuditEntity.java delete mode 100644 integrity-audit/src/main/java/org/openecomp/policy/common/ia/AuditThread.java delete mode 100644 integrity-audit/src/main/java/org/openecomp/policy/common/ia/DbAudit.java delete mode 100644 integrity-audit/src/main/java/org/openecomp/policy/common/ia/DbAuditException.java delete mode 100644 integrity-audit/src/main/java/org/openecomp/policy/common/ia/DbDAO.java delete mode 100644 integrity-audit/src/main/java/org/openecomp/policy/common/ia/DbDaoTransactionException.java delete mode 100644 integrity-audit/src/main/java/org/openecomp/policy/common/ia/IntegrityAudit.java delete mode 100644 integrity-audit/src/main/java/org/openecomp/policy/common/ia/IntegrityAuditProperties.java delete mode 100644 integrity-audit/src/main/java/org/openecomp/policy/common/ia/IntegrityAuditPropertiesException.java delete mode 100644 integrity-audit/src/main/java/org/openecomp/policy/common/ia/jpa/IntegrityAuditEntity.java create mode 100644 integrity-audit/src/test/java/org/onap/policy/common/ia/test/AuditPeriodTest.java create mode 100644 integrity-audit/src/test/java/org/onap/policy/common/ia/test/DbAuditCompareEntriesTest.java create mode 100644 integrity-audit/src/test/java/org/onap/policy/common/ia/test/DbAuditTest.java create mode 100644 integrity-audit/src/test/java/org/onap/policy/common/ia/test/DbDAOTest.java create mode 100644 integrity-audit/src/test/java/org/onap/policy/common/ia/test/IntegrityAuditDesignationTest.java create mode 100644 integrity-audit/src/test/java/org/onap/policy/common/ia/test/jpa/IaTestEntity.java create mode 100644 integrity-audit/src/test/java/org/onap/policy/common/ia/test/jpa/PersonTest.java delete mode 100644 integrity-audit/src/test/java/org/openecomp/policy/common/ia/test/AuditPeriodTest.java delete mode 100644 integrity-audit/src/test/java/org/openecomp/policy/common/ia/test/DbAuditCompareEntriesTest.java delete mode 100644 integrity-audit/src/test/java/org/openecomp/policy/common/ia/test/DbAuditTest.java delete mode 100644 integrity-audit/src/test/java/org/openecomp/policy/common/ia/test/DbDAOTest.java delete mode 100644 integrity-audit/src/test/java/org/openecomp/policy/common/ia/test/IntegrityAuditDesignationTest.java delete mode 100644 integrity-audit/src/test/java/org/openecomp/policy/common/ia/test/jpa/IaTestEntity.java delete mode 100644 integrity-audit/src/test/java/org/openecomp/policy/common/ia/test/jpa/PersonTest.java (limited to 'integrity-audit/src') diff --git a/integrity-audit/src/main/java/org/onap/policy/common/ia/AuditThread.java b/integrity-audit/src/main/java/org/onap/policy/common/ia/AuditThread.java new file mode 100644 index 00000000..e7e16a64 --- /dev/null +++ b/integrity-audit/src/main/java/org/onap/policy/common/ia/AuditThread.java @@ -0,0 +1,769 @@ +/*- + * ============LICENSE_START======================================================= + * Integrity Audit + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.common.ia; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.Date; +import java.util.List; +import java.util.Properties; +//import org.apache.log4j.Logger; + + +import org.onap.policy.common.ia.jpa.IntegrityAuditEntity; +import org.onap.policy.common.logging.eelf.MessageCodes; +import org.onap.policy.common.logging.flexlogger.FlexLogger; +import org.onap.policy.common.logging.flexlogger.Logger; + +/** + * AuditThread is the main thread for the IntegrityAudit + * + */ +public class AuditThread extends Thread { + + private static final Logger logger = FlexLogger.getLogger(AuditThread.class); + + /* + * Number of milliseconds that must elapse for audit to be considered + * complete. It's public for access by JUnit test logic. + */ + public static final long AUDIT_COMPLETION_INTERVAL = 30000; + + /* + * Number of iterations for audit simulation. + */ + public static final long AUDIT_SIMULATION_ITERATIONS = 3; + + /* + * Number of milliseconds to sleep between audit simulation iterations. It's + * public for access by JUnit test logic. + */ + public static final long AUDIT_SIMULATION_SLEEP_INTERVAL = 5000; + + /* + * Unless audit has already been run on this entity, number of milliseconds + * to sleep between audit thread iterations. If audit has already been run, + * we sleep integrityAuditPeriodMillis. + */ + private static final long AUDIT_THREAD_SLEEP_INTERVAL = 5000; + + /* + * DB access class. + */ + private DbDAO dbDAO; + + /* + * E.g. pdp_xacml + */ + private String nodeType; + + /* + * Persistence unit for which this audit is being run. + */ + private String persistenceUnit; + + /* + * Name of this resource + */ + private String resourceName; + + /* + * E.g. DB_DRIVER, SITE_NAME, NODE_TYPE + */ + private Properties properties; + + /* + * See IntegrityAudit class for usage. + */ + private int integrityAuditPeriodMillis; + + /* + * The containing IntegrityAudit instance + */ + private IntegrityAudit integrityAudit; + + /** + * AuditThread constructor + * @param resourceName + * @param persistenceUnit + * @param properties + * @param integrityAuditPeriodSeconds + * @param integrityAudit + * @throws Exception + */ + public AuditThread(String resourceName, String persistenceUnit, + Properties properties, int integrityAuditPeriodSeconds, IntegrityAudit integrityAudit) + throws Exception { + this.resourceName = resourceName; + this.persistenceUnit = persistenceUnit; + this.properties = properties; + this.integrityAuditPeriodMillis = integrityAuditPeriodSeconds * 1000; + this.integrityAudit = integrityAudit; + + /* + * The DbDAO Constructor registers this node in the IntegrityAuditEntity + * table. Each resource (node) inserts its own name, persistenceUnit, DB + * access properties and other pertinent properties in the table. This + * allows the audit on each node to compare its own version of the + * entities for the persistenceUnit in question with the versions from + * all other nodes of similar type. + */ + dbDAO = new DbDAO(this.resourceName, this.persistenceUnit, + this.properties); + this.nodeType = properties.getProperty(IntegrityAuditProperties.NODE_TYPE); + + } + + public void run() { + + logger.info("AuditThread.run: Entering"); + + try { + + /* + * Triggers change in designation, unless no other viable candidate. + */ + boolean auditCompleted = false; + + DbAudit dbAudit = new DbAudit(dbDAO); + + IntegrityAuditEntity entityCurrentlyDesignated = null; + IntegrityAuditEntity thisEntity = null; + integrityAudit.setThreadInitialized(true); // An exception will set + // it to false + + while (true) { + try{ + + /* + * It may have been awhile since we last cycled through this + * loop, so refresh the list of IntegrityAuditEntities. + */ + List integrityAuditEntityList = getIntegrityAuditEntityList(); + + /* + * We could've set entityCurrentlyDesignated as a side effect of + * getIntegrityAuditEntityList(), but then we would've had to + * make entityCurrentlyDesignated a class level attribute. Using + * this approach, we can keep it local to the run() method. + */ + entityCurrentlyDesignated = getEntityCurrentlyDesignated(integrityAuditEntityList); + + /* + * Need to refresh thisEntity each time through loop, because we + * need a fresh version of lastUpdated. + */ + thisEntity = getThisEntity(integrityAuditEntityList); + + /* + * If we haven't done the audit yet, note that we're current and + * see if we're designated. + */ + if (!auditCompleted) { + dbDAO.setLastUpdated(); + + /* + * If no current designation or currently designated node is + * stale, see if we're the next node to be designated. + */ + if (entityCurrentlyDesignated == null + || isStale(entityCurrentlyDesignated)) { + IntegrityAuditEntity designationCandidate = getDesignationCandidate(integrityAuditEntityList); + + /* + * If we're the next node to be designated, run the + * audit. + */ + if (designationCandidate.getResourceName().equals( + this.resourceName)) { + runAudit(dbAudit); + auditCompleted = true; + } else { + if (logger.isDebugEnabled()) { + logger.debug("AuditThread.run: designationCandidate, " + + designationCandidate + .getResourceName() + + ", not this entity, " + + thisEntity.getResourceName()); + } + } + + /* + * Application may have been stopped and restarted, in + * which case we might be designated but auditCompleted + * will have been reset to false, so account for this. + */ + } else if (thisEntity.getResourceName().equals( + entityCurrentlyDesignated.getResourceName())) { + + if (logger.isDebugEnabled()) { + logger.debug("AuditThread.run: Re-running audit for " + + thisEntity.getResourceName()); + } + runAudit(dbAudit); + auditCompleted = true; + + } else { + if (logger.isDebugEnabled()) { + logger.debug("AuditThread.run: Currently designated node, " + + entityCurrentlyDesignated + .getResourceName() + + ", not yet stale and not this node"); + } + } + + + /* + * Audit already completed on this node, so allow the node + * to go stale until twice the AUDIT_COMPLETION_PERIOD has + * elapsed. This should give plenty of time for another node + * (if another node is out there) to pick up designation. + */ + } else { + + auditCompleted = resetAuditCompleted(auditCompleted, + thisEntity); + + } + + /* + * If we've just run audit, sleep per the + * integrity_audit_period_seconds property, otherwise just sleep + * the normal interval. + */ + if (auditCompleted) { + + if (logger.isDebugEnabled()) { + logger.debug("AuditThread.run: Audit completed; resourceName=" + + this.resourceName + + " sleeping " + + integrityAuditPeriodMillis + "ms"); + } + Thread.sleep(integrityAuditPeriodMillis); + if (logger.isDebugEnabled()) { + logger.debug("AuditThread.run: resourceName=" + + this.resourceName + " awaking from " + + integrityAuditPeriodMillis + "ms sleep"); + } + + } else { + + if (logger.isDebugEnabled()) { + logger.debug("AuditThread.run: resourceName=" + + this.resourceName + ": Sleeping " + + AuditThread.AUDIT_THREAD_SLEEP_INTERVAL + + "ms"); + } + Thread.sleep(AuditThread.AUDIT_THREAD_SLEEP_INTERVAL); + if (logger.isDebugEnabled()) { + logger.debug("AuditThread.run: resourceName=" + + this.resourceName + ": Awaking from " + + AuditThread.AUDIT_THREAD_SLEEP_INTERVAL + + "ms sleep"); + } + + } + } catch (Exception e){ + String msg = "AuditThread.run loop - Exception thrown: " + e.getMessage() + + "; Will try audit again in " + (integrityAuditPeriodMillis/1000) + " seconds"; + logger.error(MessageCodes.EXCEPTION_ERROR, e, msg); + // Sleep and try again later + Thread.sleep(integrityAuditPeriodMillis); + continue; + } + + } + + } catch (Exception e) { + String msg = "AuditThread.run: Could not start audit loop. Exception thrown; message="+ e.getMessage(); + logger.error(MessageCodes.EXCEPTION_ERROR, e, msg); + integrityAudit.setThreadInitialized(false); + } + + logger.info("AuditThread.run: Exiting"); + } + + /* + * Used to create a list that is sorted lexicographically by resourceName. + */ + Comparator comparator = new Comparator() { + @Override + public int compare(final IntegrityAuditEntity r1, + final IntegrityAuditEntity r2) { + return r1.getResourceName().compareTo(r2.getResourceName()); + } + }; + + /** + * getDesignationCandidate() + * Using round robin algorithm, gets next candidate to be designated. Assumes + * list is sorted lexicographically by resourceName. + */ + private IntegrityAuditEntity getDesignationCandidate( + List integrityAuditEntityList) { + + //Note: assumes integrityAuditEntityList is already lexicographically sorted by resourceName + + if (logger.isDebugEnabled()) { + logger.debug("getDesignationCandidate: Entering, integrityAuditEntityList.size()=" + + integrityAuditEntityList.size()); + } + + IntegrityAuditEntity designationCandidate = null; + IntegrityAuditEntity thisEntity = null; + + int designatedEntityIndex = -1; + int entityIndex = 0; + int priorCandidateIndex = -1; + int subsequentCandidateIndex = -1; + + for (IntegrityAuditEntity integrityAuditEntity : integrityAuditEntityList) { + + if (logger.isDebugEnabled()) { + logIntegrityAuditEntity(integrityAuditEntity); + } + + if (integrityAuditEntity.getResourceName() + .equals(this.resourceName)) { + if (logger.isDebugEnabled()) { + logger.debug("getDesignationCandidate: thisEntity=" + + integrityAuditEntity.getResourceName()); + } + thisEntity = integrityAuditEntity; + } + + if (integrityAuditEntity.isDesignated()) { + if (logger.isDebugEnabled()) { + logger.debug("getDesignationCandidate: Currently designated entity resourceName=" + + integrityAuditEntity.getResourceName() + + ", persistenceUnit=" + + integrityAuditEntity.getPersistenceUnit() + + ", lastUpdated=" + + integrityAuditEntity.getLastUpdated() + + ", entityIndex=" + entityIndex); + } + designatedEntityIndex = entityIndex; + + /* + * Entity not currently designated + */ + } else { + + /* + * See if non-designated entity is stale. + */ + if (isStale(integrityAuditEntity)) { + + if (logger.isDebugEnabled()) { + logger.debug("getDesignationCandidate: Entity is stale; resourceName=" + + integrityAuditEntity.getResourceName() + + ", persistenceUnit=" + + integrityAuditEntity.getPersistenceUnit() + + ", lastUpdated=" + + integrityAuditEntity.getLastUpdated() + + ", entityIndex=" + entityIndex); + } + + /* + * Entity is current. + */ + } else { + + if (designatedEntityIndex == -1) { + + if (priorCandidateIndex == -1) { + if (logger.isDebugEnabled()) { + logger.debug("getDesignationCandidate: Prior candidate found, resourceName=" + + integrityAuditEntity + .getResourceName() + + ", persistenceUnit=" + + integrityAuditEntity + .getPersistenceUnit() + + ", lastUpdated=" + + integrityAuditEntity.getLastUpdated() + + ", entityIndex=" + entityIndex); + } + priorCandidateIndex = entityIndex; + } else { + if (logger.isDebugEnabled()) { + logger.debug("getDesignationCandidate: Prior entity current but prior candidate already found; resourceName=" + + integrityAuditEntity + .getResourceName() + + ", persistenceUnit=" + + integrityAuditEntity + .getPersistenceUnit() + + ", lastUpdated=" + + integrityAuditEntity.getLastUpdated() + + ", entityIndex=" + entityIndex); + } + } + } else { + if (subsequentCandidateIndex == -1) { + if (logger.isDebugEnabled()) { + logger.debug("getDesignationCandidate: Subsequent candidate found, resourceName=" + + integrityAuditEntity + .getResourceName() + + ", persistenceUnit=" + + integrityAuditEntity + .getPersistenceUnit() + + ", lastUpdated=" + + integrityAuditEntity.getLastUpdated() + + ", entityIndex=" + entityIndex); + } + subsequentCandidateIndex = entityIndex; + } else { + if (logger.isDebugEnabled()) { + logger.debug("getDesignationCandidate: Subsequent entity current but subsequent candidate already found; resourceName=" + + integrityAuditEntity + .getResourceName() + + ", persistenceUnit=" + + integrityAuditEntity + .getPersistenceUnit() + + ", lastUpdated=" + + integrityAuditEntity.getLastUpdated() + + ", entityIndex=" + entityIndex); + } + } + } + + } // end entity is current + + } // end entity not currently designated + + entityIndex++; + + } // end for loop + + /* + * Per round robin algorithm, if a current entity is found that is + * lexicographically after the currently designated entity, this entity + * becomes the designation candidate. If no current entity is found that + * is lexicographically after currently designated entity, we cycle back + * to beginning of list and pick the first current entity as the + * designation candidate. + */ + if (subsequentCandidateIndex != -1) { + designationCandidate = integrityAuditEntityList + .get(subsequentCandidateIndex); + if (logger.isDebugEnabled()) { + logger.debug("getDesignationCandidate: Exiting and returning subsequent designationCandidate=" + + designationCandidate.getResourceName()); + } + } else { + if (priorCandidateIndex != -1) { + designationCandidate = integrityAuditEntityList + .get(priorCandidateIndex); + if (logger.isDebugEnabled()) { + logger.debug("getDesignationCandidate: Exiting and returning prior designationCandidate=" + + designationCandidate.getResourceName()); + } + } else { + logger.debug("getDesignationCandidate: No subsequent or prior candidate found; designating thisEntity, resourceName=" + + thisEntity.getResourceName()); + designationCandidate = thisEntity; + } + } + + return designationCandidate; + + } + + /** + * getEntityCurrentlyDesignated() + * Returns entity that is currently designated. + * @param integrityAuditEntityList + * @return + */ + private IntegrityAuditEntity getEntityCurrentlyDesignated( + List integrityAuditEntityList) { + + if (logger.isDebugEnabled()) { + logger.debug("getEntityCurrentlyDesignated: Entering, integrityAuditEntityList.size=" + + integrityAuditEntityList.size()); + } + + IntegrityAuditEntity entityCurrentlyDesignated = null; + + for (IntegrityAuditEntity integrityAuditEntity : integrityAuditEntityList) { + + if (integrityAuditEntity.isDesignated()) { + if (logger.isDebugEnabled()) { + logger.debug("getEntityCurrentlyDesignated: Currently designated entity resourceName=" + + integrityAuditEntity.getResourceName() + + ", persistenceUnit=" + + integrityAuditEntity.getPersistenceUnit() + + ", lastUpdated=" + + integrityAuditEntity.getLastUpdated()); + } + entityCurrentlyDesignated = integrityAuditEntity; + } + + } // end for loop + + if (logger.isDebugEnabled()) { + if (entityCurrentlyDesignated != null) { + logger.debug("getEntityCurrentlyDesignated: Exiting and returning entityCurrentlyDesignated=" + + entityCurrentlyDesignated.getResourceName()); + } else { + logger.debug("getEntityCurrentlyDesignated: Exiting and returning entityCurrentlyDesignated=" + + entityCurrentlyDesignated); + } + } + return entityCurrentlyDesignated; + + } + + /** + * getIntegrityAuditEnityList gets the list of IntegrityAuditEntity + * @return + * @throws DbDaoTransactionException + */ + private List getIntegrityAuditEntityList() + throws DbDaoTransactionException { + + if (logger.isDebugEnabled()) { + logger.debug("getIntegrityAuditEntityList: Entering"); + } + + /* + * Get all records for this nodeType and persistenceUnit and then sort + * them lexicographically by resourceName. Get index of designated + * entity, if any. + */ + /* + * Sorted list of entities for a particular nodeType and + * persistenceUnit. + */ + List integrityAuditEntityList = new ArrayList(); + integrityAuditEntityList = dbDAO.getIntegrityAuditEntities( + this.persistenceUnit, this.nodeType); + int listSize = integrityAuditEntityList.size(); + if (logger.isDebugEnabled()) { + logger.debug("getIntegrityAuditEntityList: Got " + listSize + + " IntegrityAuditEntity records"); + } + Collections.sort((List) integrityAuditEntityList, + comparator); + + if (logger.isDebugEnabled()) { + logger.debug("getIntegrityAuditEntityList: Exiting and returning integrityAuditEntityList, size=" + + listSize); + } + return integrityAuditEntityList; + + } + + + /** + * Returns the IntegrityAuditEntity for this entity. + * @param integrityAuditEntityList + * @return + */ + private IntegrityAuditEntity getThisEntity( + List integrityAuditEntityList) { + + if (logger.isDebugEnabled()) { + logger.debug("getThisEntity: Entering, integrityAuditEntityList.size=" + + integrityAuditEntityList.size()); + } + + IntegrityAuditEntity thisEntity = null; + + for (IntegrityAuditEntity integrityAuditEntity : integrityAuditEntityList) { + + if (integrityAuditEntity.getResourceName().equals(this.resourceName)) { + if (logger.isDebugEnabled()) { + logger.debug("getThisEntity: For this entity, resourceName=" + + integrityAuditEntity.getResourceName() + + ", persistenceUnit=" + + integrityAuditEntity.getPersistenceUnit() + + ", lastUpdated=" + + integrityAuditEntity.getLastUpdated()); + } + thisEntity = integrityAuditEntity; + } + + } // end for loop + + if (logger.isDebugEnabled()) { + if (thisEntity != null) { + logger.debug("getThisEntity: Exiting and returning thisEntity=" + + thisEntity.getResourceName()); + } else { + logger.debug("getThisEntity: Exiting and returning thisEntity=" + + thisEntity); + } + } + return thisEntity; + + } + + + /** + * Returns false if the lastUpdated time for the record in question is more + * than AUDIT_COMPLETION_INTERVAL seconds ago. During an audit, lastUpdated is updated every five + * seconds or so, but when an audit finishes, the node doing the audit stops + * updating lastUpdated. + * @param integrityAuditEntity + * @return + */ + private boolean isStale(IntegrityAuditEntity integrityAuditEntity) { + + if (logger.isDebugEnabled()) { + logger.debug("isStale: Entering, resourceName=" + + integrityAuditEntity.getResourceName() + + ", persistenceUnit=" + + integrityAuditEntity.getPersistenceUnit() + + ", lastUpdated=" + integrityAuditEntity.getLastUpdated()); + } + + boolean stale = false; + + Date currentTime = new Date(); + Date lastUpdated = integrityAuditEntity.getLastUpdated(); + + /* + * If lastUpdated is null, we assume that the audit never ran for that + * node. + */ + long lastUpdatedTime = 0; + if (lastUpdated != null) { + lastUpdatedTime = lastUpdated.getTime(); + } + long timeDifference = currentTime.getTime() - lastUpdatedTime; + if (timeDifference > AUDIT_COMPLETION_INTERVAL) { + stale = true; + } + + if (logger.isDebugEnabled()) { + logger.debug("isStale: Exiting and returning stale=" + stale + + ", timeDifference=" + timeDifference); + } + + return stale; + } + + private void logIntegrityAuditEntity( + IntegrityAuditEntity integrityAuditEntity) { + + logger.debug("logIntegrityAuditEntity: id=" + + integrityAuditEntity.getId() + ", jdbcDriver=" + + integrityAuditEntity.getJdbcDriver() + ", jdbcPassword=" + + integrityAuditEntity.getJdbcPassword() + ", jdbcUrl=" + + integrityAuditEntity.getJdbcUrl() + ", jdbcUser=" + + integrityAuditEntity.getJdbcUser() + ", nodeType=" + + integrityAuditEntity.getNodeType() + ", persistenceUnit=" + + integrityAuditEntity.getPersistenceUnit() + ", resourceName=" + + integrityAuditEntity.getResourceName() + ", site=" + + integrityAuditEntity.getSite() + ", createdDate=" + + integrityAuditEntity.getCreatedDate() + ", lastUpdated=" + + integrityAuditEntity.getLastUpdated() + ", designated=" + + integrityAuditEntity.isDesignated()); + } + + /* + * If more than (AUDIT_COMPLETION_INTERVAL * 2) milliseconds have elapsed + * since we last ran the audit, reset auditCompleted, so + * + * 1) we'll eventually re-run the audit, if no other node picks up the + * designation. + * + * or + * + * 2) We'll run the audit when the round robin comes back to us. + */ + private boolean resetAuditCompleted(boolean auditCompleted, + IntegrityAuditEntity thisEntity) { + + if (logger.isDebugEnabled()) { + logger.debug("resetAuditCompleted: auditCompleted=" + + auditCompleted + "; for thisEntity, resourceName=" + + thisEntity.getResourceName() + ", persistenceUnit=" + + thisEntity.getPersistenceUnit() + ", lastUpdated=" + + thisEntity.getLastUpdated()); + } + + long timeDifference = -1; + + Date currentTime = new Date(); + Date lastUpdated = thisEntity.getLastUpdated(); + + long lastUpdatedTime = lastUpdated.getTime(); + timeDifference = currentTime.getTime() - lastUpdatedTime; + + if (timeDifference > (AUDIT_COMPLETION_INTERVAL * 2)) { + if (logger.isDebugEnabled()) { + logger.debug("resetAuditCompleted: Resetting auditCompleted for resourceName=" + + this.resourceName); + } + auditCompleted = false; + } else { + if (logger.isDebugEnabled()) { + logger.debug("resetAuditCompleted: For resourceName=" + + resourceName + + ", time since last update is only " + + timeDifference + "; retaining current value for auditCompleted"); + } + } + + if (logger.isDebugEnabled()) { + logger.debug("resetAuditCompleted: Exiting and returning auditCompleted=" + + auditCompleted + ", timeDifference=" + timeDifference); + } + return auditCompleted; + } + + private void runAudit(DbAudit dbAudit) throws Exception { + + if (logger.isDebugEnabled()) { + logger.debug("runAudit: Entering, dbAudit=" + dbAudit + + "; notifying other resources that resourceName=" + + this.resourceName + " is current"); + } + + /* + * changeDesignated marks all other nodes as non-designated and this + * node as designated. + */ + dbDAO.changeDesignated(this.resourceName, this.persistenceUnit, + this.nodeType); + + if (logger.isDebugEnabled()) { + logger.debug("runAudit: Running audit for persistenceUnit=" + + this.persistenceUnit + " on resourceName=" + + this.resourceName); + } + if (IntegrityAudit.isUnitTesting) { + dbAudit.dbAuditSimulate(this.resourceName, this.persistenceUnit, + this.nodeType); + } else { + dbAudit.dbAudit(this.resourceName, this.persistenceUnit, + this.nodeType); + } + + if (logger.isDebugEnabled()) { + logger.debug("runAudit: Exiting"); + } + + } + +} diff --git a/integrity-audit/src/main/java/org/onap/policy/common/ia/DbAudit.java b/integrity-audit/src/main/java/org/onap/policy/common/ia/DbAudit.java new file mode 100644 index 00000000..8312daa3 --- /dev/null +++ b/integrity-audit/src/main/java/org/onap/policy/common/ia/DbAudit.java @@ -0,0 +1,462 @@ +/*- + * ============LICENSE_START======================================================= + * Integrity Audit + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.common.ia; + +import java.io.Serializable; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Properties; + +import javax.persistence.Table; + +import org.apache.commons.lang3.SerializationUtils; +import org.apache.commons.lang3.builder.RecursiveToStringStyle; +import org.apache.commons.lang3.builder.ReflectionToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; +//import org.apache.log4j.Logger; + + + + + +import org.onap.policy.common.ia.jpa.IntegrityAuditEntity; +import org.onap.policy.common.logging.eelf.MessageCodes; +import org.onap.policy.common.logging.flexlogger.FlexLogger; +import org.onap.policy.common.logging.flexlogger.Logger; + +/** + * class DbAudit does actual auditing of DB tables. + */ +public class DbAudit { + + private static final Logger logger = FlexLogger.getLogger(DbAudit.class); + + DbDAO dbDAO = null; + + public DbAudit(DbDAO dbDAO) { + + if (logger.isDebugEnabled()) { + logger.debug("Constructor: Entering"); + } + + this.dbDAO = dbDAO; + + if (logger.isDebugEnabled()) { + logger.debug("Constructor: Exiting"); + } + + } + + /** + * dbAudit actually does the audit + * @param resourceName + * @param persistenceUnit + * @param nodeType + * @throws Exception + */ + public void dbAudit(String resourceName, String persistenceUnit, String nodeType) throws Exception { + + if (logger.isDebugEnabled()) { + logger.debug("dbAudit: Entering, resourceName=" + resourceName + + ", persistenceUnit=" + persistenceUnit + ", nodeType=" + + nodeType); + } + + // Get all IntegrityAudit entries so we can get the DB access info + List iaeList = dbDAO.getIntegrityAuditEntities(persistenceUnit, nodeType); + if(iaeList == null || iaeList.isEmpty()){ + + String msg = "DbAudit: for node " + resourceName + " Found no IntegrityAuditEntity entries"; + logger.error(MessageCodes.ERROR_AUDIT, msg); + throw new DbAuditException(msg); + + }else if(iaeList.size() == 1){ + + Long iaeId = null; + String iaeRN = null; + String iaeNT = null; + String iaeS = null; + for (IntegrityAuditEntity iae : iaeList){ + iaeId = iae.getId(); + iaeRN = iae.getResourceName(); + iaeNT = iae.getNodeType(); + iaeS = iae.getSite(); + } + String msg = "DbAudit: Found only one IntegrityAuditEntity entry:" + + " ID = " + iaeId + + " ResourceName = " + iaeRN + + " NodeType = " + iaeNT + + " Site = " + iaeS; + logger.warn(msg); + return; + } + + // Obtain all persistence class names for the PU we are auditing + HashSet classNameSet = dbDAO.getPersistenceClassNames(); + if(classNameSet == null || classNameSet.isEmpty()){ + + String msg = "DbAudit: For node " + resourceName + " Found no persistence class names"; + logger.error(MessageCodes.ERROR_AUDIT, msg); + throw new DbAuditException(msg); + + } + + /* + * Retrieve myIae. We are going to compare the local class entries against + * all other DB nodes. Since the audit is run in a round-robin, every instance + * will be compared against every other instance. + */ + IntegrityAuditEntity myIae = dbDAO.getMyIntegrityAuditEntity(); + + if(myIae == null){ + + String msg = "DbAudit: Found no IntegrityAuditEntity entry for resourceName: " + resourceName + + " persistenceUnit: " + persistenceUnit; + logger.error(MessageCodes.ERROR_AUDIT, msg); + throw new DbAuditException(msg); + + } + /* + * This is the map of mismatched entries indexed by className. For + * each class name there is a list of mismatched entries + */ + HashMap> misMatchedMap = new HashMap>(); + + // We need to keep track of how long the audit is taking + long startTime = System.currentTimeMillis(); + + // Retrieve all instances of the class for each node + if (logger.isDebugEnabled()) { + logger.debug("dbAudit: Traversing classNameSet, size=" + classNameSet.size()); + } + for(String clazzName: classNameSet){ + + if (logger.isDebugEnabled()) { + logger.debug("dbAudit: clazzName=" + clazzName); + } + + // all instances of the class for myIae + HashMap myEntries = dbDAO.getAllMyEntries(clazzName); + //get a map of the objects indexed by id. Does not necessarily have any entries + + if (logger.isDebugEnabled()) { + logger.debug("dbAudit: Traversing iaeList, size=" + iaeList.size()); + } + for (IntegrityAuditEntity iae : iaeList){ + if(iae.getId() == myIae.getId()){ + if (logger.isDebugEnabled()) { + logger.debug("dbAudit: My Id=" + iae.getId() + + ", resourceName=" + iae.getResourceName()); + } + continue; //no need to compare with self + } else { + if (logger.isDebugEnabled()) { + logger.debug("dbAudit: Id=" + iae.getId() + + ", resourceName=" + iae.getResourceName()); + } + } + // Create properties for the other db node + Properties theirProperties = new Properties(); + theirProperties.put(IntegrityAuditProperties.DB_DRIVER, iae.getJdbcDriver()); + theirProperties.put(IntegrityAuditProperties.DB_URL, iae.getJdbcUrl()); + theirProperties.put(IntegrityAuditProperties.DB_USER, iae.getJdbcUser()); + theirProperties.put(IntegrityAuditProperties.DB_PWD, iae.getJdbcPassword()); + theirProperties.put(IntegrityAuditProperties.SITE_NAME, iae.getSite()); + theirProperties.put(IntegrityAuditProperties.NODE_TYPE, iae.getNodeType()); + + //get a map of the instances for their iae indexed by id + HashMap theirEntries = dbDAO.getAllEntries(persistenceUnit, theirProperties, clazzName); + if (logger.isDebugEnabled()) { + logger.debug("dbAudit: For persistenceUnit=" + + persistenceUnit + ", clazzName=" + clazzName + + ", theirEntries.size()=" + + theirEntries.size()); + } + + /* + * Compare myEntries with theirEntries and get back a set of mismatched IDs. + * Collect the IDs for the class where a mismatch occurred. We will check + * them again for all nodes later. + */ + HashSet misMatchedKeySet = compareEntries(myEntries, theirEntries); + if(!misMatchedKeySet.isEmpty()){ + HashSet misMatchedEntry = misMatchedMap.get(clazzName); + if(misMatchedEntry == null){ + misMatchedMap.put(clazzName, misMatchedKeySet); + }else{ + misMatchedEntry.addAll(misMatchedKeySet); + misMatchedMap.put(clazzName, misMatchedEntry); + } + } + } //end for (IntegrityAuditEntity iae : iaeList) + //Time check + if((System.currentTimeMillis() - startTime) >= 5000){ //5 seconds + //update the timestamp + dbDAO.setLastUpdated(); + //reset the startTime + startTime=System.currentTimeMillis(); + }else{ + //sleep a couple seconds to break up the activity + if (logger.isDebugEnabled()) { + logger.debug("dbAudit: Sleeping 2 seconds"); + } + Thread.sleep(2000); + if (logger.isDebugEnabled()) { + logger.debug("dbAudit: Waking from sleep"); + } + } + }//end: for(String clazzName: classNameList) + + //check if misMatchedMap is empty + if(misMatchedMap.isEmpty()){ + + if (logger.isDebugEnabled()) { + logger.debug("dbAudit: Exiting, misMatchedMap is empty"); + } + //we are done + return; + } else { + if (logger.isDebugEnabled()) { + logger.debug("dbAudit: Doing another comparison; misMatchedMap.size()=" + misMatchedMap.size()); + } + } + + // If misMatchedMap is not empty, retrieve the entries in each misMatched list and compare again + classNameSet = new HashSet(misMatchedMap.keySet()); + // We need to keep track of how long the audit is taking + startTime = System.currentTimeMillis(); + + // Retrieve all instances of the class for each node + if (logger.isDebugEnabled()) { + logger.debug("dbAudit: Second comparison; traversing classNameSet, size=" + classNameSet.size()); + } + + int errorCount = 0; + + for(String clazzName: classNameSet){ + + if (logger.isDebugEnabled()) { + logger.debug("dbAudit: Second comparison; clazzName=" + clazzName); + } + + // all instances of the class for myIae + HashSet keySet = misMatchedMap.get(clazzName); + HashMap myEntries = dbDAO.getAllMyEntries(clazzName, keySet); + //get a map of the objects indexed by id + + if (logger.isDebugEnabled()) { + logger.debug("dbAudit: Second comparison; traversing iaeList, size=" + iaeList.size()); + } + for (IntegrityAuditEntity iae : iaeList){ + if(iae.getId() == myIae.getId()){ + if (logger.isDebugEnabled()) { + logger.debug("dbAudit: Second comparison; My Id=" + iae.getId() + + ", resourceName=" + iae.getResourceName()); + } + continue; //no need to compare with self + } else { + if (logger.isDebugEnabled()) { + logger.debug("dbAudit: Second comparison; Id=" + iae.getId() + + ", resourceName=" + iae.getResourceName()); + } + } + // Create properties for the other db node + Properties theirProperties = new Properties(); + theirProperties.put(IntegrityAuditProperties.DB_DRIVER, iae.getJdbcDriver()); + theirProperties.put(IntegrityAuditProperties.DB_URL, iae.getJdbcUrl()); + theirProperties.put(IntegrityAuditProperties.DB_USER, iae.getJdbcUser()); + theirProperties.put(IntegrityAuditProperties.DB_PWD, iae.getJdbcPassword()); + theirProperties.put(IntegrityAuditProperties.SITE_NAME, iae.getSite()); + theirProperties.put(IntegrityAuditProperties.NODE_TYPE, iae.getNodeType()); + + //get a map of the instances for their iae indexed by id + HashMap theirEntries = dbDAO.getAllEntries(persistenceUnit, theirProperties, clazzName, keySet); + + /* + * Compare myEntries with theirEntries and get back a set of mismatched IDs. + * Collect the IDs for the class where a mismatch occurred. We will now + * write an error log for each. + */ + HashSet misMatchedKeySet = compareEntries(myEntries, theirEntries); + if(!misMatchedKeySet.isEmpty()){ + String keysString = ""; + for(Object key: misMatchedKeySet){ + keysString = keysString.concat(key.toString() + ", "); + errorCount ++; + } + writeAuditSummaryLog(clazzName, resourceName, iae.getResourceName(), keysString); + if(logger.isDebugEnabled()){ + for(Object key : misMatchedKeySet){ + writeAuditDebugLog(clazzName, resourceName, iae.getResourceName(), myEntries.get(key), theirEntries.get(key)); + } + } + } + } + //Time check + if((System.currentTimeMillis() - startTime) >= 5000){ //5 seconds + //update the timestamp + dbDAO.setLastUpdated(); + //reset the startTime + startTime=System.currentTimeMillis(); + }else{ + //sleep a couple seconds to break up the activity + if (logger.isDebugEnabled()) { + logger.debug("dbAudit: Second comparison; sleeping 2 seconds"); + } + Thread.sleep(2000); + if (logger.isDebugEnabled()) { + logger.debug("dbAudit: Second comparison; waking from sleep"); + } + } + }//end: for(String clazzName: classNameList) + + if(errorCount != 0){ + String msg = " DB Audit: " + errorCount + " errors found. A large number of errors may indicate DB replication has stopped"; + logger.error(MessageCodes.ERROR_AUDIT, msg); + } + + if (logger.isDebugEnabled()) { + logger.debug("dbAudit: Exiting"); + } + + return; //all done + } + + /** + * dbAuditSimulate simulates the DB audit + * @param resourceName + * @param persistenceUnit + * @param nodeType + * @throws InterruptedException + * @throws DbDaoTransactionException + */ + public void dbAuditSimulate(String resourceName, String persistenceUnit, + String nodeType) throws InterruptedException, + DbDaoTransactionException { + + logger.info("dbAuditSimulate: Starting audit simulation for resourceName=" + + resourceName + ", persistenceUnit=" + persistenceUnit); + + for (int i = 0; i < AuditThread.AUDIT_SIMULATION_ITERATIONS; i++) { + dbDAO.setLastUpdated(); + logger.info("dbAuditSimulate: i=" + i + ", sleeping " + + AuditThread.AUDIT_SIMULATION_SLEEP_INTERVAL + "ms"); + Thread.sleep(AuditThread.AUDIT_SIMULATION_SLEEP_INTERVAL); + } + + logger.info("dbAuditSimulate: Finished audit simulation for resourceName=" + + resourceName + ", persistenceUnit=" + persistenceUnit); + + } + + /** + * compareEntries() will compare the lists of entries from the DB + * @param myEntries + * @param theirEntries + * @return + */ + public HashSet compareEntries(HashMap myEntries, HashMap theirEntries){ + /* + * Compare the entries for the same key in each of the hashmaps. The comparison will be done by serializing the objects + * (create a byte array) and then do a byte array comparison. The audit will walk the local repository hash map comparing + * to the remote cluster hashmap and then turn it around and walk the remote hashmap and look for any entries that are not + * present in the local cluster hashmap. + * + * If the objects are not identical, the audit will put the object IDs on a list to try after completing the audit of the table + * it is currently working on. + * + */ + HashSet misMatchedKeySet = new HashSet(); + for(Object key: myEntries.keySet()){ + byte[] mySerializedEntry = SerializationUtils.serialize((Serializable) myEntries.get(key)); + byte[] theirSerializedEntry = SerializationUtils.serialize((Serializable) theirEntries.get(key)); + if(!Arrays.equals(mySerializedEntry, theirSerializedEntry)){ + logger.debug("compareEntries: For myEntries.key=" + key + ", entries do not match"); + misMatchedKeySet.add(key); + } else { + logger.debug("compareEntries: For myEntries.key=" + key + ", entries match"); + } + } + //now compare it in the other direction to catch entries in their set that is not in my set + for(Object key: theirEntries.keySet()){ + byte[] mySerializedEntry = SerializationUtils.serialize((Serializable) myEntries.get(key)); + byte[] theirSerializedEntry = SerializationUtils.serialize((Serializable) theirEntries.get(key)); + if(!Arrays.equals(mySerializedEntry, theirSerializedEntry)){ + logger.debug("compareEntries: For theirEntries.key=" + key + ", entries do not match"); + misMatchedKeySet.add(key); + } else { + logger.debug("compareEntries: For theirEntries.key=" + key + ", entries match"); + } + } + + //return a Set of the object IDs + logger.debug("compareEntries: misMatchedKeySet.size()=" + misMatchedKeySet.size()); + return misMatchedKeySet; + } + + /** + * writeAuditDebugLog() writes the mismatched entry details to the debug log + * @param clazzName + * @param resourceName1 + * @param resourceName2 + * @param entry1 + * @param entry2 + * @throws ClassNotFoundException + */ + public void writeAuditDebugLog(String clazzName, String resourceName1, + String resourceName2, Object entry1, Object entry2) throws ClassNotFoundException{ + Class entityClass = Class.forName(clazzName); + String tableName = entityClass.getAnnotation(Table.class).name(); + String msg = "\nDB Audit Error: " + + "\n Table Name: " + tableName + + "\n Entry 1 (short prefix style): " + resourceName1 + ": " + new ReflectionToStringBuilder(entry1,ToStringStyle.SHORT_PREFIX_STYLE).toString() + + "\n Entry 2 (short prefix style): " + resourceName2 + ": " + new ReflectionToStringBuilder(entry2,ToStringStyle.SHORT_PREFIX_STYLE).toString() + + "\n Entry 1 (recursive style): " + resourceName1 + ": " + new ReflectionToStringBuilder(entry1, new RecursiveToStringStyle()).toString() + + "\n Entry 2 (recursive style): " + resourceName2 + ": " + new ReflectionToStringBuilder(entry2, new RecursiveToStringStyle()).toString(); + logger.debug(msg); + + } + + /** + * writeAuditSummaryLog() writes a summary of the DB mismatches to the error log + * @param clazzName + * @param resourceName1 + * @param resourceName2 + * @param keys + * @throws ClassNotFoundException + */ + public void writeAuditSummaryLog(String clazzName, String resourceName1, + String resourceName2, String keys) throws ClassNotFoundException{ + Class entityClass = Class.forName(clazzName); + String tableName = entityClass.getAnnotation(Table.class).name(); + String msg = " DB Audit Error: Table Name: " + tableName + + "; Mismatch between nodes: " + resourceName1 +" and " + resourceName2 + + "; Mismatched entries (keys): " + keys; + logger.info(msg); + } + + + + + +} diff --git a/integrity-audit/src/main/java/org/onap/policy/common/ia/DbAuditException.java b/integrity-audit/src/main/java/org/onap/policy/common/ia/DbAuditException.java new file mode 100644 index 00000000..f5cd183f --- /dev/null +++ b/integrity-audit/src/main/java/org/onap/policy/common/ia/DbAuditException.java @@ -0,0 +1,37 @@ +/*- + * ============LICENSE_START======================================================= + * Integrity Audit + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.common.ia; + +public class DbAuditException extends Exception{ + private static final long serialVersionUID = 1L; + public DbAuditException() { + } + public DbAuditException(String message) { + super(message); + } + + public DbAuditException(Throwable cause) { + super(cause); + } + public DbAuditException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/integrity-audit/src/main/java/org/onap/policy/common/ia/DbDAO.java b/integrity-audit/src/main/java/org/onap/policy/common/ia/DbDAO.java new file mode 100644 index 00000000..64c01931 --- /dev/null +++ b/integrity-audit/src/main/java/org/onap/policy/common/ia/DbDAO.java @@ -0,0 +1,739 @@ +/*- + * ============LICENSE_START======================================================= + * Integrity Audit + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.common.ia; + +import java.util.Date; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Properties; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.EntityTransaction; +import javax.persistence.LockTimeoutException; +import javax.persistence.Persistence; +import javax.persistence.PersistenceUnitUtil; +import javax.persistence.Query; +import javax.persistence.TypedQuery; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Root; +import javax.persistence.metamodel.ManagedType; +import javax.persistence.metamodel.Metamodel; + +//import org.apache.log4j.Logger; + +import org.onap.policy.common.ia.jpa.IntegrityAuditEntity; +import org.onap.policy.common.logging.flexlogger.FlexLogger; +import org.onap.policy.common.logging.flexlogger.Logger; + +/** + * class DbDAO provides the inteface to the DBs for the purpose of audits. + * + */ +public class DbDAO { + private static final Logger logger = FlexLogger.getLogger(DbDAO.class.getName()); + private String resourceName; + private String persistenceUnit; + private String dbDriver; + private String dbUrl; + private String dbUser; + private String dbPwd; + private String siteName; + private String nodeType; + private Properties properties=null; + + private EntityManagerFactory emf; + + /* + * Supports designation serialization. + */ + private static final Object lock = new Object(); + + + /** + * DbDAO Constructor + * @param resourceName + * @param persistenceUnit + * @param properties + * @throws Exception + */ + public DbDAO(String resourceName, String persistenceUnit, Properties properties) throws Exception { + logger.debug("DbDAO contructor: enter"); + + validateProperties(resourceName, persistenceUnit, properties); + + emf = Persistence.createEntityManagerFactory(persistenceUnit, properties); + + register(); + + logger.debug("DbDAO contructor: exit"); + } + + /** + * validateProperties will validate the properties + * @param resourceName + * @param persistenceUnit + * @param properties + * @throws IntegrityAuditPropertiesException + */ + private void validateProperties(String resourceName, String persistenceUnit, Properties properties) throws IntegrityAuditPropertiesException{ + String badparams=""; + if(IntegrityAudit.parmsAreBad(resourceName, persistenceUnit, properties, badparams)){ + String msg = "DbDAO: Bad parameters: badparams" + badparams; + throw new IntegrityAuditPropertiesException(msg); + } + this.resourceName = resourceName; + this.persistenceUnit = persistenceUnit; + this.dbDriver = properties.getProperty(IntegrityAuditProperties.DB_DRIVER).trim(); + this.dbUrl = properties.getProperty(IntegrityAuditProperties.DB_URL).trim(); + this.dbUser = properties.getProperty(IntegrityAuditProperties.DB_USER).trim(); + this.dbPwd = properties.getProperty(IntegrityAuditProperties.DB_PWD).trim(); + this.siteName = properties.getProperty(IntegrityAuditProperties.SITE_NAME).trim(); + this.nodeType = properties.getProperty(IntegrityAuditProperties.NODE_TYPE).trim(); + this.properties = properties; + logger.debug("DbDAO.assignProperties: exit:" + + "\nresourceName: " + this.resourceName + + "\npersistenceUnit: " + this.persistenceUnit + + "\nproperties: " + this.properties); + } + + /** + * getAllMyEntries gets all the DB entries for a particular class + * @param className + * @return + */ + public HashMap getAllMyEntries(String className) { + logger.debug("getAllMyEntries: Entering, className=" + + className); + HashMap resultMap = new HashMap(); + EntityManager em = emf.createEntityManager(); + try{ + CriteriaBuilder cb = em.getCriteriaBuilder(); + CriteriaQuery cq = cb.createQuery(); + Root rootEntry = cq.from(Class.forName(className)); + CriteriaQuery all = cq.select(rootEntry); + TypedQuery allQuery = em.createQuery(all); + List objectList = allQuery.getResultList(); + //Now create the map + + PersistenceUnitUtil util = emf.getPersistenceUnitUtil(); + for (Object o: objectList){ + Object key = util.getIdentifier(o); + resultMap.put(key, o); + } + }catch(Exception e){ + String msg = "getAllEntries encountered exception: " + e; + logger.error(msg); + System.out.println(new Date()); + e.printStackTrace(); + } + em.close(); + logger.debug("getAllMyEntries: Exit, resultMap.keySet()=" + resultMap.keySet()); + return resultMap; + } + + /** + * getAllMyEntries gets all entries for a class + * @param className + * @param keySet + * @return + */ + public HashMap getAllMyEntries(String className, HashSet keySet){ + logger.debug("getAllMyEntries: Entering, className=" + + className + ",\n keySet=" + keySet); + + HashMap resultMap = new HashMap(); + EntityManager em = emf.createEntityManager(); + try{ + Class clazz = Class.forName(className); + for(Object key : keySet){ + Object entry = em.find(clazz, key); + resultMap.put(key, entry); + } + }catch(Exception e){ + String msg = "getAllMyEntries encountered exception: " + e; + logger.error(msg); + System.out.println(new Date()); + e.printStackTrace(); + } + em.close(); + + logger.debug("getAllMyEntries: Returning resultMap, size=" + resultMap.size()); + return resultMap; + } + + /** + * getAllEntries gets all entriesfor a particular persistence unit adn className + * @param persistenceUnit + * @param properties + * @param className + * @return + */ + public HashMap getAllEntries(String persistenceUnit, Properties properties, String className){ + + logger.debug("getAllEntries: Entering, persistenceUnit=" + + persistenceUnit + ",\n className=" + className); + HashMap resultMap = new HashMap(); + + EntityManagerFactory theEmf = Persistence.createEntityManagerFactory(persistenceUnit, properties); + EntityManager em = theEmf.createEntityManager(); + try{ + CriteriaBuilder cb = em.getCriteriaBuilder(); + CriteriaQuery cq = cb.createQuery(); + Root rootEntry = cq.from(Class.forName(className)); + CriteriaQuery all = cq.select(rootEntry); + TypedQuery allQuery = em.createQuery(all); + List objectList = allQuery.getResultList(); + + PersistenceUnitUtil util = theEmf.getPersistenceUnitUtil(); + for (Object o: objectList){ + Object key = util.getIdentifier(o); + resultMap.put(key, o); + } + }catch(Exception e){ + String msg = "getAllEntries encountered exception:" + e; + logger.error(msg); + System.out.println(new Date()); + e.printStackTrace(); + } + em.close(); + + logger.debug("getAllEntries: Returning resultMap, size=" + resultMap.size()); + + return resultMap; + } + + + /** + * getAllEntries gest all entries for a persistence unit + * @param persistenceUnit + * @param properties + * @param className + * @param keySet + * @return + */ + + public HashMap getAllEntries(String persistenceUnit, Properties properties, String className, HashSet keySet){ + logger.debug("getAllEntries: Entering, persistenceUnit=" + + persistenceUnit + ",\n properties= " + properties + ",\n className=" + className + ",\n keySet= " + keySet); + EntityManagerFactory theEmf = Persistence.createEntityManagerFactory(persistenceUnit, properties); + EntityManager em = theEmf.createEntityManager(); + HashMap resultMap = new HashMap(); + try{ + Class clazz = Class.forName(className); + for(Object key : keySet){ + Object entry = em.find(clazz, key); + resultMap.put(key, entry); + } + }catch(Exception e){ + String msg = "getAllEntries encountered exception: " + e; + logger.error(msg); + System.out.println(new Date()); + e.printStackTrace(); + } + em.close(); + logger.debug("getAllEntries: Exit, resultMap, size=" + resultMap.size()); + return resultMap; + } + + /** + * getIntegrityAuditEntities() Get all the IntegrityAuditEntities for a particular persistence unit + * and node type + * @param persistenceUnit + * @param nodeType + * @return + * @throws DbDaoTransactionException + */ + @SuppressWarnings("unchecked") + public List getIntegrityAuditEntities(String persistenceUnit, String nodeType) throws DbDaoTransactionException { + logger.debug("getIntegrityAuditEntities: Entering, persistenceUnit=" + + persistenceUnit + ",\n nodeType= " + nodeType); + try{ + EntityManager em = emf.createEntityManager(); + // Start a transaction + EntityTransaction et = em.getTransaction(); + + et.begin(); + + // if IntegrityAuditEntity entry exists for resourceName and PU, update it. If not found, create a new entry + Query iaequery = em.createQuery("Select i from IntegrityAuditEntity i where i.persistenceUnit=:pu and i.nodeType=:nt"); + iaequery.setParameter("pu", persistenceUnit); + iaequery.setParameter("nt", nodeType); + + List iaeList = iaequery.getResultList(); + + // commit transaction + et.commit(); + em.close(); + logger.debug("getIntegrityAuditEntities: Exit, iaeList=" + iaeList); + return iaeList; + }catch (Exception e){ + String msg = "DbDAO: " + "getIntegrityAuditEntities() " + "ecountered a problem in execution: "; + logger.error(msg + e); + System.out.println(new Date()); + e.printStackTrace(); + throw new DbDaoTransactionException(e); + } + + } + + /** + * getMyIntegrityAuditEntity() gets my IntegrityAuditEntity + * @return + * @throws DbDaoTransactionException + */ + public IntegrityAuditEntity getMyIntegrityAuditEntity() throws DbDaoTransactionException{ + try{ + EntityManager em = emf.createEntityManager(); + + // Start a transaction + EntityTransaction et = em.getTransaction(); + + et.begin(); + + // if IntegrityAuditEntity entry exists for resourceName and PU, retrieve it + Query iaequery = em.createQuery("Select i from IntegrityAuditEntity i where i.resourceName=:rn and i.persistenceUnit=:pu"); + iaequery.setParameter("rn", this.resourceName); + iaequery.setParameter("pu", this.persistenceUnit); + + @SuppressWarnings("rawtypes") + List iaeList = iaequery.getResultList(); + IntegrityAuditEntity iae = null; + + if(!iaeList.isEmpty()){ + //ignores multiple results + iae = (IntegrityAuditEntity) iaeList.get(0); + // refresh the object from DB in case cached data was returned + em.refresh(iae); + logger.info("Resource: " + this.resourceName + " with PersistenceUnit: " + this.persistenceUnit + + " exists"); + }else{ + // If it does not exist, log an error + logger.error("Attempting to setLastUpdated" + + " on an entry that does not exist:" + +" resource " + this.resourceName + " with PersistenceUnit: " + this.persistenceUnit); + } + + // close the transaction + et.commit(); + // close the EntityManager + em.close(); + + return iae; + }catch (Exception e){ + String msg = "DbDAO: " + "setLastUpdated() " + "ecountered a problem in execution: "; + logger.error(msg + e); + System.out.println(new Date()); + e.printStackTrace(); + throw new DbDaoTransactionException(e); + } + } + + + /** + * getIntegrityAuditEntity() gets the IntegrityAuditEntity with a particular ID + * @param id + * @return + * @throws DbDaoTransactionException + */ + public IntegrityAuditEntity getIntegrityAuditEntity(long id) throws DbDaoTransactionException{ + try{ + EntityManager em = emf.createEntityManager(); + + // Start a transaction + EntityTransaction et = em.getTransaction(); + + et.begin(); + + IntegrityAuditEntity iae = em.find(IntegrityAuditEntity.class, id); + + et.commit(); + em.close(); + + return iae; + }catch (Exception e){ + String msg = "DbDAO: " + "getIntegrityAuditEntity() " + "ecountered a problem in execution: "; + logger.error(msg + e); + System.out.println(new Date()); + e.printStackTrace(); + throw new DbDaoTransactionException(e); + } + } + + /** + * getPersistenceClassNames() gets all the persistence class names. + * @return + */ + public HashSet getPersistenceClassNames(){ + logger.debug("DbDAO: getPersistenceClassNames() entry"); + HashSet returnList = new HashSet(); + final Metamodel mm = emf.getMetamodel(); + logger.debug("\n" + persistenceUnit +" persistence unit classes:"); + for (final ManagedType managedType : mm.getManagedTypes()) { + Class c = managedType.getJavaType(); + logger.debug(" " + c.getSimpleName()); + returnList.add(c.getName()); //the full class name needed to make a query using jpa + } + logger.debug("DbDAO: getPersistenceClassNames() exit"); + return returnList; + } + + /** + * Register the IntegrityAudit instance + */ + private void register() throws DbDaoTransactionException { + try{ + EntityManager em = emf.createEntityManager(); + + // Start a transaction + EntityTransaction et = em.getTransaction(); + + et.begin(); + + // if IntegrityAuditEntity entry exists for resourceName and PU, update it. If not found, create a new entry + Query iaequery = em.createQuery("Select i from IntegrityAuditEntity i where i.resourceName=:rn and i.persistenceUnit=:pu"); + iaequery.setParameter("rn", this.resourceName); + iaequery.setParameter("pu", this.persistenceUnit); + + @SuppressWarnings("rawtypes") + List iaeList = iaequery.getResultList(); + IntegrityAuditEntity iae = null; + + //If it already exists, we just want to update the properties and lastUpdated date + if(!iaeList.isEmpty()){ + //ignores multiple results + iae = (IntegrityAuditEntity) iaeList.get(0); + // refresh the object from DB in case cached data was returned + em.refresh(iae); + logger.info("Resource: " + this.resourceName + " with PersistenceUnit: " + this.persistenceUnit + + " exists and entry be updated"); + }else{ + // If it does not exist, we also must add teh resourceName, persistenceUnit and designated values + logger.info("Adding resource " + resourceName + " with PersistenceUnit: " + this.persistenceUnit + + " to IntegrityAuditEntity table"); + iae = new IntegrityAuditEntity(); + iae.setResourceName(this.resourceName); + iae.setPersistenceUnit(this.persistenceUnit); + iae.setDesignated(false); + } + //update/set properties in entry + iae.setSite(this.siteName); + iae.setNodeType(this.nodeType); + iae.setLastUpdated(new Date()); + iae.setJdbcDriver(this.dbDriver); + iae.setJdbcPassword(this.dbPwd); + iae.setJdbcUrl(dbUrl); + iae.setJdbcUser(dbUser); + + em.persist(iae); + // flush to the DB + em.flush(); + + // commit transaction + et.commit(); + em.close(); + }catch (Exception e){ + String msg = "DbDAO: " + "register() " + "ecountered a problem in execution: "; + logger.error(msg + e); + System.out.println(new Date()); + e.printStackTrace(); + throw new DbDaoTransactionException(e); + } + + } + + public void setDesignated(boolean designated) throws DbDaoTransactionException{ + setDesignated(this.resourceName, this.persistenceUnit, designated); + } + + + public void setDesignated(String rName, String pUnit, boolean desig) throws DbDaoTransactionException{ + logger.debug("setDesignated: enter, resourceName: " + rName + ", persistenceUnit: " + + pUnit + ", designated: " + desig); + try{ + + EntityManager em = emf.createEntityManager(); + + // Start a transaction + EntityTransaction et = em.getTransaction(); + + et.begin(); + + // if IntegrityAuditEntity entry exists for resourceName and PU, update it. If not found, create a new entry + Query iaequery = em.createQuery("Select i from IntegrityAuditEntity i where i.resourceName=:rn and i.persistenceUnit=:pu"); + iaequery.setParameter("rn", rName); + iaequery.setParameter("pu", pUnit); + + @SuppressWarnings("rawtypes") + List iaeList = iaequery.getResultList(); + IntegrityAuditEntity iae = null; + + if(!iaeList.isEmpty()){ + //ignores multiple results + iae = (IntegrityAuditEntity) iaeList.get(0); + // refresh the object from DB in case cached data was returned + em.refresh(iae); + logger.info("Resource: " + rName + " with PersistenceUnit: " + pUnit + + " exists and designated be updated"); + iae.setDesignated(desig); + + em.persist(iae); + // flush to the DB + em.flush(); + }else{ + // If it does not exist, log an error + logger.error("Attempting to setDesignated(" + + desig + ") on an entry that does not exist:" + +" resource " + rName + " with PersistenceUnit: " + pUnit); + } + + // close the transaction + et.commit(); + // close the EntityManager + em.close(); + }catch (Exception e){ + String msg = "DbDAO: " + "setDesignated() " + "ecountered a problem in execution: "; + logger.error(msg + e); + System.out.println(new Date()); + e.printStackTrace(); + throw new DbDaoTransactionException(e); + } + + } + + public void setLastUpdated() throws DbDaoTransactionException{ + logger.debug("setLastUpdated: enter, resourceName: " + this.resourceName + ", persistenceUnit: " + + this.persistenceUnit); + try{ + EntityManager em = emf.createEntityManager(); + + // Start a transaction + EntityTransaction et = em.getTransaction(); + + et.begin(); + + // if IntegrityAuditEntity entry exists for resourceName and PU, update it. If not found, create a new entry + Query iaequery = em.createQuery("Select i from IntegrityAuditEntity i where i.resourceName=:rn and i.persistenceUnit=:pu"); + iaequery.setParameter("rn", this.resourceName); + iaequery.setParameter("pu", this.persistenceUnit); + + @SuppressWarnings("rawtypes") + List iaeList = iaequery.getResultList(); + IntegrityAuditEntity iae = null; + + if(!iaeList.isEmpty()){ + //ignores multiple results + iae = (IntegrityAuditEntity) iaeList.get(0); + // refresh the object from DB in case cached data was returned + em.refresh(iae); + logger.info("Resource: " + this.resourceName + " with PersistenceUnit: " + this.persistenceUnit + + " exists and lastUpdated be updated"); + iae.setLastUpdated(new Date()); + + em.persist(iae); + // flush to the DB + em.flush(); + }else{ + // If it does not exist, log an error + logger.error("Attempting to setLastUpdated" + + " on an entry that does not exist:" + +" resource " + this.resourceName + " with PersistenceUnit: " + this.persistenceUnit); + } + + // close the transaction + et.commit(); + // close the EntityManager + em.close(); + }catch (Exception e){ + String msg = "DbDAO: " + "setLastUpdated() " + "ecountered a problem in execution: "; + logger.error(msg + e); + System.out.println(new Date()); + e.printStackTrace(); + throw new DbDaoTransactionException(e); + } + + } + + /** + * Normally this method should only be used in a JUnit test environment. + * Manually deletes all PDP records in droolspdpentity table. + */ + public int deleteAllIntegrityAuditEntities() throws DbDaoTransactionException { + + try{ + + if (!IntegrityAudit.isUnitTesting) { + String msg = "DbDAO: " + "deleteAllIntegrityAuditEntities() " + "should only be invoked during JUnit testing"; + logger.error(msg); + throw new DbDaoTransactionException(msg); + } + + EntityManager em = emf.createEntityManager(); + // Start a transaction + EntityTransaction et = em.getTransaction(); + + et.begin(); + + // if IntegrityAuditEntity entry exists for resourceName and PU, update it. If not found, create a new entry + Query iaequery = em.createQuery("Delete from IntegrityAuditEntity"); + + int returnCode = iaequery.executeUpdate(); + + // commit transaction + et.commit(); + em.close(); + + logger.info("deleteAllIntegrityAuditEntities: returnCode=" + returnCode); + + return returnCode; + + }catch (Exception e){ + String msg = "DbDAO: " + "deleteAllIntegrityAuditEntities() " + "encountered a problem in execution: "; + logger.error(msg + e); + System.out.println(new Date()); + e.printStackTrace(); + throw new DbDaoTransactionException(e); + } + + } + + /** + * Changes designation to specified resourceName + * + * static lock object in conjunction with synchronized keyword ensures that + * designation changes are done serially within a resource. I.e. static lock + * ensures that multiple instantiations of DbDAO don't interleave + * changeDesignated() invocations and potentially produce simultaneous + * designations. + * + * Optimistic locking (the default, versus pessimistic) is sufficient to + * avoid simultaneous designations from interleaved changeDesignated() + * invocations from different resources (entities), because it prevents + * "dirty" and "non-repeatable" reads. + * + * See http://www.objectdb.com/api/java/jpa/LockModeType + * + * and + * + * http://stackoverflow.com/questions/2120248/how-to-synchronize-a-static- + * variable-among-threads-running-different-instances-o + */ + public void changeDesignated(String resourceName, String persistenceUnit, + String nodeType) throws DbDaoTransactionException { + + if (logger.isDebugEnabled()) { + logger.debug("changeDesignated: Entering, resourceName=" + + resourceName + ", persistenceUnit=" + persistenceUnit + + ", nodeType=" + nodeType); + } + + long startTime = System.currentTimeMillis(); + + synchronized (lock) { + + EntityManager em = null; + try { + + em = emf.createEntityManager(); + em.getTransaction().begin(); + + /* + * Define query + */ + Query query = em + .createQuery("Select i from IntegrityAuditEntity i where i.persistenceUnit=:pu and i.nodeType=:nt"); + query.setParameter("pu", persistenceUnit); + query.setParameter("nt", nodeType); + + /* + * Execute query using pessimistic write lock. This ensures that if anyone else is currently reading + * the records we'll throw a LockTimeoutException. + */ + @SuppressWarnings("unchecked") + List integrityAuditEntityList = (List) query + .getResultList(); + for (Object o : integrityAuditEntityList) { + if (o instanceof IntegrityAuditEntity) { + IntegrityAuditEntity integrityAuditEntity = (IntegrityAuditEntity) o; + if (integrityAuditEntity.getResourceName().equals( + resourceName)) { + if (logger.isDebugEnabled()) { + logger.debug("changeDesignated: Designating resourceName=" + + integrityAuditEntity + .getResourceName()); + } + integrityAuditEntity.setDesignated(true); + } else { + if (logger.isDebugEnabled()) { + logger.debug("changeDesignated: Removing designation from resourceName=" + + integrityAuditEntity + .getResourceName()); + } + integrityAuditEntity.setDesignated(false); + } + } + } + + if (logger.isDebugEnabled()) { + logger.debug("changeDesignated: Committing designation to resourceName=" + + resourceName); + } + em.getTransaction().commit(); + + /* + * If we get a LockTimeoutException, no harm done really. We'll + * probably be successful on the next attempt. The odds of + * another DbDAO instance on this entity or another entity + * attempting a simultaneous IntegrityAuditEntity table + * read/update are pretty slim (we're only in this method for + * two or three milliseconds) + */ + } catch (LockTimeoutException e) { + em.getTransaction().rollback(); + String msg = "DbDAO: " + "changeDesignated() " + + "caught LockTimeoutException, message=" + e.getMessage(); + logger.error(msg + e); + System.out.println(new Date()); + e.printStackTrace(); + throw new DbDaoTransactionException(msg, e); + } catch (Exception e) { + em.getTransaction().rollback(); + String msg = "DbDAO: " + "changeDesignated() " + + "caught Exception, message=" + e.getMessage(); + logger.error(msg + e); + System.out.println(new Date()); + e.printStackTrace(); + throw new DbDaoTransactionException(msg, e); + } + + } // end synchronized block + + if (logger.isDebugEnabled()) { + logger.debug("changeDesignated: Exiting; time expended=" + + (System.currentTimeMillis() - startTime) + "ms"); + } + + } + + +} diff --git a/integrity-audit/src/main/java/org/onap/policy/common/ia/DbDaoTransactionException.java b/integrity-audit/src/main/java/org/onap/policy/common/ia/DbDaoTransactionException.java new file mode 100644 index 00000000..4ad8521a --- /dev/null +++ b/integrity-audit/src/main/java/org/onap/policy/common/ia/DbDaoTransactionException.java @@ -0,0 +1,37 @@ +/*- + * ============LICENSE_START======================================================= + * Integrity Audit + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.common.ia; + +public class DbDaoTransactionException extends Exception{ + private static final long serialVersionUID = 1L; + public DbDaoTransactionException() { + } + public DbDaoTransactionException(String message) { + super(message); + } + + public DbDaoTransactionException(Throwable cause) { + super(cause); + } + public DbDaoTransactionException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/integrity-audit/src/main/java/org/onap/policy/common/ia/IntegrityAudit.java b/integrity-audit/src/main/java/org/onap/policy/common/ia/IntegrityAudit.java new file mode 100644 index 00000000..5ec6229c --- /dev/null +++ b/integrity-audit/src/main/java/org/onap/policy/common/ia/IntegrityAudit.java @@ -0,0 +1,241 @@ +/*- + * ============LICENSE_START======================================================= + * Integrity Audit + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.common.ia; + +import java.util.Properties; + +//import org.apache.log4j.Logger; + +import org.onap.policy.common.ia.IntegrityAuditProperties.NodeTypeEnum; +import org.onap.policy.common.logging.flexlogger.FlexLogger; +import org.onap.policy.common.logging.flexlogger.Logger; + +/** + * class IntegrityAudit + * Audits all persisted entities for all resource clusters for all sites and logs any anomalies. + */ +public class IntegrityAudit { + + private static final Logger logger = FlexLogger.getLogger(IntegrityAudit.class); + + public static boolean isUnitTesting; + private boolean isThreadInitialized = false; + + AuditThread auditThread = null; + + private String persistenceUnit; + private Properties properties; + private String resourceName; + + + /* + * This is the audit period in seconds. For example, if it had a value of 3600, the audit + * can only run once per hour. If it has a value of 60, it can run once per minute. + * + * Values: + * integrityAuditPeriodSeconds < 0 (negative number) indicates the audit is off + * integrityAuditPeriodSecconds == 0 indicates the audit is to run continuously + * integrityAuditPeriodSeconds > 0 indicates the audit is to run at most once during the indicated period + * + */ + private int integrityAuditPeriodSeconds; + + /** + * IntegrityAudit constructor + * @param resourceName + * @param persistenceUnit + * @param properties + * @throws Exception + */ + public IntegrityAudit(String resourceName, String persistenceUnit, Properties properties) throws Exception { + + logger.info("Constructor: Entering and checking for nulls"); + String parmList = ""; + if (parmsAreBad(resourceName, persistenceUnit, properties, parmList)) { + logger.error("Constructor: Parms contain nulls; cannot run audit for resourceName=" + + resourceName + ", persistenceUnit=" + persistenceUnit + + ", bad parameters: " + parmList); + throw new Exception( + "Constructor: Parms contain nulls; cannot run audit for resourceName=" + + resourceName + ", persistenceUnit=" + + persistenceUnit + + ", bad parameters: " + parmList); + } + + this.persistenceUnit = persistenceUnit; + this.properties = properties; + this.resourceName = resourceName; + + if(properties.getProperty(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS) != null){ //It is allowed to be null + this.integrityAuditPeriodSeconds= Integer.parseInt(properties.getProperty(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS).trim()); + } else{ + //If it is null, set it to the default value + this.integrityAuditPeriodSeconds = IntegrityAuditProperties.DEFAULT_AUDIT_PERIOD_SECONDS; + } + logger.info("Constructor: Exiting"); + + } + + /** + * Used during JUnit testing by AuditPeriodTest.java + */ + public int getIntegrityAuditPeriodSeconds() { + return integrityAuditPeriodSeconds; + } + + /** + * Determine if the nodeType conforms to the required node types + */ + public static boolean isNodeTypeEnum(String nt) { + for (NodeTypeEnum n : NodeTypeEnum.values()) { + if (n.toString().equals(nt)) { + return true; + } + } + return false; + } + + + /** + * Makes sure we don't try to run the audit with bad parameters. + */ + public static boolean parmsAreBad(String resourceName, String persistenceUnit, + Properties properties, String badparams) { + + boolean parmsAreBad = false; + + if(resourceName == null || resourceName.isEmpty()){ + badparams = badparams.concat("resourceName "); + parmsAreBad = true; + } + + if(persistenceUnit == null || persistenceUnit.isEmpty()){ + badparams = badparams.concat("persistenceUnit "); + parmsAreBad = true; + } + + String dbDriver = properties.getProperty(IntegrityAuditProperties.DB_DRIVER).trim(); + if(dbDriver == null || dbDriver.isEmpty()){ + badparams = badparams.concat("dbDriver "); + parmsAreBad = true; + } + + String dbUrl = properties.getProperty(IntegrityAuditProperties.DB_URL).trim(); + if(dbUrl == null || dbUrl.isEmpty()){ + badparams = badparams.concat("dbUrl "); + parmsAreBad = true; + } + + String dbUser = properties.getProperty(IntegrityAuditProperties.DB_USER).trim(); + if(dbUser == null || dbUser.isEmpty()){ + badparams = badparams.concat("dbUser "); + parmsAreBad = true; + } + + String dbPwd = properties.getProperty(IntegrityAuditProperties.DB_PWD).trim(); + if(dbPwd == null){ //may be empty + badparams = badparams.concat("dbPwd "); + parmsAreBad = true; + } + + String siteName = properties.getProperty(IntegrityAuditProperties.SITE_NAME).trim(); + if(siteName == null || siteName.isEmpty()){ + badparams = badparams.concat("siteName "); + parmsAreBad = true; + } + + String nodeType = properties.getProperty(IntegrityAuditProperties.NODE_TYPE).trim(); + if(nodeType == null || nodeType.isEmpty()){ + badparams = badparams.concat("nodeType "); + parmsAreBad = true; + } else { + if (!isNodeTypeEnum(nodeType)) { + String nodetypes = "nodeType must be one of["; + for (NodeTypeEnum n : NodeTypeEnum.values()) { + nodetypes = nodetypes.concat(n.toString() + " "); + } + badparams = badparams.concat(nodetypes + "] "); + parmsAreBad = true; + } + } + if(properties.getProperty(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS) != null){ //It is allowed to be null + try{ + Integer.parseInt(properties.getProperty(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS).trim()); + }catch(NumberFormatException nfe){ + badparams = badparams.concat(", auditPeriodSeconds=" + + properties.getProperty(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS).trim()); + parmsAreBad = true; + } + } + logger.debug("parmsAreBad: exit:" + + "\nresourceName: " + resourceName + + "\npersistenceUnit: " + persistenceUnit + + "\nproperties: " + properties); + + return parmsAreBad; + } + /** + * Starts the audit thread + * @throws Exception + */ + public void startAuditThread() throws Exception { + + logger.info("startAuditThread: Entering"); + + if (integrityAuditPeriodSeconds >= 0) { + this.auditThread = new AuditThread(this.resourceName, + this.persistenceUnit, this.properties, + integrityAuditPeriodSeconds, this); + logger.info("startAuditThread: Audit started and will run every " + + integrityAuditPeriodSeconds + " seconds"); + this.auditThread.start(); + } else { + logger.info("startAuditThread: Suppressing integrity audit, integrityAuditPeriodSeconds=" + + integrityAuditPeriodSeconds); + } + + logger.info("startAuditThread: Exiting"); + } + /** + * Stops the audit thread + */ + public void stopAuditThread() { + + logger.info("stopAuditThread: Entering"); + + if (this.auditThread != null) { + this.auditThread.interrupt(); + } else { + logger.info("stopAuditThread: auditThread never instantiated; no need to interrupt"); + } + + logger.info("stopAuditThread: Exiting"); + } + + public boolean isThreadInitialized() { + return isThreadInitialized; + } + + public void setThreadInitialized(boolean isThreadInitialized) { + logger.info("setThreadInitialized: Setting isThreadInitialized=" + isThreadInitialized); + this.isThreadInitialized = isThreadInitialized; + } +} diff --git a/integrity-audit/src/main/java/org/onap/policy/common/ia/IntegrityAuditProperties.java b/integrity-audit/src/main/java/org/onap/policy/common/ia/IntegrityAuditProperties.java new file mode 100644 index 00000000..3f87cf26 --- /dev/null +++ b/integrity-audit/src/main/java/org/onap/policy/common/ia/IntegrityAuditProperties.java @@ -0,0 +1,54 @@ +/*- + * ============LICENSE_START======================================================= + * Integrity Audit + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.common.ia; + +public class IntegrityAuditProperties { + + public static final String DEFAULT_DB_DRIVER = "org.h2.Driver"; + public static final String DEFAULT_DB_URL = "jdbc:h2:file:./sql/iaTest"; + public static final String DEFAULT_DB_USER = "sa"; + public static final String DEFAULT_DB_PWD = ""; + public static final int DEFAULT_AUDIT_PERIOD_SECONDS = -1; // Audit does not run + + public static final String DB_DRIVER = "javax.persistence.jdbc.driver"; + public static final String DB_URL = "javax.persistence.jdbc.url"; + public static final String DB_USER = "javax.persistence.jdbc.user"; + public static final String DB_PWD = "javax.persistence.jdbc.password"; + public static final String AUDIT_PERIOD_SECONDS = "integrity_audit_period_seconds"; + + + public static final String SITE_NAME = "site_name"; + public static final String NODE_TYPE = "node_type"; + + public static enum NodeTypeEnum { + pdp_xacml, + pdp_drools, + pap, + pap_admin, + logparser, + brms_gateway, + astra_gateway, + elk_server, + pypdp + + } + +} diff --git a/integrity-audit/src/main/java/org/onap/policy/common/ia/IntegrityAuditPropertiesException.java b/integrity-audit/src/main/java/org/onap/policy/common/ia/IntegrityAuditPropertiesException.java new file mode 100644 index 00000000..b62e9064 --- /dev/null +++ b/integrity-audit/src/main/java/org/onap/policy/common/ia/IntegrityAuditPropertiesException.java @@ -0,0 +1,37 @@ +/*- + * ============LICENSE_START======================================================= + * Integrity Audit + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.common.ia; + +public class IntegrityAuditPropertiesException extends Exception{ + private static final long serialVersionUID = 1L; + public IntegrityAuditPropertiesException() { + } + public IntegrityAuditPropertiesException(String message) { + super(message); + } + + public IntegrityAuditPropertiesException(Throwable cause) { + super(cause); + } + public IntegrityAuditPropertiesException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/integrity-audit/src/main/java/org/onap/policy/common/ia/jpa/IntegrityAuditEntity.java b/integrity-audit/src/main/java/org/onap/policy/common/ia/jpa/IntegrityAuditEntity.java new file mode 100644 index 00000000..2b68a51d --- /dev/null +++ b/integrity-audit/src/main/java/org/onap/policy/common/ia/jpa/IntegrityAuditEntity.java @@ -0,0 +1,203 @@ +/*- + * ============LICENSE_START======================================================= + * Integrity Audit + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.common.ia.jpa; + +import java.io.Serializable; +import java.util.Date; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.NamedQueries; +import javax.persistence.NamedQuery; +import javax.persistence.PrePersist; +import javax.persistence.PreUpdate; +import javax.persistence.Table; +import javax.persistence.Temporal; +import javax.persistence.TemporalType; +/* + * The Entity class to for management of IntegrityAudits + */ + +@Entity +@Table(name="IntegrityAuditEntity") +@NamedQueries({ + @NamedQuery(name=" IntegrityAuditEntity.findAll", query="SELECT e FROM IntegrityAuditEntity e "), + @NamedQuery(name="IntegrityAuditEntity.deleteAll", query="DELETE FROM IntegrityAuditEntity WHERE 1=1") +}) + +public class IntegrityAuditEntity implements Serializable { + private static final long serialVersionUID = 1L; + + public static boolean isUnitTesting; + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + @Column(name="id") + private long id; + + @Column(name="persistenceUnit", nullable=false) + private String persistenceUnit; + + @Column(name="site", nullable=true) + private String site; + + @Column(name="nodeType", nullable=true) + private String nodeType; + + @Column(name="resourceName", nullable=false, unique=true) + private String resourceName; + + @Column(name="designated", nullable=true) + private boolean designated = false; + + @Column(name="jdbcDriver", nullable=false) + private String jdbcDriver; + + @Column(name="jdbcUrl", nullable=false) + private String jdbcUrl; + + @Column(name="jdbcUser", nullable=false) + private String jdbcUser; + + @Column(name="jdbcPassword", nullable=false) + private String jdbcPassword; + + @Temporal(TemporalType.TIMESTAMP) + @Column(name="createdDate", updatable=true) + private Date createdDate; + + @Temporal(TemporalType.TIMESTAMP) + @Column(name="lastUpdated") + private Date lastUpdated; + + + public IntegrityAuditEntity() { + } + + @PrePersist + public void prePersist() { + Date date = new Date(); + this.createdDate = date; + this.lastUpdated = date; + } + + @PreUpdate + public void preUpdate() { + if (!isUnitTesting) { + this.lastUpdated = new Date(); + } + } + + public long getId() { + return id; + } + + public String getPersistenceUnit() { + return persistenceUnit; + } + + public void setPersistenceUnit(String persistenceUnit) { + this.persistenceUnit = persistenceUnit; + } + + public String getSite() { + return site; + } + + public void setSite(String site) { + this.site = site; + } + + public String getNodeType() { + return nodeType; + } + + public void setNodeType(String nodeType) { + this.nodeType = nodeType; + } + + public String getResourceName() { + return resourceName; + } + + public void setResourceName(String resourceName) { + this.resourceName = resourceName; + } + + public boolean isDesignated() { + return designated; + } + + public void setDesignated(boolean designated) { + this.designated = designated; + } + + public String getJdbcDriver() { + return jdbcDriver; + } + + public void setJdbcDriver(String jdbcDriver) { + this.jdbcDriver = jdbcDriver; + } + + public String getJdbcUrl() { + return jdbcUrl; + } + + public void setJdbcUrl(String jdbcUrl) { + this.jdbcUrl = jdbcUrl; + } + + public String getJdbcUser() { + return jdbcUser; + } + + public void setJdbcUser(String jdbcUser) { + this.jdbcUser = jdbcUser; + } + + public String getJdbcPassword() { + return jdbcPassword; + } + + public void setJdbcPassword(String jdbcPassword) { + this.jdbcPassword = jdbcPassword; + } + + public Date getLastUpdated() { + return lastUpdated; + } + + public void setLastUpdated(Date lastUpdated) { + this.lastUpdated = lastUpdated; + } + + public Date getCreatedDate() { + return createdDate; + } + + public void setCreatedDate(Date created) { + this.createdDate = created; + } +} diff --git a/integrity-audit/src/main/java/org/openecomp/policy/common/ia/AuditThread.java b/integrity-audit/src/main/java/org/openecomp/policy/common/ia/AuditThread.java deleted file mode 100644 index 2319f211..00000000 --- a/integrity-audit/src/main/java/org/openecomp/policy/common/ia/AuditThread.java +++ /dev/null @@ -1,769 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Integrity Audit - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.openecomp.policy.common.ia; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.Date; -import java.util.List; -import java.util.Properties; -//import org.apache.log4j.Logger; - - -import org.openecomp.policy.common.ia.jpa.IntegrityAuditEntity; -import org.openecomp.policy.common.logging.eelf.MessageCodes; -import org.openecomp.policy.common.logging.flexlogger.FlexLogger; -import org.openecomp.policy.common.logging.flexlogger.Logger; - -/** - * AuditThread is the main thread for the IntegrityAudit - * - */ -public class AuditThread extends Thread { - - private static final Logger logger = FlexLogger.getLogger(AuditThread.class); - - /* - * Number of milliseconds that must elapse for audit to be considered - * complete. It's public for access by JUnit test logic. - */ - public static final long AUDIT_COMPLETION_INTERVAL = 30000; - - /* - * Number of iterations for audit simulation. - */ - public static final long AUDIT_SIMULATION_ITERATIONS = 3; - - /* - * Number of milliseconds to sleep between audit simulation iterations. It's - * public for access by JUnit test logic. - */ - public static final long AUDIT_SIMULATION_SLEEP_INTERVAL = 5000; - - /* - * Unless audit has already been run on this entity, number of milliseconds - * to sleep between audit thread iterations. If audit has already been run, - * we sleep integrityAuditPeriodMillis. - */ - private static final long AUDIT_THREAD_SLEEP_INTERVAL = 5000; - - /* - * DB access class. - */ - private DbDAO dbDAO; - - /* - * E.g. pdp_xacml - */ - private String nodeType; - - /* - * Persistence unit for which this audit is being run. - */ - private String persistenceUnit; - - /* - * Name of this resource - */ - private String resourceName; - - /* - * E.g. DB_DRIVER, SITE_NAME, NODE_TYPE - */ - private Properties properties; - - /* - * See IntegrityAudit class for usage. - */ - private int integrityAuditPeriodMillis; - - /* - * The containing IntegrityAudit instance - */ - private IntegrityAudit integrityAudit; - - /** - * AuditThread constructor - * @param resourceName - * @param persistenceUnit - * @param properties - * @param integrityAuditPeriodSeconds - * @param integrityAudit - * @throws Exception - */ - public AuditThread(String resourceName, String persistenceUnit, - Properties properties, int integrityAuditPeriodSeconds, IntegrityAudit integrityAudit) - throws Exception { - this.resourceName = resourceName; - this.persistenceUnit = persistenceUnit; - this.properties = properties; - this.integrityAuditPeriodMillis = integrityAuditPeriodSeconds * 1000; - this.integrityAudit = integrityAudit; - - /* - * The DbDAO Constructor registers this node in the IntegrityAuditEntity - * table. Each resource (node) inserts its own name, persistenceUnit, DB - * access properties and other pertinent properties in the table. This - * allows the audit on each node to compare its own version of the - * entities for the persistenceUnit in question with the versions from - * all other nodes of similar type. - */ - dbDAO = new DbDAO(this.resourceName, this.persistenceUnit, - this.properties); - this.nodeType = properties.getProperty(IntegrityAuditProperties.NODE_TYPE); - - } - - public void run() { - - logger.info("AuditThread.run: Entering"); - - try { - - /* - * Triggers change in designation, unless no other viable candidate. - */ - boolean auditCompleted = false; - - DbAudit dbAudit = new DbAudit(dbDAO); - - IntegrityAuditEntity entityCurrentlyDesignated = null; - IntegrityAuditEntity thisEntity = null; - integrityAudit.setThreadInitialized(true); // An exception will set - // it to false - - while (true) { - try{ - - /* - * It may have been awhile since we last cycled through this - * loop, so refresh the list of IntegrityAuditEntities. - */ - List integrityAuditEntityList = getIntegrityAuditEntityList(); - - /* - * We could've set entityCurrentlyDesignated as a side effect of - * getIntegrityAuditEntityList(), but then we would've had to - * make entityCurrentlyDesignated a class level attribute. Using - * this approach, we can keep it local to the run() method. - */ - entityCurrentlyDesignated = getEntityCurrentlyDesignated(integrityAuditEntityList); - - /* - * Need to refresh thisEntity each time through loop, because we - * need a fresh version of lastUpdated. - */ - thisEntity = getThisEntity(integrityAuditEntityList); - - /* - * If we haven't done the audit yet, note that we're current and - * see if we're designated. - */ - if (!auditCompleted) { - dbDAO.setLastUpdated(); - - /* - * If no current designation or currently designated node is - * stale, see if we're the next node to be designated. - */ - if (entityCurrentlyDesignated == null - || isStale(entityCurrentlyDesignated)) { - IntegrityAuditEntity designationCandidate = getDesignationCandidate(integrityAuditEntityList); - - /* - * If we're the next node to be designated, run the - * audit. - */ - if (designationCandidate.getResourceName().equals( - this.resourceName)) { - runAudit(dbAudit); - auditCompleted = true; - } else { - if (logger.isDebugEnabled()) { - logger.debug("AuditThread.run: designationCandidate, " - + designationCandidate - .getResourceName() - + ", not this entity, " - + thisEntity.getResourceName()); - } - } - - /* - * Application may have been stopped and restarted, in - * which case we might be designated but auditCompleted - * will have been reset to false, so account for this. - */ - } else if (thisEntity.getResourceName().equals( - entityCurrentlyDesignated.getResourceName())) { - - if (logger.isDebugEnabled()) { - logger.debug("AuditThread.run: Re-running audit for " - + thisEntity.getResourceName()); - } - runAudit(dbAudit); - auditCompleted = true; - - } else { - if (logger.isDebugEnabled()) { - logger.debug("AuditThread.run: Currently designated node, " - + entityCurrentlyDesignated - .getResourceName() - + ", not yet stale and not this node"); - } - } - - - /* - * Audit already completed on this node, so allow the node - * to go stale until twice the AUDIT_COMPLETION_PERIOD has - * elapsed. This should give plenty of time for another node - * (if another node is out there) to pick up designation. - */ - } else { - - auditCompleted = resetAuditCompleted(auditCompleted, - thisEntity); - - } - - /* - * If we've just run audit, sleep per the - * integrity_audit_period_seconds property, otherwise just sleep - * the normal interval. - */ - if (auditCompleted) { - - if (logger.isDebugEnabled()) { - logger.debug("AuditThread.run: Audit completed; resourceName=" - + this.resourceName - + " sleeping " - + integrityAuditPeriodMillis + "ms"); - } - Thread.sleep(integrityAuditPeriodMillis); - if (logger.isDebugEnabled()) { - logger.debug("AuditThread.run: resourceName=" - + this.resourceName + " awaking from " - + integrityAuditPeriodMillis + "ms sleep"); - } - - } else { - - if (logger.isDebugEnabled()) { - logger.debug("AuditThread.run: resourceName=" - + this.resourceName + ": Sleeping " - + AuditThread.AUDIT_THREAD_SLEEP_INTERVAL - + "ms"); - } - Thread.sleep(AuditThread.AUDIT_THREAD_SLEEP_INTERVAL); - if (logger.isDebugEnabled()) { - logger.debug("AuditThread.run: resourceName=" - + this.resourceName + ": Awaking from " - + AuditThread.AUDIT_THREAD_SLEEP_INTERVAL - + "ms sleep"); - } - - } - } catch (Exception e){ - String msg = "AuditThread.run loop - Exception thrown: " + e.getMessage() - + "; Will try audit again in " + (integrityAuditPeriodMillis/1000) + " seconds"; - logger.error(MessageCodes.EXCEPTION_ERROR, e, msg); - // Sleep and try again later - Thread.sleep(integrityAuditPeriodMillis); - continue; - } - - } - - } catch (Exception e) { - String msg = "AuditThread.run: Could not start audit loop. Exception thrown; message="+ e.getMessage(); - logger.error(MessageCodes.EXCEPTION_ERROR, e, msg); - integrityAudit.setThreadInitialized(false); - } - - logger.info("AuditThread.run: Exiting"); - } - - /* - * Used to create a list that is sorted lexicographically by resourceName. - */ - Comparator comparator = new Comparator() { - @Override - public int compare(final IntegrityAuditEntity r1, - final IntegrityAuditEntity r2) { - return r1.getResourceName().compareTo(r2.getResourceName()); - } - }; - - /** - * getDesignationCandidate() - * Using round robin algorithm, gets next candidate to be designated. Assumes - * list is sorted lexicographically by resourceName. - */ - private IntegrityAuditEntity getDesignationCandidate( - List integrityAuditEntityList) { - - //Note: assumes integrityAuditEntityList is already lexicographically sorted by resourceName - - if (logger.isDebugEnabled()) { - logger.debug("getDesignationCandidate: Entering, integrityAuditEntityList.size()=" - + integrityAuditEntityList.size()); - } - - IntegrityAuditEntity designationCandidate = null; - IntegrityAuditEntity thisEntity = null; - - int designatedEntityIndex = -1; - int entityIndex = 0; - int priorCandidateIndex = -1; - int subsequentCandidateIndex = -1; - - for (IntegrityAuditEntity integrityAuditEntity : integrityAuditEntityList) { - - if (logger.isDebugEnabled()) { - logIntegrityAuditEntity(integrityAuditEntity); - } - - if (integrityAuditEntity.getResourceName() - .equals(this.resourceName)) { - if (logger.isDebugEnabled()) { - logger.debug("getDesignationCandidate: thisEntity=" - + integrityAuditEntity.getResourceName()); - } - thisEntity = integrityAuditEntity; - } - - if (integrityAuditEntity.isDesignated()) { - if (logger.isDebugEnabled()) { - logger.debug("getDesignationCandidate: Currently designated entity resourceName=" - + integrityAuditEntity.getResourceName() - + ", persistenceUnit=" - + integrityAuditEntity.getPersistenceUnit() - + ", lastUpdated=" - + integrityAuditEntity.getLastUpdated() - + ", entityIndex=" + entityIndex); - } - designatedEntityIndex = entityIndex; - - /* - * Entity not currently designated - */ - } else { - - /* - * See if non-designated entity is stale. - */ - if (isStale(integrityAuditEntity)) { - - if (logger.isDebugEnabled()) { - logger.debug("getDesignationCandidate: Entity is stale; resourceName=" - + integrityAuditEntity.getResourceName() - + ", persistenceUnit=" - + integrityAuditEntity.getPersistenceUnit() - + ", lastUpdated=" - + integrityAuditEntity.getLastUpdated() - + ", entityIndex=" + entityIndex); - } - - /* - * Entity is current. - */ - } else { - - if (designatedEntityIndex == -1) { - - if (priorCandidateIndex == -1) { - if (logger.isDebugEnabled()) { - logger.debug("getDesignationCandidate: Prior candidate found, resourceName=" - + integrityAuditEntity - .getResourceName() - + ", persistenceUnit=" - + integrityAuditEntity - .getPersistenceUnit() - + ", lastUpdated=" - + integrityAuditEntity.getLastUpdated() - + ", entityIndex=" + entityIndex); - } - priorCandidateIndex = entityIndex; - } else { - if (logger.isDebugEnabled()) { - logger.debug("getDesignationCandidate: Prior entity current but prior candidate already found; resourceName=" - + integrityAuditEntity - .getResourceName() - + ", persistenceUnit=" - + integrityAuditEntity - .getPersistenceUnit() - + ", lastUpdated=" - + integrityAuditEntity.getLastUpdated() - + ", entityIndex=" + entityIndex); - } - } - } else { - if (subsequentCandidateIndex == -1) { - if (logger.isDebugEnabled()) { - logger.debug("getDesignationCandidate: Subsequent candidate found, resourceName=" - + integrityAuditEntity - .getResourceName() - + ", persistenceUnit=" - + integrityAuditEntity - .getPersistenceUnit() - + ", lastUpdated=" - + integrityAuditEntity.getLastUpdated() - + ", entityIndex=" + entityIndex); - } - subsequentCandidateIndex = entityIndex; - } else { - if (logger.isDebugEnabled()) { - logger.debug("getDesignationCandidate: Subsequent entity current but subsequent candidate already found; resourceName=" - + integrityAuditEntity - .getResourceName() - + ", persistenceUnit=" - + integrityAuditEntity - .getPersistenceUnit() - + ", lastUpdated=" - + integrityAuditEntity.getLastUpdated() - + ", entityIndex=" + entityIndex); - } - } - } - - } // end entity is current - - } // end entity not currently designated - - entityIndex++; - - } // end for loop - - /* - * Per round robin algorithm, if a current entity is found that is - * lexicographically after the currently designated entity, this entity - * becomes the designation candidate. If no current entity is found that - * is lexicographically after currently designated entity, we cycle back - * to beginning of list and pick the first current entity as the - * designation candidate. - */ - if (subsequentCandidateIndex != -1) { - designationCandidate = integrityAuditEntityList - .get(subsequentCandidateIndex); - if (logger.isDebugEnabled()) { - logger.debug("getDesignationCandidate: Exiting and returning subsequent designationCandidate=" - + designationCandidate.getResourceName()); - } - } else { - if (priorCandidateIndex != -1) { - designationCandidate = integrityAuditEntityList - .get(priorCandidateIndex); - if (logger.isDebugEnabled()) { - logger.debug("getDesignationCandidate: Exiting and returning prior designationCandidate=" - + designationCandidate.getResourceName()); - } - } else { - logger.debug("getDesignationCandidate: No subsequent or prior candidate found; designating thisEntity, resourceName=" - + thisEntity.getResourceName()); - designationCandidate = thisEntity; - } - } - - return designationCandidate; - - } - - /** - * getEntityCurrentlyDesignated() - * Returns entity that is currently designated. - * @param integrityAuditEntityList - * @return - */ - private IntegrityAuditEntity getEntityCurrentlyDesignated( - List integrityAuditEntityList) { - - if (logger.isDebugEnabled()) { - logger.debug("getEntityCurrentlyDesignated: Entering, integrityAuditEntityList.size=" - + integrityAuditEntityList.size()); - } - - IntegrityAuditEntity entityCurrentlyDesignated = null; - - for (IntegrityAuditEntity integrityAuditEntity : integrityAuditEntityList) { - - if (integrityAuditEntity.isDesignated()) { - if (logger.isDebugEnabled()) { - logger.debug("getEntityCurrentlyDesignated: Currently designated entity resourceName=" - + integrityAuditEntity.getResourceName() - + ", persistenceUnit=" - + integrityAuditEntity.getPersistenceUnit() - + ", lastUpdated=" - + integrityAuditEntity.getLastUpdated()); - } - entityCurrentlyDesignated = integrityAuditEntity; - } - - } // end for loop - - if (logger.isDebugEnabled()) { - if (entityCurrentlyDesignated != null) { - logger.debug("getEntityCurrentlyDesignated: Exiting and returning entityCurrentlyDesignated=" - + entityCurrentlyDesignated.getResourceName()); - } else { - logger.debug("getEntityCurrentlyDesignated: Exiting and returning entityCurrentlyDesignated=" - + entityCurrentlyDesignated); - } - } - return entityCurrentlyDesignated; - - } - - /** - * getIntegrityAuditEnityList gets the list of IntegrityAuditEntity - * @return - * @throws DbDaoTransactionException - */ - private List getIntegrityAuditEntityList() - throws DbDaoTransactionException { - - if (logger.isDebugEnabled()) { - logger.debug("getIntegrityAuditEntityList: Entering"); - } - - /* - * Get all records for this nodeType and persistenceUnit and then sort - * them lexicographically by resourceName. Get index of designated - * entity, if any. - */ - /* - * Sorted list of entities for a particular nodeType and - * persistenceUnit. - */ - List integrityAuditEntityList = new ArrayList(); - integrityAuditEntityList = dbDAO.getIntegrityAuditEntities( - this.persistenceUnit, this.nodeType); - int listSize = integrityAuditEntityList.size(); - if (logger.isDebugEnabled()) { - logger.debug("getIntegrityAuditEntityList: Got " + listSize - + " IntegrityAuditEntity records"); - } - Collections.sort((List) integrityAuditEntityList, - comparator); - - if (logger.isDebugEnabled()) { - logger.debug("getIntegrityAuditEntityList: Exiting and returning integrityAuditEntityList, size=" - + listSize); - } - return integrityAuditEntityList; - - } - - - /** - * Returns the IntegrityAuditEntity for this entity. - * @param integrityAuditEntityList - * @return - */ - private IntegrityAuditEntity getThisEntity( - List integrityAuditEntityList) { - - if (logger.isDebugEnabled()) { - logger.debug("getThisEntity: Entering, integrityAuditEntityList.size=" - + integrityAuditEntityList.size()); - } - - IntegrityAuditEntity thisEntity = null; - - for (IntegrityAuditEntity integrityAuditEntity : integrityAuditEntityList) { - - if (integrityAuditEntity.getResourceName().equals(this.resourceName)) { - if (logger.isDebugEnabled()) { - logger.debug("getThisEntity: For this entity, resourceName=" - + integrityAuditEntity.getResourceName() - + ", persistenceUnit=" - + integrityAuditEntity.getPersistenceUnit() - + ", lastUpdated=" - + integrityAuditEntity.getLastUpdated()); - } - thisEntity = integrityAuditEntity; - } - - } // end for loop - - if (logger.isDebugEnabled()) { - if (thisEntity != null) { - logger.debug("getThisEntity: Exiting and returning thisEntity=" - + thisEntity.getResourceName()); - } else { - logger.debug("getThisEntity: Exiting and returning thisEntity=" - + thisEntity); - } - } - return thisEntity; - - } - - - /** - * Returns false if the lastUpdated time for the record in question is more - * than AUDIT_COMPLETION_INTERVAL seconds ago. During an audit, lastUpdated is updated every five - * seconds or so, but when an audit finishes, the node doing the audit stops - * updating lastUpdated. - * @param integrityAuditEntity - * @return - */ - private boolean isStale(IntegrityAuditEntity integrityAuditEntity) { - - if (logger.isDebugEnabled()) { - logger.debug("isStale: Entering, resourceName=" - + integrityAuditEntity.getResourceName() - + ", persistenceUnit=" - + integrityAuditEntity.getPersistenceUnit() - + ", lastUpdated=" + integrityAuditEntity.getLastUpdated()); - } - - boolean stale = false; - - Date currentTime = new Date(); - Date lastUpdated = integrityAuditEntity.getLastUpdated(); - - /* - * If lastUpdated is null, we assume that the audit never ran for that - * node. - */ - long lastUpdatedTime = 0; - if (lastUpdated != null) { - lastUpdatedTime = lastUpdated.getTime(); - } - long timeDifference = currentTime.getTime() - lastUpdatedTime; - if (timeDifference > AUDIT_COMPLETION_INTERVAL) { - stale = true; - } - - if (logger.isDebugEnabled()) { - logger.debug("isStale: Exiting and returning stale=" + stale - + ", timeDifference=" + timeDifference); - } - - return stale; - } - - private void logIntegrityAuditEntity( - IntegrityAuditEntity integrityAuditEntity) { - - logger.debug("logIntegrityAuditEntity: id=" - + integrityAuditEntity.getId() + ", jdbcDriver=" - + integrityAuditEntity.getJdbcDriver() + ", jdbcPassword=" - + integrityAuditEntity.getJdbcPassword() + ", jdbcUrl=" - + integrityAuditEntity.getJdbcUrl() + ", jdbcUser=" - + integrityAuditEntity.getJdbcUser() + ", nodeType=" - + integrityAuditEntity.getNodeType() + ", persistenceUnit=" - + integrityAuditEntity.getPersistenceUnit() + ", resourceName=" - + integrityAuditEntity.getResourceName() + ", site=" - + integrityAuditEntity.getSite() + ", createdDate=" - + integrityAuditEntity.getCreatedDate() + ", lastUpdated=" - + integrityAuditEntity.getLastUpdated() + ", designated=" - + integrityAuditEntity.isDesignated()); - } - - /* - * If more than (AUDIT_COMPLETION_INTERVAL * 2) milliseconds have elapsed - * since we last ran the audit, reset auditCompleted, so - * - * 1) we'll eventually re-run the audit, if no other node picks up the - * designation. - * - * or - * - * 2) We'll run the audit when the round robin comes back to us. - */ - private boolean resetAuditCompleted(boolean auditCompleted, - IntegrityAuditEntity thisEntity) { - - if (logger.isDebugEnabled()) { - logger.debug("resetAuditCompleted: auditCompleted=" - + auditCompleted + "; for thisEntity, resourceName=" - + thisEntity.getResourceName() + ", persistenceUnit=" - + thisEntity.getPersistenceUnit() + ", lastUpdated=" - + thisEntity.getLastUpdated()); - } - - long timeDifference = -1; - - Date currentTime = new Date(); - Date lastUpdated = thisEntity.getLastUpdated(); - - long lastUpdatedTime = lastUpdated.getTime(); - timeDifference = currentTime.getTime() - lastUpdatedTime; - - if (timeDifference > (AUDIT_COMPLETION_INTERVAL * 2)) { - if (logger.isDebugEnabled()) { - logger.debug("resetAuditCompleted: Resetting auditCompleted for resourceName=" - + this.resourceName); - } - auditCompleted = false; - } else { - if (logger.isDebugEnabled()) { - logger.debug("resetAuditCompleted: For resourceName=" - + resourceName - + ", time since last update is only " - + timeDifference + "; retaining current value for auditCompleted"); - } - } - - if (logger.isDebugEnabled()) { - logger.debug("resetAuditCompleted: Exiting and returning auditCompleted=" - + auditCompleted + ", timeDifference=" + timeDifference); - } - return auditCompleted; - } - - private void runAudit(DbAudit dbAudit) throws Exception { - - if (logger.isDebugEnabled()) { - logger.debug("runAudit: Entering, dbAudit=" + dbAudit - + "; notifying other resources that resourceName=" - + this.resourceName + " is current"); - } - - /* - * changeDesignated marks all other nodes as non-designated and this - * node as designated. - */ - dbDAO.changeDesignated(this.resourceName, this.persistenceUnit, - this.nodeType); - - if (logger.isDebugEnabled()) { - logger.debug("runAudit: Running audit for persistenceUnit=" - + this.persistenceUnit + " on resourceName=" - + this.resourceName); - } - if (IntegrityAudit.isUnitTesting) { - dbAudit.dbAuditSimulate(this.resourceName, this.persistenceUnit, - this.nodeType); - } else { - dbAudit.dbAudit(this.resourceName, this.persistenceUnit, - this.nodeType); - } - - if (logger.isDebugEnabled()) { - logger.debug("runAudit: Exiting"); - } - - } - -} diff --git a/integrity-audit/src/main/java/org/openecomp/policy/common/ia/DbAudit.java b/integrity-audit/src/main/java/org/openecomp/policy/common/ia/DbAudit.java deleted file mode 100644 index b509d563..00000000 --- a/integrity-audit/src/main/java/org/openecomp/policy/common/ia/DbAudit.java +++ /dev/null @@ -1,462 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Integrity Audit - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.openecomp.policy.common.ia; - -import java.io.Serializable; -import java.util.Arrays; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Properties; - -import javax.persistence.Table; - -import org.apache.commons.lang3.SerializationUtils; -import org.apache.commons.lang3.builder.RecursiveToStringStyle; -import org.apache.commons.lang3.builder.ReflectionToStringBuilder; -import org.apache.commons.lang3.builder.ToStringStyle; -//import org.apache.log4j.Logger; - - - - - -import org.openecomp.policy.common.ia.jpa.IntegrityAuditEntity; -import org.openecomp.policy.common.logging.eelf.MessageCodes; -import org.openecomp.policy.common.logging.flexlogger.FlexLogger; -import org.openecomp.policy.common.logging.flexlogger.Logger; - -/** - * class DbAudit does actual auditing of DB tables. - */ -public class DbAudit { - - private static final Logger logger = FlexLogger.getLogger(DbAudit.class); - - DbDAO dbDAO = null; - - public DbAudit(DbDAO dbDAO) { - - if (logger.isDebugEnabled()) { - logger.debug("Constructor: Entering"); - } - - this.dbDAO = dbDAO; - - if (logger.isDebugEnabled()) { - logger.debug("Constructor: Exiting"); - } - - } - - /** - * dbAudit actually does the audit - * @param resourceName - * @param persistenceUnit - * @param nodeType - * @throws Exception - */ - public void dbAudit(String resourceName, String persistenceUnit, String nodeType) throws Exception { - - if (logger.isDebugEnabled()) { - logger.debug("dbAudit: Entering, resourceName=" + resourceName - + ", persistenceUnit=" + persistenceUnit + ", nodeType=" - + nodeType); - } - - // Get all IntegrityAudit entries so we can get the DB access info - List iaeList = dbDAO.getIntegrityAuditEntities(persistenceUnit, nodeType); - if(iaeList == null || iaeList.isEmpty()){ - - String msg = "DbAudit: for node " + resourceName + " Found no IntegrityAuditEntity entries"; - logger.error(MessageCodes.ERROR_AUDIT, msg); - throw new DbAuditException(msg); - - }else if(iaeList.size() == 1){ - - Long iaeId = null; - String iaeRN = null; - String iaeNT = null; - String iaeS = null; - for (IntegrityAuditEntity iae : iaeList){ - iaeId = iae.getId(); - iaeRN = iae.getResourceName(); - iaeNT = iae.getNodeType(); - iaeS = iae.getSite(); - } - String msg = "DbAudit: Found only one IntegrityAuditEntity entry:" - + " ID = " + iaeId - + " ResourceName = " + iaeRN - + " NodeType = " + iaeNT - + " Site = " + iaeS; - logger.warn(msg); - return; - } - - // Obtain all persistence class names for the PU we are auditing - HashSet classNameSet = dbDAO.getPersistenceClassNames(); - if(classNameSet == null || classNameSet.isEmpty()){ - - String msg = "DbAudit: For node " + resourceName + " Found no persistence class names"; - logger.error(MessageCodes.ERROR_AUDIT, msg); - throw new DbAuditException(msg); - - } - - /* - * Retrieve myIae. We are going to compare the local class entries against - * all other DB nodes. Since the audit is run in a round-robin, every instance - * will be compared against every other instance. - */ - IntegrityAuditEntity myIae = dbDAO.getMyIntegrityAuditEntity(); - - if(myIae == null){ - - String msg = "DbAudit: Found no IntegrityAuditEntity entry for resourceName: " + resourceName - + " persistenceUnit: " + persistenceUnit; - logger.error(MessageCodes.ERROR_AUDIT, msg); - throw new DbAuditException(msg); - - } - /* - * This is the map of mismatched entries indexed by className. For - * each class name there is a list of mismatched entries - */ - HashMap> misMatchedMap = new HashMap>(); - - // We need to keep track of how long the audit is taking - long startTime = System.currentTimeMillis(); - - // Retrieve all instances of the class for each node - if (logger.isDebugEnabled()) { - logger.debug("dbAudit: Traversing classNameSet, size=" + classNameSet.size()); - } - for(String clazzName: classNameSet){ - - if (logger.isDebugEnabled()) { - logger.debug("dbAudit: clazzName=" + clazzName); - } - - // all instances of the class for myIae - HashMap myEntries = dbDAO.getAllMyEntries(clazzName); - //get a map of the objects indexed by id. Does not necessarily have any entries - - if (logger.isDebugEnabled()) { - logger.debug("dbAudit: Traversing iaeList, size=" + iaeList.size()); - } - for (IntegrityAuditEntity iae : iaeList){ - if(iae.getId() == myIae.getId()){ - if (logger.isDebugEnabled()) { - logger.debug("dbAudit: My Id=" + iae.getId() - + ", resourceName=" + iae.getResourceName()); - } - continue; //no need to compare with self - } else { - if (logger.isDebugEnabled()) { - logger.debug("dbAudit: Id=" + iae.getId() - + ", resourceName=" + iae.getResourceName()); - } - } - // Create properties for the other db node - Properties theirProperties = new Properties(); - theirProperties.put(IntegrityAuditProperties.DB_DRIVER, iae.getJdbcDriver()); - theirProperties.put(IntegrityAuditProperties.DB_URL, iae.getJdbcUrl()); - theirProperties.put(IntegrityAuditProperties.DB_USER, iae.getJdbcUser()); - theirProperties.put(IntegrityAuditProperties.DB_PWD, iae.getJdbcPassword()); - theirProperties.put(IntegrityAuditProperties.SITE_NAME, iae.getSite()); - theirProperties.put(IntegrityAuditProperties.NODE_TYPE, iae.getNodeType()); - - //get a map of the instances for their iae indexed by id - HashMap theirEntries = dbDAO.getAllEntries(persistenceUnit, theirProperties, clazzName); - if (logger.isDebugEnabled()) { - logger.debug("dbAudit: For persistenceUnit=" - + persistenceUnit + ", clazzName=" + clazzName - + ", theirEntries.size()=" - + theirEntries.size()); - } - - /* - * Compare myEntries with theirEntries and get back a set of mismatched IDs. - * Collect the IDs for the class where a mismatch occurred. We will check - * them again for all nodes later. - */ - HashSet misMatchedKeySet = compareEntries(myEntries, theirEntries); - if(!misMatchedKeySet.isEmpty()){ - HashSet misMatchedEntry = misMatchedMap.get(clazzName); - if(misMatchedEntry == null){ - misMatchedMap.put(clazzName, misMatchedKeySet); - }else{ - misMatchedEntry.addAll(misMatchedKeySet); - misMatchedMap.put(clazzName, misMatchedEntry); - } - } - } //end for (IntegrityAuditEntity iae : iaeList) - //Time check - if((System.currentTimeMillis() - startTime) >= 5000){ //5 seconds - //update the timestamp - dbDAO.setLastUpdated(); - //reset the startTime - startTime=System.currentTimeMillis(); - }else{ - //sleep a couple seconds to break up the activity - if (logger.isDebugEnabled()) { - logger.debug("dbAudit: Sleeping 2 seconds"); - } - Thread.sleep(2000); - if (logger.isDebugEnabled()) { - logger.debug("dbAudit: Waking from sleep"); - } - } - }//end: for(String clazzName: classNameList) - - //check if misMatchedMap is empty - if(misMatchedMap.isEmpty()){ - - if (logger.isDebugEnabled()) { - logger.debug("dbAudit: Exiting, misMatchedMap is empty"); - } - //we are done - return; - } else { - if (logger.isDebugEnabled()) { - logger.debug("dbAudit: Doing another comparison; misMatchedMap.size()=" + misMatchedMap.size()); - } - } - - // If misMatchedMap is not empty, retrieve the entries in each misMatched list and compare again - classNameSet = new HashSet(misMatchedMap.keySet()); - // We need to keep track of how long the audit is taking - startTime = System.currentTimeMillis(); - - // Retrieve all instances of the class for each node - if (logger.isDebugEnabled()) { - logger.debug("dbAudit: Second comparison; traversing classNameSet, size=" + classNameSet.size()); - } - - int errorCount = 0; - - for(String clazzName: classNameSet){ - - if (logger.isDebugEnabled()) { - logger.debug("dbAudit: Second comparison; clazzName=" + clazzName); - } - - // all instances of the class for myIae - HashSet keySet = misMatchedMap.get(clazzName); - HashMap myEntries = dbDAO.getAllMyEntries(clazzName, keySet); - //get a map of the objects indexed by id - - if (logger.isDebugEnabled()) { - logger.debug("dbAudit: Second comparison; traversing iaeList, size=" + iaeList.size()); - } - for (IntegrityAuditEntity iae : iaeList){ - if(iae.getId() == myIae.getId()){ - if (logger.isDebugEnabled()) { - logger.debug("dbAudit: Second comparison; My Id=" + iae.getId() - + ", resourceName=" + iae.getResourceName()); - } - continue; //no need to compare with self - } else { - if (logger.isDebugEnabled()) { - logger.debug("dbAudit: Second comparison; Id=" + iae.getId() - + ", resourceName=" + iae.getResourceName()); - } - } - // Create properties for the other db node - Properties theirProperties = new Properties(); - theirProperties.put(IntegrityAuditProperties.DB_DRIVER, iae.getJdbcDriver()); - theirProperties.put(IntegrityAuditProperties.DB_URL, iae.getJdbcUrl()); - theirProperties.put(IntegrityAuditProperties.DB_USER, iae.getJdbcUser()); - theirProperties.put(IntegrityAuditProperties.DB_PWD, iae.getJdbcPassword()); - theirProperties.put(IntegrityAuditProperties.SITE_NAME, iae.getSite()); - theirProperties.put(IntegrityAuditProperties.NODE_TYPE, iae.getNodeType()); - - //get a map of the instances for their iae indexed by id - HashMap theirEntries = dbDAO.getAllEntries(persistenceUnit, theirProperties, clazzName, keySet); - - /* - * Compare myEntries with theirEntries and get back a set of mismatched IDs. - * Collect the IDs for the class where a mismatch occurred. We will now - * write an error log for each. - */ - HashSet misMatchedKeySet = compareEntries(myEntries, theirEntries); - if(!misMatchedKeySet.isEmpty()){ - String keysString = ""; - for(Object key: misMatchedKeySet){ - keysString = keysString.concat(key.toString() + ", "); - errorCount ++; - } - writeAuditSummaryLog(clazzName, resourceName, iae.getResourceName(), keysString); - if(logger.isDebugEnabled()){ - for(Object key : misMatchedKeySet){ - writeAuditDebugLog(clazzName, resourceName, iae.getResourceName(), myEntries.get(key), theirEntries.get(key)); - } - } - } - } - //Time check - if((System.currentTimeMillis() - startTime) >= 5000){ //5 seconds - //update the timestamp - dbDAO.setLastUpdated(); - //reset the startTime - startTime=System.currentTimeMillis(); - }else{ - //sleep a couple seconds to break up the activity - if (logger.isDebugEnabled()) { - logger.debug("dbAudit: Second comparison; sleeping 2 seconds"); - } - Thread.sleep(2000); - if (logger.isDebugEnabled()) { - logger.debug("dbAudit: Second comparison; waking from sleep"); - } - } - }//end: for(String clazzName: classNameList) - - if(errorCount != 0){ - String msg = " DB Audit: " + errorCount + " errors found. A large number of errors may indicate DB replication has stopped"; - logger.error(MessageCodes.ERROR_AUDIT, msg); - } - - if (logger.isDebugEnabled()) { - logger.debug("dbAudit: Exiting"); - } - - return; //all done - } - - /** - * dbAuditSimulate simulates the DB audit - * @param resourceName - * @param persistenceUnit - * @param nodeType - * @throws InterruptedException - * @throws DbDaoTransactionException - */ - public void dbAuditSimulate(String resourceName, String persistenceUnit, - String nodeType) throws InterruptedException, - DbDaoTransactionException { - - logger.info("dbAuditSimulate: Starting audit simulation for resourceName=" - + resourceName + ", persistenceUnit=" + persistenceUnit); - - for (int i = 0; i < AuditThread.AUDIT_SIMULATION_ITERATIONS; i++) { - dbDAO.setLastUpdated(); - logger.info("dbAuditSimulate: i=" + i + ", sleeping " - + AuditThread.AUDIT_SIMULATION_SLEEP_INTERVAL + "ms"); - Thread.sleep(AuditThread.AUDIT_SIMULATION_SLEEP_INTERVAL); - } - - logger.info("dbAuditSimulate: Finished audit simulation for resourceName=" - + resourceName + ", persistenceUnit=" + persistenceUnit); - - } - - /** - * compareEntries() will compare the lists of entries from the DB - * @param myEntries - * @param theirEntries - * @return - */ - public HashSet compareEntries(HashMap myEntries, HashMap theirEntries){ - /* - * Compare the entries for the same key in each of the hashmaps. The comparison will be done by serializing the objects - * (create a byte array) and then do a byte array comparison. The audit will walk the local repository hash map comparing - * to the remote cluster hashmap and then turn it around and walk the remote hashmap and look for any entries that are not - * present in the local cluster hashmap. - * - * If the objects are not identical, the audit will put the object IDs on a list to try after completing the audit of the table - * it is currently working on. - * - */ - HashSet misMatchedKeySet = new HashSet(); - for(Object key: myEntries.keySet()){ - byte[] mySerializedEntry = SerializationUtils.serialize((Serializable) myEntries.get(key)); - byte[] theirSerializedEntry = SerializationUtils.serialize((Serializable) theirEntries.get(key)); - if(!Arrays.equals(mySerializedEntry, theirSerializedEntry)){ - logger.debug("compareEntries: For myEntries.key=" + key + ", entries do not match"); - misMatchedKeySet.add(key); - } else { - logger.debug("compareEntries: For myEntries.key=" + key + ", entries match"); - } - } - //now compare it in the other direction to catch entries in their set that is not in my set - for(Object key: theirEntries.keySet()){ - byte[] mySerializedEntry = SerializationUtils.serialize((Serializable) myEntries.get(key)); - byte[] theirSerializedEntry = SerializationUtils.serialize((Serializable) theirEntries.get(key)); - if(!Arrays.equals(mySerializedEntry, theirSerializedEntry)){ - logger.debug("compareEntries: For theirEntries.key=" + key + ", entries do not match"); - misMatchedKeySet.add(key); - } else { - logger.debug("compareEntries: For theirEntries.key=" + key + ", entries match"); - } - } - - //return a Set of the object IDs - logger.debug("compareEntries: misMatchedKeySet.size()=" + misMatchedKeySet.size()); - return misMatchedKeySet; - } - - /** - * writeAuditDebugLog() writes the mismatched entry details to the debug log - * @param clazzName - * @param resourceName1 - * @param resourceName2 - * @param entry1 - * @param entry2 - * @throws ClassNotFoundException - */ - public void writeAuditDebugLog(String clazzName, String resourceName1, - String resourceName2, Object entry1, Object entry2) throws ClassNotFoundException{ - Class entityClass = Class.forName(clazzName); - String tableName = entityClass.getAnnotation(Table.class).name(); - String msg = "\nDB Audit Error: " - + "\n Table Name: " + tableName - + "\n Entry 1 (short prefix style): " + resourceName1 + ": " + new ReflectionToStringBuilder(entry1,ToStringStyle.SHORT_PREFIX_STYLE).toString() - + "\n Entry 2 (short prefix style): " + resourceName2 + ": " + new ReflectionToStringBuilder(entry2,ToStringStyle.SHORT_PREFIX_STYLE).toString() - + "\n Entry 1 (recursive style): " + resourceName1 + ": " + new ReflectionToStringBuilder(entry1, new RecursiveToStringStyle()).toString() - + "\n Entry 2 (recursive style): " + resourceName2 + ": " + new ReflectionToStringBuilder(entry2, new RecursiveToStringStyle()).toString(); - logger.debug(msg); - - } - - /** - * writeAuditSummaryLog() writes a summary of the DB mismatches to the error log - * @param clazzName - * @param resourceName1 - * @param resourceName2 - * @param keys - * @throws ClassNotFoundException - */ - public void writeAuditSummaryLog(String clazzName, String resourceName1, - String resourceName2, String keys) throws ClassNotFoundException{ - Class entityClass = Class.forName(clazzName); - String tableName = entityClass.getAnnotation(Table.class).name(); - String msg = " DB Audit Error: Table Name: " + tableName - + "; Mismatch between nodes: " + resourceName1 +" and " + resourceName2 - + "; Mismatched entries (keys): " + keys; - logger.info(msg); - } - - - - - -} diff --git a/integrity-audit/src/main/java/org/openecomp/policy/common/ia/DbAuditException.java b/integrity-audit/src/main/java/org/openecomp/policy/common/ia/DbAuditException.java deleted file mode 100644 index 956dd431..00000000 --- a/integrity-audit/src/main/java/org/openecomp/policy/common/ia/DbAuditException.java +++ /dev/null @@ -1,37 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Integrity Audit - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.openecomp.policy.common.ia; - -public class DbAuditException extends Exception{ - private static final long serialVersionUID = 1L; - public DbAuditException() { - } - public DbAuditException(String message) { - super(message); - } - - public DbAuditException(Throwable cause) { - super(cause); - } - public DbAuditException(String message, Throwable cause) { - super(message, cause); - } -} diff --git a/integrity-audit/src/main/java/org/openecomp/policy/common/ia/DbDAO.java b/integrity-audit/src/main/java/org/openecomp/policy/common/ia/DbDAO.java deleted file mode 100644 index 5ab16d58..00000000 --- a/integrity-audit/src/main/java/org/openecomp/policy/common/ia/DbDAO.java +++ /dev/null @@ -1,739 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Integrity Audit - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.openecomp.policy.common.ia; - -import java.util.Date; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Properties; - -import javax.persistence.EntityManager; -import javax.persistence.EntityManagerFactory; -import javax.persistence.EntityTransaction; -import javax.persistence.LockTimeoutException; -import javax.persistence.Persistence; -import javax.persistence.PersistenceUnitUtil; -import javax.persistence.Query; -import javax.persistence.TypedQuery; -import javax.persistence.criteria.CriteriaBuilder; -import javax.persistence.criteria.CriteriaQuery; -import javax.persistence.criteria.Root; -import javax.persistence.metamodel.ManagedType; -import javax.persistence.metamodel.Metamodel; - -//import org.apache.log4j.Logger; - -import org.openecomp.policy.common.ia.jpa.IntegrityAuditEntity; -import org.openecomp.policy.common.logging.flexlogger.FlexLogger; -import org.openecomp.policy.common.logging.flexlogger.Logger; - -/** - * class DbDAO provides the inteface to the DBs for the purpose of audits. - * - */ -public class DbDAO { - private static final Logger logger = FlexLogger.getLogger(DbDAO.class.getName()); - private String resourceName; - private String persistenceUnit; - private String dbDriver; - private String dbUrl; - private String dbUser; - private String dbPwd; - private String siteName; - private String nodeType; - private Properties properties=null; - - private EntityManagerFactory emf; - - /* - * Supports designation serialization. - */ - private static final Object lock = new Object(); - - - /** - * DbDAO Constructor - * @param resourceName - * @param persistenceUnit - * @param properties - * @throws Exception - */ - public DbDAO(String resourceName, String persistenceUnit, Properties properties) throws Exception { - logger.debug("DbDAO contructor: enter"); - - validateProperties(resourceName, persistenceUnit, properties); - - emf = Persistence.createEntityManagerFactory(persistenceUnit, properties); - - register(); - - logger.debug("DbDAO contructor: exit"); - } - - /** - * validateProperties will validate the properties - * @param resourceName - * @param persistenceUnit - * @param properties - * @throws IntegrityAuditPropertiesException - */ - private void validateProperties(String resourceName, String persistenceUnit, Properties properties) throws IntegrityAuditPropertiesException{ - String badparams=""; - if(IntegrityAudit.parmsAreBad(resourceName, persistenceUnit, properties, badparams)){ - String msg = "DbDAO: Bad parameters: badparams" + badparams; - throw new IntegrityAuditPropertiesException(msg); - } - this.resourceName = resourceName; - this.persistenceUnit = persistenceUnit; - this.dbDriver = properties.getProperty(IntegrityAuditProperties.DB_DRIVER).trim(); - this.dbUrl = properties.getProperty(IntegrityAuditProperties.DB_URL).trim(); - this.dbUser = properties.getProperty(IntegrityAuditProperties.DB_USER).trim(); - this.dbPwd = properties.getProperty(IntegrityAuditProperties.DB_PWD).trim(); - this.siteName = properties.getProperty(IntegrityAuditProperties.SITE_NAME).trim(); - this.nodeType = properties.getProperty(IntegrityAuditProperties.NODE_TYPE).trim(); - this.properties = properties; - logger.debug("DbDAO.assignProperties: exit:" - + "\nresourceName: " + this.resourceName - + "\npersistenceUnit: " + this.persistenceUnit - + "\nproperties: " + this.properties); - } - - /** - * getAllMyEntries gets all the DB entries for a particular class - * @param className - * @return - */ - public HashMap getAllMyEntries(String className) { - logger.debug("getAllMyEntries: Entering, className=" - + className); - HashMap resultMap = new HashMap(); - EntityManager em = emf.createEntityManager(); - try{ - CriteriaBuilder cb = em.getCriteriaBuilder(); - CriteriaQuery cq = cb.createQuery(); - Root rootEntry = cq.from(Class.forName(className)); - CriteriaQuery all = cq.select(rootEntry); - TypedQuery allQuery = em.createQuery(all); - List objectList = allQuery.getResultList(); - //Now create the map - - PersistenceUnitUtil util = emf.getPersistenceUnitUtil(); - for (Object o: objectList){ - Object key = util.getIdentifier(o); - resultMap.put(key, o); - } - }catch(Exception e){ - String msg = "getAllEntries encountered exception: " + e; - logger.error(msg); - System.out.println(new Date()); - e.printStackTrace(); - } - em.close(); - logger.debug("getAllMyEntries: Exit, resultMap.keySet()=" + resultMap.keySet()); - return resultMap; - } - - /** - * getAllMyEntries gets all entries for a class - * @param className - * @param keySet - * @return - */ - public HashMap getAllMyEntries(String className, HashSet keySet){ - logger.debug("getAllMyEntries: Entering, className=" - + className + ",\n keySet=" + keySet); - - HashMap resultMap = new HashMap(); - EntityManager em = emf.createEntityManager(); - try{ - Class clazz = Class.forName(className); - for(Object key : keySet){ - Object entry = em.find(clazz, key); - resultMap.put(key, entry); - } - }catch(Exception e){ - String msg = "getAllMyEntries encountered exception: " + e; - logger.error(msg); - System.out.println(new Date()); - e.printStackTrace(); - } - em.close(); - - logger.debug("getAllMyEntries: Returning resultMap, size=" + resultMap.size()); - return resultMap; - } - - /** - * getAllEntries gets all entriesfor a particular persistence unit adn className - * @param persistenceUnit - * @param properties - * @param className - * @return - */ - public HashMap getAllEntries(String persistenceUnit, Properties properties, String className){ - - logger.debug("getAllEntries: Entering, persistenceUnit=" - + persistenceUnit + ",\n className=" + className); - HashMap resultMap = new HashMap(); - - EntityManagerFactory theEmf = Persistence.createEntityManagerFactory(persistenceUnit, properties); - EntityManager em = theEmf.createEntityManager(); - try{ - CriteriaBuilder cb = em.getCriteriaBuilder(); - CriteriaQuery cq = cb.createQuery(); - Root rootEntry = cq.from(Class.forName(className)); - CriteriaQuery all = cq.select(rootEntry); - TypedQuery allQuery = em.createQuery(all); - List objectList = allQuery.getResultList(); - - PersistenceUnitUtil util = theEmf.getPersistenceUnitUtil(); - for (Object o: objectList){ - Object key = util.getIdentifier(o); - resultMap.put(key, o); - } - }catch(Exception e){ - String msg = "getAllEntries encountered exception:" + e; - logger.error(msg); - System.out.println(new Date()); - e.printStackTrace(); - } - em.close(); - - logger.debug("getAllEntries: Returning resultMap, size=" + resultMap.size()); - - return resultMap; - } - - - /** - * getAllEntries gest all entries for a persistence unit - * @param persistenceUnit - * @param properties - * @param className - * @param keySet - * @return - */ - - public HashMap getAllEntries(String persistenceUnit, Properties properties, String className, HashSet keySet){ - logger.debug("getAllEntries: Entering, persistenceUnit=" - + persistenceUnit + ",\n properties= " + properties + ",\n className=" + className + ",\n keySet= " + keySet); - EntityManagerFactory theEmf = Persistence.createEntityManagerFactory(persistenceUnit, properties); - EntityManager em = theEmf.createEntityManager(); - HashMap resultMap = new HashMap(); - try{ - Class clazz = Class.forName(className); - for(Object key : keySet){ - Object entry = em.find(clazz, key); - resultMap.put(key, entry); - } - }catch(Exception e){ - String msg = "getAllEntries encountered exception: " + e; - logger.error(msg); - System.out.println(new Date()); - e.printStackTrace(); - } - em.close(); - logger.debug("getAllEntries: Exit, resultMap, size=" + resultMap.size()); - return resultMap; - } - - /** - * getIntegrityAuditEntities() Get all the IntegrityAuditEntities for a particular persistence unit - * and node type - * @param persistenceUnit - * @param nodeType - * @return - * @throws DbDaoTransactionException - */ - @SuppressWarnings("unchecked") - public List getIntegrityAuditEntities(String persistenceUnit, String nodeType) throws DbDaoTransactionException { - logger.debug("getIntegrityAuditEntities: Entering, persistenceUnit=" - + persistenceUnit + ",\n nodeType= " + nodeType); - try{ - EntityManager em = emf.createEntityManager(); - // Start a transaction - EntityTransaction et = em.getTransaction(); - - et.begin(); - - // if IntegrityAuditEntity entry exists for resourceName and PU, update it. If not found, create a new entry - Query iaequery = em.createQuery("Select i from IntegrityAuditEntity i where i.persistenceUnit=:pu and i.nodeType=:nt"); - iaequery.setParameter("pu", persistenceUnit); - iaequery.setParameter("nt", nodeType); - - List iaeList = iaequery.getResultList(); - - // commit transaction - et.commit(); - em.close(); - logger.debug("getIntegrityAuditEntities: Exit, iaeList=" + iaeList); - return iaeList; - }catch (Exception e){ - String msg = "DbDAO: " + "getIntegrityAuditEntities() " + "ecountered a problem in execution: "; - logger.error(msg + e); - System.out.println(new Date()); - e.printStackTrace(); - throw new DbDaoTransactionException(e); - } - - } - - /** - * getMyIntegrityAuditEntity() gets my IntegrityAuditEntity - * @return - * @throws DbDaoTransactionException - */ - public IntegrityAuditEntity getMyIntegrityAuditEntity() throws DbDaoTransactionException{ - try{ - EntityManager em = emf.createEntityManager(); - - // Start a transaction - EntityTransaction et = em.getTransaction(); - - et.begin(); - - // if IntegrityAuditEntity entry exists for resourceName and PU, retrieve it - Query iaequery = em.createQuery("Select i from IntegrityAuditEntity i where i.resourceName=:rn and i.persistenceUnit=:pu"); - iaequery.setParameter("rn", this.resourceName); - iaequery.setParameter("pu", this.persistenceUnit); - - @SuppressWarnings("rawtypes") - List iaeList = iaequery.getResultList(); - IntegrityAuditEntity iae = null; - - if(!iaeList.isEmpty()){ - //ignores multiple results - iae = (IntegrityAuditEntity) iaeList.get(0); - // refresh the object from DB in case cached data was returned - em.refresh(iae); - logger.info("Resource: " + this.resourceName + " with PersistenceUnit: " + this.persistenceUnit - + " exists"); - }else{ - // If it does not exist, log an error - logger.error("Attempting to setLastUpdated" - + " on an entry that does not exist:" - +" resource " + this.resourceName + " with PersistenceUnit: " + this.persistenceUnit); - } - - // close the transaction - et.commit(); - // close the EntityManager - em.close(); - - return iae; - }catch (Exception e){ - String msg = "DbDAO: " + "setLastUpdated() " + "ecountered a problem in execution: "; - logger.error(msg + e); - System.out.println(new Date()); - e.printStackTrace(); - throw new DbDaoTransactionException(e); - } - } - - - /** - * getIntegrityAuditEntity() gets the IntegrityAuditEntity with a particular ID - * @param id - * @return - * @throws DbDaoTransactionException - */ - public IntegrityAuditEntity getIntegrityAuditEntity(long id) throws DbDaoTransactionException{ - try{ - EntityManager em = emf.createEntityManager(); - - // Start a transaction - EntityTransaction et = em.getTransaction(); - - et.begin(); - - IntegrityAuditEntity iae = em.find(IntegrityAuditEntity.class, id); - - et.commit(); - em.close(); - - return iae; - }catch (Exception e){ - String msg = "DbDAO: " + "getIntegrityAuditEntity() " + "ecountered a problem in execution: "; - logger.error(msg + e); - System.out.println(new Date()); - e.printStackTrace(); - throw new DbDaoTransactionException(e); - } - } - - /** - * getPersistenceClassNames() gets all the persistence class names. - * @return - */ - public HashSet getPersistenceClassNames(){ - logger.debug("DbDAO: getPersistenceClassNames() entry"); - HashSet returnList = new HashSet(); - final Metamodel mm = emf.getMetamodel(); - logger.debug("\n" + persistenceUnit +" persistence unit classes:"); - for (final ManagedType managedType : mm.getManagedTypes()) { - Class c = managedType.getJavaType(); - logger.debug(" " + c.getSimpleName()); - returnList.add(c.getName()); //the full class name needed to make a query using jpa - } - logger.debug("DbDAO: getPersistenceClassNames() exit"); - return returnList; - } - - /** - * Register the IntegrityAudit instance - */ - private void register() throws DbDaoTransactionException { - try{ - EntityManager em = emf.createEntityManager(); - - // Start a transaction - EntityTransaction et = em.getTransaction(); - - et.begin(); - - // if IntegrityAuditEntity entry exists for resourceName and PU, update it. If not found, create a new entry - Query iaequery = em.createQuery("Select i from IntegrityAuditEntity i where i.resourceName=:rn and i.persistenceUnit=:pu"); - iaequery.setParameter("rn", this.resourceName); - iaequery.setParameter("pu", this.persistenceUnit); - - @SuppressWarnings("rawtypes") - List iaeList = iaequery.getResultList(); - IntegrityAuditEntity iae = null; - - //If it already exists, we just want to update the properties and lastUpdated date - if(!iaeList.isEmpty()){ - //ignores multiple results - iae = (IntegrityAuditEntity) iaeList.get(0); - // refresh the object from DB in case cached data was returned - em.refresh(iae); - logger.info("Resource: " + this.resourceName + " with PersistenceUnit: " + this.persistenceUnit - + " exists and entry be updated"); - }else{ - // If it does not exist, we also must add teh resourceName, persistenceUnit and designated values - logger.info("Adding resource " + resourceName + " with PersistenceUnit: " + this.persistenceUnit - + " to IntegrityAuditEntity table"); - iae = new IntegrityAuditEntity(); - iae.setResourceName(this.resourceName); - iae.setPersistenceUnit(this.persistenceUnit); - iae.setDesignated(false); - } - //update/set properties in entry - iae.setSite(this.siteName); - iae.setNodeType(this.nodeType); - iae.setLastUpdated(new Date()); - iae.setJdbcDriver(this.dbDriver); - iae.setJdbcPassword(this.dbPwd); - iae.setJdbcUrl(dbUrl); - iae.setJdbcUser(dbUser); - - em.persist(iae); - // flush to the DB - em.flush(); - - // commit transaction - et.commit(); - em.close(); - }catch (Exception e){ - String msg = "DbDAO: " + "register() " + "ecountered a problem in execution: "; - logger.error(msg + e); - System.out.println(new Date()); - e.printStackTrace(); - throw new DbDaoTransactionException(e); - } - - } - - public void setDesignated(boolean designated) throws DbDaoTransactionException{ - setDesignated(this.resourceName, this.persistenceUnit, designated); - } - - - public void setDesignated(String rName, String pUnit, boolean desig) throws DbDaoTransactionException{ - logger.debug("setDesignated: enter, resourceName: " + rName + ", persistenceUnit: " - + pUnit + ", designated: " + desig); - try{ - - EntityManager em = emf.createEntityManager(); - - // Start a transaction - EntityTransaction et = em.getTransaction(); - - et.begin(); - - // if IntegrityAuditEntity entry exists for resourceName and PU, update it. If not found, create a new entry - Query iaequery = em.createQuery("Select i from IntegrityAuditEntity i where i.resourceName=:rn and i.persistenceUnit=:pu"); - iaequery.setParameter("rn", rName); - iaequery.setParameter("pu", pUnit); - - @SuppressWarnings("rawtypes") - List iaeList = iaequery.getResultList(); - IntegrityAuditEntity iae = null; - - if(!iaeList.isEmpty()){ - //ignores multiple results - iae = (IntegrityAuditEntity) iaeList.get(0); - // refresh the object from DB in case cached data was returned - em.refresh(iae); - logger.info("Resource: " + rName + " with PersistenceUnit: " + pUnit - + " exists and designated be updated"); - iae.setDesignated(desig); - - em.persist(iae); - // flush to the DB - em.flush(); - }else{ - // If it does not exist, log an error - logger.error("Attempting to setDesignated(" - + desig + ") on an entry that does not exist:" - +" resource " + rName + " with PersistenceUnit: " + pUnit); - } - - // close the transaction - et.commit(); - // close the EntityManager - em.close(); - }catch (Exception e){ - String msg = "DbDAO: " + "setDesignated() " + "ecountered a problem in execution: "; - logger.error(msg + e); - System.out.println(new Date()); - e.printStackTrace(); - throw new DbDaoTransactionException(e); - } - - } - - public void setLastUpdated() throws DbDaoTransactionException{ - logger.debug("setLastUpdated: enter, resourceName: " + this.resourceName + ", persistenceUnit: " - + this.persistenceUnit); - try{ - EntityManager em = emf.createEntityManager(); - - // Start a transaction - EntityTransaction et = em.getTransaction(); - - et.begin(); - - // if IntegrityAuditEntity entry exists for resourceName and PU, update it. If not found, create a new entry - Query iaequery = em.createQuery("Select i from IntegrityAuditEntity i where i.resourceName=:rn and i.persistenceUnit=:pu"); - iaequery.setParameter("rn", this.resourceName); - iaequery.setParameter("pu", this.persistenceUnit); - - @SuppressWarnings("rawtypes") - List iaeList = iaequery.getResultList(); - IntegrityAuditEntity iae = null; - - if(!iaeList.isEmpty()){ - //ignores multiple results - iae = (IntegrityAuditEntity) iaeList.get(0); - // refresh the object from DB in case cached data was returned - em.refresh(iae); - logger.info("Resource: " + this.resourceName + " with PersistenceUnit: " + this.persistenceUnit - + " exists and lastUpdated be updated"); - iae.setLastUpdated(new Date()); - - em.persist(iae); - // flush to the DB - em.flush(); - }else{ - // If it does not exist, log an error - logger.error("Attempting to setLastUpdated" - + " on an entry that does not exist:" - +" resource " + this.resourceName + " with PersistenceUnit: " + this.persistenceUnit); - } - - // close the transaction - et.commit(); - // close the EntityManager - em.close(); - }catch (Exception e){ - String msg = "DbDAO: " + "setLastUpdated() " + "ecountered a problem in execution: "; - logger.error(msg + e); - System.out.println(new Date()); - e.printStackTrace(); - throw new DbDaoTransactionException(e); - } - - } - - /** - * Normally this method should only be used in a JUnit test environment. - * Manually deletes all PDP records in droolspdpentity table. - */ - public int deleteAllIntegrityAuditEntities() throws DbDaoTransactionException { - - try{ - - if (!IntegrityAudit.isUnitTesting) { - String msg = "DbDAO: " + "deleteAllIntegrityAuditEntities() " + "should only be invoked during JUnit testing"; - logger.error(msg); - throw new DbDaoTransactionException(msg); - } - - EntityManager em = emf.createEntityManager(); - // Start a transaction - EntityTransaction et = em.getTransaction(); - - et.begin(); - - // if IntegrityAuditEntity entry exists for resourceName and PU, update it. If not found, create a new entry - Query iaequery = em.createQuery("Delete from IntegrityAuditEntity"); - - int returnCode = iaequery.executeUpdate(); - - // commit transaction - et.commit(); - em.close(); - - logger.info("deleteAllIntegrityAuditEntities: returnCode=" + returnCode); - - return returnCode; - - }catch (Exception e){ - String msg = "DbDAO: " + "deleteAllIntegrityAuditEntities() " + "encountered a problem in execution: "; - logger.error(msg + e); - System.out.println(new Date()); - e.printStackTrace(); - throw new DbDaoTransactionException(e); - } - - } - - /** - * Changes designation to specified resourceName - * - * static lock object in conjunction with synchronized keyword ensures that - * designation changes are done serially within a resource. I.e. static lock - * ensures that multiple instantiations of DbDAO don't interleave - * changeDesignated() invocations and potentially produce simultaneous - * designations. - * - * Optimistic locking (the default, versus pessimistic) is sufficient to - * avoid simultaneous designations from interleaved changeDesignated() - * invocations from different resources (entities), because it prevents - * "dirty" and "non-repeatable" reads. - * - * See http://www.objectdb.com/api/java/jpa/LockModeType - * - * and - * - * http://stackoverflow.com/questions/2120248/how-to-synchronize-a-static- - * variable-among-threads-running-different-instances-o - */ - public void changeDesignated(String resourceName, String persistenceUnit, - String nodeType) throws DbDaoTransactionException { - - if (logger.isDebugEnabled()) { - logger.debug("changeDesignated: Entering, resourceName=" - + resourceName + ", persistenceUnit=" + persistenceUnit - + ", nodeType=" + nodeType); - } - - long startTime = System.currentTimeMillis(); - - synchronized (lock) { - - EntityManager em = null; - try { - - em = emf.createEntityManager(); - em.getTransaction().begin(); - - /* - * Define query - */ - Query query = em - .createQuery("Select i from IntegrityAuditEntity i where i.persistenceUnit=:pu and i.nodeType=:nt"); - query.setParameter("pu", persistenceUnit); - query.setParameter("nt", nodeType); - - /* - * Execute query using pessimistic write lock. This ensures that if anyone else is currently reading - * the records we'll throw a LockTimeoutException. - */ - @SuppressWarnings("unchecked") - List integrityAuditEntityList = (List) query - .getResultList(); - for (Object o : integrityAuditEntityList) { - if (o instanceof IntegrityAuditEntity) { - IntegrityAuditEntity integrityAuditEntity = (IntegrityAuditEntity) o; - if (integrityAuditEntity.getResourceName().equals( - resourceName)) { - if (logger.isDebugEnabled()) { - logger.debug("changeDesignated: Designating resourceName=" - + integrityAuditEntity - .getResourceName()); - } - integrityAuditEntity.setDesignated(true); - } else { - if (logger.isDebugEnabled()) { - logger.debug("changeDesignated: Removing designation from resourceName=" - + integrityAuditEntity - .getResourceName()); - } - integrityAuditEntity.setDesignated(false); - } - } - } - - if (logger.isDebugEnabled()) { - logger.debug("changeDesignated: Committing designation to resourceName=" - + resourceName); - } - em.getTransaction().commit(); - - /* - * If we get a LockTimeoutException, no harm done really. We'll - * probably be successful on the next attempt. The odds of - * another DbDAO instance on this entity or another entity - * attempting a simultaneous IntegrityAuditEntity table - * read/update are pretty slim (we're only in this method for - * two or three milliseconds) - */ - } catch (LockTimeoutException e) { - em.getTransaction().rollback(); - String msg = "DbDAO: " + "changeDesignated() " - + "caught LockTimeoutException, message=" + e.getMessage(); - logger.error(msg + e); - System.out.println(new Date()); - e.printStackTrace(); - throw new DbDaoTransactionException(msg, e); - } catch (Exception e) { - em.getTransaction().rollback(); - String msg = "DbDAO: " + "changeDesignated() " - + "caught Exception, message=" + e.getMessage(); - logger.error(msg + e); - System.out.println(new Date()); - e.printStackTrace(); - throw new DbDaoTransactionException(msg, e); - } - - } // end synchronized block - - if (logger.isDebugEnabled()) { - logger.debug("changeDesignated: Exiting; time expended=" - + (System.currentTimeMillis() - startTime) + "ms"); - } - - } - - -} diff --git a/integrity-audit/src/main/java/org/openecomp/policy/common/ia/DbDaoTransactionException.java b/integrity-audit/src/main/java/org/openecomp/policy/common/ia/DbDaoTransactionException.java deleted file mode 100644 index fc2fea03..00000000 --- a/integrity-audit/src/main/java/org/openecomp/policy/common/ia/DbDaoTransactionException.java +++ /dev/null @@ -1,37 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Integrity Audit - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.openecomp.policy.common.ia; - -public class DbDaoTransactionException extends Exception{ - private static final long serialVersionUID = 1L; - public DbDaoTransactionException() { - } - public DbDaoTransactionException(String message) { - super(message); - } - - public DbDaoTransactionException(Throwable cause) { - super(cause); - } - public DbDaoTransactionException(String message, Throwable cause) { - super(message, cause); - } -} diff --git a/integrity-audit/src/main/java/org/openecomp/policy/common/ia/IntegrityAudit.java b/integrity-audit/src/main/java/org/openecomp/policy/common/ia/IntegrityAudit.java deleted file mode 100644 index 1dd260de..00000000 --- a/integrity-audit/src/main/java/org/openecomp/policy/common/ia/IntegrityAudit.java +++ /dev/null @@ -1,241 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Integrity Audit - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.openecomp.policy.common.ia; - -import java.util.Properties; - -//import org.apache.log4j.Logger; - -import org.openecomp.policy.common.ia.IntegrityAuditProperties.NodeTypeEnum; -import org.openecomp.policy.common.logging.flexlogger.FlexLogger; -import org.openecomp.policy.common.logging.flexlogger.Logger; - -/** - * class IntegrityAudit - * Audits all persisted entities for all resource clusters for all sites and logs any anomalies. - */ -public class IntegrityAudit { - - private static final Logger logger = FlexLogger.getLogger(IntegrityAudit.class); - - public static boolean isUnitTesting; - private boolean isThreadInitialized = false; - - AuditThread auditThread = null; - - private String persistenceUnit; - private Properties properties; - private String resourceName; - - - /* - * This is the audit period in seconds. For example, if it had a value of 3600, the audit - * can only run once per hour. If it has a value of 60, it can run once per minute. - * - * Values: - * integrityAuditPeriodSeconds < 0 (negative number) indicates the audit is off - * integrityAuditPeriodSecconds == 0 indicates the audit is to run continuously - * integrityAuditPeriodSeconds > 0 indicates the audit is to run at most once during the indicated period - * - */ - private int integrityAuditPeriodSeconds; - - /** - * IntegrityAudit constructor - * @param resourceName - * @param persistenceUnit - * @param properties - * @throws Exception - */ - public IntegrityAudit(String resourceName, String persistenceUnit, Properties properties) throws Exception { - - logger.info("Constructor: Entering and checking for nulls"); - String parmList = ""; - if (parmsAreBad(resourceName, persistenceUnit, properties, parmList)) { - logger.error("Constructor: Parms contain nulls; cannot run audit for resourceName=" - + resourceName + ", persistenceUnit=" + persistenceUnit - + ", bad parameters: " + parmList); - throw new Exception( - "Constructor: Parms contain nulls; cannot run audit for resourceName=" - + resourceName + ", persistenceUnit=" - + persistenceUnit - + ", bad parameters: " + parmList); - } - - this.persistenceUnit = persistenceUnit; - this.properties = properties; - this.resourceName = resourceName; - - if(properties.getProperty(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS) != null){ //It is allowed to be null - this.integrityAuditPeriodSeconds= Integer.parseInt(properties.getProperty(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS).trim()); - } else{ - //If it is null, set it to the default value - this.integrityAuditPeriodSeconds = IntegrityAuditProperties.DEFAULT_AUDIT_PERIOD_SECONDS; - } - logger.info("Constructor: Exiting"); - - } - - /** - * Used during JUnit testing by AuditPeriodTest.java - */ - public int getIntegrityAuditPeriodSeconds() { - return integrityAuditPeriodSeconds; - } - - /** - * Determine if the nodeType conforms to the required node types - */ - public static boolean isNodeTypeEnum(String nt) { - for (NodeTypeEnum n : NodeTypeEnum.values()) { - if (n.toString().equals(nt)) { - return true; - } - } - return false; - } - - - /** - * Makes sure we don't try to run the audit with bad parameters. - */ - public static boolean parmsAreBad(String resourceName, String persistenceUnit, - Properties properties, String badparams) { - - boolean parmsAreBad = false; - - if(resourceName == null || resourceName.isEmpty()){ - badparams = badparams.concat("resourceName "); - parmsAreBad = true; - } - - if(persistenceUnit == null || persistenceUnit.isEmpty()){ - badparams = badparams.concat("persistenceUnit "); - parmsAreBad = true; - } - - String dbDriver = properties.getProperty(IntegrityAuditProperties.DB_DRIVER).trim(); - if(dbDriver == null || dbDriver.isEmpty()){ - badparams = badparams.concat("dbDriver "); - parmsAreBad = true; - } - - String dbUrl = properties.getProperty(IntegrityAuditProperties.DB_URL).trim(); - if(dbUrl == null || dbUrl.isEmpty()){ - badparams = badparams.concat("dbUrl "); - parmsAreBad = true; - } - - String dbUser = properties.getProperty(IntegrityAuditProperties.DB_USER).trim(); - if(dbUser == null || dbUser.isEmpty()){ - badparams = badparams.concat("dbUser "); - parmsAreBad = true; - } - - String dbPwd = properties.getProperty(IntegrityAuditProperties.DB_PWD).trim(); - if(dbPwd == null){ //may be empty - badparams = badparams.concat("dbPwd "); - parmsAreBad = true; - } - - String siteName = properties.getProperty(IntegrityAuditProperties.SITE_NAME).trim(); - if(siteName == null || siteName.isEmpty()){ - badparams = badparams.concat("siteName "); - parmsAreBad = true; - } - - String nodeType = properties.getProperty(IntegrityAuditProperties.NODE_TYPE).trim(); - if(nodeType == null || nodeType.isEmpty()){ - badparams = badparams.concat("nodeType "); - parmsAreBad = true; - } else { - if (!isNodeTypeEnum(nodeType)) { - String nodetypes = "nodeType must be one of["; - for (NodeTypeEnum n : NodeTypeEnum.values()) { - nodetypes = nodetypes.concat(n.toString() + " "); - } - badparams = badparams.concat(nodetypes + "] "); - parmsAreBad = true; - } - } - if(properties.getProperty(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS) != null){ //It is allowed to be null - try{ - Integer.parseInt(properties.getProperty(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS).trim()); - }catch(NumberFormatException nfe){ - badparams = badparams.concat(", auditPeriodSeconds=" - + properties.getProperty(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS).trim()); - parmsAreBad = true; - } - } - logger.debug("parmsAreBad: exit:" - + "\nresourceName: " + resourceName - + "\npersistenceUnit: " + persistenceUnit - + "\nproperties: " + properties); - - return parmsAreBad; - } - /** - * Starts the audit thread - * @throws Exception - */ - public void startAuditThread() throws Exception { - - logger.info("startAuditThread: Entering"); - - if (integrityAuditPeriodSeconds >= 0) { - this.auditThread = new AuditThread(this.resourceName, - this.persistenceUnit, this.properties, - integrityAuditPeriodSeconds, this); - logger.info("startAuditThread: Audit started and will run every " - + integrityAuditPeriodSeconds + " seconds"); - this.auditThread.start(); - } else { - logger.info("startAuditThread: Suppressing integrity audit, integrityAuditPeriodSeconds=" - + integrityAuditPeriodSeconds); - } - - logger.info("startAuditThread: Exiting"); - } - /** - * Stops the audit thread - */ - public void stopAuditThread() { - - logger.info("stopAuditThread: Entering"); - - if (this.auditThread != null) { - this.auditThread.interrupt(); - } else { - logger.info("stopAuditThread: auditThread never instantiated; no need to interrupt"); - } - - logger.info("stopAuditThread: Exiting"); - } - - public boolean isThreadInitialized() { - return isThreadInitialized; - } - - public void setThreadInitialized(boolean isThreadInitialized) { - logger.info("setThreadInitialized: Setting isThreadInitialized=" + isThreadInitialized); - this.isThreadInitialized = isThreadInitialized; - } -} diff --git a/integrity-audit/src/main/java/org/openecomp/policy/common/ia/IntegrityAuditProperties.java b/integrity-audit/src/main/java/org/openecomp/policy/common/ia/IntegrityAuditProperties.java deleted file mode 100644 index 5a87c9cc..00000000 --- a/integrity-audit/src/main/java/org/openecomp/policy/common/ia/IntegrityAuditProperties.java +++ /dev/null @@ -1,54 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Integrity Audit - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.openecomp.policy.common.ia; - -public class IntegrityAuditProperties { - - public static final String DEFAULT_DB_DRIVER = "org.h2.Driver"; - public static final String DEFAULT_DB_URL = "jdbc:h2:file:./sql/iaTest"; - public static final String DEFAULT_DB_USER = "sa"; - public static final String DEFAULT_DB_PWD = ""; - public static final int DEFAULT_AUDIT_PERIOD_SECONDS = -1; // Audit does not run - - public static final String DB_DRIVER = "javax.persistence.jdbc.driver"; - public static final String DB_URL = "javax.persistence.jdbc.url"; - public static final String DB_USER = "javax.persistence.jdbc.user"; - public static final String DB_PWD = "javax.persistence.jdbc.password"; - public static final String AUDIT_PERIOD_SECONDS = "integrity_audit_period_seconds"; - - - public static final String SITE_NAME = "site_name"; - public static final String NODE_TYPE = "node_type"; - - public static enum NodeTypeEnum { - pdp_xacml, - pdp_drools, - pap, - pap_admin, - logparser, - brms_gateway, - astra_gateway, - elk_server, - pypdp - - } - -} diff --git a/integrity-audit/src/main/java/org/openecomp/policy/common/ia/IntegrityAuditPropertiesException.java b/integrity-audit/src/main/java/org/openecomp/policy/common/ia/IntegrityAuditPropertiesException.java deleted file mode 100644 index 77c85585..00000000 --- a/integrity-audit/src/main/java/org/openecomp/policy/common/ia/IntegrityAuditPropertiesException.java +++ /dev/null @@ -1,37 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Integrity Audit - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.openecomp.policy.common.ia; - -public class IntegrityAuditPropertiesException extends Exception{ - private static final long serialVersionUID = 1L; - public IntegrityAuditPropertiesException() { - } - public IntegrityAuditPropertiesException(String message) { - super(message); - } - - public IntegrityAuditPropertiesException(Throwable cause) { - super(cause); - } - public IntegrityAuditPropertiesException(String message, Throwable cause) { - super(message, cause); - } -} diff --git a/integrity-audit/src/main/java/org/openecomp/policy/common/ia/jpa/IntegrityAuditEntity.java b/integrity-audit/src/main/java/org/openecomp/policy/common/ia/jpa/IntegrityAuditEntity.java deleted file mode 100644 index 21b200c8..00000000 --- a/integrity-audit/src/main/java/org/openecomp/policy/common/ia/jpa/IntegrityAuditEntity.java +++ /dev/null @@ -1,203 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Integrity Audit - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.openecomp.policy.common.ia.jpa; - -import java.io.Serializable; -import java.util.Date; - -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; -import javax.persistence.NamedQueries; -import javax.persistence.NamedQuery; -import javax.persistence.PrePersist; -import javax.persistence.PreUpdate; -import javax.persistence.Table; -import javax.persistence.Temporal; -import javax.persistence.TemporalType; -/* - * The Entity class to for management of IntegrityAudits - */ - -@Entity -@Table(name="IntegrityAuditEntity") -@NamedQueries({ - @NamedQuery(name=" IntegrityAuditEntity.findAll", query="SELECT e FROM IntegrityAuditEntity e "), - @NamedQuery(name="IntegrityAuditEntity.deleteAll", query="DELETE FROM IntegrityAuditEntity WHERE 1=1") -}) - -public class IntegrityAuditEntity implements Serializable { - private static final long serialVersionUID = 1L; - - public static boolean isUnitTesting; - - @Id - @GeneratedValue(strategy = GenerationType.AUTO) - @Column(name="id") - private long id; - - @Column(name="persistenceUnit", nullable=false) - private String persistenceUnit; - - @Column(name="site", nullable=true) - private String site; - - @Column(name="nodeType", nullable=true) - private String nodeType; - - @Column(name="resourceName", nullable=false, unique=true) - private String resourceName; - - @Column(name="designated", nullable=true) - private boolean designated = false; - - @Column(name="jdbcDriver", nullable=false) - private String jdbcDriver; - - @Column(name="jdbcUrl", nullable=false) - private String jdbcUrl; - - @Column(name="jdbcUser", nullable=false) - private String jdbcUser; - - @Column(name="jdbcPassword", nullable=false) - private String jdbcPassword; - - @Temporal(TemporalType.TIMESTAMP) - @Column(name="createdDate", updatable=true) - private Date createdDate; - - @Temporal(TemporalType.TIMESTAMP) - @Column(name="lastUpdated") - private Date lastUpdated; - - - public IntegrityAuditEntity() { - } - - @PrePersist - public void prePersist() { - Date date = new Date(); - this.createdDate = date; - this.lastUpdated = date; - } - - @PreUpdate - public void preUpdate() { - if (!isUnitTesting) { - this.lastUpdated = new Date(); - } - } - - public long getId() { - return id; - } - - public String getPersistenceUnit() { - return persistenceUnit; - } - - public void setPersistenceUnit(String persistenceUnit) { - this.persistenceUnit = persistenceUnit; - } - - public String getSite() { - return site; - } - - public void setSite(String site) { - this.site = site; - } - - public String getNodeType() { - return nodeType; - } - - public void setNodeType(String nodeType) { - this.nodeType = nodeType; - } - - public String getResourceName() { - return resourceName; - } - - public void setResourceName(String resourceName) { - this.resourceName = resourceName; - } - - public boolean isDesignated() { - return designated; - } - - public void setDesignated(boolean designated) { - this.designated = designated; - } - - public String getJdbcDriver() { - return jdbcDriver; - } - - public void setJdbcDriver(String jdbcDriver) { - this.jdbcDriver = jdbcDriver; - } - - public String getJdbcUrl() { - return jdbcUrl; - } - - public void setJdbcUrl(String jdbcUrl) { - this.jdbcUrl = jdbcUrl; - } - - public String getJdbcUser() { - return jdbcUser; - } - - public void setJdbcUser(String jdbcUser) { - this.jdbcUser = jdbcUser; - } - - public String getJdbcPassword() { - return jdbcPassword; - } - - public void setJdbcPassword(String jdbcPassword) { - this.jdbcPassword = jdbcPassword; - } - - public Date getLastUpdated() { - return lastUpdated; - } - - public void setLastUpdated(Date lastUpdated) { - this.lastUpdated = lastUpdated; - } - - public Date getCreatedDate() { - return createdDate; - } - - public void setCreatedDate(Date created) { - this.createdDate = created; - } -} diff --git a/integrity-audit/src/main/resources/META-INF/persistence.xml b/integrity-audit/src/main/resources/META-INF/persistence.xml index 31ffb4de..9e4880fa 100644 --- a/integrity-audit/src/main/resources/META-INF/persistence.xml +++ b/integrity-audit/src/main/resources/META-INF/persistence.xml @@ -23,8 +23,8 @@ org.eclipse.persistence.jpa.PersistenceProvider - org.openecomp.policy.common.ia.jpa.IntegrityAuditEntity - org.openecomp.policy.common.ia.test.jpa.IaTestEntity + org.onap.policy.common.ia.jpa.IntegrityAuditEntity + org.onap.policy.common.ia.test.jpa.IaTestEntity NONE @@ -37,7 +37,7 @@ org.eclipse.persistence.jpa.PersistenceProvider - org.openecomp.policy.common.ia.jpa.IntegrityAuditEntity + org.onap.policy.common.ia.jpa.IntegrityAuditEntity NONE diff --git a/integrity-audit/src/main/resources/logback.xml b/integrity-audit/src/main/resources/logback.xml index 426adf9e..c3b2f9f6 100644 --- a/integrity-audit/src/main/resources/logback.xml +++ b/integrity-audit/src/main/resources/logback.xml @@ -27,7 +27,7 @@ + ::= "MSO" | "DCAE" | "ASDC " | "AAI" |"Policy" | "SDNC" | "AC" --> diff --git a/integrity-audit/src/test/java/org/onap/policy/common/ia/test/AuditPeriodTest.java b/integrity-audit/src/test/java/org/onap/policy/common/ia/test/AuditPeriodTest.java new file mode 100644 index 00000000..52f847cf --- /dev/null +++ b/integrity-audit/src/test/java/org/onap/policy/common/ia/test/AuditPeriodTest.java @@ -0,0 +1,480 @@ +/*- + * ============LICENSE_START======================================================= + * Integrity Audit + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.common.ia.test; + + +import static org.junit.Assert.*; + +import java.io.BufferedReader; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Properties; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.EntityTransaction; +import javax.persistence.Persistence; + +//import org.apache.commons.logging.Log; +//import org.apache.commons.logging.LogFactory; +import org.junit.After; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; + +import org.onap.policy.common.ia.AuditThread; +import org.onap.policy.common.ia.IntegrityAudit; +import org.onap.policy.common.ia.IntegrityAuditProperties; +import org.onap.policy.common.logging.flexlogger.FlexLogger; +import org.onap.policy.common.logging.flexlogger.Logger; + +/* + * All JUnits are designed to run in the local development environment + * where they have write privileges and can execute time-sensitive + * tasks. + */ +public class AuditPeriodTest { + + private static Logger logger = FlexLogger.getLogger(AuditPeriodTest.class); + + private static final String AUDIT_PERIOD_TEST_LOG = "./testingLogs/common-modules/integrity-audit/debug.log"; + + private static String persistenceUnit; + private static Properties properties; + private static String resourceName; + + @Before + public void setUp() throws Exception { + + + System.out.println("setUp: Clearing " + AUDIT_PERIOD_TEST_LOG); + FileOutputStream fstream = new FileOutputStream(AUDIT_PERIOD_TEST_LOG); + fstream.close(); + + logger.info("setUp: Entering"); + + IntegrityAudit.isUnitTesting = true; + + properties = new Properties(); + properties.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER); + properties.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL); + properties.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER); + properties.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD); + properties.put(IntegrityAuditProperties.SITE_NAME, "SiteA"); + properties.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml"); + + persistenceUnit = "testPU"; + resourceName = "pdp1"; + + //Clean up the DB + EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit, properties); + + EntityManager em = emf.createEntityManager(); + // Start a transaction + EntityTransaction et = em.getTransaction(); + + et.begin(); + + // if IntegrityAuditEntity entry exists for resourceName and PU, update it. If not found, create a new entry + em.createQuery("Delete from IntegrityAuditEntity").executeUpdate(); + + // commit transaction + et.commit(); + em.close(); + + logger.info("setUp: Exiting"); + + } + + + @After + public void tearDown() throws Exception { + + logger.info("tearDown: Entering"); + + logger.info("tearDown: Exiting"); + + } + + /* + * Verifies (via log parsing) that when a negative audit period is + * specified, the audit is suppressed. + */ + @Ignore + @Test + public void testNegativeAuditPeriod() throws Exception { + + logger.info("testNegativeAuditPeriod: Entering"); + + properties.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "-1"); + + IntegrityAudit integrityAudit = new IntegrityAudit(resourceName, persistenceUnit, properties); + integrityAudit.startAuditThread(); + + /* + * Sleep long enough to allow + * + * 1) audit to immediately terminate. + */ + Thread.sleep(1000); + + logger.info("testNegativeAuditPeriod: Stopping audit thread (should be a no-op!)"); + integrityAudit.stopAuditThread(); + + FileInputStream fstream = new FileInputStream(AUDIT_PERIOD_TEST_LOG); + BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); + String strLine; + int startIndex; + ArrayList expectedResult = new ArrayList(Arrays.asList("-1")); + ArrayList delegates = new ArrayList(); + while ((strLine = br.readLine()) != null) { + /* parse strLine to obtain what you want */ + if (strLine.contains("Suppressing integrity audit, integrityAuditPeriodSeconds=")) { + startIndex = strLine.indexOf("integrityAuditPeriodSeconds=") + 28; + + String integrityAuditPeriodSeconds = strLine.substring(startIndex); + + delegates.add(integrityAuditPeriodSeconds); + } + } + + for (String delegate: delegates) { + logger.info("testNegativeAuditPeriod: delegate: " + delegate); + } + + fstream.close(); + + assertTrue(expectedResult.equals(delegates)); + + logger.info("testNegativeAuditPeriod: Exiting"); + + } + + /* + * Verifies (via log parsing) that when an audit period of zero is + * specified, the audit runs continuously, generating a number of + * sleep/wake sequences in a short period of time (e.g. 100ms). + */ + @Ignore + @Test + public void testZeroAuditPeriod() throws Exception { + + logger.info("testZeroAuditPeriod: Entering"); + + properties.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "0"); + + IntegrityAudit integrityAudit = new IntegrityAudit(resourceName, + persistenceUnit, properties); + integrityAudit.startAuditThread(); + + /* + * Sleep long enough to allow + * + * 1) audit to generate a bunch of sleep wake sequences. + * + * Note: + * + * (AuditThread.AUDIT_SIMULATION_SLEEP_INTERVAL * + * AuditThread.AUDIT_SIMULATION_ITERATIONS) is the time it takes for the + * audit simulation to run. + * + * (integrityAudit.getIntegrityAuditPeriodSeconds() should return a + * value of zero; i.e. audit should not sleep at all between iterations + * + * "100"ms is the time we allow the audit to cycle continuously + */ + long sleepMillis = (AuditThread.AUDIT_SIMULATION_SLEEP_INTERVAL * AuditThread.AUDIT_SIMULATION_ITERATIONS) + + (integrityAudit.getIntegrityAuditPeriodSeconds() * 1000) + + 100; + logger.info("testZeroAuditPeriod: Sleeping " + sleepMillis + "ms before stopping auditThread"); + Thread.sleep(sleepMillis); + + logger.info("testZeroAuditPeriod: Stopping audit thread"); + integrityAudit.stopAuditThread(); + + /* + * Before audit completion message upon awaking from sleep is upper case "Awaking". After audit + * completion, all awakings are lower case "awaking". + */ + logger.info("testZeroAuditPeriod: Parsing " + AUDIT_PERIOD_TEST_LOG + " for 'awaking'"); + FileInputStream fstream = new FileInputStream(AUDIT_PERIOD_TEST_LOG); + BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); + String strLine = ""; + int awakings = 0; + int lines = 0; + while ((strLine = br.readLine()) != null) { + if (strLine.contains("Awaking from 0ms sleep")) { + fail("Audit appears not to have run!? Got '" + strLine + "'"); + } else { + if (strLine.contains("awaking from 0ms sleep")) { + awakings++; + } + } + lines++; + } + logger.info("testZeroAuditPeriod: Done parsing " + + AUDIT_PERIOD_TEST_LOG + " for 'awaking'; lines parsed=" + + lines + ", closing stream"); + fstream.close(); + + /* + * We should get at least 10 sleep/wake sequences. + */ + assertTrue("Only " + awakings + " awakings", awakings > 10); + assertTrue(integrityAudit.getIntegrityAuditPeriodSeconds() == 0); + + logger.info("testZeroAuditPeriod: Exiting, awakings=" + + awakings + ", integrityAuditPeriodSeconds=" + + integrityAudit.getIntegrityAuditPeriodSeconds()); + + } + + /* + * Verifies (via log parsing) that when an audit period of five minutes is + * specified, there is a five minute interval between the audits run + * on each of three different entities. + */ + @Ignore + @Test + public void testFiveMinuteAuditPeriod() throws Exception { + + logger.info("testFiveMinuteAuditPeriod: Entering"); + + properties.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "300"); + + /* + * Start audit for pdp1. + */ + IntegrityAudit integrityAudit = new IntegrityAudit(resourceName, + persistenceUnit, properties); + integrityAudit.startAuditThread(); + + /* + * Start audit for pdp2. + */ + Properties properties2 = new Properties(); + properties2.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER); + properties2.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL); + properties2.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER); + properties2.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD); + properties2.put(IntegrityAuditProperties.SITE_NAME, "SiteA"); + properties2.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml"); + properties2.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "300"); + String persistenceUnit2 = "testPU"; + String resourceName2 = "pdp2"; + IntegrityAudit integrityAudit2 = new IntegrityAudit(resourceName2, persistenceUnit2, properties2); + integrityAudit2.startAuditThread(); + + /* + * Start audit for pdp3. + */ + Properties properties3 = new Properties(); + properties3.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER); + properties3.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL); + properties3.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER); + properties3.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD); + properties3.put(IntegrityAuditProperties.SITE_NAME, "SiteA"); + properties3.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml"); + properties3.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "300"); + String persistenceUnit3 = "testPU"; + String resourceName3 = "pdp3"; + IntegrityAudit integrityAudit3 = new IntegrityAudit(resourceName3, persistenceUnit3, properties3); + integrityAudit3.startAuditThread(); + + + /* + * 1) All three audit run once. This should take approximately 105 seconds, as follows: + * + * T0: pdp1 runs audit (15 seconds), then sleeps for five minutes (300 seconds) + * pdp2 recognizes that pdp1 is stale (30 seconds) and runs its audit (15 seconds) + * pdp3 recognizes that pdp2 is stale (30 seconds) and runs its audit (15 seconds) + * + * 2) Five minutes after T0, at T1, pdp1 wakes up and the above sequence begins again, + * which should take another 115 seconds: + * + * T1: pdp1 runs audit (15 seconds), then sleeps for two minutes (300 seconds) + * pdp2 wakes up, resets auditCompleted and sleeps (5 seconds), recognizes that pdp1 is stale (30 seconds) and runs its audit (15 seconds) + * pdp3 wakes up, resets auditCompleted and sleeps (5 seconds), recognizes that pdp2 is stale (30 seconds) and runs its audit (15 seconds) + * + * So, the entire sequence should take 15 + 300 + 115 = 430 seconds + * Adding a fudge factor, we sleep for 450 seconds + */ + Thread.sleep(450000); + + + logger.info("testFiveMinuteAuditPeriod: Stopping all three audit threads"); + integrityAudit.stopAuditThread(); + + integrityAudit.stopAuditThread(); + integrityAudit2.stopAuditThread(); + integrityAudit3.stopAuditThread(); + + FileInputStream fstream = new FileInputStream(AUDIT_PERIOD_TEST_LOG); + BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); + String strLine; + int startIndex; + int endIndex; + ArrayList expectedResult = new ArrayList(Arrays.asList("pdp1", "pdp2", "pdp3", "pdp1", "pdp2", "pdp3")); + ArrayList delegates = new ArrayList(); + while ((strLine = br.readLine()) != null) { + /* parse strLine to obtain what you want */ + if (strLine.contains("Starting audit simulation for resourceName=")) { + startIndex = strLine.indexOf("resourceName=") + 13; + endIndex = strLine.indexOf(","); + + String rName = strLine.substring(startIndex, endIndex); + + delegates.add(rName); + } + } + + for (String delegate: delegates) { + logger.info("testFiveMinuteAuditPeriod: delegate: " + delegate); + } + + fstream.close(); + + assertTrue("delegate count only " + delegates.size(), delegates.size() >= 6); + assertTrue("delegate 0 is " + expectedResult.get(0), expectedResult.get(0).equals(delegates.get(0))); + assertTrue("delegate 1 is " + expectedResult.get(1), expectedResult.get(1).equals(delegates.get(1))); + assertTrue("delegate 2 is " + expectedResult.get(2), expectedResult.get(2).equals(delegates.get(2))); + assertTrue("delegate 3 is " + expectedResult.get(3), expectedResult.get(3).equals(delegates.get(3))); + assertTrue("delegate 4 is " + expectedResult.get(4), expectedResult.get(4).equals(delegates.get(4))); + assertTrue("delegate 5 is " + expectedResult.get(5), expectedResult.get(5).equals(delegates.get(5))); + + logger.info("testFiveMinuteAuditPeriod: Exiting"); + } + + /* + * Verifies (via log parsing) that when an audit period of 20 seconds is + * specified, there is a 20 second interval between the audits run + * on each of three different entities. + */ + @Ignore + @Test + public void testTwentySecondAuditPeriod() throws Exception { + + logger.info("testTwentySecondAuditPeriod: Entering"); + + properties.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "20"); + + /* + * Start audit for pdp1. + */ + IntegrityAudit integrityAudit = new IntegrityAudit(resourceName, + persistenceUnit, properties); + integrityAudit.startAuditThread(); + + /* + * Start audit for pdp2. + */ + Properties properties2 = new Properties(); + properties2.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER); + properties2.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL); + properties2.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER); + properties2.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD); + properties2.put(IntegrityAuditProperties.SITE_NAME, "SiteA"); + properties2.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml"); + properties2.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "20"); + String persistenceUnit2 = "testPU"; + String resourceName2 = "pdp2"; + IntegrityAudit integrityAudit2 = new IntegrityAudit(resourceName2, persistenceUnit2, properties2); + integrityAudit2.startAuditThread(); + + /* + * Start audit for pdp3. + */ + Properties properties3 = new Properties(); + properties3.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER); + properties3.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL); + properties3.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER); + properties3.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD); + properties3.put(IntegrityAuditProperties.SITE_NAME, "SiteA"); + properties3.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml"); + properties3.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "20"); + String persistenceUnit3 = "testPU"; + String resourceName3 = "pdp3"; + IntegrityAudit integrityAudit3 = new IntegrityAudit(resourceName3, persistenceUnit3, properties3); + integrityAudit3.startAuditThread(); + + + /* + * 1) All three audit run once. + * + * pdp1 runs audit (15 seconds), then goes into 20 second sleep cycles + * pdp2 recognizes that pdp1 is stale (30 seconds), runs its audit (15 seconds), then goes into 20 second sleep cycles + * pdp3 recognizes that pdp2 is stale (30 seconds), runs its audit (15 seconds), then goes into 20 second sleep cycles + * + * 2) Eventually pdp2 gets stale, pdp1 recognizes this and cycle begins again. + * + * So, we allow 15 + (5 * 45) = 240 seconds plus a fudge factor. + * + */ + Thread.sleep(250000); + + + logger.info("testTwentySecondAuditPeriod: Stopping all three audit threads"); + integrityAudit.stopAuditThread(); + + integrityAudit.stopAuditThread(); + integrityAudit2.stopAuditThread(); + integrityAudit3.stopAuditThread(); + + FileInputStream fstream = new FileInputStream(AUDIT_PERIOD_TEST_LOG); + BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); + String strLine; + int startIndex; + int endIndex; + ArrayList expectedResult = new ArrayList(Arrays.asList("pdp1", "pdp2", "pdp3", "pdp1", "pdp2", "pdp3")); + ArrayList delegates = new ArrayList(); + while ((strLine = br.readLine()) != null) { + /* parse strLine to obtain what you want */ + if (strLine.contains("Starting audit simulation for resourceName=")) { + startIndex = strLine.indexOf("resourceName=") + 13; + endIndex = strLine.indexOf(","); + + String rName = strLine.substring(startIndex, endIndex); + + delegates.add(rName); + } + } + + for (String delegate: delegates) { + logger.info("testTwentySecondAuditPeriod: delegate: " + delegate); + } + + fstream.close(); + + assertTrue("delegate count only " + delegates.size(), delegates.size() >= 6); + assertTrue("delegate 0 is " + expectedResult.get(0), expectedResult.get(0).equals(delegates.get(0))); + assertTrue("delegate 1 is " + expectedResult.get(1), expectedResult.get(1).equals(delegates.get(1))); + assertTrue("delegate 2 is " + expectedResult.get(2), expectedResult.get(2).equals(delegates.get(2))); + assertTrue("delegate 3 is " + expectedResult.get(3), expectedResult.get(3).equals(delegates.get(3))); + assertTrue("delegate 4 is " + expectedResult.get(4), expectedResult.get(4).equals(delegates.get(4))); + assertTrue("delegate 5 is " + expectedResult.get(5), expectedResult.get(5).equals(delegates.get(5))); + + logger.info("testTwentySecondAuditPeriod: Exiting"); + } + +} diff --git a/integrity-audit/src/test/java/org/onap/policy/common/ia/test/DbAuditCompareEntriesTest.java b/integrity-audit/src/test/java/org/onap/policy/common/ia/test/DbAuditCompareEntriesTest.java new file mode 100644 index 00000000..8a811366 --- /dev/null +++ b/integrity-audit/src/test/java/org/onap/policy/common/ia/test/DbAuditCompareEntriesTest.java @@ -0,0 +1,617 @@ +/*- + * ============LICENSE_START======================================================= + * Integrity Audit + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.common.ia.test; + +import static org.junit.Assert.*; + +import java.io.FileOutputStream; +import java.util.Date; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Properties; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.EntityTransaction; +import javax.persistence.Persistence; + + +//import org.apache.commons.logging.Log; +//import org.apache.commons.logging.LogFactory; +import org.junit.After; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; + +import org.onap.policy.common.ia.DbAudit; +import org.onap.policy.common.ia.DbDAO; +import org.onap.policy.common.ia.IntegrityAudit; +import org.onap.policy.common.ia.IntegrityAuditProperties; +import org.onap.policy.common.ia.jpa.IntegrityAuditEntity; +import org.onap.policy.common.ia.test.jpa.IaTestEntity; +import org.onap.policy.common.ia.test.jpa.PersonTest; +import org.onap.policy.common.logging.flexlogger.FlexLogger; +import org.onap.policy.common.logging.flexlogger.Logger; + +/* + * All JUnits are designed to run in the local development environment + * where they have write privileges and can execute time-sensitive + * tasks. + */ +public class DbAuditCompareEntriesTest { + + private static Logger logger = FlexLogger.getLogger(DbAuditCompareEntriesTest.class); + private DbDAO dbDAO; + private static String persistenceUnit; + private static Properties properties; + private static String resourceName; + private String dbDriver; + private String dbUrl; + private String dbUser; + private String dbPwd; + private String siteName; + private String nodeType; + private static final String TEST_LOG = "./testingLogs/common-modules/integrity-audit/debug.log"; + + @Before + public void setUp() throws Exception { + System.out.println("setUp: Clearing IntegrityAudit.log"); + //FileOutputStream fstream = new FileOutputStream("IntegrityAudit.log"); + FileOutputStream fstream = new FileOutputStream(TEST_LOG); + fstream.close(); + + logger.info("setUp: Entering"); + + IntegrityAudit.isUnitTesting = true; + + properties = new Properties(); + properties.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER); + properties.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL); + properties.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER); + properties.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD); + properties.put(IntegrityAuditProperties.SITE_NAME, "SiteA"); + properties.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml"); + + dbDriver = IntegrityAuditProperties.DEFAULT_DB_DRIVER; + dbUrl = IntegrityAuditProperties.DEFAULT_DB_URL; + dbUser = IntegrityAuditProperties.DEFAULT_DB_USER; + dbPwd = IntegrityAuditProperties.DEFAULT_DB_PWD; + siteName = "SiteA"; + nodeType = "pdp_xacml"; + persistenceUnit = "testPU"; + resourceName = "pdp1"; + + logger.info("setUp: Exiting"); + } + + /* + * Clean up DB after each test. + */ + @After + public void tearDown() throws Exception { + logger.info("tearDown: Entering"); + + logger.info("tearDown: Exiting"); + } + + /* + * Tests that a comparison between hashsets is successful if + * the entries match + */ + @Ignore + @Test + public void testSuccessfulComparison() throws Exception { + logger.info("testSuccessfulComparison: Entering"); + + dbDAO = new DbDAO(resourceName, persistenceUnit, properties); + DbAudit dbAudit = new DbAudit(dbDAO); + + String className = null; + //There is only one entry IntegrityAuditEntity, but we will check anyway + HashSet classNameSet = dbDAO.getPersistenceClassNames(); + for(String c : classNameSet){ + if (c.equals("org.onap.policy.common.ia.jpa.IntegrityAuditEntity")){ + className = c; + } + } + String resourceName1 = resourceName; + String resourceName2 = resourceName; + + IntegrityAuditEntity entry1 = new IntegrityAuditEntity(); + IntegrityAuditEntity entry2 = new IntegrityAuditEntity(); + Date date = new Date(); + + /* + * Two entries with the same field values + */ + entry1.setDesignated(false); + entry1.setJdbcDriver(dbDriver); + entry1.setJdbcPassword(dbPwd); + entry1.setJdbcUrl(dbUrl); + entry1.setJdbcUser(dbUser); + entry1.setLastUpdated(date); + entry1.setNodeType(nodeType); + entry1.setPersistenceUnit(persistenceUnit); + entry1.setResourceName(resourceName1); + entry1.setSite(siteName); + + entry2.setDesignated(false); + entry2.setJdbcDriver(dbDriver); + entry2.setJdbcPassword(dbPwd); + entry2.setJdbcUrl(dbUrl); + entry2.setJdbcUser(dbUser); + entry2.setLastUpdated(date); + entry2.setNodeType(nodeType); + entry2.setPersistenceUnit(persistenceUnit); + entry2.setResourceName(resourceName2); + entry2.setSite(siteName); + + dbAudit.writeAuditDebugLog(className, resourceName1, resourceName2, entry1, entry2); + + HashMap myEntries = new HashMap(); + HashMap theirEntries = new HashMap(); + + myEntries.put("pdp1", entry1); + theirEntries.put("pdp1", entry2); + + HashSet result = dbAudit.compareEntries(myEntries, theirEntries); + + /* + * Assert that there are no mismatches returned + */ + assertTrue(result.isEmpty()); + + logger.info("testSuccessfulComparison: Exit"); + } + + /* + * Tests that an error is detected if an entry in one hashset doesn't + * match the other + */ + @Ignore + @Test + public void testComparisonError() throws Exception { + logger.info("testComparisonError: Entering"); + + dbDAO = new DbDAO(resourceName, persistenceUnit, properties); + DbAudit dbAudit = new DbAudit(dbDAO); + + String resourceName1 = resourceName; + String resourceName2 = resourceName; + + IntegrityAuditEntity entry1 = new IntegrityAuditEntity(); + IntegrityAuditEntity entry2 = new IntegrityAuditEntity(); + Date date = new Date(); + + /* + * Create two entries with different designated values + */ + entry1.setDesignated(false); + entry1.setJdbcDriver(dbDriver); + entry1.setJdbcPassword(dbPwd); + entry1.setJdbcUrl(dbUrl); + entry1.setJdbcUser(dbUser); + entry1.setLastUpdated(date); + entry1.setNodeType(nodeType); + entry1.setPersistenceUnit(persistenceUnit); + entry1.setResourceName(resourceName1); + entry1.setSite(siteName); + + entry2.setDesignated(true); + entry2.setJdbcDriver(dbDriver); + entry2.setJdbcPassword(dbPwd); + entry2.setJdbcUrl(dbUrl); + entry2.setJdbcUser(dbUser); + entry2.setLastUpdated(date); + entry2.setNodeType(nodeType); + entry2.setPersistenceUnit(persistenceUnit); + entry2.setResourceName(resourceName2); + entry2.setSite(siteName); + + HashMap myEntries = new HashMap(); + HashMap theirEntries = new HashMap(); + + myEntries.put("pdp1", entry1); + theirEntries.put("pdp1", entry2); + + HashSet result = dbAudit.compareEntries(myEntries, theirEntries); + + /* + * Assert that there was one mismatch + */ + assertEquals(1, result.size()); + + logger.info("testComparisonError: Exit"); + } + + /* + * Tests that a mismatch/miss entry is detected if there are missing entries in + * one or both of the hashsets + */ + @Ignore + @Test + public void testCompareMissingEntries() throws Exception { + logger.info("testCompareMissingEntries: Entering"); + + dbDAO = new DbDAO(resourceName, persistenceUnit, properties); + DbAudit dbAudit = new DbAudit(dbDAO); + + String resourceName1 = resourceName; + String resourceName2 = resourceName; + + IntegrityAuditEntity entry1 = new IntegrityAuditEntity(); + IntegrityAuditEntity entry2 = new IntegrityAuditEntity(); + IntegrityAuditEntity entry3 = new IntegrityAuditEntity(); + IntegrityAuditEntity entry4 = new IntegrityAuditEntity(); + + Date date = new Date(); + + /* + * 4 entries, one mismatch, two miss entries + */ + entry1.setDesignated(false); + entry1.setJdbcDriver(dbDriver); + entry1.setJdbcPassword(dbPwd); + entry1.setJdbcUrl(dbUrl); + entry1.setJdbcUser(dbUser); + entry1.setLastUpdated(date); + entry1.setNodeType(nodeType); + entry1.setPersistenceUnit(persistenceUnit); + entry1.setResourceName(resourceName1); + entry1.setSite(siteName); + + entry2.setDesignated(true); + entry2.setJdbcDriver(dbDriver); + entry2.setJdbcPassword(dbPwd); + entry2.setJdbcUrl(dbUrl); + entry2.setJdbcUser(dbUser); + entry2.setLastUpdated(date); + entry2.setNodeType(nodeType); + entry2.setPersistenceUnit(persistenceUnit); + entry2.setResourceName(resourceName2); + entry2.setSite(siteName); + + entry3.setDesignated(false); + entry3.setJdbcDriver(dbDriver); + entry3.setJdbcPassword(dbPwd); + entry3.setJdbcUrl(dbUrl); + entry3.setJdbcUser(dbUser); + entry3.setLastUpdated(date); + entry3.setNodeType(nodeType); + entry3.setPersistenceUnit(persistenceUnit); + entry3.setResourceName(resourceName2); + entry3.setSite("SiteB"); + + entry4.setDesignated(false); + entry4.setJdbcDriver(dbDriver); + entry4.setJdbcPassword(dbPwd); + entry4.setJdbcUrl(dbUrl); + entry4.setJdbcUser(dbUser); + entry4.setLastUpdated(date); + entry4.setNodeType(nodeType); + entry4.setPersistenceUnit(persistenceUnit); + entry4.setResourceName(resourceName2); + entry4.setSite("SiteB"); + + HashMap myEntries = new HashMap(); + HashMap theirEntries = new HashMap(); + + myEntries.put("0", entry1); + myEntries.put("1", entry3); + theirEntries.put("0", entry2); + theirEntries.put("2", entry4); + + HashSet mismatchResult = dbAudit.compareEntries(myEntries, theirEntries); + + /* + * Assert 3 mismatches/missing entries were found + */ + assertEquals(3, mismatchResult.size()); + + logger.info("testCompareMissingEntries: Exit"); + } + + /* + * Tests that comparison algorithm works for each entity in the hashsets + */ + @Ignore + @Test + public void testCompareAllHashEntities() throws Exception { + logger.info("testCompareAllHashEntities: Entering"); + + dbDAO = new DbDAO(resourceName, persistenceUnit, properties); + DbAudit dbAudit = new DbAudit(dbDAO); + + HashSet classNameSet = dbDAO.getPersistenceClassNames(); + HashSet mismatchResult = new HashSet(); + for(String c : classNameSet) { + if (c.equals("org.onap.policy.common.ia.jpa.IntegrityAuditEntity")){ + String resourceName1 = resourceName; + String resourceName2 = resourceName; + + IntegrityAuditEntity entry1 = new IntegrityAuditEntity(); + IntegrityAuditEntity entry2 = new IntegrityAuditEntity(); + Date date = new Date(); + + /* + * Two entries with the same field values + */ + entry1.setDesignated(false); + entry1.setJdbcDriver(dbDriver); + entry1.setJdbcPassword(dbPwd); + entry1.setJdbcUrl(dbUrl); + entry1.setJdbcUser(dbUser); + entry1.setLastUpdated(date); + entry1.setNodeType(nodeType); + entry1.setPersistenceUnit(persistenceUnit); + entry1.setResourceName(resourceName1); + entry1.setSite(siteName); + + entry2.setDesignated(false); + entry2.setJdbcDriver(dbDriver); + entry2.setJdbcPassword(dbPwd); + entry2.setJdbcUrl(dbUrl); + entry2.setJdbcUser(dbUser); + entry2.setLastUpdated(date); + entry2.setNodeType(nodeType); + entry2.setPersistenceUnit(persistenceUnit); + entry2.setResourceName(resourceName2); + entry2.setSite(siteName); + + HashMap myEntries = new HashMap(); + HashMap theirEntries = new HashMap(); + + myEntries.put("pdp1", entry1); + theirEntries.put("pdp1", entry2); + + mismatchResult = dbAudit.compareEntries(myEntries, theirEntries); + + /* + * Assert there was no mismatches + */ + assertTrue(mismatchResult.isEmpty()); + } + else if (c.equals("org.onap.policy.common.ia.test.jpa.IaTestEntity")) { + IaTestEntity iate = new IaTestEntity(); + IaTestEntity iate2 = new IaTestEntity(); + IaTestEntity iate3 = new IaTestEntity(); + IaTestEntity iate4 = new IaTestEntity(); + + Date date = new Date(); + + /* + * Four entries, 2 mismatches + */ + iate.setCreatedBy("Ford"); + iate.setModifiedBy("Ford"); + iate.setModifiedDate(date); + + iate2.setCreatedBy("Ford"); + iate2.setModifiedBy("Zaphod"); + iate2.setModifiedDate(date); + + iate3.setCreatedBy("Zaphod"); + iate3.setModifiedBy("Ford"); + iate3.setModifiedDate(date); + + iate4.setCreatedBy("Ford"); + iate4.setModifiedBy("Ford"); + iate4.setModifiedDate(date); + + HashMap myEntries = new HashMap(); + HashMap theirEntries = new HashMap(); + + myEntries.put("0", iate); + myEntries.put("1", iate2); + theirEntries.put("0", iate3); + theirEntries.put("1", iate4); + + mismatchResult = dbAudit.compareEntries(myEntries, theirEntries); + + /* + * Assert that there is 2 mismatches + */ + assertEquals(2, mismatchResult.size()); + } + } + + logger.info("testCompareAllHashEntities: Exit"); + } + + /* + * Tests that comparison algorithm works for each entity in the database + */ + @Ignore + @Test + public void testCompareAllDbEntities() throws Exception { + logger.info("testCompareAllDbEntities: Entering"); + + logger.info("Setting up DB"); + + IntegrityAudit.isUnitTesting = true; + + properties = new Properties(); + properties.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER); + properties.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL); + properties.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER); + properties.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD); + properties.put(IntegrityAuditProperties.SITE_NAME, "SiteA"); + properties.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml"); + + Properties properties2 = new Properties(); + properties2.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER); + properties2.put(IntegrityAuditProperties.DB_URL, "jdbc:h2:file:./sql/iaTest2"); + properties2.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER); + properties2.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD); + properties2.put(IntegrityAuditProperties.SITE_NAME, "SiteA"); + properties2.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml"); + + dbDriver = IntegrityAuditProperties.DEFAULT_DB_DRIVER; + dbUrl = IntegrityAuditProperties.DEFAULT_DB_URL; + dbUser = IntegrityAuditProperties.DEFAULT_DB_USER; + dbPwd = IntegrityAuditProperties.DEFAULT_DB_PWD; + siteName = "SiteA"; + nodeType = "pdp_xacml"; + persistenceUnit = "testPU"; + resourceName = "pdp1"; + + //Clean up the two DBs + EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit, properties); + EntityManagerFactory emf2 = Persistence.createEntityManagerFactory(persistenceUnit, properties2); + + EntityManager em = emf.createEntityManager(); + EntityManager em2 = emf2.createEntityManager(); + // Start a transaction + EntityTransaction et = em.getTransaction(); + EntityTransaction et2 = em2.getTransaction(); + + /* + * Delete entries in first DB + */ + et.begin(); + + // Clean the IntegrityAuditEntity table + em.createQuery("Delete from IntegrityAuditEntity").executeUpdate(); + + // commit transaction + et.commit(); + + et.begin(); + + // Clean the IaTestEntity table + em.createQuery("Delete from IaTestEntity").executeUpdate(); + + // commit transaction + et.commit(); + em.close(); + + /* + * Delete entries in second DB + */ + et2.begin(); + + // Clean the IntegrityAuditEntity table + em2.createQuery("Delete from IntegrityAuditEntity").executeUpdate(); + + // commit transaction + et2.commit(); + + et2.begin(); + + // Clean the IaTestEntity table + em2.createQuery("Delete from IaTestEntity").executeUpdate(); + + // commit transaction + et2.commit(); + em2.close(); + logger.info("Exiting set up"); + + // Add entries into DB1 + dbDAO = new DbDAO(resourceName, persistenceUnit, properties); + new DbDAO("pdp2", persistenceUnit, properties); + DbAudit dbAudit = new DbAudit(dbDAO); + + // Add entries into DB2 + DbDAO dbDAO3 = new DbDAO(resourceName, persistenceUnit, properties2); + new DbDAO("pdp2", persistenceUnit, properties2); + + // Pull all entries and compare + HashSet classNameSet = dbDAO.getPersistenceClassNames(); + HashMap myEntries; + HashMap theirEntries; + HashSet mismatchResult = new HashSet(); + String className; + for(String c : classNameSet) { + className = c; + logger.info("classNameSet entry = " + c); + myEntries = dbDAO.getAllEntries(persistenceUnit, properties, className); + theirEntries = dbDAO3.getAllEntries(persistenceUnit, properties2, className); + mismatchResult = dbAudit.compareEntries(myEntries, theirEntries); + if(className.contains("IntegrityAuditEntity")){ + break; + } + } + + // Assert that there is 2 mismatches between IntegrityAuditEntity tables + assertEquals(2, mismatchResult.size()); + + logger.info("testCompareAllDbEntities: Exit"); + } + + /* + * Tests that differences in embedded classes are still caught + */ + @Ignore + @Test + public void testEmbeddedClass() throws Exception { + logger.info("testEmbeddedClasses: Entering"); + + dbDAO = new DbDAO(resourceName, persistenceUnit, properties); + DbAudit dbAudit = new DbAudit(dbDAO); + + String className = null; + //There is only one entry IntegrityAuditEntity, but we will check anyway + HashSet classNameSet = dbDAO.getPersistenceClassNames(); + for(String c : classNameSet){ + if (c.equals("org.onap.policy.common.ia.test.jpa.IaTestEntity")){ + className = c; + } + } + + IaTestEntity iate = new IaTestEntity(); + IaTestEntity iate2 = new IaTestEntity(); + + Date date = new Date(); + + PersonTest person = new PersonTest("Ford", "Prefect", 21); + PersonTest person2 = new PersonTest("Zaphod", "Beeblebrox", 25); + + /* + * Two entries, 1 mismatch + */ + iate.setCreatedBy("Ford"); + iate.setModifiedBy("Zaphod"); + iate.setModifiedDate(date); + iate.setPersonTest(person); + + iate2.setCreatedBy("Ford"); + iate2.setModifiedBy("Zaphod"); + iate2.setModifiedDate(date); + iate2.setPersonTest(person2); + + dbAudit.writeAuditDebugLog(className, "resource1", "resource2", iate, iate2); + + HashMap myEntries = new HashMap(); + HashMap theirEntries = new HashMap(); + + myEntries.put("0", iate); + theirEntries.put("0", iate2); + + HashSet result = dbAudit.compareEntries(myEntries, theirEntries); + + /* + * Assert that there are no mismatches returned + */ + assertTrue(!result.isEmpty()); + + logger.info("testEmbeddedClasses: Exit"); + } +} diff --git a/integrity-audit/src/test/java/org/onap/policy/common/ia/test/DbAuditTest.java b/integrity-audit/src/test/java/org/onap/policy/common/ia/test/DbAuditTest.java new file mode 100644 index 00000000..fd1172a8 --- /dev/null +++ b/integrity-audit/src/test/java/org/onap/policy/common/ia/test/DbAuditTest.java @@ -0,0 +1,753 @@ +/*- + * ============LICENSE_START======================================================= + * Integrity Audit + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.common.ia.test; + +import static org.junit.Assert.*; + +import java.io.BufferedReader; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.InputStreamReader; +import java.util.Date; +import java.util.List; +import java.util.Properties; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.EntityTransaction; +import javax.persistence.Persistence; +import javax.persistence.Query; + + +//import org.apache.commons.logging.Log; +//import org.apache.commons.logging.LogFactory; +import org.junit.After; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; + +import org.onap.policy.common.ia.DbAudit; +import org.onap.policy.common.ia.DbAuditException; +import org.onap.policy.common.ia.DbDAO; +import org.onap.policy.common.ia.DbDaoTransactionException; +import org.onap.policy.common.ia.IntegrityAudit; +import org.onap.policy.common.ia.IntegrityAuditProperties; +import org.onap.policy.common.ia.jpa.IntegrityAuditEntity; +import org.onap.policy.common.logging.flexlogger.FlexLogger; +import org.onap.policy.common.logging.flexlogger.Logger; + +/* + * All JUnits are designed to run in the local development environment + * where they have write privileges and can execute time-sensitive + * tasks. + */ +public class DbAuditTest { + + private static Logger logger = FlexLogger.getLogger(DbAuditTest.class); + + private DbDAO dbDAO; + private static String persistenceUnit; + private static Properties properties; + private static String resourceName; + private String dbDriver; + private String dbUrl; + private String dbUser; + private String dbPwd; + private String siteName; + private String nodeType; + private static final String TEST_LOG = "./testingLogs/common-modules/integrity-audit/debug.log"; + private static final String ERROR_LOG = "./testingLogs/common-modules/integrity-audit/error.log"; + + public void cleanLog() throws Exception{ + + logger.debug("cleanLog: enter"); + //FileOutputStream fstream = new FileOutputStream("IntegrityAudit.log"); + FileOutputStream fstream = new FileOutputStream(TEST_LOG); + fstream.close(); + fstream = new FileOutputStream(ERROR_LOG); + fstream.close(); + logger.debug("cleanLog: exit"); + } + + public void cleanDb(String persistenceUnit, Properties properties){ + logger.debug("cleanDb: enter"); + + EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit, properties); + + EntityManager em = emf.createEntityManager(); + // Start a transaction + EntityTransaction et = em.getTransaction(); + + et.begin(); + + // Clean up the DB + em.createQuery("Delete from IntegrityAuditEntity").executeUpdate(); + + // commit transaction + et.commit(); + em.close(); + logger.debug("cleanDb: exit"); + } + + + @Before + public void setUp() throws Exception { + + logger.info("setUp: Entering"); + + IntegrityAudit.isUnitTesting = true; + IntegrityAuditEntity.isUnitTesting = true; + + properties = new Properties(); + properties.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER); + properties.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL); + properties.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER); + properties.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD); + properties.put(IntegrityAuditProperties.SITE_NAME, "SiteA"); + properties.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml"); + + dbDriver = IntegrityAuditProperties.DEFAULT_DB_DRIVER; + dbUrl = IntegrityAuditProperties.DEFAULT_DB_URL; + dbUser = IntegrityAuditProperties.DEFAULT_DB_USER; + dbPwd = IntegrityAuditProperties.DEFAULT_DB_PWD; + siteName = "SiteA"; + nodeType = "pdp_xacml"; + persistenceUnit = "testPU"; + resourceName = "pdp1"; + + logger.info("setUp: Exiting"); + + } + + @After + public void tearDown() throws Exception { + + logger.info("tearDown: Entering"); + + //cleanDb(persistenceUnit, properties); + + logger.info("tearDown: Exiting"); + } + + @Ignore + @Test + public void runAllTests() throws Exception{ + //The order is important - I haven't figured out why, but it is. + mismatchTest(); + noEntitiesTest(); + oneEntityTest(); + } + + + /* + * Tests printing an error to the log in the event where + * there are no entities saved in the database + */ + public void noEntitiesTest() throws Exception { + cleanLog(); + cleanDb(persistenceUnit, properties); + + logger.info("noEntitiesTest: Entering"); + + // Boolean to assert there are no entries found + Boolean noEntities = false; + + dbDAO = new DbDAO(resourceName, persistenceUnit, properties); + dbDAO.deleteAllIntegrityAuditEntities(); + try { + DbAudit dbAudit = new DbAudit(dbDAO); + dbAudit.dbAudit(resourceName, persistenceUnit, nodeType); + } + catch (DbAuditException e) { + noEntities = true; + } + + dbDAO.deleteAllIntegrityAuditEntities(); + + logger.info("noEntitiesTest: No entities are persisted in the database"); + + // Assert there are no entities retrieved + assertTrue(noEntities); + + logger.info("noEntitiesTest: Exit"); + } + + /* + * Tests the detection of only one entry in the database + */ + public void oneEntityTest() throws Exception{ + cleanLog(); + cleanDb(persistenceUnit, properties); + + logger.info("oneEntityTest: Entering"); + + // Add one entry in the database + dbDAO = new DbDAO(resourceName, persistenceUnit, properties); + DbAudit dbAudit = new DbAudit(dbDAO); + dbAudit.dbAudit(resourceName, persistenceUnit, nodeType); + + List iaeList = dbDAO.getIntegrityAuditEntities(persistenceUnit, nodeType); + logger.info("List size: " + iaeList.size()); + + //FileInputStream fstream = new FileInputStream("IntegrityAudit.log"); + FileInputStream fstream = new FileInputStream(TEST_LOG); + BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); + String strLine; + Boolean oneEntity = false; + while ((strLine = br.readLine()) != null) { + //parse strLine to obtain what you want + + if (strLine.contains("DbAudit: Found only one IntegrityAuditEntity entry:")) { + oneEntity = true; + } + + } + if(oneEntity){ + logger.info("oneEntityTest: One entity is persisted in the database"); + }else{ + logger.info("oneEntityTest: No entities are persisted in the database"); + } + + + // Assert there is only one entry + assertTrue(oneEntity); + + br.close(); + + logger.info("oneEntityTest: Exit"); + } + + /* + * Tests reporting mismatches and misentries using the error log + */ + @SuppressWarnings("unused") + public void mismatchTest() throws Exception{ + cleanLog(); + logger.info("mismatchTest: Entering"); + + // Properties for DB2 + Properties properties2 = new Properties(); + properties2.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER); + properties2.put(IntegrityAuditProperties.DB_URL, "jdbc:h2:file:./sql/iaTest2"); + properties2.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER); + properties2.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD); + properties2.put(IntegrityAuditProperties.SITE_NAME, "SiteA"); + properties2.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml"); + + //Clean the DBs before we begin + cleanDb(persistenceUnit, properties); + cleanDb(persistenceUnit, properties2); + + // Entries in DB1 + dbDAO = new DbDAO(resourceName, persistenceUnit, properties); + DbDAO dbDAO2 = new DbDAO("pdp2", persistenceUnit, properties); + + /* + * dbDAO3 is a mismatch entry, dbDAO7 is a misentry + */ + DbDAO dbDAO3 = new DbDAO("pdp3", persistenceUnit, properties); + DbDAO dbDAO7 = new DbDAO("pdp4", persistenceUnit, properties); + Date date = new Date(); + + EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit, properties); + + /* + * Update DB url's in DB1 to point to DB2 + */ + try{ + EntityManager em = emf.createEntityManager(); + // Start a transaction + EntityTransaction et = em.getTransaction(); + + et.begin(); + + // if IntegrityAuditEntity entry exists for resourceName and PU, update it. If not found, create a new entry + Query iaequery = em.createQuery("Select i from IntegrityAuditEntity i where i.resourceName=:rn and i.persistenceUnit=:pu"); + iaequery.setParameter("rn", "pdp2"); + iaequery.setParameter("pu", persistenceUnit); + + @SuppressWarnings("rawtypes") + List iaeList = iaequery.getResultList(); + IntegrityAuditEntity iae = null; + + //If it already exists, we just want to update the properties and lastUpdated date + if(!iaeList.isEmpty()){ + //ignores multiple results + iae = (IntegrityAuditEntity) iaeList.get(0); + // refresh the object from DB in case cached data was returned + em.refresh(iae); + logger.info("Resource: " + "pdp2" + " with PersistenceUnit: " + persistenceUnit + + " exists and entry be updated"); + }else{ + // If it does not exist, we also must add teh resourceName, persistenceUnit and designated values + logger.info("Adding resource " + "pdp2" + " with PersistenceUnit: " + persistenceUnit + + " to IntegrityAuditEntity table"); + iae = new IntegrityAuditEntity(); + iae.setResourceName("pdp2"); + iae.setPersistenceUnit(persistenceUnit); + iae.setDesignated(false); + } + + //update/set properties in entry + iae.setSite(siteName); + iae.setNodeType(nodeType); + iae.setLastUpdated(date); + iae.setCreatedDate(date); + iae.setJdbcDriver(dbDriver); + iae.setJdbcPassword(dbPwd); + iae.setJdbcUrl("jdbc:h2:file:./sql/iaTest2"); + iae.setJdbcUser(dbUser); + + em.persist(iae); + // flush to the DB + em.flush(); + + // commit transaction + et.commit(); + + et.begin(); + + // if IntegrityAuditEntity entry exists for resourceName and PU, update it. If not found, create a new entry + iaequery = em.createQuery("Select i from IntegrityAuditEntity i where i.resourceName=:rn and i.persistenceUnit=:pu"); + iaequery.setParameter("rn", "pdp1"); + iaequery.setParameter("pu", persistenceUnit); + + @SuppressWarnings("rawtypes") + List iaeList2 = iaequery.getResultList(); + iae = null; + + //If it already exists, we just want to update the properties and lastUpdated date + if(!iaeList2.isEmpty()){ + //ignores multiple results + iae = (IntegrityAuditEntity) iaeList2.get(0); + // refresh the object from DB in case cached data was returned + em.refresh(iae); + logger.info("Resource: " + "pdp1" + " with PersistenceUnit: " + persistenceUnit + + " exists and entry be updated"); + }else{ + // If it does not exist, we also must add teh resourceName, persistenceUnit and designated values + logger.info("Adding resource " + "pdp1" + " with PersistenceUnit: " + persistenceUnit + + " to IntegrityAuditEntity table"); + iae = new IntegrityAuditEntity(); + iae.setResourceName("pdp1"); + iae.setPersistenceUnit(persistenceUnit); + iae.setDesignated(false); + } + + //update/set properties in entry + iae.setSite(siteName); + iae.setNodeType(nodeType); + iae.setLastUpdated(date); + iae.setCreatedDate(date); + iae.setJdbcDriver(dbDriver); + iae.setJdbcPassword(dbPwd); + iae.setJdbcUrl("jdbc:h2:file:./sql/iaTest2"); + iae.setJdbcUser(dbUser); + + em.persist(iae); + // flush to the DB + em.flush(); + + // commit transaction + et.commit(); + + et.begin(); + + // if IntegrityAuditEntity entry exists for resourceName and PU, update it. If not found, create a new entry + iaequery = em.createQuery("Select i from IntegrityAuditEntity i where i.resourceName=:rn and i.persistenceUnit=:pu"); + iaequery.setParameter("rn", "pdp3"); + iaequery.setParameter("pu", persistenceUnit); + + @SuppressWarnings("rawtypes") + List iaeList3 = iaequery.getResultList(); + iae = null; + + //If it already exists, we just want to update the properties and lastUpdated date + if(!iaeList3.isEmpty()){ + //ignores multiple results + iae = (IntegrityAuditEntity) iaeList3.get(0); + // refresh the object from DB in case cached data was returned + em.refresh(iae); + logger.info("Resource: " + "pdp3" + " with PersistenceUnit: " + persistenceUnit + + " exists and entry be updated"); + }else{ + // If it does not exist, we also must add the resourceName, persistenceUnit and designated values + logger.info("Adding resource " + "pdp3" + " with PersistenceUnit: " + persistenceUnit + + " to IntegrityAuditEntity table"); + iae = new IntegrityAuditEntity(); + iae.setResourceName("pdp3"); + iae.setPersistenceUnit(persistenceUnit); + iae.setDesignated(false); + } + + //update/set properties in entry + iae.setSite(siteName); + iae.setNodeType(nodeType); + iae.setLastUpdated(date); + iae.setCreatedDate(date); + iae.setJdbcDriver(dbDriver); + iae.setJdbcPassword(dbPwd); + iae.setJdbcUrl(dbUrl); + iae.setJdbcUser(dbUser); + + em.persist(iae); + // flush to the DB + em.flush(); + + // commit transaction + et.commit(); + + et.begin(); + + // if IntegrityAuditEntity entry exists for resourceName and PU, update it. If not found, create a new entry + iaequery = em.createQuery("Select i from IntegrityAuditEntity i where i.resourceName=:rn and i.persistenceUnit=:pu"); + iaequery.setParameter("rn", "pdp4"); + iaequery.setParameter("pu", persistenceUnit); + + @SuppressWarnings("rawtypes") + List iaeList4 = iaequery.getResultList(); + iae = null; + + //If it already exists, we just want to update the properties and lastUpdated date + if(!iaeList4.isEmpty()){ + //ignores multiple results + iae = (IntegrityAuditEntity) iaeList4.get(0); + // refresh the object from DB in case cached data was returned + em.refresh(iae); + logger.info("Resource: " + "pdp4" + " with PersistenceUnit: " + persistenceUnit + + " exists and entry be updated"); + }else{ + // If it does not exist, we also must add the resourceName, persistenceUnit and designated values + logger.info("Adding resource " + "pdp4" + " with PersistenceUnit: " + persistenceUnit + + " to IntegrityAuditEntity table"); + iae = new IntegrityAuditEntity(); + iae.setResourceName("pdp4"); + iae.setPersistenceUnit(persistenceUnit); + iae.setDesignated(false); + } + + //update/set properties in entry + iae.setSite(siteName); + iae.setNodeType(nodeType); + iae.setLastUpdated(date); + iae.setCreatedDate(date); + iae.setJdbcDriver(dbDriver); + iae.setJdbcPassword(dbPwd); + iae.setJdbcUrl("jdbc:h2:file:./sql/iaTest2"); + iae.setJdbcUser(dbUser); + + em.persist(iae); + // flush to the DB + em.flush(); + + // commit transaction + et.commit(); + + em.close(); + }catch (Exception e){ + String msg = "DbDAO: " + "register() " + "ecountered a problem in execution: "; + logger.error(msg + e); + throw new DbDaoTransactionException(e); + } + + /* + * Identical entries in from DB1 in DB2 except for dbDAO6 + */ + emf = Persistence.createEntityManagerFactory(persistenceUnit, properties2); + DbDAO dbDAO4 = new DbDAO(resourceName, persistenceUnit, properties2); + + DbDAO dbDAO5 = new DbDAO("pdp2", persistenceUnit, properties2); + + /* + * This is the mismatch entry + */ + DbDAO dbDAO6 = new DbDAO("pdp3", persistenceUnit, properties2); + try{ + EntityManager em = emf.createEntityManager(); + // Start a transaction + EntityTransaction et = em.getTransaction(); + + et.begin(); + + // if IntegrityAuditEntity entry exists for resourceName and PU, update it. If not found, create a new entry + Query iaequery = em.createQuery("Select i from IntegrityAuditEntity i where i.resourceName=:rn and i.persistenceUnit=:pu"); + iaequery.setParameter("rn", "pdp2"); + iaequery.setParameter("pu", persistenceUnit); + + @SuppressWarnings("rawtypes") + List iaeList = iaequery.getResultList(); + IntegrityAuditEntity iae = null; + + //If it already exists, we just want to update the properties and lastUpdated date + if(!iaeList.isEmpty()){ + //ignores multiple results + iae = (IntegrityAuditEntity) iaeList.get(0); + // refresh the object from DB in case cached data was returned + em.refresh(iae); + logger.info("Resource: " + "pdp2" + " with PersistenceUnit: " + persistenceUnit + + " exists and entry be updated"); + }else{ + // If it does not exist, we also must add teh resourceName, persistenceUnit and designated values + logger.info("Adding resource " + "pdp2" + " with PersistenceUnit: " + persistenceUnit + + " to IntegrityAuditEntity table"); + iae = new IntegrityAuditEntity(); + iae.setResourceName("pdp2"); + iae.setPersistenceUnit(persistenceUnit); + iae.setDesignated(false); + } + + //update/set properties in entry + iae.setSite(siteName); + iae.setNodeType(nodeType); + iae.setLastUpdated(date); + iae.setCreatedDate(date); + iae.setJdbcDriver(dbDriver); + iae.setJdbcPassword(dbPwd); + iae.setJdbcUrl("jdbc:h2:file:./sql/iaTest2"); + iae.setJdbcUser(dbUser); + + em.persist(iae); + // flush to the DB + em.flush(); + + // commit transaction + et.commit(); + + et.begin(); + + // if IntegrityAuditEntity entry exists for resourceName and PU, update it. If not found, create a new entry + iaequery = em.createQuery("Select i from IntegrityAuditEntity i where i.resourceName=:rn and i.persistenceUnit=:pu"); + iaequery.setParameter("rn", "pdp1"); + iaequery.setParameter("pu", persistenceUnit); + + @SuppressWarnings("rawtypes") + List iaeList2 = iaequery.getResultList(); + iae = null; + + //If it already exists, we just want to update the properties and lastUpdated date + if(!iaeList2.isEmpty()){ + //ignores multiple results + iae = (IntegrityAuditEntity) iaeList2.get(0); + // refresh the object from DB in case cached data was returned + em.refresh(iae); + logger.info("Resource: " + "pdp1" + " with PersistenceUnit: " + persistenceUnit + + " exists and entry be updated"); + }else{ + // If it does not exist, we also must add teh resourceName, persistenceUnit and designated values + logger.info("Adding resource " + "pdp1" + " with PersistenceUnit: " + persistenceUnit + + " to IntegrityAuditEntity table"); + iae = new IntegrityAuditEntity(); + iae.setResourceName("pdp1"); + iae.setPersistenceUnit(persistenceUnit); + iae.setDesignated(false); + } + + //update/set properties in entry + iae.setSite(siteName); + iae.setNodeType(nodeType); + iae.setLastUpdated(date); + iae.setCreatedDate(date); + iae.setJdbcDriver(dbDriver); + iae.setJdbcPassword(dbPwd); + iae.setJdbcUrl("jdbc:h2:file:./sql/iaTest2"); + iae.setJdbcUser(dbUser); + + em.persist(iae); + // flush to the DB + em.flush(); + + // commit transaction + et.commit(); + + et.begin(); + + // if IntegrityAuditEntity entry exists for resourceName and PU, update it. If not found, create a new entry + iaequery = em.createQuery("Select i from IntegrityAuditEntity i where i.resourceName=:rn and i.persistenceUnit=:pu"); + iaequery.setParameter("rn", "pdp3"); + iaequery.setParameter("pu", persistenceUnit); + + @SuppressWarnings("rawtypes") + List iaeList3 = iaequery.getResultList(); + iae = null; + + //If it already exists, we just want to update the properties and lastUpdated date + if(!iaeList3.isEmpty()){ + //ignores multiple results + iae = (IntegrityAuditEntity) iaeList3.get(0); + // refresh the object from DB in case cached data was returned + em.refresh(iae); + logger.info("Resource: " + "pdp3" + " with PersistenceUnit: " + persistenceUnit + + " exists and entry be updated"); + }else{ + // If it does not exist, we also must add teh resourceName, persistenceUnit and designated values + logger.info("Adding resource " + "pdp3" + " with PersistenceUnit: " + persistenceUnit + + " to IntegrityAuditEntity table"); + iae = new IntegrityAuditEntity(); + iae.setResourceName("pdp3"); + iae.setPersistenceUnit(persistenceUnit); + iae.setDesignated(false); + } + + //update/set properties in entry + iae.setSite(siteName); + iae.setNodeType(nodeType); + iae.setLastUpdated(date); + iae.setCreatedDate(date); + iae.setJdbcDriver(dbDriver); + iae.setJdbcPassword(dbPwd); + iae.setJdbcUrl("jdbc:h2:file:./sql/iaTest2"); + iae.setJdbcUser(dbUser); + + em.persist(iae); + // flush to the DB + em.flush(); + + // commit transaction + et.commit(); + + em.close(); + }catch (Exception e){ + String msg = "DbDAO: " + "register() " + "ecountered a problem in execution: "; + logger.error(msg + e); + throw new DbDaoTransactionException(e); + + } + + /* + * Run the DB Audit, once it finds a mismatch and sleeps, update DB1 + * to have the same entry as DB2 it can be confirmed that the mismatch + * is resolved + */ + DbAudit dbAudit = new DbAudit(dbDAO); + dbAudit.dbAudit(resourceName, persistenceUnit, nodeType); + emf = Persistence.createEntityManagerFactory(persistenceUnit, properties); + try{ + EntityManager em = emf.createEntityManager(); + // Start a transaction + EntityTransaction et = em.getTransaction(); + + et.begin(); + + // if IntegrityAuditEntity entry exists for resourceName and PU, update it. If not found, create a new entry + Query iaequery = em.createQuery("Select i from IntegrityAuditEntity i where i.resourceName=:rn and i.persistenceUnit=:pu"); + iaequery.setParameter("rn", "pdp3"); + iaequery.setParameter("pu", persistenceUnit); + + @SuppressWarnings("rawtypes") + List iaeList = iaequery.getResultList(); + IntegrityAuditEntity iae = null; + + //If it already exists, we just want to update the properties and lastUpdated date + if(!iaeList.isEmpty()){ + //ignores multiple results + iae = (IntegrityAuditEntity) iaeList.get(0); + // refresh the object from DB in case cached data was returned + em.refresh(iae); + logger.info("Resource: " + "pdp3" + " with PersistenceUnit: " + persistenceUnit + + " exists and entry be updated"); + }else{ + // If it does not exist, we also must add the resourceName, persistenceUnit and designated values + logger.info("Adding resource " + "pdp3" + " with PersistenceUnit: " + persistenceUnit + + " to IntegrityAuditEntity table"); + iae = new IntegrityAuditEntity(); + iae.setResourceName("pdp3"); + iae.setPersistenceUnit(persistenceUnit); + iae.setDesignated(false); + } + + //update/set properties in entry + iae.setSite(siteName); + iae.setNodeType(nodeType); + iae.setLastUpdated(date); + iae.setCreatedDate(date); + iae.setJdbcDriver(dbDriver); + iae.setJdbcPassword(dbPwd); + iae.setJdbcUrl("jdbc:h2:file:./sql/iaTest2"); + iae.setJdbcUser(dbUser); + + em.persist(iae); + // flush to the DB + em.flush(); + + // commit transaction + et.commit(); + em.close(); + }catch (Exception e){ + String msg = "DbDAO: " + "register() " + "ecountered a problem in execution: "; + logger.error(msg + e); + throw new DbDaoTransactionException(e); + } + + /* + * Run the audit again and correct the mismatch, the result should be one + * entry in the mismatchKeySet because of the misentry from the beginning + * of the test + */ + dbAudit.dbAudit(resourceName, persistenceUnit, nodeType); + + //Cleanup DB2 + cleanDb(persistenceUnit, properties2); + + FileInputStream fstream = new FileInputStream(TEST_LOG); + BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); + String strLine; + int startIndex; + String mismatchIndex = ""; + while ((strLine = br.readLine()) != null) { + //parse strLine to obtain what you want...retrieve the last entry + + if (strLine.contains("Mismatched entries (keys):")) { + startIndex = strLine.indexOf("(keys):") + 8; + mismatchIndex = strLine.substring(startIndex); + } + } + int mismatchEntries = mismatchIndex.trim().split(",").length; + logger.info("mismatchTest: mismatchIndex found: '" + mismatchIndex + "'" + + " mismatachEntries = " + mismatchEntries); + + // Assert there is only one entry index + assertEquals(1, mismatchEntries); + + br.close(); + + //Now check the entry in the error.log + fstream = new FileInputStream(ERROR_LOG); + br = new BufferedReader(new InputStreamReader(fstream)); + String mismatchNum = ""; + while ((strLine = br.readLine()) != null) { + //parse strLine to obtain what you want...retrieve the last entry + + if (strLine.contains("DB Audit:")) { + startIndex = strLine.indexOf("DB Audit:") + 10; + mismatchNum = strLine.substring(startIndex, startIndex+1); + } + } + logger.info("mismatchTest: mismatchNum found: '" + mismatchNum + "'"); + + // Assert that there are a total of 3 mismatches - 1 between each comparison node. + assertEquals("3", mismatchNum); + + br.close(); + + logger.info("mismatchTest: Exit"); + } + +} diff --git a/integrity-audit/src/test/java/org/onap/policy/common/ia/test/DbDAOTest.java b/integrity-audit/src/test/java/org/onap/policy/common/ia/test/DbDAOTest.java new file mode 100644 index 00000000..983e8911 --- /dev/null +++ b/integrity-audit/src/test/java/org/onap/policy/common/ia/test/DbDAOTest.java @@ -0,0 +1,735 @@ +/*- + * ============LICENSE_START======================================================= + * Integrity Audit + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.common.ia.test; + +import static org.junit.Assert.*; + +/* + * All JUnits are designed to run in the local development environment + * where they have write privileges and can execute time-sensitive + * tasks. + */ +import java.util.Date; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Properties; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.EntityTransaction; +import javax.persistence.Persistence; +import javax.persistence.PersistenceUnitUtil; +import javax.persistence.Query; +import javax.persistence.TypedQuery; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Root; + +import org.junit.After; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; + +import org.onap.policy.common.ia.DbDAO; +import org.onap.policy.common.ia.DbDaoTransactionException; +import org.onap.policy.common.ia.IntegrityAuditProperties; +import org.onap.policy.common.ia.jpa.IntegrityAuditEntity; + +/* + * All JUnits are designed to run in the local development environment + * where they have write privileges and can execute time-sensitive + * tasks. + */ +public class DbDAOTest { + private static String persistenceUnit; + private static Properties properties; + private static String resourceName; + + DbDAO d; + + @Before + public void setUp() throws Exception { + properties = new Properties(); + properties.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER); + properties.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL); + properties.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER); + properties.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD); + properties.put(IntegrityAuditProperties.SITE_NAME, "SiteA"); + properties.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml"); + + persistenceUnit = "integrityAuditPU"; + resourceName = "pdp0"; + } + + @After + public void tearDown() throws Exception { + } + + /* Tests registering a new IntegrityAuditEntity object in the DB */ + @Ignore + @Test + public void testNewRegistration() { + try { + EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit, properties); + EntityManager em = emf.createEntityManager(); + + // Start a transaction + EntityTransaction et = em.getTransaction(); + + // Begin Transaction + et.begin(); + + // Clean the DB + em.createQuery("DELETE FROM IntegrityAuditEntity").executeUpdate(); + + // flush to the DB + em.flush(); + et.commit(); + + et.begin(); + d = new DbDAO(resourceName, persistenceUnit, properties); + + // Find the proper entry in the database + Query iaequery = em.createQuery("Select i from IntegrityAuditEntity i where i.resourceName=:rn and i.persistenceUnit=:pu"); + iaequery.setParameter("rn", DbDAOTest.resourceName); + iaequery.setParameter("pu", DbDAOTest.persistenceUnit); + + @SuppressWarnings("rawtypes") + List iaeList = iaequery.getResultList(); + + // Assert that the IntegrityAuditEntity object was found + assertNotNull(iaeList); + + // flush to the DB + em.flush(); + et.commit(); + em.close(); + + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + /* Tests updating an IntegrityAuditEntity if it has already been registered */ + @Ignore + @Test + public void testUpdateRegistration() { + EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit, properties); + EntityManager em = emf.createEntityManager(); + + // Start a transaction + EntityTransaction et = em.getTransaction(); + + // Begin transaction + et.begin(); + + // Clean the DB + em.createQuery("DELETE FROM IntegrityAuditEntity").executeUpdate(); + + // flush to the DB + em.flush(); + et.commit(); + + // close the EntityManager + em.close(); + + try { + d = new DbDAO(resourceName, persistenceUnit, properties); + + // Change site_name in properties to test that an update was made to an existing entry in the table + properties.put(IntegrityAuditProperties.SITE_NAME, "SiteB"); + d = new DbDAO(resourceName, persistenceUnit, properties); + + em = emf.createEntityManager(); + + // Start a transaction + et = em.getTransaction(); + + // Begin Transaction + et.begin(); + + // Find the proper entry in the database + Query iaequery = em.createQuery("Select i from IntegrityAuditEntity i where i.resourceName=:rn and i.persistenceUnit=:pu"); + iaequery.setParameter("rn", DbDAOTest.resourceName); + iaequery.setParameter("pu", DbDAOTest.persistenceUnit); + + @SuppressWarnings("rawtypes") + List iaeList = iaequery.getResultList(); + IntegrityAuditEntity iae = null; + if(!iaeList.isEmpty()) { + //ignores multiple results + iae = (IntegrityAuditEntity) iaeList.get(0); + + em.refresh(iae); + em.persist(iae); + + // flush to the DB + em.flush(); + + // commit transaction + et.commit(); + + // close the EntityManager + em.close(); + + // Assert that the site_name for the existing entry was updated + assertEquals("SiteB", iae.getSite()); + } + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + /* Tests obtaining all Integrity Audit Entities from a table */ + @Ignore + @Test + public void testGetIntegrityAuditEntities() { + EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit, properties); + EntityManager em = emf.createEntityManager(); + + // Start a transaction + EntityTransaction et = em.getTransaction(); + + et.begin(); + + // Clean the DB + em.createQuery("DELETE FROM IntegrityAuditEntity").executeUpdate(); + + // flush to the DB + em.flush(); + + // close the transaction + et.commit(); + + // close the EntityManager + em.close(); + + try { + // Add some entries to the DB + d = new DbDAO(resourceName, persistenceUnit, properties); + new DbDAO("pdp1", persistenceUnit, properties); + properties.put(IntegrityAuditProperties.NODE_TYPE, "pdp_drools"); + new DbDAO("pdp2", persistenceUnit, properties); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + List entities; + try { + // Obtain entries based on persistenceUnit and nodeType + entities = d.getIntegrityAuditEntities(persistenceUnit, "pdp_xacml"); + assertEquals(2, entities.size()); + } catch (DbDaoTransactionException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + /* Tests retrieving a DbDAO instance's IntegrityAuditEntity */ + @Ignore + @Test + public void testGetMyIntegrityAuditEntity() { + try { + d = new DbDAO(resourceName, persistenceUnit, properties); + IntegrityAuditEntity iae = d.getMyIntegrityAuditEntity(); + assertEquals("integrityAuditPU", iae.getPersistenceUnit()); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + /* Tests obtaining an IntegrityAuditEntity by ID */ + @Ignore + @Test + public void testGetIntegrityAuditEntity() { + EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit, properties); + EntityManager em = emf.createEntityManager(); + + // Start a transaction + EntityTransaction et = em.getTransaction(); + + // Begin transaction + et.begin(); + + // Clean the DB + em.createQuery("DELETE FROM IntegrityAuditEntity").executeUpdate(); + + // flush to the DB + em.flush(); + + // close the transaction + et.commit(); + + try { + // Obtain an entry from the database based on ID + d = new DbDAO(resourceName, persistenceUnit, properties); + + et.begin(); + + // Find the proper database entry + Query iaequery = em.createQuery("Select i from IntegrityAuditEntity i where i.resourceName=:rn and i.persistenceUnit=:pu"); + iaequery.setParameter("rn", DbDAOTest.resourceName); + iaequery.setParameter("pu", DbDAOTest.persistenceUnit); + + @SuppressWarnings("rawtypes") + List iaeList = iaequery.getResultList(); + IntegrityAuditEntity iae = null; + if(!iaeList.isEmpty()){ + //ignores multiple results + iae = (IntegrityAuditEntity) iaeList.get(0); + + // refresh the object from DB in case cached data was returned + em.refresh(iae); + + // Obtain ID for an IntegrityAuditEntity + PersistenceUnitUtil util = emf.getPersistenceUnitUtil(); + Object iaeId = util.getIdentifier(iae); + + // Obtain the same IntegrityAuditEntity based on ID + IntegrityAuditEntity iaeDuplicate = d.getIntegrityAuditEntity((long) iaeId); + Object duplicateId = util.getIdentifier(iaeDuplicate); + + // Assert that the proper entry was retrieved based on ID + assertEquals((long) iaeId, (long) duplicateId); + } + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + // close the EntityManager + em.close(); + } + + /* Tests setting an IntegrityAuditEntity as the designated node */ + @Ignore + @Test + public void testSetDesignated() { + try { + EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit, properties); + EntityManager em = emf.createEntityManager(); + + // Start a transaction + EntityTransaction et = em.getTransaction(); + + // Begin transaction + et.begin(); + + // Clean the DB + em.createQuery("DELETE FROM IntegrityAuditEntity").executeUpdate(); + + // flush to the DB + em.flush(); + et.commit(); + + et.begin(); + + // Create an entry and set it's designated field to true + d = new DbDAO(resourceName, persistenceUnit, properties); + d.setDesignated(resourceName, persistenceUnit, true); + + // Find the proper entry in the database + Query iaequery = em.createQuery("Select i from IntegrityAuditEntity i where i.resourceName=:rn and i.persistenceUnit=:pu"); + iaequery.setParameter("rn", resourceName); + iaequery.setParameter("pu", persistenceUnit); + + @SuppressWarnings("rawtypes") + List iaeList = iaequery.getResultList(); + IntegrityAuditEntity iae = null; + + if(!iaeList.isEmpty()){ + //ignores multiple results + iae = (IntegrityAuditEntity) iaeList.get(0); + em.refresh(iae); + + // Check if the node is designated + boolean result = iae.isDesignated(); + + // Assert that it is designated + assertTrue(result); + } + + // flush to the DB + em.flush(); + + // close the transaction + et.commit(); + + // close the EntityManager + em.close(); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + /* Tests that the lastUpdated column in the database is updated properly */ + @Ignore + @Test + public void testSetLastUpdated() { + try { + EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit, properties); + EntityManager em = emf.createEntityManager(); + + // Start a transaction + EntityTransaction et = em.getTransaction(); + + // Begin transaction + et.begin(); + + // Clean the DB + em.createQuery("DELETE FROM IntegrityAuditEntity").executeUpdate(); + + // flush to the DB + em.flush(); + et.commit(); + + et.begin(); + + // Create an entry + d = new DbDAO(resourceName, persistenceUnit, properties); + + // Find the proper entry in the database + Query iaequery = em.createQuery("Select i from IntegrityAuditEntity i where i.resourceName=:rn and i.persistenceUnit=:pu"); + iaequery.setParameter("rn", resourceName); + iaequery.setParameter("pu", persistenceUnit); + + @SuppressWarnings("rawtypes") + List iaeList = iaequery.getResultList(); + IntegrityAuditEntity iae = null; + + if(!iaeList.isEmpty()){ + // ignores multiple results + iae = (IntegrityAuditEntity) iaeList.get(0); + // refresh the object from DB in case cached data was returned + em.refresh(iae); + + // Obtain old update value and set new update value + Date oldDate = iae.getLastUpdated(); + iae.setSite("SiteB"); + iae.setLastUpdated(new Date()); + Date newDate = iae.getLastUpdated(); + + em.persist(iae); + // flush to the DB + em.flush(); + // close the transaction + et.commit(); + // close the EntityManager + em.close(); + + // Assert that the old and new update times are different + assertNotEquals(oldDate, newDate); + } + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + /* Tests that all the entries from a class can be retrieved */ + @Ignore + @Test + public void testGetAllMyEntriesString() { + EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit, properties); + EntityManager em = emf.createEntityManager(); + + // Start a transaction + EntityTransaction et = em.getTransaction(); + + // Begin transaction + et.begin(); + + // Clean the DB + em.createQuery("DELETE FROM IntegrityAuditEntity").executeUpdate(); + + // flush to the DB + em.flush(); + et.commit(); + + // close the EntityManager + em.close(); + + try { + // create entries for the IntegrityAuditEntity table + d = new DbDAO(resourceName, persistenceUnit, properties); + new DbDAO("pdp1", persistenceUnit, properties); + new DbDAO("pdp2", persistenceUnit, properties); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + try { + // Obtain a hash with the persisted objects + HashMap entries = d.getAllMyEntries("org.onap.policy.common.ia.jpa.IntegrityAuditEntity"); + + // Assert there were 3 entries for that class + assertEquals(3, entries.size()); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + /* Tests retrieving all entities in a Persistence Unit using the class name and a hashset of IDs */ + @Ignore + @Test + public void testGetAllMyEntriesStringHashSet() { + EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit, properties); + EntityManager em = emf.createEntityManager(); + + // Start a transaction + EntityTransaction et = em.getTransaction(); + + // Begin transaction + et.begin(); + + // Clean the DB + em.createQuery("DELETE FROM IntegrityAuditEntity").executeUpdate(); + + // flush to the DB + em.flush(); + et.commit(); + + try { + // create entries for the IntegrityAuditEntity table + d = new DbDAO(resourceName, persistenceUnit, properties); + new DbDAO("pdp1", persistenceUnit, properties); + new DbDAO("pdp2", persistenceUnit, properties); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + try { + // Obtain all entity keys + CriteriaBuilder cb = em.getCriteriaBuilder(); + CriteriaQuery cq = cb.createQuery(); + Root rootEntry = cq.from(Class.forName("org.onap.policy.common.ia.jpa.IntegrityAuditEntity")); + CriteriaQuery all = cq.select(rootEntry); + TypedQuery allQuery = em.createQuery(all); + List objectList = allQuery.getResultList(); + HashSet resultSet = new HashSet(); + PersistenceUnitUtil util = emf.getPersistenceUnitUtil(); + for (Object o: objectList){ + Object key = util.getIdentifier(o); + resultSet.add(key); + } + + // Obtain a hash with the persisted objects + HashMap entries = d.getAllMyEntries("org.onap.policy.common.ia.jpa.IntegrityAuditEntity", resultSet); + + // Assert there were 3 entries for that class + assertEquals(3, entries.size()); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + // close the EntityManager + em.close(); + } + + /* Tests retrieving all entities in a Persistence Unit using the persistence unit, properties, and class name */ + @Ignore + @Test + public void testGetAllEntriesStringPropertiesString() { + EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit, properties); + EntityManager em = emf.createEntityManager(); + + // Start a transaction + EntityTransaction et = em.getTransaction(); + + // Begin transaction + et.begin(); + + // Clean the DB + em.createQuery("DELETE FROM IntegrityAuditEntity").executeUpdate(); + + // flush to the DB + em.flush(); + et.commit(); + + // close the EntityManager + em.close(); + + try { + // create entries for the IntegrityAuditEntity table + d = new DbDAO(resourceName, persistenceUnit, properties); + new DbDAO("pdp1", persistenceUnit, properties); + new DbDAO("pdp2", persistenceUnit, properties); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + try { + // Obtain a hash with the persisted objects + HashMap entries = d.getAllEntries("integrityAuditPU", properties, "org.onap.policy.common.ia.jpa.IntegrityAuditEntity"); + + // Assert there were 3 entries for that class + assertEquals(3, entries.size()); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + /* Tests retrieving all entities in a Persistence Unit using the persistence unit, properties, class name, and a hashset of IDs */ + @Ignore + @Test + public void testGetAllEntriesStringPropertiesStringHashSet() { + EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit, properties); + EntityManager em = emf.createEntityManager(); + + // Start a transaction + EntityTransaction et = em.getTransaction(); + + // Begin transaction + et.begin(); + + // Clean the DB + em.createQuery("DELETE FROM IntegrityAuditEntity").executeUpdate(); + + // flush to the DB + em.flush(); + et.commit(); + + try { + // create entries for the IntegrityAuditEntity table + d = new DbDAO(resourceName, persistenceUnit, properties); + new DbDAO("pdp1", persistenceUnit, properties); + new DbDAO("pdp2", persistenceUnit, properties); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + try { + // Obtain all entity keys + CriteriaBuilder cb = em.getCriteriaBuilder(); + CriteriaQuery cq = cb.createQuery(); + Root rootEntry = cq.from(Class.forName("org.onap.policy.common.ia.jpa.IntegrityAuditEntity")); + CriteriaQuery all = cq.select(rootEntry); + TypedQuery allQuery = em.createQuery(all); + List objectList = allQuery.getResultList(); + HashSet resultSet = new HashSet(); + PersistenceUnitUtil util = emf.getPersistenceUnitUtil(); + for (Object o: objectList){ + Object key = util.getIdentifier(o); + resultSet.add(key); + } + + // Obtain a hash with the persisted objects + HashMap entries = d.getAllEntries("integrityAuditPU", properties, "org.onap.policy.common.ia.jpa.IntegrityAuditEntity", resultSet); + + // Assert there were 3 entries for that class + assertEquals(3, entries.size()); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + // close the EntityManager + em.close(); + } + + /* Tests getting all the entries from a class based on persistenceUnit, properties, and className */ + @Ignore + @Test + public void testGetAllEntries() { + EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit, properties); + EntityManager em = emf.createEntityManager(); + + // Start a transaction + EntityTransaction et = em.getTransaction(); + + // Begin transaction + et.begin(); + + // Clean the DB + em.createQuery("DELETE FROM IntegrityAuditEntity").executeUpdate(); + + // flush to the DB + em.flush(); + et.commit(); + + // close the EntityManager + em.close(); + + try { + // create entries for the IntegrityAuditEntity table + d = new DbDAO(resourceName, persistenceUnit, properties); + new DbDAO("pdp1", persistenceUnit, properties); + new DbDAO("pdp2", persistenceUnit, properties); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + try { + // Obtain a hash with the persisted objects + HashMap entries = d.getAllEntries(persistenceUnit, properties, "org.onap.policy.common.ia.jpa.IntegrityAuditEntity"); + + // Assert there were 3 entries for that class + assertEquals(3, entries.size()); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + /* Tests obtaining all class names of persisted classes */ + public void testGetPersistenceClassNames() { + EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit, properties); + EntityManager em = emf.createEntityManager(); + + // Start a transaction + EntityTransaction et = em.getTransaction(); + + // Begin transaction + et.begin(); + + // Clean the DB + em.createQuery("DELETE FROM IntegrityAuditEntity").executeUpdate(); + + // flush to the DB + em.flush(); + et.commit(); + + // close the EntityManager + em.close(); + + try { + d = new DbDAO(resourceName, persistenceUnit, properties); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + // Retrieve persistence class names + HashSet result = d.getPersistenceClassNames(); + assertEquals(1, result.size()); + } +} diff --git a/integrity-audit/src/test/java/org/onap/policy/common/ia/test/IntegrityAuditDesignationTest.java b/integrity-audit/src/test/java/org/onap/policy/common/ia/test/IntegrityAuditDesignationTest.java new file mode 100644 index 00000000..f74b354b --- /dev/null +++ b/integrity-audit/src/test/java/org/onap/policy/common/ia/test/IntegrityAuditDesignationTest.java @@ -0,0 +1,1109 @@ +/*- + * ============LICENSE_START======================================================= + * Integrity Audit + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.common.ia.test; + +import static org.junit.Assert.*; + +import java.io.BufferedReader; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Properties; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.EntityTransaction; +import javax.persistence.Persistence; + + + +//import org.apache.commons.logging.Log; +//import org.apache.commons.logging.LogFactory; +import org.junit.After; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; + +import org.onap.policy.common.ia.AuditThread; +import org.onap.policy.common.ia.DbDAO; +import org.onap.policy.common.ia.IntegrityAudit; +import org.onap.policy.common.ia.IntegrityAuditProperties; +import org.onap.policy.common.logging.flexlogger.FlexLogger; +import org.onap.policy.common.logging.flexlogger.Logger; + +/* + * All JUnits are designed to run in the local development environment + * where they have write privileges and can execute time-sensitive + * tasks. + */ +public class IntegrityAuditDesignationTest { + + private static Logger logger = FlexLogger.getLogger(IntegrityAuditDesignationTest.class); + + /* + * Provides a little cushion for timing events. + */ + private static int FUDGE_FACTOR = 15000; + + private static String persistenceUnit; + private static Properties properties; + private static String resourceName; + private static final String TEST_LOG = "./testingLogs/common-modules/integrity-audit/debug.log"; + @Before + public void setUp() throws Exception { + + + System.out.println("setUp: Clearing debug.log"); + FileOutputStream fstream = new FileOutputStream(TEST_LOG); + fstream.close(); + + logger.info("setUp: Entering"); + + IntegrityAudit.isUnitTesting = true; + + properties = new Properties(); + properties.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER); + properties.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL); + properties.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER); + properties.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD); + properties.put(IntegrityAuditProperties.SITE_NAME, "SiteA"); + properties.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml"); + + /* + * AuditThread.AUDIT_THREAD_SLEEP_INTERVAL is also five seconds, so + * setting AUDIT_PERIOD_SECONDS to 5 ensures that whether or not audit + * has already been run on a node, it will sleep the same amount of + * time. + */ + properties.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5"); + + persistenceUnit = "testPU"; + resourceName = "pdp1"; + + + //Clean up the DB + EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit, properties); + + EntityManager em = emf.createEntityManager(); + // Start a transaction + EntityTransaction et = em.getTransaction(); + + et.begin(); + + // if IntegrityAuditEntity entry exists for resourceName and PU, update it. If not found, create a new entry + em.createQuery("Delete from IntegrityAuditEntity").executeUpdate(); + + // commit transaction + et.commit(); + em.close(); + logger.info("setUp: Exiting"); + + } + + + @After + public void tearDown() throws Exception { + + logger.info("tearDown: Entering"); + + logger.info("tearDown: Exiting"); + + } + + /* + * Tests designation logic when only one functioning resource is in play. Designation + * should stay with single resource. + * + * Note: console.log must be examined to ascertain whether or not this test was successful. + */ + @Ignore + @Test + public void testOneResource() throws Exception { + + logger.info("testOneResource: Entering"); + + IntegrityAudit integrityAudit = new IntegrityAudit(resourceName, persistenceUnit, properties); + integrityAudit.startAuditThread(); + + /* + * Sleep long enough to allow + * + * 1) pdp1 to run audit (15 seconds) + * + * 2) Logic to detect that no other node is available for designation (60 seconds) + * + * 3) pdp1 to run audit again (15 seconds) + */ + logger.info("testOneResource: Sleeping 100 seconds"); + Thread.sleep(100000); + + logger.info("testOneResource: Stopping audit thread"); + integrityAudit.stopAuditThread(); + + FileInputStream fstream = new FileInputStream(TEST_LOG); + BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); + String strLine; + int startIndex; + int endIndex; + + String rName = ""; + while ((strLine = br.readLine()) != null) { + // parse strLine to obtain what you want + if (strLine.contains("Starting audit simulation for resourceName=")) { + startIndex = strLine.indexOf("resourceName=") + 13; + endIndex = strLine.indexOf(","); + rName = strLine.substring(startIndex, endIndex); + logger.info("testOneResource: rName: " + rName); + assertEquals("pdp1", rName); + } + } + fstream.close(); + + /* + * Test fix for ONAPD2TD-783: Audit fails to run when application is restarted. + */ + integrityAudit.startAuditThread(); + + /* + * Sleep long enough to allow + * + * 1) pdp1 to run audit (15 seconds) + * + * 2) Logic to detect that no other node is available for designation (60 seconds) + * + * 3) pdp1 to run audit again (15 seconds) + */ + logger.info("testOneResource: Sleeping 100 seconds for second time"); + Thread.sleep(100000); + + logger.info("testOneResource: Stopping audit thread for second time"); + integrityAudit.stopAuditThread(); + + fstream = new FileInputStream(TEST_LOG); + br = new BufferedReader(new InputStreamReader(fstream)); + + rName = ""; + while ((strLine = br.readLine()) != null) { + // parse strLine to obtain what you want + if (strLine.contains("Starting audit simulation for resourceName=")) { + startIndex = strLine.indexOf("resourceName=") + 13; + endIndex = strLine.indexOf(","); + rName = strLine.substring(startIndex, endIndex); + logger.info("testOneResource: rName: " + rName); + assertEquals("pdp1", rName); + } + } + fstream.close(); + + logger.info("testOneResource: Exiting"); + + } + + /* + * Tests designation logic when two functioning resources are in play. + * Designation should alternate between resources. + * + * Note: console.log must be examined to ascertain whether or not this test + * was successful. A quick way of examining the log is to search for the + * string "audit simulation": + * + * As you can see from the "dbAuditSimulate" method, when it executes, it + * logs the "Starting audit simulation..." message and when it finishes, it + * logs the "Finished audit simulation..." message. By looking for these + * messages, you can verify that the audits are run by the proper resource. + * For example, when testFourResourcesOneDead is run, you should see a + * Starting.../Finished... sequence for pdp1, followed by a + * Starting.../Finished... sequence for pdp2, followed by a + * Starting.../Finished... sequence for pdp4 (pdp3 is skipped as it's + * dead/hung), followed by a Starting.../Finished... sequence for pdp1, etc. + */ + @Ignore + @Test + public void testTwoResources() throws Exception { + + logger.info("testTwoResources: Entering"); + + /* + * Start audit for pdp1. + */ + IntegrityAudit integrityAudit = new IntegrityAudit(resourceName, persistenceUnit, properties); + integrityAudit.startAuditThread(); + + /* + * Start audit for pdp2. + */ + Properties properties2 = new Properties(); + properties2.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER); + properties2.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL); + properties2.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER); + properties2.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD); + properties2.put(IntegrityAuditProperties.SITE_NAME, "SiteA"); + properties2.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml"); + properties2.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5"); + String persistenceUnit2 = "testPU"; + String resourceName2 = "pdp2"; + IntegrityAudit integrityAudit2 = new IntegrityAudit(resourceName2, persistenceUnit2, properties2); + integrityAudit2.startAuditThread(); + + /* + * Sleep long enough to allow + * + * 1) pdp1 to run audit (15 seconds) + * + * 2) Logic to detect that pdp1 is stale and designate pdp2 (30 seconds) + * + * 3) pdp2 to run audit (15 seconds) + * + * 4) Logic to detect that pdp2 is stale and designate pdp1 (30 seconds) + * + * 5) pdp1 to run audit (15 seconds) + */ + Thread.sleep(120000); + + logger.info("testTwoResources: Stopping audit threads"); + integrityAudit.stopAuditThread(); + integrityAudit2.stopAuditThread(); + + FileInputStream fstream = new FileInputStream(TEST_LOG); + BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); + String strLine; + int startIndex; + int endIndex; + ArrayList expectedResult = new ArrayList(Arrays.asList("pdp1", "pdp2", "pdp1")); + ArrayList delegates = new ArrayList(); + while ((strLine = br.readLine()) != null) { + /* parse strLine to obtain what you want */ + if (strLine.contains("Starting audit simulation for resourceName=")) { + startIndex = strLine.indexOf("resourceName=") + 13; + endIndex = strLine.indexOf(","); + + String rName = strLine.substring(startIndex, endIndex); + + delegates.add(rName); + } + } + + for (String delegate: delegates) { + logger.info("testTwoResources: delegate: " + delegate); + } + + fstream.close(); + + assertTrue(expectedResult.equals(delegates)); + + assertTrue("delegate count only " + delegates.size(), delegates.size() >= 3); + assertTrue("delegate 0 is " + expectedResult.get(0), expectedResult.get(0).equals(delegates.get(0))); + assertTrue("delegate 1 is " + expectedResult.get(1), expectedResult.get(1).equals(delegates.get(1))); + assertTrue("delegate 2 is " + expectedResult.get(2), expectedResult.get(2).equals(delegates.get(2))); + + logger.info("testTwoResources: Exiting"); + + } + + /* + * Tests designation logic when two functioning resources are in play, each + * with different PUs. Audits for "testPU" and "integrityAuditPU" should run + * simultaneously. Designation should not alternate. + * + * Note: console.log must be examined to ascertain whether or not this test + * was successful. + */ + @Ignore + @Test + public void testTwoResourcesDifferentPus() throws Exception { + + logger.info("testTwoResourcesDifferentPus: Entering"); + + /* + * Start audit for pdp1. + */ + IntegrityAudit integrityAudit = new IntegrityAudit(resourceName, persistenceUnit, properties); + integrityAudit.startAuditThread(); + + /* + * Start audit for pdp2. + */ + Properties properties2 = new Properties(); + properties2.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER); + properties2.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL); + properties2.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER); + properties2.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD); + properties2.put(IntegrityAuditProperties.SITE_NAME, "SiteA"); + properties2.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml"); + properties2.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5"); + String persistenceUnit2 = "integrityAuditPU"; + String resourceName2 = "pdp2"; + IntegrityAudit integrityAudit2 = new IntegrityAudit(resourceName2, persistenceUnit2, properties2); + integrityAudit2.startAuditThread(); + + /* + * Sleep long enough to allow + * + * 1) pdp1 and pdp2 to run audit simultaneously (15 seconds) + * + * 2) Logic to detect that no other node is available for designation for either pdp1 or pdp2 (60 seconds) + * + * 3) pdp1 and pdp2 to again run audit simultaneously (15 seconds) + * + * NOTE: Based on the above, you would think a 100000ms sleep would be appropriate, + * but for some reason, when all tests are run this test errors. + */ + logger.info("testTwoResourcesDifferentPus: Sleeping 80 seconds"); + Thread.sleep(100000); + + logger.info("testTwoResourcesDifferentPus: Stopping audit threads"); + integrityAudit.stopAuditThread(); + integrityAudit2.stopAuditThread(); + + FileInputStream fstream = new FileInputStream(TEST_LOG); + BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); + String strLine; + int startIndex; + int endIndex; + ArrayList expectedResult = new ArrayList(Arrays.asList("pdp1", "pdp2", "pdp1", "pdp2")); + ArrayList delegates = new ArrayList(); + while ((strLine = br.readLine()) != null) { + /* parse strLine to obtain what you want */ + if (strLine.contains("Starting audit simulation for resourceName=")) { + startIndex = strLine.indexOf("resourceName=") + 13; + endIndex = strLine.indexOf(","); + + String rName = strLine.substring(startIndex, endIndex); + + delegates.add(rName); + } + } + + for (String delegate: delegates) { + logger.info("testTwoResourcesDifferentPus: delegate: " + delegate); + } + + fstream.close(); + + assertTrue("delegate count only " + delegates.size(), delegates.size() >= 4); + assertTrue("delegate 0 is " + expectedResult.get(0), expectedResult.get(0).equals(delegates.get(0))); + assertTrue("delegate 1 is " + expectedResult.get(1), expectedResult.get(1).equals(delegates.get(1))); + assertTrue("delegate 2 is " + expectedResult.get(2), expectedResult.get(2).equals(delegates.get(2))); + assertTrue("delegate 3 is " + expectedResult.get(3), expectedResult.get(3).equals(delegates.get(3))); + + assertTrue(expectedResult.equals(delegates)); + + logger.info("testTwoResourcesDifferentPus: Exiting"); + + } + + + /* + * Tests designation logic when two resources are in play but one of them is + * dead/hung. Designation should move to second resource but then get + * restored back to original resource when it's discovered that second + * resource is dead. + * + * Note: console.log must be examined to ascertain whether or not this test + * was successful. + */ + @Ignore + @Test + public void testTwoResourcesOneDead() throws Exception { + + logger.info("testTwoResourcesOneDead: Entering"); + + /* + * Start audit for pdp1. + */ + IntegrityAudit integrityAudit = new IntegrityAudit(resourceName, persistenceUnit, properties); + integrityAudit.startAuditThread(); + + /* + * Populate DB for pdp2, which will simulate it having registered but then having died. + */ + Properties properties2 = new Properties(); + properties2.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER); + properties2.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL); + properties2.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER); + properties2.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD); + properties2.put(IntegrityAuditProperties.SITE_NAME, "SiteA"); + properties2.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml"); + properties2.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5"); + String persistenceUnit2 = "testPU"; + String resourceName2 = "pdp2"; + new DbDAO(resourceName2, persistenceUnit2, properties2); + + /* + * Sleep long enough to allow + * + * 1) pdp1 to run audit (15 seconds) + * + * 2) Logic to detect that other node, pdp2, is not available for designation (60 seconds) + * + * 3) pdp1 to run audit again (15 seconds) + */ + logger.info("testTwoResourcesOneDead: Sleeping 100 seconds"); + Thread.sleep(100000); + + logger.info("testTwoResourcesOneDead: Stopping audit thread"); + integrityAudit.stopAuditThread(); + + FileInputStream fstream = new FileInputStream(TEST_LOG); + BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); + String strLine; + int startIndex; + int endIndex; + ArrayList expectedResult = new ArrayList(Arrays.asList("pdp1", "pdp1")); + ArrayList delegates = new ArrayList(); + while ((strLine = br.readLine()) != null) { + /* parse strLine to obtain what you want */ + if (strLine.contains("Starting audit simulation for resourceName=")) { + startIndex = strLine.indexOf("resourceName=") + 13; + endIndex = strLine.indexOf(","); + + String rName = strLine.substring(startIndex, endIndex); + + delegates.add(rName); + } + } + + for (String delegate: delegates) { + logger.info("testTwoResourcesOneDead: delegate: " + delegate); + } + + fstream.close(); + + assertTrue("delegate count only " + delegates.size(), delegates.size() >= 2); + assertTrue("delegate 0 is " + expectedResult.get(0), expectedResult.get(0).equals(delegates.get(0))); + assertTrue("delegate 1 is " + expectedResult.get(1), expectedResult.get(1).equals(delegates.get(1))); + + logger.info("testTwoResourcesOneDead: Exiting"); + + } + + + /* + * Tests designation logic when three functioning resources are in play. Designation should + * round robin among resources. + * + * Note: console.log must be examined to ascertain whether or not this test was successful. + */ + @Ignore + @Test + public void testThreeResources() throws Exception { + + logger.info("testThreeResources: Entering"); + + /* + * Start audit for pdp1. + */ + IntegrityAudit integrityAudit = new IntegrityAudit(resourceName, persistenceUnit, properties); + integrityAudit.startAuditThread(); + + /* + * Start audit for pdp2. + */ + Properties properties2 = new Properties(); + properties2.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER); + properties2.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL); + properties2.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER); + properties2.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD); + properties2.put(IntegrityAuditProperties.SITE_NAME, "SiteA"); + properties2.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml"); + properties2.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5"); + String persistenceUnit2 = "testPU"; + String resourceName2 = "pdp2"; + IntegrityAudit integrityAudit2 = new IntegrityAudit(resourceName2, persistenceUnit2, properties2); + integrityAudit2.startAuditThread(); + + /* + * Start audit for pdp3. + */ + Properties properties3 = new Properties(); + properties3.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER); + properties3.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL); + properties3.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER); + properties3.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD); + properties3.put(IntegrityAuditProperties.SITE_NAME, "SiteA"); + properties3.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml"); + properties3.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5"); + String persistenceUnit3 = "testPU"; + String resourceName3 = "pdp3"; + IntegrityAudit integrityAudit3 = new IntegrityAudit(resourceName3, persistenceUnit3, properties3); + integrityAudit3.startAuditThread(); + + /* + * Sleep long enough to allow + * + * 1) pdp1 to run audit (15 seconds) + * + * 2) Logic to detect that pdp1 is stale and designate pdp2 (30 seconds) + * + * 3) pdp2 to run audit (15 seconds) + * + * 4) Logic to detect that pdp2 is stale and designate pdp3 (30 seconds) + * + * 5) pdp3 to run audit (15 seconds) + * + * 6) Logic to detect that pdp3 is stale and designate pdp1 (30 seconds) + * + * 7) pdp1 to run audit (15 seconds) + */ + logger.info("testThreeResources: Sleeping 160 seconds"); + Thread.sleep(160000); + + logger.info("testThreeResources: Stopping threads"); + integrityAudit.stopAuditThread(); + integrityAudit2.stopAuditThread(); + integrityAudit3.stopAuditThread(); + + FileInputStream fstream = new FileInputStream(TEST_LOG); + BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); + String strLine; + int startIndex; + int endIndex; + ArrayList expectedResult = new ArrayList(Arrays.asList("pdp1", "pdp2", "pdp3", "pdp1")); + ArrayList delegates = new ArrayList(); + while ((strLine = br.readLine()) != null) { + /* parse strLine to obtain what you want */ + if (strLine.contains("Starting audit simulation for resourceName=")) { + startIndex = strLine.indexOf("resourceName=") + 13; + endIndex = strLine.indexOf(","); + + String rName = strLine.substring(startIndex, endIndex); + + delegates.add(rName); + } + } + + for (String delegate: delegates) { + logger.info("testThreeResources: delegate: " + delegate); + } + + fstream.close(); + + assertTrue("delegate count only " + delegates.size(), delegates.size() >= 3); + assertTrue("delegate 0 is " + expectedResult.get(0), expectedResult.get(0).equals(delegates.get(0))); + assertTrue("delegate 1 is " + expectedResult.get(1), expectedResult.get(1).equals(delegates.get(1))); + assertTrue("delegate 2 is " + expectedResult.get(2), expectedResult.get(2).equals(delegates.get(2))); + assertTrue("delegate 3 is " + expectedResult.get(3), expectedResult.get(3).equals(delegates.get(3))); + + logger.info("testThreeResources: Exiting"); + + } + + /* + * Tests designation logic when four functioning resources are in play, two + * with one PU, two with another. Audits for "testPU" and "integrityAuditPU" should run + * simultaneously. Designation should alternate between resources for each of the two + * persistence units. + * + * Note: console.log must be examined to ascertain whether or not this test + * was successful. + */ + @Ignore + @Test + public void testFourResourcesDifferentPus() throws Exception { + + logger.info("testFourResourcesDifferentPus: Entering"); + + /* + * Start audit for pdp1, testPU. + */ + IntegrityAudit integrityAudit = new IntegrityAudit(resourceName, persistenceUnit, properties); + integrityAudit.startAuditThread(); + + /* + * Start audit for pdp2, integrityAuditPU. + */ + Properties properties2 = new Properties(); + properties2.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER); + properties2.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL); + properties2.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER); + properties2.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD); + properties2.put(IntegrityAuditProperties.SITE_NAME, "SiteA"); + properties2.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml"); + properties2.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5"); + String persistenceUnit2 = "integrityAuditPU"; + String resourceName2 = "pdp2"; + IntegrityAudit integrityAudit2 = new IntegrityAudit(resourceName2, persistenceUnit2, properties2); + integrityAudit2.startAuditThread(); + + /* + * Start audit for pdp3, testPU. + */ + Properties properties3 = new Properties(); + properties3.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER); + properties3.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL); + properties3.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER); + properties3.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD); + properties3.put(IntegrityAuditProperties.SITE_NAME, "SiteA"); + properties3.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml"); + properties3.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5"); + String persistenceUnit3 = "testPU"; + String resourceName3 = "pdp3"; + IntegrityAudit integrityAudit3 = new IntegrityAudit(resourceName3, persistenceUnit3, properties3); + integrityAudit3.startAuditThread(); + + /* + * Start audit for pdp4, integrityAuditPU. + */ + Properties properties4 = new Properties(); + properties4.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER); + properties4.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL); + properties4.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER); + properties4.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD); + properties4.put(IntegrityAuditProperties.SITE_NAME, "SiteA"); + properties4.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml"); + properties4.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5"); + String persistenceUnit4 = "integrityAuditPU"; + String resourceName4 = "pdp4"; + IntegrityAudit integrityAudit4 = new IntegrityAudit(resourceName4, persistenceUnit4, properties4); + integrityAudit4.startAuditThread(); + + /* + * Sleep long enough to allow + * + * 1) pdp1 and pdp2 to run audit simultaneously (15 seconds) + * + * 2) Logic to detect that pdp1 and pdp2 are stale and designate pdp3 (one's counterpart) and pdp4 (two's counterpart) (30 seconds) + * + * 3) pdp3 and pdp4 to run audit simultaneously (15 seconds) + * + * 4) Logic to detect that pdp3 and pdp4 are stale and designate pdp1 (three's counterpart) and pdp2 (four's counterpart) (30 seconds) + * + * 5) pdp1 and pdp2 to run audit simultaneously (15 seconds) + */ + logger.info("testFourResourcesDifferentPus: Sleeping 120 seconds"); + Thread.sleep(120000); + + logger.info("testFourResourcesDifferentPus: Stopping threads"); + integrityAudit.stopAuditThread(); + integrityAudit2.stopAuditThread(); + integrityAudit3.stopAuditThread(); + integrityAudit4.stopAuditThread(); + + FileInputStream fstream = new FileInputStream(TEST_LOG); + BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); + String strLine; + int startIndex; + int endIndex; + ArrayList expectedResult = new ArrayList(Arrays.asList("pdp1", "pdp2", "pdp3", "pdp4", "pdp1", "pdp2")); + ArrayList delegates = new ArrayList(); + while ((strLine = br.readLine()) != null) { + /* parse strLine to obtain what you want */ + if (strLine.contains("Starting audit simulation for resourceName=")) { + startIndex = strLine.indexOf("resourceName=") + 13; + endIndex = strLine.indexOf(","); + + String rName = strLine.substring(startIndex, endIndex); + + delegates.add(rName); + } + } + + for (String delegate: delegates) { + logger.info("testFourResourcesDifferentPus: delegate: " + delegate); + } + + fstream.close(); + + assertTrue("delegate count only " + delegates.size(), delegates.size() >= 6); + assertTrue("delegate 0 is " + expectedResult.get(0), expectedResult.get(0).equals(delegates.get(0))); + assertTrue("delegate 1 is " + expectedResult.get(1), expectedResult.get(1).equals(delegates.get(1))); + assertTrue("delegate 2 is " + expectedResult.get(2), expectedResult.get(2).equals(delegates.get(2))); + assertTrue("delegate 3 is " + expectedResult.get(3), expectedResult.get(3).equals(delegates.get(3))); + assertTrue("delegate 4 is " + expectedResult.get(4), expectedResult.get(4).equals(delegates.get(4))); + assertTrue("delegate 5 is " + expectedResult.get(5), expectedResult.get(5).equals(delegates.get(5))); + + logger.info("testFourResourcesDifferentPus: Exiting"); + + } + + /* + * Tests designation logic when four resources are in play but one is not + * functioning. Designation should round robin among functioning resources + * only. + * + * Note: console.log must be examined to ascertain whether or not this test + * was successful. + */ + @Ignore + @Test + public void testFourResourcesOneDead() throws Exception { + + logger.info("testFourResourcesOneDead: Entering"); + + /* + * Start audit for pdp1. + */ + IntegrityAudit integrityAudit = new IntegrityAudit(resourceName, persistenceUnit, properties); + integrityAudit.startAuditThread(); + + /* + * Start audit for pdp2. + */ + Properties properties2 = new Properties(); + properties2.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER); + properties2.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL); + properties2.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER); + properties2.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD); + properties2.put(IntegrityAuditProperties.SITE_NAME, "SiteA"); + properties2.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml"); + properties2.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5"); + String persistenceUnit2 = "testPU"; + String resourceName2 = "pdp2"; + IntegrityAudit integrityAudit2 = new IntegrityAudit(resourceName2, persistenceUnit2, properties2); + integrityAudit2.startAuditThread(); + + /* + * Populate DB for pdp3, which will simulate it having registered but then having died. + */ + Properties properties3 = new Properties(); + properties3.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER); + properties3.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL); + properties3.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER); + properties3.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD); + properties3.put(IntegrityAuditProperties.SITE_NAME, "SiteA"); + properties3.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml"); + properties3.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5"); + String persistenceUnit3 = "testPU"; + String resourceName3 = "pdp3"; + new DbDAO(resourceName3, persistenceUnit3, properties3); + + /* + * Start audit for pdp4. + */ + Properties properties4 = new Properties(); + properties4.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER); + properties4.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL); + properties4.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER); + properties4.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD); + properties4.put(IntegrityAuditProperties.SITE_NAME, "SiteA"); + properties4.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml"); + properties4.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5"); + String persistenceUnit4 = "testPU"; + String resourceName4 = "pdp4"; + IntegrityAudit integrityAudit4 = new IntegrityAudit(resourceName4, persistenceUnit4, properties4); + integrityAudit4.startAuditThread(); + + /* + * Sleep long enough to allow + * + * 1) pdp1 to run audit (15 seconds) + * + * 2) Logic to detect that pdp1 is stale and designate pdp2 (30 seconds) + * + * 3) pdp2 to run audit (15 seconds) + * + * 4) Logic to detect that pdp2 is stale and designate pdp4 (30 seconds) + * + * 5) pdp4 to run audit (15 seconds) + * + * 6) Logic to detect that pdp4 is stale and designate pdp1 (30 seconds) + * + * 7) pdp1 to run audit (15 seconds) + * + * 8) Logic to detect that pdp1 is stale and designate pdp2 (30 seconds) + * + * 7) pdp2 to run audit (15 seconds) + */ + logger.info("testFourResourcesOneDead: Sleeping 210 seconds"); + Thread.sleep(210000); + + logger.info("testFourResourcesOneDead: Stopping threads"); + integrityAudit.stopAuditThread(); + integrityAudit2.stopAuditThread(); + integrityAudit4.stopAuditThread(); + + FileInputStream fstream = new FileInputStream(TEST_LOG); + BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); + String strLine; + int startIndex; + int endIndex; + ArrayList expectedResult = new ArrayList(Arrays.asList("pdp1", "pdp2", "pdp4", "pdp1", "pdp2", "pdp4")); + ArrayList delegates = new ArrayList(); + while ((strLine = br.readLine()) != null) { + /* parse strLine to obtain what you want */ + if (strLine.contains("Starting audit simulation for resourceName=")) { + startIndex = strLine.indexOf("resourceName=") + 13; + endIndex = strLine.indexOf(","); + + String rName = strLine.substring(startIndex, endIndex); + + delegates.add(rName); + } + } + + for (String delegate : delegates) { + logger.info("testFourResourcesOneDead: delegate: " + delegate); + } + + fstream.close(); + + assertTrue("delegate count only " + delegates.size(), delegates.size() >= 6); + assertTrue("delegate 0 is " + expectedResult.get(0), expectedResult.get(0).equals(delegates.get(0))); + assertTrue("delegate 1 is " + expectedResult.get(1), expectedResult.get(1).equals(delegates.get(1))); + assertTrue("delegate 2 is " + expectedResult.get(2), expectedResult.get(2).equals(delegates.get(2))); + assertTrue("delegate 3 is " + expectedResult.get(3), expectedResult.get(3).equals(delegates.get(3))); + assertTrue("delegate 4 is " + expectedResult.get(4), expectedResult.get(4).equals(delegates.get(4))); + assertTrue("delegate 5 is " + expectedResult.get(5), expectedResult.get(5).equals(delegates.get(5))); + + logger.info("testFourResourcesOneDead: Exiting"); + + } + + /* + * Tests designation logic when four resources are in play but only one is + * functioning. Designation should remain with sole functioning resource. + * + * Note: console.log must be examined to ascertain whether or not this test + * was successful. + */ + @Ignore + @Test + public void testFourResourcesThreeDead() throws Exception { + + logger.info("testFourResourcesThreeDead: Entering"); + + /* + * Populate DB for pdp1, which will simulate it having registered but then having died. + */ + new DbDAO(resourceName, persistenceUnit, properties); + + + /* + * Populate DB for pdp2, which will simulate it having registered but then having died. + */ + Properties properties2 = new Properties(); + properties2.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER); + properties2.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL); + properties2.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER); + properties2.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD); + properties2.put(IntegrityAuditProperties.SITE_NAME, "SiteA"); + properties2.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml"); + properties2.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5"); + String persistenceUnit2 = "testPU"; + String resourceName2 = "pdp2"; + new DbDAO(resourceName2, persistenceUnit2, properties2); + + /* + * Start audit for pdp3. + */ + Properties properties3 = new Properties(); + properties3.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER); + properties3.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL); + properties3.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER); + properties3.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD); + properties3.put(IntegrityAuditProperties.SITE_NAME, "SiteA"); + properties3.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml"); + properties3.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5"); + String persistenceUnit3 = "testPU"; + String resourceName3 = "pdp3"; + IntegrityAudit integrityAudit3 = new IntegrityAudit(resourceName3, persistenceUnit3, properties3); + integrityAudit3.startAuditThread(); + + /* + * Populate DB for pdp4, which will simulate it having registered but then having died. + */ + Properties properties4 = new Properties(); + properties4.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER); + properties4.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL); + properties4.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER); + properties4.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD); + properties4.put(IntegrityAuditProperties.SITE_NAME, "SiteA"); + properties4.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml"); + properties4.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5"); + String persistenceUnit4 = "testPU"; + String resourceName4 = "pdp4"; + new DbDAO(resourceName4, persistenceUnit4, properties4); + + /* + * Sleep long enough to allow + * + * 1) pdp3 to discover that all other designation candidates are stale (30 seconds) + * + * 1) pdp3 to run audit (15 seconds) + * + * 2) Logic to detect that no other nodes are available for designation (60 seconds) + * + * 3) pdp3 to run audit again (15 seconds) + */ + logger.info("testFourResourcesThreeDead: Sleeping 130 seconds"); + Thread.sleep(130000); + + logger.info("testFourResourcesThreeDead: Stopping thread"); + integrityAudit3.stopAuditThread(); + + FileInputStream fstream = new FileInputStream(TEST_LOG); + BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); + String strLine; + int startIndex; + int endIndex; + ArrayList expectedResult = new ArrayList(Arrays.asList("pdp3", "pdp3")); + ArrayList delegates = new ArrayList(); + while ((strLine = br.readLine()) != null) { + /* parse strLine to obtain what you want */ + if (strLine.contains("Starting audit simulation for resourceName=")) { + startIndex = strLine.indexOf("resourceName=") + 13; + endIndex = strLine.indexOf(","); + + String rName = strLine.substring(startIndex, endIndex); + + delegates.add(rName); + } + } + + for (String delegate : delegates) { + logger.info("testFourResourcesThreeDead: delegate: " + delegate); + } + + fstream.close(); + + assertTrue("delegate count only " + delegates.size(), delegates.size() >= 2); + assertTrue("delegate 0 is " + expectedResult.get(0), expectedResult.get(0).equals(delegates.get(0))); + assertTrue("delegate 1 is " + expectedResult.get(1), expectedResult.get(1).equals(delegates.get(1))); + + logger.info("testFourResourcesThreeDead: Exiting"); + + } + + + /* + * Tests designation logic when the designated node dies and is no longer + * current + * + * Note: console.log must be examined to ascertain whether or not this test + * was successful. + */ + @Ignore + @Test + public void testDesignatedNodeDead() throws Exception { + logger.info("testDesignatedNodeDead: Entering"); + + /* + * Instantiate audit object for pdp1. + */ + IntegrityAudit integrityAudit = new IntegrityAudit(resourceName, persistenceUnit, properties); + + /* + * Start audit for pdp2. + */ + Properties properties2 = new Properties(); + properties2.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER); + properties2.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL); + properties2.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER); + properties2.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD); + properties2.put(IntegrityAuditProperties.SITE_NAME, "SiteA"); + properties2.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml"); + properties2.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5"); + String persistenceUnit2 = "testPU"; + String resourceName2 = "pdp2"; + IntegrityAudit integrityAudit2 = new IntegrityAudit(resourceName2, persistenceUnit2, properties2); + + /* + * Instantiate audit object for pdp3. + */ + Properties properties3 = new Properties(); + properties3.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER); + properties3.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL); + properties3.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER); + properties3.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD); + properties3.put(IntegrityAuditProperties.SITE_NAME, "SiteA"); + properties3.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml"); + properties3.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5"); + String persistenceUnit3 = "testPU"; + String resourceName3 = "pdp3"; + IntegrityAudit integrityAudit3 = new IntegrityAudit(resourceName3, persistenceUnit3, properties3); + + // Start audit on pdp1 + logger.info("testDesignatedNodeDead: Start audit on pdp1"); + integrityAudit.startAuditThread(); + + // Start the auditing threads on other nodes. + logger.info("testDesignatedNodeDead: Start audit on pdp2"); + integrityAudit2.startAuditThread(); + logger.info("testDesignatedNodeDead: Start audit on pdp3"); + integrityAudit3.startAuditThread(); + + + // Kill audit on pdp1 + logger.info("testDesignatedNodeDead: Kill audit on pdp1"); + integrityAudit.stopAuditThread(); + + // Sleep long enough for pdp1 to get stale and pdp2 to take over + logger.info("testDesignatedNodeDead: Sleep long enough for pdp1 to get stale and pdp2 to take over"); + Thread.sleep(AuditThread.AUDIT_COMPLETION_INTERVAL + FUDGE_FACTOR); + + // Start audit thread on pdp1 again. + logger.info("testDesignatedNodeDead: Start audit thread on pdp1 again."); + integrityAudit.startAuditThread(); + + // Sleep long enough for pdp2 to complete its audit and get stale, at + // which point pdp3 should take over + logger.info("testDesignatedNodeDead: Sleep long enough for pdp2 to complete its audit and get stale, at which point pdp3 should take over"); + Thread.sleep((AuditThread.AUDIT_SIMULATION_SLEEP_INTERVAL * AuditThread.AUDIT_SIMULATION_ITERATIONS) + + AuditThread.AUDIT_COMPLETION_INTERVAL + FUDGE_FACTOR); + + // Kill audit on pdp3 + logger.info("testDesignatedNodeDead: Killing audit on pdp3"); + integrityAudit3.stopAuditThread(); + + // Sleep long enough for pdp3 to get stale and pdp1 to take over + logger.info("testDesignatedNodeDead: Sleep long enough for pdp3 to get stale and pdp1 to take over"); + Thread.sleep(AuditThread.AUDIT_COMPLETION_INTERVAL + FUDGE_FACTOR); + + FileInputStream fstream = new FileInputStream(TEST_LOG); + BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); + String strLine; + int startIndex; + int endIndex; + ArrayList expectedResult = new ArrayList(Arrays.asList("pdp1", "pdp2", "pdp3", "pdp1")); + ArrayList delegates = new ArrayList(); + while ((strLine = br.readLine()) != null) { + /* parse strLine to obtain what you want */ + if (strLine.contains("Starting audit simulation for resourceName=")) { + startIndex = strLine.indexOf("resourceName=") + 13; + endIndex = strLine.indexOf(","); + + String rName = strLine.substring(startIndex, endIndex); + + delegates.add(rName); + } + } + fstream.close(); + + // Stop remaining threads. + logger.info("testDesignatedNodeDead: Stopping remaining threads"); + integrityAudit.stopAuditThread(); + integrityAudit2.stopAuditThread(); + + for (String delegate: delegates) { + logger.info("testDesignatedNodeDead: delegate: " + delegate); + } + + assertTrue("delegate count only " + delegates.size(), delegates.size() >= 4); + assertTrue("delegate 0 is " + expectedResult.get(0), expectedResult.get(0).equals(delegates.get(0))); + assertTrue("delegate 1 is " + expectedResult.get(1), expectedResult.get(1).equals(delegates.get(1))); + assertTrue("delegate 2 is " + expectedResult.get(2), expectedResult.get(2).equals(delegates.get(2))); + assertTrue("delegate 3 is " + expectedResult.get(3), expectedResult.get(3).equals(delegates.get(3))); + + logger.info("testDesignatedNodeDead: Exiting"); + } +} diff --git a/integrity-audit/src/test/java/org/onap/policy/common/ia/test/jpa/IaTestEntity.java b/integrity-audit/src/test/java/org/onap/policy/common/ia/test/jpa/IaTestEntity.java new file mode 100644 index 00000000..53289e50 --- /dev/null +++ b/integrity-audit/src/test/java/org/onap/policy/common/ia/test/jpa/IaTestEntity.java @@ -0,0 +1,157 @@ +/*- + * ============LICENSE_START======================================================= + * Integrity Audit + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.common.ia.test.jpa; + +import java.io.Serializable; +import java.util.Date; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.NamedQueries; +import javax.persistence.NamedQuery; +import javax.persistence.PrePersist; +import javax.persistence.PreUpdate; +import javax.persistence.Table; +import javax.persistence.Temporal; +import javax.persistence.TemporalType; + +@Entity +@Table(name="IaTestEntity") +@NamedQueries({ + @NamedQuery(name=" IaTestEntity.findAll", query="SELECT e FROM IaTestEntity e "), + @NamedQuery(name="IaTestEntity.deleteAll", query="DELETE FROM IaTestEntity WHERE 1=1") +}) +//@SequenceGenerator(name="seqImTest", initialValue=1, allocationSize=1) + +public class IaTestEntity implements Serializable { + private static final long serialVersionUID = 1L; + + @Id + //@GeneratedValue(strategy = GenerationType.SEQUENCE, generator="seqImTest") + @GeneratedValue(strategy = GenerationType.AUTO) + @Column(name="ImTestId") + private long imTestId; + + @Column(name="created_by", nullable=false, length=255) + private String createdBy = "guest"; + + @Column(name="person", nullable=false, length=255) + private PersonTest person; + + @Temporal(TemporalType.TIMESTAMP) + @Column(name="created_date", updatable=false) + private Date createdDate; + + @Column(name="modified_by", nullable=false, length=255) + private String modifiedBy = "guest"; + + @Temporal(TemporalType.TIMESTAMP) + @Column(name="modified_date", nullable=false) + private Date modifiedDate; + + public IaTestEntity() { + } + + @PrePersist + public void prePersist() { + Date date = new Date(); + this.createdDate = date; + this.modifiedDate = date; + } + + @PreUpdate + public void preUpdate() { + this.modifiedDate = new Date(); + } + + /** + * @return the Id + */ + public long getImTestId() { + return imTestId; + } + + /** + * @return the createdBy + */ + public String getCreatedBy() { + return createdBy; + } + + /** + * @param createdBy the createdBy to set + */ + public void setCreatedBy(String createdBy) { + this.createdBy = createdBy; + } + + /** + * @return the modifiedBy + */ + public String getModifiedBy() { + return modifiedBy; + } + + /** + * @param modifiedBy the modifiedBy to set + */ + public void setModifiedBy(String modifiedBy) { + this.modifiedBy = modifiedBy; + } + + /** + * @return the modifiedDate + */ + public Date getModifiedDate() { + return modifiedDate; + } + + /** + * @param modifiedDate the modifiedDate to set + */ + public void setModifiedDate(Date modifiedDate) { + this.modifiedDate = modifiedDate; + } + + /** + * @return the createdDate + */ + public Date getCreatedDate() { + return createdDate; + } + + /** + * @param the person to set + */ + public void setPersonTest(PersonTest p) { + this.person = p; + } + + /** + * @return the person + */ + public PersonTest getPersonTest() { + return person; + } +} diff --git a/integrity-audit/src/test/java/org/onap/policy/common/ia/test/jpa/PersonTest.java b/integrity-audit/src/test/java/org/onap/policy/common/ia/test/jpa/PersonTest.java new file mode 100644 index 00000000..0ca986bc --- /dev/null +++ b/integrity-audit/src/test/java/org/onap/policy/common/ia/test/jpa/PersonTest.java @@ -0,0 +1,64 @@ +/*- + * ============LICENSE_START======================================================= + * Integrity Audit + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.common.ia.test.jpa; + +import java.io.Serializable; + +public class PersonTest implements Serializable { + /** + * + */ + private static final long serialVersionUID = 1L; + private String firstName; + private String lastName; + private int age; + + public PersonTest(String first, String last, int age) { + this.firstName = first; + this.lastName = last; + this.age = age; + } + + public String getFirstName() { + return this.firstName; + } + + public void setFirstName(String name) { + this.firstName = name; + } + + public String getLasttName() { + return this.lastName; + } + + public void setLastName(String name) { + this.lastName = name; + } + + public int getAge() { + return this.age; + } + + public void setAge(int age) { + this.age = age; + } + +} diff --git a/integrity-audit/src/test/java/org/openecomp/policy/common/ia/test/AuditPeriodTest.java b/integrity-audit/src/test/java/org/openecomp/policy/common/ia/test/AuditPeriodTest.java deleted file mode 100644 index d0c64065..00000000 --- a/integrity-audit/src/test/java/org/openecomp/policy/common/ia/test/AuditPeriodTest.java +++ /dev/null @@ -1,480 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Integrity Audit - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.openecomp.policy.common.ia.test; - - -import static org.junit.Assert.*; - -import java.io.BufferedReader; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.InputStreamReader; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Properties; - -import javax.persistence.EntityManager; -import javax.persistence.EntityManagerFactory; -import javax.persistence.EntityTransaction; -import javax.persistence.Persistence; - -//import org.apache.commons.logging.Log; -//import org.apache.commons.logging.LogFactory; -import org.junit.After; -import org.junit.Before; -import org.junit.Ignore; -import org.junit.Test; - -import org.openecomp.policy.common.ia.AuditThread; -import org.openecomp.policy.common.ia.IntegrityAudit; -import org.openecomp.policy.common.ia.IntegrityAuditProperties; -import org.openecomp.policy.common.logging.flexlogger.FlexLogger; -import org.openecomp.policy.common.logging.flexlogger.Logger; - -/* - * All JUnits are designed to run in the local development environment - * where they have write privileges and can execute time-sensitive - * tasks. - */ -public class AuditPeriodTest { - - private static Logger logger = FlexLogger.getLogger(AuditPeriodTest.class); - - private static final String AUDIT_PERIOD_TEST_LOG = "./testingLogs/common-modules/integrity-audit/debug.log"; - - private static String persistenceUnit; - private static Properties properties; - private static String resourceName; - - @Before - public void setUp() throws Exception { - - - System.out.println("setUp: Clearing " + AUDIT_PERIOD_TEST_LOG); - FileOutputStream fstream = new FileOutputStream(AUDIT_PERIOD_TEST_LOG); - fstream.close(); - - logger.info("setUp: Entering"); - - IntegrityAudit.isUnitTesting = true; - - properties = new Properties(); - properties.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER); - properties.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL); - properties.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER); - properties.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD); - properties.put(IntegrityAuditProperties.SITE_NAME, "SiteA"); - properties.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml"); - - persistenceUnit = "testPU"; - resourceName = "pdp1"; - - //Clean up the DB - EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit, properties); - - EntityManager em = emf.createEntityManager(); - // Start a transaction - EntityTransaction et = em.getTransaction(); - - et.begin(); - - // if IntegrityAuditEntity entry exists for resourceName and PU, update it. If not found, create a new entry - em.createQuery("Delete from IntegrityAuditEntity").executeUpdate(); - - // commit transaction - et.commit(); - em.close(); - - logger.info("setUp: Exiting"); - - } - - - @After - public void tearDown() throws Exception { - - logger.info("tearDown: Entering"); - - logger.info("tearDown: Exiting"); - - } - - /* - * Verifies (via log parsing) that when a negative audit period is - * specified, the audit is suppressed. - */ - @Ignore - @Test - public void testNegativeAuditPeriod() throws Exception { - - logger.info("testNegativeAuditPeriod: Entering"); - - properties.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "-1"); - - IntegrityAudit integrityAudit = new IntegrityAudit(resourceName, persistenceUnit, properties); - integrityAudit.startAuditThread(); - - /* - * Sleep long enough to allow - * - * 1) audit to immediately terminate. - */ - Thread.sleep(1000); - - logger.info("testNegativeAuditPeriod: Stopping audit thread (should be a no-op!)"); - integrityAudit.stopAuditThread(); - - FileInputStream fstream = new FileInputStream(AUDIT_PERIOD_TEST_LOG); - BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); - String strLine; - int startIndex; - ArrayList expectedResult = new ArrayList(Arrays.asList("-1")); - ArrayList delegates = new ArrayList(); - while ((strLine = br.readLine()) != null) { - /* parse strLine to obtain what you want */ - if (strLine.contains("Suppressing integrity audit, integrityAuditPeriodSeconds=")) { - startIndex = strLine.indexOf("integrityAuditPeriodSeconds=") + 28; - - String integrityAuditPeriodSeconds = strLine.substring(startIndex); - - delegates.add(integrityAuditPeriodSeconds); - } - } - - for (String delegate: delegates) { - logger.info("testNegativeAuditPeriod: delegate: " + delegate); - } - - fstream.close(); - - assertTrue(expectedResult.equals(delegates)); - - logger.info("testNegativeAuditPeriod: Exiting"); - - } - - /* - * Verifies (via log parsing) that when an audit period of zero is - * specified, the audit runs continuously, generating a number of - * sleep/wake sequences in a short period of time (e.g. 100ms). - */ - @Ignore - @Test - public void testZeroAuditPeriod() throws Exception { - - logger.info("testZeroAuditPeriod: Entering"); - - properties.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "0"); - - IntegrityAudit integrityAudit = new IntegrityAudit(resourceName, - persistenceUnit, properties); - integrityAudit.startAuditThread(); - - /* - * Sleep long enough to allow - * - * 1) audit to generate a bunch of sleep wake sequences. - * - * Note: - * - * (AuditThread.AUDIT_SIMULATION_SLEEP_INTERVAL * - * AuditThread.AUDIT_SIMULATION_ITERATIONS) is the time it takes for the - * audit simulation to run. - * - * (integrityAudit.getIntegrityAuditPeriodSeconds() should return a - * value of zero; i.e. audit should not sleep at all between iterations - * - * "100"ms is the time we allow the audit to cycle continuously - */ - long sleepMillis = (AuditThread.AUDIT_SIMULATION_SLEEP_INTERVAL * AuditThread.AUDIT_SIMULATION_ITERATIONS) - + (integrityAudit.getIntegrityAuditPeriodSeconds() * 1000) - + 100; - logger.info("testZeroAuditPeriod: Sleeping " + sleepMillis + "ms before stopping auditThread"); - Thread.sleep(sleepMillis); - - logger.info("testZeroAuditPeriod: Stopping audit thread"); - integrityAudit.stopAuditThread(); - - /* - * Before audit completion message upon awaking from sleep is upper case "Awaking". After audit - * completion, all awakings are lower case "awaking". - */ - logger.info("testZeroAuditPeriod: Parsing " + AUDIT_PERIOD_TEST_LOG + " for 'awaking'"); - FileInputStream fstream = new FileInputStream(AUDIT_PERIOD_TEST_LOG); - BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); - String strLine = ""; - int awakings = 0; - int lines = 0; - while ((strLine = br.readLine()) != null) { - if (strLine.contains("Awaking from 0ms sleep")) { - fail("Audit appears not to have run!? Got '" + strLine + "'"); - } else { - if (strLine.contains("awaking from 0ms sleep")) { - awakings++; - } - } - lines++; - } - logger.info("testZeroAuditPeriod: Done parsing " - + AUDIT_PERIOD_TEST_LOG + " for 'awaking'; lines parsed=" - + lines + ", closing stream"); - fstream.close(); - - /* - * We should get at least 10 sleep/wake sequences. - */ - assertTrue("Only " + awakings + " awakings", awakings > 10); - assertTrue(integrityAudit.getIntegrityAuditPeriodSeconds() == 0); - - logger.info("testZeroAuditPeriod: Exiting, awakings=" - + awakings + ", integrityAuditPeriodSeconds=" - + integrityAudit.getIntegrityAuditPeriodSeconds()); - - } - - /* - * Verifies (via log parsing) that when an audit period of five minutes is - * specified, there is a five minute interval between the audits run - * on each of three different entities. - */ - @Ignore - @Test - public void testFiveMinuteAuditPeriod() throws Exception { - - logger.info("testFiveMinuteAuditPeriod: Entering"); - - properties.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "300"); - - /* - * Start audit for pdp1. - */ - IntegrityAudit integrityAudit = new IntegrityAudit(resourceName, - persistenceUnit, properties); - integrityAudit.startAuditThread(); - - /* - * Start audit for pdp2. - */ - Properties properties2 = new Properties(); - properties2.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER); - properties2.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL); - properties2.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER); - properties2.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD); - properties2.put(IntegrityAuditProperties.SITE_NAME, "SiteA"); - properties2.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml"); - properties2.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "300"); - String persistenceUnit2 = "testPU"; - String resourceName2 = "pdp2"; - IntegrityAudit integrityAudit2 = new IntegrityAudit(resourceName2, persistenceUnit2, properties2); - integrityAudit2.startAuditThread(); - - /* - * Start audit for pdp3. - */ - Properties properties3 = new Properties(); - properties3.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER); - properties3.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL); - properties3.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER); - properties3.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD); - properties3.put(IntegrityAuditProperties.SITE_NAME, "SiteA"); - properties3.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml"); - properties3.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "300"); - String persistenceUnit3 = "testPU"; - String resourceName3 = "pdp3"; - IntegrityAudit integrityAudit3 = new IntegrityAudit(resourceName3, persistenceUnit3, properties3); - integrityAudit3.startAuditThread(); - - - /* - * 1) All three audit run once. This should take approximately 105 seconds, as follows: - * - * T0: pdp1 runs audit (15 seconds), then sleeps for five minutes (300 seconds) - * pdp2 recognizes that pdp1 is stale (30 seconds) and runs its audit (15 seconds) - * pdp3 recognizes that pdp2 is stale (30 seconds) and runs its audit (15 seconds) - * - * 2) Five minutes after T0, at T1, pdp1 wakes up and the above sequence begins again, - * which should take another 115 seconds: - * - * T1: pdp1 runs audit (15 seconds), then sleeps for two minutes (300 seconds) - * pdp2 wakes up, resets auditCompleted and sleeps (5 seconds), recognizes that pdp1 is stale (30 seconds) and runs its audit (15 seconds) - * pdp3 wakes up, resets auditCompleted and sleeps (5 seconds), recognizes that pdp2 is stale (30 seconds) and runs its audit (15 seconds) - * - * So, the entire sequence should take 15 + 300 + 115 = 430 seconds - * Adding a fudge factor, we sleep for 450 seconds - */ - Thread.sleep(450000); - - - logger.info("testFiveMinuteAuditPeriod: Stopping all three audit threads"); - integrityAudit.stopAuditThread(); - - integrityAudit.stopAuditThread(); - integrityAudit2.stopAuditThread(); - integrityAudit3.stopAuditThread(); - - FileInputStream fstream = new FileInputStream(AUDIT_PERIOD_TEST_LOG); - BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); - String strLine; - int startIndex; - int endIndex; - ArrayList expectedResult = new ArrayList(Arrays.asList("pdp1", "pdp2", "pdp3", "pdp1", "pdp2", "pdp3")); - ArrayList delegates = new ArrayList(); - while ((strLine = br.readLine()) != null) { - /* parse strLine to obtain what you want */ - if (strLine.contains("Starting audit simulation for resourceName=")) { - startIndex = strLine.indexOf("resourceName=") + 13; - endIndex = strLine.indexOf(","); - - String rName = strLine.substring(startIndex, endIndex); - - delegates.add(rName); - } - } - - for (String delegate: delegates) { - logger.info("testFiveMinuteAuditPeriod: delegate: " + delegate); - } - - fstream.close(); - - assertTrue("delegate count only " + delegates.size(), delegates.size() >= 6); - assertTrue("delegate 0 is " + expectedResult.get(0), expectedResult.get(0).equals(delegates.get(0))); - assertTrue("delegate 1 is " + expectedResult.get(1), expectedResult.get(1).equals(delegates.get(1))); - assertTrue("delegate 2 is " + expectedResult.get(2), expectedResult.get(2).equals(delegates.get(2))); - assertTrue("delegate 3 is " + expectedResult.get(3), expectedResult.get(3).equals(delegates.get(3))); - assertTrue("delegate 4 is " + expectedResult.get(4), expectedResult.get(4).equals(delegates.get(4))); - assertTrue("delegate 5 is " + expectedResult.get(5), expectedResult.get(5).equals(delegates.get(5))); - - logger.info("testFiveMinuteAuditPeriod: Exiting"); - } - - /* - * Verifies (via log parsing) that when an audit period of 20 seconds is - * specified, there is a 20 second interval between the audits run - * on each of three different entities. - */ - @Ignore - @Test - public void testTwentySecondAuditPeriod() throws Exception { - - logger.info("testTwentySecondAuditPeriod: Entering"); - - properties.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "20"); - - /* - * Start audit for pdp1. - */ - IntegrityAudit integrityAudit = new IntegrityAudit(resourceName, - persistenceUnit, properties); - integrityAudit.startAuditThread(); - - /* - * Start audit for pdp2. - */ - Properties properties2 = new Properties(); - properties2.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER); - properties2.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL); - properties2.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER); - properties2.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD); - properties2.put(IntegrityAuditProperties.SITE_NAME, "SiteA"); - properties2.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml"); - properties2.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "20"); - String persistenceUnit2 = "testPU"; - String resourceName2 = "pdp2"; - IntegrityAudit integrityAudit2 = new IntegrityAudit(resourceName2, persistenceUnit2, properties2); - integrityAudit2.startAuditThread(); - - /* - * Start audit for pdp3. - */ - Properties properties3 = new Properties(); - properties3.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER); - properties3.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL); - properties3.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER); - properties3.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD); - properties3.put(IntegrityAuditProperties.SITE_NAME, "SiteA"); - properties3.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml"); - properties3.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "20"); - String persistenceUnit3 = "testPU"; - String resourceName3 = "pdp3"; - IntegrityAudit integrityAudit3 = new IntegrityAudit(resourceName3, persistenceUnit3, properties3); - integrityAudit3.startAuditThread(); - - - /* - * 1) All three audit run once. - * - * pdp1 runs audit (15 seconds), then goes into 20 second sleep cycles - * pdp2 recognizes that pdp1 is stale (30 seconds), runs its audit (15 seconds), then goes into 20 second sleep cycles - * pdp3 recognizes that pdp2 is stale (30 seconds), runs its audit (15 seconds), then goes into 20 second sleep cycles - * - * 2) Eventually pdp2 gets stale, pdp1 recognizes this and cycle begins again. - * - * So, we allow 15 + (5 * 45) = 240 seconds plus a fudge factor. - * - */ - Thread.sleep(250000); - - - logger.info("testTwentySecondAuditPeriod: Stopping all three audit threads"); - integrityAudit.stopAuditThread(); - - integrityAudit.stopAuditThread(); - integrityAudit2.stopAuditThread(); - integrityAudit3.stopAuditThread(); - - FileInputStream fstream = new FileInputStream(AUDIT_PERIOD_TEST_LOG); - BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); - String strLine; - int startIndex; - int endIndex; - ArrayList expectedResult = new ArrayList(Arrays.asList("pdp1", "pdp2", "pdp3", "pdp1", "pdp2", "pdp3")); - ArrayList delegates = new ArrayList(); - while ((strLine = br.readLine()) != null) { - /* parse strLine to obtain what you want */ - if (strLine.contains("Starting audit simulation for resourceName=")) { - startIndex = strLine.indexOf("resourceName=") + 13; - endIndex = strLine.indexOf(","); - - String rName = strLine.substring(startIndex, endIndex); - - delegates.add(rName); - } - } - - for (String delegate: delegates) { - logger.info("testTwentySecondAuditPeriod: delegate: " + delegate); - } - - fstream.close(); - - assertTrue("delegate count only " + delegates.size(), delegates.size() >= 6); - assertTrue("delegate 0 is " + expectedResult.get(0), expectedResult.get(0).equals(delegates.get(0))); - assertTrue("delegate 1 is " + expectedResult.get(1), expectedResult.get(1).equals(delegates.get(1))); - assertTrue("delegate 2 is " + expectedResult.get(2), expectedResult.get(2).equals(delegates.get(2))); - assertTrue("delegate 3 is " + expectedResult.get(3), expectedResult.get(3).equals(delegates.get(3))); - assertTrue("delegate 4 is " + expectedResult.get(4), expectedResult.get(4).equals(delegates.get(4))); - assertTrue("delegate 5 is " + expectedResult.get(5), expectedResult.get(5).equals(delegates.get(5))); - - logger.info("testTwentySecondAuditPeriod: Exiting"); - } - -} diff --git a/integrity-audit/src/test/java/org/openecomp/policy/common/ia/test/DbAuditCompareEntriesTest.java b/integrity-audit/src/test/java/org/openecomp/policy/common/ia/test/DbAuditCompareEntriesTest.java deleted file mode 100644 index e2fd922d..00000000 --- a/integrity-audit/src/test/java/org/openecomp/policy/common/ia/test/DbAuditCompareEntriesTest.java +++ /dev/null @@ -1,617 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Integrity Audit - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.openecomp.policy.common.ia.test; - -import static org.junit.Assert.*; - -import java.io.FileOutputStream; -import java.util.Date; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Properties; - -import javax.persistence.EntityManager; -import javax.persistence.EntityManagerFactory; -import javax.persistence.EntityTransaction; -import javax.persistence.Persistence; - - -//import org.apache.commons.logging.Log; -//import org.apache.commons.logging.LogFactory; -import org.junit.After; -import org.junit.Before; -import org.junit.Ignore; -import org.junit.Test; - -import org.openecomp.policy.common.ia.DbAudit; -import org.openecomp.policy.common.ia.DbDAO; -import org.openecomp.policy.common.ia.IntegrityAudit; -import org.openecomp.policy.common.ia.IntegrityAuditProperties; -import org.openecomp.policy.common.ia.jpa.IntegrityAuditEntity; -import org.openecomp.policy.common.ia.test.jpa.IaTestEntity; -import org.openecomp.policy.common.ia.test.jpa.PersonTest; -import org.openecomp.policy.common.logging.flexlogger.FlexLogger; -import org.openecomp.policy.common.logging.flexlogger.Logger; - -/* - * All JUnits are designed to run in the local development environment - * where they have write privileges and can execute time-sensitive - * tasks. - */ -public class DbAuditCompareEntriesTest { - - private static Logger logger = FlexLogger.getLogger(DbAuditCompareEntriesTest.class); - private DbDAO dbDAO; - private static String persistenceUnit; - private static Properties properties; - private static String resourceName; - private String dbDriver; - private String dbUrl; - private String dbUser; - private String dbPwd; - private String siteName; - private String nodeType; - private static final String TEST_LOG = "./testingLogs/common-modules/integrity-audit/debug.log"; - - @Before - public void setUp() throws Exception { - System.out.println("setUp: Clearing IntegrityAudit.log"); - //FileOutputStream fstream = new FileOutputStream("IntegrityAudit.log"); - FileOutputStream fstream = new FileOutputStream(TEST_LOG); - fstream.close(); - - logger.info("setUp: Entering"); - - IntegrityAudit.isUnitTesting = true; - - properties = new Properties(); - properties.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER); - properties.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL); - properties.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER); - properties.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD); - properties.put(IntegrityAuditProperties.SITE_NAME, "SiteA"); - properties.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml"); - - dbDriver = IntegrityAuditProperties.DEFAULT_DB_DRIVER; - dbUrl = IntegrityAuditProperties.DEFAULT_DB_URL; - dbUser = IntegrityAuditProperties.DEFAULT_DB_USER; - dbPwd = IntegrityAuditProperties.DEFAULT_DB_PWD; - siteName = "SiteA"; - nodeType = "pdp_xacml"; - persistenceUnit = "testPU"; - resourceName = "pdp1"; - - logger.info("setUp: Exiting"); - } - - /* - * Clean up DB after each test. - */ - @After - public void tearDown() throws Exception { - logger.info("tearDown: Entering"); - - logger.info("tearDown: Exiting"); - } - - /* - * Tests that a comparison between hashsets is successful if - * the entries match - */ - @Ignore - @Test - public void testSuccessfulComparison() throws Exception { - logger.info("testSuccessfulComparison: Entering"); - - dbDAO = new DbDAO(resourceName, persistenceUnit, properties); - DbAudit dbAudit = new DbAudit(dbDAO); - - String className = null; - //There is only one entry IntegrityAuditEntity, but we will check anyway - HashSet classNameSet = dbDAO.getPersistenceClassNames(); - for(String c : classNameSet){ - if (c.equals("org.openecomp.policy.common.ia.jpa.IntegrityAuditEntity")){ - className = c; - } - } - String resourceName1 = resourceName; - String resourceName2 = resourceName; - - IntegrityAuditEntity entry1 = new IntegrityAuditEntity(); - IntegrityAuditEntity entry2 = new IntegrityAuditEntity(); - Date date = new Date(); - - /* - * Two entries with the same field values - */ - entry1.setDesignated(false); - entry1.setJdbcDriver(dbDriver); - entry1.setJdbcPassword(dbPwd); - entry1.setJdbcUrl(dbUrl); - entry1.setJdbcUser(dbUser); - entry1.setLastUpdated(date); - entry1.setNodeType(nodeType); - entry1.setPersistenceUnit(persistenceUnit); - entry1.setResourceName(resourceName1); - entry1.setSite(siteName); - - entry2.setDesignated(false); - entry2.setJdbcDriver(dbDriver); - entry2.setJdbcPassword(dbPwd); - entry2.setJdbcUrl(dbUrl); - entry2.setJdbcUser(dbUser); - entry2.setLastUpdated(date); - entry2.setNodeType(nodeType); - entry2.setPersistenceUnit(persistenceUnit); - entry2.setResourceName(resourceName2); - entry2.setSite(siteName); - - dbAudit.writeAuditDebugLog(className, resourceName1, resourceName2, entry1, entry2); - - HashMap myEntries = new HashMap(); - HashMap theirEntries = new HashMap(); - - myEntries.put("pdp1", entry1); - theirEntries.put("pdp1", entry2); - - HashSet result = dbAudit.compareEntries(myEntries, theirEntries); - - /* - * Assert that there are no mismatches returned - */ - assertTrue(result.isEmpty()); - - logger.info("testSuccessfulComparison: Exit"); - } - - /* - * Tests that an error is detected if an entry in one hashset doesn't - * match the other - */ - @Ignore - @Test - public void testComparisonError() throws Exception { - logger.info("testComparisonError: Entering"); - - dbDAO = new DbDAO(resourceName, persistenceUnit, properties); - DbAudit dbAudit = new DbAudit(dbDAO); - - String resourceName1 = resourceName; - String resourceName2 = resourceName; - - IntegrityAuditEntity entry1 = new IntegrityAuditEntity(); - IntegrityAuditEntity entry2 = new IntegrityAuditEntity(); - Date date = new Date(); - - /* - * Create two entries with different designated values - */ - entry1.setDesignated(false); - entry1.setJdbcDriver(dbDriver); - entry1.setJdbcPassword(dbPwd); - entry1.setJdbcUrl(dbUrl); - entry1.setJdbcUser(dbUser); - entry1.setLastUpdated(date); - entry1.setNodeType(nodeType); - entry1.setPersistenceUnit(persistenceUnit); - entry1.setResourceName(resourceName1); - entry1.setSite(siteName); - - entry2.setDesignated(true); - entry2.setJdbcDriver(dbDriver); - entry2.setJdbcPassword(dbPwd); - entry2.setJdbcUrl(dbUrl); - entry2.setJdbcUser(dbUser); - entry2.setLastUpdated(date); - entry2.setNodeType(nodeType); - entry2.setPersistenceUnit(persistenceUnit); - entry2.setResourceName(resourceName2); - entry2.setSite(siteName); - - HashMap myEntries = new HashMap(); - HashMap theirEntries = new HashMap(); - - myEntries.put("pdp1", entry1); - theirEntries.put("pdp1", entry2); - - HashSet result = dbAudit.compareEntries(myEntries, theirEntries); - - /* - * Assert that there was one mismatch - */ - assertEquals(1, result.size()); - - logger.info("testComparisonError: Exit"); - } - - /* - * Tests that a mismatch/miss entry is detected if there are missing entries in - * one or both of the hashsets - */ - @Ignore - @Test - public void testCompareMissingEntries() throws Exception { - logger.info("testCompareMissingEntries: Entering"); - - dbDAO = new DbDAO(resourceName, persistenceUnit, properties); - DbAudit dbAudit = new DbAudit(dbDAO); - - String resourceName1 = resourceName; - String resourceName2 = resourceName; - - IntegrityAuditEntity entry1 = new IntegrityAuditEntity(); - IntegrityAuditEntity entry2 = new IntegrityAuditEntity(); - IntegrityAuditEntity entry3 = new IntegrityAuditEntity(); - IntegrityAuditEntity entry4 = new IntegrityAuditEntity(); - - Date date = new Date(); - - /* - * 4 entries, one mismatch, two miss entries - */ - entry1.setDesignated(false); - entry1.setJdbcDriver(dbDriver); - entry1.setJdbcPassword(dbPwd); - entry1.setJdbcUrl(dbUrl); - entry1.setJdbcUser(dbUser); - entry1.setLastUpdated(date); - entry1.setNodeType(nodeType); - entry1.setPersistenceUnit(persistenceUnit); - entry1.setResourceName(resourceName1); - entry1.setSite(siteName); - - entry2.setDesignated(true); - entry2.setJdbcDriver(dbDriver); - entry2.setJdbcPassword(dbPwd); - entry2.setJdbcUrl(dbUrl); - entry2.setJdbcUser(dbUser); - entry2.setLastUpdated(date); - entry2.setNodeType(nodeType); - entry2.setPersistenceUnit(persistenceUnit); - entry2.setResourceName(resourceName2); - entry2.setSite(siteName); - - entry3.setDesignated(false); - entry3.setJdbcDriver(dbDriver); - entry3.setJdbcPassword(dbPwd); - entry3.setJdbcUrl(dbUrl); - entry3.setJdbcUser(dbUser); - entry3.setLastUpdated(date); - entry3.setNodeType(nodeType); - entry3.setPersistenceUnit(persistenceUnit); - entry3.setResourceName(resourceName2); - entry3.setSite("SiteB"); - - entry4.setDesignated(false); - entry4.setJdbcDriver(dbDriver); - entry4.setJdbcPassword(dbPwd); - entry4.setJdbcUrl(dbUrl); - entry4.setJdbcUser(dbUser); - entry4.setLastUpdated(date); - entry4.setNodeType(nodeType); - entry4.setPersistenceUnit(persistenceUnit); - entry4.setResourceName(resourceName2); - entry4.setSite("SiteB"); - - HashMap myEntries = new HashMap(); - HashMap theirEntries = new HashMap(); - - myEntries.put("0", entry1); - myEntries.put("1", entry3); - theirEntries.put("0", entry2); - theirEntries.put("2", entry4); - - HashSet mismatchResult = dbAudit.compareEntries(myEntries, theirEntries); - - /* - * Assert 3 mismatches/missing entries were found - */ - assertEquals(3, mismatchResult.size()); - - logger.info("testCompareMissingEntries: Exit"); - } - - /* - * Tests that comparison algorithm works for each entity in the hashsets - */ - @Ignore - @Test - public void testCompareAllHashEntities() throws Exception { - logger.info("testCompareAllHashEntities: Entering"); - - dbDAO = new DbDAO(resourceName, persistenceUnit, properties); - DbAudit dbAudit = new DbAudit(dbDAO); - - HashSet classNameSet = dbDAO.getPersistenceClassNames(); - HashSet mismatchResult = new HashSet(); - for(String c : classNameSet) { - if (c.equals("org.openecomp.policy.common.ia.jpa.IntegrityAuditEntity")){ - String resourceName1 = resourceName; - String resourceName2 = resourceName; - - IntegrityAuditEntity entry1 = new IntegrityAuditEntity(); - IntegrityAuditEntity entry2 = new IntegrityAuditEntity(); - Date date = new Date(); - - /* - * Two entries with the same field values - */ - entry1.setDesignated(false); - entry1.setJdbcDriver(dbDriver); - entry1.setJdbcPassword(dbPwd); - entry1.setJdbcUrl(dbUrl); - entry1.setJdbcUser(dbUser); - entry1.setLastUpdated(date); - entry1.setNodeType(nodeType); - entry1.setPersistenceUnit(persistenceUnit); - entry1.setResourceName(resourceName1); - entry1.setSite(siteName); - - entry2.setDesignated(false); - entry2.setJdbcDriver(dbDriver); - entry2.setJdbcPassword(dbPwd); - entry2.setJdbcUrl(dbUrl); - entry2.setJdbcUser(dbUser); - entry2.setLastUpdated(date); - entry2.setNodeType(nodeType); - entry2.setPersistenceUnit(persistenceUnit); - entry2.setResourceName(resourceName2); - entry2.setSite(siteName); - - HashMap myEntries = new HashMap(); - HashMap theirEntries = new HashMap(); - - myEntries.put("pdp1", entry1); - theirEntries.put("pdp1", entry2); - - mismatchResult = dbAudit.compareEntries(myEntries, theirEntries); - - /* - * Assert there was no mismatches - */ - assertTrue(mismatchResult.isEmpty()); - } - else if (c.equals("org.openecomp.policy.common.ia.test.jpa.IaTestEntity")) { - IaTestEntity iate = new IaTestEntity(); - IaTestEntity iate2 = new IaTestEntity(); - IaTestEntity iate3 = new IaTestEntity(); - IaTestEntity iate4 = new IaTestEntity(); - - Date date = new Date(); - - /* - * Four entries, 2 mismatches - */ - iate.setCreatedBy("Ford"); - iate.setModifiedBy("Ford"); - iate.setModifiedDate(date); - - iate2.setCreatedBy("Ford"); - iate2.setModifiedBy("Zaphod"); - iate2.setModifiedDate(date); - - iate3.setCreatedBy("Zaphod"); - iate3.setModifiedBy("Ford"); - iate3.setModifiedDate(date); - - iate4.setCreatedBy("Ford"); - iate4.setModifiedBy("Ford"); - iate4.setModifiedDate(date); - - HashMap myEntries = new HashMap(); - HashMap theirEntries = new HashMap(); - - myEntries.put("0", iate); - myEntries.put("1", iate2); - theirEntries.put("0", iate3); - theirEntries.put("1", iate4); - - mismatchResult = dbAudit.compareEntries(myEntries, theirEntries); - - /* - * Assert that there is 2 mismatches - */ - assertEquals(2, mismatchResult.size()); - } - } - - logger.info("testCompareAllHashEntities: Exit"); - } - - /* - * Tests that comparison algorithm works for each entity in the database - */ - @Ignore - @Test - public void testCompareAllDbEntities() throws Exception { - logger.info("testCompareAllDbEntities: Entering"); - - logger.info("Setting up DB"); - - IntegrityAudit.isUnitTesting = true; - - properties = new Properties(); - properties.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER); - properties.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL); - properties.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER); - properties.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD); - properties.put(IntegrityAuditProperties.SITE_NAME, "SiteA"); - properties.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml"); - - Properties properties2 = new Properties(); - properties2.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER); - properties2.put(IntegrityAuditProperties.DB_URL, "jdbc:h2:file:./sql/iaTest2"); - properties2.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER); - properties2.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD); - properties2.put(IntegrityAuditProperties.SITE_NAME, "SiteA"); - properties2.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml"); - - dbDriver = IntegrityAuditProperties.DEFAULT_DB_DRIVER; - dbUrl = IntegrityAuditProperties.DEFAULT_DB_URL; - dbUser = IntegrityAuditProperties.DEFAULT_DB_USER; - dbPwd = IntegrityAuditProperties.DEFAULT_DB_PWD; - siteName = "SiteA"; - nodeType = "pdp_xacml"; - persistenceUnit = "testPU"; - resourceName = "pdp1"; - - //Clean up the two DBs - EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit, properties); - EntityManagerFactory emf2 = Persistence.createEntityManagerFactory(persistenceUnit, properties2); - - EntityManager em = emf.createEntityManager(); - EntityManager em2 = emf2.createEntityManager(); - // Start a transaction - EntityTransaction et = em.getTransaction(); - EntityTransaction et2 = em2.getTransaction(); - - /* - * Delete entries in first DB - */ - et.begin(); - - // Clean the IntegrityAuditEntity table - em.createQuery("Delete from IntegrityAuditEntity").executeUpdate(); - - // commit transaction - et.commit(); - - et.begin(); - - // Clean the IaTestEntity table - em.createQuery("Delete from IaTestEntity").executeUpdate(); - - // commit transaction - et.commit(); - em.close(); - - /* - * Delete entries in second DB - */ - et2.begin(); - - // Clean the IntegrityAuditEntity table - em2.createQuery("Delete from IntegrityAuditEntity").executeUpdate(); - - // commit transaction - et2.commit(); - - et2.begin(); - - // Clean the IaTestEntity table - em2.createQuery("Delete from IaTestEntity").executeUpdate(); - - // commit transaction - et2.commit(); - em2.close(); - logger.info("Exiting set up"); - - // Add entries into DB1 - dbDAO = new DbDAO(resourceName, persistenceUnit, properties); - new DbDAO("pdp2", persistenceUnit, properties); - DbAudit dbAudit = new DbAudit(dbDAO); - - // Add entries into DB2 - DbDAO dbDAO3 = new DbDAO(resourceName, persistenceUnit, properties2); - new DbDAO("pdp2", persistenceUnit, properties2); - - // Pull all entries and compare - HashSet classNameSet = dbDAO.getPersistenceClassNames(); - HashMap myEntries; - HashMap theirEntries; - HashSet mismatchResult = new HashSet(); - String className; - for(String c : classNameSet) { - className = c; - logger.info("classNameSet entry = " + c); - myEntries = dbDAO.getAllEntries(persistenceUnit, properties, className); - theirEntries = dbDAO3.getAllEntries(persistenceUnit, properties2, className); - mismatchResult = dbAudit.compareEntries(myEntries, theirEntries); - if(className.contains("IntegrityAuditEntity")){ - break; - } - } - - // Assert that there is 2 mismatches between IntegrityAuditEntity tables - assertEquals(2, mismatchResult.size()); - - logger.info("testCompareAllDbEntities: Exit"); - } - - /* - * Tests that differences in embedded classes are still caught - */ - @Ignore - @Test - public void testEmbeddedClass() throws Exception { - logger.info("testEmbeddedClasses: Entering"); - - dbDAO = new DbDAO(resourceName, persistenceUnit, properties); - DbAudit dbAudit = new DbAudit(dbDAO); - - String className = null; - //There is only one entry IntegrityAuditEntity, but we will check anyway - HashSet classNameSet = dbDAO.getPersistenceClassNames(); - for(String c : classNameSet){ - if (c.equals("org.openecomp.policy.common.ia.test.jpa.IaTestEntity")){ - className = c; - } - } - - IaTestEntity iate = new IaTestEntity(); - IaTestEntity iate2 = new IaTestEntity(); - - Date date = new Date(); - - PersonTest person = new PersonTest("Ford", "Prefect", 21); - PersonTest person2 = new PersonTest("Zaphod", "Beeblebrox", 25); - - /* - * Two entries, 1 mismatch - */ - iate.setCreatedBy("Ford"); - iate.setModifiedBy("Zaphod"); - iate.setModifiedDate(date); - iate.setPersonTest(person); - - iate2.setCreatedBy("Ford"); - iate2.setModifiedBy("Zaphod"); - iate2.setModifiedDate(date); - iate2.setPersonTest(person2); - - dbAudit.writeAuditDebugLog(className, "resource1", "resource2", iate, iate2); - - HashMap myEntries = new HashMap(); - HashMap theirEntries = new HashMap(); - - myEntries.put("0", iate); - theirEntries.put("0", iate2); - - HashSet result = dbAudit.compareEntries(myEntries, theirEntries); - - /* - * Assert that there are no mismatches returned - */ - assertTrue(!result.isEmpty()); - - logger.info("testEmbeddedClasses: Exit"); - } -} diff --git a/integrity-audit/src/test/java/org/openecomp/policy/common/ia/test/DbAuditTest.java b/integrity-audit/src/test/java/org/openecomp/policy/common/ia/test/DbAuditTest.java deleted file mode 100644 index 82ea8669..00000000 --- a/integrity-audit/src/test/java/org/openecomp/policy/common/ia/test/DbAuditTest.java +++ /dev/null @@ -1,753 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Integrity Audit - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.openecomp.policy.common.ia.test; - -import static org.junit.Assert.*; - -import java.io.BufferedReader; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.InputStreamReader; -import java.util.Date; -import java.util.List; -import java.util.Properties; - -import javax.persistence.EntityManager; -import javax.persistence.EntityManagerFactory; -import javax.persistence.EntityTransaction; -import javax.persistence.Persistence; -import javax.persistence.Query; - - -//import org.apache.commons.logging.Log; -//import org.apache.commons.logging.LogFactory; -import org.junit.After; -import org.junit.Before; -import org.junit.Ignore; -import org.junit.Test; - -import org.openecomp.policy.common.ia.DbAudit; -import org.openecomp.policy.common.ia.DbAuditException; -import org.openecomp.policy.common.ia.DbDAO; -import org.openecomp.policy.common.ia.DbDaoTransactionException; -import org.openecomp.policy.common.ia.IntegrityAudit; -import org.openecomp.policy.common.ia.IntegrityAuditProperties; -import org.openecomp.policy.common.ia.jpa.IntegrityAuditEntity; -import org.openecomp.policy.common.logging.flexlogger.FlexLogger; -import org.openecomp.policy.common.logging.flexlogger.Logger; - -/* - * All JUnits are designed to run in the local development environment - * where they have write privileges and can execute time-sensitive - * tasks. - */ -public class DbAuditTest { - - private static Logger logger = FlexLogger.getLogger(DbAuditTest.class); - - private DbDAO dbDAO; - private static String persistenceUnit; - private static Properties properties; - private static String resourceName; - private String dbDriver; - private String dbUrl; - private String dbUser; - private String dbPwd; - private String siteName; - private String nodeType; - private static final String TEST_LOG = "./testingLogs/common-modules/integrity-audit/debug.log"; - private static final String ERROR_LOG = "./testingLogs/common-modules/integrity-audit/error.log"; - - public void cleanLog() throws Exception{ - - logger.debug("cleanLog: enter"); - //FileOutputStream fstream = new FileOutputStream("IntegrityAudit.log"); - FileOutputStream fstream = new FileOutputStream(TEST_LOG); - fstream.close(); - fstream = new FileOutputStream(ERROR_LOG); - fstream.close(); - logger.debug("cleanLog: exit"); - } - - public void cleanDb(String persistenceUnit, Properties properties){ - logger.debug("cleanDb: enter"); - - EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit, properties); - - EntityManager em = emf.createEntityManager(); - // Start a transaction - EntityTransaction et = em.getTransaction(); - - et.begin(); - - // Clean up the DB - em.createQuery("Delete from IntegrityAuditEntity").executeUpdate(); - - // commit transaction - et.commit(); - em.close(); - logger.debug("cleanDb: exit"); - } - - - @Before - public void setUp() throws Exception { - - logger.info("setUp: Entering"); - - IntegrityAudit.isUnitTesting = true; - IntegrityAuditEntity.isUnitTesting = true; - - properties = new Properties(); - properties.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER); - properties.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL); - properties.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER); - properties.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD); - properties.put(IntegrityAuditProperties.SITE_NAME, "SiteA"); - properties.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml"); - - dbDriver = IntegrityAuditProperties.DEFAULT_DB_DRIVER; - dbUrl = IntegrityAuditProperties.DEFAULT_DB_URL; - dbUser = IntegrityAuditProperties.DEFAULT_DB_USER; - dbPwd = IntegrityAuditProperties.DEFAULT_DB_PWD; - siteName = "SiteA"; - nodeType = "pdp_xacml"; - persistenceUnit = "testPU"; - resourceName = "pdp1"; - - logger.info("setUp: Exiting"); - - } - - @After - public void tearDown() throws Exception { - - logger.info("tearDown: Entering"); - - //cleanDb(persistenceUnit, properties); - - logger.info("tearDown: Exiting"); - } - - @Ignore - @Test - public void runAllTests() throws Exception{ - //The order is important - I haven't figured out why, but it is. - mismatchTest(); - noEntitiesTest(); - oneEntityTest(); - } - - - /* - * Tests printing an error to the log in the event where - * there are no entities saved in the database - */ - public void noEntitiesTest() throws Exception { - cleanLog(); - cleanDb(persistenceUnit, properties); - - logger.info("noEntitiesTest: Entering"); - - // Boolean to assert there are no entries found - Boolean noEntities = false; - - dbDAO = new DbDAO(resourceName, persistenceUnit, properties); - dbDAO.deleteAllIntegrityAuditEntities(); - try { - DbAudit dbAudit = new DbAudit(dbDAO); - dbAudit.dbAudit(resourceName, persistenceUnit, nodeType); - } - catch (DbAuditException e) { - noEntities = true; - } - - dbDAO.deleteAllIntegrityAuditEntities(); - - logger.info("noEntitiesTest: No entities are persisted in the database"); - - // Assert there are no entities retrieved - assertTrue(noEntities); - - logger.info("noEntitiesTest: Exit"); - } - - /* - * Tests the detection of only one entry in the database - */ - public void oneEntityTest() throws Exception{ - cleanLog(); - cleanDb(persistenceUnit, properties); - - logger.info("oneEntityTest: Entering"); - - // Add one entry in the database - dbDAO = new DbDAO(resourceName, persistenceUnit, properties); - DbAudit dbAudit = new DbAudit(dbDAO); - dbAudit.dbAudit(resourceName, persistenceUnit, nodeType); - - List iaeList = dbDAO.getIntegrityAuditEntities(persistenceUnit, nodeType); - logger.info("List size: " + iaeList.size()); - - //FileInputStream fstream = new FileInputStream("IntegrityAudit.log"); - FileInputStream fstream = new FileInputStream(TEST_LOG); - BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); - String strLine; - Boolean oneEntity = false; - while ((strLine = br.readLine()) != null) { - //parse strLine to obtain what you want - - if (strLine.contains("DbAudit: Found only one IntegrityAuditEntity entry:")) { - oneEntity = true; - } - - } - if(oneEntity){ - logger.info("oneEntityTest: One entity is persisted in the database"); - }else{ - logger.info("oneEntityTest: No entities are persisted in the database"); - } - - - // Assert there is only one entry - assertTrue(oneEntity); - - br.close(); - - logger.info("oneEntityTest: Exit"); - } - - /* - * Tests reporting mismatches and misentries using the error log - */ - @SuppressWarnings("unused") - public void mismatchTest() throws Exception{ - cleanLog(); - logger.info("mismatchTest: Entering"); - - // Properties for DB2 - Properties properties2 = new Properties(); - properties2.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER); - properties2.put(IntegrityAuditProperties.DB_URL, "jdbc:h2:file:./sql/iaTest2"); - properties2.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER); - properties2.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD); - properties2.put(IntegrityAuditProperties.SITE_NAME, "SiteA"); - properties2.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml"); - - //Clean the DBs before we begin - cleanDb(persistenceUnit, properties); - cleanDb(persistenceUnit, properties2); - - // Entries in DB1 - dbDAO = new DbDAO(resourceName, persistenceUnit, properties); - DbDAO dbDAO2 = new DbDAO("pdp2", persistenceUnit, properties); - - /* - * dbDAO3 is a mismatch entry, dbDAO7 is a misentry - */ - DbDAO dbDAO3 = new DbDAO("pdp3", persistenceUnit, properties); - DbDAO dbDAO7 = new DbDAO("pdp4", persistenceUnit, properties); - Date date = new Date(); - - EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit, properties); - - /* - * Update DB url's in DB1 to point to DB2 - */ - try{ - EntityManager em = emf.createEntityManager(); - // Start a transaction - EntityTransaction et = em.getTransaction(); - - et.begin(); - - // if IntegrityAuditEntity entry exists for resourceName and PU, update it. If not found, create a new entry - Query iaequery = em.createQuery("Select i from IntegrityAuditEntity i where i.resourceName=:rn and i.persistenceUnit=:pu"); - iaequery.setParameter("rn", "pdp2"); - iaequery.setParameter("pu", persistenceUnit); - - @SuppressWarnings("rawtypes") - List iaeList = iaequery.getResultList(); - IntegrityAuditEntity iae = null; - - //If it already exists, we just want to update the properties and lastUpdated date - if(!iaeList.isEmpty()){ - //ignores multiple results - iae = (IntegrityAuditEntity) iaeList.get(0); - // refresh the object from DB in case cached data was returned - em.refresh(iae); - logger.info("Resource: " + "pdp2" + " with PersistenceUnit: " + persistenceUnit - + " exists and entry be updated"); - }else{ - // If it does not exist, we also must add teh resourceName, persistenceUnit and designated values - logger.info("Adding resource " + "pdp2" + " with PersistenceUnit: " + persistenceUnit - + " to IntegrityAuditEntity table"); - iae = new IntegrityAuditEntity(); - iae.setResourceName("pdp2"); - iae.setPersistenceUnit(persistenceUnit); - iae.setDesignated(false); - } - - //update/set properties in entry - iae.setSite(siteName); - iae.setNodeType(nodeType); - iae.setLastUpdated(date); - iae.setCreatedDate(date); - iae.setJdbcDriver(dbDriver); - iae.setJdbcPassword(dbPwd); - iae.setJdbcUrl("jdbc:h2:file:./sql/iaTest2"); - iae.setJdbcUser(dbUser); - - em.persist(iae); - // flush to the DB - em.flush(); - - // commit transaction - et.commit(); - - et.begin(); - - // if IntegrityAuditEntity entry exists for resourceName and PU, update it. If not found, create a new entry - iaequery = em.createQuery("Select i from IntegrityAuditEntity i where i.resourceName=:rn and i.persistenceUnit=:pu"); - iaequery.setParameter("rn", "pdp1"); - iaequery.setParameter("pu", persistenceUnit); - - @SuppressWarnings("rawtypes") - List iaeList2 = iaequery.getResultList(); - iae = null; - - //If it already exists, we just want to update the properties and lastUpdated date - if(!iaeList2.isEmpty()){ - //ignores multiple results - iae = (IntegrityAuditEntity) iaeList2.get(0); - // refresh the object from DB in case cached data was returned - em.refresh(iae); - logger.info("Resource: " + "pdp1" + " with PersistenceUnit: " + persistenceUnit - + " exists and entry be updated"); - }else{ - // If it does not exist, we also must add teh resourceName, persistenceUnit and designated values - logger.info("Adding resource " + "pdp1" + " with PersistenceUnit: " + persistenceUnit - + " to IntegrityAuditEntity table"); - iae = new IntegrityAuditEntity(); - iae.setResourceName("pdp1"); - iae.setPersistenceUnit(persistenceUnit); - iae.setDesignated(false); - } - - //update/set properties in entry - iae.setSite(siteName); - iae.setNodeType(nodeType); - iae.setLastUpdated(date); - iae.setCreatedDate(date); - iae.setJdbcDriver(dbDriver); - iae.setJdbcPassword(dbPwd); - iae.setJdbcUrl("jdbc:h2:file:./sql/iaTest2"); - iae.setJdbcUser(dbUser); - - em.persist(iae); - // flush to the DB - em.flush(); - - // commit transaction - et.commit(); - - et.begin(); - - // if IntegrityAuditEntity entry exists for resourceName and PU, update it. If not found, create a new entry - iaequery = em.createQuery("Select i from IntegrityAuditEntity i where i.resourceName=:rn and i.persistenceUnit=:pu"); - iaequery.setParameter("rn", "pdp3"); - iaequery.setParameter("pu", persistenceUnit); - - @SuppressWarnings("rawtypes") - List iaeList3 = iaequery.getResultList(); - iae = null; - - //If it already exists, we just want to update the properties and lastUpdated date - if(!iaeList3.isEmpty()){ - //ignores multiple results - iae = (IntegrityAuditEntity) iaeList3.get(0); - // refresh the object from DB in case cached data was returned - em.refresh(iae); - logger.info("Resource: " + "pdp3" + " with PersistenceUnit: " + persistenceUnit - + " exists and entry be updated"); - }else{ - // If it does not exist, we also must add the resourceName, persistenceUnit and designated values - logger.info("Adding resource " + "pdp3" + " with PersistenceUnit: " + persistenceUnit - + " to IntegrityAuditEntity table"); - iae = new IntegrityAuditEntity(); - iae.setResourceName("pdp3"); - iae.setPersistenceUnit(persistenceUnit); - iae.setDesignated(false); - } - - //update/set properties in entry - iae.setSite(siteName); - iae.setNodeType(nodeType); - iae.setLastUpdated(date); - iae.setCreatedDate(date); - iae.setJdbcDriver(dbDriver); - iae.setJdbcPassword(dbPwd); - iae.setJdbcUrl(dbUrl); - iae.setJdbcUser(dbUser); - - em.persist(iae); - // flush to the DB - em.flush(); - - // commit transaction - et.commit(); - - et.begin(); - - // if IntegrityAuditEntity entry exists for resourceName and PU, update it. If not found, create a new entry - iaequery = em.createQuery("Select i from IntegrityAuditEntity i where i.resourceName=:rn and i.persistenceUnit=:pu"); - iaequery.setParameter("rn", "pdp4"); - iaequery.setParameter("pu", persistenceUnit); - - @SuppressWarnings("rawtypes") - List iaeList4 = iaequery.getResultList(); - iae = null; - - //If it already exists, we just want to update the properties and lastUpdated date - if(!iaeList4.isEmpty()){ - //ignores multiple results - iae = (IntegrityAuditEntity) iaeList4.get(0); - // refresh the object from DB in case cached data was returned - em.refresh(iae); - logger.info("Resource: " + "pdp4" + " with PersistenceUnit: " + persistenceUnit - + " exists and entry be updated"); - }else{ - // If it does not exist, we also must add the resourceName, persistenceUnit and designated values - logger.info("Adding resource " + "pdp4" + " with PersistenceUnit: " + persistenceUnit - + " to IntegrityAuditEntity table"); - iae = new IntegrityAuditEntity(); - iae.setResourceName("pdp4"); - iae.setPersistenceUnit(persistenceUnit); - iae.setDesignated(false); - } - - //update/set properties in entry - iae.setSite(siteName); - iae.setNodeType(nodeType); - iae.setLastUpdated(date); - iae.setCreatedDate(date); - iae.setJdbcDriver(dbDriver); - iae.setJdbcPassword(dbPwd); - iae.setJdbcUrl("jdbc:h2:file:./sql/iaTest2"); - iae.setJdbcUser(dbUser); - - em.persist(iae); - // flush to the DB - em.flush(); - - // commit transaction - et.commit(); - - em.close(); - }catch (Exception e){ - String msg = "DbDAO: " + "register() " + "ecountered a problem in execution: "; - logger.error(msg + e); - throw new DbDaoTransactionException(e); - } - - /* - * Identical entries in from DB1 in DB2 except for dbDAO6 - */ - emf = Persistence.createEntityManagerFactory(persistenceUnit, properties2); - DbDAO dbDAO4 = new DbDAO(resourceName, persistenceUnit, properties2); - - DbDAO dbDAO5 = new DbDAO("pdp2", persistenceUnit, properties2); - - /* - * This is the mismatch entry - */ - DbDAO dbDAO6 = new DbDAO("pdp3", persistenceUnit, properties2); - try{ - EntityManager em = emf.createEntityManager(); - // Start a transaction - EntityTransaction et = em.getTransaction(); - - et.begin(); - - // if IntegrityAuditEntity entry exists for resourceName and PU, update it. If not found, create a new entry - Query iaequery = em.createQuery("Select i from IntegrityAuditEntity i where i.resourceName=:rn and i.persistenceUnit=:pu"); - iaequery.setParameter("rn", "pdp2"); - iaequery.setParameter("pu", persistenceUnit); - - @SuppressWarnings("rawtypes") - List iaeList = iaequery.getResultList(); - IntegrityAuditEntity iae = null; - - //If it already exists, we just want to update the properties and lastUpdated date - if(!iaeList.isEmpty()){ - //ignores multiple results - iae = (IntegrityAuditEntity) iaeList.get(0); - // refresh the object from DB in case cached data was returned - em.refresh(iae); - logger.info("Resource: " + "pdp2" + " with PersistenceUnit: " + persistenceUnit - + " exists and entry be updated"); - }else{ - // If it does not exist, we also must add teh resourceName, persistenceUnit and designated values - logger.info("Adding resource " + "pdp2" + " with PersistenceUnit: " + persistenceUnit - + " to IntegrityAuditEntity table"); - iae = new IntegrityAuditEntity(); - iae.setResourceName("pdp2"); - iae.setPersistenceUnit(persistenceUnit); - iae.setDesignated(false); - } - - //update/set properties in entry - iae.setSite(siteName); - iae.setNodeType(nodeType); - iae.setLastUpdated(date); - iae.setCreatedDate(date); - iae.setJdbcDriver(dbDriver); - iae.setJdbcPassword(dbPwd); - iae.setJdbcUrl("jdbc:h2:file:./sql/iaTest2"); - iae.setJdbcUser(dbUser); - - em.persist(iae); - // flush to the DB - em.flush(); - - // commit transaction - et.commit(); - - et.begin(); - - // if IntegrityAuditEntity entry exists for resourceName and PU, update it. If not found, create a new entry - iaequery = em.createQuery("Select i from IntegrityAuditEntity i where i.resourceName=:rn and i.persistenceUnit=:pu"); - iaequery.setParameter("rn", "pdp1"); - iaequery.setParameter("pu", persistenceUnit); - - @SuppressWarnings("rawtypes") - List iaeList2 = iaequery.getResultList(); - iae = null; - - //If it already exists, we just want to update the properties and lastUpdated date - if(!iaeList2.isEmpty()){ - //ignores multiple results - iae = (IntegrityAuditEntity) iaeList2.get(0); - // refresh the object from DB in case cached data was returned - em.refresh(iae); - logger.info("Resource: " + "pdp1" + " with PersistenceUnit: " + persistenceUnit - + " exists and entry be updated"); - }else{ - // If it does not exist, we also must add teh resourceName, persistenceUnit and designated values - logger.info("Adding resource " + "pdp1" + " with PersistenceUnit: " + persistenceUnit - + " to IntegrityAuditEntity table"); - iae = new IntegrityAuditEntity(); - iae.setResourceName("pdp1"); - iae.setPersistenceUnit(persistenceUnit); - iae.setDesignated(false); - } - - //update/set properties in entry - iae.setSite(siteName); - iae.setNodeType(nodeType); - iae.setLastUpdated(date); - iae.setCreatedDate(date); - iae.setJdbcDriver(dbDriver); - iae.setJdbcPassword(dbPwd); - iae.setJdbcUrl("jdbc:h2:file:./sql/iaTest2"); - iae.setJdbcUser(dbUser); - - em.persist(iae); - // flush to the DB - em.flush(); - - // commit transaction - et.commit(); - - et.begin(); - - // if IntegrityAuditEntity entry exists for resourceName and PU, update it. If not found, create a new entry - iaequery = em.createQuery("Select i from IntegrityAuditEntity i where i.resourceName=:rn and i.persistenceUnit=:pu"); - iaequery.setParameter("rn", "pdp3"); - iaequery.setParameter("pu", persistenceUnit); - - @SuppressWarnings("rawtypes") - List iaeList3 = iaequery.getResultList(); - iae = null; - - //If it already exists, we just want to update the properties and lastUpdated date - if(!iaeList3.isEmpty()){ - //ignores multiple results - iae = (IntegrityAuditEntity) iaeList3.get(0); - // refresh the object from DB in case cached data was returned - em.refresh(iae); - logger.info("Resource: " + "pdp3" + " with PersistenceUnit: " + persistenceUnit - + " exists and entry be updated"); - }else{ - // If it does not exist, we also must add teh resourceName, persistenceUnit and designated values - logger.info("Adding resource " + "pdp3" + " with PersistenceUnit: " + persistenceUnit - + " to IntegrityAuditEntity table"); - iae = new IntegrityAuditEntity(); - iae.setResourceName("pdp3"); - iae.setPersistenceUnit(persistenceUnit); - iae.setDesignated(false); - } - - //update/set properties in entry - iae.setSite(siteName); - iae.setNodeType(nodeType); - iae.setLastUpdated(date); - iae.setCreatedDate(date); - iae.setJdbcDriver(dbDriver); - iae.setJdbcPassword(dbPwd); - iae.setJdbcUrl("jdbc:h2:file:./sql/iaTest2"); - iae.setJdbcUser(dbUser); - - em.persist(iae); - // flush to the DB - em.flush(); - - // commit transaction - et.commit(); - - em.close(); - }catch (Exception e){ - String msg = "DbDAO: " + "register() " + "ecountered a problem in execution: "; - logger.error(msg + e); - throw new DbDaoTransactionException(e); - - } - - /* - * Run the DB Audit, once it finds a mismatch and sleeps, update DB1 - * to have the same entry as DB2 it can be confirmed that the mismatch - * is resolved - */ - DbAudit dbAudit = new DbAudit(dbDAO); - dbAudit.dbAudit(resourceName, persistenceUnit, nodeType); - emf = Persistence.createEntityManagerFactory(persistenceUnit, properties); - try{ - EntityManager em = emf.createEntityManager(); - // Start a transaction - EntityTransaction et = em.getTransaction(); - - et.begin(); - - // if IntegrityAuditEntity entry exists for resourceName and PU, update it. If not found, create a new entry - Query iaequery = em.createQuery("Select i from IntegrityAuditEntity i where i.resourceName=:rn and i.persistenceUnit=:pu"); - iaequery.setParameter("rn", "pdp3"); - iaequery.setParameter("pu", persistenceUnit); - - @SuppressWarnings("rawtypes") - List iaeList = iaequery.getResultList(); - IntegrityAuditEntity iae = null; - - //If it already exists, we just want to update the properties and lastUpdated date - if(!iaeList.isEmpty()){ - //ignores multiple results - iae = (IntegrityAuditEntity) iaeList.get(0); - // refresh the object from DB in case cached data was returned - em.refresh(iae); - logger.info("Resource: " + "pdp3" + " with PersistenceUnit: " + persistenceUnit - + " exists and entry be updated"); - }else{ - // If it does not exist, we also must add the resourceName, persistenceUnit and designated values - logger.info("Adding resource " + "pdp3" + " with PersistenceUnit: " + persistenceUnit - + " to IntegrityAuditEntity table"); - iae = new IntegrityAuditEntity(); - iae.setResourceName("pdp3"); - iae.setPersistenceUnit(persistenceUnit); - iae.setDesignated(false); - } - - //update/set properties in entry - iae.setSite(siteName); - iae.setNodeType(nodeType); - iae.setLastUpdated(date); - iae.setCreatedDate(date); - iae.setJdbcDriver(dbDriver); - iae.setJdbcPassword(dbPwd); - iae.setJdbcUrl("jdbc:h2:file:./sql/iaTest2"); - iae.setJdbcUser(dbUser); - - em.persist(iae); - // flush to the DB - em.flush(); - - // commit transaction - et.commit(); - em.close(); - }catch (Exception e){ - String msg = "DbDAO: " + "register() " + "ecountered a problem in execution: "; - logger.error(msg + e); - throw new DbDaoTransactionException(e); - } - - /* - * Run the audit again and correct the mismatch, the result should be one - * entry in the mismatchKeySet because of the misentry from the beginning - * of the test - */ - dbAudit.dbAudit(resourceName, persistenceUnit, nodeType); - - //Cleanup DB2 - cleanDb(persistenceUnit, properties2); - - FileInputStream fstream = new FileInputStream(TEST_LOG); - BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); - String strLine; - int startIndex; - String mismatchIndex = ""; - while ((strLine = br.readLine()) != null) { - //parse strLine to obtain what you want...retrieve the last entry - - if (strLine.contains("Mismatched entries (keys):")) { - startIndex = strLine.indexOf("(keys):") + 8; - mismatchIndex = strLine.substring(startIndex); - } - } - int mismatchEntries = mismatchIndex.trim().split(",").length; - logger.info("mismatchTest: mismatchIndex found: '" + mismatchIndex + "'" - + " mismatachEntries = " + mismatchEntries); - - // Assert there is only one entry index - assertEquals(1, mismatchEntries); - - br.close(); - - //Now check the entry in the error.log - fstream = new FileInputStream(ERROR_LOG); - br = new BufferedReader(new InputStreamReader(fstream)); - String mismatchNum = ""; - while ((strLine = br.readLine()) != null) { - //parse strLine to obtain what you want...retrieve the last entry - - if (strLine.contains("DB Audit:")) { - startIndex = strLine.indexOf("DB Audit:") + 10; - mismatchNum = strLine.substring(startIndex, startIndex+1); - } - } - logger.info("mismatchTest: mismatchNum found: '" + mismatchNum + "'"); - - // Assert that there are a total of 3 mismatches - 1 between each comparison node. - assertEquals("3", mismatchNum); - - br.close(); - - logger.info("mismatchTest: Exit"); - } - -} diff --git a/integrity-audit/src/test/java/org/openecomp/policy/common/ia/test/DbDAOTest.java b/integrity-audit/src/test/java/org/openecomp/policy/common/ia/test/DbDAOTest.java deleted file mode 100644 index 3bd847c2..00000000 --- a/integrity-audit/src/test/java/org/openecomp/policy/common/ia/test/DbDAOTest.java +++ /dev/null @@ -1,735 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Integrity Audit - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.openecomp.policy.common.ia.test; - -import static org.junit.Assert.*; - -/* - * All JUnits are designed to run in the local development environment - * where they have write privileges and can execute time-sensitive - * tasks. - */ -import java.util.Date; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Properties; - -import javax.persistence.EntityManager; -import javax.persistence.EntityManagerFactory; -import javax.persistence.EntityTransaction; -import javax.persistence.Persistence; -import javax.persistence.PersistenceUnitUtil; -import javax.persistence.Query; -import javax.persistence.TypedQuery; -import javax.persistence.criteria.CriteriaBuilder; -import javax.persistence.criteria.CriteriaQuery; -import javax.persistence.criteria.Root; - -import org.junit.After; -import org.junit.Before; -import org.junit.Ignore; -import org.junit.Test; - -import org.openecomp.policy.common.ia.DbDAO; -import org.openecomp.policy.common.ia.DbDaoTransactionException; -import org.openecomp.policy.common.ia.IntegrityAuditProperties; -import org.openecomp.policy.common.ia.jpa.IntegrityAuditEntity; - -/* - * All JUnits are designed to run in the local development environment - * where they have write privileges and can execute time-sensitive - * tasks. - */ -public class DbDAOTest { - private static String persistenceUnit; - private static Properties properties; - private static String resourceName; - - DbDAO d; - - @Before - public void setUp() throws Exception { - properties = new Properties(); - properties.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER); - properties.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL); - properties.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER); - properties.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD); - properties.put(IntegrityAuditProperties.SITE_NAME, "SiteA"); - properties.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml"); - - persistenceUnit = "integrityAuditPU"; - resourceName = "pdp0"; - } - - @After - public void tearDown() throws Exception { - } - - /* Tests registering a new IntegrityAuditEntity object in the DB */ - @Ignore - @Test - public void testNewRegistration() { - try { - EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit, properties); - EntityManager em = emf.createEntityManager(); - - // Start a transaction - EntityTransaction et = em.getTransaction(); - - // Begin Transaction - et.begin(); - - // Clean the DB - em.createQuery("DELETE FROM IntegrityAuditEntity").executeUpdate(); - - // flush to the DB - em.flush(); - et.commit(); - - et.begin(); - d = new DbDAO(resourceName, persistenceUnit, properties); - - // Find the proper entry in the database - Query iaequery = em.createQuery("Select i from IntegrityAuditEntity i where i.resourceName=:rn and i.persistenceUnit=:pu"); - iaequery.setParameter("rn", DbDAOTest.resourceName); - iaequery.setParameter("pu", DbDAOTest.persistenceUnit); - - @SuppressWarnings("rawtypes") - List iaeList = iaequery.getResultList(); - - // Assert that the IntegrityAuditEntity object was found - assertNotNull(iaeList); - - // flush to the DB - em.flush(); - et.commit(); - em.close(); - - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - - /* Tests updating an IntegrityAuditEntity if it has already been registered */ - @Ignore - @Test - public void testUpdateRegistration() { - EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit, properties); - EntityManager em = emf.createEntityManager(); - - // Start a transaction - EntityTransaction et = em.getTransaction(); - - // Begin transaction - et.begin(); - - // Clean the DB - em.createQuery("DELETE FROM IntegrityAuditEntity").executeUpdate(); - - // flush to the DB - em.flush(); - et.commit(); - - // close the EntityManager - em.close(); - - try { - d = new DbDAO(resourceName, persistenceUnit, properties); - - // Change site_name in properties to test that an update was made to an existing entry in the table - properties.put(IntegrityAuditProperties.SITE_NAME, "SiteB"); - d = new DbDAO(resourceName, persistenceUnit, properties); - - em = emf.createEntityManager(); - - // Start a transaction - et = em.getTransaction(); - - // Begin Transaction - et.begin(); - - // Find the proper entry in the database - Query iaequery = em.createQuery("Select i from IntegrityAuditEntity i where i.resourceName=:rn and i.persistenceUnit=:pu"); - iaequery.setParameter("rn", DbDAOTest.resourceName); - iaequery.setParameter("pu", DbDAOTest.persistenceUnit); - - @SuppressWarnings("rawtypes") - List iaeList = iaequery.getResultList(); - IntegrityAuditEntity iae = null; - if(!iaeList.isEmpty()) { - //ignores multiple results - iae = (IntegrityAuditEntity) iaeList.get(0); - - em.refresh(iae); - em.persist(iae); - - // flush to the DB - em.flush(); - - // commit transaction - et.commit(); - - // close the EntityManager - em.close(); - - // Assert that the site_name for the existing entry was updated - assertEquals("SiteB", iae.getSite()); - } - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - - /* Tests obtaining all Integrity Audit Entities from a table */ - @Ignore - @Test - public void testGetIntegrityAuditEntities() { - EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit, properties); - EntityManager em = emf.createEntityManager(); - - // Start a transaction - EntityTransaction et = em.getTransaction(); - - et.begin(); - - // Clean the DB - em.createQuery("DELETE FROM IntegrityAuditEntity").executeUpdate(); - - // flush to the DB - em.flush(); - - // close the transaction - et.commit(); - - // close the EntityManager - em.close(); - - try { - // Add some entries to the DB - d = new DbDAO(resourceName, persistenceUnit, properties); - new DbDAO("pdp1", persistenceUnit, properties); - properties.put(IntegrityAuditProperties.NODE_TYPE, "pdp_drools"); - new DbDAO("pdp2", persistenceUnit, properties); - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - List entities; - try { - // Obtain entries based on persistenceUnit and nodeType - entities = d.getIntegrityAuditEntities(persistenceUnit, "pdp_xacml"); - assertEquals(2, entities.size()); - } catch (DbDaoTransactionException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - - /* Tests retrieving a DbDAO instance's IntegrityAuditEntity */ - @Ignore - @Test - public void testGetMyIntegrityAuditEntity() { - try { - d = new DbDAO(resourceName, persistenceUnit, properties); - IntegrityAuditEntity iae = d.getMyIntegrityAuditEntity(); - assertEquals("integrityAuditPU", iae.getPersistenceUnit()); - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - - /* Tests obtaining an IntegrityAuditEntity by ID */ - @Ignore - @Test - public void testGetIntegrityAuditEntity() { - EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit, properties); - EntityManager em = emf.createEntityManager(); - - // Start a transaction - EntityTransaction et = em.getTransaction(); - - // Begin transaction - et.begin(); - - // Clean the DB - em.createQuery("DELETE FROM IntegrityAuditEntity").executeUpdate(); - - // flush to the DB - em.flush(); - - // close the transaction - et.commit(); - - try { - // Obtain an entry from the database based on ID - d = new DbDAO(resourceName, persistenceUnit, properties); - - et.begin(); - - // Find the proper database entry - Query iaequery = em.createQuery("Select i from IntegrityAuditEntity i where i.resourceName=:rn and i.persistenceUnit=:pu"); - iaequery.setParameter("rn", DbDAOTest.resourceName); - iaequery.setParameter("pu", DbDAOTest.persistenceUnit); - - @SuppressWarnings("rawtypes") - List iaeList = iaequery.getResultList(); - IntegrityAuditEntity iae = null; - if(!iaeList.isEmpty()){ - //ignores multiple results - iae = (IntegrityAuditEntity) iaeList.get(0); - - // refresh the object from DB in case cached data was returned - em.refresh(iae); - - // Obtain ID for an IntegrityAuditEntity - PersistenceUnitUtil util = emf.getPersistenceUnitUtil(); - Object iaeId = util.getIdentifier(iae); - - // Obtain the same IntegrityAuditEntity based on ID - IntegrityAuditEntity iaeDuplicate = d.getIntegrityAuditEntity((long) iaeId); - Object duplicateId = util.getIdentifier(iaeDuplicate); - - // Assert that the proper entry was retrieved based on ID - assertEquals((long) iaeId, (long) duplicateId); - } - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - // close the EntityManager - em.close(); - } - - /* Tests setting an IntegrityAuditEntity as the designated node */ - @Ignore - @Test - public void testSetDesignated() { - try { - EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit, properties); - EntityManager em = emf.createEntityManager(); - - // Start a transaction - EntityTransaction et = em.getTransaction(); - - // Begin transaction - et.begin(); - - // Clean the DB - em.createQuery("DELETE FROM IntegrityAuditEntity").executeUpdate(); - - // flush to the DB - em.flush(); - et.commit(); - - et.begin(); - - // Create an entry and set it's designated field to true - d = new DbDAO(resourceName, persistenceUnit, properties); - d.setDesignated(resourceName, persistenceUnit, true); - - // Find the proper entry in the database - Query iaequery = em.createQuery("Select i from IntegrityAuditEntity i where i.resourceName=:rn and i.persistenceUnit=:pu"); - iaequery.setParameter("rn", resourceName); - iaequery.setParameter("pu", persistenceUnit); - - @SuppressWarnings("rawtypes") - List iaeList = iaequery.getResultList(); - IntegrityAuditEntity iae = null; - - if(!iaeList.isEmpty()){ - //ignores multiple results - iae = (IntegrityAuditEntity) iaeList.get(0); - em.refresh(iae); - - // Check if the node is designated - boolean result = iae.isDesignated(); - - // Assert that it is designated - assertTrue(result); - } - - // flush to the DB - em.flush(); - - // close the transaction - et.commit(); - - // close the EntityManager - em.close(); - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - - /* Tests that the lastUpdated column in the database is updated properly */ - @Ignore - @Test - public void testSetLastUpdated() { - try { - EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit, properties); - EntityManager em = emf.createEntityManager(); - - // Start a transaction - EntityTransaction et = em.getTransaction(); - - // Begin transaction - et.begin(); - - // Clean the DB - em.createQuery("DELETE FROM IntegrityAuditEntity").executeUpdate(); - - // flush to the DB - em.flush(); - et.commit(); - - et.begin(); - - // Create an entry - d = new DbDAO(resourceName, persistenceUnit, properties); - - // Find the proper entry in the database - Query iaequery = em.createQuery("Select i from IntegrityAuditEntity i where i.resourceName=:rn and i.persistenceUnit=:pu"); - iaequery.setParameter("rn", resourceName); - iaequery.setParameter("pu", persistenceUnit); - - @SuppressWarnings("rawtypes") - List iaeList = iaequery.getResultList(); - IntegrityAuditEntity iae = null; - - if(!iaeList.isEmpty()){ - // ignores multiple results - iae = (IntegrityAuditEntity) iaeList.get(0); - // refresh the object from DB in case cached data was returned - em.refresh(iae); - - // Obtain old update value and set new update value - Date oldDate = iae.getLastUpdated(); - iae.setSite("SiteB"); - iae.setLastUpdated(new Date()); - Date newDate = iae.getLastUpdated(); - - em.persist(iae); - // flush to the DB - em.flush(); - // close the transaction - et.commit(); - // close the EntityManager - em.close(); - - // Assert that the old and new update times are different - assertNotEquals(oldDate, newDate); - } - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - - /* Tests that all the entries from a class can be retrieved */ - @Ignore - @Test - public void testGetAllMyEntriesString() { - EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit, properties); - EntityManager em = emf.createEntityManager(); - - // Start a transaction - EntityTransaction et = em.getTransaction(); - - // Begin transaction - et.begin(); - - // Clean the DB - em.createQuery("DELETE FROM IntegrityAuditEntity").executeUpdate(); - - // flush to the DB - em.flush(); - et.commit(); - - // close the EntityManager - em.close(); - - try { - // create entries for the IntegrityAuditEntity table - d = new DbDAO(resourceName, persistenceUnit, properties); - new DbDAO("pdp1", persistenceUnit, properties); - new DbDAO("pdp2", persistenceUnit, properties); - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - try { - // Obtain a hash with the persisted objects - HashMap entries = d.getAllMyEntries("org.openecomp.policy.common.ia.jpa.IntegrityAuditEntity"); - - // Assert there were 3 entries for that class - assertEquals(3, entries.size()); - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - - /* Tests retrieving all entities in a Persistence Unit using the class name and a hashset of IDs */ - @Ignore - @Test - public void testGetAllMyEntriesStringHashSet() { - EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit, properties); - EntityManager em = emf.createEntityManager(); - - // Start a transaction - EntityTransaction et = em.getTransaction(); - - // Begin transaction - et.begin(); - - // Clean the DB - em.createQuery("DELETE FROM IntegrityAuditEntity").executeUpdate(); - - // flush to the DB - em.flush(); - et.commit(); - - try { - // create entries for the IntegrityAuditEntity table - d = new DbDAO(resourceName, persistenceUnit, properties); - new DbDAO("pdp1", persistenceUnit, properties); - new DbDAO("pdp2", persistenceUnit, properties); - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - try { - // Obtain all entity keys - CriteriaBuilder cb = em.getCriteriaBuilder(); - CriteriaQuery cq = cb.createQuery(); - Root rootEntry = cq.from(Class.forName("org.openecomp.policy.common.ia.jpa.IntegrityAuditEntity")); - CriteriaQuery all = cq.select(rootEntry); - TypedQuery allQuery = em.createQuery(all); - List objectList = allQuery.getResultList(); - HashSet resultSet = new HashSet(); - PersistenceUnitUtil util = emf.getPersistenceUnitUtil(); - for (Object o: objectList){ - Object key = util.getIdentifier(o); - resultSet.add(key); - } - - // Obtain a hash with the persisted objects - HashMap entries = d.getAllMyEntries("org.openecomp.policy.common.ia.jpa.IntegrityAuditEntity", resultSet); - - // Assert there were 3 entries for that class - assertEquals(3, entries.size()); - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - // close the EntityManager - em.close(); - } - - /* Tests retrieving all entities in a Persistence Unit using the persistence unit, properties, and class name */ - @Ignore - @Test - public void testGetAllEntriesStringPropertiesString() { - EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit, properties); - EntityManager em = emf.createEntityManager(); - - // Start a transaction - EntityTransaction et = em.getTransaction(); - - // Begin transaction - et.begin(); - - // Clean the DB - em.createQuery("DELETE FROM IntegrityAuditEntity").executeUpdate(); - - // flush to the DB - em.flush(); - et.commit(); - - // close the EntityManager - em.close(); - - try { - // create entries for the IntegrityAuditEntity table - d = new DbDAO(resourceName, persistenceUnit, properties); - new DbDAO("pdp1", persistenceUnit, properties); - new DbDAO("pdp2", persistenceUnit, properties); - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - try { - // Obtain a hash with the persisted objects - HashMap entries = d.getAllEntries("integrityAuditPU", properties, "org.openecomp.policy.common.ia.jpa.IntegrityAuditEntity"); - - // Assert there were 3 entries for that class - assertEquals(3, entries.size()); - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - - /* Tests retrieving all entities in a Persistence Unit using the persistence unit, properties, class name, and a hashset of IDs */ - @Ignore - @Test - public void testGetAllEntriesStringPropertiesStringHashSet() { - EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit, properties); - EntityManager em = emf.createEntityManager(); - - // Start a transaction - EntityTransaction et = em.getTransaction(); - - // Begin transaction - et.begin(); - - // Clean the DB - em.createQuery("DELETE FROM IntegrityAuditEntity").executeUpdate(); - - // flush to the DB - em.flush(); - et.commit(); - - try { - // create entries for the IntegrityAuditEntity table - d = new DbDAO(resourceName, persistenceUnit, properties); - new DbDAO("pdp1", persistenceUnit, properties); - new DbDAO("pdp2", persistenceUnit, properties); - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - try { - // Obtain all entity keys - CriteriaBuilder cb = em.getCriteriaBuilder(); - CriteriaQuery cq = cb.createQuery(); - Root rootEntry = cq.from(Class.forName("org.openecomp.policy.common.ia.jpa.IntegrityAuditEntity")); - CriteriaQuery all = cq.select(rootEntry); - TypedQuery allQuery = em.createQuery(all); - List objectList = allQuery.getResultList(); - HashSet resultSet = new HashSet(); - PersistenceUnitUtil util = emf.getPersistenceUnitUtil(); - for (Object o: objectList){ - Object key = util.getIdentifier(o); - resultSet.add(key); - } - - // Obtain a hash with the persisted objects - HashMap entries = d.getAllEntries("integrityAuditPU", properties, "org.openecomp.policy.common.ia.jpa.IntegrityAuditEntity", resultSet); - - // Assert there were 3 entries for that class - assertEquals(3, entries.size()); - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - // close the EntityManager - em.close(); - } - - /* Tests getting all the entries from a class based on persistenceUnit, properties, and className */ - @Ignore - @Test - public void testGetAllEntries() { - EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit, properties); - EntityManager em = emf.createEntityManager(); - - // Start a transaction - EntityTransaction et = em.getTransaction(); - - // Begin transaction - et.begin(); - - // Clean the DB - em.createQuery("DELETE FROM IntegrityAuditEntity").executeUpdate(); - - // flush to the DB - em.flush(); - et.commit(); - - // close the EntityManager - em.close(); - - try { - // create entries for the IntegrityAuditEntity table - d = new DbDAO(resourceName, persistenceUnit, properties); - new DbDAO("pdp1", persistenceUnit, properties); - new DbDAO("pdp2", persistenceUnit, properties); - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - try { - // Obtain a hash with the persisted objects - HashMap entries = d.getAllEntries(persistenceUnit, properties, "org.openecomp.policy.common.ia.jpa.IntegrityAuditEntity"); - - // Assert there were 3 entries for that class - assertEquals(3, entries.size()); - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - - /* Tests obtaining all class names of persisted classes */ - public void testGetPersistenceClassNames() { - EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit, properties); - EntityManager em = emf.createEntityManager(); - - // Start a transaction - EntityTransaction et = em.getTransaction(); - - // Begin transaction - et.begin(); - - // Clean the DB - em.createQuery("DELETE FROM IntegrityAuditEntity").executeUpdate(); - - // flush to the DB - em.flush(); - et.commit(); - - // close the EntityManager - em.close(); - - try { - d = new DbDAO(resourceName, persistenceUnit, properties); - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - // Retrieve persistence class names - HashSet result = d.getPersistenceClassNames(); - assertEquals(1, result.size()); - } -} diff --git a/integrity-audit/src/test/java/org/openecomp/policy/common/ia/test/IntegrityAuditDesignationTest.java b/integrity-audit/src/test/java/org/openecomp/policy/common/ia/test/IntegrityAuditDesignationTest.java deleted file mode 100644 index 69936bfe..00000000 --- a/integrity-audit/src/test/java/org/openecomp/policy/common/ia/test/IntegrityAuditDesignationTest.java +++ /dev/null @@ -1,1109 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Integrity Audit - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.openecomp.policy.common.ia.test; - -import static org.junit.Assert.*; - -import java.io.BufferedReader; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.InputStreamReader; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Properties; - -import javax.persistence.EntityManager; -import javax.persistence.EntityManagerFactory; -import javax.persistence.EntityTransaction; -import javax.persistence.Persistence; - - - -//import org.apache.commons.logging.Log; -//import org.apache.commons.logging.LogFactory; -import org.junit.After; -import org.junit.Before; -import org.junit.Ignore; -import org.junit.Test; - -import org.openecomp.policy.common.ia.AuditThread; -import org.openecomp.policy.common.ia.DbDAO; -import org.openecomp.policy.common.ia.IntegrityAudit; -import org.openecomp.policy.common.ia.IntegrityAuditProperties; -import org.openecomp.policy.common.logging.flexlogger.FlexLogger; -import org.openecomp.policy.common.logging.flexlogger.Logger; - -/* - * All JUnits are designed to run in the local development environment - * where they have write privileges and can execute time-sensitive - * tasks. - */ -public class IntegrityAuditDesignationTest { - - private static Logger logger = FlexLogger.getLogger(IntegrityAuditDesignationTest.class); - - /* - * Provides a little cushion for timing events. - */ - private static int FUDGE_FACTOR = 15000; - - private static String persistenceUnit; - private static Properties properties; - private static String resourceName; - private static final String TEST_LOG = "./testingLogs/common-modules/integrity-audit/debug.log"; - @Before - public void setUp() throws Exception { - - - System.out.println("setUp: Clearing debug.log"); - FileOutputStream fstream = new FileOutputStream(TEST_LOG); - fstream.close(); - - logger.info("setUp: Entering"); - - IntegrityAudit.isUnitTesting = true; - - properties = new Properties(); - properties.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER); - properties.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL); - properties.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER); - properties.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD); - properties.put(IntegrityAuditProperties.SITE_NAME, "SiteA"); - properties.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml"); - - /* - * AuditThread.AUDIT_THREAD_SLEEP_INTERVAL is also five seconds, so - * setting AUDIT_PERIOD_SECONDS to 5 ensures that whether or not audit - * has already been run on a node, it will sleep the same amount of - * time. - */ - properties.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5"); - - persistenceUnit = "testPU"; - resourceName = "pdp1"; - - - //Clean up the DB - EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit, properties); - - EntityManager em = emf.createEntityManager(); - // Start a transaction - EntityTransaction et = em.getTransaction(); - - et.begin(); - - // if IntegrityAuditEntity entry exists for resourceName and PU, update it. If not found, create a new entry - em.createQuery("Delete from IntegrityAuditEntity").executeUpdate(); - - // commit transaction - et.commit(); - em.close(); - logger.info("setUp: Exiting"); - - } - - - @After - public void tearDown() throws Exception { - - logger.info("tearDown: Entering"); - - logger.info("tearDown: Exiting"); - - } - - /* - * Tests designation logic when only one functioning resource is in play. Designation - * should stay with single resource. - * - * Note: console.log must be examined to ascertain whether or not this test was successful. - */ - @Ignore - @Test - public void testOneResource() throws Exception { - - logger.info("testOneResource: Entering"); - - IntegrityAudit integrityAudit = new IntegrityAudit(resourceName, persistenceUnit, properties); - integrityAudit.startAuditThread(); - - /* - * Sleep long enough to allow - * - * 1) pdp1 to run audit (15 seconds) - * - * 2) Logic to detect that no other node is available for designation (60 seconds) - * - * 3) pdp1 to run audit again (15 seconds) - */ - logger.info("testOneResource: Sleeping 100 seconds"); - Thread.sleep(100000); - - logger.info("testOneResource: Stopping audit thread"); - integrityAudit.stopAuditThread(); - - FileInputStream fstream = new FileInputStream(TEST_LOG); - BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); - String strLine; - int startIndex; - int endIndex; - - String rName = ""; - while ((strLine = br.readLine()) != null) { - // parse strLine to obtain what you want - if (strLine.contains("Starting audit simulation for resourceName=")) { - startIndex = strLine.indexOf("resourceName=") + 13; - endIndex = strLine.indexOf(","); - rName = strLine.substring(startIndex, endIndex); - logger.info("testOneResource: rName: " + rName); - assertEquals("pdp1", rName); - } - } - fstream.close(); - - /* - * Test fix for ECOMPD2TD-783: Audit fails to run when application is restarted. - */ - integrityAudit.startAuditThread(); - - /* - * Sleep long enough to allow - * - * 1) pdp1 to run audit (15 seconds) - * - * 2) Logic to detect that no other node is available for designation (60 seconds) - * - * 3) pdp1 to run audit again (15 seconds) - */ - logger.info("testOneResource: Sleeping 100 seconds for second time"); - Thread.sleep(100000); - - logger.info("testOneResource: Stopping audit thread for second time"); - integrityAudit.stopAuditThread(); - - fstream = new FileInputStream(TEST_LOG); - br = new BufferedReader(new InputStreamReader(fstream)); - - rName = ""; - while ((strLine = br.readLine()) != null) { - // parse strLine to obtain what you want - if (strLine.contains("Starting audit simulation for resourceName=")) { - startIndex = strLine.indexOf("resourceName=") + 13; - endIndex = strLine.indexOf(","); - rName = strLine.substring(startIndex, endIndex); - logger.info("testOneResource: rName: " + rName); - assertEquals("pdp1", rName); - } - } - fstream.close(); - - logger.info("testOneResource: Exiting"); - - } - - /* - * Tests designation logic when two functioning resources are in play. - * Designation should alternate between resources. - * - * Note: console.log must be examined to ascertain whether or not this test - * was successful. A quick way of examining the log is to search for the - * string "audit simulation": - * - * As you can see from the "dbAuditSimulate" method, when it executes, it - * logs the "Starting audit simulation..." message and when it finishes, it - * logs the "Finished audit simulation..." message. By looking for these - * messages, you can verify that the audits are run by the proper resource. - * For example, when testFourResourcesOneDead is run, you should see a - * Starting.../Finished... sequence for pdp1, followed by a - * Starting.../Finished... sequence for pdp2, followed by a - * Starting.../Finished... sequence for pdp4 (pdp3 is skipped as it's - * dead/hung), followed by a Starting.../Finished... sequence for pdp1, etc. - */ - @Ignore - @Test - public void testTwoResources() throws Exception { - - logger.info("testTwoResources: Entering"); - - /* - * Start audit for pdp1. - */ - IntegrityAudit integrityAudit = new IntegrityAudit(resourceName, persistenceUnit, properties); - integrityAudit.startAuditThread(); - - /* - * Start audit for pdp2. - */ - Properties properties2 = new Properties(); - properties2.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER); - properties2.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL); - properties2.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER); - properties2.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD); - properties2.put(IntegrityAuditProperties.SITE_NAME, "SiteA"); - properties2.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml"); - properties2.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5"); - String persistenceUnit2 = "testPU"; - String resourceName2 = "pdp2"; - IntegrityAudit integrityAudit2 = new IntegrityAudit(resourceName2, persistenceUnit2, properties2); - integrityAudit2.startAuditThread(); - - /* - * Sleep long enough to allow - * - * 1) pdp1 to run audit (15 seconds) - * - * 2) Logic to detect that pdp1 is stale and designate pdp2 (30 seconds) - * - * 3) pdp2 to run audit (15 seconds) - * - * 4) Logic to detect that pdp2 is stale and designate pdp1 (30 seconds) - * - * 5) pdp1 to run audit (15 seconds) - */ - Thread.sleep(120000); - - logger.info("testTwoResources: Stopping audit threads"); - integrityAudit.stopAuditThread(); - integrityAudit2.stopAuditThread(); - - FileInputStream fstream = new FileInputStream(TEST_LOG); - BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); - String strLine; - int startIndex; - int endIndex; - ArrayList expectedResult = new ArrayList(Arrays.asList("pdp1", "pdp2", "pdp1")); - ArrayList delegates = new ArrayList(); - while ((strLine = br.readLine()) != null) { - /* parse strLine to obtain what you want */ - if (strLine.contains("Starting audit simulation for resourceName=")) { - startIndex = strLine.indexOf("resourceName=") + 13; - endIndex = strLine.indexOf(","); - - String rName = strLine.substring(startIndex, endIndex); - - delegates.add(rName); - } - } - - for (String delegate: delegates) { - logger.info("testTwoResources: delegate: " + delegate); - } - - fstream.close(); - - assertTrue(expectedResult.equals(delegates)); - - assertTrue("delegate count only " + delegates.size(), delegates.size() >= 3); - assertTrue("delegate 0 is " + expectedResult.get(0), expectedResult.get(0).equals(delegates.get(0))); - assertTrue("delegate 1 is " + expectedResult.get(1), expectedResult.get(1).equals(delegates.get(1))); - assertTrue("delegate 2 is " + expectedResult.get(2), expectedResult.get(2).equals(delegates.get(2))); - - logger.info("testTwoResources: Exiting"); - - } - - /* - * Tests designation logic when two functioning resources are in play, each - * with different PUs. Audits for "testPU" and "integrityAuditPU" should run - * simultaneously. Designation should not alternate. - * - * Note: console.log must be examined to ascertain whether or not this test - * was successful. - */ - @Ignore - @Test - public void testTwoResourcesDifferentPus() throws Exception { - - logger.info("testTwoResourcesDifferentPus: Entering"); - - /* - * Start audit for pdp1. - */ - IntegrityAudit integrityAudit = new IntegrityAudit(resourceName, persistenceUnit, properties); - integrityAudit.startAuditThread(); - - /* - * Start audit for pdp2. - */ - Properties properties2 = new Properties(); - properties2.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER); - properties2.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL); - properties2.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER); - properties2.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD); - properties2.put(IntegrityAuditProperties.SITE_NAME, "SiteA"); - properties2.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml"); - properties2.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5"); - String persistenceUnit2 = "integrityAuditPU"; - String resourceName2 = "pdp2"; - IntegrityAudit integrityAudit2 = new IntegrityAudit(resourceName2, persistenceUnit2, properties2); - integrityAudit2.startAuditThread(); - - /* - * Sleep long enough to allow - * - * 1) pdp1 and pdp2 to run audit simultaneously (15 seconds) - * - * 2) Logic to detect that no other node is available for designation for either pdp1 or pdp2 (60 seconds) - * - * 3) pdp1 and pdp2 to again run audit simultaneously (15 seconds) - * - * NOTE: Based on the above, you would think a 100000ms sleep would be appropriate, - * but for some reason, when all tests are run this test errors. - */ - logger.info("testTwoResourcesDifferentPus: Sleeping 80 seconds"); - Thread.sleep(100000); - - logger.info("testTwoResourcesDifferentPus: Stopping audit threads"); - integrityAudit.stopAuditThread(); - integrityAudit2.stopAuditThread(); - - FileInputStream fstream = new FileInputStream(TEST_LOG); - BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); - String strLine; - int startIndex; - int endIndex; - ArrayList expectedResult = new ArrayList(Arrays.asList("pdp1", "pdp2", "pdp1", "pdp2")); - ArrayList delegates = new ArrayList(); - while ((strLine = br.readLine()) != null) { - /* parse strLine to obtain what you want */ - if (strLine.contains("Starting audit simulation for resourceName=")) { - startIndex = strLine.indexOf("resourceName=") + 13; - endIndex = strLine.indexOf(","); - - String rName = strLine.substring(startIndex, endIndex); - - delegates.add(rName); - } - } - - for (String delegate: delegates) { - logger.info("testTwoResourcesDifferentPus: delegate: " + delegate); - } - - fstream.close(); - - assertTrue("delegate count only " + delegates.size(), delegates.size() >= 4); - assertTrue("delegate 0 is " + expectedResult.get(0), expectedResult.get(0).equals(delegates.get(0))); - assertTrue("delegate 1 is " + expectedResult.get(1), expectedResult.get(1).equals(delegates.get(1))); - assertTrue("delegate 2 is " + expectedResult.get(2), expectedResult.get(2).equals(delegates.get(2))); - assertTrue("delegate 3 is " + expectedResult.get(3), expectedResult.get(3).equals(delegates.get(3))); - - assertTrue(expectedResult.equals(delegates)); - - logger.info("testTwoResourcesDifferentPus: Exiting"); - - } - - - /* - * Tests designation logic when two resources are in play but one of them is - * dead/hung. Designation should move to second resource but then get - * restored back to original resource when it's discovered that second - * resource is dead. - * - * Note: console.log must be examined to ascertain whether or not this test - * was successful. - */ - @Ignore - @Test - public void testTwoResourcesOneDead() throws Exception { - - logger.info("testTwoResourcesOneDead: Entering"); - - /* - * Start audit for pdp1. - */ - IntegrityAudit integrityAudit = new IntegrityAudit(resourceName, persistenceUnit, properties); - integrityAudit.startAuditThread(); - - /* - * Populate DB for pdp2, which will simulate it having registered but then having died. - */ - Properties properties2 = new Properties(); - properties2.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER); - properties2.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL); - properties2.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER); - properties2.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD); - properties2.put(IntegrityAuditProperties.SITE_NAME, "SiteA"); - properties2.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml"); - properties2.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5"); - String persistenceUnit2 = "testPU"; - String resourceName2 = "pdp2"; - new DbDAO(resourceName2, persistenceUnit2, properties2); - - /* - * Sleep long enough to allow - * - * 1) pdp1 to run audit (15 seconds) - * - * 2) Logic to detect that other node, pdp2, is not available for designation (60 seconds) - * - * 3) pdp1 to run audit again (15 seconds) - */ - logger.info("testTwoResourcesOneDead: Sleeping 100 seconds"); - Thread.sleep(100000); - - logger.info("testTwoResourcesOneDead: Stopping audit thread"); - integrityAudit.stopAuditThread(); - - FileInputStream fstream = new FileInputStream(TEST_LOG); - BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); - String strLine; - int startIndex; - int endIndex; - ArrayList expectedResult = new ArrayList(Arrays.asList("pdp1", "pdp1")); - ArrayList delegates = new ArrayList(); - while ((strLine = br.readLine()) != null) { - /* parse strLine to obtain what you want */ - if (strLine.contains("Starting audit simulation for resourceName=")) { - startIndex = strLine.indexOf("resourceName=") + 13; - endIndex = strLine.indexOf(","); - - String rName = strLine.substring(startIndex, endIndex); - - delegates.add(rName); - } - } - - for (String delegate: delegates) { - logger.info("testTwoResourcesOneDead: delegate: " + delegate); - } - - fstream.close(); - - assertTrue("delegate count only " + delegates.size(), delegates.size() >= 2); - assertTrue("delegate 0 is " + expectedResult.get(0), expectedResult.get(0).equals(delegates.get(0))); - assertTrue("delegate 1 is " + expectedResult.get(1), expectedResult.get(1).equals(delegates.get(1))); - - logger.info("testTwoResourcesOneDead: Exiting"); - - } - - - /* - * Tests designation logic when three functioning resources are in play. Designation should - * round robin among resources. - * - * Note: console.log must be examined to ascertain whether or not this test was successful. - */ - @Ignore - @Test - public void testThreeResources() throws Exception { - - logger.info("testThreeResources: Entering"); - - /* - * Start audit for pdp1. - */ - IntegrityAudit integrityAudit = new IntegrityAudit(resourceName, persistenceUnit, properties); - integrityAudit.startAuditThread(); - - /* - * Start audit for pdp2. - */ - Properties properties2 = new Properties(); - properties2.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER); - properties2.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL); - properties2.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER); - properties2.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD); - properties2.put(IntegrityAuditProperties.SITE_NAME, "SiteA"); - properties2.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml"); - properties2.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5"); - String persistenceUnit2 = "testPU"; - String resourceName2 = "pdp2"; - IntegrityAudit integrityAudit2 = new IntegrityAudit(resourceName2, persistenceUnit2, properties2); - integrityAudit2.startAuditThread(); - - /* - * Start audit for pdp3. - */ - Properties properties3 = new Properties(); - properties3.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER); - properties3.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL); - properties3.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER); - properties3.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD); - properties3.put(IntegrityAuditProperties.SITE_NAME, "SiteA"); - properties3.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml"); - properties3.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5"); - String persistenceUnit3 = "testPU"; - String resourceName3 = "pdp3"; - IntegrityAudit integrityAudit3 = new IntegrityAudit(resourceName3, persistenceUnit3, properties3); - integrityAudit3.startAuditThread(); - - /* - * Sleep long enough to allow - * - * 1) pdp1 to run audit (15 seconds) - * - * 2) Logic to detect that pdp1 is stale and designate pdp2 (30 seconds) - * - * 3) pdp2 to run audit (15 seconds) - * - * 4) Logic to detect that pdp2 is stale and designate pdp3 (30 seconds) - * - * 5) pdp3 to run audit (15 seconds) - * - * 6) Logic to detect that pdp3 is stale and designate pdp1 (30 seconds) - * - * 7) pdp1 to run audit (15 seconds) - */ - logger.info("testThreeResources: Sleeping 160 seconds"); - Thread.sleep(160000); - - logger.info("testThreeResources: Stopping threads"); - integrityAudit.stopAuditThread(); - integrityAudit2.stopAuditThread(); - integrityAudit3.stopAuditThread(); - - FileInputStream fstream = new FileInputStream(TEST_LOG); - BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); - String strLine; - int startIndex; - int endIndex; - ArrayList expectedResult = new ArrayList(Arrays.asList("pdp1", "pdp2", "pdp3", "pdp1")); - ArrayList delegates = new ArrayList(); - while ((strLine = br.readLine()) != null) { - /* parse strLine to obtain what you want */ - if (strLine.contains("Starting audit simulation for resourceName=")) { - startIndex = strLine.indexOf("resourceName=") + 13; - endIndex = strLine.indexOf(","); - - String rName = strLine.substring(startIndex, endIndex); - - delegates.add(rName); - } - } - - for (String delegate: delegates) { - logger.info("testThreeResources: delegate: " + delegate); - } - - fstream.close(); - - assertTrue("delegate count only " + delegates.size(), delegates.size() >= 3); - assertTrue("delegate 0 is " + expectedResult.get(0), expectedResult.get(0).equals(delegates.get(0))); - assertTrue("delegate 1 is " + expectedResult.get(1), expectedResult.get(1).equals(delegates.get(1))); - assertTrue("delegate 2 is " + expectedResult.get(2), expectedResult.get(2).equals(delegates.get(2))); - assertTrue("delegate 3 is " + expectedResult.get(3), expectedResult.get(3).equals(delegates.get(3))); - - logger.info("testThreeResources: Exiting"); - - } - - /* - * Tests designation logic when four functioning resources are in play, two - * with one PU, two with another. Audits for "testPU" and "integrityAuditPU" should run - * simultaneously. Designation should alternate between resources for each of the two - * persistence units. - * - * Note: console.log must be examined to ascertain whether or not this test - * was successful. - */ - @Ignore - @Test - public void testFourResourcesDifferentPus() throws Exception { - - logger.info("testFourResourcesDifferentPus: Entering"); - - /* - * Start audit for pdp1, testPU. - */ - IntegrityAudit integrityAudit = new IntegrityAudit(resourceName, persistenceUnit, properties); - integrityAudit.startAuditThread(); - - /* - * Start audit for pdp2, integrityAuditPU. - */ - Properties properties2 = new Properties(); - properties2.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER); - properties2.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL); - properties2.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER); - properties2.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD); - properties2.put(IntegrityAuditProperties.SITE_NAME, "SiteA"); - properties2.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml"); - properties2.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5"); - String persistenceUnit2 = "integrityAuditPU"; - String resourceName2 = "pdp2"; - IntegrityAudit integrityAudit2 = new IntegrityAudit(resourceName2, persistenceUnit2, properties2); - integrityAudit2.startAuditThread(); - - /* - * Start audit for pdp3, testPU. - */ - Properties properties3 = new Properties(); - properties3.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER); - properties3.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL); - properties3.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER); - properties3.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD); - properties3.put(IntegrityAuditProperties.SITE_NAME, "SiteA"); - properties3.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml"); - properties3.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5"); - String persistenceUnit3 = "testPU"; - String resourceName3 = "pdp3"; - IntegrityAudit integrityAudit3 = new IntegrityAudit(resourceName3, persistenceUnit3, properties3); - integrityAudit3.startAuditThread(); - - /* - * Start audit for pdp4, integrityAuditPU. - */ - Properties properties4 = new Properties(); - properties4.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER); - properties4.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL); - properties4.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER); - properties4.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD); - properties4.put(IntegrityAuditProperties.SITE_NAME, "SiteA"); - properties4.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml"); - properties4.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5"); - String persistenceUnit4 = "integrityAuditPU"; - String resourceName4 = "pdp4"; - IntegrityAudit integrityAudit4 = new IntegrityAudit(resourceName4, persistenceUnit4, properties4); - integrityAudit4.startAuditThread(); - - /* - * Sleep long enough to allow - * - * 1) pdp1 and pdp2 to run audit simultaneously (15 seconds) - * - * 2) Logic to detect that pdp1 and pdp2 are stale and designate pdp3 (one's counterpart) and pdp4 (two's counterpart) (30 seconds) - * - * 3) pdp3 and pdp4 to run audit simultaneously (15 seconds) - * - * 4) Logic to detect that pdp3 and pdp4 are stale and designate pdp1 (three's counterpart) and pdp2 (four's counterpart) (30 seconds) - * - * 5) pdp1 and pdp2 to run audit simultaneously (15 seconds) - */ - logger.info("testFourResourcesDifferentPus: Sleeping 120 seconds"); - Thread.sleep(120000); - - logger.info("testFourResourcesDifferentPus: Stopping threads"); - integrityAudit.stopAuditThread(); - integrityAudit2.stopAuditThread(); - integrityAudit3.stopAuditThread(); - integrityAudit4.stopAuditThread(); - - FileInputStream fstream = new FileInputStream(TEST_LOG); - BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); - String strLine; - int startIndex; - int endIndex; - ArrayList expectedResult = new ArrayList(Arrays.asList("pdp1", "pdp2", "pdp3", "pdp4", "pdp1", "pdp2")); - ArrayList delegates = new ArrayList(); - while ((strLine = br.readLine()) != null) { - /* parse strLine to obtain what you want */ - if (strLine.contains("Starting audit simulation for resourceName=")) { - startIndex = strLine.indexOf("resourceName=") + 13; - endIndex = strLine.indexOf(","); - - String rName = strLine.substring(startIndex, endIndex); - - delegates.add(rName); - } - } - - for (String delegate: delegates) { - logger.info("testFourResourcesDifferentPus: delegate: " + delegate); - } - - fstream.close(); - - assertTrue("delegate count only " + delegates.size(), delegates.size() >= 6); - assertTrue("delegate 0 is " + expectedResult.get(0), expectedResult.get(0).equals(delegates.get(0))); - assertTrue("delegate 1 is " + expectedResult.get(1), expectedResult.get(1).equals(delegates.get(1))); - assertTrue("delegate 2 is " + expectedResult.get(2), expectedResult.get(2).equals(delegates.get(2))); - assertTrue("delegate 3 is " + expectedResult.get(3), expectedResult.get(3).equals(delegates.get(3))); - assertTrue("delegate 4 is " + expectedResult.get(4), expectedResult.get(4).equals(delegates.get(4))); - assertTrue("delegate 5 is " + expectedResult.get(5), expectedResult.get(5).equals(delegates.get(5))); - - logger.info("testFourResourcesDifferentPus: Exiting"); - - } - - /* - * Tests designation logic when four resources are in play but one is not - * functioning. Designation should round robin among functioning resources - * only. - * - * Note: console.log must be examined to ascertain whether or not this test - * was successful. - */ - @Ignore - @Test - public void testFourResourcesOneDead() throws Exception { - - logger.info("testFourResourcesOneDead: Entering"); - - /* - * Start audit for pdp1. - */ - IntegrityAudit integrityAudit = new IntegrityAudit(resourceName, persistenceUnit, properties); - integrityAudit.startAuditThread(); - - /* - * Start audit for pdp2. - */ - Properties properties2 = new Properties(); - properties2.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER); - properties2.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL); - properties2.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER); - properties2.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD); - properties2.put(IntegrityAuditProperties.SITE_NAME, "SiteA"); - properties2.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml"); - properties2.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5"); - String persistenceUnit2 = "testPU"; - String resourceName2 = "pdp2"; - IntegrityAudit integrityAudit2 = new IntegrityAudit(resourceName2, persistenceUnit2, properties2); - integrityAudit2.startAuditThread(); - - /* - * Populate DB for pdp3, which will simulate it having registered but then having died. - */ - Properties properties3 = new Properties(); - properties3.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER); - properties3.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL); - properties3.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER); - properties3.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD); - properties3.put(IntegrityAuditProperties.SITE_NAME, "SiteA"); - properties3.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml"); - properties3.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5"); - String persistenceUnit3 = "testPU"; - String resourceName3 = "pdp3"; - new DbDAO(resourceName3, persistenceUnit3, properties3); - - /* - * Start audit for pdp4. - */ - Properties properties4 = new Properties(); - properties4.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER); - properties4.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL); - properties4.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER); - properties4.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD); - properties4.put(IntegrityAuditProperties.SITE_NAME, "SiteA"); - properties4.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml"); - properties4.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5"); - String persistenceUnit4 = "testPU"; - String resourceName4 = "pdp4"; - IntegrityAudit integrityAudit4 = new IntegrityAudit(resourceName4, persistenceUnit4, properties4); - integrityAudit4.startAuditThread(); - - /* - * Sleep long enough to allow - * - * 1) pdp1 to run audit (15 seconds) - * - * 2) Logic to detect that pdp1 is stale and designate pdp2 (30 seconds) - * - * 3) pdp2 to run audit (15 seconds) - * - * 4) Logic to detect that pdp2 is stale and designate pdp4 (30 seconds) - * - * 5) pdp4 to run audit (15 seconds) - * - * 6) Logic to detect that pdp4 is stale and designate pdp1 (30 seconds) - * - * 7) pdp1 to run audit (15 seconds) - * - * 8) Logic to detect that pdp1 is stale and designate pdp2 (30 seconds) - * - * 7) pdp2 to run audit (15 seconds) - */ - logger.info("testFourResourcesOneDead: Sleeping 210 seconds"); - Thread.sleep(210000); - - logger.info("testFourResourcesOneDead: Stopping threads"); - integrityAudit.stopAuditThread(); - integrityAudit2.stopAuditThread(); - integrityAudit4.stopAuditThread(); - - FileInputStream fstream = new FileInputStream(TEST_LOG); - BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); - String strLine; - int startIndex; - int endIndex; - ArrayList expectedResult = new ArrayList(Arrays.asList("pdp1", "pdp2", "pdp4", "pdp1", "pdp2", "pdp4")); - ArrayList delegates = new ArrayList(); - while ((strLine = br.readLine()) != null) { - /* parse strLine to obtain what you want */ - if (strLine.contains("Starting audit simulation for resourceName=")) { - startIndex = strLine.indexOf("resourceName=") + 13; - endIndex = strLine.indexOf(","); - - String rName = strLine.substring(startIndex, endIndex); - - delegates.add(rName); - } - } - - for (String delegate : delegates) { - logger.info("testFourResourcesOneDead: delegate: " + delegate); - } - - fstream.close(); - - assertTrue("delegate count only " + delegates.size(), delegates.size() >= 6); - assertTrue("delegate 0 is " + expectedResult.get(0), expectedResult.get(0).equals(delegates.get(0))); - assertTrue("delegate 1 is " + expectedResult.get(1), expectedResult.get(1).equals(delegates.get(1))); - assertTrue("delegate 2 is " + expectedResult.get(2), expectedResult.get(2).equals(delegates.get(2))); - assertTrue("delegate 3 is " + expectedResult.get(3), expectedResult.get(3).equals(delegates.get(3))); - assertTrue("delegate 4 is " + expectedResult.get(4), expectedResult.get(4).equals(delegates.get(4))); - assertTrue("delegate 5 is " + expectedResult.get(5), expectedResult.get(5).equals(delegates.get(5))); - - logger.info("testFourResourcesOneDead: Exiting"); - - } - - /* - * Tests designation logic when four resources are in play but only one is - * functioning. Designation should remain with sole functioning resource. - * - * Note: console.log must be examined to ascertain whether or not this test - * was successful. - */ - @Ignore - @Test - public void testFourResourcesThreeDead() throws Exception { - - logger.info("testFourResourcesThreeDead: Entering"); - - /* - * Populate DB for pdp1, which will simulate it having registered but then having died. - */ - new DbDAO(resourceName, persistenceUnit, properties); - - - /* - * Populate DB for pdp2, which will simulate it having registered but then having died. - */ - Properties properties2 = new Properties(); - properties2.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER); - properties2.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL); - properties2.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER); - properties2.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD); - properties2.put(IntegrityAuditProperties.SITE_NAME, "SiteA"); - properties2.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml"); - properties2.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5"); - String persistenceUnit2 = "testPU"; - String resourceName2 = "pdp2"; - new DbDAO(resourceName2, persistenceUnit2, properties2); - - /* - * Start audit for pdp3. - */ - Properties properties3 = new Properties(); - properties3.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER); - properties3.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL); - properties3.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER); - properties3.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD); - properties3.put(IntegrityAuditProperties.SITE_NAME, "SiteA"); - properties3.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml"); - properties3.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5"); - String persistenceUnit3 = "testPU"; - String resourceName3 = "pdp3"; - IntegrityAudit integrityAudit3 = new IntegrityAudit(resourceName3, persistenceUnit3, properties3); - integrityAudit3.startAuditThread(); - - /* - * Populate DB for pdp4, which will simulate it having registered but then having died. - */ - Properties properties4 = new Properties(); - properties4.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER); - properties4.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL); - properties4.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER); - properties4.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD); - properties4.put(IntegrityAuditProperties.SITE_NAME, "SiteA"); - properties4.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml"); - properties4.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5"); - String persistenceUnit4 = "testPU"; - String resourceName4 = "pdp4"; - new DbDAO(resourceName4, persistenceUnit4, properties4); - - /* - * Sleep long enough to allow - * - * 1) pdp3 to discover that all other designation candidates are stale (30 seconds) - * - * 1) pdp3 to run audit (15 seconds) - * - * 2) Logic to detect that no other nodes are available for designation (60 seconds) - * - * 3) pdp3 to run audit again (15 seconds) - */ - logger.info("testFourResourcesThreeDead: Sleeping 130 seconds"); - Thread.sleep(130000); - - logger.info("testFourResourcesThreeDead: Stopping thread"); - integrityAudit3.stopAuditThread(); - - FileInputStream fstream = new FileInputStream(TEST_LOG); - BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); - String strLine; - int startIndex; - int endIndex; - ArrayList expectedResult = new ArrayList(Arrays.asList("pdp3", "pdp3")); - ArrayList delegates = new ArrayList(); - while ((strLine = br.readLine()) != null) { - /* parse strLine to obtain what you want */ - if (strLine.contains("Starting audit simulation for resourceName=")) { - startIndex = strLine.indexOf("resourceName=") + 13; - endIndex = strLine.indexOf(","); - - String rName = strLine.substring(startIndex, endIndex); - - delegates.add(rName); - } - } - - for (String delegate : delegates) { - logger.info("testFourResourcesThreeDead: delegate: " + delegate); - } - - fstream.close(); - - assertTrue("delegate count only " + delegates.size(), delegates.size() >= 2); - assertTrue("delegate 0 is " + expectedResult.get(0), expectedResult.get(0).equals(delegates.get(0))); - assertTrue("delegate 1 is " + expectedResult.get(1), expectedResult.get(1).equals(delegates.get(1))); - - logger.info("testFourResourcesThreeDead: Exiting"); - - } - - - /* - * Tests designation logic when the designated node dies and is no longer - * current - * - * Note: console.log must be examined to ascertain whether or not this test - * was successful. - */ - @Ignore - @Test - public void testDesignatedNodeDead() throws Exception { - logger.info("testDesignatedNodeDead: Entering"); - - /* - * Instantiate audit object for pdp1. - */ - IntegrityAudit integrityAudit = new IntegrityAudit(resourceName, persistenceUnit, properties); - - /* - * Start audit for pdp2. - */ - Properties properties2 = new Properties(); - properties2.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER); - properties2.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL); - properties2.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER); - properties2.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD); - properties2.put(IntegrityAuditProperties.SITE_NAME, "SiteA"); - properties2.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml"); - properties2.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5"); - String persistenceUnit2 = "testPU"; - String resourceName2 = "pdp2"; - IntegrityAudit integrityAudit2 = new IntegrityAudit(resourceName2, persistenceUnit2, properties2); - - /* - * Instantiate audit object for pdp3. - */ - Properties properties3 = new Properties(); - properties3.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER); - properties3.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL); - properties3.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER); - properties3.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD); - properties3.put(IntegrityAuditProperties.SITE_NAME, "SiteA"); - properties3.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml"); - properties3.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5"); - String persistenceUnit3 = "testPU"; - String resourceName3 = "pdp3"; - IntegrityAudit integrityAudit3 = new IntegrityAudit(resourceName3, persistenceUnit3, properties3); - - // Start audit on pdp1 - logger.info("testDesignatedNodeDead: Start audit on pdp1"); - integrityAudit.startAuditThread(); - - // Start the auditing threads on other nodes. - logger.info("testDesignatedNodeDead: Start audit on pdp2"); - integrityAudit2.startAuditThread(); - logger.info("testDesignatedNodeDead: Start audit on pdp3"); - integrityAudit3.startAuditThread(); - - - // Kill audit on pdp1 - logger.info("testDesignatedNodeDead: Kill audit on pdp1"); - integrityAudit.stopAuditThread(); - - // Sleep long enough for pdp1 to get stale and pdp2 to take over - logger.info("testDesignatedNodeDead: Sleep long enough for pdp1 to get stale and pdp2 to take over"); - Thread.sleep(AuditThread.AUDIT_COMPLETION_INTERVAL + FUDGE_FACTOR); - - // Start audit thread on pdp1 again. - logger.info("testDesignatedNodeDead: Start audit thread on pdp1 again."); - integrityAudit.startAuditThread(); - - // Sleep long enough for pdp2 to complete its audit and get stale, at - // which point pdp3 should take over - logger.info("testDesignatedNodeDead: Sleep long enough for pdp2 to complete its audit and get stale, at which point pdp3 should take over"); - Thread.sleep((AuditThread.AUDIT_SIMULATION_SLEEP_INTERVAL * AuditThread.AUDIT_SIMULATION_ITERATIONS) - + AuditThread.AUDIT_COMPLETION_INTERVAL + FUDGE_FACTOR); - - // Kill audit on pdp3 - logger.info("testDesignatedNodeDead: Killing audit on pdp3"); - integrityAudit3.stopAuditThread(); - - // Sleep long enough for pdp3 to get stale and pdp1 to take over - logger.info("testDesignatedNodeDead: Sleep long enough for pdp3 to get stale and pdp1 to take over"); - Thread.sleep(AuditThread.AUDIT_COMPLETION_INTERVAL + FUDGE_FACTOR); - - FileInputStream fstream = new FileInputStream(TEST_LOG); - BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); - String strLine; - int startIndex; - int endIndex; - ArrayList expectedResult = new ArrayList(Arrays.asList("pdp1", "pdp2", "pdp3", "pdp1")); - ArrayList delegates = new ArrayList(); - while ((strLine = br.readLine()) != null) { - /* parse strLine to obtain what you want */ - if (strLine.contains("Starting audit simulation for resourceName=")) { - startIndex = strLine.indexOf("resourceName=") + 13; - endIndex = strLine.indexOf(","); - - String rName = strLine.substring(startIndex, endIndex); - - delegates.add(rName); - } - } - fstream.close(); - - // Stop remaining threads. - logger.info("testDesignatedNodeDead: Stopping remaining threads"); - integrityAudit.stopAuditThread(); - integrityAudit2.stopAuditThread(); - - for (String delegate: delegates) { - logger.info("testDesignatedNodeDead: delegate: " + delegate); - } - - assertTrue("delegate count only " + delegates.size(), delegates.size() >= 4); - assertTrue("delegate 0 is " + expectedResult.get(0), expectedResult.get(0).equals(delegates.get(0))); - assertTrue("delegate 1 is " + expectedResult.get(1), expectedResult.get(1).equals(delegates.get(1))); - assertTrue("delegate 2 is " + expectedResult.get(2), expectedResult.get(2).equals(delegates.get(2))); - assertTrue("delegate 3 is " + expectedResult.get(3), expectedResult.get(3).equals(delegates.get(3))); - - logger.info("testDesignatedNodeDead: Exiting"); - } -} diff --git a/integrity-audit/src/test/java/org/openecomp/policy/common/ia/test/jpa/IaTestEntity.java b/integrity-audit/src/test/java/org/openecomp/policy/common/ia/test/jpa/IaTestEntity.java deleted file mode 100644 index 21b09460..00000000 --- a/integrity-audit/src/test/java/org/openecomp/policy/common/ia/test/jpa/IaTestEntity.java +++ /dev/null @@ -1,157 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Integrity Audit - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.openecomp.policy.common.ia.test.jpa; - -import java.io.Serializable; -import java.util.Date; - -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; -import javax.persistence.NamedQueries; -import javax.persistence.NamedQuery; -import javax.persistence.PrePersist; -import javax.persistence.PreUpdate; -import javax.persistence.Table; -import javax.persistence.Temporal; -import javax.persistence.TemporalType; - -@Entity -@Table(name="IaTestEntity") -@NamedQueries({ - @NamedQuery(name=" IaTestEntity.findAll", query="SELECT e FROM IaTestEntity e "), - @NamedQuery(name="IaTestEntity.deleteAll", query="DELETE FROM IaTestEntity WHERE 1=1") -}) -//@SequenceGenerator(name="seqImTest", initialValue=1, allocationSize=1) - -public class IaTestEntity implements Serializable { - private static final long serialVersionUID = 1L; - - @Id - //@GeneratedValue(strategy = GenerationType.SEQUENCE, generator="seqImTest") - @GeneratedValue(strategy = GenerationType.AUTO) - @Column(name="ImTestId") - private long imTestId; - - @Column(name="created_by", nullable=false, length=255) - private String createdBy = "guest"; - - @Column(name="person", nullable=false, length=255) - private PersonTest person; - - @Temporal(TemporalType.TIMESTAMP) - @Column(name="created_date", updatable=false) - private Date createdDate; - - @Column(name="modified_by", nullable=false, length=255) - private String modifiedBy = "guest"; - - @Temporal(TemporalType.TIMESTAMP) - @Column(name="modified_date", nullable=false) - private Date modifiedDate; - - public IaTestEntity() { - } - - @PrePersist - public void prePersist() { - Date date = new Date(); - this.createdDate = date; - this.modifiedDate = date; - } - - @PreUpdate - public void preUpdate() { - this.modifiedDate = new Date(); - } - - /** - * @return the Id - */ - public long getImTestId() { - return imTestId; - } - - /** - * @return the createdBy - */ - public String getCreatedBy() { - return createdBy; - } - - /** - * @param createdBy the createdBy to set - */ - public void setCreatedBy(String createdBy) { - this.createdBy = createdBy; - } - - /** - * @return the modifiedBy - */ - public String getModifiedBy() { - return modifiedBy; - } - - /** - * @param modifiedBy the modifiedBy to set - */ - public void setModifiedBy(String modifiedBy) { - this.modifiedBy = modifiedBy; - } - - /** - * @return the modifiedDate - */ - public Date getModifiedDate() { - return modifiedDate; - } - - /** - * @param modifiedDate the modifiedDate to set - */ - public void setModifiedDate(Date modifiedDate) { - this.modifiedDate = modifiedDate; - } - - /** - * @return the createdDate - */ - public Date getCreatedDate() { - return createdDate; - } - - /** - * @param the person to set - */ - public void setPersonTest(PersonTest p) { - this.person = p; - } - - /** - * @return the person - */ - public PersonTest getPersonTest() { - return person; - } -} diff --git a/integrity-audit/src/test/java/org/openecomp/policy/common/ia/test/jpa/PersonTest.java b/integrity-audit/src/test/java/org/openecomp/policy/common/ia/test/jpa/PersonTest.java deleted file mode 100644 index 7ec5e294..00000000 --- a/integrity-audit/src/test/java/org/openecomp/policy/common/ia/test/jpa/PersonTest.java +++ /dev/null @@ -1,64 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Integrity Audit - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.openecomp.policy.common.ia.test.jpa; - -import java.io.Serializable; - -public class PersonTest implements Serializable { - /** - * - */ - private static final long serialVersionUID = 1L; - private String firstName; - private String lastName; - private int age; - - public PersonTest(String first, String last, int age) { - this.firstName = first; - this.lastName = last; - this.age = age; - } - - public String getFirstName() { - return this.firstName; - } - - public void setFirstName(String name) { - this.firstName = name; - } - - public String getLasttName() { - return this.lastName; - } - - public void setLastName(String name) { - this.lastName = name; - } - - public int getAge() { - return this.age; - } - - public void setAge(int age) { - this.age = age; - } - -} diff --git a/integrity-audit/src/test/resources/logback.xml b/integrity-audit/src/test/resources/logback.xml index 78de63a3..9086aa9d 100644 --- a/integrity-audit/src/test/resources/logback.xml +++ b/integrity-audit/src/test/resources/logback.xml @@ -29,7 +29,7 @@ + ::= "MSO" | "DCAE" | "ASDC " | "AAI" |"Policy" | "SDNC" | "AC" --> -- cgit 1.2.3-korg