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.sql.Connection;
\r
28 import java.sql.PreparedStatement;
\r
29 import java.sql.ResultSet;
\r
30 import java.sql.SQLException;
\r
31 import java.sql.Statement;
\r
32 import java.util.Objects;
\r
33 import java.util.SortedSet;
\r
34 import java.util.TreeSet;
\r
36 import com.att.eelf.configuration.EELFLogger;
\r
37 import com.att.eelf.configuration.EELFManager;
\r
38 import org.json.JSONObject;
\r
39 import org.onap.dmaap.datarouter.provisioning.utils.DB;
\r
42 * The representation of one route in the Egress Route Table.
\r
44 * @author Robert P. Eby
\r
45 * @version $Id: EgressRoute.java,v 1.3 2013/12/16 20:30:23 eby Exp $
\r
47 public class EgressRoute extends NodeClass implements Comparable<EgressRoute> {
\r
49 private static EELFLogger intlogger = EELFManager.getInstance().getLogger("InternalLog");
\r
50 private static final String SQLEXCEPTION = "SQLException: ";
\r
51 private final int subid;
\r
52 private final int nodeid;
\r
55 * Get a set of all Egress Routes in the DB. The set is sorted according to the natural sorting order of the routes
\r
56 * (based on the subscription ID in each route).
\r
58 * @return the sorted set
\r
60 public static SortedSet<EgressRoute> getAllEgressRoutes() {
\r
61 SortedSet<EgressRoute> set = new TreeSet<>();
\r
64 @SuppressWarnings("resource")
\r
65 Connection conn = db.getConnection();
\r
66 try (Statement stmt = conn.createStatement()) {
\r
67 try (ResultSet rs = stmt.executeQuery("select SUBID, NODEID from EGRESS_ROUTES")) {
\r
69 int subid = rs.getInt("SUBID");
\r
70 int nodeid = rs.getInt("NODEID");
\r
71 set.add(new EgressRoute(subid, nodeid));
\r
77 } catch (SQLException e) {
\r
78 intlogger.error("PROV0008 EgressRoute.getAllEgressRoutes: " + e.getMessage(), e);
\r
84 * Get a single Egress Route for the subscription <i>sub</i>.
\r
86 * @param sub the subscription to lookup
\r
87 * @return an EgressRoute, or null if there is no route for this subscription
\r
89 public static EgressRoute getEgressRoute(int sub) {
\r
90 EgressRoute v = null;
\r
91 PreparedStatement ps = null;
\r
94 @SuppressWarnings("resource")
\r
95 Connection conn = db.getConnection();
\r
96 String sql = "select NODEID from EGRESS_ROUTES where SUBID = ?";
\r
97 ps = conn.prepareStatement(sql);
\r
99 try (ResultSet rs = ps.executeQuery()) {
\r
101 int node = rs.getInt("NODEID");
\r
102 v = new EgressRoute(sub, node);
\r
107 } catch (SQLException e) {
\r
108 intlogger.error("PROV0009 EgressRoute.getEgressRoute: " + e.getMessage(), e);
\r
114 } catch (SQLException e) {
\r
115 intlogger.error(SQLEXCEPTION + e.getMessage(), e);
\r
121 public EgressRoute(int subid, int nodeid) {
\r
122 this.subid = subid;
\r
123 this.nodeid = nodeid;
\r
124 // Note: unlike for Feeds, it subscriptions can be removed from the tables, so it is
\r
125 // possible that an orphan ERT entry can exist if a sub is removed.
\r
126 // if (Subscription.getSubscriptionById(subid) == null)
\r
127 // throw new IllegalArgumentException("No such subscription: "+subid);
\r
130 public EgressRoute(int subid, String node) {
\r
131 this(subid, lookupNodeName(node));
\r
135 public boolean doDelete(Connection c) {
\r
137 PreparedStatement ps = null;
\r
139 String sql = "delete from EGRESS_ROUTES where SUBID = ?";
\r
140 ps = c.prepareStatement(sql);
\r
141 ps.setInt(1, subid);
\r
143 } catch (SQLException e) {
\r
145 intlogger.error("PROV0007 doDelete: " + e.getMessage(), e);
\r
151 } catch (SQLException e) {
\r
152 intlogger.error(SQLEXCEPTION + e.getMessage(), e);
\r
159 public boolean doInsert(Connection c) {
\r
160 boolean rv = false;
\r
161 PreparedStatement ps = null;
\r
163 // Create the NETWORK_ROUTES row
\r
164 String sql = "insert into EGRESS_ROUTES (SUBID, NODEID) values (?, ?)";
\r
165 ps = c.prepareStatement(sql);
\r
166 ps.setInt(1, this.subid);
\r
167 ps.setInt(2, this.nodeid);
\r
171 } catch (SQLException e) {
\r
172 intlogger.warn("PROV0005 doInsert: " + e.getMessage(), e);
\r
178 } catch (SQLException e) {
\r
179 intlogger.error(SQLEXCEPTION + e.getMessage(), e);
\r
186 public boolean doUpdate(Connection c) {
\r
188 PreparedStatement ps = null;
\r
190 String sql = "update EGRESS_ROUTES set NODEID = ? where SUBID = ?";
\r
191 ps = c.prepareStatement(sql);
\r
192 ps.setInt(1, nodeid);
\r
193 ps.setInt(2, subid);
\r
194 ps.executeUpdate();
\r
195 } catch (SQLException e) {
\r
197 intlogger.warn("PROV0006 doUpdate: " + e.getMessage(), e);
\r
203 } catch (SQLException e) {
\r
204 intlogger.error(SQLEXCEPTION + e.getMessage(), e);
\r
211 public JSONObject asJSONObject() {
\r
212 JSONObject jo = new JSONObject();
\r
213 jo.put("" + subid, lookupNodeID(nodeid));
\r
218 public String getKey() {
\r
223 public boolean equals(Object obj) {
\r
224 if (!(obj instanceof EgressRoute)) {
\r
227 EgressRoute on = (EgressRoute) obj;
\r
228 return (subid == on.subid) && (nodeid == on.nodeid);
\r
232 public int compareTo(EgressRoute o) {
\r
233 return this.subid - o.subid;
\r
237 public String toString() {
\r
238 return String.format("EGRESS: sub=%d, node=%d", subid, nodeid);
\r
242 public int hashCode() {
\r
243 return Objects.hash(subid, nodeid);
\r