Fix logging and formatting
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / onap / dmaap / datarouter / provisioning / beans / IngressRoute.java
index 6b390c4..033b390 100644 (file)
@@ -52,6 +52,7 @@ import org.onap.dmaap.datarouter.provisioning.utils.DB;
  * @version $Id: IngressRoute.java,v 1.3 2013/12/16 20:30:23 eby Exp $\r
  */\r
 public class IngressRoute extends NodeClass implements Comparable<IngressRoute> {\r
+\r
     private static Logger intlogger = Logger.getLogger("org.onap.dmaap.datarouter.provisioning.internal");\r
     private final int seq;\r
     private final int feedid;\r
@@ -76,7 +77,8 @@ public class IngressRoute extends NodeClass implements Comparable<IngressRoute>
      * @return a set of IngressRoutes\r
      */\r
     public static Set<IngressRoute> getIngressRoutesForSeq(int seq) {\r
-        return getAllIngressRoutesForSQL("select SEQUENCE, FEEDID, USERID, SUBNET, NODESET from INGRESS_ROUTES where SEQUENCE = " + seq);\r
+        return getAllIngressRoutesForSQL(\r
+                "select SEQUENCE, FEEDID, USERID, SUBNET, NODESET from INGRESS_ROUTES where SEQUENCE = " + seq);\r
     }\r
 \r
     private static SortedSet<IngressRoute> getAllIngressRoutesForSQL(String sql) {\r
@@ -85,8 +87,8 @@ public class IngressRoute extends NodeClass implements Comparable<IngressRoute>
             DB db = new DB();\r
             @SuppressWarnings("resource")\r
             Connection conn = db.getConnection();\r
-            try(Statement stmt = conn.createStatement()) {\r
-                try(ResultSet rs = stmt.executeQuery(sql)) {\r
+            try (Statement stmt = conn.createStatement()) {\r
+                try (ResultSet rs = stmt.executeQuery(sql)) {\r
                     while (rs.next()) {\r
                         int seq = rs.getInt("SEQUENCE");\r
                         int feedid = rs.getInt("FEEDID");\r
@@ -128,12 +130,12 @@ public class IngressRoute extends NodeClass implements Comparable<IngressRoute>
             DB db = new DB();\r
             @SuppressWarnings("resource")\r
             Connection conn = db.getConnection();\r
-            try(Statement stmt = conn.createStatement()) {\r
-               try(ResultSet rs = stmt.executeQuery(sql)) {\r
-                   if (rs.next()) {\r
-                       rv = rs.getInt("MAX");\r
-                   }\r
-               }\r
+            try (Statement stmt = conn.createStatement()) {\r
+                try (ResultSet rs = stmt.executeQuery(sql)) {\r
+                    if (rs.next()) {\r
+                        rv = rs.getInt("MAX");\r
+                    }\r
+                }\r
             }\r
             db.release(conn);\r
         } catch (SQLException e) {\r
@@ -146,7 +148,7 @@ public class IngressRoute extends NodeClass implements Comparable<IngressRoute>
      * Get an Ingress Route for a particular feed ID, user, and subnet\r
      *\r
      * @param feedid the Feed ID to look for\r
-     * @param user   the user name to look for\r
+     * @param user the user name to look for\r
      * @param subnet the subnet to look for\r
      * @return the Ingress Route, or null of there is none\r
      */\r
@@ -162,7 +164,7 @@ public class IngressRoute extends NodeClass implements Comparable<IngressRoute>
             ps.setInt(1, feedid);\r
             ps.setString(2, user);\r
             ps.setString(3, subnet);\r
-            try(ResultSet rs = ps.executeQuery()) {\r
+            try (ResultSet rs = ps.executeQuery()) {\r
                 if (rs.next()) {\r
                     int seq = rs.getInt("SEQUENCE");\r
                     int nodeset = rs.getInt("NODESET");\r
@@ -175,7 +177,7 @@ public class IngressRoute extends NodeClass implements Comparable<IngressRoute>
             intlogger.error("SQLException " + e.getMessage());\r
         } finally {\r
             try {\r
-                if(ps!=null) {\r
+                if (ps != null) {\r
                     ps.close();\r
                 }\r
             } catch (SQLException e) {\r
@@ -198,9 +200,9 @@ public class IngressRoute extends NodeClass implements Comparable<IngressRoute>
             @SuppressWarnings("resource")\r
             Connection conn = db.getConnection();\r
             String sql = "select FEEDID, USERID, SUBNET, NODESET from INGRESS_ROUTES where SEQUENCE = ?";\r
-            try(PreparedStatement ps = conn.prepareStatement(sql)) {\r
+            try (PreparedStatement ps = conn.prepareStatement(sql)) {\r
                 ps.setInt(1, seq);\r
-                try(ResultSet rs = ps.executeQuery()) {\r
+                try (ResultSet rs = ps.executeQuery()) {\r
                     while (rs.next()) {\r
                         int feedid = rs.getInt("FEEDID");\r
                         String user = rs.getString("USERID");\r
@@ -239,12 +241,14 @@ public class IngressRoute extends NodeClass implements Comparable<IngressRoute>
         this.subnet = (subnet == null) ? "-" : subnet;\r
         this.nodelist = -1;\r
         this.nodes = null;\r
-        if (Feed.getFeedById(feedid) == null)\r
+        if (Feed.getFeedById(feedid) == null) {\r
             throw new IllegalArgumentException("No such feed: " + feedid);\r
+        }\r
         if (!this.subnet.equals("-")) {\r
             SubnetMatcher sm = new SubnetMatcher(subnet);\r
-            if (!sm.isValid())\r
+            if (!sm.isValid()) {\r
                 throw new IllegalArgumentException("Invalid subnet: " + subnet);\r
+            }\r
         }\r
     }\r
 \r
@@ -258,37 +262,41 @@ public class IngressRoute extends NodeClass implements Comparable<IngressRoute>
         this.nodelist = -1;\r
         this.nodes = new TreeSet<String>();\r
         JSONArray ja = jo.getJSONArray("node");\r
-        for (int i = 0; i < ja.length(); i++)\r
+        for (int i = 0; i < ja.length(); i++) {\r
             this.nodes.add(ja.getString(i));\r
+        }\r
     }\r
 \r
     /**\r
-     * Does this particular IngressRoute match a request, represented by feedid and req?\r
-     * To match, <i>feedid</i> must match the feed ID in the route, the user in the route\r
-     * (if specified) must match the user in the request, and the subnet in the route (if specified)\r
-     * must match the subnet from the request.\r
+     * Does this particular IngressRoute match a request, represented by feedid and req? To match, <i>feedid</i> must\r
+     * match the feed ID in the route, the user in the route (if specified) must match the user in the request, and the\r
+     * subnet in the route (if specified) must match the subnet from the request.\r
      *\r
      * @param feedid the feedid for this request\r
-     * @param req    the remainder of the request\r
+     * @param req the remainder of the request\r
      * @return true if a match, false otherwise\r
      */\r
     public boolean matches(int feedid, HttpServletRequest req) {\r
         // Check feedid\r
-        if (this.feedid != feedid)\r
+        if (this.feedid != feedid) {\r
             return false;\r
+        }\r
 \r
         // Get user from request and compare\r
         // Note: we don't check the password; the node will do that\r
         if (userid.length() > 0 && !userid.equals("-")) {\r
             String credentials = req.getHeader("Authorization");\r
-            if (credentials == null || !credentials.startsWith("Basic "))\r
+            if (credentials == null || !credentials.startsWith("Basic ")) {\r
                 return false;\r
+            }\r
             String t = new String(Base64.decodeBase64(credentials.substring(6)));\r
             int ix = t.indexOf(':');\r
-            if (ix >= 0)\r
+            if (ix >= 0) {\r
                 t = t.substring(0, ix);\r
-            if (!t.equals(this.userid))\r
+            }\r
+            if (!t.equals(this.userid)) {\r
                 return false;\r
+            }\r
         }\r
 \r
         // If this route has a subnet, match it against the requester's IP addr\r
@@ -305,10 +313,11 @@ public class IngressRoute extends NodeClass implements Comparable<IngressRoute>
     }\r
 \r
     /**\r
-     * Compare IP addresses as byte arrays to a subnet specified as a CIDR.\r
-     * Taken from org.onap.dmaap.datarouter.node.SubnetMatcher and modified somewhat.\r
+     * Compare IP addresses as byte arrays to a subnet specified as a CIDR. Taken from\r
+     * org.onap.dmaap.datarouter.node.SubnetMatcher and modified somewhat.\r
      */\r
     public class SubnetMatcher {\r
+\r
         private byte[] sn;\r
         private int len;\r
         private int mask;\r
@@ -386,9 +395,9 @@ public class IngressRoute extends NodeClass implements Comparable<IngressRoute>
             @SuppressWarnings("resource")\r
             Connection conn = db.getConnection();\r
             String sql = "select NODEID from NODESETS where SETID = ?";\r
-            try(PreparedStatement ps = conn.prepareStatement(sql)) {\r
+            try (PreparedStatement ps = conn.prepareStatement(sql)) {\r
                 ps.setInt(1, nodelist);\r
-                try(ResultSet rs = ps.executeQuery()) {\r
+                try (ResultSet rs = ps.executeQuery()) {\r
                     while (rs.next()) {\r
                         int id = rs.getInt("NODEID");\r
                         set.add(lookupNodeID(id));\r
@@ -425,10 +434,9 @@ public class IngressRoute extends NodeClass implements Comparable<IngressRoute>
         } catch (SQLException e) {\r
             rv = false;\r
             intlogger.warn("PROV0007 doDelete: " + e.getMessage());\r
-            intlogger.error("SQLException " + e.getMessage());\r
         } finally {\r
             try {\r
-                if(ps!=null) {\r
+                if (ps != null) {\r
                     ps.close();\r
                 }\r
             } catch (SQLException e) {\r
@@ -457,7 +465,8 @@ public class IngressRoute extends NodeClass implements Comparable<IngressRoute>
             }\r
 \r
             // Create the INGRESS_ROUTES row\r
-            ps = c.prepareStatement("insert into INGRESS_ROUTES (SEQUENCE, FEEDID, USERID, SUBNET, NODESET) values (?, ?, ?, ?, ?)");\r
+            ps = c.prepareStatement(\r
+                    "insert into INGRESS_ROUTES (SEQUENCE, FEEDID, USERID, SUBNET, NODESET) values (?, ?, ?, ?, ?)");\r
             ps.setInt(1, this.seq);\r
             ps.setInt(2, this.feedid);\r
             ps.setString(3, this.userid);\r
@@ -468,10 +477,9 @@ public class IngressRoute extends NodeClass implements Comparable<IngressRoute>
             rv = true;\r
         } catch (SQLException e) {\r
             intlogger.warn("PROV0005 doInsert: " + e.getMessage());\r
-            intlogger.error("SQLException " + e.getMessage());\r
         } finally {\r
             try {\r
-                if(ps!=null) {\r
+                if (ps != null) {\r
                     ps.close();\r
                 }\r
             } catch (SQLException e) {\r
@@ -491,10 +499,12 @@ public class IngressRoute extends NodeClass implements Comparable<IngressRoute>
         JSONObject jo = new JSONObject();\r
         jo.put("feedid", feedid);\r
         // Note: for user and subnet, null, "", and "-" are equivalent\r
-        if (userid != null && !userid.equals("-") && !userid.equals(""))\r
+        if (userid != null && !userid.equals("-") && !userid.equals("")) {\r
             jo.put("user", userid);\r
-        if (subnet != null && !subnet.equals("-") && !subnet.equals(""))\r
+        }\r
+        if (subnet != null && !subnet.equals("-") && !subnet.equals("")) {\r
             jo.put("subnet", subnet);\r
+        }\r
         jo.put("seq", seq);\r
         jo.put("node", nodes);\r
         return jo;\r
@@ -502,7 +512,8 @@ public class IngressRoute extends NodeClass implements Comparable<IngressRoute>
 \r
     @Override\r
     public String getKey() {\r
-        return String.format("%d/%s/%s/%d", feedid, (userid == null) ? "" : userid, (subnet == null) ? "" : subnet, seq);\r
+        return String\r
+                .format("%d/%s/%s/%d", feedid, (userid == null) ? "" : userid, (subnet == null) ? "" : subnet, seq);\r
     }\r
 \r
     @Override\r
@@ -513,8 +524,9 @@ public class IngressRoute extends NodeClass implements Comparable<IngressRoute>
     @Override\r
     public boolean equals(Object obj) {\r
         try {\r
-            if (!(obj instanceof IngressRoute))\r
+            if (!(obj instanceof IngressRoute)) {\r
                 return false;\r
+            }\r
             return this.compareTo((IngressRoute) obj) == 0;\r
         } catch (NullPointerException e) {\r
             return false;\r
@@ -523,25 +535,31 @@ public class IngressRoute extends NodeClass implements Comparable<IngressRoute>
 \r
     @Override\r
     public int compareTo(IngressRoute in) {\r
-        if (in == null)\r
+        if (in == null) {\r
             throw new NullPointerException();\r
+        }\r
         int n = this.feedid - in.feedid;\r
-        if (n != 0)\r
+        if (n != 0) {\r
             return n;\r
+        }\r
         n = this.seq - in.seq;\r
-        if (n != 0)\r
+        if (n != 0) {\r
             return n;\r
+        }\r
         n = this.userid.compareTo(in.userid);\r
-        if (n != 0)\r
+        if (n != 0) {\r
             return n;\r
+        }\r
         n = this.subnet.compareTo(in.subnet);\r
-        if (n != 0)\r
+        if (n != 0) {\r
             return n;\r
+        }\r
         return this.nodes.equals(in.nodes) ? 0 : 1;\r
     }\r
 \r
     @Override\r
     public String toString() {\r
-        return String.format("INGRESS: feed=%d, userid=%s, subnet=%s, seq=%d", feedid, (userid == null) ? "" : userid, (subnet == null) ? "" : subnet, seq);\r
+        return String.format("INGRESS: feed=%d, userid=%s, subnet=%s, seq=%d", feedid, (userid == null) ? "" : userid,\r
+                (subnet == null) ? "" : subnet, seq);\r
     }\r
 }\r