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