import java.sql.PreparedStatement;\r
import java.sql.ResultSet;\r
import java.sql.SQLException;\r
-import java.sql.Statement;\r
import java.util.Collection;\r
import java.util.Set;\r
import java.util.SortedSet;\r
import java.util.TreeSet;\r
-import javax.servlet.http.HttpServletRequest;\r
+import jakarta.servlet.http.HttpServletRequest;\r
import org.apache.commons.codec.binary.Base64;\r
import org.json.JSONArray;\r
import org.json.JSONObject;\r
-import org.onap.dmaap.datarouter.provisioning.utils.DB;\r
+import org.onap.dmaap.datarouter.provisioning.utils.ProvDbUtils;\r
\r
/**\r
* The representation of one route in the Ingress Route Table.\r
* @author Robert P. Eby\r
* @version $Id: IngressRoute.java,v 1.3 2013/12/16 20:30:23 eby Exp $\r
*/\r
+\r
public class IngressRoute extends NodeClass implements Comparable<IngressRoute> {\r
\r
private static final String NODESET = "NODESET";\r
private int nodelist;\r
private SortedSet<String> nodes;\r
\r
+ /**\r
+ * Ingress route constructor.\r
+ * @param seq squence number\r
+ * @param feedid id for feed\r
+ * @param user user name\r
+ * @param subnet subnet string\r
+ * @param nodes collection of nodes\r
+ */\r
public IngressRoute(int seq, int feedid, String user, String subnet, Collection<String> nodes) {\r
this(seq, feedid, user, subnet);\r
this.nodelist = -1;\r
}\r
}\r
\r
+ /**\r
+ * Ingress route constructor.\r
+ * @param jo JSONObject\r
+ */\r
public IngressRoute(JSONObject jo) {\r
this.seq = jo.optInt("seq");\r
this.feedid = jo.optInt("feedid");\r
- String t = jo.optString("user");\r
- this.userid = "".equals(t) ? "-" : t;\r
- t = jo.optString("subnet");\r
- this.subnet = "".equals(t) ? "-" : t;\r
+ String user = jo.optString("user");\r
+ this.userid = "".equals(user) ? "-" : user;\r
+ user = jo.optString("subnet");\r
+ this.subnet = "".equals(user) ? "-" : user;\r
this.nodelist = -1;\r
this.nodes = new TreeSet<>();\r
JSONArray ja = jo.getJSONArray("node");\r
*/\r
public static Set<IngressRoute> getIngressRoutesForSeq(int seq) {\r
return getAllIngressRoutesForSQL(\r
- "select SEQUENCE, FEEDID, USERID, SUBNET, NODESET from INGRESS_ROUTES where SEQUENCE = " + seq);\r
+ "select SEQUENCE, FEEDID, USERID, SUBNET, NODESET from INGRESS_ROUTES where SEQUENCE = " + seq);\r
}\r
\r
private static SortedSet<IngressRoute> getAllIngressRoutesForSQL(String sql) {\r
SortedSet<IngressRoute> set = new TreeSet<>();\r
- try {\r
- DB db = new DB();\r
- @SuppressWarnings("resource")\r
- Connection conn = db.getConnection();\r
- try (Statement stmt = conn.createStatement()) {\r
- try (ResultSet rs = stmt.executeQuery(sql)) {\r
- addIngressRouteToSet(set, rs);\r
- }\r
- }\r
- db.release(conn);\r
+ try (Connection conn = ProvDbUtils.getInstance().getConnection();\r
+ PreparedStatement ps = conn.prepareStatement(sql);\r
+ ResultSet rs = ps.executeQuery()) {\r
+ addIngressRouteToSet(set, rs);\r
} catch (SQLException e) {\r
intlogger.error("PROV0001 getAllIngressRoutesForSQL: " + e.getMessage(), e);\r
}\r
\r
private static int getMax(String sql) {\r
int rv = 0;\r
- DB db = new DB();\r
- try (Connection conn = db.getConnection();\r
- Statement stmt = conn.createStatement()) {\r
- try (ResultSet rs = stmt.executeQuery(sql)) {\r
- if (rs.next()) {\r
- rv = rs.getInt("MAX");\r
- }\r
+ try (Connection conn = ProvDbUtils.getInstance().getConnection();\r
+ PreparedStatement ps = conn.prepareStatement(sql);\r
+ ResultSet rs = ps.executeQuery(sql)) {\r
+ if (rs.next()) {\r
+ rv = rs.getInt("MAX");\r
}\r
- db.release(conn);\r
} catch (SQLException e) {\r
intlogger.error("PROV0002 getMax: " + e.getMessage(), e);\r
}\r
* @return the Ingress Route, or null of there is none\r
*/\r
public static IngressRoute getIngressRoute(int feedid, String user, String subnet) {\r
- IngressRoute v = null;\r
- DB db = new DB();\r
- String sql = "select SEQUENCE, NODESET from INGRESS_ROUTES where FEEDID = ? AND USERID = ? and SUBNET = ?";\r
- try (Connection conn = db.getConnection();\r
- PreparedStatement ps = conn.prepareStatement(sql)) {\r
+ IngressRoute ir = null;\r
+ try (Connection conn = ProvDbUtils.getInstance().getConnection();\r
+ PreparedStatement ps = conn.prepareStatement(\r
+ "select SEQUENCE, NODESET from INGRESS_ROUTES where FEEDID = ? AND USERID = ? and SUBNET = ?")) {\r
ps.setInt(1, feedid);\r
ps.setString(2, user);\r
ps.setString(3, subnet);\r
if (rs.next()) {\r
int seq = rs.getInt("SEQUENCE");\r
int nodeset = rs.getInt(NODESET);\r
- v = new IngressRoute(seq, feedid, user, subnet, nodeset);\r
+ ir = new IngressRoute(seq, feedid, user, subnet, nodeset);\r
}\r
}\r
- db.release(conn);\r
} catch (SQLException e) {\r
intlogger.error("PROV0003 getIngressRoute: " + e.getMessage(), e);\r
}\r
- return v;\r
+ return ir;\r
}\r
\r
/**\r
if (credentials == null || !credentials.startsWith("Basic ")) {\r
return false;\r
}\r
- String t = new String(Base64.decodeBase64(credentials.substring(6)));\r
- int ix = t.indexOf(':');\r
+ String cred = new String(Base64.decodeBase64(credentials.substring(6)));\r
+ int ix = cred.indexOf(':');\r
if (ix >= 0) {\r
- t = t.substring(0, ix);\r
+ cred = cred.substring(0, ix);\r
}\r
- if (!t.equals(this.userid)) {\r
+ if (!cred.equals(this.userid)) {\r
return false;\r
}\r
}\r
* Compare IP addresses as byte arrays to a subnet specified as a CIDR. Taken from\r
* org.onap.dmaap.datarouter.node.SubnetMatcher and modified somewhat.\r
*/\r
- public class SubnetMatcher {\r
+ public static class SubnetMatcher {\r
\r
private byte[] sn;\r
private int len;\r
*\r
* @param subnet The CIDR to match\r
*/\r
- public SubnetMatcher(String subnet) {\r
- int i = subnet.lastIndexOf('/');\r
- if (i == -1) {\r
+ SubnetMatcher(String subnet) {\r
+ int index = subnet.lastIndexOf('/');\r
+ if (index == -1) {\r
try {\r
sn = InetAddress.getByName(subnet).getAddress();\r
len = sn.length;\r
}\r
mask = 0;\r
} else {\r
- int n = Integer.parseInt(subnet.substring(i + 1));\r
+ int num = Integer.parseInt(subnet.substring(index + 1));\r
try {\r
- sn = InetAddress.getByName(subnet.substring(0, i)).getAddress();\r
+ sn = InetAddress.getByName(subnet.substring(0, index)).getAddress();\r
valid = true;\r
} catch (UnknownHostException e) {\r
intlogger.error("PROV0008 SubnetMatcher: " + e.getMessage(), e);\r
valid = false;\r
}\r
- len = n / 8;\r
- mask = ((0xff00) >> (n % 8)) & 0xff;\r
+ len = num / 8;\r
+ mask = ((0xff00) >> (num % 8)) & 0xff;\r
}\r
}\r
\r
\r
private Collection<String> readNodes() {\r
Collection<String> set = new TreeSet<>();\r
- DB db = new DB();\r
- String sql = "select NODEID from NODESETS where SETID = ?";\r
- try (Connection conn = db.getConnection()) {\r
- try (PreparedStatement ps = conn.prepareStatement(sql)) {\r
- ps.setInt(1, nodelist);\r
- addNodeToSet(set, ps);\r
- }\r
- db.release(conn);\r
+ try (Connection conn = ProvDbUtils.getInstance().getConnection();\r
+ PreparedStatement ps = conn.prepareStatement("select NODEID from NODESETS where SETID = ?")) {\r
+ ps.setInt(1, nodelist);\r
+ addNodeToSet(set, ps);\r
} catch (SQLException e) {\r
intlogger.error(SQLEXCEPTION + e.getMessage(), e);\r
}\r
* @return true if the delete succeeded\r
*/\r
@Override\r
- public boolean doDelete(Connection c) {\r
+ public boolean doDelete(Connection conn) {\r
boolean rv = true;\r
- try (PreparedStatement ps = c.prepareStatement(\r
- "delete from INGRESS_ROUTES where FEEDID = ? and USERID = ? and SUBNET = ?");\r
- PreparedStatement ps2 = c.prepareStatement("delete from NODESETS where SETID = ?")) {\r
+ try (PreparedStatement ps = conn.prepareStatement(\r
+ "delete from INGRESS_ROUTES where FEEDID = ? and USERID = ? and SUBNET = ?");\r
+ PreparedStatement ps2 = conn.prepareStatement("delete from NODESETS where SETID = ?")) {\r
// Delete the Ingress Route\r
ps.setInt(1, feedid);\r
ps.setString(2, userid);\r
ps.setString(3, subnet);\r
ps.execute();\r
- ps.close();\r
// Delete the NodeSet\r
ps2.setInt(1, nodelist);\r
ps2.execute();\r
} catch (SQLException e) {\r
rv = false;\r
- intlogger.warn("PROV0007 doDelete: " + e.getMessage(), e);\r
+ intlogger.error("PROV0007 doDelete: " + e.getMessage(), e);\r
}\r
return rv;\r
}\r
\r
@Override\r
- public boolean doInsert(Connection c) {\r
+ public boolean doInsert(Connection conn) {\r
boolean rv = false;\r
- try (PreparedStatement ps = c.prepareStatement("insert into NODESETS (SETID, NODEID) values (?,?)");\r
- PreparedStatement ps2 = c.prepareStatement("insert into INGRESS_ROUTES (SEQUENCE, FEEDID, USERID,"\r
- + " SUBNET, NODESET) values (?, ?, ?, ?, ?)")) {\r
+ try (PreparedStatement ps = conn.prepareStatement("insert into NODESETS (SETID, NODEID) values (?,?)");\r
+ PreparedStatement ps2 = conn.prepareStatement("insert into INGRESS_ROUTES (SEQUENCE, FEEDID, USERID,"\r
+ + " SUBNET, NODESET) values (?, ?, ?, ?, ?)")) {\r
// Create the NODESETS rows & set nodelist\r
this.nodelist = getMaxNodeSetID() + 1;\r
for (String node : nodes) {\r
ps2.execute();\r
rv = true;\r
} catch (SQLException e) {\r
- intlogger.warn("PROV0005 doInsert: " + e.getMessage(), e);\r
+ intlogger.error("PROV0005 doInsert: " + e.getMessage(), e);\r
}\r
return rv;\r
}\r
\r
@Override\r
- public boolean doUpdate(Connection c) {\r
- return doDelete(c) && doInsert(c);\r
+ public boolean doUpdate(Connection conn) {\r
+ return doDelete(conn) && doInsert(conn);\r
}\r
\r
@Override\r
@Override\r
public String getKey() {\r
return String\r
- .format("%d/%s/%s/%d", feedid, (userid == null) ? "" : userid, (subnet == null) ? "" : subnet, seq);\r
+ .format("%d/%s/%s/%d", feedid, (userid == null) ? "" : userid, (subnet == null) ? "" : subnet, seq);\r
}\r
\r
@Override\r
if (in == null) {\r
throw new NullPointerException();\r
}\r
- int n = this.feedid - in.feedid;\r
- if (n != 0) {\r
- return n;\r
+ int num = this.feedid - in.feedid;\r
+ if (num != 0) {\r
+ return num;\r
}\r
- n = this.seq - in.seq;\r
- if (n != 0) {\r
- return n;\r
+ num = this.seq - in.seq;\r
+ if (num != 0) {\r
+ return num;\r
}\r
- n = this.userid.compareTo(in.userid);\r
- if (n != 0) {\r
- return n;\r
+ num = this.userid.compareTo(in.userid);\r
+ if (num != 0) {\r
+ return num;\r
}\r
- n = this.subnet.compareTo(in.subnet);\r
- if (n != 0) {\r
- return n;\r
+ num = this.subnet.compareTo(in.subnet);\r
+ if (num != 0) {\r
+ return num;\r
}\r
return this.nodes.equals(in.nodes) ? 0 : 1;\r
}\r
@Override\r
public String toString() {\r
return String.format("INGRESS: feed=%d, userid=%s, subnet=%s, seq=%d", feedid, (userid == null) ? "" : userid,\r
- (subnet == null) ? "" : subnet, seq);\r
+ (subnet == null) ? "" : subnet, seq);\r
}\r
}\r