Yet more sonar issues in drools-applications 23/68523/7
authorJoshua Reich <jreich@research.att.com>
Fri, 21 Sep 2018 23:44:25 +0000 (16:44 -0700)
committerJoshua Reich <jreich@research.att.com>
Thu, 27 Sep 2018 19:53:38 +0000 (12:53 -0700)
removed unused imports and variable definitions
moved variable declarations above methods
refactored deeply nested if/while
removed code smell due to unecessary use of static keyword
moved string literals to left side of comparator
fixed logging of exceptions
immediately return expression instead of assigning it to the temporary variable

Change-Id: Idfd2a2bdf3c2f3bbfcb54a06b713d57bc867b3ab
Issue-ID: POLICY-1129
Signed-off-by: Joshua Reich <jreich@research.att.com>
controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/ControlLoopEventManager.java
controlloop/common/guard/src/main/java/org/onap/policy/guard/PipEngineGetStatus.java
controlloop/templates/template.demo.clc/src/main/java/org/onap/policy/guard/CallGuardTaskEmbedded.java
controlloop/templates/template.demo.clc/src/main/java/org/onap/policy/guard/PolicyGuardXacmlHelperEmbedded.java

index 4ab7b49..e9fc277 100644 (file)
@@ -499,9 +499,7 @@ public class ControlLoopEventManager implements LockCallback, Serializable {
                                                            this.currentOperation.getTargetEntity(),
                                                            this.onset.getRequestId(), this);
             this.targetLock = lock;
-            LockResult<GuardResult, TargetLock> lockResult = 
-                    LockResult.createLockResult(GuardResult.LOCK_ACQUIRED, lock);
-            return lockResult;
+            return LockResult.createLockResult(GuardResult.LOCK_ACQUIRED, lock);
         }
         //
         // Have we acquired it already?
