X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=datarouter-prov%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fdmaap%2Fdatarouter%2Fprovisioning%2Fbeans%2FIngressRoute.java;h=973f868db4b158b245bcfb89023f41c50d85616d;hb=bda6aeaa60607ab4fe5af508156019d7bd5c0ce4;hp=1df093df4ac6a5af7a5529b74399e8d88f85b361;hpb=f20778ffa99aa9c6f30a0f84112a5392b259ea63;p=dmaap%2Fdatarouter.git diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/IngressRoute.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/IngressRoute.java index 1df093df..973f868d 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/IngressRoute.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/IngressRoute.java @@ -32,16 +32,15 @@ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; -import java.sql.Statement; import java.util.Collection; import java.util.Set; import java.util.SortedSet; import java.util.TreeSet; -import javax.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletRequest; import org.apache.commons.codec.binary.Base64; import org.json.JSONArray; 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 Ingress Route Table. @@ -49,6 +48,7 @@ import org.onap.dmaap.datarouter.provisioning.utils.DB; * @author Robert P. Eby * @version $Id: IngressRoute.java,v 1.3 2013/12/16 20:30:23 eby Exp $ */ + public class IngressRoute extends NodeClass implements Comparable { private static final String NODESET = "NODESET"; @@ -61,6 +61,14 @@ public class IngressRoute extends NodeClass implements Comparable private int nodelist; private SortedSet nodes; + /** + * Ingress route constructor. + * @param seq squence number + * @param feedid id for feed + * @param user user name + * @param subnet subnet string + * @param nodes collection of nodes + */ public IngressRoute(int seq, int feedid, String user, String subnet, Collection nodes) { this(seq, feedid, user, subnet); this.nodelist = -1; @@ -91,13 +99,17 @@ public class IngressRoute extends NodeClass implements Comparable } } + /** + * Ingress route constructor. + * @param jo JSONObject + */ public IngressRoute(JSONObject jo) { this.seq = jo.optInt("seq"); this.feedid = jo.optInt("feedid"); - String t = jo.optString("user"); - this.userid = "".equals(t) ? "-" : t; - t = jo.optString("subnet"); - this.subnet = "".equals(t) ? "-" : t; + String user = jo.optString("user"); + this.userid = "".equals(user) ? "-" : user; + user = jo.optString("subnet"); + this.subnet = "".equals(user) ? "-" : user; this.nodelist = -1; this.nodes = new TreeSet<>(); JSONArray ja = jo.getJSONArray("node"); @@ -124,21 +136,15 @@ public class IngressRoute extends NodeClass implements Comparable */ public static Set getIngressRoutesForSeq(int seq) { return getAllIngressRoutesForSQL( - "select SEQUENCE, FEEDID, USERID, SUBNET, NODESET from INGRESS_ROUTES where SEQUENCE = " + seq); + "select SEQUENCE, FEEDID, USERID, SUBNET, NODESET from INGRESS_ROUTES where SEQUENCE = " + seq); } private static SortedSet getAllIngressRoutesForSQL(String sql) { 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(sql)) { - addIngressRouteToSet(set, rs); - } - } - db.release(conn); + try (Connection conn = ProvDbUtils.getInstance().getConnection(); + PreparedStatement ps = conn.prepareStatement(sql); + ResultSet rs = ps.executeQuery()) { + addIngressRouteToSet(set, rs); } catch (SQLException e) { intlogger.error("PROV0001 getAllIngressRoutesForSQL: " + e.getMessage(), e); } @@ -176,15 +182,12 @@ public class IngressRoute extends NodeClass implements Comparable private static int getMax(String sql) { int rv = 0; - DB db = new DB(); - try (Connection conn = db.getConnection(); - Statement stmt = conn.createStatement()) { - try (ResultSet rs = stmt.executeQuery(sql)) { - if (rs.next()) { - rv = rs.getInt("MAX"); - } + try (Connection conn = ProvDbUtils.getInstance().getConnection(); + PreparedStatement ps = conn.prepareStatement(sql); + ResultSet rs = ps.executeQuery(sql)) { + if (rs.next()) { + rv = rs.getInt("MAX"); } - db.release(conn); } catch (SQLException e) { intlogger.error("PROV0002 getMax: " + e.getMessage(), e); } @@ -200,11 +203,10 @@ public class IngressRoute extends NodeClass implements Comparable * @return the Ingress Route, or null of there is none */ public static IngressRoute getIngressRoute(int feedid, String user, String subnet) { - IngressRoute v = null; - DB db = new DB(); - String sql = "select SEQUENCE, NODESET from INGRESS_ROUTES where FEEDID = ? AND USERID = ? and SUBNET = ?"; - try (Connection conn = db.getConnection(); - PreparedStatement ps = conn.prepareStatement(sql)) { + IngressRoute ir = null; + try (Connection conn = ProvDbUtils.getInstance().getConnection(); + PreparedStatement ps = conn.prepareStatement( + "select SEQUENCE, NODESET from INGRESS_ROUTES where FEEDID = ? AND USERID = ? and SUBNET = ?")) { ps.setInt(1, feedid); ps.setString(2, user); ps.setString(3, subnet); @@ -212,14 +214,13 @@ public class IngressRoute extends NodeClass implements Comparable if (rs.next()) { int seq = rs.getInt("SEQUENCE"); int nodeset = rs.getInt(NODESET); - v = new IngressRoute(seq, feedid, user, subnet, nodeset); + ir = new IngressRoute(seq, feedid, user, subnet, nodeset); } } - db.release(conn); } catch (SQLException e) { intlogger.error("PROV0003 getIngressRoute: " + e.getMessage(), e); } - return v; + return ir; } /** @@ -243,12 +244,12 @@ public class IngressRoute extends NodeClass implements Comparable if (credentials == null || !credentials.startsWith("Basic ")) { return false; } - String t = new String(Base64.decodeBase64(credentials.substring(6))); - int ix = t.indexOf(':'); + String cred = new String(Base64.decodeBase64(credentials.substring(6))); + int ix = cred.indexOf(':'); if (ix >= 0) { - t = t.substring(0, ix); + cred = cred.substring(0, ix); } - if (!t.equals(this.userid)) { + if (!cred.equals(this.userid)) { return false; } } @@ -270,7 +271,7 @@ public class IngressRoute extends NodeClass implements Comparable * Compare IP addresses as byte arrays to a subnet specified as a CIDR. Taken from * org.onap.dmaap.datarouter.node.SubnetMatcher and modified somewhat. */ - public class SubnetMatcher { + public static class SubnetMatcher { private byte[] sn; private int len; @@ -282,9 +283,9 @@ public class IngressRoute extends NodeClass implements Comparable * * @param subnet The CIDR to match */ - public SubnetMatcher(String subnet) { - int i = subnet.lastIndexOf('/'); - if (i == -1) { + SubnetMatcher(String subnet) { + int index = subnet.lastIndexOf('/'); + if (index == -1) { try { sn = InetAddress.getByName(subnet).getAddress(); len = sn.length; @@ -296,16 +297,16 @@ public class IngressRoute extends NodeClass implements Comparable } mask = 0; } else { - int n = Integer.parseInt(subnet.substring(i + 1)); + int num = Integer.parseInt(subnet.substring(index + 1)); try { - sn = InetAddress.getByName(subnet.substring(0, i)).getAddress(); + sn = InetAddress.getByName(subnet.substring(0, index)).getAddress(); valid = true; } catch (UnknownHostException e) { intlogger.error("PROV0008 SubnetMatcher: " + e.getMessage(), e); valid = false; } - len = n / 8; - mask = ((0xff00) >> (n % 8)) & 0xff; + len = num / 8; + mask = ((0xff00) >> (num % 8)) & 0xff; } } @@ -346,14 +347,10 @@ public class IngressRoute extends NodeClass implements Comparable private Collection readNodes() { Collection set = new TreeSet<>(); - DB db = new DB(); - String sql = "select NODEID from NODESETS where SETID = ?"; - try (Connection conn = db.getConnection()) { - try (PreparedStatement ps = conn.prepareStatement(sql)) { - ps.setInt(1, nodelist); - addNodeToSet(set, ps); - } - db.release(conn); + try (Connection conn = ProvDbUtils.getInstance().getConnection(); + PreparedStatement ps = conn.prepareStatement("select NODEID from NODESETS where SETID = ?")) { + ps.setInt(1, nodelist); + addNodeToSet(set, ps); } catch (SQLException e) { intlogger.error(SQLEXCEPTION + e.getMessage(), e); } @@ -375,33 +372,32 @@ public class IngressRoute extends NodeClass implements Comparable * @return true if the delete succeeded */ @Override - public boolean doDelete(Connection c) { + public boolean doDelete(Connection conn) { boolean rv = true; - try (PreparedStatement ps = c.prepareStatement( - "delete from INGRESS_ROUTES where FEEDID = ? and USERID = ? and SUBNET = ?"); - PreparedStatement ps2 = c.prepareStatement("delete from NODESETS where SETID = ?")) { + try (PreparedStatement ps = conn.prepareStatement( + "delete from INGRESS_ROUTES where FEEDID = ? and USERID = ? and SUBNET = ?"); + PreparedStatement ps2 = conn.prepareStatement("delete from NODESETS where SETID = ?")) { // Delete the Ingress Route ps.setInt(1, feedid); ps.setString(2, userid); ps.setString(3, subnet); ps.execute(); - ps.close(); // Delete the NodeSet ps2.setInt(1, nodelist); ps2.execute(); } catch (SQLException e) { rv = false; - intlogger.warn("PROV0007 doDelete: " + e.getMessage(), e); + intlogger.error("PROV0007 doDelete: " + e.getMessage(), e); } return rv; } @Override - public boolean doInsert(Connection c) { + public boolean doInsert(Connection conn) { boolean rv = false; - try (PreparedStatement ps = c.prepareStatement("insert into NODESETS (SETID, NODEID) values (?,?)"); - PreparedStatement ps2 = c.prepareStatement("insert into INGRESS_ROUTES (SEQUENCE, FEEDID, USERID," - + " SUBNET, NODESET) values (?, ?, ?, ?, ?)")) { + try (PreparedStatement ps = conn.prepareStatement("insert into NODESETS (SETID, NODEID) values (?,?)"); + PreparedStatement ps2 = conn.prepareStatement("insert into INGRESS_ROUTES (SEQUENCE, FEEDID, USERID," + + " SUBNET, NODESET) values (?, ?, ?, ?, ?)")) { // Create the NODESETS rows & set nodelist this.nodelist = getMaxNodeSetID() + 1; for (String node : nodes) { @@ -419,14 +415,14 @@ public class IngressRoute extends NodeClass implements Comparable ps2.execute(); rv = true; } catch (SQLException e) { - intlogger.warn("PROV0005 doInsert: " + e.getMessage(), e); + intlogger.error("PROV0005 doInsert: " + e.getMessage(), e); } return rv; } @Override - public boolean doUpdate(Connection c) { - return doDelete(c) && doInsert(c); + public boolean doUpdate(Connection conn) { + return doDelete(conn) && doInsert(conn); } @Override @@ -448,7 +444,7 @@ public class IngressRoute extends NodeClass implements Comparable @Override public String getKey() { return String - .format("%d/%s/%s/%d", feedid, (userid == null) ? "" : userid, (subnet == null) ? "" : subnet, seq); + .format("%d/%s/%s/%d", feedid, (userid == null) ? "" : userid, (subnet == null) ? "" : subnet, seq); } @Override @@ -469,21 +465,21 @@ public class IngressRoute extends NodeClass implements Comparable if (in == null) { throw new NullPointerException(); } - int n = this.feedid - in.feedid; - if (n != 0) { - return n; + int num = this.feedid - in.feedid; + if (num != 0) { + return num; } - n = this.seq - in.seq; - if (n != 0) { - return n; + num = this.seq - in.seq; + if (num != 0) { + return num; } - n = this.userid.compareTo(in.userid); - if (n != 0) { - return n; + num = this.userid.compareTo(in.userid); + if (num != 0) { + return num; } - n = this.subnet.compareTo(in.subnet); - if (n != 0) { - return n; + num = this.subnet.compareTo(in.subnet); + if (num != 0) { + return num; } return this.nodes.equals(in.nodes) ? 0 : 1; } @@ -491,6 +487,6 @@ public class IngressRoute extends NodeClass implements Comparable @Override public String toString() { return String.format("INGRESS: feed=%d, userid=%s, subnet=%s, seq=%d", feedid, (userid == null) ? "" : userid, - (subnet == null) ? "" : subnet, seq); + (subnet == null) ? "" : subnet, seq); } }