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=4332d7d7167c3af562b82042f941b2ad6ef43af8;hb=9730517e01a5c8d866f6c0d8e9051a92ccf29dd6;hp=a4ed60a269beb487789913d819b67a7c5d7b77fe;hpb=cced8d2c272c83dfafbd0832a47846f1dc9bd295;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 a4ed60a2..4332d7d7 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 @@ -39,8 +39,9 @@ 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.apache.log4j.Logger; import org.json.JSONArray; import org.json.JSONObject; import org.onap.dmaap.datarouter.provisioning.utils.DB; @@ -52,7 +53,9 @@ import org.onap.dmaap.datarouter.provisioning.utils.DB; * @version $Id: IngressRoute.java,v 1.3 2013/12/16 20:30:23 eby Exp $ */ public class IngressRoute 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 seq; private final int feedid; private final String userid; @@ -76,7 +79,8 @@ public class IngressRoute extends NodeClass implements Comparable * @return a set of IngressRoutes */ public static Set getIngressRoutesForSeq(int seq) { - return getAllIngressRoutesForSQL("select SEQUENCE, FEEDID, USERID, SUBNET, NODESET from INGRESS_ROUTES where SEQUENCE = " + seq); + return getAllIngressRoutesForSQL( + "select SEQUENCE, FEEDID, USERID, SUBNET, NODESET from INGRESS_ROUTES where SEQUENCE = " + seq); } private static SortedSet getAllIngressRoutesForSQL(String sql) { @@ -85,8 +89,8 @@ public class IngressRoute extends NodeClass implements Comparable DB db = new DB(); @SuppressWarnings("resource") Connection conn = db.getConnection(); - try(Statement stmt = conn.createStatement()) { - try(ResultSet rs = stmt.executeQuery(sql)) { + try (Statement stmt = conn.createStatement()) { + try (ResultSet rs = stmt.executeQuery(sql)) { while (rs.next()) { int seq = rs.getInt("SEQUENCE"); int feedid = rs.getInt("FEEDID"); @@ -99,7 +103,7 @@ public class IngressRoute extends NodeClass implements Comparable } db.release(conn); } catch (SQLException e) { - e.printStackTrace(); + intlogger.error("PROV0001 getAllIngressRoutesForSQL: " + e.getMessage(), e); } return set; } @@ -128,16 +132,16 @@ public class IngressRoute extends NodeClass implements Comparable 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"); - } - } + try (Statement stmt = conn.createStatement()) { + try (ResultSet rs = stmt.executeQuery(sql)) { + if (rs.next()) { + rv = rs.getInt("MAX"); + } + } } db.release(conn); } catch (SQLException e) { - e.printStackTrace(); + intlogger.error("PROV0002 getMax: " + e.getMessage(), e); } return rv; } @@ -146,7 +150,7 @@ public class IngressRoute extends NodeClass implements Comparable * 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 + * @param user the user name to look for * @param subnet the subnet to look for * @return the Ingress Route, or null of there is none */ @@ -162,7 +166,7 @@ public class IngressRoute extends NodeClass implements Comparable ps.setInt(1, feedid); ps.setString(2, user); ps.setString(3, subnet); - try(ResultSet rs = ps.executeQuery()) { + try (ResultSet rs = ps.executeQuery()) { if (rs.next()) { int seq = rs.getInt("SEQUENCE"); int nodeset = rs.getInt("NODESET"); @@ -172,14 +176,14 @@ public class IngressRoute extends NodeClass implements Comparable ps.close(); db.release(conn); } catch (SQLException e) { - e.printStackTrace(); + intlogger.error("PROV0003 getIngressRoute: " + e.getMessage(), e); } finally { try { - if(ps!=null) { + if (ps != null) { ps.close(); } } catch (SQLException e) { - e.printStackTrace(); + intlogger.error(SQLEXCEPTION + e.getMessage(), e); } } return v; @@ -198,9 +202,9 @@ public class IngressRoute extends NodeClass implements Comparable @SuppressWarnings("resource") Connection conn = db.getConnection(); String sql = "select FEEDID, USERID, SUBNET, NODESET from INGRESS_ROUTES where SEQUENCE = ?"; - try(PreparedStatement ps = conn.prepareStatement(sql)) { + try (PreparedStatement ps = conn.prepareStatement(sql)) { ps.setInt(1, seq); - try(ResultSet rs = ps.executeQuery()) { + try (ResultSet rs = ps.executeQuery()) { while (rs.next()) { int feedid = rs.getInt("FEEDID"); String user = rs.getString("USERID"); @@ -212,7 +216,7 @@ public class IngressRoute extends NodeClass implements Comparable } db.release(conn); } catch (SQLException e) { - e.printStackTrace(); + intlogger.error("PROV0004 getIngressRoute: " + e.getMessage(), e); } return rv; } @@ -239,12 +243,14 @@ public class IngressRoute extends NodeClass implements Comparable this.subnet = (subnet == null) ? "-" : subnet; this.nodelist = -1; this.nodes = null; - if (Feed.getFeedById(feedid) == 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()) + if (!sm.isValid()) { throw new IllegalArgumentException("Invalid subnet: " + subnet); + } } } @@ -258,37 +264,41 @@ public class IngressRoute extends NodeClass implements Comparable this.nodelist = -1; this.nodes = new TreeSet(); JSONArray ja = jo.getJSONArray("node"); - for (int i = 0; i < ja.length(); i++) + for (int i = 0; i < ja.length(); i++) { this.nodes.add(ja.getString(i)); + } } /** - * Does this particular IngressRoute match a request, represented by feedid and req? - * To match, feedid must match the feed ID in the route, the user in the route - * (if specified) must match the user in the request, and the subnet in the route (if specified) - * must match the subnet from the request. + * Does this particular IngressRoute match a request, represented by feedid and req? To match, feedid must + * match the feed ID in the route, the user in the route (if specified) must match the user in the request, and the + * subnet in the route (if specified) must match the subnet from the request. * * @param feedid the feedid for this request - * @param req the remainder of the request + * @param req the remainder of the request * @return true if a match, false otherwise */ public boolean matches(int feedid, HttpServletRequest req) { // Check feedid - if (this.feedid != feedid) + 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("-")) { String credentials = req.getHeader("Authorization"); - if (credentials == null || !credentials.startsWith("Basic ")) + if (credentials == null || !credentials.startsWith("Basic ")) { return false; + } String t = new String(Base64.decodeBase64(credentials.substring(6))); int ix = t.indexOf(':'); - if (ix >= 0) + if (ix >= 0) { t = t.substring(0, ix); - if (!t.equals(this.userid)) + } + if (!t.equals(this.userid)) { return false; + } } // If this route has a subnet, match it against the requester's IP addr @@ -298,6 +308,7 @@ public class IngressRoute extends NodeClass implements Comparable SubnetMatcher sm = new SubnetMatcher(subnet); return sm.matches(inet.getAddress()); } catch (UnknownHostException e) { + intlogger.error("PROV0008 matches: " + e.getMessage(), e); return false; } } @@ -305,10 +316,11 @@ 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. + * 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 { + private byte[] sn; private int len; private int mask; @@ -327,6 +339,7 @@ public class IngressRoute extends NodeClass implements Comparable len = sn.length; valid = true; } catch (UnknownHostException e) { + intlogger.error("PROV0008 SubnetMatcher: " + e.getMessage(), e); len = 0; valid = false; } @@ -337,6 +350,7 @@ public class IngressRoute extends NodeClass implements Comparable sn = InetAddress.getByName(subnet.substring(0, i)).getAddress(); valid = true; } catch (UnknownHostException e) { + intlogger.error("PROV0008 SubnetMatcher: " + e.getMessage(), e); valid = false; } len = n / 8; @@ -380,15 +394,15 @@ public class IngressRoute extends NodeClass implements Comparable } private Collection readNodes() { - Collection set = new TreeSet(); + Collection set = new TreeSet<>(); try { DB db = new DB(); @SuppressWarnings("resource") Connection conn = db.getConnection(); String sql = "select NODEID from NODESETS where SETID = ?"; - try(PreparedStatement ps = conn.prepareStatement(sql)) { + try (PreparedStatement ps = conn.prepareStatement(sql)) { ps.setInt(1, nodelist); - try(ResultSet rs = ps.executeQuery()) { + try (ResultSet rs = ps.executeQuery()) { while (rs.next()) { int id = rs.getInt("NODEID"); set.add(lookupNodeID(id)); @@ -397,7 +411,7 @@ public class IngressRoute extends NodeClass implements Comparable } db.release(conn); } catch (SQLException e) { - e.printStackTrace(); + intlogger.error(SQLEXCEPTION + e.getMessage(), e); } return set; } @@ -424,15 +438,14 @@ public class IngressRoute extends NodeClass implements Comparable ps.execute(); } catch (SQLException e) { rv = false; - intlogger.warn("PROV0007 doDelete: " + e.getMessage()); - e.printStackTrace(); + intlogger.warn("PROV0007 doDelete: " + e.getMessage(), e); } finally { try { - if(ps!=null) { + if (ps != null) { ps.close(); } } catch (SQLException e) { - e.printStackTrace(); + intlogger.error(SQLEXCEPTION + e.getMessage(), e); } } return rv; @@ -457,7 +470,8 @@ public class IngressRoute extends NodeClass implements Comparable } // Create the INGRESS_ROUTES row - ps = c.prepareStatement("insert into INGRESS_ROUTES (SEQUENCE, FEEDID, USERID, SUBNET, NODESET) values (?, ?, ?, ?, ?)"); + 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); @@ -467,15 +481,14 @@ public class IngressRoute extends NodeClass implements Comparable ps.close(); rv = true; } catch (SQLException e) { - intlogger.warn("PROV0005 doInsert: " + e.getMessage()); - e.printStackTrace(); + intlogger.warn("PROV0005 doInsert: " + e.getMessage(), e); } finally { try { - if(ps!=null) { + if (ps != null) { ps.close(); } } catch (SQLException e) { - e.printStackTrace(); + intlogger.error(SQLEXCEPTION + e.getMessage(), e); } } return rv; @@ -491,10 +504,12 @@ 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 && !userid.equals("-") && !userid.equals("")) { jo.put("user", userid); - if (subnet != null && !subnet.equals("-") && !subnet.equals("")) + } + if (subnet != null && !subnet.equals("-") && !subnet.equals("")) { jo.put("subnet", subnet); + } jo.put("seq", seq); jo.put("node", nodes); return jo; @@ -502,7 +517,8 @@ 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); + return String + .format("%d/%s/%s/%d", feedid, (userid == null) ? "" : userid, (subnet == null) ? "" : subnet, seq); } @Override @@ -512,36 +528,39 @@ public class IngressRoute extends NodeClass implements Comparable @Override public boolean equals(Object obj) { - try { - if (!(obj instanceof IngressRoute)) - return false; - return this.compareTo((IngressRoute) obj) == 0; - } catch (NullPointerException e) { + if (!(obj instanceof IngressRoute)) { return false; } + return this.compareTo((IngressRoute) obj) == 0; } @Override public int compareTo(IngressRoute in) { - if (in == null) + if (in == null) { throw new NullPointerException(); + } int n = this.feedid - in.feedid; - if (n != 0) + if (n != 0) { return n; + } n = this.seq - in.seq; - if (n != 0) + if (n != 0) { return n; + } n = this.userid.compareTo(in.userid); - if (n != 0) + if (n != 0) { return n; + } n = this.subnet.compareTo(in.subnet); - if (n != 0) + if (n != 0) { return n; + } return this.nodes.equals(in.nodes) ? 0 : 1; } @Override public String toString() { - return String.format("INGRESS: feed=%d, userid=%s, subnet=%s, seq=%d", feedid, (userid == null) ? "" : userid, (subnet == null) ? "" : subnet, seq); + return String.format("INGRESS: feed=%d, userid=%s, subnet=%s, seq=%d", feedid, (userid == null) ? "" : userid, + (subnet == null) ? "" : subnet, seq); } }