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.SortedSet;
\r
33 import java.util.TreeSet;
\r
35 import org.apache.log4j.Logger;
\r
36 import org.json.JSONObject;
\r
37 import org.onap.dmaap.datarouter.provisioning.utils.DB;
\r
40 * The representation of one route in the Network Route Table.
\r
42 * @author Robert P. Eby
\r
43 * @version $Id: NetworkRoute.java,v 1.2 2013/12/16 20:30:23 eby Exp $
\r
45 public class NetworkRoute extends NodeClass implements Comparable<NetworkRoute> {
\r
46 private static Logger intlogger = Logger.getLogger("org.onap.dmaap.datarouter.provisioning.internal");
\r
47 private final int fromnode;
\r
48 private final int tonode;
\r
49 private final int vianode;
\r
52 * Get a set of all Network Routes in the DB. The set is sorted according to the natural sorting order
\r
53 * of the routes (based on the from and to node names in each route).
\r
54 * @return the sorted set
\r
56 public static SortedSet<NetworkRoute> getAllNetworkRoutes() {
\r
57 SortedSet<NetworkRoute> set = new TreeSet<NetworkRoute>();
\r
60 @SuppressWarnings("resource")
\r
61 Connection conn = db.getConnection();
\r
62 Statement stmt = conn.createStatement();
\r
63 ResultSet rs = stmt.executeQuery("select FROMNODE, TONODE, VIANODE from NETWORK_ROUTES");
\r
65 int fromnode = rs.getInt("FROMNODE");
\r
66 int tonode = rs.getInt("TONODE");
\r
67 int vianode = rs.getInt("VIANODE");
\r
68 set.add(new NetworkRoute(fromnode, tonode, vianode));
\r
73 } catch (SQLException e) {
\r
74 e.printStackTrace();
\r
79 public NetworkRoute(String fromnode, String tonode) throws IllegalArgumentException {
\r
80 this.fromnode = lookupNodeName(fromnode);
\r
81 this.tonode = lookupNodeName(tonode);
\r
85 public NetworkRoute(String fromnode, String tonode, String vianode) throws IllegalArgumentException {
\r
86 this.fromnode = lookupNodeName(fromnode);
\r
87 this.tonode = lookupNodeName(tonode);
\r
88 this.vianode = lookupNodeName(vianode);
\r
91 public NetworkRoute(JSONObject jo) throws IllegalArgumentException {
\r
92 this.fromnode = lookupNodeName(jo.getString("from"));
\r
93 this.tonode = lookupNodeName(jo.getString("to"));
\r
94 this.vianode = lookupNodeName(jo.getString("via"));
\r
97 public NetworkRoute(int fromnode, int tonode, int vianode) throws IllegalArgumentException {
\r
98 this.fromnode = fromnode;
\r
99 this.tonode = tonode;
\r
100 this.vianode = vianode;
\r
103 public int getFromnode() {
\r
107 public int getTonode() {
\r
111 public int getVianode() {
\r
116 public boolean doDelete(Connection c) {
\r
118 PreparedStatement ps = null;
\r
120 String sql = "delete from NETWORK_ROUTES where FROMNODE = ? AND TONODE = ?";
\r
121 ps = c.prepareStatement(sql);
\r
122 ps.setInt(1, fromnode);
\r
123 ps.setInt(2, tonode);
\r
125 } catch (SQLException e) {
\r
127 intlogger.warn("PROV0007 doDelete: "+e.getMessage());
\r
128 e.printStackTrace();
\r
132 } catch (SQLException e) {
\r
133 e.printStackTrace();
\r
140 public boolean doInsert(Connection c) {
\r
141 boolean rv = false;
\r
142 if (this.vianode >= 0) {
\r
143 PreparedStatement ps = null;
\r
145 // Create the NETWORK_ROUTES row
\r
146 String sql = "insert into NETWORK_ROUTES (FROMNODE, TONODE, VIANODE) values (?, ?, ?)";
\r
147 ps = c.prepareStatement(sql);
\r
148 ps.setInt(1, this.fromnode);
\r
149 ps.setInt(2, this.tonode);
\r
150 ps.setInt(3, this.vianode);
\r
154 } catch (SQLException e) {
\r
155 intlogger.warn("PROV0005 doInsert: "+e.getMessage());
\r
156 e.printStackTrace();
\r
160 } catch (SQLException e) {
\r
161 e.printStackTrace();
\r
169 public boolean doUpdate(Connection c) {
\r
171 PreparedStatement ps = null;
\r
173 String sql = "update NETWORK_ROUTES set VIANODE = ? where FROMNODE = ? and TONODE = ?";
\r
174 ps = c.prepareStatement(sql);
\r
175 ps.setInt(1, vianode);
\r
176 ps.setInt(2, fromnode);
\r
177 ps.setInt(3, tonode);
\r
178 ps.executeUpdate();
\r
179 } catch (SQLException e) {
\r
181 intlogger.warn("PROV0006 doUpdate: "+e.getMessage());
\r
182 e.printStackTrace();
\r
186 } catch (SQLException e) {
\r
187 e.printStackTrace();
\r
194 public JSONObject asJSONObject() {
\r
195 JSONObject jo = new JSONObject();
\r
196 jo.put("from", lookupNodeID(fromnode));
\r
197 jo.put("to", lookupNodeID(tonode));
\r
198 jo.put("via", lookupNodeID(vianode));
\r
203 public String getKey() {
\r
204 return lookupNodeID(fromnode)+":"+lookupNodeID(tonode);
\r
208 public boolean equals(Object obj) {
\r
209 if (!(obj instanceof NetworkRoute))
\r
211 NetworkRoute on = (NetworkRoute)obj;
\r
212 return (fromnode == on.fromnode) && (tonode == on.tonode) && (vianode == on.vianode);
\r
216 public int compareTo(NetworkRoute o) {
\r
217 if (this.fromnode == o.fromnode) {
\r
218 if (this.tonode == o.tonode)
\r
219 return this.vianode - o.vianode;
\r
220 return this.tonode - o.tonode;
\r
222 return this.fromnode - o.fromnode;
\r
226 public String toString() {
\r
227 return String.format("NETWORK: from=%d, to=%d, via=%d", fromnode, tonode, vianode);
\r