Fix Sonar Blockers for Drools-Applications 39/8239/4
authorMike Babinski <mb1915@att.com>
Tue, 22 Aug 2017 04:40:23 +0000 (04:40 +0000)
committerMike Babinski <mb1915@att.com>
Wed, 23 Aug 2017 22:19:40 +0000 (22:19 +0000)
Add null checks to protect against potential null exceptions.

Issue-ID: POLICY-111
Change-Id: Ib079d1b48a6858332ea3eaa0e75a991f518a0f9a
Signed-off-by: Mike Babinski <mb1915@att.com>
controlloop/common/guard/src/main/java/org/onap/policy/guard/PIPEngineGetHistory.java
controlloop/common/model-impl/mso/src/main/java/org/onap/policy/mso/MSOManager.java
controlloop/common/model-impl/rest/src/main/java/org/onap/policy/rest/RESTManager.java

index 57deee5..30f1ef8 100644 (file)
@@ -57,7 +57,6 @@ public class PIPEngineGetHistory extends StdConfigurableEngine{
 
        
        private static final Logger logger = LoggerFactory.getLogger(PIPEngineGetHistory.class);
-       //private static EntityManager em;
        
        public static final String DEFAULT_DESCRIPTION          = "PIP for retrieving Operations History from DB";
        
@@ -89,7 +88,7 @@ public class PIPEngineGetHistory extends StdConfigurableEngine{
                try {
                        attributeValue  = DataTypes.DT_INTEGER.createAttributeValue(value);
                } catch (Exception ex) {
-                       this.logger.error("Failed to convert {} to an AttributeValue<Boolean>",value, ex);
+                       logger.error("Failed to convert {} to an AttributeValue<Boolean>",value, ex);
                }
                if (attributeValue != null) {
                        stdPIPResponse.addAttribute(new StdMutableAttribute(category, attributeId, attributeValue, pipRequest.getIssuer()/*this.getIssuer()*/, false));
@@ -120,22 +119,20 @@ public class PIPEngineGetHistory extends StdConfigurableEngine{
        @Override
        public PIPResponse getAttributes(PIPRequest pipRequest, PIPFinder pipFinder) throws PIPException {
                // TODO Auto-generated method stub
-               System.out.println("Entering FeqLimiter PIP");
+               logger.debug("Entering FeqLimiter PIP");
                
                /*
                 * First check to see if the issuer is set and then match it
                 */
                String string;
                if ((string = pipRequest.getIssuer()) == null) {
-                       this.logger.debug("No issuer in the request...");
-                       System.out.println("FeqLimiter PIP - No issuer in the request!");
+                       logger.debug("No issuer in the request...");
                        return StdPIPResponse.PIP_RESPONSE_EMPTY;
                }
                else{
                        //Notice, we are checking here for the base issuer prefix.
                        if (!string.contains(this.getIssuer())) {
-                               this.logger.debug("Requested issuer '" + string + "' does not match " + (this.getIssuer() == null ? "null" : "'" + this.getIssuer() + "'"));
-                               System.out.println("FeqLimiter PIP - Issuer "+ string +" does not match with: "+this.getIssuer());
+                               logger.debug("Requested issuer '" + string + "' does not match " + (this.getIssuer() == null ? "null" : "'" + this.getIssuer() + "'"));
                                return StdPIPResponse.PIP_RESPONSE_EMPTY;
                        }
                }
@@ -151,7 +148,7 @@ public class PIPEngineGetHistory extends StdConfigurableEngine{
        
                String timeWindow = timeWindowVal + " " + timeWindowScale;
                
-               System.out.println("Going to query DB about: "+actor + " " + operation + " " + target + " " + timeWindow);
+               logger.debug("Going to query DB about: "+actor + " " + operation + " " + target + " " + timeWindow);
                int countFromDB = getCountFromDB(actor, operation, target, timeWindow);
                 
                StdMutablePIPResponse stdPIPResponse    = new StdMutablePIPResponse();
@@ -186,11 +183,11 @@ public class PIPEngineGetHistory extends StdConfigurableEngine{
                try {
                        pipResponse     = pipFinder.getMatchingAttributes(pipRequest, this);
                        if (pipResponse.getStatus() != null && !pipResponse.getStatus().isOk()) {
-                               System.out.println("Error retrieving " + pipRequest.getAttributeId().stringValue() + ": " + pipResponse.getStatus().toString());
+                               logger.debug("Error retrieving " + pipRequest.getAttributeId().stringValue() + ": " + pipResponse.getStatus().toString());
                                pipResponse     = null;
                        }
-                       if (pipResponse.getAttributes().size() == 0) {
-                               System.out.println("No value for " + pipRequest.getAttributeId().stringValue());
+                       if (pipResponse.getAttributes() != null && pipResponse.getAttributes().isEmpty()) {
+                               logger.debug("No value for {}", pipRequest.getAttributeId().stringValue());
                                pipResponse     = null;
                        }
                } catch (PIPException ex) {
@@ -290,8 +287,6 @@ public class PIPEngineGetHistory extends StdConfigurableEngine{
        
        private static int getCountFromDB(String actor, String operation, String target, String timeWindow){
                
-               //long startTime = System.nanoTime();
-       
                EntityManager em;
                try{
                        em = Persistence.createEntityManagerFactory("OperationsHistoryPU").createEntityManager();
@@ -321,10 +316,6 @@ public class PIPEngineGetHistory extends StdConfigurableEngine{
                        return -1;
                }
                
-               //System.out.println("###########************** History count: " + ret);
-               
-               //long estimatedTime = System.nanoTime() - startTime;
-               //System.out.println("time took: " + (double)estimatedTime/1000/1000 + " mili sec.");
 
                em.close();
                
index 81d43ef..9f4fe5b 100644 (file)
@@ -87,8 +87,12 @@ public final class MSOManager {
                                        }
                                        Thread.sleep(20000);
                                }
-                               
-                               System.out.println("***** ########  VF Module Creation timeout. Status: ("+responseGet.request.requestStatus.requestState+")");
+                               if (responseGet != null
+                                && responseGet.request != null
+                                &&     responseGet.request.requestStatus != null
+                                && responseGet.request.requestStatus.requestState != null) {
+                                       logger.warn("***** ########  VF Module Creation timeout. Status: ( {})", responseGet.request.requestStatus.requestState);
+                               }
                                return responseGet;
                        } catch (JsonSyntaxException e) {
                                logger.error("Failed to deserialize into MSOResponse: ", e);
index 8c79d2a..c38c107 100644 (file)
@@ -67,8 +67,10 @@ public final class RESTManager {
                try (CloseableHttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(credentials).build()) {
 
                        HttpPost post = new HttpPost(url);
-                       for (String key : headers.keySet()) {
-                               post.addHeader(key, headers.get(key));
+                       if (headers != null)  {
+                               for (String key : headers.keySet()) {
+                                       post.addHeader(key, headers.get(key));
+                               }
                        }
                        post.addHeader("Content-Type", contentType);
                        
@@ -100,16 +102,17 @@ public final class RESTManager {
                try (CloseableHttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(credentials).build()) {
 
                        HttpGet get = new HttpGet(url);
-                       for (String key : headers.keySet()) {
-                               get.addHeader(key, headers.get(key));
+                       if (headers != null) {
+                               for (String key : headers.keySet()) {
+                                       get.addHeader(key, headers.get(key));
+                               }
                        }
                        
                        HttpResponse response = client.execute(get);
                        
                        String returnBody = EntityUtils.toString(response.getEntity(), "UTF-8");
-                       System.out.println("HTTP GET Response Status Code: " + response.getStatusLine().getStatusCode());
-                       System.out.println("HTTP GET Response Body:");
-                       System.out.println(returnBody);
+                       logger.debug("HTTP GET Response Status Code: " + response.getStatusLine().getStatusCode());
+                       logger.debug("HTTP GET Response Body: " + returnBody);
 
                        return new Pair<Integer, String>(response.getStatusLine().getStatusCode(), returnBody);
                } catch (IOException e) {