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%2FNetworkRoute.java;h=2ada1ff9e2d2b0495f746c1ef3047f63ed3890bf;hp=00eb6a2690faf5739cd80ff68d8c0cbfc2f2d67c;hb=68a9ca240970fceaf12bbe91b7bad8e1d98ecd93;hpb=a640609ea3ba7cf2adc2f593d9ead3b5e229649b diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/NetworkRoute.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/NetworkRoute.java index 00eb6a26..2ada1ff9 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/NetworkRoute.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/NetworkRoute.java @@ -24,18 +24,17 @@ 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; import java.sql.SQLException; -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 Network Route Table. @@ -44,64 +43,85 @@ import org.onap.dmaap.datarouter.provisioning.utils.DB; * @version $Id: NetworkRoute.java,v 1.2 2013/12/16 20:30:23 eby Exp $ */ public class NetworkRoute 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 static final String SQLEXCEPTION = "SQLException: "; private final int fromnode; private final int tonode; private final int vianode; /** - * Get a set of all Network Routes in the DB. The set is sorted according to the natural sorting order - * of the routes (based on the from and to node names in each route). - * - * @return the sorted set + * NetworkRoute Constructor. + * @param fromnode node source + * @param tonode node destination */ - public static SortedSet getAllNetworkRoutes() { - 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 FROMNODE, TONODE, VIANODE from NETWORK_ROUTES")) { - while (rs.next()) { - int fromnode = rs.getInt("FROMNODE"); - int tonode = rs.getInt("TONODE"); - int vianode = rs.getInt("VIANODE"); - set.add(new NetworkRoute(fromnode, tonode, vianode)); - } - } - } - db.release(conn); - } catch (SQLException e) { - e.printStackTrace(); - } - return set; - } - - public NetworkRoute(String fromnode, String tonode) throws IllegalArgumentException { + public NetworkRoute(String fromnode, String tonode) { this.fromnode = lookupNodeName(fromnode); this.tonode = lookupNodeName(tonode); this.vianode = -1; } - public NetworkRoute(String fromnode, String tonode, String vianode) throws IllegalArgumentException { + /** + * NetworkRoute Constructor. + * @param fromnode node source + * @param tonode node destination + * @param vianode via node + */ + public NetworkRoute(String fromnode, String tonode, String vianode) { this.fromnode = lookupNodeName(fromnode); this.tonode = lookupNodeName(tonode); this.vianode = lookupNodeName(vianode); } - public NetworkRoute(JSONObject jo) throws IllegalArgumentException { + /** + * NetworkRoute Constructor. + * @param jo JSONObject of attributes + */ + public NetworkRoute(JSONObject jo) { this.fromnode = lookupNodeName(jo.getString("from")); this.tonode = lookupNodeName(jo.getString("to")); this.vianode = lookupNodeName(jo.getString("via")); } - public NetworkRoute(int fromnode, int tonode, int vianode) throws IllegalArgumentException { + /** + * NetworkRoute Constructor. + * @param fromnode integer source node + * @param tonode integer destination node + * @param vianode integer via node + */ + private NetworkRoute(int fromnode, int tonode, int vianode) { this.fromnode = fromnode; this.tonode = tonode; this.vianode = vianode; } + /** + * Get a set of all Network Routes in the DB. The set is sorted according to the natural sorting order of the + * routes (based on the from and to node names in each route). + * + * @return the sorted set + */ + public static SortedSet getAllNetworkRoutes() { + SortedSet set = new TreeSet<>(); + try (Connection conn = ProvDbUtils.getInstance().getConnection(); + PreparedStatement ps = conn.prepareStatement("select FROMNODE, TONODE, VIANODE from NETWORK_ROUTES"); + ResultSet rs = ps.executeQuery()) { + addNetworkRouteToSet(set, rs); + } catch (SQLException e) { + intlogger.error(SQLEXCEPTION + e.getMessage(), e); + } + return set; + } + + private static void addNetworkRouteToSet(SortedSet set, ResultSet rs) throws SQLException { + while (rs.next()) { + int fromnode = rs.getInt("FROMNODE"); + int tonode = rs.getInt("TONODE"); + int vianode = rs.getInt("VIANODE"); + set.add(new NetworkRoute(fromnode, tonode, vianode)); + } + } + public int getFromnode() { return fromnode; } @@ -110,90 +130,56 @@ public class NetworkRoute extends NodeClass implements Comparable return tonode; } - public int getVianode() { + int getVianode() { return vianode; } @Override - public boolean doDelete(Connection c) { + public boolean doDelete(Connection conn) { boolean rv = true; - PreparedStatement ps = null; - try { - String sql = "delete from NETWORK_ROUTES where FROMNODE = ? AND TONODE = ?"; - ps = c.prepareStatement(sql); + try (PreparedStatement ps = conn.prepareStatement( + "delete from NETWORK_ROUTES where FROMNODE = ? AND TONODE = ?")) { ps.setInt(1, fromnode); ps.setInt(2, tonode); ps.execute(); } catch (SQLException e) { rv = false; - intlogger.warn("PROV0007 doDelete: " + e.getMessage()); - e.printStackTrace(); - } finally { - try { - if(ps!=null) { - ps.close(); - } - } catch (SQLException e) { - e.printStackTrace(); - } + intlogger.warn("PROV0007 doDelete: " + e.getMessage(), e); } return rv; } @Override - public boolean doInsert(Connection c) { + public boolean doInsert(Connection conn) { boolean rv = false; if (this.vianode >= 0) { - PreparedStatement ps = null; - try { + try (PreparedStatement ps = conn.prepareStatement( + "insert into NETWORK_ROUTES (FROMNODE, TONODE, VIANODE) values (?, ?, ?)")) { // Create the NETWORK_ROUTES row - String sql = "insert into NETWORK_ROUTES (FROMNODE, TONODE, VIANODE) values (?, ?, ?)"; - ps = c.prepareStatement(sql); ps.setInt(1, this.fromnode); ps.setInt(2, this.tonode); ps.setInt(3, this.vianode); ps.execute(); - ps.close(); rv = true; } catch (SQLException e) { - intlogger.warn("PROV0005 doInsert: " + e.getMessage()); - e.printStackTrace(); - } finally { - try { - if(ps!=null) { - ps.close(); - } - } catch (SQLException e) { - e.printStackTrace(); - } + 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 NETWORK_ROUTES set VIANODE = ? where FROMNODE = ? and TONODE = ?"; - ps = c.prepareStatement(sql); + try (PreparedStatement ps = conn.prepareStatement( + "update NETWORK_ROUTES set VIANODE = ? where FROMNODE = ? and TONODE = ?")) { ps.setInt(1, vianode); ps.setInt(2, fromnode); ps.setInt(3, tonode); ps.executeUpdate(); } catch (SQLException e) { rv = false; - intlogger.warn("PROV0006 doUpdate: " + e.getMessage()); - e.printStackTrace(); - } finally { - try { - if(ps!=null) { - ps.close(); - } - } catch (SQLException e) { - e.printStackTrace(); - } + intlogger.warn("PROV0006 doUpdate: " + e.getMessage(), e); } return rv; } @@ -214,8 +200,9 @@ public class NetworkRoute extends NodeClass implements Comparable @Override public boolean equals(Object obj) { - if (!(obj instanceof NetworkRoute)) + if (!(obj instanceof NetworkRoute)) { return false; + } NetworkRoute on = (NetworkRoute) obj; return (fromnode == on.fromnode) && (tonode == on.tonode) && (vianode == on.vianode); } @@ -226,13 +213,14 @@ public class NetworkRoute extends NodeClass implements Comparable } @Override - public int compareTo(NetworkRoute o) { - if (this.fromnode == o.fromnode) { - if (this.tonode == o.tonode) - return this.vianode - o.vianode; - return this.tonode - o.tonode; + public int compareTo(NetworkRoute nr) { + if (this.fromnode == nr.fromnode) { + if (this.tonode == nr.tonode) { + return this.vianode - nr.vianode; + } + return this.tonode - nr.tonode; } - return this.fromnode - o.fromnode; + return this.fromnode - nr.fromnode; } @Override