From d5c365f60e492e37a361654974eba1719146bdc2 Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Mon, 31 Aug 2020 08:22:13 -0400 Subject: Fix more sonars in drools-pdp Fixed more sonars in drools-pdp: - remove commented code - don't throw generic Exception - unused field (made it protected instead of private) - log conditionally - cognitive complexity - too many break/continue - return empty list instead of null - Random() is not secure Fixed more eclipse warnings: - parameterize generic types Issue-ID: POLICY-2616-sonars3 Change-Id: Ia5ad769b2ea763568cfae3d81807926d89153b09 Signed-off-by: Jim Hahn --- .../org/onap/policy/drools/serverpool/Bucket.java | 6 +- .../org/onap/policy/drools/serverpool/Server.java | 7 +-- .../policy/drools/serverpool/ServerPoolApi.java | 3 +- .../drools/serverpool/persistence/Persistence.java | 73 ++++++++++++---------- .../onap/policy/drools/serverpool/AdapterImpl.java | 8 ++- 5 files changed, 55 insertions(+), 42 deletions(-) (limited to 'feature-server-pool') diff --git a/feature-server-pool/src/main/java/org/onap/policy/drools/serverpool/Bucket.java b/feature-server-pool/src/main/java/org/onap/policy/drools/serverpool/Bucket.java index 6241a297..3284a6b7 100644 --- a/feature-server-pool/src/main/java/org/onap/policy/drools/serverpool/Bucket.java +++ b/feature-server-pool/src/main/java/org/onap/policy/drools/serverpool/Bucket.java @@ -783,7 +783,11 @@ public class Bucket { * current host. */ UUID key = oldHost.uuid; - for (int count = new Random().nextInt(rb.testServers.size() - 1); count >= 0; count -= 1) { + /* + * Disabling sonar, because this Random() is not used for security purposes. + */ + int randomStart = new Random().nextInt(rb.testServers.size() - 1); // NOSONAR + for (int count = randomStart; count >= 0; count -= 1) { key = rb.testServers.higherKey(key); if (key == null) { // wrap to the beginning of the list diff --git a/feature-server-pool/src/main/java/org/onap/policy/drools/serverpool/Server.java b/feature-server-pool/src/main/java/org/onap/policy/drools/serverpool/Server.java index 80b5891a..ffddf0a0 100644 --- a/feature-server-pool/src/main/java/org/onap/policy/drools/serverpool/Server.java +++ b/feature-server-pool/src/main/java/org/onap/policy/drools/serverpool/Server.java @@ -334,11 +334,8 @@ public class Server implements Comparable { // add any additional servlets for (ServerPoolApi feature : ServerPoolApi.impl.getList()) { - Collection> classes = feature.servletClasses(); - if (classes != null) { - for (Class clazz : classes) { - restServer.addServletClass(null, clazz.getName()); - } + for (Class clazz : feature.servletClasses()) { + restServer.addServletClass(null, clazz.getName()); } } diff --git a/feature-server-pool/src/main/java/org/onap/policy/drools/serverpool/ServerPoolApi.java b/feature-server-pool/src/main/java/org/onap/policy/drools/serverpool/ServerPoolApi.java index d267ce82..0442912f 100644 --- a/feature-server-pool/src/main/java/org/onap/policy/drools/serverpool/ServerPoolApi.java +++ b/feature-server-pool/src/main/java/org/onap/policy/drools/serverpool/ServerPoolApi.java @@ -21,6 +21,7 @@ package org.onap.policy.drools.serverpool; import java.util.Collection; +import java.util.Collections; import org.onap.policy.common.utils.services.OrderedService; import org.onap.policy.common.utils.services.OrderedServiceImpl; @@ -39,7 +40,7 @@ public interface ServerPoolApi extends OrderedService { * @return a Collection of classes implementing REST methods */ public default Collection> servletClasses() { - return null; + return Collections.emptyList(); } /** diff --git a/feature-server-pool/src/main/java/org/onap/policy/drools/serverpool/persistence/Persistence.java b/feature-server-pool/src/main/java/org/onap/policy/drools/serverpool/persistence/Persistence.java index e8121f3a..c1d7192f 100644 --- a/feature-server-pool/src/main/java/org/onap/policy/drools/serverpool/persistence/Persistence.java +++ b/feature-server-pool/src/main/java/org/onap/policy/drools/serverpool/persistence/Persistence.java @@ -31,6 +31,7 @@ import java.util.IdentityHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.Map.Entry; import java.util.Properties; import java.util.Set; import java.util.UUID; @@ -698,47 +699,53 @@ public class Persistence implements PolicySessionFeatureApi, ServerPoolApi { private List restoreBucketDroolsSessions() { List sessionLatches = new LinkedList<>(); for (Map.Entry entry : sessionData.entrySet()) { - String sessionName = entry.getKey(); - ReceiverSessionBucketData rsbd = entry.getValue(); + restoreBucketDroolsSession(sessionLatches, entry); + } + return sessionLatches; + } - PolicySession policySession = detmPolicySession(sessionName); - if (policySession == null) { - logger.error(RESTORE_BUCKET_ERROR - + "Can't find PolicySession{}", sessionName); - continue; - } + private void restoreBucketDroolsSession(List sessionLatches, + Entry entry) { - final Map droolsObjects = deserializeMap(sessionName, rsbd, policySession); - if (droolsObjects == null) { - continue; - } + String sessionName = entry.getKey(); + ReceiverSessionBucketData rsbd = entry.getValue(); - // if we reach this point, we have decoded the persistent data + PolicySession policySession = detmPolicySession(sessionName); + if (policySession == null) { + logger.error(RESTORE_BUCKET_ERROR + + "Can't find PolicySession{}", sessionName); + return; + } - // signal when restore is complete - final CountDownLatch sessionLatch = new CountDownLatch(1); + final Map droolsObjects = deserializeMap(sessionName, rsbd, policySession); + if (droolsObjects == null) { + return; + } - // 'KieSession' object - final KieSession kieSession = policySession.getKieSession(); + // if we reach this point, we have decoded the persistent data - // run the following within the Drools session thread - DroolsRunnable insertDroolsObjects = () -> { - try { - // insert all of the Drools objects into the session - for (Object droolsObj : droolsObjects.keySet()) { - kieSession.insert(droolsObj); - } - } finally { - // signal completion - sessionLatch.countDown(); + // signal when restore is complete + final CountDownLatch sessionLatch = new CountDownLatch(1); + + // 'KieSession' object + final KieSession kieSession = policySession.getKieSession(); + + // run the following within the Drools session thread + DroolsRunnable insertDroolsObjects = () -> { + try { + // insert all of the Drools objects into the session + for (Object droolsObj : droolsObjects.keySet()) { + kieSession.insert(droolsObj); } - }; - kieSession.insert(insertDroolsObjects); + } finally { + // signal completion + sessionLatch.countDown(); + } + }; + kieSession.insert(insertDroolsObjects); - // add this to the set of 'CountDownLatch's we are waiting for - sessionLatches.add(sessionLatch); - } - return sessionLatches; + // add this to the set of 'CountDownLatch's we are waiting for + sessionLatches.add(sessionLatch); } private PolicySession detmPolicySession(String sessionName) { diff --git a/feature-server-pool/src/test/java/org/onap/policy/drools/serverpool/AdapterImpl.java b/feature-server-pool/src/test/java/org/onap/policy/drools/serverpool/AdapterImpl.java index 044067a3..865f4e90 100644 --- a/feature-server-pool/src/test/java/org/onap/policy/drools/serverpool/AdapterImpl.java +++ b/feature-server-pool/src/test/java/org/onap/policy/drools/serverpool/AdapterImpl.java @@ -55,8 +55,12 @@ public class AdapterImpl extends Adapter { * Each 'AdapterImpl' instance has it's own class object, making it a * singleton. There is only a single 'Adapter' class object, and all * 'AdapterImpl' classes are derived from it. + * + * Sonar thinks this field isn't used. However, it's value is actually + * retrieved via Whitebox, below. Thus it is marked "protected" instead + * of "private" to avoid the sonar complaint. */ - private static AdapterImpl adapter = null; + protected static AdapterImpl adapter = null; // this is the adapter index private int index; @@ -347,7 +351,7 @@ public class AdapterImpl extends Adapter { boolean rval = false; ClassLoader myClassLoader = AdapterImpl.class.getClassLoader(); for (Object o : objects) { - Class clazz = o.getClass(); + Class clazz = o.getClass(); ClassLoader objClassLoader = clazz.getClassLoader(); try { -- cgit 1.2.3-korg