X-Git-Url: https://gerrit.onap.org/r/gitweb?p=dmaap%2Fdatarouter.git;a=blobdiff_plain;f=datarouter-prov%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fdmaap%2Fdatarouter%2Fprovisioning%2Fbeans%2FEgressRoute.java;h=8cd198660794c50feb2f861c12eadd19f613ebc9;hp=242c1053e54145715cb19c6cf7768f04308d956b;hb=68a9ca240970fceaf12bbe91b7bad8e1d98ecd93;hpb=67b1f764c52c684df62b90936f9acf2cfe5c39c4 diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/EgressRoute.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/EgressRoute.java index 242c1053..8cd19866 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/EgressRoute.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/EgressRoute.java @@ -24,6 +24,8 @@ package org.onap.dmaap.datarouter.provisioning.beans; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -32,10 +34,8 @@ import java.sql.Statement; import java.util.Objects; import java.util.SortedSet; import java.util.TreeSet; - -import org.apache.log4j.Logger; import org.json.JSONObject; -import org.onap.dmaap.datarouter.provisioning.utils.DB; +import org.onap.dmaap.datarouter.provisioning.utils.ProvDbUtils; /** * The representation of one route in the Egress Route Table. @@ -44,39 +44,51 @@ import org.onap.dmaap.datarouter.provisioning.utils.DB; * @version $Id: EgressRoute.java,v 1.3 2013/12/16 20:30:23 eby Exp $ */ public class EgressRoute extends NodeClass implements Comparable { - private static Logger intlogger = Logger.getLogger("org.onap.dmaap.datarouter.provisioning.internal"); + + private static EELFLogger intlogger = EELFManager.getInstance().getLogger("InternalLog"); private final int subid; private final int nodeid; /** - * Get a set of all Egress Routes in the DB. The set is sorted according to the natural sorting order - * of the routes (based on the subscription ID in each route). + * EgressRoute constructor. + * @param subid subscription id + * @param nodeid node id + */ + public EgressRoute(int subid, int nodeid) { + this.subid = subid; + this.nodeid = nodeid; + } + + public EgressRoute(int subid, String node) { + this(subid, lookupNodeName(node)); + } + + /** + * Get a set of all Egress Routes in the DB. The set is sorted according to the natural sorting order of the routes + * (based on the subscription ID in each route). * * @return the sorted set */ public static SortedSet getAllEgressRoutes() { - SortedSet set = new TreeSet(); - try { - DB db = new DB(); - @SuppressWarnings("resource") - Connection conn = db.getConnection(); - try( Statement stmt = conn.createStatement()) { - try(ResultSet rs = stmt.executeQuery("select SUBID, NODEID from EGRESS_ROUTES")) { - while (rs.next()) { - int subid = rs.getInt("SUBID"); - int nodeid = rs.getInt("NODEID"); - set.add(new EgressRoute(subid, nodeid)); - } - } - } - - db.release(conn); + SortedSet set = new TreeSet<>(); + try (Connection conn = ProvDbUtils.getInstance().getConnection(); + Statement stmt = conn.createStatement(); + ResultSet rs = stmt.executeQuery("select SUBID, NODEID from EGRESS_ROUTES")) { + addEgressRouteToSet(set, rs); } catch (SQLException e) { - intlogger.error("SQLException " + e.getMessage()); + intlogger.error("PROV0008 EgressRoute.getAllEgressRoutes: " + e.getMessage(), e); } return set; } + private static void addEgressRouteToSet(SortedSet set, ResultSet rs) throws SQLException { + while (rs.next()) { + int subid = rs.getInt("SUBID"); + int nodeid = rs.getInt("NODEID"); + set.add(new EgressRoute(subid, nodeid)); + } + } + /** * Get a single Egress Route for the subscription sub. * @@ -84,125 +96,58 @@ public class EgressRoute extends NodeClass implements Comparable { * @return an EgressRoute, or null if there is no route for this subscription */ public static EgressRoute getEgressRoute(int sub) { - EgressRoute v = null; - PreparedStatement ps = null; - try { - DB db = new DB(); - @SuppressWarnings("resource") - Connection conn = db.getConnection(); - String sql = "select NODEID from EGRESS_ROUTES where SUBID = ?"; - ps = conn.prepareStatement(sql); + EgressRoute er = null; + try (Connection conn = ProvDbUtils.getInstance().getConnection(); + PreparedStatement ps = conn.prepareStatement("select NODEID from EGRESS_ROUTES where SUBID = ?")) { ps.setInt(1, sub); - try(ResultSet rs = ps.executeQuery()) { - if (rs.next()) { - int node = rs.getInt("NODEID"); - v = new EgressRoute(sub, node); - } + ResultSet rs = ps.executeQuery(); + if (rs.next()) { + int node = rs.getInt("NODEID"); + er = new EgressRoute(sub, node); } - ps.close(); - db.release(conn); } catch (SQLException e) { - intlogger.error("SQLException " + e.getMessage()); - } finally { - try { - if(ps!=null) { - ps.close(); - } - } catch (SQLException e) { - intlogger.error("SQLException " + e.getMessage()); - } + intlogger.error("PROV0009 EgressRoute.getEgressRoute: " + e.getMessage(), e); } - return v; - } - - public EgressRoute(int subid, int nodeid) throws IllegalArgumentException { - this.subid = subid; - this.nodeid = nodeid; -// Note: unlike for Feeds, it subscriptions can be removed from the tables, so it is -// possible that an orphan ERT entry can exist if a sub is removed. -// if (Subscription.getSubscriptionById(subid) == null) -// throw new IllegalArgumentException("No such subscription: "+subid); - } - - public EgressRoute(int subid, String node) throws IllegalArgumentException { - this(subid, lookupNodeName(node)); + return er; } @Override - public boolean doDelete(Connection c) { + public boolean doDelete(Connection conn) { boolean rv = true; - PreparedStatement ps = null; - try { - String sql = "delete from EGRESS_ROUTES where SUBID = ?"; - ps = c.prepareStatement(sql); + try (PreparedStatement ps = conn.prepareStatement("delete from EGRESS_ROUTES where SUBID = ?")) { ps.setInt(1, subid); ps.execute(); } catch (SQLException e) { rv = false; - intlogger.warn("PROV0007 doDelete: " + e.getMessage()); - intlogger.error("SQLException " + e.getMessage()); - } finally { - try { - if(ps!=null) { - ps.close(); - } - } catch (SQLException e) { - intlogger.error("SQLException " + e.getMessage()); - } + intlogger.error("PROV0007 doDelete: " + e.getMessage(), e); } return rv; } @Override - public boolean doInsert(Connection c) { + public boolean doInsert(Connection conn) { boolean rv = false; - PreparedStatement ps = null; - try { - // Create the NETWORK_ROUTES row - String sql = "insert into EGRESS_ROUTES (SUBID, NODEID) values (?, ?)"; - ps = c.prepareStatement(sql); + try (PreparedStatement ps = conn.prepareStatement("insert into EGRESS_ROUTES (SUBID, NODEID) values (?, ?)")) { ps.setInt(1, this.subid); ps.setInt(2, this.nodeid); ps.execute(); - ps.close(); rv = true; } catch (SQLException e) { - intlogger.warn("PROV0005 doInsert: " + e.getMessage()); - intlogger.error("SQLException " + e.getMessage()); - } finally { - try { - if(ps!=null) { - ps.close(); - } - } catch (SQLException e) { - intlogger.error("SQLException " + e.getMessage()); - } + intlogger.warn("PROV0005 doInsert: " + e.getMessage(), e); } return rv; } @Override - public boolean doUpdate(Connection c) { + public boolean doUpdate(Connection conn) { boolean rv = true; - PreparedStatement ps = null; - try { - String sql = "update EGRESS_ROUTES set NODEID = ? where SUBID = ?"; - ps = c.prepareStatement(sql); + try (PreparedStatement ps = conn.prepareStatement("update EGRESS_ROUTES set NODEID = ? where SUBID = ?")) { ps.setInt(1, nodeid); ps.setInt(2, subid); ps.executeUpdate(); } catch (SQLException e) { rv = false; - intlogger.warn("PROV0006 doUpdate: " + e.getMessage()); - intlogger.error("SQLException " + e.getMessage()); - } finally { - try { - if(ps!=null) { - ps.close(); - } - } catch (SQLException e) { - intlogger.error("SQLException " + e.getMessage()); - } + intlogger.warn("PROV0006 doUpdate: " + e.getMessage(), e); } return rv; } @@ -221,15 +166,16 @@ public class EgressRoute extends NodeClass implements Comparable { @Override public boolean equals(Object obj) { - if (!(obj instanceof EgressRoute)) + if (!(obj instanceof EgressRoute)) { return false; + } EgressRoute on = (EgressRoute) obj; return (subid == on.subid) && (nodeid == on.nodeid); } @Override - public int compareTo(EgressRoute o) { - return this.subid - o.subid; + public int compareTo(EgressRoute er) { + return this.subid - er.subid; } @Override