X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=datarouter-prov%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fdmaap%2Fdatarouter%2Fprovisioning%2Fbeans%2FNetworkRoute.java;h=dd9a624ecaa64cbb8e2c8738cf238cf22f67520f;hb=bc1df610cddfb558cf6bde90c269b4af59768648;hp=8264de28087099161c9b042bd6838c69f0a563e9;hpb=3a2e2a602b9aa3677d941f3d5d65ea0dce80b7ab;p=dmaap%2Fdatarouter.git 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 8264de28..dd9a624e 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,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,8 +34,6 @@ 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; @@ -45,64 +45,91 @@ import org.onap.dmaap.datarouter.provisioning.utils.DB; */ 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) { - intlogger.error("SQLException " + e.getMessage()); - } - 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 { + 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")) { + addNetworkRouteToSet(set, rs); + } + } finally { + db.release(conn); + } + } 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; } @@ -116,82 +143,51 @@ public class NetworkRoute extends NodeClass implements Comparable } @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); + String sql = "delete from NETWORK_ROUTES where FROMNODE = ? AND TONODE = ?"; + try (PreparedStatement ps = conn.prepareStatement(sql)) { ps.setInt(1, fromnode); ps.setInt(2, tonode); ps.execute(); } catch (SQLException e) { rv = false; - intlogger.warn("PROV0007 doDelete: " + e.getMessage()); - } finally { - try { - if (ps != null) { - ps.close(); - } - } catch (SQLException e) { - intlogger.error("SQLException " + e.getMessage()); - } + intlogger.warn("PROV0007 doDelete: " + e.getMessage(), e); } return rv; } @Override - public boolean doInsert(Connection c) { + public boolean doInsert(Connection conn) { boolean rv = false; + String sql = "insert into NETWORK_ROUTES (FROMNODE, TONODE, VIANODE) values (?, ?, ?)"; if (this.vianode >= 0) { - PreparedStatement ps = null; - try { + try (PreparedStatement ps = conn.prepareStatement(sql)) { // 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()); - } 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 NETWORK_ROUTES set VIANODE = ? where FROMNODE = ? and TONODE = ?"; - ps = c.prepareStatement(sql); + String sql = "update NETWORK_ROUTES set VIANODE = ? where FROMNODE = ? and TONODE = ?"; + try (PreparedStatement ps = conn.prepareStatement(sql)) { 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()); - } finally { - try { - if (ps != null) { - ps.close(); - } - } catch (SQLException e) { - intlogger.error("SQLException " + e.getMessage()); - } + intlogger.warn("PROV0006 doUpdate: " + e.getMessage(), e); } return rv; } @@ -225,14 +221,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; + public int compareTo(NetworkRoute nr) { + if (this.fromnode == nr.fromnode) { + if (this.tonode == nr.tonode) { + return this.vianode - nr.vianode; } - return this.tonode - o.tonode; + return this.tonode - nr.tonode; } - return this.fromnode - o.fromnode; + return this.fromnode - nr.fromnode; } @Override