diff options
author | Jim Hahn <jrh3@att.com> | 2021-08-26 14:36:35 -0400 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2021-08-26 15:12:19 -0400 |
commit | d04af420514c9c852af77780f0bd5ee23ebb401a (patch) | |
tree | 4212f3e411714770bd4bae3ecb25634190868b13 /feature-active-standby-management/src/main/java/org | |
parent | 9869e3a9665c0215dc9f9724fe3b5576c0479157 (diff) |
Fix sonars in drools-pdp
Fixed sonars:
- use "var"
- override "equals"
- unused imports
Also changed some of the JPA queries to make them typed queries.
Issue-ID: POLICY-3289
Change-Id: Ib3a00be322d8d00ba492673708439cacfe2c7fc4
Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'feature-active-standby-management/src/main/java/org')
7 files changed, 59 insertions, 69 deletions
diff --git a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/ActiveStandbyFeature.java b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/ActiveStandbyFeature.java index bd3f0215..e3125a5f 100644 --- a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/ActiveStandbyFeature.java +++ b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/ActiveStandbyFeature.java @@ -23,8 +23,6 @@ package org.onap.policy.drools.activestandby; import java.io.IOException; import java.util.HashMap; import java.util.Map; -import java.util.Properties; -import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; import org.eclipse.persistence.config.PersistenceUnitProperties; import org.onap.policy.common.im.MonitorTime; @@ -107,7 +105,7 @@ public class ActiveStandbyFeature implements ActiveStandbyFeatureApi, //Create an instance of the Observer - PmStandbyStateChangeNotifier pmNotifier = new PmStandbyStateChangeNotifier(); + var pmNotifier = new PmStandbyStateChangeNotifier(); //Register the PMStandbyStateChangeNotifier Observer stateManagementFeature.addObserver(pmNotifier); @@ -132,7 +130,7 @@ public class ActiveStandbyFeature implements ActiveStandbyFeatureApi, private static void initializePersistence(String configDir) { //Get the Active Standby properties try { - Properties activeStandbyProperties = + var activeStandbyProperties = PropertyUtil.getProperties(configDir + "/feature-active-standby-management.properties"); ActiveStandbyProperties.initProperties(activeStandbyProperties); logger.info("initializePersistence: ActiveStandbyProperties success"); @@ -140,7 +138,7 @@ public class ActiveStandbyFeature implements ActiveStandbyFeatureApi, logger.error("ActiveStandbyFeature: initializePersistence ActiveStandbyProperties", e); } - DroolsPdpsConnector conn = getDroolsPdpsConnector("activeStandbyPU"); + var conn = getDroolsPdpsConnector("activeStandbyPU"); String resourceName = ActiveStandbyProperties.getProperty(ActiveStandbyProperties.NODE_NAME); if (resourceName == null) { throw new NullPointerException(); @@ -199,8 +197,7 @@ public class ActiveStandbyFeature implements ActiveStandbyFeatureApi, propMap.put(PersistenceUnitProperties.TARGET_DATABASE, ActiveStandbyProperties.getProperty(ActiveStandbyProperties.DB_TYPE)); - EntityManagerFactory emf = Persistence.createEntityManagerFactory( - pu, propMap); + var emf = Persistence.createEntityManagerFactory(pu, propMap); return new JpaDroolsPdpsConnector(emf); } diff --git a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpEntity.java b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpEntity.java index cf610d9d..526200fc 100644 --- a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpEntity.java +++ b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpEntity.java @@ -28,6 +28,7 @@ import javax.persistence.Id; import javax.persistence.NamedQuery; import javax.persistence.Temporal; import javax.persistence.TemporalType; +import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.Setter; import org.onap.policy.common.im.MonitorTime; @@ -39,6 +40,7 @@ import org.onap.policy.common.im.MonitorTime; @NamedQuery(name = "DroolsPdpEntity.deleteAll", query = "DELETE FROM DroolsPdpEntity WHERE 1=1") @Getter @Setter +@EqualsAndHashCode(onlyExplicitlyIncluded = true, callSuper = true) public class DroolsPdpEntity extends DroolsPdpObject implements Serializable { private static final long serialVersionUID = 1L; diff --git a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpImpl.java b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpImpl.java index f86b1a59..dac35ea3 100644 --- a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpImpl.java +++ b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpImpl.java @@ -21,11 +21,13 @@ package org.onap.policy.drools.activestandby; import java.util.Date; +import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.Setter; @Getter @Setter +@EqualsAndHashCode(onlyExplicitlyIncluded = true, callSuper = true) public class DroolsPdpImpl extends DroolsPdpObject { private boolean designated; diff --git a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpObject.java b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpObject.java index fd25a6d3..c8971130 100644 --- a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpObject.java +++ b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpObject.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * feature-active-standby-management * ================================================================================ - * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2019, 2021 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. @@ -34,8 +34,8 @@ public abstract class DroolsPdpObject implements DroolsPdp { @Override public int hashCode() { - final int prime = 31; - int result = 1; + final var prime = 31; + var result = 1; result = prime * result + (this.getPdpId() == null ? 0 : this.getPdpId().hashCode()); return result; } diff --git a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpsElectionHandler.java b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpsElectionHandler.java index 8e6b5ef9..ff4b21df 100644 --- a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpsElectionHandler.java +++ b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpsElectionHandler.java @@ -25,7 +25,6 @@ import java.util.Collection; import java.util.Date; import java.util.List; import java.util.Objects; -import java.util.Timer; import java.util.TimerTask; import lombok.Getter; import lombok.Setter; @@ -104,7 +103,7 @@ public class DroolsPdpsElectionHandler implements ThreadRunningChecker { this.isDesignated = false; // The interval between checks of the DesignationWaiter to be sure it is running. - int pdpCheckInterval = 3000; + var pdpCheckInterval = 3000; try { pdpCheckInterval = Integer.parseInt(ActiveStandbyProperties.getProperty( ActiveStandbyProperties.PDP_CHECK_INVERVAL)); @@ -119,14 +118,14 @@ public class DroolsPdpsElectionHandler implements ThreadRunningChecker { logger.error("Could not get pdpUpdateInterval property. Using default {} ", pdpUpdateInterval, e); } - Date now = currentTime.getDate(); + var now = currentTime.getDate(); // Retrieve the ms since the epoch final long nowMs = now.getTime(); // Create the timer which will update the updateDate in DroolsPdpEntity table. // This is the heartbeat - Timer updateWorker = Factory.getInstance().makeTimer(); + var updateWorker = Factory.getInstance().makeTimer(); // Schedule the TimerUpdateClass to run at 100 ms and run at pdpCheckInterval ms thereafter // NOTE: The first run of the TimerUpdateClass results in myPdp being added to the @@ -134,12 +133,12 @@ public class DroolsPdpsElectionHandler implements ThreadRunningChecker { updateWorker.scheduleAtFixedRate(new TimerUpdateClass(), 100, pdpCheckInterval); // Create the timer which will run the election algorithm - Timer waitTimer = Factory.getInstance().makeTimer(); + var waitTimer = Factory.getInstance().makeTimer(); // Schedule it to start in startMs ms // (so it will run after the updateWorker and run at pdpUpdateInterval ms thereafter long startMs = getDWaiterStartMs(); - DesignationWaiter designationWaiter = new DesignationWaiter(); + var designationWaiter = new DesignationWaiter(); waitTimer.scheduleAtFixedRate(designationWaiter, startMs, pdpUpdateInterval); waitTimerLastRunDate = new Date(nowMs + startMs); @@ -267,7 +266,7 @@ public class DroolsPdpsElectionHandler implements ThreadRunningChecker { logger.debug("DesignatedWaiter.run: myPdp: {}; Returning, isDesignated= {}", isDesignated, myPdp.getPdpId()); - Date tmpDate = currentTime.getDate(); + var tmpDate = currentTime.getDate(); logger.debug("DesignatedWaiter.run (end of run) waitTimerLastRunDate = {}", tmpDate); waitTimerLastRunDate = tmpDate; @@ -631,8 +630,8 @@ public class DroolsPdpsElectionHandler implements ThreadRunningChecker { */ public List<DroolsPdp> santizeDesignatedList(List<DroolsPdp> listOfDesignated) { - boolean containsDesignated = false; - boolean containsHotStandby = false; + var containsDesignated = false; + var containsHotStandby = false; List<DroolsPdp> listForRemoval = new ArrayList<>(); for (DroolsPdp pdp : listOfDesignated) { logger.debug("DesignatedWaiter.run sanitizing: pdp = {}" @@ -768,7 +767,7 @@ public class DroolsPdpsElectionHandler implements ThreadRunningChecker { logger.debug("DesignatedWaiter.run: myPdp: {} listOfDesignated.size(): {}", myPdp.getPdpId(), listOfDesignated.size()); - DesignatedData data = new DesignatedData(); + var data = new DesignatedData(); for (DroolsPdp pdp : listOfDesignated) { DroolsPdp rejectedPdp; @@ -922,7 +921,7 @@ public class DroolsPdpsElectionHandler implements ThreadRunningChecker { synchronized (checkWaitTimerLock) { try { logger.debug("checkWaitTimer: entry"); - Date now = currentTime.getDate(); + var now = currentTime.getDate(); long nowMs = now.getTime(); long waitTimerMs = waitTimerLastRunDate.getTime(); @@ -953,7 +952,7 @@ public class DroolsPdpsElectionHandler implements ThreadRunningChecker { } private long getDWaiterStartMs() { - Date now = currentTime.getDate(); + var now = currentTime.getDate(); // Retrieve the ms since the epoch long nowMs = now.getTime(); diff --git a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/JpaDroolsPdpsConnector.java b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/JpaDroolsPdpsConnector.java index 471ef49b..39386d5e 100644 --- a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/JpaDroolsPdpsConnector.java +++ b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/JpaDroolsPdpsConnector.java @@ -21,14 +21,12 @@ package org.onap.policy.drools.activestandby; import java.util.Collection; -import java.util.Date; import java.util.LinkedList; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.FlushModeType; import javax.persistence.LockModeType; -import javax.persistence.Query; import lombok.AllArgsConstructor; import org.onap.policy.common.im.MonitorTime; import org.onap.policy.common.utils.time.CurrentTime; @@ -50,19 +48,15 @@ public class JpaDroolsPdpsConnector implements DroolsPdpsConnector { @Override public Collection<DroolsPdp> getDroolsPdps() { //return a list of all the DroolsPdps in the database - EntityManager em = emf.createEntityManager(); + var em = emf.createEntityManager(); try { em.getTransaction().begin(); - Query droolsPdpsListQuery = em.createQuery("SELECT p FROM DroolsPdpEntity p"); - List<?> droolsPdpsList = droolsPdpsListQuery.setLockMode(LockModeType.NONE) + var droolsPdpsListQuery = em.createQuery("SELECT p FROM DroolsPdpEntity p", DroolsPdp.class); + List<DroolsPdp> droolsPdpsList = droolsPdpsListQuery.setLockMode(LockModeType.NONE) .setFlushMode(FlushModeType.COMMIT).getResultList(); LinkedList<DroolsPdp> droolsPdpsReturnList = new LinkedList<>(); - for (Object o : droolsPdpsList) { - if (!(o instanceof DroolsPdp)) { - continue; - } + for (DroolsPdp droolsPdp : droolsPdpsList) { //Make sure it is not a cached version - DroolsPdp droolsPdp = (DroolsPdp) o; em.refresh(droolsPdp); droolsPdpsReturnList.add(droolsPdp); if (logger.isDebugEnabled()) { @@ -100,18 +94,18 @@ public class JpaDroolsPdpsConnector implements DroolsPdpsConnector { logger.debug("update: Entering, pdpId={}", pdp.getPdpId()); //this is to update our own pdp in the database - EntityManager em = emf.createEntityManager(); + var em = emf.createEntityManager(); try { em.getTransaction().begin(); - Query droolsPdpsListQuery = em.createQuery(SELECT_PDP_BY_ID); + var droolsPdpsListQuery = em.createQuery(SELECT_PDP_BY_ID, DroolsPdpEntity.class); droolsPdpsListQuery.setParameter(PDP_ID_PARAM, pdp.getPdpId()); - List<?> droolsPdpsList = droolsPdpsListQuery.setLockMode(LockModeType.NONE) + List<DroolsPdpEntity> droolsPdpsList = droolsPdpsListQuery.setLockMode(LockModeType.NONE) .setFlushMode(FlushModeType.COMMIT).getResultList(); DroolsPdpEntity droolsPdpEntity; - if (droolsPdpsList.size() == 1 && (droolsPdpsList.get(0) instanceof DroolsPdpEntity)) { - droolsPdpEntity = (DroolsPdpEntity) droolsPdpsList.get(0); + if (droolsPdpsList.size() == 1) { + droolsPdpEntity = droolsPdpsList.get(0); em.refresh(droolsPdpEntity); //Make sure we have current values - Date currentDate = currentTime.getDate(); + var currentDate = currentTime.getDate(); long difference = currentDate.getTime() - droolsPdpEntity.getUpdatedDate().getTime(); //just set some kind of default here long pdpTimeout = 15000; @@ -175,18 +169,18 @@ public class JpaDroolsPdpsConnector implements DroolsPdpsConnector { boolean isCurrent = isCurrent(pdp); - EntityManager em = emf.createEntityManager(); + var em = emf.createEntityManager(); try { if (!isCurrent && pdp.isDesignated()) { em.getTransaction().begin(); - Query droolsPdpsListQuery = em.createQuery(SELECT_PDP_BY_ID); + var droolsPdpsListQuery = em.createQuery(SELECT_PDP_BY_ID, DroolsPdpEntity.class); droolsPdpsListQuery.setParameter(PDP_ID_PARAM, pdp.getPdpId()); - List<?> droolsPdpsList = droolsPdpsListQuery.setLockMode(LockModeType.NONE) + List<DroolsPdpEntity> droolsPdpsList = droolsPdpsListQuery.setLockMode(LockModeType.NONE) .setFlushMode(FlushModeType.COMMIT).getResultList(); - if (droolsPdpsList.size() == 1 && droolsPdpsList.get(0) instanceof DroolsPdpEntity) { + if (droolsPdpsList.size() == 1) { logger.debug("isPdpCurrent: PDP={} designated but not current; setting designated to false", pdp.getPdpId()); - DroolsPdpEntity droolsPdpEntity = (DroolsPdpEntity) droolsPdpsList.get(0); + var droolsPdpEntity = droolsPdpsList.get(0); droolsPdpEntity.setDesignated(false); em.getTransaction().commit(); } else { @@ -217,15 +211,13 @@ public class JpaDroolsPdpsConnector implements DroolsPdpsConnector { try { em = emf.createEntityManager(); em.getTransaction().begin(); - Query droolsPdpsListQuery = em - .createQuery(SELECT_PDP_BY_ID); + var droolsPdpsListQuery = em + .createQuery(SELECT_PDP_BY_ID, DroolsPdpEntity.class); droolsPdpsListQuery.setParameter(PDP_ID_PARAM, pdp.getPdpId()); - List<?> droolsPdpsList = droolsPdpsListQuery.setLockMode( + List<DroolsPdpEntity> droolsPdpsList = droolsPdpsListQuery.setLockMode( LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList(); - if (droolsPdpsList.size() == 1 - && droolsPdpsList.get(0) instanceof DroolsPdpEntity) { - DroolsPdpEntity droolsPdpEntity = (DroolsPdpEntity) droolsPdpsList - .get(0); + if (droolsPdpsList.size() == 1) { + var droolsPdpEntity = droolsPdpsList.get(0); logger.debug("setDesignated: PDP={}" + " found, designated= {}" @@ -275,15 +267,13 @@ public class JpaDroolsPdpsConnector implements DroolsPdpsConnector { * Get droolspdpentity record for this PDP and mark DESIGNATED as * false. */ - Query droolsPdpsListQuery = em - .createQuery(SELECT_PDP_BY_ID); + var droolsPdpsListQuery = em + .createQuery(SELECT_PDP_BY_ID, DroolsPdpEntity.class); droolsPdpsListQuery.setParameter(PDP_ID_PARAM, pdpId); - List<?> droolsPdpsList = droolsPdpsListQuery.setLockMode( + List<DroolsPdpEntity> droolsPdpsList = droolsPdpsListQuery.setLockMode( LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList(); - DroolsPdpEntity droolsPdpEntity; - if (droolsPdpsList.size() == 1 - && (droolsPdpsList.get(0) instanceof DroolsPdpEntity)) { - droolsPdpEntity = (DroolsPdpEntity) droolsPdpsList.get(0); + if (droolsPdpsList.size() == 1) { + var droolsPdpEntity = droolsPdpsList.get(0); droolsPdpEntity.setDesignated(false); em.persist(droolsPdpEntity); logger.debug("standDownPdp: PDP={} persisted as non-designated.", pdpId); @@ -324,8 +314,8 @@ public class JpaDroolsPdpsConnector implements DroolsPdpsConnector { logger.debug("hasDesignatedPdpFailed: Entering, pdps.size()={}", pdps.size()); - boolean failed = true; - boolean foundDesignatedPdp = false; + var failed = true; + var foundDesignatedPdp = false; for (DroolsPdp pdp : pdps) { @@ -360,13 +350,13 @@ public class JpaDroolsPdpsConnector implements DroolsPdpsConnector { logger.debug("isCurrent: Entering, pdpId={}", pdp.getPdpId()); - boolean current = false; + var current = false; // Return if the current PDP is considered "current" based on whatever // time box that may be. // If the the PDP is not current, we should mark it as not primary in // the database - Date currentDate = currentTime.getDate(); + var currentDate = currentTime.getDate(); long difference = currentDate.getTime() - pdp.getUpdatedDate().getTime(); // just set some kind of default here @@ -402,7 +392,7 @@ public class JpaDroolsPdpsConnector implements DroolsPdpsConnector { try { em = emf.createEntityManager(); em.getTransaction().begin(); - Query droolsPdpsListQuery = em + var droolsPdpsListQuery = em .createQuery(SELECT_PDP_BY_ID); droolsPdpsListQuery.setParameter(PDP_ID_PARAM, pdpId); List<?> droolsPdpsList = droolsPdpsListQuery.setLockMode( @@ -446,14 +436,14 @@ public class JpaDroolsPdpsConnector implements DroolsPdpsConnector { /* * Start transaction */ - EntityManager em = emf.createEntityManager(); + var em = emf.createEntityManager(); try { em.getTransaction().begin(); /* * Insert record. */ - DroolsPdpEntity droolsPdpEntity = new DroolsPdpEntity(); + var droolsPdpEntity = new DroolsPdpEntity(); em.persist(droolsPdpEntity); droolsPdpEntity.setPdpId(pdp.getPdpId()); droolsPdpEntity.setDesignated(pdp.isDesignated()); @@ -484,11 +474,11 @@ public class JpaDroolsPdpsConnector implements DroolsPdpsConnector { /* * Start transaction */ - EntityManager em = emf.createEntityManager(); + var em = emf.createEntityManager(); try { em.getTransaction().begin(); - Query droolsPdpsListQuery = em + var droolsPdpsListQuery = em .createQuery("SELECT p FROM DroolsPdpEntity p"); @SuppressWarnings("unchecked") List<DroolsPdp> droolsPdpsList = droolsPdpsListQuery.setLockMode( @@ -521,14 +511,14 @@ public class JpaDroolsPdpsConnector implements DroolsPdpsConnector { /* * Start transaction */ - EntityManager em = emf.createEntityManager(); + var em = emf.createEntityManager(); try { em.getTransaction().begin(); /* * Delete record. */ - DroolsPdpEntity droolsPdpEntity = em.find(DroolsPdpEntity.class, pdpId); + var droolsPdpEntity = em.find(DroolsPdpEntity.class, pdpId); if (droolsPdpEntity != null) { logger.debug("deletePdp: Removing PDP"); em.remove(droolsPdpEntity); diff --git a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/PmStandbyStateChangeNotifier.java b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/PmStandbyStateChangeNotifier.java index 776b70ee..1756246b 100644 --- a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/PmStandbyStateChangeNotifier.java +++ b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/PmStandbyStateChangeNotifier.java @@ -82,7 +82,7 @@ public class PmStandbyStateChangeNotifier extends StateChangeNotifier { * */ public PmStandbyStateChangeNotifier() { - int pdpUpdateInterval = + var pdpUpdateInterval = Integer.parseInt(ActiveStandbyProperties.getProperty(ActiveStandbyProperties.PDP_UPDATE_INTERVAL)); isWaitingForActivation = false; startTimeWaitingForActivationMs = currentTime.getMillis(); |