Update project structure to org.onap
[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          * @return the sorted set\r
54          */\r
55         public static SortedSet<EgressRoute> getAllEgressRoutes() {\r
56                 SortedSet<EgressRoute> set = new TreeSet<EgressRoute>();\r
57                 try {\r
58                         DB db = new DB();\r
59                         @SuppressWarnings("resource")\r
60                         Connection conn = db.getConnection();\r
61                         Statement  stmt = conn.createStatement();\r
62                         ResultSet    rs = stmt.executeQuery("select SUBID, NODEID from EGRESS_ROUTES");\r
63                         while (rs.next()) {\r
64                                 int subid  = rs.getInt("SUBID");\r
65                                 int nodeid = rs.getInt("NODEID");\r
66                                 set.add(new EgressRoute(subid, nodeid));\r
67                         }\r
68                         rs.close();\r
69                         stmt.close();\r
70                         db.release(conn);\r
71                 } catch (SQLException e) {\r
72                         e.printStackTrace();\r
73                 }\r
74                 return set;\r
75         }\r
76         /**\r
77          * Get a single Egress Route for the subscription <i>sub</i>.\r
78          * @param sub the subscription to lookup\r
79          * @return an EgressRoute, or null if there is no route for this subscription\r
80          */\r
81         public static EgressRoute getEgressRoute(int sub) {\r
82                 EgressRoute v = null;\r
83                 PreparedStatement ps = null;\r
84                 try {\r
85                         DB db = new DB();\r
86                         @SuppressWarnings("resource")\r
87                         Connection conn = db.getConnection();\r
88                         String sql = "select NODEID from EGRESS_ROUTES where SUBID = ?";\r
89                         ps = conn.prepareStatement(sql);\r
90                         ps.setInt(1, sub);\r
91                         ResultSet rs = ps.executeQuery();\r
92                         if (rs.next()) {\r
93                                 int node = rs.getInt("NODEID");\r
94                                 v = new EgressRoute(sub, node);\r
95                         }\r
96                         rs.close();\r
97                         ps.close();\r
98                         db.release(conn);\r
99                 } catch (SQLException e) {\r
100                         e.printStackTrace();\r
101                 } finally {\r
102                         try {\r
103                                 ps.close();\r
104                         } catch (SQLException e) {\r
105                                 e.printStackTrace();\r
106                         }\r
107                 }\r
108                 return v;\r
109         }\r
110 \r
111         public EgressRoute(int subid, int nodeid) throws IllegalArgumentException {\r
112                 this.subid = subid;\r
113                 this.nodeid = nodeid;\r
114 // Note: unlike for Feeds, it subscriptions can be removed from the tables, so it is\r
115 // possible that an orphan ERT entry can exist if a sub is removed.\r
116 //              if (Subscription.getSubscriptionById(subid) == null)\r
117 //                      throw new IllegalArgumentException("No such subscription: "+subid);\r
118         }\r
119 \r
120         public EgressRoute(int subid, String node) throws IllegalArgumentException {\r
121                 this(subid, lookupNodeName(node));\r
122         }\r
123 \r
124         @Override\r
125         public boolean doDelete(Connection c) {\r
126                 boolean rv = true;\r
127                 PreparedStatement ps = null;\r
128                 try {\r
129                         String sql = "delete from EGRESS_ROUTES where SUBID = ?";\r
130                         ps = c.prepareStatement(sql);\r
131                         ps.setInt(1, subid);\r
132                         ps.execute();\r
133                 } catch (SQLException e) {\r
134                         rv = false;\r
135                         intlogger.warn("PROV0007 doDelete: "+e.getMessage());\r
136                         e.printStackTrace();\r
137                 } finally {\r
138                         try {\r
139                                 ps.close();\r
140                         } catch (SQLException e) {\r
141                                 e.printStackTrace();\r
142                         }\r
143                 }\r
144                 return rv;\r
145         }\r
146 \r
147         @Override\r
148         public boolean doInsert(Connection c) {\r
149                 boolean rv = false;\r
150                 PreparedStatement ps = null;\r
151                 try {\r
152                         // Create the NETWORK_ROUTES row\r
153                         String sql = "insert into EGRESS_ROUTES (SUBID, NODEID) values (?, ?)";\r
154                         ps = c.prepareStatement(sql);\r
155                         ps.setInt(1, this.subid);\r
156                         ps.setInt(2, this.nodeid);\r
157                         ps.execute();\r
158                         ps.close();\r
159                         rv = true;\r
160                 } catch (SQLException e) {\r
161                         intlogger.warn("PROV0005 doInsert: "+e.getMessage());\r
162                         e.printStackTrace();\r
163                 } finally {\r
164                         try {\r
165                                 ps.close();\r
166                         } catch (SQLException e) {\r
167                                 e.printStackTrace();\r
168                         }\r
169                 }\r
170                 return rv;\r
171         }\r
172 \r
173         @Override\r
174         public boolean doUpdate(Connection c) {\r
175                 boolean rv = true;\r
176                 PreparedStatement ps = null;\r
177                 try {\r
178                         String sql = "update EGRESS_ROUTES set NODEID = ? where SUBID = ?";\r
179                         ps = c.prepareStatement(sql);\r
180                         ps.setInt(1, nodeid);\r
181                         ps.setInt(2, subid);\r
182                         ps.executeUpdate();\r
183                 } catch (SQLException e) {\r
184                         rv = false;\r
185                         intlogger.warn("PROV0006 doUpdate: "+e.getMessage());\r
186                         e.printStackTrace();\r
187                 } finally {\r
188                         try {\r
189                                 ps.close();\r
190                         } catch (SQLException e) {\r
191                                 e.printStackTrace();\r
192                         }\r
193                 }\r
194                 return rv;\r
195         }\r
196 \r
197         @Override\r
198         public JSONObject asJSONObject() {\r
199                 JSONObject jo = new JSONObject();\r
200                 jo.put(""+subid, lookupNodeID(nodeid));\r
201                 return jo;\r
202         }\r
203 \r
204         @Override\r
205         public String getKey() {\r
206                 return ""+subid;\r
207         }\r
208 \r
209         @Override\r
210         public boolean equals(Object obj) {\r
211                 if (!(obj instanceof EgressRoute))\r
212                         return false;\r
213                 EgressRoute on = (EgressRoute)obj;\r
214                 return (subid == on.subid) && (nodeid == on.nodeid);\r
215         }\r
216 \r
217         @Override\r
218         public int compareTo(EgressRoute o) {\r
219                 return this.subid - o.subid;\r
220         }\r
221 \r
222         @Override\r
223         public String toString() {\r
224                 return String.format("EGRESS: sub=%d, node=%d", subid, nodeid);\r
225         }\r
226 }\r