diff options
author | Jim Hahn <jrh3@att.com> | 2019-06-19 17:31:24 -0400 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2019-06-21 11:36:18 -0400 |
commit | c9392e1a126de54ae8a0e1f3316729e878115300 (patch) | |
tree | 5a371d53f5e6cf8bedbb4daf94f558844037fd7d /integrity-audit/src/main/java/org/onap | |
parent | 62463da9961f7e524559b34766baf2dd469fa910 (diff) |
Fix sonar issues in integrity audit
Mostly used Eclipse Refactor->Extract Method to reduce cyclomatic
complexity. Also combined a few "if" statements to reduce nesting
levels.
Also addressed some sonar issues in the tests (e.g., use "<>" where
appropriate).
Did not attempt to increase junit coverage.
Change-Id: I9d6c1305ce455f0d64249b548d123bb9bf37292a
Issue-ID: POLICY-1791
Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'integrity-audit/src/main/java/org/onap')
5 files changed, 521 insertions, 452 deletions
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 index df979286..25bafdc4 100644 --- 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 @@ -147,146 +147,7 @@ public class AuditThread extends Thread { /* * Triggers change in designation, unless no other viable candidate. */ - boolean auditCompleted = false; - - DbAudit dbAudit = new DbAudit(dbDao); - - IntegrityAuditEntity entityCurrentlyDesignated; - IntegrityAuditEntity thisEntity; - 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<IntegrityAuditEntity> 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) { - // for junit testing: indicate that an audit has completed - auditCompleted(); - - if (logger.isDebugEnabled()) { - logger.debug("AuditThread.run: Audit completed; resourceName=" + this.resourceName - + " sleeping " + integrityAuditPeriodSeconds + "s"); - } - AuditorTime.getInstance().sleep(integrityAuditPeriodSeconds * 1000L); - if (logger.isDebugEnabled()) { - logger.debug(AUDIT_THREAD_MESSAGE + this.resourceName + " awaking from " - + integrityAuditPeriodSeconds + "s sleep"); - } - - } else { - - if (logger.isDebugEnabled()) { - logger.debug(AUDIT_THREAD_MESSAGE + this.resourceName + ": Sleeping " - + AuditThread.AUDIT_THREAD_SLEEP_INTERVAL_MS + "ms"); - } - AuditorTime.getInstance().sleep(AuditThread.AUDIT_THREAD_SLEEP_INTERVAL_MS); - if (logger.isDebugEnabled()) { - logger.debug(AUDIT_THREAD_MESSAGE + this.resourceName + ": Awaking from " - + AuditThread.AUDIT_THREAD_SLEEP_INTERVAL_MS + "ms sleep"); - } - - } - - } catch (Exception e) { - if (isInterruptedException(e)) { - String msg = "AuditThread.run loop - Exception thrown: " + e.getMessage() + "; Stopping."; - logger.error(MessageCodes.EXCEPTION_ERROR, e, msg); - break; - } - - String msg = "AuditThread.run loop - Exception thrown: " + e.getMessage() - + "; Will try audit again in " + integrityAuditPeriodSeconds + " seconds"; - logger.error(MessageCodes.EXCEPTION_ERROR, e, msg); - // Sleep and try again later - AuditorTime.getInstance().sleep(integrityAuditPeriodSeconds * 1000L); - } - - } + runUntilInterrupted(); } catch (Exception e) { String msg = "AuditThread.run: Could not start audit loop. Exception thrown; message=" + e.getMessage(); @@ -299,6 +160,156 @@ public class AuditThread extends Thread { logger.info("AuditThread.run: Exiting"); } + private void runUntilInterrupted() throws InterruptedException { + boolean auditCompleted = false; + + DbAudit dbAudit = new DbAudit(dbDao); + + IntegrityAuditEntity entityCurrentlyDesignated; + IntegrityAuditEntity thisEntity; + 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<IntegrityAuditEntity> 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. + */ + auditCompleted = doAudit(auditCompleted, dbAudit, entityCurrentlyDesignated, thisEntity, + integrityAuditEntityList); + + /* + * If we've just run audit, sleep per the integrity_audit_period_seconds + * property, otherwise just sleep the normal interval. + */ + sleepAfterAudit(auditCompleted); + + } catch (Exception e) { + if (isInterruptedException(e)) { + String msg = "AuditThread.run loop - Exception thrown: " + e.getMessage() + "; Stopping."; + logger.error(MessageCodes.EXCEPTION_ERROR, e, msg); + break; + } + + String msg = "AuditThread.run loop - Exception thrown: " + e.getMessage() + + "; Will try audit again in " + integrityAuditPeriodSeconds + " seconds"; + logger.error(MessageCodes.EXCEPTION_ERROR, e, msg); + // Sleep and try again later + AuditorTime.getInstance().sleep(integrityAuditPeriodSeconds * 1000L); + } + + } + } + + private boolean doAudit(boolean auditCompleted, DbAudit dbAudit, IntegrityAuditEntity entityCurrentlyDesignated, + IntegrityAuditEntity thisEntity, List<IntegrityAuditEntity> integrityAuditEntityList) + throws IntegrityAuditException { + + 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); + + } + return auditCompleted; + } + + private void sleepAfterAudit(boolean auditCompleted) throws InterruptedException { + if (auditCompleted) { + // for junit testing: indicate that an audit has completed + auditCompleted(); + + if (logger.isDebugEnabled()) { + logger.debug("AuditThread.run: Audit completed; resourceName=" + this.resourceName + + " sleeping " + integrityAuditPeriodSeconds + "s"); + } + AuditorTime.getInstance().sleep(integrityAuditPeriodSeconds * 1000L); + if (logger.isDebugEnabled()) { + logger.debug(AUDIT_THREAD_MESSAGE + this.resourceName + " awaking from " + + integrityAuditPeriodSeconds + "s sleep"); + } + + } else { + + if (logger.isDebugEnabled()) { + logger.debug(AUDIT_THREAD_MESSAGE + this.resourceName + ": Sleeping " + + AuditThread.AUDIT_THREAD_SLEEP_INTERVAL_MS + "ms"); + } + AuditorTime.getInstance().sleep(AuditThread.AUDIT_THREAD_SLEEP_INTERVAL_MS); + if (logger.isDebugEnabled()) { + logger.debug(AUDIT_THREAD_MESSAGE + this.resourceName + ": Awaking from " + + AuditThread.AUDIT_THREAD_SLEEP_INTERVAL_MS + "ms sleep"); + } + + } + } + /** * Determines if an exception is an InterruptedException or was caused by an * InterruptedException. @@ -332,7 +343,6 @@ public class AuditThread extends Thread { + integrityAuditEntityList.size()); } - IntegrityAuditEntity designationCandidate; IntegrityAuditEntity thisEntity = null; int designatedEntityIndex = -1; @@ -346,94 +356,112 @@ public class AuditThread extends Thread { logIntegrityAuditEntity(integrityAuditEntity); } - if (integrityAuditEntity.getResourceName().equals(this.resourceName)) { - if (logger.isDebugEnabled()) { - logger.debug("getDesignationCandidate: thisEntity=" + integrityAuditEntity.getResourceName()); - } - thisEntity = integrityAuditEntity; - } + thisEntity = detmEntity(integrityAuditEntity, thisEntity); if (integrityAuditEntity.isDesignated()) { if (logger.isDebugEnabled()) { logger.debug("getDesignationCandidate: Currently designated entity resourceName=" - + integrityAuditEntity.getResourceName() + PERSISTENCE_MESSAGE - + integrityAuditEntity.getPersistenceUnit() + LAST_UPDATED_MESSAGE - + integrityAuditEntity.getLastUpdated() + ENTITY_INDEX_MESSAGE + entityIndex); + + integrityAuditEntity.getResourceName() + PERSISTENCE_MESSAGE + + integrityAuditEntity.getPersistenceUnit() + LAST_UPDATED_MESSAGE + + integrityAuditEntity.getLastUpdated() + ENTITY_INDEX_MESSAGE + entityIndex); } designatedEntityIndex = entityIndex; /* * Entity not currently designated */ - } else { + } else if (isStale(integrityAuditEntity)) { + /* + * Non-designated entity is stale. + */ + + if (logger.isDebugEnabled()) { + logger.debug("getDesignationCandidate: Entity is stale; resourceName=" + + integrityAuditEntity.getResourceName() + PERSISTENCE_MESSAGE + + integrityAuditEntity.getPersistenceUnit() + LAST_UPDATED_MESSAGE + + integrityAuditEntity.getLastUpdated() + ENTITY_INDEX_MESSAGE + entityIndex); + } /* - * See if non-designated entity is stale. + * Entity is current. */ - if (isStale(integrityAuditEntity)) { + } else if (designatedEntityIndex == -1) { + priorCandidateIndex = detmPriorCandidate(entityIndex, integrityAuditEntity, priorCandidateIndex); - if (logger.isDebugEnabled()) { - logger.debug("getDesignationCandidate: Entity is stale; resourceName=" - + integrityAuditEntity.getResourceName() + PERSISTENCE_MESSAGE - + integrityAuditEntity.getPersistenceUnit() + LAST_UPDATED_MESSAGE - + integrityAuditEntity.getLastUpdated() + ENTITY_INDEX_MESSAGE + entityIndex); - } - - /* - * Entity is current. - */ - } else { - - if (designatedEntityIndex == -1) { - - if (priorCandidateIndex == -1) { - if (logger.isDebugEnabled()) { - logger.debug("getDesignationCandidate: Prior candidate found, resourceName=" - + integrityAuditEntity.getResourceName() + PERSISTENCE_MESSAGE - + integrityAuditEntity.getPersistenceUnit() + LAST_UPDATED_MESSAGE - + integrityAuditEntity.getLastUpdated() + ENTITY_INDEX_MESSAGE + entityIndex); - } - priorCandidateIndex = entityIndex; - } else { - if (logger.isDebugEnabled()) { - logger.debug( - "getDesignationCandidate: Prior entity current but prior candidate already " - + "found; resourceName=" + integrityAuditEntity.getResourceName() - + PERSISTENCE_MESSAGE + integrityAuditEntity.getPersistenceUnit() - + LAST_UPDATED_MESSAGE + integrityAuditEntity.getLastUpdated() - + ENTITY_INDEX_MESSAGE + entityIndex); - } - } - } else { - if (subsequentCandidateIndex == -1) { - if (logger.isDebugEnabled()) { - logger.debug("getDesignationCandidate: Subsequent candidate found, resourceName=" - + integrityAuditEntity.getResourceName() + PERSISTENCE_MESSAGE - + integrityAuditEntity.getPersistenceUnit() + LAST_UPDATED_MESSAGE - + integrityAuditEntity.getLastUpdated() + ENTITY_INDEX_MESSAGE + entityIndex); - } - subsequentCandidateIndex = entityIndex; - } else { - if (logger.isDebugEnabled()) { - logger.debug( - "getDesignationCandidate: Subsequent entity current but subsequent candidate " - + "already found; resourceName=" - + integrityAuditEntity.getResourceName() + PERSISTENCE_MESSAGE - + integrityAuditEntity.getPersistenceUnit() + LAST_UPDATED_MESSAGE - + integrityAuditEntity.getLastUpdated() + ENTITY_INDEX_MESSAGE - + entityIndex); - } - } - } - - } // end entity is current - - } // end entity not currently designated + } else { + subsequentCandidateIndex = + detmSubsequentCandidate(entityIndex, integrityAuditEntity, subsequentCandidateIndex); + } entityIndex++; } // end for loop + return detmDesignationCandidate(integrityAuditEntityList, thisEntity, priorCandidateIndex, + subsequentCandidateIndex); + } + + private IntegrityAuditEntity detmEntity(IntegrityAuditEntity integrityAuditEntity, + IntegrityAuditEntity thisEntity) { + if (integrityAuditEntity.getResourceName().equals(this.resourceName)) { + if (logger.isDebugEnabled()) { + logger.debug("getDesignationCandidate: thisEntity=" + integrityAuditEntity.getResourceName()); + } + thisEntity = integrityAuditEntity; + } + return thisEntity; + } + + private int detmPriorCandidate(int entityIndex, IntegrityAuditEntity integrityAuditEntity, + int priorCandidateIndex) { + if (priorCandidateIndex == -1) { + if (logger.isDebugEnabled()) { + logger.debug("getDesignationCandidate: Prior candidate found, resourceName=" + + integrityAuditEntity.getResourceName() + PERSISTENCE_MESSAGE + + integrityAuditEntity.getPersistenceUnit() + LAST_UPDATED_MESSAGE + + integrityAuditEntity.getLastUpdated() + ENTITY_INDEX_MESSAGE + entityIndex); + } + priorCandidateIndex = entityIndex; + } else { + if (logger.isDebugEnabled()) { + logger.debug( + "getDesignationCandidate: Prior entity current but prior candidate already " + + "found; resourceName=" + integrityAuditEntity.getResourceName() + + PERSISTENCE_MESSAGE + integrityAuditEntity.getPersistenceUnit() + + LAST_UPDATED_MESSAGE + integrityAuditEntity.getLastUpdated() + + ENTITY_INDEX_MESSAGE + entityIndex); + } + } + return priorCandidateIndex; + } + + private int detmSubsequentCandidate(int entityIndex, IntegrityAuditEntity integrityAuditEntity, + int subsequentCandidateIndex) { + if (subsequentCandidateIndex == -1) { + if (logger.isDebugEnabled()) { + logger.debug("getDesignationCandidate: Subsequent candidate found, resourceName=" + + integrityAuditEntity.getResourceName() + PERSISTENCE_MESSAGE + + integrityAuditEntity.getPersistenceUnit() + LAST_UPDATED_MESSAGE + + integrityAuditEntity.getLastUpdated() + ENTITY_INDEX_MESSAGE + entityIndex); + } + subsequentCandidateIndex = entityIndex; + } else { + if (logger.isDebugEnabled()) { + logger.debug( + "getDesignationCandidate: Subsequent entity current but subsequent candidate " + + "already found; resourceName=" + + integrityAuditEntity.getResourceName() + PERSISTENCE_MESSAGE + + integrityAuditEntity.getPersistenceUnit() + LAST_UPDATED_MESSAGE + + integrityAuditEntity.getLastUpdated() + ENTITY_INDEX_MESSAGE + + entityIndex); + } + } + return subsequentCandidateIndex; + } + + private IntegrityAuditEntity detmDesignationCandidate(List<IntegrityAuditEntity> integrityAuditEntityList, + IntegrityAuditEntity thisEntity, int priorCandidateIndex, int subsequentCandidateIndex) { + IntegrityAuditEntity designationCandidate; /* * 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 @@ -454,15 +482,18 @@ public class AuditThread extends Thread { logger.debug("getDesignationCandidate: Exiting and returning prior designationCandidate=" + designationCandidate.getResourceName()); } - } else { + } else if (thisEntity != null) { logger.debug("getDesignationCandidate: No subsequent or prior candidate found; designating thisEntity, " - + "resourceName=" + thisEntity.getResourceName()); + + "resourceName=" + thisEntity.getResourceName()); designationCandidate = thisEntity; + } else { + // this shouldn't happen, but adding it to make sonar happy + logger.debug("getDesignationCandidate: No entities available"); + designationCandidate = null; } } return designationCandidate; - } /** 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 index 9a73b79c..4e718a60 100644 --- 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 @@ -144,6 +144,23 @@ public class DbAudit { */ Map<String, Set<Object>> misMatchedMap = new HashMap<>(); + compareList(persistenceUnit, iaeList, myIae, classNameSet, misMatchedMap); + + // If misMatchedMap is not empty, retrieve the entries in each misMatched list and compare + // again + recompareList(resourceName, persistenceUnit, iaeList, myIae, misMatchedMap); + + if (logger.isDebugEnabled()) { + logger.debug("dbAudit: Exiting"); + } + + return; // all done + } + + private void compareList(String persistenceUnit, List<IntegrityAuditEntity> iaeList, IntegrityAuditEntity myIae, + Set<String> classNameSet, Map<String, Set<Object>> misMatchedMap) + throws IntegrityAuditException { + // We need to keep track of how long the audit is taking long startTime = AuditorTime.getInstance().getMillis(); @@ -161,52 +178,7 @@ public class DbAudit { Map<Object, Object> 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() + COMMA_RESOURCE_NAME + iae.getResourceName()); - } - continue; // no need to compare with self - } else { - if (logger.isDebugEnabled()) { - logger.debug("dbAudit: Id=" + iae.getId() + COMMA_RESOURCE_NAME + 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 - Map<Object, Object> 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. - */ - Set<Object> misMatchedKeySet = compareEntries(myEntries, theirEntries); - if (!misMatchedKeySet.isEmpty()) { - Set<Object> misMatchedEntry = misMatchedMap.get(clazzName); - if (misMatchedEntry == null) { - misMatchedMap.put(clazzName, misMatchedKeySet); - } else { - misMatchedEntry.addAll(misMatchedKeySet); - misMatchedMap.put(clazzName, misMatchedEntry); - } - } - } // end for (IntegrityAuditEntity iae : iaeList) + compareMineWithTheirs(persistenceUnit, iaeList, myIae, misMatchedMap, clazzName, myEntries); // Time check if ((AuditorTime.getInstance().getMillis() - startTime) >= DB_AUDIT_UPDATE_MS) { @@ -239,9 +211,84 @@ public class DbAudit { 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 + private void compareMineWithTheirs(String persistenceUnit, List<IntegrityAuditEntity> iaeList, + IntegrityAuditEntity myIae, Map<String, Set<Object>> misMatchedMap, String clazzName, + Map<Object, Object> myEntries) { + + 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() + COMMA_RESOURCE_NAME + iae.getResourceName()); + } + continue; // no need to compare with self + } + + if (logger.isDebugEnabled()) { + logger.debug("dbAudit: Id=" + iae.getId() + COMMA_RESOURCE_NAME + iae.getResourceName()); + } + + // get a map of the instances for their iae indexed by id + Map<Object, Object> theirEntries = + dbDao.getAllEntries(persistenceUnit, getTheirDaoProperties(iae), 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. + */ + compareMineWithTheirs(myEntries, theirEntries, clazzName, misMatchedMap); + } // end for (IntegrityAuditEntity iae : iaeList) + } + + private void compareMineWithTheirs(Map<Object, Object> myEntries, Map<Object, Object> theirEntries, + String clazzName, Map<String, Set<Object>> misMatchedMap) { + + Set<Object> misMatchedKeySet = compareEntries(myEntries, theirEntries); + if (misMatchedKeySet.isEmpty()) { + return; + } + + Set<Object> misMatchedEntry = misMatchedMap.get(clazzName); + if (misMatchedEntry == null) { + misMatchedMap.put(clazzName, misMatchedKeySet); + } else { + misMatchedEntry.addAll(misMatchedKeySet); + } + } + + /** + * Creates properties for the other db node. + * @param iae target DB node + * @return DAO properties for the given DB node + */ + private Properties getTheirDaoProperties(IntegrityAuditEntity iae) { + 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()); + + return theirProperties; + } + + private void recompareList(String resourceName, String persistenceUnit, List<IntegrityAuditEntity> iaeList, + IntegrityAuditEntity myIae, Map<String, Set<Object>> misMatchedMap) + throws IntegrityAuditException { + + Set<String> classNameSet; + long startTime; classNameSet = new HashSet<>(misMatchedMap.keySet()); // We need to keep track of how long the audit is taking startTime = AuditorTime.getInstance().getMillis(); @@ -267,53 +314,8 @@ public class DbAudit { 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() + COMMA_RESOURCE_NAME - + iae.getResourceName()); - } - continue; // no need to compare with self - } else { - if (logger.isDebugEnabled()) { - logger.debug("dbAudit: Second comparison; Id=" + iae.getId() + COMMA_RESOURCE_NAME - + 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 - Map<Object, Object> 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. - */ - Set<Object> 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)); - } - } - } - } + errorCount += recompareMineWithTheirs(resourceName, persistenceUnit, iaeList, myIae, clazzName, + keySet, myEntries); // Time check if ((AuditorTime.getInstance().getMillis() - startTime) >= DB_AUDIT_UPDATE_MS) { // update the timestamp @@ -337,12 +339,61 @@ public class DbAudit { + " 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"); + private int recompareMineWithTheirs(String resourceName, String persistenceUnit, List<IntegrityAuditEntity> iaeList, + IntegrityAuditEntity myIae, String clazzName, Set<Object> keySet, Map<Object, Object> myEntries) + throws IntegrityAuditException { + + int errorCount = 0; + for (IntegrityAuditEntity iae : iaeList) { + if (iae.getId() == myIae.getId()) { + if (logger.isDebugEnabled()) { + logger.debug("dbAudit: Second comparison; My Id=" + iae.getId() + COMMA_RESOURCE_NAME + + iae.getResourceName()); + } + continue; // no need to compare with self + } + + if (logger.isDebugEnabled()) { + logger.debug("dbAudit: Second comparison; Id=" + iae.getId() + COMMA_RESOURCE_NAME + + iae.getResourceName()); + } + + // get a map of the instances for their iae indexed by id + Map<Object, Object> theirEntries = + dbDao.getAllEntries(persistenceUnit, getTheirDaoProperties(iae), 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. + */ + errorCount += recompareMineWithTheirs(resourceName, clazzName, myEntries, iae, theirEntries); } + return errorCount; + } - return; // all done + private int recompareMineWithTheirs(String resourceName, String clazzName, Map<Object, Object> myEntries, + IntegrityAuditEntity iae, Map<Object, Object> theirEntries) throws IntegrityAuditException { + Set<Object> misMatchedKeySet = compareEntries(myEntries, theirEntries); + if (misMatchedKeySet.isEmpty()) { + return 0; + } + + StringBuilder keyBuilder = new StringBuilder(); + for (Object key : misMatchedKeySet) { + keyBuilder.append(key.toString()); + keyBuilder.append(", "); + } + writeAuditSummaryLog(clazzName, resourceName, iae.getResourceName(), keyBuilder.toString()); + if (logger.isDebugEnabled()) { + for (Object key : misMatchedKeySet) { + writeAuditDebugLog(clazzName, resourceName, iae.getResourceName(), myEntries.get(key), + theirEntries.get(key)); + } + } + return misMatchedKeySet.size(); } /** 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 index 08243310..4833733c 100644 --- 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 @@ -7,9 +7,9 @@ * 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. @@ -29,7 +29,6 @@ import java.util.Set; 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; @@ -45,11 +44,11 @@ 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; @@ -65,7 +64,7 @@ public class DbDao { * Supports designation serialization. */ private static final Object lock = new Object(); - + /* * Common strings. */ @@ -73,7 +72,7 @@ public class DbDao { private static final String WITH_PERSISTENCE_MESSAGE = " with PersistenceUnit: "; private static final String DBDAO_MESSAGE = "DbDao: "; private static final String ENCOUNTERED_MESSAGE = "ecountered a problem in execution: "; - + /* * DB SELECT String. */ @@ -82,7 +81,7 @@ public class DbDao { /** * DbDao Constructor. - * + * * @param resourceName the resource name * @param persistenceUnit the persistence unit * @param properties the properties @@ -94,7 +93,7 @@ public class DbDao { /** * DbDao Constructor. - * + * * @param resourceName the resource name * @param persistenceUnit the persistence unit * @param properties the properties @@ -124,7 +123,7 @@ public class DbDao { /** * validateProperties will validate the properties. - * + * * @param resourceName the rseource name * @param persistenceUnit the persistence unit * @param properties the properties @@ -151,7 +150,7 @@ public class DbDao { /** * getAllMyEntries gets all the DB entries for a particular class. - * + * * @param className the class name * @return all the DB entries for the given class */ @@ -183,7 +182,7 @@ public class DbDao { /** * getAllMyEntries gets all entries for a class. - * + * * @param className the name of the class * @param keySet the keys to get the entries for * @return the map of requested entries @@ -210,7 +209,7 @@ public class DbDao { /** * getAllEntries gets all entriesfor a particular persistence unit adn className. - * + * * @param persistenceUnit the persistence unit * @param properties the properties * @param className the class name @@ -250,7 +249,7 @@ public class DbDao { /** * getAllEntries gets all entries for a persistence unit. - * + * * @param persistenceUnit the persistence unit * @param properties the properties * @param className the class name @@ -284,7 +283,7 @@ public class DbDao { /** * getIntegrityAuditEntities() Get all the IntegrityAuditEntities for a particular persistence * unit and node type. - * + * * @param persistenceUnit the persistence unit * @param nodeType the node type * @return the list of IntegrityAuditEntity @@ -326,7 +325,7 @@ public class DbDao { /** * getMyIntegrityAuditEntity() gets my IntegrityAuditEntity. - * + * * @return the IntegrityAuditEntity * @throws DbDaoTransactionException if an error occurs */ @@ -378,7 +377,7 @@ public class DbDao { /** * getIntegrityAuditEntity() gets the IntegrityAuditEntity with a particular ID. - * + * * @param id the ID * @return the IntegrityAuditEntity * @throws DbDaoTransactionException if an error occurs @@ -407,7 +406,7 @@ public class DbDao { /** * getPersistenceClassNames() gets all the persistence class names. - * + * * @return the persistence class names */ public Set<String> getPersistenceClassNames() { @@ -426,7 +425,7 @@ public class DbDao { /** * Register the IntegrityAudit instance. - * + * * @param altDbUrl alternate DB URL to be placed into the record, or {@code null} to use the * default */ @@ -497,7 +496,7 @@ public class DbDao { /** * Set designated. - * + * * @param resourceName the resource name * @param persistenceUnit the persistence unit * @param desig true if is designated @@ -559,7 +558,7 @@ public class DbDao { /** * Set last updated. - * + * * @throws DbDaoTransactionException if an error occurs */ public void setLastUpdated() throws DbDaoTransactionException { @@ -659,20 +658,20 @@ public class DbDao { /** * Changes designation to specified resourceName - * + * * <p>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. - * + * * <p>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. - * + * * <p>See http://www.objectdb.com/api/java/jpa/LockModeType - * + * * <p>and - * + * * <p>http://stackoverflow.com/questions/2120248/how-to-synchronize-a-static- * variable-among-threads-running-different-instances-o */ @@ -706,26 +705,7 @@ public class DbDao { * 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<IntegrityAuditEntity> integrityAuditEntityList = 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); - } - } - } + setDesignatedEntity(resourceName, query); if (logger.isDebugEnabled()) { logger.debug("changeDesignated: Committing designation to resourceName=" + resourceName); @@ -739,20 +719,6 @@ public class DbDao { * read/update are pretty slim (we're only in this method for two or three * milliseconds) */ - } catch (LockTimeoutException e) { - if (em != null) { - em.getTransaction().rollback(); - - String msg = "DbDao: changeDesignated() caught LockTimeoutException, message=" - + e.getMessage(); - logger.error(msg + e); - throw new DbDaoTransactionException(msg, e); - } else { - String msg = "DbDao: changeDesignated() caught LockTimeoutException, message=" - + e.getMessage() + ". Error rolling back transaction."; - logger.error(msg + e); - throw new DbDaoTransactionException(msg, e); - } } catch (Exception e) { if (em != null) { em.getTransaction().rollback(); @@ -777,4 +743,27 @@ public class DbDao { } + private void setDesignatedEntity(String resourceName, Query query) { + for (Object o : query.getResultList()) { + if (!(o instanceof IntegrityAuditEntity)) { + continue; + } + + 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); + } + } + } + } 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 index d50c7059..02e4c70b 100644 --- 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 @@ -2,14 +2,14 @@ * ============LICENSE_START======================================================= * Integrity Audit * ================================================================================ - * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2019 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. @@ -21,6 +21,7 @@ package org.onap.policy.common.ia; import java.util.Properties; +import org.apache.commons.lang3.StringUtils; import org.onap.policy.common.ia.IntegrityAuditProperties.NodeTypeEnum; import org.onap.policy.common.logging.flexlogger.FlexLogger; import org.onap.policy.common.logging.flexlogger.Logger; @@ -46,18 +47,18 @@ public class IntegrityAudit { /* * This is the audit period in milliseconds. For example, if it had a value of 3600000, the * audit can only run once per hour. If it has a value of 6000, it can run once per minute. - * + * * Values: integrityAuditPeriodSeconds < 0 (negative number) indicates the audit is off * integrityAuditPeriodSeconds == 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 the resource name * @param persistenceUnit the persistence unit * @param properties the properties @@ -118,100 +119,97 @@ public class IntegrityAudit { public static boolean parmsAreBad(String resourceName, String persistenceUnit, Properties properties, StringBuilder badparams) { - boolean parmsAreBad = false; - - if (resourceName == null || resourceName.isEmpty()) { - badparams.append("resourceName "); - parmsAreBad = true; - } - - if (persistenceUnit == null || persistenceUnit.isEmpty()) { - badparams.append("persistenceUnit "); - parmsAreBad = true; - } + boolean parmsAreBad = checkEmpty(badparams, "resourceName", resourceName); + parmsAreBad = checkEmpty(badparams, "persistenceUnit", persistenceUnit) || parmsAreBad; if (properties == null || properties.isEmpty()) { badparams.append("properties "); parmsAreBad = true; } else { - String dbDriver = properties.getProperty(IntegrityAuditProperties.DB_DRIVER); - if (dbDriver == null || dbDriver.isEmpty()) { - badparams.append("dbDriver "); - parmsAreBad = true; - } + parmsAreBad = checkProperties(properties, badparams) || parmsAreBad; + } // End else + logger.debug("parmsAreBad: exit:" + "\nresourceName: " + resourceName + "\npersistenceUnit: " + persistenceUnit + + "\nproperties: " + properties); - String dbUrl = properties.getProperty(IntegrityAuditProperties.DB_URL); - if (dbUrl == null || dbUrl.isEmpty()) { - badparams.append("dbUrl "); - parmsAreBad = true; - } + return parmsAreBad; + } - String dbUser = properties.getProperty(IntegrityAuditProperties.DB_USER); - if (dbUser == null || dbUser.isEmpty()) { - badparams.append("dbUser "); - parmsAreBad = true; - } + private static boolean checkEmpty(StringBuilder builder, String name, String value) { + if (StringUtils.isEmpty(value)) { + builder.append(name); + builder.append(' '); + return true; - String dbPwd = properties.getProperty(IntegrityAuditProperties.DB_PWD); - if (dbPwd == null) { // may be empty - badparams.append("dbPwd "); - parmsAreBad = true; - } + } else { + return false; + } + } - String siteName = properties.getProperty(IntegrityAuditProperties.SITE_NAME); - if (siteName == null || siteName.isEmpty()) { - badparams.append("siteName "); - parmsAreBad = true; - } + private static boolean checkProperties(Properties properties, StringBuilder badparams) { + boolean parmsAreBad = + checkEmpty(badparams, "dbDriver", properties.getProperty(IntegrityAuditProperties.DB_DRIVER)); + parmsAreBad = checkEmpty(badparams, "dbUrl", properties.getProperty(IntegrityAuditProperties.DB_URL)) + || parmsAreBad; + parmsAreBad = checkEmpty(badparams, "dbUser", properties.getProperty(IntegrityAuditProperties.DB_USER)) + || parmsAreBad; + parmsAreBad = checkEmpty(badparams, "dbPwd", properties.getProperty(IntegrityAuditProperties.DB_PWD)) + || parmsAreBad; + parmsAreBad = checkEmpty(badparams, "siteName", properties.getProperty(IntegrityAuditProperties.SITE_NAME)) + || parmsAreBad; + parmsAreBad = checkNodeType(properties, badparams) || parmsAreBad; + + return checkAuditPeriod(properties, badparams) || parmsAreBad; + } - String nodeType = properties.getProperty(IntegrityAuditProperties.NODE_TYPE); - if (nodeType == null || nodeType.isEmpty()) { - badparams.append("nodeType "); - parmsAreBad = true; - } else { - nodeType = nodeType.trim(); - if (!isNodeTypeEnum(nodeType)) { - String nodetypes = "nodeType must be one of["; - for (NodeTypeEnum n : NodeTypeEnum.values()) { - nodetypes = nodetypes.concat(n.toString() + " "); - } - badparams.append(nodetypes + "] "); - parmsAreBad = true; - } - } - // IntegrityAuditProperties.AUDIT_PERIOD_SECONDS and - // IntegrityAuditProperties.AUDIT_PERIOD_MILLISECONDS are allowed to be null - if (properties.getProperty(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS) != null) { - try { - Integer.parseInt(properties.getProperty(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS).trim()); - } catch (NumberFormatException nfe) { - badparams.append(", auditPeriodSeconds=" - + properties.getProperty(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS).trim()); - parmsAreBad = true; + private static boolean checkNodeType(Properties properties, StringBuilder badparams) { + String nodeType = properties.getProperty(IntegrityAuditProperties.NODE_TYPE); + if (nodeType == null || nodeType.isEmpty()) { + badparams.append("nodeType "); + return true; + } else { + nodeType = nodeType.trim(); + if (!isNodeTypeEnum(nodeType)) { + String nodetypes = "nodeType must be one of["; + for (NodeTypeEnum n : NodeTypeEnum.values()) { + nodetypes = nodetypes.concat(n.toString() + " "); } + badparams.append(nodetypes + "] "); + return true; } - } // End else - logger.debug("parmsAreBad: exit:" + "\nresourceName: " + resourceName + "\npersistenceUnit: " + persistenceUnit - + "\nproperties: " + properties); + } + return false; + } - return parmsAreBad; + private static boolean checkAuditPeriod(Properties properties, StringBuilder badparams) { + // IntegrityAuditProperties.AUDIT_PERIOD_SECONDS and + // IntegrityAuditProperties.AUDIT_PERIOD_MILLISECONDS are allowed to be null + if (properties.getProperty(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS) != null) { + try { + Integer.parseInt(properties.getProperty(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS).trim()); + } catch (NumberFormatException nfe) { + badparams.append(", auditPeriodSeconds=" + + properties.getProperty(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS).trim()); + return true; + } + } + return false; } /** * Starts the audit thread. - * + * * @throws IntegrityAuditException if an error occurs */ public void startAuditThread() throws IntegrityAuditException { logger.info("startAuditThread: Entering"); if (integrityAuditPeriodSeconds >= 0) { - this.auditThread = makeAuditThread(this.resourceName, this.persistenceUnit, + this.auditThread = makeAuditThread(this.resourceName, this.persistenceUnit, this.properties, integrityAuditPeriodSeconds); - logger.info("startAuditThread: Audit started and will run every " + integrityAuditPeriodSeconds + logger.info("startAuditThread: Audit started and will run every " + integrityAuditPeriodSeconds + " seconds"); this.auditThread.start(); - + } else { logger.info("startAuditThread: Suppressing integrity audit, integrityAuditPeriodSeconds=" + integrityAuditPeriodSeconds); @@ -255,7 +253,7 @@ public class IntegrityAudit { /** * Waits a bit for the AuditThread to complete. Used by JUnit tests. - * + * * @param twaitms wait time, in milliseconds * @return {@code true} if the thread stopped within the given time, {@code false} otherwise * @throws InterruptedException if the thread is interrupted @@ -272,7 +270,7 @@ public class IntegrityAudit { /** * Return if audit thread. - * + * * @return {@code true} if an audit thread exists, {@code false} otherwise */ protected boolean haveAuditThread() { @@ -281,12 +279,12 @@ public class IntegrityAudit { /** * Creates an audit thread. May be overridden by junit tests. - * + * * @param resourceName2 the resource name * @param persistenceUnit2 the persistence unit * @param properties2 properties * @param integrityAuditPeriodSeconds2 - * + * * @return a new audit thread * @throws IntegrityAuditException audit exception */ 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 index 3afab61f..2ca5dd58 100644 --- 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 @@ -2,14 +2,14 @@ * ============LICENSE_START======================================================= * Integrity Audit * ================================================================================ - * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2019 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. |