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 com.att.research.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
47 import com.att.research.datarouter.provisioning.utils.DB;
\r
50 * The representation of one route in the Ingress Route Table.
\r
52 * @author Robert P. Eby
\r
53 * @version $Id: IngressRoute.java,v 1.3 2013/12/16 20:30:23 eby Exp $
\r
55 public class IngressRoute extends NodeClass implements Comparable<IngressRoute> {
\r
56 private static Logger intlogger = Logger.getLogger("com.att.research.datarouter.provisioning.internal");
\r
57 private final int seq;
\r
58 private final int feedid;
\r
59 private final String userid;
\r
60 private final String subnet;
\r
61 private int nodelist;
\r
62 private SortedSet<String> nodes;
\r
65 * 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
72 * Get all IngressRoutes in the database with a particular sequence number.
\r
73 * @param seq the sequence number
\r
74 * @return a set of IngressRoutes
\r
76 public static Set<IngressRoute> getIngressRoutesForSeq(int seq) {
\r
77 return getAllIngressRoutesForSQL("select SEQUENCE, FEEDID, USERID, SUBNET, NODESET from INGRESS_ROUTES where SEQUENCE = "+seq);
\r
79 private static SortedSet<IngressRoute> getAllIngressRoutesForSQL(String sql) {
\r
80 SortedSet<IngressRoute> set = new TreeSet<IngressRoute>();
\r
83 @SuppressWarnings("resource")
\r
84 Connection conn = db.getConnection();
\r
85 Statement stmt = conn.createStatement();
\r
86 ResultSet rs = stmt.executeQuery(sql);
\r
88 int seq = rs.getInt("SEQUENCE");
\r
89 int feedid = rs.getInt("FEEDID");
\r
90 String user = rs.getString("USERID");
\r
91 String subnet = rs.getString("SUBNET");
\r
92 int nodeset = rs.getInt("NODESET");
\r
93 set.add(new IngressRoute(seq, feedid, user, subnet, nodeset));
\r
98 } catch (SQLException e) {
\r
99 e.printStackTrace();
\r
105 * Get the maximum node set ID in use in the DB.
\r
106 * @return the integer value of the maximum
\r
108 public static int getMaxNodeSetID() {
\r
109 return getMax("select max(SETID) as MAX from NODESETS");
\r
112 * Get the maximum node sequence number in use in the DB.
\r
113 * @return the integer value of the maximum
\r
115 public static int getMaxSequence() {
\r
116 return getMax("select max(SEQUENCE) as MAX from INGRESS_ROUTES");
\r
118 private static int getMax(String sql) {
\r
122 @SuppressWarnings("resource")
\r
123 Connection conn = db.getConnection();
\r
124 Statement stmt = conn.createStatement();
\r
125 ResultSet rs = stmt.executeQuery(sql);
\r
127 rv = rs.getInt("MAX");
\r
132 } catch (SQLException e) {
\r
133 e.printStackTrace();
\r
139 * Get an Ingress Route for a particular feed ID, user, and subnet
\r
140 * @param feedid the Feed ID to look for
\r
141 * @param user the user name to look for
\r
142 * @param subnet the subnet to look for
\r
143 * @return the Ingress Route, or null of there is none
\r
145 public static IngressRoute getIngressRoute(int feedid, String user, String subnet) {
\r
146 IngressRoute v = null;
\r
147 PreparedStatement ps = null;
\r
150 @SuppressWarnings("resource")
\r
151 Connection conn = db.getConnection();
\r
152 String sql = "select SEQUENCE, NODESET from INGRESS_ROUTES where FEEDID = ? AND USERID = ? and SUBNET = ?";
\r
153 ps = conn.prepareStatement(sql);
\r
154 ps.setInt(1, feedid);
\r
155 ps.setString(2, user);
\r
156 ps.setString(3, subnet);
\r
157 ResultSet rs = ps.executeQuery();
\r
159 int seq = rs.getInt("SEQUENCE");
\r
160 int nodeset = rs.getInt("NODESET");
\r
161 v = new IngressRoute(seq, feedid, user, subnet, nodeset);
\r
166 } catch (SQLException e) {
\r
167 e.printStackTrace();
\r
171 } catch (SQLException e) {
\r
172 e.printStackTrace();
\r
179 * Get a collection of all Ingress Routes with a particular sequence number.
\r
180 * @param seq the sequence number to look for
\r
181 * @return the collection (may be empty).
\r
183 public static Collection<IngressRoute> getIngressRoute(int seq) {
\r
184 Collection<IngressRoute> rv = new ArrayList<IngressRoute>();
\r
185 PreparedStatement ps = null;
\r
188 @SuppressWarnings("resource")
\r
189 Connection conn = db.getConnection();
\r
190 String sql = "select FEEDID, USERID, SUBNET, NODESET from INGRESS_ROUTES where SEQUENCE = ?";
\r
191 ps = conn.prepareStatement(sql);
\r
193 ResultSet rs = ps.executeQuery();
\r
194 while (rs.next()) {
\r
195 int feedid = rs.getInt("FEEDID");
\r
196 String user = rs.getString("USERID");
\r
197 String subnet = rs.getString("SUBNET");
\r
198 int nodeset = rs.getInt("NODESET");
\r
199 rv.add(new IngressRoute(seq, feedid, user, subnet, nodeset));
\r
204 } catch (SQLException e) {
\r
205 e.printStackTrace();
\r
209 } catch (SQLException e) {
\r
210 e.printStackTrace();
\r
216 public IngressRoute(int seq, int feedid, String user, String subnet, Collection<String> nodes)
\r
217 throws IllegalArgumentException
\r
219 this(seq, feedid, user, subnet);
\r
220 this.nodelist = -1;
\r
221 this.nodes = new TreeSet<String>(nodes);
\r
224 public IngressRoute(int seq, int feedid, String user, String subnet, int nodeset)
\r
225 throws IllegalArgumentException
\r
227 this(seq, feedid, user, subnet);
\r
228 this.nodelist = nodeset;
\r
229 this.nodes = new TreeSet<String>(readNodes());
\r
232 private IngressRoute(int seq, int feedid, String user, String subnet)
\r
233 throws IllegalArgumentException
\r
236 this.feedid = feedid;
\r
237 this.userid = (user == null) ? "-" : user;
\r
238 this.subnet = (subnet == null) ? "-" : subnet;
\r
239 this.nodelist = -1;
\r
241 if (Feed.getFeedById(feedid) == null)
\r
242 throw new IllegalArgumentException("No such feed: "+feedid);
\r
243 if (!this.subnet.equals("-")) {
\r
244 SubnetMatcher sm = new SubnetMatcher(subnet);
\r
246 throw new IllegalArgumentException("Invalid subnet: "+subnet);
\r
250 public IngressRoute(JSONObject jo) {
\r
251 this.seq = jo.optInt("seq");
\r
252 this.feedid = jo.optInt("feedid");
\r
253 String t = jo.optString("user");
\r
254 this.userid = t.equals("") ? "-" : t;
\r
255 t = jo.optString("subnet");
\r
256 this.subnet = t.equals("") ? "-" : t;
\r
257 this.nodelist = -1;
\r
258 this.nodes = new TreeSet<String>();
\r
259 JSONArray ja = jo.getJSONArray("node");
\r
260 for (int i = 0; i < ja.length(); i++)
\r
261 this.nodes.add(ja.getString(i));
\r
264 * Does this particular IngressRoute match a request, represented by feedid and req?
\r
265 * To match, <i>feedid</i> must match the feed ID in the route, the user in the route
\r
266 * (if specified) must match the user in the request, and the subnet in the route (if specified)
\r
267 * must match the subnet from the request.
\r
268 * @param feedid the feedid for this request
\r
269 * @param req the remainder of the request
\r
270 * @return true if a match, false otherwise
\r
272 public boolean matches(int feedid, HttpServletRequest req) {
\r
274 if (this.feedid != feedid)
\r
277 // Get user from request and compare
\r
278 // Note: we don't check the password; the node will do that
\r
279 if (userid.length() > 0 && !userid.equals("-")) {
\r
280 String credentials = req.getHeader("Authorization");
\r
281 if (credentials == null || !credentials.startsWith("Basic "))
\r
283 String t = new String(Base64.decodeBase64(credentials.substring(6)));
\r
284 int ix = t.indexOf(':');
\r
286 t = t.substring(0, ix);
\r
287 if (!t.equals(this.userid))
\r
291 // If this route has a subnet, match it against the requester's IP addr
\r
292 if (subnet.length() > 0 && !subnet.equals("-")) {
\r
294 InetAddress inet = InetAddress.getByName(req.getRemoteAddr());
\r
295 SubnetMatcher sm = new SubnetMatcher(subnet);
\r
296 return sm.matches(inet.getAddress());
\r
297 } catch (UnknownHostException e) {
\r
305 * Compare IP addresses as byte arrays to a subnet specified as a CIDR.
\r
306 * Taken from com.att.research.datarouter.node.SubnetMatcher and modified somewhat.
\r
308 public class SubnetMatcher {
\r
312 private boolean valid;
\r
315 * Construct a subnet matcher given a CIDR
\r
316 * @param subnet The CIDR to match
\r
318 public SubnetMatcher(String subnet) {
\r
319 int i = subnet.lastIndexOf('/');
\r
322 sn = InetAddress.getByName(subnet).getAddress();
\r
325 } catch (UnknownHostException e) {
\r
331 int n = Integer.parseInt(subnet.substring(i + 1));
\r
333 sn = InetAddress.getByName(subnet.substring(0, i)).getAddress();
\r
335 } catch (UnknownHostException e) {
\r
339 mask = ((0xff00) >> (n % 8)) & 0xff;
\r
342 public boolean isValid() {
\r
346 * Is the IP address in the CIDR?
\r
347 * @param addr the IP address as bytes in network byte order
\r
348 * @return true if the IP address matches.
\r
350 public boolean matches(byte[] addr) {
\r
351 if (!valid || addr.length != sn.length) {
\r
354 for (int i = 0; i < len; i++) {
\r
355 if (addr[i] != sn[i]) {
\r
359 if (mask != 0 && ((addr[len] ^ sn[len]) & mask) != 0) {
\r
367 * Get the list of node names for this route.
\r
370 public SortedSet<String> getNodes() {
\r
374 private Collection<String> readNodes() {
\r
375 Collection<String> set = new TreeSet<String>();
\r
376 PreparedStatement ps = null;
\r
379 @SuppressWarnings("resource")
\r
380 Connection conn = db.getConnection();
\r
381 Statement stmt = conn.createStatement();
\r
382 String sql = "select NODEID from NODESETS where SETID = ?";
\r
383 ps = conn.prepareStatement(sql);
\r
384 ps.setInt(1, nodelist);
\r
385 ResultSet rs = ps.executeQuery();
\r
386 while (rs.next()) {
\r
387 int id = rs.getInt("NODEID");
\r
388 set.add(lookupNodeID(id));
\r
393 } catch (SQLException e) {
\r
394 e.printStackTrace();
\r
398 } catch (SQLException e) {
\r
399 e.printStackTrace();
\r
406 * Delete the IRT route having this IngressRoutes feed ID, user ID, and subnet from the database.
\r
407 * @return true if the delete succeeded
\r
410 public boolean doDelete(Connection c) {
\r
412 PreparedStatement ps = null;
\r
414 ps = c.prepareStatement("delete from INGRESS_ROUTES where FEEDID = ? and USERID = ? and SUBNET = ?");
\r
415 ps.setInt(1, feedid);
\r
416 ps.setString(2, userid);
\r
417 ps.setString(3, subnet);
\r
421 ps = c.prepareStatement("delete from NODESETS where SETID = ?");
\r
422 ps.setInt(1, nodelist);
\r
424 } catch (SQLException e) {
\r
426 intlogger.warn("PROV0007 doDelete: "+e.getMessage());
\r
427 e.printStackTrace();
\r
431 } catch (SQLException e) {
\r
432 e.printStackTrace();
\r
438 @SuppressWarnings("resource")
\r
440 public boolean doInsert(Connection c) {
\r
441 boolean rv = false;
\r
442 PreparedStatement ps = null;
\r
444 // Create the NODESETS rows & set nodelist
\r
445 int set = getMaxNodeSetID() + 1;
\r
446 this.nodelist = set;
\r
447 for (String node : nodes) {
\r
448 int id = lookupNodeName(node);
\r
449 ps = c.prepareStatement("insert into NODESETS (SETID, NODEID) values (?,?)");
\r
450 ps.setInt(1, this.nodelist);
\r
456 // Create the INGRESS_ROUTES row
\r
457 ps = c.prepareStatement("insert into INGRESS_ROUTES (SEQUENCE, FEEDID, USERID, SUBNET, NODESET) values (?, ?, ?, ?, ?)");
\r
458 ps.setInt(1, this.seq);
\r
459 ps.setInt(2, this.feedid);
\r
460 ps.setString(3, this.userid);
\r
461 ps.setString(4, this.subnet);
\r
462 ps.setInt(5, this.nodelist);
\r
466 } catch (SQLException e) {
\r
467 intlogger.warn("PROV0005 doInsert: "+e.getMessage());
\r
468 e.printStackTrace();
\r
472 } catch (SQLException e) {
\r
473 e.printStackTrace();
\r
480 public boolean doUpdate(Connection c) {
\r
481 return doDelete(c) && doInsert(c);
\r
485 public JSONObject asJSONObject() {
\r
486 JSONObject jo = new JSONObject();
\r
487 jo.put("feedid", feedid);
\r
488 // Note: for user and subnet, null, "", and "-" are equivalent
\r
489 if (userid != null && !userid.equals("-") && !userid.equals(""))
\r
490 jo.put("user", userid);
\r
491 if (subnet != null && !subnet.equals("-") && !subnet.equals(""))
\r
492 jo.put("subnet", subnet);
\r
493 jo.put("seq", seq);
\r
494 jo.put("node", nodes);
\r
499 public String getKey() {
\r
500 return String.format("%d/%s/%s/%d", feedid, (userid == null)?"":userid, (subnet == null)?"":subnet, seq);
\r
504 public int hashCode() {
\r
505 return toString().hashCode();
\r
509 public boolean equals(Object obj) {
\r
511 if (!(obj instanceof IngressRoute))
\r
513 return this.compareTo((IngressRoute) obj) == 0;
\r
514 } catch (NullPointerException e) {
\r
520 public int compareTo(IngressRoute in) {
\r
522 throw new NullPointerException();
\r
523 int n = this.feedid - in.feedid;
\r
526 n = this.seq - in.seq;
\r
529 n = this.userid.compareTo(in.userid);
\r
532 n = this.subnet.compareTo(in.subnet);
\r
535 return this.nodes.equals(in.nodes) ? 0 : 1;
\r
539 public String toString() {
\r
540 return String.format("INGRESS: feed=%d, userid=%s, subnet=%s, seq=%d", feedid, (userid == null)?"":userid, (subnet == null)?"":subnet, seq);
\r