Fix Group Vulnerabilities
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / onap / dmaap / datarouter / provisioning / beans / Group.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 package org.onap.dmaap.datarouter.provisioning.beans;\r
25 \r
26 import java.io.InvalidObjectException;\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.*;\r
33 \r
34 import org.apache.log4j.Logger;\r
35 import org.json.JSONObject;\r
36 import org.onap.dmaap.datarouter.provisioning.utils.DB;\r
37 import org.onap.dmaap.datarouter.provisioning.utils.URLUtilities;\r
38 \r
39 /**\r
40  * The representation of a Subscription.  Subscriptions can be retrieved from the DB, or stored/updated in the DB.\r
41  *\r
42  * @author vikram\r
43  * @version $Id: Group.java,v 1.0 2016/07/19\r
44  */\r
45 public class Group extends Syncable {\r
46     private static Logger intlogger = Logger.getLogger("org.onap.dmaap.datarouter.provisioning.internal");\r
47     private static int next_groupid = getMaxGroupID() + 1;\r
48 \r
49     private int groupid;\r
50     private String authid;\r
51     private String name;\r
52     private String description;\r
53     private String classification;\r
54     private String members;\r
55     private Date last_mod;\r
56 \r
57 \r
58     public static Group getGroupMatching(Group gup) {\r
59         String sql = String.format(\r
60                 "select * from GROUPS where NAME='%s'",\r
61                 gup.getName()\r
62         );\r
63         List<Group> list = getGroupsForSQL(sql);\r
64         return list.size() > 0 ? list.get(0) : null;\r
65     }\r
66 \r
67     public static Group getGroupMatching(Group gup, int groupid) {\r
68         String sql = String.format(\r
69                 "select * from GROUPS where  NAME = '%s' and GROUPID != %d ",\r
70                 gup.getName(),\r
71                 gup.getGroupid()\r
72         );\r
73         List<Group> list = getGroupsForSQL(sql);\r
74         return list.size() > 0 ? list.get(0) : null;\r
75     }\r
76 \r
77     public static Group getGroupById(int id) {\r
78         String sql = "select * from GROUPS where GROUPID = " + id;\r
79         List<Group> list = getGroupsForSQL(sql);\r
80         return list.size() > 0 ? list.get(0) : null;\r
81     }\r
82 \r
83     public static Group getGroupByAuthId(String id) {\r
84         String sql = "select * from GROUPS where AUTHID = '" + id + "'";\r
85         List<Group> list = getGroupsForSQL(sql);\r
86         return list.size() > 0 ? list.get(0) : null;\r
87     }\r
88 \r
89     public static Collection<Group> getAllgroups() {\r
90         return getGroupsForSQL("select * from GROUPS");\r
91     }\r
92 \r
93     private static List<Group> getGroupsForSQL(String sql) {\r
94         List<Group> list = new ArrayList<Group>();\r
95         try {\r
96             DB db = new DB();\r
97             @SuppressWarnings("resource")\r
98             Connection conn = db.getConnection();\r
99             try(Statement stmt = conn.createStatement()) {\r
100                 try(ResultSet rs = stmt.executeQuery(sql)) {\r
101                     while (rs.next()) {\r
102                         Group group = new Group(rs);\r
103                         list.add(group);\r
104                     }\r
105                 }\r
106             }\r
107             db.release(conn);\r
108         } catch (SQLException e) {\r
109             intlogger.error("SQLException " + e.getMessage());\r
110         }\r
111         return list;\r
112     }\r
113 \r
114     public static int getMaxGroupID() {\r
115         int max = 0;\r
116         try {\r
117             DB db = new DB();\r
118             @SuppressWarnings("resource")\r
119             Connection conn = db.getConnection();\r
120             try(Statement stmt = conn.createStatement()) {\r
121                 try(ResultSet rs = stmt.executeQuery("select MAX(groupid) from GROUPS")) {\r
122                     if (rs.next()) {\r
123                         max = rs.getInt(1);\r
124                     }\r
125                 }\r
126             }\r
127             db.release(conn);\r
128         } catch (SQLException e) {\r
129             intlogger.info("getMaxSubID: " + e.getMessage());\r
130             intlogger.error("SQLException " + e.getMessage());\r
131         }\r
132         return max;\r
133     }\r
134 \r
135     public static Collection<String> getGroupsByClassfication(String classfication) {\r
136         List<String> list = new ArrayList<>();\r
137         String sql = "select * from GROUPS where classification = ?";\r
138         try {\r
139             DB db = new DB();\r
140             @SuppressWarnings("resource")\r
141             Connection conn = db.getConnection();\r
142             try(PreparedStatement stmt = conn.prepareStatement(sql)) {\r
143                 stmt.setString(1, classfication);\r
144                 try(ResultSet rs = stmt.executeQuery()) {\r
145                     while (rs.next()) {\r
146                         int groupid = rs.getInt("groupid");\r
147 \r
148                     }\r
149                 }\r
150             }\r
151             db.release(conn);\r
152         } catch (SQLException e) {\r
153             intlogger.error("SQLException " + e.getMessage());\r
154         }\r
155         return list;\r
156     }\r
157 \r
158     /**\r
159      * Return a count of the number of active subscriptions in the DB.\r
160      *\r
161      * @return the count\r
162      */\r
163     public static int countActiveSubscriptions() {\r
164         int count = 0;\r
165         try {\r
166             DB db = new DB();\r
167             @SuppressWarnings("resource")\r
168             Connection conn = db.getConnection();\r
169             try(Statement stmt = conn.createStatement()) {\r
170                 try(ResultSet rs = stmt.executeQuery("select count(*) from SUBSCRIPTIONS")) {\r
171                     if (rs.next()) {\r
172                         count = rs.getInt(1);\r
173                     }\r
174                 }\r
175             }\r
176             db.release(conn);\r
177         } catch (SQLException e) {\r
178             intlogger.warn("PROV0008 countActiveSubscriptions: " + e.getMessage());\r
179             intlogger.error("SQLException " + e.getMessage());\r
180         }\r
181         return count;\r
182     }\r
183 \r
184     public Group() {\r
185         this("", "", "");\r
186     }\r
187 \r
188     public Group(String name, String desc, String members) {\r
189         this.groupid = -1;\r
190         this.authid = "";\r
191         this.name = name;\r
192         this.description = desc;\r
193         this.members = members;\r
194         this.classification = "";\r
195         this.last_mod = new Date();\r
196     }\r
197 \r
198 \r
199     public Group(ResultSet rs) throws SQLException {\r
200         this.groupid = rs.getInt("GROUPID");\r
201         this.authid = rs.getString("AUTHID");\r
202         this.name = rs.getString("NAME");\r
203         this.description = rs.getString("DESCRIPTION");\r
204         this.classification = rs.getString("CLASSIFICATION");\r
205         this.members = rs.getString("MEMBERS");\r
206         this.last_mod = rs.getDate("LAST_MOD");\r
207     }\r
208 \r
209 \r
210     public Group(JSONObject jo) throws InvalidObjectException {\r
211         this("", "", "");\r
212         try {\r
213             // The JSONObject is assumed to contain a vnd.att-dr.group representation\r
214             this.groupid = jo.optInt("groupid", -1);\r
215             String gname = jo.getString("name");\r
216             String gdescription = jo.getString("description");\r
217 \r
218             this.authid = jo.getString("authid");\r
219             this.name = gname;\r
220             this.description = gdescription;\r
221             this.classification = jo.getString("classification");\r
222             this.members = jo.getString("members");\r
223 \r
224             if (gname.length() > 50)\r
225                 throw new InvalidObjectException("Group name is too long");\r
226             if (gdescription.length() > 256)\r
227                 throw new InvalidObjectException("Group Description is too long");\r
228         } catch (InvalidObjectException e) {\r
229             throw e;\r
230         } catch (Exception e) {\r
231             throw new InvalidObjectException("invalid JSON: " + e.getMessage());\r
232         }\r
233     }\r
234 \r
235     public int getGroupid() {\r
236         return groupid;\r
237     }\r
238 \r
239     public static Logger getIntlogger() {\r
240         return intlogger;\r
241     }\r
242 \r
243     public void setGroupid(int groupid) {\r
244         this.groupid = groupid;\r
245     }\r
246 \r
247     public static void setIntlogger(Logger intlogger) {\r
248         Group.intlogger = intlogger;\r
249     }\r
250 \r
251     public static int getNext_groupid() {\r
252         return next_groupid;\r
253     }\r
254 \r
255     public static void setNext_groupid(int next_groupid) {\r
256         Group.next_groupid = next_groupid;\r
257     }\r
258 \r
259     public String getAuthid() {\r
260         return authid;\r
261     }\r
262 \r
263     public void setAuthid(String authid) {\r
264         this.authid = authid;\r
265     }\r
266 \r
267     public String getName() {\r
268         return name;\r
269     }\r
270 \r
271     public void setName(String name) {\r
272         this.name = name;\r
273     }\r
274 \r
275     public String getDescription() {\r
276         return description;\r
277     }\r
278 \r
279     public void setDescription(String description) {\r
280         this.description = description;\r
281     }\r
282 \r
283     public String getClassification() {\r
284         return classification;\r
285     }\r
286 \r
287     public void setClassification(String classification) {\r
288         this.classification = classification;\r
289     }\r
290 \r
291     public String getMembers() {\r
292         return members;\r
293     }\r
294 \r
295     public void setMembers(String members) {\r
296         this.members = members;\r
297     }\r
298 \r
299     public Date getLast_mod() {\r
300         return last_mod;\r
301     }\r
302 \r
303     public void setLast_mod(Date last_mod) {\r
304         this.last_mod = last_mod;\r
305     }\r
306 \r
307 \r
308     @Override\r
309     public JSONObject asJSONObject() {\r
310         JSONObject jo = new JSONObject();\r
311         jo.put("groupid", groupid);\r
312         jo.put("authid", authid);\r
313         jo.put("name", name);\r
314         jo.put("description", description);\r
315         jo.put("classification", classification);\r
316         jo.put("members", members);\r
317         jo.put("last_mod", last_mod.getTime());\r
318         return jo;\r
319     }\r
320 \r
321     @Override\r
322     public boolean doInsert(Connection c) {\r
323         boolean rv = true;\r
324         PreparedStatement ps = null;\r
325         try {\r
326             if (groupid == -1) {\r
327                 // No feed ID assigned yet, so assign the next available one\r
328                 setGroupid(next_groupid++);\r
329             }\r
330             // In case we insert a gropup from synchronization\r
331             if (groupid > next_groupid)\r
332                 next_groupid = groupid + 1;\r
333 \r
334 \r
335             // Create the GROUPS row\r
336             String sql = "insert into GROUPS (GROUPID, AUTHID, NAME, DESCRIPTION, CLASSIFICATION, MEMBERS) values (?, ?, ?, ?, ?, ?)";\r
337             ps = c.prepareStatement(sql, new String[]{"GROUPID"});\r
338             ps.setInt(1, groupid);\r
339             ps.setString(2, authid);\r
340             ps.setString(3, name);\r
341             ps.setString(4, description);\r
342             ps.setString(5, classification);\r
343             ps.setString(6, members);\r
344             ps.execute();\r
345             ps.close();\r
346         } catch (SQLException e) {\r
347             rv = false;\r
348             intlogger.warn("PROV0005 doInsert: " + e.getMessage());\r
349             intlogger.error("SQLException " + e.getMessage());\r
350         } finally {\r
351             try {\r
352                 if(ps!=null) {\r
353                     ps.close();\r
354                 }\r
355             } catch (SQLException e) {\r
356                 intlogger.error("SQLException " + e.getMessage());\r
357             }\r
358         }\r
359         return rv;\r
360     }\r
361 \r
362     @Override\r
363     public boolean doUpdate(Connection c) {\r
364         boolean rv = true;\r
365         PreparedStatement ps = null;\r
366         try {\r
367             String sql = "update GROUPS set AUTHID = ?, NAME = ?, DESCRIPTION = ?, CLASSIFICATION = ? ,  MEMBERS = ? where GROUPID = ?";\r
368             ps = c.prepareStatement(sql);\r
369             ps.setString(1, authid);\r
370             ps.setString(2, name);\r
371             ps.setString(3, description);\r
372             ps.setString(4, classification);\r
373             ps.setString(5, members);\r
374             ps.setInt(6, groupid);\r
375             ps.executeUpdate();\r
376         } catch (SQLException e) {\r
377             rv = false;\r
378             intlogger.warn("PROV0006 doUpdate: " + e.getMessage());\r
379             intlogger.error("SQLException " + e.getMessage());\r
380         } finally {\r
381             try {\r
382                 if(ps!=null) {\r
383                     ps.close();\r
384                 }\r
385             } catch (SQLException e) {\r
386                 intlogger.error("SQLException " + e.getMessage());\r
387             }\r
388         }\r
389         return rv;\r
390     }\r
391 \r
392     @Override\r
393     public boolean doDelete(Connection c) {\r
394         boolean rv = true;\r
395         PreparedStatement ps = null;\r
396         try {\r
397             String sql = "delete from GROUPS where GROUPID = ?";\r
398             ps = c.prepareStatement(sql);\r
399             ps.setInt(1, groupid);\r
400             ps.execute();\r
401         } catch (SQLException e) {\r
402             rv = false;\r
403             intlogger.warn("PROV0007 doDelete: " + e.getMessage());\r
404             intlogger.error("SQLException " + e.getMessage());\r
405         } finally {\r
406             try {\r
407                 if(ps!=null) {\r
408                     ps.close();\r
409                 }\r
410             } catch (SQLException e) {\r
411                 intlogger.error("SQLException " + e.getMessage());\r
412             }\r
413         }\r
414         return rv;\r
415     }\r
416 \r
417     @Override\r
418     public String getKey() {\r
419         return "" + getGroupid();\r
420     }\r
421 \r
422     @Override\r
423     public boolean equals(Object obj) {\r
424         if (!(obj instanceof Group))\r
425             return false;\r
426         Group os = (Group) obj;\r
427         if (groupid != os.groupid)\r
428             return false;\r
429         if (authid != os.authid)\r
430             return false;\r
431         if (!name.equals(os.name))\r
432             return false;\r
433         if (description != os.description)\r
434             return false;\r
435         if (!classification.equals(os.classification))\r
436             return false;\r
437         if (!members.equals(os.members))\r
438             return false;\r
439 \r
440         return true;\r
441     }\r
442 \r
443     @Override\r
444     public String toString() {\r
445         return "GROUP: groupid=" + groupid;\r
446     }\r
447 \r
448     @Override\r
449     public int hashCode() {\r
450         return Objects.hash(groupid, authid, name, description, classification, members, last_mod);\r
451     }\r
452 }\r