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 Network Route Table.
\r
44 * @author Robert P. Eby
\r
45 * @version $Id: NetworkRoute.java,v 1.2 2013/12/16 20:30:23 eby Exp $
\r
47 public class NetworkRoute extends NodeClass implements Comparable<NetworkRoute> {
\r
49 private static EELFLogger intlogger = EELFManager.getInstance().getLogger("InternalLog");
\r
50 private static final String SQLEXCEPTION = "SQLException: ";
\r
51 private final int fromnode;
\r
52 private final int tonode;
\r
53 private final int vianode;
\r
56 * Get a set of all Network Routes in the DB. The set is sorted according to the natural sorting order of the
\r
57 * routes (based on the from and to node names in each route).
\r
59 * @return the sorted set
\r
61 public static SortedSet<NetworkRoute> getAllNetworkRoutes() {
\r
62 SortedSet<NetworkRoute> set = new TreeSet<>();
\r
65 @SuppressWarnings("resource")
\r
66 Connection conn = db.getConnection();
\r
67 try (Statement stmt = conn.createStatement()) {
\r
68 try (ResultSet rs = stmt.executeQuery("select FROMNODE, TONODE, VIANODE from NETWORK_ROUTES")) {
\r
70 int fromnode = rs.getInt("FROMNODE");
\r
71 int tonode = rs.getInt("TONODE");
\r
72 int vianode = rs.getInt("VIANODE");
\r
73 set.add(new NetworkRoute(fromnode, tonode, vianode));
\r
78 } catch (SQLException e) {
\r
79 intlogger.error(SQLEXCEPTION + e.getMessage(), e);
\r
84 public NetworkRoute(String fromnode, String tonode) {
\r
85 this.fromnode = lookupNodeName(fromnode);
\r
86 this.tonode = lookupNodeName(tonode);
\r
90 public NetworkRoute(String fromnode, String tonode, String vianode) {
\r
91 this.fromnode = lookupNodeName(fromnode);
\r
92 this.tonode = lookupNodeName(tonode);
\r
93 this.vianode = lookupNodeName(vianode);
\r
96 public NetworkRoute(JSONObject jo) {
\r
97 this.fromnode = lookupNodeName(jo.getString("from"));
\r
98 this.tonode = lookupNodeName(jo.getString("to"));
\r
99 this.vianode = lookupNodeName(jo.getString("via"));
\r
102 public NetworkRoute(int fromnode, int tonode, int vianode) {
\r
103 this.fromnode = fromnode;
\r
104 this.tonode = tonode;
\r
105 this.vianode = vianode;
\r
108 public int getFromnode() {
\r
112 public int getTonode() {
\r
117 public boolean doDelete(Connection c) {
\r
119 PreparedStatement ps = null;
\r
121 String sql = "delete from NETWORK_ROUTES where FROMNODE = ? AND TONODE = ?";
\r
122 ps = c.prepareStatement(sql);
\r
123 ps.setInt(1, fromnode);
\r
124 ps.setInt(2, tonode);
\r
126 } catch (SQLException e) {
\r
128 intlogger.warn("PROV0007 doDelete: " + e.getMessage(), e);
\r
134 } catch (SQLException e) {
\r
135 intlogger.error(SQLEXCEPTION + e.getMessage(), e);
\r
142 public boolean doInsert(Connection c) {
\r
143 boolean rv = false;
\r
144 if (this.vianode >= 0) {
\r
145 PreparedStatement ps = null;
\r
147 // Create the NETWORK_ROUTES row
\r
148 String sql = "insert into NETWORK_ROUTES (FROMNODE, TONODE, VIANODE) values (?, ?, ?)";
\r
149 ps = c.prepareStatement(sql);
\r
150 ps.setInt(1, this.fromnode);
\r
151 ps.setInt(2, this.tonode);
\r
152 ps.setInt(3, this.vianode);
\r
156 } catch (SQLException e) {
\r
157 intlogger.warn("PROV0005 doInsert: " + e.getMessage(), e);
\r
163 } catch (SQLException e) {
\r
164 intlogger.error(SQLEXCEPTION + e.getMessage(), e);
\r
172 public boolean doUpdate(Connection c) {
\r
174 PreparedStatement ps = null;
\r
176 String sql = "update NETWORK_ROUTES set VIANODE = ? where FROMNODE = ? and TONODE = ?";
\r
177 ps = c.prepareStatement(sql);
\r
178 ps.setInt(1, vianode);
\r
179 ps.setInt(2, fromnode);
\r
180 ps.setInt(3, tonode);
\r
181 ps.executeUpdate();
\r
182 } catch (SQLException e) {
\r
184 intlogger.warn("PROV0006 doUpdate: " + e.getMessage(), e);
\r
190 } catch (SQLException e) {
\r
191 intlogger.error(SQLEXCEPTION + e.getMessage(), e);
\r
198 public JSONObject asJSONObject() {
\r
199 JSONObject jo = new JSONObject();
\r
200 jo.put("from", lookupNodeID(fromnode));
\r
201 jo.put("to", lookupNodeID(tonode));
\r
202 jo.put("via", lookupNodeID(vianode));
\r
207 public String getKey() {
\r
208 return lookupNodeID(fromnode) + ":" + lookupNodeID(tonode);
\r
212 public boolean equals(Object obj) {
\r
213 if (!(obj instanceof NetworkRoute)) {
\r
216 NetworkRoute on = (NetworkRoute) obj;
\r
217 return (fromnode == on.fromnode) && (tonode == on.tonode) && (vianode == on.vianode);
\r
221 public int hashCode() {
\r
222 return Objects.hash(fromnode, tonode, vianode);
\r
226 public int compareTo(NetworkRoute o) {
\r
227 if (this.fromnode == o.fromnode) {
\r
228 if (this.tonode == o.tonode) {
\r
229 return this.vianode - o.vianode;
\r
231 return this.tonode - o.tonode;
\r
233 return this.fromnode - o.fromnode;
\r
237 public String toString() {
\r
238 return String.format("NETWORK: from=%d, to=%d, via=%d", fromnode, tonode, vianode);
\r