2b6462db7630c8946302a20f9edb084a6eef1923
[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 com.att.eelf.configuration.EELFLogger;\r
28 import com.att.eelf.configuration.EELFManager;\r
29 import java.sql.Connection;\r
30 import java.sql.PreparedStatement;\r
31 import java.sql.ResultSet;\r
32 import java.sql.SQLException;\r
33 import java.sql.Statement;\r
34 import java.util.Objects;\r
35 import java.util.SortedSet;\r
36 import java.util.TreeSet;\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 \r
48     private static EELFLogger intlogger = EELFManager.getInstance().getLogger("InternalLog");\r
49     private final int subid;\r
50     private final int nodeid;\r
51 \r
52     /**\r
53      * EgressRoute constructor.\r
54      * @param subid subscription id\r
55      * @param nodeid node id\r
56      */\r
57     public EgressRoute(int subid, int nodeid) {\r
58         this.subid = subid;\r
59         this.nodeid = nodeid;\r
60     }\r
61 \r
62     public EgressRoute(int subid, String node) {\r
63         this(subid, lookupNodeName(node));\r
64     }\r
65 \r
66     /**\r
67      * Get a set of all Egress Routes in the DB.  The set is sorted according to the natural sorting order of the routes\r
68      * (based on the subscription ID in each route).\r
69      *\r
70      * @return the sorted set\r
71      */\r
72     public static SortedSet<EgressRoute> getAllEgressRoutes() {\r
73         SortedSet<EgressRoute> set = new TreeSet<>();\r
74         DB db = new DB();\r
75         String sql = "select SUBID, NODEID from EGRESS_ROUTES";\r
76         try (Connection conn = db.getConnection()) {\r
77             try (Statement stmt = conn.createStatement()) {\r
78                 try (ResultSet rs = stmt.executeQuery(sql)) {\r
79                     addEgressRouteToSet(set, rs);\r
80                 }\r
81             } finally {\r
82                 db.release(conn);\r
83             }\r
84         } catch (SQLException e) {\r
85             intlogger.error("PROV0008 EgressRoute.getAllEgressRoutes: " + e.getMessage(), e);\r
86         }\r
87         return set;\r
88     }\r
89 \r
90     private static void addEgressRouteToSet(SortedSet<EgressRoute> set, ResultSet rs) throws SQLException {\r
91         while (rs.next()) {\r
92             int subid = rs.getInt("SUBID");\r
93             int nodeid = rs.getInt("NODEID");\r
94             set.add(new EgressRoute(subid, nodeid));\r
95         }\r
96     }\r
97 \r
98     /**\r
99      * Get a single Egress Route for the subscription <i>sub</i>.\r
100      *\r
101      * @param sub the subscription to lookup\r
102      * @return an EgressRoute, or null if there is no route for this subscription\r
103      */\r
104     public static EgressRoute getEgressRoute(int sub) {\r
105         EgressRoute er = null;\r
106         DB db = new DB();\r
107         String sql = "select NODEID from EGRESS_ROUTES where SUBID = ?";\r
108         try (Connection conn = db.getConnection();\r
109                 PreparedStatement ps = conn.prepareStatement(sql)) {\r
110             ps.setInt(1, sub);\r
111             try (ResultSet rs = ps.executeQuery()) {\r
112                 if (rs.next()) {\r
113                     int node = rs.getInt("NODEID");\r
114                     er = new EgressRoute(sub, node);\r
115                 }\r
116             } finally {\r
117                 db.release(conn);\r
118             }\r
119         } catch (SQLException e) {\r
120             intlogger.error("PROV0009 EgressRoute.getEgressRoute: " + e.getMessage(), e);\r
121         }\r
122         return er;\r
123     }\r
124 \r
125     @Override\r
126     public boolean doDelete(Connection conn) {\r
127         boolean rv = true;\r
128         String sql = "delete from EGRESS_ROUTES where SUBID = ?";\r
129         try (PreparedStatement ps = conn.prepareStatement(sql)) {\r
130             ps.setInt(1, subid);\r
131             ps.execute();\r
132         } catch (SQLException e) {\r
133             rv = false;\r
134             intlogger.error("PROV0007 doDelete: " + e.getMessage(), e);\r
135         }\r
136         return rv;\r
137     }\r
138 \r
139     @Override\r
140     public boolean doInsert(Connection conn) {\r
141         boolean rv = false;\r
142         String sql = "insert into EGRESS_ROUTES (SUBID, NODEID) values (?, ?)";\r
143         try (PreparedStatement ps = conn.prepareStatement(sql)) {\r
144             // Create the NETWORK_ROUTES row\r
145             ps.setInt(1, this.subid);\r
146             ps.setInt(2, this.nodeid);\r
147             ps.execute();\r
148             rv = true;\r
149         } catch (SQLException e) {\r
150             intlogger.warn("PROV0005 doInsert: " + e.getMessage(), e);\r
151         }\r
152         return rv;\r
153     }\r
154 \r
155     @Override\r
156     public boolean doUpdate(Connection conn) {\r
157         boolean rv = true;\r
158         String sql = "update EGRESS_ROUTES set NODEID = ? where SUBID = ?";\r
159         try (PreparedStatement ps = conn.prepareStatement(sql)) {\r
160             ps.setInt(1, nodeid);\r
161             ps.setInt(2, subid);\r
162             ps.executeUpdate();\r
163         } catch (SQLException e) {\r
164             rv = false;\r
165             intlogger.warn("PROV0006 doUpdate: " + e.getMessage(), e);\r
166         }\r
167         return rv;\r
168     }\r
169 \r
170     @Override\r
171     public JSONObject asJSONObject() {\r
172         JSONObject jo = new JSONObject();\r
173         jo.put("" + subid, lookupNodeID(nodeid));\r
174         return jo;\r
175     }\r
176 \r
177     @Override\r
178     public String getKey() {\r
179         return "" + subid;\r
180     }\r
181 \r
182     @Override\r
183     public boolean equals(Object obj) {\r
184         if (!(obj instanceof EgressRoute)) {\r
185             return false;\r
186         }\r
187         EgressRoute on = (EgressRoute) obj;\r
188         return (subid == on.subid) && (nodeid == on.nodeid);\r
189     }\r
190 \r
191     @Override\r
192     public int compareTo(EgressRoute er) {\r
193         return this.subid - er.subid;\r
194     }\r
195 \r
196     @Override\r
197     public String toString() {\r
198         return String.format("EGRESS: sub=%d, node=%d", subid, nodeid);\r
199     }\r
200 \r
201     @Override\r
202     public int hashCode() {\r
203         return Objects.hash(subid, nodeid);\r
204     }\r
205 }\r