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