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
116 public int getVianode() {
\r
121 public boolean doDelete(Connection c) {
\r
123 PreparedStatement ps = null;
\r
125 String sql = "delete from NETWORK_ROUTES where FROMNODE = ? AND TONODE = ?";
\r
126 ps = c.prepareStatement(sql);
\r
127 ps.setInt(1, fromnode);
\r
128 ps.setInt(2, tonode);
\r
130 } catch (SQLException e) {
\r
132 intlogger.warn("PROV0007 doDelete: " + e.getMessage(), e);
\r
138 } catch (SQLException e) {
\r
139 intlogger.error(SQLEXCEPTION + e.getMessage(), e);
\r
146 public boolean doInsert(Connection c) {
\r
147 boolean rv = false;
\r
148 if (this.vianode >= 0) {
\r
149 PreparedStatement ps = null;
\r
151 // Create the NETWORK_ROUTES row
\r
152 String sql = "insert into NETWORK_ROUTES (FROMNODE, TONODE, VIANODE) values (?, ?, ?)";
\r
153 ps = c.prepareStatement(sql);
\r
154 ps.setInt(1, this.fromnode);
\r
155 ps.setInt(2, this.tonode);
\r
156 ps.setInt(3, this.vianode);
\r
160 } catch (SQLException e) {
\r
161 intlogger.warn("PROV0005 doInsert: " + e.getMessage(), e);
\r
167 } catch (SQLException e) {
\r
168 intlogger.error(SQLEXCEPTION + e.getMessage(), e);
\r
176 public boolean doUpdate(Connection c) {
\r
178 PreparedStatement ps = null;
\r
180 String sql = "update NETWORK_ROUTES set VIANODE = ? where FROMNODE = ? and TONODE = ?";
\r
181 ps = c.prepareStatement(sql);
\r
182 ps.setInt(1, vianode);
\r
183 ps.setInt(2, fromnode);
\r
184 ps.setInt(3, tonode);
\r
185 ps.executeUpdate();
\r
186 } catch (SQLException e) {
\r
188 intlogger.warn("PROV0006 doUpdate: " + e.getMessage(), e);
\r
194 } catch (SQLException e) {
\r
195 intlogger.error(SQLEXCEPTION + e.getMessage(), e);
\r
202 public JSONObject asJSONObject() {
\r
203 JSONObject jo = new JSONObject();
\r
204 jo.put("from", lookupNodeID(fromnode));
\r
205 jo.put("to", lookupNodeID(tonode));
\r
206 jo.put("via", lookupNodeID(vianode));
\r
211 public String getKey() {
\r
212 return lookupNodeID(fromnode) + ":" + lookupNodeID(tonode);
\r
216 public boolean equals(Object obj) {
\r
217 if (!(obj instanceof NetworkRoute)) {
\r
220 NetworkRoute on = (NetworkRoute) obj;
\r
221 return (fromnode == on.fromnode) && (tonode == on.tonode) && (vianode == on.vianode);
\r
225 public int hashCode() {
\r
226 return Objects.hash(fromnode, tonode, vianode);
\r
230 public int compareTo(NetworkRoute o) {
\r
231 if (this.fromnode == o.fromnode) {
\r
232 if (this.tonode == o.tonode) {
\r
233 return this.vianode - o.vianode;
\r
235 return this.tonode - o.tonode;
\r
237 return this.fromnode - o.fromnode;
\r
241 public String toString() {
\r
242 return String.format("NETWORK: from=%d, to=%d, via=%d", fromnode, tonode, vianode);
\r