Merge "Unit test base"
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / onap / dmaap / datarouter / provisioning / beans / EgressRoute.java
1 /*******************************************************************************\r
2  * ============LICENSE_START==================================================\r
3  * * org.onap.dmaap\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
10  * *\r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * *\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
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 \r
24 \r
25 package org.onap.dmaap.datarouter.provisioning.beans;\r
26 \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
34 \r
35 import org.apache.log4j.Logger;\r
36 import org.json.JSONObject;\r
37 import org.onap.dmaap.datarouter.provisioning.utils.DB;\r
38 \r
39 /**\r
40  * The representation of one route in the Egress Route Table.\r
41  *\r
42  * @author Robert P. Eby\r
43  * @version $Id: EgressRoute.java,v 1.3 2013/12/16 20:30:23 eby Exp $\r
44  */\r
45 public class EgressRoute extends NodeClass implements Comparable<EgressRoute> {\r
46     private static Logger intlogger = Logger.getLogger("org.onap.dmaap.datarouter.provisioning.internal");\r
47     private final int subid;\r
48     private final int nodeid;\r
49 \r
50     /**\r
51      * Get a set of all Egress Routes in the DB.  The set is sorted according to the natural sorting order\r
52      * of the routes (based on the subscription ID in each route).\r
53      *\r
54      * @return the sorted set\r
55      */\r
56     public static SortedSet<EgressRoute> getAllEgressRoutes() {\r
57         SortedSet<EgressRoute> set = new TreeSet<EgressRoute>();\r
58         try {\r
59             DB db = new DB();\r
60             @SuppressWarnings("resource")\r
61             Connection conn = db.getConnection();\r
62             Statement stmt = conn.createStatement();\r
63             ResultSet rs = stmt.executeQuery("select SUBID, NODEID from EGRESS_ROUTES");\r
64             while (rs.next()) {\r
65                 int subid = rs.getInt("SUBID");\r
66                 int nodeid = rs.getInt("NODEID");\r
67                 set.add(new EgressRoute(subid, nodeid));\r
68             }\r
69             rs.close();\r
70             stmt.close();\r
71             db.release(conn);\r
72         } catch (SQLException e) {\r
73             e.printStackTrace();\r
74         }\r
75         return set;\r
76     }\r
77 \r
78     /**\r
79      * Get a single Egress Route for the subscription <i>sub</i>.\r
80      *\r
81      * @param sub the subscription to lookup\r
82      * @return an EgressRoute, or null if there is no route for this subscription\r
83      */\r
84     public static EgressRoute getEgressRoute(int sub) {\r
85         EgressRoute v = null;\r
86         PreparedStatement ps = null;\r
87         try {\r
88             DB db = new DB();\r
89             @SuppressWarnings("resource")\r
90             Connection conn = db.getConnection();\r
91             String sql = "select NODEID from EGRESS_ROUTES where SUBID = ?";\r
92             ps = conn.prepareStatement(sql);\r
93             ps.setInt(1, sub);\r
94             ResultSet rs = ps.executeQuery();\r
95             if (rs.next()) {\r
96                 int node = rs.getInt("NODEID");\r
97                 v = new EgressRoute(sub, node);\r
98             }\r
99             rs.close();\r
100             ps.close();\r
101             db.release(conn);\r
102         } catch (SQLException e) {\r
103             e.printStackTrace();\r
104         } finally {\r
105             try {\r
106                 ps.close();\r
107             } catch (SQLException e) {\r
108                 e.printStackTrace();\r
109             }\r
110         }\r
111         return v;\r
112     }\r
113 \r
114     public EgressRoute(int subid, int nodeid) throws IllegalArgumentException {\r
115         this.subid = subid;\r
116         this.nodeid = nodeid;\r
117 // Note: unlike for Feeds, it subscriptions can be removed from the tables, so it is\r
118 // possible that an orphan ERT entry can exist if a sub is removed.\r
119 //        if (Subscription.getSubscriptionById(subid) == null)\r
120 //            throw new IllegalArgumentException("No such subscription: "+subid);\r
121     }\r
122 \r
123     public EgressRoute(int subid, String node) throws IllegalArgumentException {\r
124         this(subid, lookupNodeName(node));\r
125     }\r
126 \r
127     @Override\r
128     public boolean doDelete(Connection c) {\r
129         boolean rv = true;\r
130         PreparedStatement ps = null;\r
131         try {\r
132             String sql = "delete from EGRESS_ROUTES where SUBID = ?";\r
133             ps = c.prepareStatement(sql);\r
134             ps.setInt(1, subid);\r
135             ps.execute();\r
136         } catch (SQLException e) {\r
137             rv = false;\r
138             intlogger.warn("PROV0007 doDelete: " + e.getMessage());\r
139             e.printStackTrace();\r
140         } finally {\r
141             try {\r
142                 ps.close();\r
143             } catch (SQLException e) {\r
144                 e.printStackTrace();\r
145             }\r
146         }\r
147         return rv;\r
148     }\r
149 \r
150     @Override\r
151     public boolean doInsert(Connection c) {\r
152         boolean rv = false;\r
153         PreparedStatement ps = null;\r
154         try {\r
155             // Create the NETWORK_ROUTES row\r
156             String sql = "insert into EGRESS_ROUTES (SUBID, NODEID) values (?, ?)";\r
157             ps = c.prepareStatement(sql);\r
158             ps.setInt(1, this.subid);\r
159             ps.setInt(2, this.nodeid);\r
160             ps.execute();\r
161             ps.close();\r
162             rv = true;\r
163         } catch (SQLException e) {\r
164             intlogger.warn("PROV0005 doInsert: " + e.getMessage());\r
165             e.printStackTrace();\r
166         } finally {\r
167             try {\r
168                 ps.close();\r
169             } catch (SQLException e) {\r
170                 e.printStackTrace();\r
171             }\r
172         }\r
173         return rv;\r
174     }\r
175 \r
176     @Override\r
177     public boolean doUpdate(Connection c) {\r
178         boolean rv = true;\r
179         PreparedStatement ps = null;\r
180         try {\r
181             String sql = "update EGRESS_ROUTES set NODEID = ? where SUBID = ?";\r
182             ps = c.prepareStatement(sql);\r
183             ps.setInt(1, nodeid);\r
184             ps.setInt(2, subid);\r
185             ps.executeUpdate();\r
186         } catch (SQLException e) {\r
187             rv = false;\r
188             intlogger.warn("PROV0006 doUpdate: " + e.getMessage());\r
189             e.printStackTrace();\r
190         } finally {\r
191             try {\r
192                 ps.close();\r
193             } catch (SQLException e) {\r
194                 e.printStackTrace();\r
195             }\r
196         }\r
197         return rv;\r
198     }\r
199 \r
200     @Override\r
201     public JSONObject asJSONObject() {\r
202         JSONObject jo = new JSONObject();\r
203         jo.put("" + subid, lookupNodeID(nodeid));\r
204         return jo;\r
205     }\r
206 \r
207     @Override\r
208     public String getKey() {\r
209         return "" + subid;\r
210     }\r
211 \r
212     @Override\r
213     public boolean equals(Object obj) {\r
214         if (!(obj instanceof EgressRoute))\r
215             return false;\r
216         EgressRoute on = (EgressRoute) obj;\r
217         return (subid == on.subid) && (nodeid == on.nodeid);\r
218     }\r
219 \r
220     @Override\r
221     public int compareTo(EgressRoute o) {\r
222         return this.subid - o.subid;\r
223     }\r
224 \r
225     @Override\r
226     public String toString() {\r
227         return String.format("EGRESS: sub=%d, node=%d", subid, nodeid);\r
228     }\r
229 }\r