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.util.Objects;
\r
34 import java.util.SortedSet;
\r
35 import java.util.TreeSet;
\r
36 import org.json.JSONObject;
\r
37 import org.onap.dmaap.datarouter.provisioning.utils.ProvDbUtils;
\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
47 private static EELFLogger intlogger = EELFManager.getInstance().getLogger("InternalLog");
\r
48 private static final String SQLEXCEPTION = "SQLException: ";
\r
49 private final int fromnode;
\r
50 private final int tonode;
\r
51 private final int vianode;
\r
54 * NetworkRoute Constructor.
\r
55 * @param fromnode node source
\r
56 * @param tonode node destination
\r
58 public NetworkRoute(String fromnode, String tonode) {
\r
59 this.fromnode = lookupNodeName(fromnode);
\r
60 this.tonode = lookupNodeName(tonode);
\r
65 * NetworkRoute Constructor.
\r
66 * @param fromnode node source
\r
67 * @param tonode node destination
\r
68 * @param vianode via node
\r
70 public NetworkRoute(String fromnode, String tonode, String vianode) {
\r
71 this.fromnode = lookupNodeName(fromnode);
\r
72 this.tonode = lookupNodeName(tonode);
\r
73 this.vianode = lookupNodeName(vianode);
\r
77 * NetworkRoute Constructor.
\r
78 * @param jo JSONObject of attributes
\r
80 public NetworkRoute(JSONObject jo) {
\r
81 this.fromnode = lookupNodeName(jo.getString("from"));
\r
82 this.tonode = lookupNodeName(jo.getString("to"));
\r
83 this.vianode = lookupNodeName(jo.getString("via"));
\r
87 * NetworkRoute Constructor.
\r
88 * @param fromnode integer source node
\r
89 * @param tonode integer destination node
\r
90 * @param vianode integer via node
\r
92 private NetworkRoute(int fromnode, int tonode, int vianode) {
\r
93 this.fromnode = fromnode;
\r
94 this.tonode = tonode;
\r
95 this.vianode = vianode;
\r
99 * Get a set of all Network Routes in the DB. The set is sorted according to the natural sorting order of the
\r
100 * routes (based on the from and to node names in each route).
\r
102 * @return the sorted set
\r
104 public static SortedSet<NetworkRoute> getAllNetworkRoutes() {
\r
105 SortedSet<NetworkRoute> set = new TreeSet<>();
\r
106 try (Connection conn = ProvDbUtils.getInstance().getConnection();
\r
107 PreparedStatement ps = conn.prepareStatement("select FROMNODE, TONODE, VIANODE from NETWORK_ROUTES");
\r
108 ResultSet rs = ps.executeQuery()) {
\r
109 addNetworkRouteToSet(set, rs);
\r
110 } catch (SQLException e) {
\r
111 intlogger.error(SQLEXCEPTION + e.getMessage(), e);
\r
116 private static void addNetworkRouteToSet(SortedSet<NetworkRoute> set, ResultSet rs) throws SQLException {
\r
117 while (rs.next()) {
\r
118 int fromnode = rs.getInt("FROMNODE");
\r
119 int tonode = rs.getInt("TONODE");
\r
120 int vianode = rs.getInt("VIANODE");
\r
121 set.add(new NetworkRoute(fromnode, tonode, vianode));
\r
125 public int getFromnode() {
\r
129 public int getTonode() {
\r
138 public boolean doDelete(Connection conn) {
\r
140 try (PreparedStatement ps = conn.prepareStatement(
\r
141 "delete from NETWORK_ROUTES where FROMNODE = ? AND TONODE = ?")) {
\r
142 ps.setInt(1, fromnode);
\r
143 ps.setInt(2, tonode);
\r
145 } catch (SQLException e) {
\r
147 intlogger.warn("PROV0007 doDelete: " + e.getMessage(), e);
\r
153 public boolean doInsert(Connection conn) {
\r
154 boolean rv = false;
\r
155 if (this.vianode >= 0) {
\r
156 try (PreparedStatement ps = conn.prepareStatement(
\r
157 "insert into NETWORK_ROUTES (FROMNODE, TONODE, VIANODE) values (?, ?, ?)")) {
\r
158 // Create the NETWORK_ROUTES row
\r
159 ps.setInt(1, this.fromnode);
\r
160 ps.setInt(2, this.tonode);
\r
161 ps.setInt(3, this.vianode);
\r
164 } catch (SQLException e) {
\r
165 intlogger.warn("PROV0005 doInsert: " + e.getMessage(), e);
\r
172 public boolean doUpdate(Connection conn) {
\r
174 try (PreparedStatement ps = conn.prepareStatement(
\r
175 "update NETWORK_ROUTES set VIANODE = ? where FROMNODE = ? and TONODE = ?")) {
\r
176 ps.setInt(1, vianode);
\r
177 ps.setInt(2, fromnode);
\r
178 ps.setInt(3, tonode);
\r
179 ps.executeUpdate();
\r
180 } catch (SQLException e) {
\r
182 intlogger.warn("PROV0006 doUpdate: " + e.getMessage(), e);
\r
188 public JSONObject asJSONObject() {
\r
189 JSONObject jo = new JSONObject();
\r
190 jo.put("from", lookupNodeID(fromnode));
\r
191 jo.put("to", lookupNodeID(tonode));
\r
192 jo.put("via", lookupNodeID(vianode));
\r
197 public String getKey() {
\r
198 return lookupNodeID(fromnode) + ":" + lookupNodeID(tonode);
\r
202 public boolean equals(Object obj) {
\r
203 if (!(obj instanceof NetworkRoute)) {
\r
206 NetworkRoute on = (NetworkRoute) obj;
\r
207 return (fromnode == on.fromnode) && (tonode == on.tonode) && (vianode == on.vianode);
\r
211 public int hashCode() {
\r
212 return Objects.hash(fromnode, tonode, vianode);
\r
216 public int compareTo(NetworkRoute nr) {
\r
217 if (this.fromnode == nr.fromnode) {
\r
218 if (this.tonode == nr.tonode) {
\r
219 return this.vianode - nr.vianode;
\r
221 return this.tonode - nr.tonode;
\r
223 return this.fromnode - nr.fromnode;
\r
227 public String toString() {
\r
228 return String.format("NETWORK: from=%d, to=%d, via=%d", fromnode, tonode, vianode);
\r