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