Refactor Prov DB handling
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / onap / dmaap / datarouter / provisioning / beans / EgressRoute.java
index 8c8b715..8cd1986 100644 (file)
@@ -7,9 +7,9 @@
  * * Licensed under the Apache License, Version 2.0 (the "License");\r
  * * you may not use this file except in compliance with the License.\r
  * * You may obtain a copy of the License at\r
- * * \r
+ * *\r
  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
- * * \r
+ * *\r
  *  * Unless required by applicable law or agreed to in writing, software\r
  * * distributed under the License is distributed on an "AS IS" BASIS,\r
  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
 \r
 package org.onap.dmaap.datarouter.provisioning.beans;\r
 \r
+import com.att.eelf.configuration.EELFLogger;\r
+import com.att.eelf.configuration.EELFManager;\r
 import java.sql.Connection;\r
 import java.sql.PreparedStatement;\r
 import java.sql.ResultSet;\r
 import java.sql.SQLException;\r
 import java.sql.Statement;\r
+import java.util.Objects;\r
 import java.util.SortedSet;\r
 import java.util.TreeSet;\r
-\r
-import org.apache.log4j.Logger;\r
 import org.json.JSONObject;\r
-import org.onap.dmaap.datarouter.provisioning.utils.DB;\r
+import org.onap.dmaap.datarouter.provisioning.utils.ProvDbUtils;\r
 \r
 /**\r
  * The representation of one route in the Egress Route Table.\r
@@ -43,184 +44,147 @@ import org.onap.dmaap.datarouter.provisioning.utils.DB;
  * @version $Id: EgressRoute.java,v 1.3 2013/12/16 20:30:23 eby Exp $\r
  */\r
 public class EgressRoute extends NodeClass implements Comparable<EgressRoute> {\r
-       private static Logger intlogger = Logger.getLogger("org.onap.dmaap.datarouter.provisioning.internal");\r
-       private final int subid;\r
-       private final int nodeid;\r
-\r
-       /**\r
-        * Get a set of all Egress Routes in the DB.  The set is sorted according to the natural sorting order\r
-        * of the routes (based on the subscription ID in each route).\r
-        * @return the sorted set\r
-        */\r
-       public static SortedSet<EgressRoute> getAllEgressRoutes() {\r
-               SortedSet<EgressRoute> set = new TreeSet<EgressRoute>();\r
-               try {\r
-                       DB db = new DB();\r
-                       @SuppressWarnings("resource")\r
-                       Connection conn = db.getConnection();\r
-                       Statement  stmt = conn.createStatement();\r
-                       ResultSet    rs = stmt.executeQuery("select SUBID, NODEID from EGRESS_ROUTES");\r
-                       while (rs.next()) {\r
-                               int subid  = rs.getInt("SUBID");\r
-                               int nodeid = rs.getInt("NODEID");\r
-                               set.add(new EgressRoute(subid, nodeid));\r
-                       }\r
-                       rs.close();\r
-                       stmt.close();\r
-                       db.release(conn);\r
-               } catch (SQLException e) {\r
-                       e.printStackTrace();\r
-               }\r
-               return set;\r
-       }\r
-       /**\r
-        * Get a single Egress Route for the subscription <i>sub</i>.\r
-        * @param sub the subscription to lookup\r
-        * @return an EgressRoute, or null if there is no route for this subscription\r
-        */\r
-       public static EgressRoute getEgressRoute(int sub) {\r
-               EgressRoute v = null;\r
-               PreparedStatement ps = null;\r
-               try {\r
-                       DB db = new DB();\r
-                       @SuppressWarnings("resource")\r
-                       Connection conn = db.getConnection();\r
-                       String sql = "select NODEID from EGRESS_ROUTES where SUBID = ?";\r
-                       ps = conn.prepareStatement(sql);\r
-                       ps.setInt(1, sub);\r
-                       ResultSet rs = ps.executeQuery();\r
-                       if (rs.next()) {\r
-                               int node = rs.getInt("NODEID");\r
-                               v = new EgressRoute(sub, node);\r
-                       }\r
-                       rs.close();\r
-                       ps.close();\r
-                       db.release(conn);\r
-               } catch (SQLException e) {\r
-                       e.printStackTrace();\r
-               } finally {\r
-                       try {\r
-                               ps.close();\r
-                       } catch (SQLException e) {\r
-                               e.printStackTrace();\r
-                       }\r
-               }\r
-               return v;\r
-       }\r
-\r
-       public EgressRoute(int subid, int nodeid) throws IllegalArgumentException {\r
-               this.subid = subid;\r
-               this.nodeid = nodeid;\r
-// Note: unlike for Feeds, it subscriptions can be removed from the tables, so it is\r
-// possible that an orphan ERT entry can exist if a sub is removed.\r
-//             if (Subscription.getSubscriptionById(subid) == null)\r
-//                     throw new IllegalArgumentException("No such subscription: "+subid);\r
-       }\r
-\r
-       public EgressRoute(int subid, String node) throws IllegalArgumentException {\r
-               this(subid, lookupNodeName(node));\r
-       }\r
-\r
-       @Override\r
-       public boolean doDelete(Connection c) {\r
-               boolean rv = true;\r
-               PreparedStatement ps = null;\r
-               try {\r
-                       String sql = "delete from EGRESS_ROUTES where SUBID = ?";\r
-                       ps = c.prepareStatement(sql);\r
-                       ps.setInt(1, subid);\r
-                       ps.execute();\r
-               } catch (SQLException e) {\r
-                       rv = false;\r
-                       intlogger.warn("PROV0007 doDelete: "+e.getMessage());\r
-                       e.printStackTrace();\r
-               } finally {\r
-                       try {\r
-                               ps.close();\r
-                       } catch (SQLException e) {\r
-                               e.printStackTrace();\r
-                       }\r
-               }\r
-               return rv;\r
-       }\r
-\r
-       @Override\r
-       public boolean doInsert(Connection c) {\r
-               boolean rv = false;\r
-               PreparedStatement ps = null;\r
-               try {\r
-                       // Create the NETWORK_ROUTES row\r
-                       String sql = "insert into EGRESS_ROUTES (SUBID, NODEID) values (?, ?)";\r
-                       ps = c.prepareStatement(sql);\r
-                       ps.setInt(1, this.subid);\r
-                       ps.setInt(2, this.nodeid);\r
-                       ps.execute();\r
-                       ps.close();\r
-                       rv = true;\r
-               } catch (SQLException e) {\r
-                       intlogger.warn("PROV0005 doInsert: "+e.getMessage());\r
-                       e.printStackTrace();\r
-               } finally {\r
-                       try {\r
-                               ps.close();\r
-                       } catch (SQLException e) {\r
-                               e.printStackTrace();\r
-                       }\r
-               }\r
-               return rv;\r
-       }\r
-\r
-       @Override\r
-       public boolean doUpdate(Connection c) {\r
-               boolean rv = true;\r
-               PreparedStatement ps = null;\r
-               try {\r
-                       String sql = "update EGRESS_ROUTES set NODEID = ? where SUBID = ?";\r
-                       ps = c.prepareStatement(sql);\r
-                       ps.setInt(1, nodeid);\r
-                       ps.setInt(2, subid);\r
-                       ps.executeUpdate();\r
-               } catch (SQLException e) {\r
-                       rv = false;\r
-                       intlogger.warn("PROV0006 doUpdate: "+e.getMessage());\r
-                       e.printStackTrace();\r
-               } finally {\r
-                       try {\r
-                               ps.close();\r
-                       } catch (SQLException e) {\r
-                               e.printStackTrace();\r
-                       }\r
-               }\r
-               return rv;\r
-       }\r
-\r
-       @Override\r
-       public JSONObject asJSONObject() {\r
-               JSONObject jo = new JSONObject();\r
-               jo.put(""+subid, lookupNodeID(nodeid));\r
-               return jo;\r
-       }\r
-\r
-       @Override\r
-       public String getKey() {\r
-               return ""+subid;\r
-       }\r
-\r
-       @Override\r
-       public boolean equals(Object obj) {\r
-               if (!(obj instanceof EgressRoute))\r
-                       return false;\r
-               EgressRoute on = (EgressRoute)obj;\r
-               return (subid == on.subid) && (nodeid == on.nodeid);\r
-       }\r
-\r
-       @Override\r
-       public int compareTo(EgressRoute o) {\r
-               return this.subid - o.subid;\r
-       }\r
-\r
-       @Override\r
-       public String toString() {\r
-               return String.format("EGRESS: sub=%d, node=%d", subid, nodeid);\r
-       }\r
+\r
+    private static EELFLogger intlogger = EELFManager.getInstance().getLogger("InternalLog");\r
+    private final int subid;\r
+    private final int nodeid;\r
+\r
+    /**\r
+     * EgressRoute constructor.\r
+     * @param subid subscription id\r
+     * @param nodeid node id\r
+     */\r
+    public EgressRoute(int subid, int nodeid) {\r
+        this.subid = subid;\r
+        this.nodeid = nodeid;\r
+    }\r
+\r
+    public EgressRoute(int subid, String node) {\r
+        this(subid, lookupNodeName(node));\r
+    }\r
+\r
+    /**\r
+     * Get a set of all Egress Routes in the DB.  The set is sorted according to the natural sorting order of the routes\r
+     * (based on the subscription ID in each route).\r
+     *\r
+     * @return the sorted set\r
+     */\r
+    public static SortedSet<EgressRoute> getAllEgressRoutes() {\r
+        SortedSet<EgressRoute> set = new TreeSet<>();\r
+        try (Connection conn = ProvDbUtils.getInstance().getConnection();\r
+            Statement stmt = conn.createStatement();\r
+            ResultSet rs = stmt.executeQuery("select SUBID, NODEID from EGRESS_ROUTES")) {\r
+            addEgressRouteToSet(set, rs);\r
+        } catch (SQLException e) {\r
+            intlogger.error("PROV0008 EgressRoute.getAllEgressRoutes: " + e.getMessage(), e);\r
+        }\r
+        return set;\r
+    }\r
+\r
+    private static void addEgressRouteToSet(SortedSet<EgressRoute> set, ResultSet rs) throws SQLException {\r
+        while (rs.next()) {\r
+            int subid = rs.getInt("SUBID");\r
+            int nodeid = rs.getInt("NODEID");\r
+            set.add(new EgressRoute(subid, nodeid));\r
+        }\r
+    }\r
+\r
+    /**\r
+     * Get a single Egress Route for the subscription <i>sub</i>.\r
+     *\r
+     * @param sub the subscription to lookup\r
+     * @return an EgressRoute, or null if there is no route for this subscription\r
+     */\r
+    public static EgressRoute getEgressRoute(int sub) {\r
+        EgressRoute er = null;\r
+        try (Connection conn = ProvDbUtils.getInstance().getConnection();\r
+            PreparedStatement ps = conn.prepareStatement("select NODEID from EGRESS_ROUTES where SUBID = ?")) {\r
+            ps.setInt(1, sub);\r
+            ResultSet rs = ps.executeQuery();\r
+            if (rs.next()) {\r
+                int node = rs.getInt("NODEID");\r
+                er = new EgressRoute(sub, node);\r
+            }\r
+        } catch (SQLException e) {\r
+            intlogger.error("PROV0009 EgressRoute.getEgressRoute: " + e.getMessage(), e);\r
+        }\r
+        return er;\r
+    }\r
+\r
+    @Override\r
+    public boolean doDelete(Connection conn) {\r
+        boolean rv = true;\r
+        try (PreparedStatement ps = conn.prepareStatement("delete from EGRESS_ROUTES where SUBID = ?")) {\r
+            ps.setInt(1, subid);\r
+            ps.execute();\r
+        } catch (SQLException e) {\r
+            rv = false;\r
+            intlogger.error("PROV0007 doDelete: " + e.getMessage(), e);\r
+        }\r
+        return rv;\r
+    }\r
+\r
+    @Override\r
+    public boolean doInsert(Connection conn) {\r
+        boolean rv = false;\r
+        try (PreparedStatement ps = conn.prepareStatement("insert into EGRESS_ROUTES (SUBID, NODEID) values (?, ?)")) {\r
+            ps.setInt(1, this.subid);\r
+            ps.setInt(2, this.nodeid);\r
+            ps.execute();\r
+            rv = true;\r
+        } catch (SQLException e) {\r
+            intlogger.warn("PROV0005 doInsert: " + e.getMessage(), e);\r
+        }\r
+        return rv;\r
+    }\r
+\r
+    @Override\r
+    public boolean doUpdate(Connection conn) {\r
+        boolean rv = true;\r
+        try (PreparedStatement ps = conn.prepareStatement("update EGRESS_ROUTES set NODEID = ? where SUBID = ?")) {\r
+            ps.setInt(1, nodeid);\r
+            ps.setInt(2, subid);\r
+            ps.executeUpdate();\r
+        } catch (SQLException e) {\r
+            rv = false;\r
+            intlogger.warn("PROV0006 doUpdate: " + e.getMessage(), e);\r
+        }\r
+        return rv;\r
+    }\r
+\r
+    @Override\r
+    public JSONObject asJSONObject() {\r
+        JSONObject jo = new JSONObject();\r
+        jo.put("" + subid, lookupNodeID(nodeid));\r
+        return jo;\r
+    }\r
+\r
+    @Override\r
+    public String getKey() {\r
+        return "" + subid;\r
+    }\r
+\r
+    @Override\r
+    public boolean equals(Object obj) {\r
+        if (!(obj instanceof EgressRoute)) {\r
+            return false;\r
+        }\r
+        EgressRoute on = (EgressRoute) obj;\r
+        return (subid == on.subid) && (nodeid == on.nodeid);\r
+    }\r
+\r
+    @Override\r
+    public int compareTo(EgressRoute er) {\r
+        return this.subid - er.subid;\r
+    }\r
+\r
+    @Override\r
+    public String toString() {\r
+        return String.format("EGRESS: sub=%d, node=%d", subid, nodeid);\r
+    }\r
+\r
+    @Override\r
+    public int hashCode() {\r
+        return Objects.hash(subid, nodeid);\r
+    }\r
 }\r