index 36bb36f..b61ab5d 100644 (file)
@@ -148,7 +148,7 @@ public class PipEngineGetStatus extends StdConfigurableEngine {
         try {
             pipResponse = pipFinder.getMatchingAttributes(pipRequest, this);
         } catch (PIPException ex) {
-            logger.error("getAttribute threw:", ex);
+            logger.error("getAttribute threw", ex);
             return null;
         }
         if (pipResponse == null) {
@@ -197,16 +197,17 @@ public class PipEngineGetStatus extends StdConfigurableEngine {
         Set<String> setUids = new HashSet<>();
         for (Attribute attributeUid : listUids) {
             Iterator<AttributeValue<String>> iterAttributeValues = attributeUid.findValues(DataTypes.DT_STRING);
-            if (iterAttributeValues != null) {
-                while (iterAttributeValues.hasNext()) {
-                    String uid = iterAttributeValues.next().getValue();
-                    if (uid != null) {
-                        setUids.add(uid);
-                    }
+            if (iterAttributeValues == null) {
+                continue;
+            }
+            while (iterAttributeValues.hasNext()) {
+                String uid = iterAttributeValues.next().getValue();
+                if (uid == null) {
+                    continue;
                 }
+                setUids.add(uid);
             }
         }
-
         return setUids;
     }
 
@@ -220,13 +221,13 @@ public class PipEngineGetStatus extends StdConfigurableEngine {
             props.put(Util.ECLIPSE_LINK_KEY_USER, PolicyEngine.manager.getEnvironmentProperty(Util.ONAP_KEY_USER));
             props.put(Util.ECLIPSE_LINK_KEY_PASS, PolicyEngine.manager.getEnvironmentProperty(Util.ONAP_KEY_PASS));
         } catch (NullPointerException e) {
-            logger.error("getStatusFromDB: {} when setting properties", e.getMessage());
+            logger.error("getStatusFromDb: when setting properties", e);
         }
         //
         // Set opsHistPu to the correct value and clear properties if necessary.
         //
         String opsHistPu = System.getProperty("OperationsHistoryPU");
-        if (opsHistPu == null || !opsHistPu.equals("TestOperationsHistoryPU")) {
+        if (!"TestOperationsHistoryPU".equals(opsHistPu)) {
             opsHistPu = "OperationsHistoryPU";
         } else {
             props.clear();
@@ -240,14 +241,14 @@ public class PipEngineGetStatus extends StdConfigurableEngine {
             emf = Persistence.createEntityManagerFactory(opsHistPu, props);
         } catch (Exception ex) {
             logger.error("PIP thread got Exception. Can't connect to Operations History DB -- {}", opsHistPu);
-            logger.error("getStatusFromDb threw", ex);
+            logger.error("getStatusFromDb threw", ex);
             return null;
         }
         try {
             em = emf.createEntityManager();
         } catch (Exception ex) {
             logger.error("PIP thread got Exception. Problem creating EntityManager");
-            logger.error("getStatusFromDb threw", ex);
+            logger.error("getStatusFromDb threw", ex);
             emf.close();
             return null;
         } 
@@ -269,7 +270,7 @@ public class PipEngineGetStatus extends StdConfigurableEngine {
         try {
             ret = ((String)nq.getSingleResult());
         } catch (NoResultException ex) {
-            logger.debug("NoResultException for getSingleResult()");
+            logger.debug("NoResultException for getSingleResult()", ex);
             ret = "NO_MATCHING_ENTRY";
         } catch (Exception ex) {
             logger.error("getStatusFromDB threw an exception", ex);
@@ -283,12 +284,12 @@ public class PipEngineGetStatus extends StdConfigurableEngine {
         try {
             em.close();
         } catch (Exception ex) {
-            logger.error("getStatusFromDB threw an exception ", ex);
+            logger.error("getStatusFromDB threw an exception", ex);
         }
         try {
             emf.close();
         } catch (Exception ex) {
-            logger.error("getStatusFromDB threw an exception ", ex);
+            logger.error("getStatusFromDB threw an exception", ex);
         }
         return ret;
     }
index 7abf386..c75c8a0 100644 (file)
@@ -151,7 +151,7 @@ public class CallGuardTaskEmbedded implements Runnable {
         //
         // Create an artificial Guard response in case we didn't get a clear Permit or Deny
         //
-        if (guardResponse.getResult().equals("Indeterminate")) {
+        if ("Indeterminate".equals(guardResponse.getResult())) {
             guardResponse.setOperation(recipe);
             guardResponse.setRequestID(UUID.fromString(requestId));
         }
index e149462..64fb70b 100644 (file)
@@ -60,9 +60,14 @@ public class PolicyGuardXacmlHelperEmbedded {
     private static final Logger netLogger =
             LoggerFactory.getLogger(org.onap.policy.common.endpoints.event.comm.Topic.NETWORK_LOGGER);
 
-    // Constant for the systme line separator
+    // Constant for the system line separator
     private static final String SYSTEM_LS = System.lineSeparator();
-    private static String propfile;
+    private String propfile;
+    private UrlEntry[] restUrls = null;
+    private int restUrlIndex = 0;
+
+    // REST timeout, initialized from 'pdpx.timeout' property
+    private int timeout = 20000;
     
     public PolicyGuardXacmlHelperEmbedded() {
         init(PolicyEngine.manager.getEnvironment());
@@ -82,12 +87,6 @@ public class PolicyGuardXacmlHelperEmbedded {
         String environment = null;
     }
 
-    private UrlEntry[] restUrls = null;
-    private int restUrlIndex = 0;
-
-    // REST timeout, initialized from 'pdpx.timeout' property
-    private int timeout = 20000;
-
     /**
      * Call PDP.
      * 
@@ -133,7 +132,7 @@ public class PolicyGuardXacmlHelperEmbedded {
                     urlEntry.authorization, urlEntry.clientAuth, urlEntry.environment);
             netLogger.info("[IN|{}|{}|]{}{}", "GUARD", urlEntry.restUrl, SYSTEM_LS, response);
         } catch (Exception e) {
-            logger.error("Error in sending RESTful request", e);
+            logger.error("Error in sending RESTful request", e);
         }
 
         return response;
@@ -235,7 +234,7 @@ public class PolicyGuardXacmlHelperEmbedded {
      * @param xacmlReq the XACML request
      * @return the response
      */
-    public static String callEmbeddedPdp(PolicyGuardXacmlRequestAttributes xacmlReq) {
+    public String callEmbeddedPdp(PolicyGuardXacmlRequestAttributes xacmlReq) {
         com.att.research.xacml.api.Response response = null;
         Properties props = new Properties();
         //
@@ -246,7 +245,7 @@ public class PolicyGuardXacmlHelperEmbedded {
               BufferedReader br = new BufferedReader(isr) ) {
             props.load(br);
         } catch (Exception e) {
-            logger.error("Unable to load properties file {} {}", propfile, e.getMessage());
+            logger.error("Unable to load properties file {}", propfile, e);
         }
         //
         // Create embedded PDPEngine
@@ -255,7 +254,7 @@ public class PolicyGuardXacmlHelperEmbedded {
         try {
             xacmlPdpEngine = ATTPDPEngineFactory.newInstance().newEngine(props);
         } catch (Exception e) {
-            logger.error("Failed to create new PDPEngine {}", e.getMessage());
+            logger.error("callEmbeddedPdpEngine failed to create new PDPEngine", e);
             return null;
         }
         logger.debug("embedded Engine created");
@@ -269,7 +268,7 @@ public class PolicyGuardXacmlHelperEmbedded {
         try {
             response = xacmlPdpEngine.decide(RequestParser.parseRequest(xacmlReq));
         } catch (Exception e) {
-            logger.error(e.getMessage(), e);
+            logger.error("callEmbeddedPdpEngine failed on decide", e);
         }
         long timeEnd = System.currentTimeMillis();
         logger.debug("Elapsed Time: {} ms", (timeEnd - timeStart));