From 6d7e231986b84cf6646e46beba7e7e05699bffe6 Mon Sep 17 00:00:00 2001 From: "Magnusen, Drew (dm741q)" Date: Wed, 16 Aug 2017 15:21:15 -0500 Subject: Code cleanup to resolve critical sonar issues Code cleanup mostly involed directing the output of exception messages to the correct logger stream. Issue-ID: [POLICY-115] Change-Id: I2042bac3d3b0991a2ebed33421a73f1aa300c7c1 Signed-off-by: Magnusen, Drew (dm741q) --- .../java/org/onap/policy/guard/CallGuardTask.java | 7 ++-- .../org/onap/policy/guard/PIPEngineGetHistory.java | 38 +++++++++++----------- .../onap/policy/guard/PolicyGuardXacmlHelper.java | 13 +++++--- .../onap/policy/guard/PolicyGuardYamlToXacml.java | 9 ++--- 4 files changed, 37 insertions(+), 30 deletions(-) (limited to 'controlloop/common/guard') diff --git a/controlloop/common/guard/src/main/java/org/onap/policy/guard/CallGuardTask.java b/controlloop/common/guard/src/main/java/org/onap/policy/guard/CallGuardTask.java index dbef0c433..53e972941 100644 --- a/controlloop/common/guard/src/main/java/org/onap/policy/guard/CallGuardTask.java +++ b/controlloop/common/guard/src/main/java/org/onap/policy/guard/CallGuardTask.java @@ -23,13 +23,17 @@ package org.onap.policy.guard; import com.att.research.xacml.api.DataTypeException; import com.att.research.xacml.api.pdp.PDPEngine; import com.att.research.xacml.std.annotations.RequestParser; + import java.util.UUID; import org.drools.core.WorkingMemory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class CallGuardTask implements Runnable { + private static final Logger logger = LoggerFactory.getLogger(CallGuardTask.class); WorkingMemory workingMemory; PDPEngine embeddedPdpEngine; String restfulPdpUrl; @@ -59,8 +63,7 @@ public class CallGuardTask implements Runnable { try { request = RequestParser.parseRequest(xacmlReq); } catch (IllegalArgumentException | IllegalAccessException | DataTypeException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + logger.error("CallGuardTask.run threw: ", e); } diff --git a/controlloop/common/guard/src/main/java/org/onap/policy/guard/PIPEngineGetHistory.java b/controlloop/common/guard/src/main/java/org/onap/policy/guard/PIPEngineGetHistory.java index 8f3928cdd..57deee5f1 100644 --- a/controlloop/common/guard/src/main/java/org/onap/policy/guard/PIPEngineGetHistory.java +++ b/controlloop/common/guard/src/main/java/org/onap/policy/guard/PIPEngineGetHistory.java @@ -47,15 +47,16 @@ import com.att.research.xacml.api.Attribute; import com.att.research.xacml.api.AttributeValue; import com.att.research.xacml.api.Identifier; import com.att.research.xacml.std.datatypes.DataTypes; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class PIPEngineGetHistory extends StdConfigurableEngine{ - private Log logger = LogFactory.getLog(this.getClass()); + 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"; @@ -88,7 +89,7 @@ public class PIPEngineGetHistory extends StdConfigurableEngine{ try { attributeValue = DataTypes.DT_INTEGER.createAttributeValue(value); } catch (Exception ex) { - this.logger.error("Failed to convert " + value + " to an AttributeValue", ex); + this.logger.error("Failed to convert {} to an AttributeValue",value, ex); } if (attributeValue != null) { stdPIPResponse.addAttribute(new StdMutableAttribute(category, attributeId, attributeValue, pipRequest.getIssuer()/*this.getIssuer()*/, false)); @@ -193,7 +194,7 @@ public class PIPEngineGetHistory extends StdConfigurableEngine{ pipResponse = null; } } catch (PIPException ex) { - System.out.println("PIPException getting subject-id attribute: " + ex.getMessage()); + logger.error("getAttribute threw: ", ex); } return pipResponse; } @@ -295,29 +296,28 @@ public class PIPEngineGetHistory extends StdConfigurableEngine{ try{ em = Persistence.createEntityManagerFactory("OperationsHistoryPU").createEntityManager(); }catch(Exception e){ - System.err.println("PIP thread got Exception " + e.getLocalizedMessage() + " Can't connect to Operations History DB."); + logger.error("getCountFromDB threw: ", e); return -1; } - String sql = "select count(*) as count from operationshistory10 where outcome<>'Failure_Guard' and actor='" - + actor - + "' and operation='" - + operation - + "' and target='" - + target - + "' " - + "and endtime between date_sub(now(),interval " - + timeWindow - + ") and now()"; - - Query nq = em.createNativeQuery(sql); + String sql = "select count(*) as count from operationshistory10 where outcome<>'Failure_Guard'" + + " and actor=:actor" + + " and operation=:operation" + + " and target=:target" + + " and endtime between date_sub(now(),interval :timeWindow) and now()"; + + Query nq = em.createNativeQuery(sql); + nq = nq.setParameter("actor", actor); + nq = nq.setParameter("operation", operation); + nq = nq.setParameter("target", target); + nq = nq.setParameter("timeWindow", timeWindow); int ret = -1; try{ ret = ((Number)nq.getSingleResult()).intValue(); } catch(NoResultException | NonUniqueResultException ex){ - System.err.println("PIP thread got Exception " + ex.getLocalizedMessage()); + logger.error("getCountFromDB threw: ", ex); return -1; } diff --git a/controlloop/common/guard/src/main/java/org/onap/policy/guard/PolicyGuardXacmlHelper.java b/controlloop/common/guard/src/main/java/org/onap/policy/guard/PolicyGuardXacmlHelper.java index 20d50fd3b..c0ed80086 100644 --- a/controlloop/common/guard/src/main/java/org/onap/policy/guard/PolicyGuardXacmlHelper.java +++ b/controlloop/common/guard/src/main/java/org/onap/policy/guard/PolicyGuardXacmlHelper.java @@ -30,6 +30,8 @@ import java.util.UUID; import org.apache.commons.io.IOUtils; import org.apache.http.entity.ContentType; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import com.att.research.xacml.api.Attribute; import com.att.research.xacml.api.AttributeCategory; @@ -44,7 +46,7 @@ import com.att.research.xacml.std.json.JSONResponse; public class PolicyGuardXacmlHelper { - + private static final Logger logger = LoggerFactory.getLogger(PolicyGuardXacmlHelper.class); public static com.att.research.xacml.api.Response callPDP(PDPEngine xacmlEmbeddedPdpEngine, String restfulPdpUrl, com.att.research.xacml.api.Request request, boolean isREST) { // @@ -133,19 +135,20 @@ public class PolicyGuardXacmlHelper { contentType.getMimeType().equalsIgnoreCase("application/xacml+xml") ) { response = (com.att.research.xacml.api.Response) DOMResponse.load(connection.getInputStream()); } else { - System.err.println("unknown content-type: " + contentType); + logger.error("{}: unknown content-type: ", contentType); } } catch (Exception e) { String message = "Parsing Content-Type: " + connection.getContentType() + ", error=" + e.getMessage(); - System.err.println(message); + logger.error("{}: callRESTfulPDP threw: ", message); } } else { - System.err.println(connection.getResponseCode() + " " + connection.getResponseMessage()); + logger.error("unknown content-type: {} {}", connection.getResponseCode(), connection.getResponseMessage() ); } } catch (Exception e) { - System.err.println(e); + + logger.error("callRESTfulPDP threw: ", e); } return response; diff --git a/controlloop/common/guard/src/main/java/org/onap/policy/guard/PolicyGuardYamlToXacml.java b/controlloop/common/guard/src/main/java/org/onap/policy/guard/PolicyGuardYamlToXacml.java index 4d952b58e..21584f0b5 100644 --- a/controlloop/common/guard/src/main/java/org/onap/policy/guard/PolicyGuardYamlToXacml.java +++ b/controlloop/common/guard/src/main/java/org/onap/policy/guard/PolicyGuardYamlToXacml.java @@ -30,11 +30,14 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import org.onap.policy.controlloop.policy.guard.ControlLoopGuard; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class PolicyGuardYamlToXacml { + private static final Logger logger = LoggerFactory.getLogger(PolicyGuardYamlToXacml.class); public static void fromYamlToXacml(String yamlFile, String xacmlTemplate, String xacmlPolicyOutput){ @@ -67,8 +70,7 @@ public class PolicyGuardYamlToXacml { Files.write(Paths.get(xacmlPolicyOutput), xacmlPolicyContent.getBytes()); } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + logger.error("fromYamlToXacml threw: ", e); } } @@ -198,8 +200,7 @@ public class PolicyGuardYamlToXacml { Files.write(Paths.get(xacmlPolicyOutput), xacmlPolicyContent.getBytes()); } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + logger.error("fromYamlToXacmlBlacklist threw: ", e); } } -- cgit 1.2.3-korg