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