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%2FIngressRoute.java;h=f1fa54c70b4673fe444422b90009682c167ebef9;hp=4332d7d7167c3af562b82042f941b2ad6ef43af8;hb=bc1df610cddfb558cf6bde90c269b4af59768648;hpb=376688e95bd516282bc55ca88318b12043aa25dd 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 4332d7d7..f1fa54c7 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 @@ -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.net.InetAddress; import java.net.UnknownHostException; import java.sql.Connection; @@ -31,16 +33,11 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; -import java.util.ArrayList; import java.util.Collection; import java.util.Set; import java.util.SortedSet; import java.util.TreeSet; - import javax.servlet.http.HttpServletRequest; - -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; import org.apache.commons.codec.binary.Base64; import org.json.JSONArray; import org.json.JSONObject; @@ -52,8 +49,10 @@ 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"; private static EELFLogger intlogger = EELFManager.getInstance().getLogger("InternalLog"); private static final String SQLEXCEPTION = "SQLException: "; private final int seq; @@ -63,6 +62,64 @@ 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; + this.nodes = new TreeSet<>(nodes); + } + + private IngressRoute(int seq, int feedid, String user, String subnet, int nodeset) { + this(seq, feedid, user, subnet); + this.nodelist = nodeset; + this.nodes = new TreeSet<>(readNodes()); + } + + private IngressRoute(int seq, int feedid, String user, String subnet) { + this.seq = seq; + this.feedid = feedid; + this.userid = (user == null) ? "-" : user; + this.subnet = (subnet == null) ? "-" : subnet; + this.nodelist = -1; + this.nodes = null; + if (Feed.getFeedById(feedid) == null) { + throw new IllegalArgumentException("No such feed: " + feedid); + } + if (!"-".equals(this.subnet)) { + SubnetMatcher sm = new SubnetMatcher(subnet); + if (!sm.isValid()) { + throw new IllegalArgumentException("Invalid subnet: " + subnet); + } + } + } + + /** + * Ingress route constructor. + * @param jo JSONObject + */ + public IngressRoute(JSONObject jo) { + this.seq = jo.optInt("seq"); + this.feedid = jo.optInt("feedid"); + 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"); + for (int i = 0; i < ja.length(); i++) { + this.nodes.add(ja.getString(i)); + } + } + + /** * Get all IngressRoutes in the database, sorted in order according to their sequence field. * @@ -84,21 +141,14 @@ public class IngressRoute extends NodeClass implements Comparable } private static SortedSet getAllIngressRoutesForSQL(String sql) { - SortedSet set = new TreeSet(); + 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)) { - while (rs.next()) { - int seq = rs.getInt("SEQUENCE"); - int feedid = rs.getInt("FEEDID"); - String user = rs.getString("USERID"); - String subnet = rs.getString("SUBNET"); - int nodeset = rs.getInt("NODESET"); - set.add(new IngressRoute(seq, feedid, user, subnet, nodeset)); - } + addIngressRouteToSet(set, rs); } } db.release(conn); @@ -108,12 +158,23 @@ public class IngressRoute extends NodeClass implements Comparable return set; } + private static void addIngressRouteToSet(SortedSet set, ResultSet rs) throws SQLException { + while (rs.next()) { + int seq = rs.getInt("SEQUENCE"); + int feedid = rs.getInt("FEEDID"); + String user = rs.getString("USERID"); + String subnet = rs.getString("SUBNET"); + int nodeset = rs.getInt(NODESET); + set.add(new IngressRoute(seq, feedid, user, subnet, nodeset)); + } + } + /** * Get the maximum node set ID in use in the DB. * * @return the integer value of the maximum */ - public static int getMaxNodeSetID() { + private static int getMaxNodeSetID() { return getMax("select max(SETID) as MAX from NODESETS"); } @@ -128,15 +189,12 @@ public class IngressRoute extends NodeClass implements Comparable private static int getMax(String sql) { int rv = 0; - try { - DB db = new DB(); - @SuppressWarnings("resource") - Connection conn = db.getConnection(); - try (Statement stmt = conn.createStatement()) { - try (ResultSet rs = stmt.executeQuery(sql)) { - if (rs.next()) { - rv = rs.getInt("MAX"); - } + 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"); } } db.release(conn); @@ -147,7 +205,7 @@ public class IngressRoute extends NodeClass implements Comparable } /** - * Get an Ingress Route for a particular feed ID, user, and subnet + * Get an Ingress Route for a particular feed ID, user, and subnet. * * @param feedid the Feed ID to look for * @param user the user name to look for @@ -155,118 +213,26 @@ 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; - PreparedStatement ps = null; - try { - DB db = new DB(); - @SuppressWarnings("resource") - Connection conn = db.getConnection(); - String sql = "select SEQUENCE, NODESET from INGRESS_ROUTES where FEEDID = ? AND USERID = ? and SUBNET = ?"; - ps = conn.prepareStatement(sql); + IngressRoute ir = 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)) { ps.setInt(1, feedid); ps.setString(2, user); ps.setString(3, subnet); try (ResultSet rs = ps.executeQuery()) { if (rs.next()) { int seq = rs.getInt("SEQUENCE"); - int nodeset = rs.getInt("NODESET"); - v = new IngressRoute(seq, feedid, user, subnet, nodeset); + int nodeset = rs.getInt(NODESET); + ir = new IngressRoute(seq, feedid, user, subnet, nodeset); } } - ps.close(); db.release(conn); } catch (SQLException e) { intlogger.error("PROV0003 getIngressRoute: " + e.getMessage(), e); - } finally { - try { - if (ps != null) { - ps.close(); - } - } catch (SQLException e) { - intlogger.error(SQLEXCEPTION + e.getMessage(), e); - } - } - return v; - } - - /** - * Get a collection of all Ingress Routes with a particular sequence number. - * - * @param seq the sequence number to look for - * @return the collection (may be empty). - */ - public static Collection getIngressRoute(int seq) { - Collection rv = new ArrayList(); - try { - DB db = new DB(); - @SuppressWarnings("resource") - Connection conn = db.getConnection(); - String sql = "select FEEDID, USERID, SUBNET, NODESET from INGRESS_ROUTES where SEQUENCE = ?"; - try (PreparedStatement ps = conn.prepareStatement(sql)) { - ps.setInt(1, seq); - try (ResultSet rs = ps.executeQuery()) { - while (rs.next()) { - int feedid = rs.getInt("FEEDID"); - String user = rs.getString("USERID"); - String subnet = rs.getString("SUBNET"); - int nodeset = rs.getInt("NODESET"); - rv.add(new IngressRoute(seq, feedid, user, subnet, nodeset)); - } - } - } - db.release(conn); - } catch (SQLException e) { - intlogger.error("PROV0004 getIngressRoute: " + e.getMessage(), e); - } - return rv; - } - - public IngressRoute(int seq, int feedid, String user, String subnet, Collection nodes) - throws IllegalArgumentException { - this(seq, feedid, user, subnet); - this.nodelist = -1; - this.nodes = new TreeSet(nodes); - } - - public IngressRoute(int seq, int feedid, String user, String subnet, int nodeset) - throws IllegalArgumentException { - this(seq, feedid, user, subnet); - this.nodelist = nodeset; - this.nodes = new TreeSet(readNodes()); - } - - private IngressRoute(int seq, int feedid, String user, String subnet) - throws IllegalArgumentException { - this.seq = seq; - this.feedid = feedid; - this.userid = (user == null) ? "-" : user; - this.subnet = (subnet == null) ? "-" : subnet; - this.nodelist = -1; - this.nodes = null; - if (Feed.getFeedById(feedid) == null) { - throw new IllegalArgumentException("No such feed: " + feedid); - } - if (!this.subnet.equals("-")) { - SubnetMatcher sm = new SubnetMatcher(subnet); - if (!sm.isValid()) { - throw new IllegalArgumentException("Invalid subnet: " + subnet); - } - } - } - - public IngressRoute(JSONObject jo) { - this.seq = jo.optInt("seq"); - this.feedid = jo.optInt("feedid"); - String t = jo.optString("user"); - this.userid = t.equals("") ? "-" : t; - t = jo.optString("subnet"); - this.subnet = t.equals("") ? "-" : t; - this.nodelist = -1; - this.nodes = new TreeSet(); - JSONArray ja = jo.getJSONArray("node"); - for (int i = 0; i < ja.length(); i++) { - this.nodes.add(ja.getString(i)); } + return ir; } /** @@ -283,26 +249,24 @@ public class IngressRoute extends NodeClass implements Comparable if (this.feedid != feedid) { return false; } - // Get user from request and compare // Note: we don't check the password; the node will do that - if (userid.length() > 0 && !userid.equals("-")) { + if (userid.length() > 0 && !"-".equals(userid)) { String credentials = req.getHeader("Authorization"); 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; } } - // If this route has a subnet, match it against the requester's IP addr - if (subnet.length() > 0 && !subnet.equals("-")) { + if (subnet.length() > 0 && !"-".equals(subnet)) { try { InetAddress inet = InetAddress.getByName(req.getRemoteAddr()); SubnetMatcher sm = new SubnetMatcher(subnet); @@ -327,13 +291,13 @@ public class IngressRoute extends NodeClass implements Comparable private boolean valid; /** - * Construct a subnet matcher given a CIDR + * Construct a subnet matcher given a CIDR. * * @param subnet The CIDR to match */ public SubnetMatcher(String subnet) { - int i = subnet.lastIndexOf('/'); - if (i == -1) { + int index = subnet.lastIndexOf('/'); + if (index == -1) { try { sn = InetAddress.getByName(subnet).getAddress(); len = sn.length; @@ -345,31 +309,31 @@ 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; } } - public boolean isValid() { + boolean isValid() { return valid; } /** - * Is the IP address in the CIDR? + * Is the IP address in the CIDR?. * * @param addr the IP address as bytes in network byte order * @return true if the IP address matches. */ - public boolean matches(byte[] addr) { - if (!valid || addr.length != sn.length) { + boolean matches(byte[] addr) { + if (!valid || (addr.length != sn.length)) { return false; } for (int i = 0; i < len; i++) { @@ -395,19 +359,12 @@ public class IngressRoute extends NodeClass implements Comparable private Collection readNodes() { Collection set = new TreeSet<>(); - try { - DB db = new DB(); - @SuppressWarnings("resource") - Connection conn = db.getConnection(); - String sql = "select NODEID from NODESETS where SETID = ?"; + 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); - try (ResultSet rs = ps.executeQuery()) { - while (rs.next()) { - int id = rs.getInt("NODEID"); - set.add(lookupNodeID(id)); - } - } + addNodeToSet(set, ps); } db.release(conn); } catch (SQLException e) { @@ -416,87 +373,73 @@ public class IngressRoute extends NodeClass implements Comparable return set; } + private void addNodeToSet(Collection set, PreparedStatement ps) throws SQLException { + try (ResultSet rs = ps.executeQuery()) { + while (rs.next()) { + int id = rs.getInt("NODEID"); + set.add(lookupNodeID(id)); + } + } + } + /** * Delete the IRT route having this IngressRoutes feed ID, user ID, and subnet from the database. * * @return true if the delete succeeded */ @Override - public boolean doDelete(Connection c) { + public boolean doDelete(Connection conn) { boolean rv = true; - PreparedStatement ps = null; - try { - ps = c.prepareStatement("delete from INGRESS_ROUTES where FEEDID = ? and USERID = ? and SUBNET = ?"); + 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(); - - ps = c.prepareStatement("delete from NODESETS where SETID = ?"); - ps.setInt(1, nodelist); - ps.execute(); + // Delete the NodeSet + ps2.setInt(1, nodelist); + ps2.execute(); } catch (SQLException e) { rv = false; intlogger.warn("PROV0007 doDelete: " + e.getMessage(), e); - } finally { - try { - if (ps != null) { - ps.close(); - } - } catch (SQLException e) { - intlogger.error(SQLEXCEPTION + e.getMessage(), e); - } } return rv; } - @SuppressWarnings("resource") @Override - public boolean doInsert(Connection c) { + public boolean doInsert(Connection conn) { boolean rv = false; - PreparedStatement ps = null; - try { + 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 - int set = getMaxNodeSetID() + 1; - this.nodelist = set; + this.nodelist = getMaxNodeSetID() + 1; for (String node : nodes) { int id = lookupNodeName(node); - ps = c.prepareStatement("insert into NODESETS (SETID, NODEID) values (?,?)"); ps.setInt(1, this.nodelist); ps.setInt(2, id); ps.execute(); - ps.close(); } - // Create the INGRESS_ROUTES row - ps = c.prepareStatement( - "insert into INGRESS_ROUTES (SEQUENCE, FEEDID, USERID, SUBNET, NODESET) values (?, ?, ?, ?, ?)"); - ps.setInt(1, this.seq); - ps.setInt(2, this.feedid); - ps.setString(3, this.userid); - ps.setString(4, this.subnet); - ps.setInt(5, this.nodelist); - ps.execute(); - ps.close(); + ps2.setInt(1, this.seq); + ps2.setInt(2, this.feedid); + ps2.setString(3, this.userid); + ps2.setString(4, this.subnet); + ps2.setInt(5, this.nodelist); + ps2.execute(); rv = true; } catch (SQLException e) { intlogger.warn("PROV0005 doInsert: " + e.getMessage(), e); - } finally { - try { - if (ps != null) { - ps.close(); - } - } catch (SQLException e) { - intlogger.error(SQLEXCEPTION + 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 @@ -504,10 +447,10 @@ public class IngressRoute extends NodeClass implements Comparable JSONObject jo = new JSONObject(); jo.put("feedid", feedid); // Note: for user and subnet, null, "", and "-" are equivalent - if (userid != null && !userid.equals("-") && !userid.equals("")) { + if (userid != null && !"-".equals(userid) && !"".equals(userid)) { jo.put("user", userid); } - if (subnet != null && !subnet.equals("-") && !subnet.equals("")) { + if (subnet != null && !"-".equals(subnet) && !"".equals(subnet)) { jo.put("subnet", subnet); } jo.put("seq", seq); @@ -539,21 +482,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; }