1 /*******************************************************************************
\r
2 * ============LICENSE_START==================================================
\r
4 * * ===========================================================================
\r
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * * ===========================================================================
\r
7 * * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * * you may not use this file except in compliance with the License.
\r
9 * * You may obtain a copy of the License at
\r
11 * * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * * Unless required by applicable law or agreed to in writing, software
\r
14 * * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * * See the License for the specific language governing permissions and
\r
17 * * limitations under the License.
\r
18 * * ============LICENSE_END====================================================
\r
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
25 package org.onap.dmaap.datarouter.provisioning.beans;
\r
27 import java.net.InetAddress;
\r
28 import java.net.UnknownHostException;
\r
29 import java.sql.Connection;
\r
30 import java.sql.PreparedStatement;
\r
31 import java.sql.ResultSet;
\r
32 import java.sql.SQLException;
\r
33 import java.sql.Statement;
\r
34 import java.util.ArrayList;
\r
35 import java.util.Collection;
\r
36 import java.util.Set;
\r
37 import java.util.SortedSet;
\r
38 import java.util.TreeSet;
\r
40 import javax.servlet.http.HttpServletRequest;
\r
42 import org.apache.commons.codec.binary.Base64;
\r
43 import org.apache.log4j.Logger;
\r
44 import org.json.JSONArray;
\r
45 import org.json.JSONObject;
\r
46 import org.onap.dmaap.datarouter.provisioning.utils.DB;
\r
49 * The representation of one route in the Ingress Route Table.
\r
51 * @author Robert P. Eby
\r
52 * @version $Id: IngressRoute.java,v 1.3 2013/12/16 20:30:23 eby Exp $
\r
54 public class IngressRoute extends NodeClass implements Comparable<IngressRoute> {
\r
55 private static Logger intlogger = Logger.getLogger("org.onap.dmaap.datarouter.provisioning.internal");
\r
56 private final int seq;
\r
57 private final int feedid;
\r
58 private final String userid;
\r
59 private final String subnet;
\r
60 private int nodelist;
\r
61 private SortedSet<String> nodes;
\r
64 * Get all IngressRoutes in the database, sorted in order according to their sequence field.
\r
66 * @return a sorted set of IngressRoutes
\r
68 public static SortedSet<IngressRoute> getAllIngressRoutes() {
\r
69 return getAllIngressRoutesForSQL("select SEQUENCE, FEEDID, USERID, SUBNET, NODESET from INGRESS_ROUTES");
\r
73 * Get all IngressRoutes in the database with a particular sequence number.
\r
75 * @param seq the sequence number
\r
76 * @return a set of IngressRoutes
\r
78 public static Set<IngressRoute> getIngressRoutesForSeq(int seq) {
\r
79 return getAllIngressRoutesForSQL("select SEQUENCE, FEEDID, USERID, SUBNET, NODESET from INGRESS_ROUTES where SEQUENCE = " + seq);
\r
82 private static SortedSet<IngressRoute> getAllIngressRoutesForSQL(String sql) {
\r
83 SortedSet<IngressRoute> set = new TreeSet<IngressRoute>();
\r
86 @SuppressWarnings("resource")
\r
87 Connection conn = db.getConnection();
\r
88 try(Statement stmt = conn.createStatement()) {
\r
89 try(ResultSet rs = stmt.executeQuery(sql)) {
\r
91 int seq = rs.getInt("SEQUENCE");
\r
92 int feedid = rs.getInt("FEEDID");
\r
93 String user = rs.getString("USERID");
\r
94 String subnet = rs.getString("SUBNET");
\r
95 int nodeset = rs.getInt("NODESET");
\r
96 set.add(new IngressRoute(seq, feedid, user, subnet, nodeset));
\r
101 } catch (SQLException e) {
\r
102 e.printStackTrace();
\r
108 * Get the maximum node set ID in use in the DB.
\r
110 * @return the integer value of the maximum
\r
112 public static int getMaxNodeSetID() {
\r
113 return getMax("select max(SETID) as MAX from NODESETS");
\r
117 * Get the maximum node sequence number in use in the DB.
\r
119 * @return the integer value of the maximum
\r
121 public static int getMaxSequence() {
\r
122 return getMax("select max(SEQUENCE) as MAX from INGRESS_ROUTES");
\r
125 private static int getMax(String sql) {
\r
129 @SuppressWarnings("resource")
\r
130 Connection conn = db.getConnection();
\r
131 try(Statement stmt = conn.createStatement()) {
\r
132 try(ResultSet rs = stmt.executeQuery(sql)) {
\r
134 rv = rs.getInt("MAX");
\r
139 } catch (SQLException e) {
\r
140 e.printStackTrace();
\r
146 * Get an Ingress Route for a particular feed ID, user, and subnet
\r
148 * @param feedid the Feed ID to look for
\r
149 * @param user the user name to look for
\r
150 * @param subnet the subnet to look for
\r
151 * @return the Ingress Route, or null of there is none
\r
153 public static IngressRoute getIngressRoute(int feedid, String user, String subnet) {
\r
154 IngressRoute v = null;
\r
155 PreparedStatement ps = null;
\r
158 @SuppressWarnings("resource")
\r
159 Connection conn = db.getConnection();
\r
160 String sql = "select SEQUENCE, NODESET from INGRESS_ROUTES where FEEDID = ? AND USERID = ? and SUBNET = ?";
\r
161 ps = conn.prepareStatement(sql);
\r
162 ps.setInt(1, feedid);
\r
163 ps.setString(2, user);
\r
164 ps.setString(3, subnet);
\r
165 try(ResultSet rs = ps.executeQuery()) {
\r
167 int seq = rs.getInt("SEQUENCE");
\r
168 int nodeset = rs.getInt("NODESET");
\r
169 v = new IngressRoute(seq, feedid, user, subnet, nodeset);
\r
174 } catch (SQLException e) {
\r
175 e.printStackTrace();
\r
181 } catch (SQLException e) {
\r
182 e.printStackTrace();
\r
189 * Get a collection of all Ingress Routes with a particular sequence number.
\r
191 * @param seq the sequence number to look for
\r
192 * @return the collection (may be empty).
\r
194 public static Collection<IngressRoute> getIngressRoute(int seq) {
\r
195 Collection<IngressRoute> rv = new ArrayList<IngressRoute>();
\r
198 @SuppressWarnings("resource")
\r
199 Connection conn = db.getConnection();
\r
200 String sql = "select FEEDID, USERID, SUBNET, NODESET from INGRESS_ROUTES where SEQUENCE = ?";
\r
201 try(PreparedStatement ps = conn.prepareStatement(sql)) {
\r
203 try(ResultSet rs = ps.executeQuery()) {
\r
204 while (rs.next()) {
\r
205 int feedid = rs.getInt("FEEDID");
\r
206 String user = rs.getString("USERID");
\r
207 String subnet = rs.getString("SUBNET");
\r
208 int nodeset = rs.getInt("NODESET");
\r
209 rv.add(new IngressRoute(seq, feedid, user, subnet, nodeset));
\r
214 } catch (SQLException e) {
\r
215 e.printStackTrace();
\r
220 public IngressRoute(int seq, int feedid, String user, String subnet, Collection<String> nodes)
\r
221 throws IllegalArgumentException {
\r
222 this(seq, feedid, user, subnet);
\r
223 this.nodelist = -1;
\r
224 this.nodes = new TreeSet<String>(nodes);
\r
227 public IngressRoute(int seq, int feedid, String user, String subnet, int nodeset)
\r
228 throws IllegalArgumentException {
\r
229 this(seq, feedid, user, subnet);
\r
230 this.nodelist = nodeset;
\r
231 this.nodes = new TreeSet<String>(readNodes());
\r
234 private IngressRoute(int seq, int feedid, String user, String subnet)
\r
235 throws IllegalArgumentException {
\r
237 this.feedid = feedid;
\r
238 this.userid = (user == null) ? "-" : user;
\r
239 this.subnet = (subnet == null) ? "-" : subnet;
\r
240 this.nodelist = -1;
\r
242 if (Feed.getFeedById(feedid) == null)
\r
243 throw new IllegalArgumentException("No such feed: " + feedid);
\r
244 if (!this.subnet.equals("-")) {
\r
245 SubnetMatcher sm = new SubnetMatcher(subnet);
\r
247 throw new IllegalArgumentException("Invalid subnet: " + subnet);
\r
251 public IngressRoute(JSONObject jo) {
\r
252 this.seq = jo.optInt("seq");
\r
253 this.feedid = jo.optInt("feedid");
\r
254 String t = jo.optString("user");
\r
255 this.userid = t.equals("") ? "-" : t;
\r
256 t = jo.optString("subnet");
\r
257 this.subnet = t.equals("") ? "-" : t;
\r
258 this.nodelist = -1;
\r
259 this.nodes = new TreeSet<String>();
\r
260 JSONArray ja = jo.getJSONArray("node");
\r
261 for (int i = 0; i < ja.length(); i++)
\r
262 this.nodes.add(ja.getString(i));
\r
266 * Does this particular IngressRoute match a request, represented by feedid and req?
\r
267 * To match, <i>feedid</i> must match the feed ID in the route, the user in the route
\r
268 * (if specified) must match the user in the request, and the subnet in the route (if specified)
\r
269 * must match the subnet from the request.
\r
271 * @param feedid the feedid for this request
\r
272 * @param req the remainder of the request
\r
273 * @return true if a match, false otherwise
\r
275 public boolean matches(int feedid, HttpServletRequest req) {
\r
277 if (this.feedid != feedid)
\r
280 // Get user from request and compare
\r
281 // Note: we don't check the password; the node will do that
\r
282 if (userid.length() > 0 && !userid.equals("-")) {
\r
283 String credentials = req.getHeader("Authorization");
\r
284 if (credentials == null || !credentials.startsWith("Basic "))
\r
286 String t = new String(Base64.decodeBase64(credentials.substring(6)));
\r
287 int ix = t.indexOf(':');
\r
289 t = t.substring(0, ix);
\r
290 if (!t.equals(this.userid))
\r
294 // If this route has a subnet, match it against the requester's IP addr
\r
295 if (subnet.length() > 0 && !subnet.equals("-")) {
\r
297 InetAddress inet = InetAddress.getByName(req.getRemoteAddr());
\r
298 SubnetMatcher sm = new SubnetMatcher(subnet);
\r
299 return sm.matches(inet.getAddress());
\r
300 } catch (UnknownHostException e) {
\r
308 * Compare IP addresses as byte arrays to a subnet specified as a CIDR.
\r
309 * Taken from org.onap.dmaap.datarouter.node.SubnetMatcher and modified somewhat.
\r
311 public class SubnetMatcher {
\r
315 private boolean valid;
\r
318 * Construct a subnet matcher given a CIDR
\r
320 * @param subnet The CIDR to match
\r
322 public SubnetMatcher(String subnet) {
\r
323 int i = subnet.lastIndexOf('/');
\r
326 sn = InetAddress.getByName(subnet).getAddress();
\r
329 } catch (UnknownHostException e) {
\r
335 int n = Integer.parseInt(subnet.substring(i + 1));
\r
337 sn = InetAddress.getByName(subnet.substring(0, i)).getAddress();
\r
339 } catch (UnknownHostException e) {
\r
343 mask = ((0xff00) >> (n % 8)) & 0xff;
\r
347 public boolean isValid() {
\r
352 * Is the IP address in the CIDR?
\r
354 * @param addr the IP address as bytes in network byte order
\r
355 * @return true if the IP address matches.
\r
357 public boolean matches(byte[] addr) {
\r
358 if (!valid || addr.length != sn.length) {
\r
361 for (int i = 0; i < len; i++) {
\r
362 if (addr[i] != sn[i]) {
\r
366 if (mask != 0 && ((addr[len] ^ sn[len]) & mask) != 0) {
\r
374 * Get the list of node names for this route.
\r
378 public SortedSet<String> getNodes() {
\r
382 private Collection<String> readNodes() {
\r
383 Collection<String> set = new TreeSet<String>();
\r
386 @SuppressWarnings("resource")
\r
387 Connection conn = db.getConnection();
\r
388 String sql = "select NODEID from NODESETS where SETID = ?";
\r
389 try(PreparedStatement ps = conn.prepareStatement(sql)) {
\r
390 ps.setInt(1, nodelist);
\r
391 try(ResultSet rs = ps.executeQuery()) {
\r
392 while (rs.next()) {
\r
393 int id = rs.getInt("NODEID");
\r
394 set.add(lookupNodeID(id));
\r
399 } catch (SQLException e) {
\r
400 e.printStackTrace();
\r
406 * Delete the IRT route having this IngressRoutes feed ID, user ID, and subnet from the database.
\r
408 * @return true if the delete succeeded
\r
411 public boolean doDelete(Connection c) {
\r
413 PreparedStatement ps = null;
\r
415 ps = c.prepareStatement("delete from INGRESS_ROUTES where FEEDID = ? and USERID = ? and SUBNET = ?");
\r
416 ps.setInt(1, feedid);
\r
417 ps.setString(2, userid);
\r
418 ps.setString(3, subnet);
\r
422 ps = c.prepareStatement("delete from NODESETS where SETID = ?");
\r
423 ps.setInt(1, nodelist);
\r
425 } catch (SQLException e) {
\r
427 intlogger.warn("PROV0007 doDelete: " + e.getMessage());
\r
428 e.printStackTrace();
\r
434 } catch (SQLException e) {
\r
435 e.printStackTrace();
\r
441 @SuppressWarnings("resource")
\r
443 public boolean doInsert(Connection c) {
\r
444 boolean rv = false;
\r
445 PreparedStatement ps = null;
\r
447 // Create the NODESETS rows & set nodelist
\r
448 int set = getMaxNodeSetID() + 1;
\r
449 this.nodelist = set;
\r
450 for (String node : nodes) {
\r
451 int id = lookupNodeName(node);
\r
452 ps = c.prepareStatement("insert into NODESETS (SETID, NODEID) values (?,?)");
\r
453 ps.setInt(1, this.nodelist);
\r
459 // Create the INGRESS_ROUTES row
\r
460 ps = c.prepareStatement("insert into INGRESS_ROUTES (SEQUENCE, FEEDID, USERID, SUBNET, NODESET) values (?, ?, ?, ?, ?)");
\r
461 ps.setInt(1, this.seq);
\r
462 ps.setInt(2, this.feedid);
\r
463 ps.setString(3, this.userid);
\r
464 ps.setString(4, this.subnet);
\r
465 ps.setInt(5, this.nodelist);
\r
469 } catch (SQLException e) {
\r
470 intlogger.warn("PROV0005 doInsert: " + e.getMessage());
\r
471 e.printStackTrace();
\r
477 } catch (SQLException e) {
\r
478 e.printStackTrace();
\r
485 public boolean doUpdate(Connection c) {
\r
486 return doDelete(c) && doInsert(c);
\r
490 public JSONObject asJSONObject() {
\r
491 JSONObject jo = new JSONObject();
\r
492 jo.put("feedid", feedid);
\r
493 // Note: for user and subnet, null, "", and "-" are equivalent
\r
494 if (userid != null && !userid.equals("-") && !userid.equals(""))
\r
495 jo.put("user", userid);
\r
496 if (subnet != null && !subnet.equals("-") && !subnet.equals(""))
\r
497 jo.put("subnet", subnet);
\r
498 jo.put("seq", seq);
\r
499 jo.put("node", nodes);
\r
504 public String getKey() {
\r
505 return String.format("%d/%s/%s/%d", feedid, (userid == null) ? "" : userid, (subnet == null) ? "" : subnet, seq);
\r
509 public int hashCode() {
\r
510 return toString().hashCode();
\r
514 public boolean equals(Object obj) {
\r
516 if (!(obj instanceof IngressRoute))
\r
518 return this.compareTo((IngressRoute) obj) == 0;
\r
519 } catch (NullPointerException e) {
\r
525 public int compareTo(IngressRoute in) {
\r
527 throw new NullPointerException();
\r
528 int n = this.feedid - in.feedid;
\r
531 n = this.seq - in.seq;
\r
534 n = this.userid.compareTo(in.userid);
\r
537 n = this.subnet.compareTo(in.subnet);
\r
540 return this.nodes.equals(in.nodes) ? 0 : 1;
\r
544 public String toString() {
\r
545 return String.format("INGRESS: feed=%d, userid=%s, subnet=%s, seq=%d", feedid, (userid == null) ? "" : userid, (subnet == null) ? "" : subnet, seq);
\r