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 com.att.eelf.configuration.EELFLogger;
\r
28 import com.att.eelf.configuration.EELFManager;
\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.Objects;
\r
35 import java.util.SortedSet;
\r
36 import java.util.TreeSet;
\r
37 import org.json.JSONObject;
\r
38 import org.onap.dmaap.datarouter.provisioning.utils.ProvDbUtils;
\r
41 * The representation of one route in the Egress Route Table.
\r
43 * @author Robert P. Eby
\r
44 * @version $Id: EgressRoute.java,v 1.3 2013/12/16 20:30:23 eby Exp $
\r
46 public class EgressRoute extends NodeClass implements Comparable<EgressRoute> {
\r
48 private static EELFLogger intlogger = EELFManager.getInstance().getLogger("InternalLog");
\r
49 private final int subid;
\r
50 private final int nodeid;
\r
53 * EgressRoute constructor.
\r
54 * @param subid subscription id
\r
55 * @param nodeid node id
\r
57 public EgressRoute(int subid, int nodeid) {
\r
59 this.nodeid = nodeid;
\r
62 public EgressRoute(int subid, String node) {
\r
63 this(subid, lookupNodeName(node));
\r
67 * Get a set of all Egress Routes in the DB. The set is sorted according to the natural sorting order of the routes
\r
68 * (based on the subscription ID in each route).
\r
70 * @return the sorted set
\r
72 public static SortedSet<EgressRoute> getAllEgressRoutes() {
\r
73 SortedSet<EgressRoute> set = new TreeSet<>();
\r
74 try (Connection conn = ProvDbUtils.getInstance().getConnection();
\r
75 Statement stmt = conn.createStatement();
\r
76 ResultSet rs = stmt.executeQuery("select SUBID, NODEID from EGRESS_ROUTES")) {
\r
77 addEgressRouteToSet(set, rs);
\r
78 } catch (SQLException e) {
\r
79 intlogger.error("PROV0008 EgressRoute.getAllEgressRoutes: " + e.getMessage(), e);
\r
84 private static void addEgressRouteToSet(SortedSet<EgressRoute> set, ResultSet rs) throws SQLException {
\r
86 int subid = rs.getInt("SUBID");
\r
87 int nodeid = rs.getInt("NODEID");
\r
88 set.add(new EgressRoute(subid, nodeid));
\r
93 * Get a single Egress Route for the subscription <i>sub</i>.
\r
95 * @param sub the subscription to lookup
\r
96 * @return an EgressRoute, or null if there is no route for this subscription
\r
98 public static EgressRoute getEgressRoute(int sub) {
\r
99 EgressRoute er = null;
\r
100 try (Connection conn = ProvDbUtils.getInstance().getConnection();
\r
101 PreparedStatement ps = conn.prepareStatement("select NODEID from EGRESS_ROUTES where SUBID = ?")) {
\r
103 try (ResultSet rs = ps.executeQuery()) {
\r
105 int node = rs.getInt("NODEID");
\r
106 er = new EgressRoute(sub, node);
\r
109 } catch (SQLException e) {
\r
110 intlogger.error("PROV0009 EgressRoute.getEgressRoute: " + e.getMessage(), e);
\r
116 public boolean doDelete(Connection conn) {
\r
118 try (PreparedStatement ps = conn.prepareStatement("delete from EGRESS_ROUTES where SUBID = ?")) {
\r
119 ps.setInt(1, subid);
\r
121 } catch (SQLException e) {
\r
123 intlogger.error("PROV0007 doDelete: " + e.getMessage(), e);
\r
129 public boolean doInsert(Connection conn) {
\r
130 boolean rv = false;
\r
131 try (PreparedStatement ps = conn.prepareStatement("insert into EGRESS_ROUTES (SUBID, NODEID) values (?, ?)")) {
\r
132 ps.setInt(1, this.subid);
\r
133 ps.setInt(2, this.nodeid);
\r
136 } catch (SQLException e) {
\r
137 intlogger.warn("PROV0005 doInsert: " + e.getMessage(), e);
\r
143 public boolean doUpdate(Connection conn) {
\r
145 try (PreparedStatement ps = conn.prepareStatement("update EGRESS_ROUTES set NODEID = ? where SUBID = ?")) {
\r
146 ps.setInt(1, nodeid);
\r
147 ps.setInt(2, subid);
\r
148 ps.executeUpdate();
\r
149 } catch (SQLException e) {
\r
151 intlogger.warn("PROV0006 doUpdate: " + e.getMessage(), e);
\r
157 public JSONObject asJSONObject() {
\r
158 JSONObject jo = new JSONObject();
\r
159 jo.put("" + subid, lookupNodeID(nodeid));
\r
164 public String getKey() {
\r
169 public boolean equals(Object obj) {
\r
170 if (!(obj instanceof EgressRoute)) {
\r
173 EgressRoute on = (EgressRoute) obj;
\r
174 return (subid == on.subid) && (nodeid == on.nodeid);
\r
178 public int compareTo(EgressRoute er) {
\r
179 return this.subid - er.subid;
\r
183 public String toString() {
\r
184 return String.format("EGRESS: sub=%d, node=%d", subid, nodeid);
\r
188 public int hashCode() {
\r
189 return Objects.hash(subid, nodeid);
\r