1 /*******************************************************************************
\r
2 * ============LICENSE_START==================================================
\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
11 * * http://www.apache.org/licenses/LICENSE-2.0
\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
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
24 package org.onap.dmaap.datarouter.provisioning.beans;
\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.ArrayList;
\r
33 import java.util.Collection;
\r
34 import java.util.Date;
\r
35 import java.util.List;
\r
37 import org.apache.log4j.Logger;
\r
38 import org.json.JSONObject;
\r
39 import org.onap.dmaap.datarouter.provisioning.utils.DB;
\r
40 import org.onap.dmaap.datarouter.provisioning.utils.URLUtilities;
\r
42 import java.util.Properties;
\r
45 * The representation of a Subscription. Subscriptions can be retrieved from the DB, or stored/updated in the DB.
\r
46 * @author Robert Eby
\r
47 * @version $Id: Subscription.java,v 1.9 2013/10/28 18:06:53 eby Exp $
\r
49 public class Subscription extends Syncable {
\r
50 private static Logger intlogger = Logger.getLogger("org.onap.dmaap.datarouter.provisioning.internal");
\r
51 private static int next_subid = getMaxSubID() + 1;
\r
55 private int groupid; //New field is added - Groups feature Rally:US708115 - 1610
\r
56 private SubDelivery delivery;
\r
57 private boolean metadataOnly;
\r
58 private String subscriber;
\r
59 private SubLinks links;
\r
60 private boolean suspended;
\r
61 private Date last_mod;
\r
62 private Date created_date;
\r
64 public static Subscription getSubscriptionMatching(Subscription sub) {
\r
65 SubDelivery deli = sub.getDelivery();
\r
66 String sql = String.format(
\r
67 "select * from SUBSCRIPTIONS where FEEDID = %d and DELIVERY_URL = \"%s\" and DELIVERY_USER = \"%s\" and DELIVERY_PASSWORD = \"%s\" and DELIVERY_USE100 = %d and METADATA_ONLY = %d",
\r
72 deli.isUse100() ? 1 : 0,
\r
73 sub.isMetadataOnly() ? 1 : 0
\r
75 List<Subscription> list = getSubscriptionsForSQL(sql);
\r
76 return list.size() > 0 ? list.get(0) : null;
\r
78 public static Subscription getSubscriptionById(int id) {
\r
79 String sql = "select * from SUBSCRIPTIONS where SUBID = " + id;
\r
80 List<Subscription> list = getSubscriptionsForSQL(sql);
\r
81 return list.size() > 0 ? list.get(0) : null;
\r
83 public static Collection<Subscription> getAllSubscriptions() {
\r
84 return getSubscriptionsForSQL("select * from SUBSCRIPTIONS");
\r
86 private static List<Subscription> getSubscriptionsForSQL(String sql) {
\r
87 List<Subscription> list = new ArrayList<Subscription>();
\r
90 @SuppressWarnings("resource")
\r
91 Connection conn = db.getConnection();
\r
92 Statement stmt = conn.createStatement();
\r
93 ResultSet rs = stmt.executeQuery(sql);
\r
95 Subscription sub = new Subscription(rs);
\r
101 } catch (SQLException e) {
\r
102 e.printStackTrace();
\r
106 public static int getMaxSubID() {
\r
110 @SuppressWarnings("resource")
\r
111 Connection conn = db.getConnection();
\r
112 Statement stmt = conn.createStatement();
\r
113 ResultSet rs = stmt.executeQuery("select MAX(subid) from SUBSCRIPTIONS");
\r
115 max = rs.getInt(1);
\r
120 } catch (SQLException e) {
\r
121 intlogger.info("getMaxSubID: "+e.getMessage());
\r
122 e.printStackTrace();
\r
126 public static Collection<String> getSubscriptionUrlList(int feedid) {
\r
127 List<String> list = new ArrayList<String>();
\r
128 String sql = "select SUBID from SUBSCRIPTIONS where FEEDID = "+feedid;
\r
131 @SuppressWarnings("resource")
\r
132 Connection conn = db.getConnection();
\r
133 Statement stmt = conn.createStatement();
\r
134 ResultSet rs = stmt.executeQuery(sql);
\r
135 while (rs.next()) {
\r
136 int subid = rs.getInt("SUBID");
\r
137 list.add(URLUtilities.generateSubscriptionURL(subid));
\r
142 } catch (SQLException e) {
\r
143 e.printStackTrace();
\r
148 * Return a count of the number of active subscriptions in the DB.
\r
149 * @return the count
\r
151 public static int countActiveSubscriptions() {
\r
155 @SuppressWarnings("resource")
\r
156 Connection conn = db.getConnection();
\r
157 Statement stmt = conn.createStatement();
\r
158 ResultSet rs = stmt.executeQuery("select count(*) from SUBSCRIPTIONS");
\r
160 count = rs.getInt(1);
\r
165 } catch (SQLException e) {
\r
166 intlogger.warn("PROV0008 countActiveSubscriptions: "+e.getMessage());
\r
167 e.printStackTrace();
\r
172 public Subscription() {
\r
175 public Subscription(String url, String user, String password) {
\r
178 this.groupid = -1; //New field is added - Groups feature Rally:US708115 - 1610
\r
179 this.delivery = new SubDelivery(url, user, password, false);
\r
180 this.metadataOnly = false;
\r
181 this.subscriber = "";
\r
182 this.links = new SubLinks();
\r
183 this.suspended = false;
\r
184 this.last_mod = new Date();
\r
185 this.created_date = new Date();
\r
187 public Subscription(ResultSet rs) throws SQLException {
\r
188 this.subid = rs.getInt("SUBID");
\r
189 this.feedid = rs.getInt("FEEDID");
\r
190 this.groupid = rs.getInt("GROUPID"); //New field is added - Groups feature Rally:US708115 - 1610
\r
191 this.delivery = new SubDelivery(rs);
\r
192 this.metadataOnly = rs.getBoolean("METADATA_ONLY");
\r
193 this.subscriber = rs.getString("SUBSCRIBER");
\r
194 this.links = new SubLinks(rs.getString("SELF_LINK"), URLUtilities.generateFeedURL(feedid), rs.getString("LOG_LINK"));
\r
195 this.suspended = rs.getBoolean("SUSPENDED");
\r
196 this.last_mod = rs.getDate("LAST_MOD");
\r
197 this.created_date = rs.getDate("CREATED_DATE");
\r
199 public Subscription(JSONObject jo) throws InvalidObjectException {
\r
202 // The JSONObject is assumed to contain a vnd.att-dr.subscription representation
\r
203 this.subid = jo.optInt("subid", -1);
\r
204 this.feedid = jo.optInt("feedid", -1);
\r
205 this.groupid = jo.optInt("groupid", -1); //New field is added - Groups feature Rally:US708115 - 1610
\r
207 JSONObject jdeli = jo.getJSONObject("delivery");
\r
208 String url = jdeli.getString("url");
\r
209 String user = jdeli.getString("user");
\r
210 String password = jdeli.getString("password");
\r
211 boolean use100 = jdeli.getBoolean("use100");
\r
214 //Data Router Subscriber HTTPS Relaxation feature USERSTORYID:US674047.
\r
215 Properties p = (new DB()).getProperties();
\r
216 if(p.get("com.att.research.datarouter.provserver.https.relaxation").toString().equals("false") && !jo.has("sync")) {
\r
217 if (!url.startsWith("https://"))
\r
218 throw new InvalidObjectException("delivery URL is not HTTPS");
\r
221 if (url.length() > 256)
\r
222 throw new InvalidObjectException("delivery url field is too long");
\r
223 if (user.length() > 20)
\r
224 throw new InvalidObjectException("delivery user field is too long");
\r
225 if (password.length() > 32)
\r
226 throw new InvalidObjectException("delivery password field is too long");
\r
227 this.delivery = new SubDelivery(url, user, password, use100);
\r
229 this.metadataOnly = jo.getBoolean("metadataOnly");
\r
230 this.suspended = jo.optBoolean("suspend", false);
\r
232 this.subscriber = jo.optString("subscriber", "");
\r
233 JSONObject jol = jo.optJSONObject("links");
\r
234 this.links = (jol == null) ? (new SubLinks()) : (new SubLinks(jol));
\r
235 } catch (InvalidObjectException e) {
\r
237 } catch (Exception e) {
\r
238 throw new InvalidObjectException("invalid JSON: "+e.getMessage());
\r
241 public int getSubid() {
\r
244 public void setSubid(int subid) {
\r
245 this.subid = subid;
\r
247 // Create link URLs
\r
248 SubLinks sl = getLinks();
\r
249 sl.setSelf(URLUtilities.generateSubscriptionURL(subid));
\r
250 sl.setLog(URLUtilities.generateSubLogURL(subid));
\r
252 public int getFeedid() {
\r
255 public void setFeedid(int feedid) {
\r
256 this.feedid = feedid;
\r
258 // Create link URLs
\r
259 SubLinks sl = getLinks();
\r
260 sl.setFeed(URLUtilities.generateFeedURL(feedid));
\r
263 //New getter setters for Groups feature Rally:US708115 - 1610
\r
264 public int getGroupid() {
\r
267 public void setGroupid(int groupid) {
\r
268 this.groupid = groupid;
\r
271 public SubDelivery getDelivery() {
\r
274 public void setDelivery(SubDelivery delivery) {
\r
275 this.delivery = delivery;
\r
277 public boolean isMetadataOnly() {
\r
278 return metadataOnly;
\r
280 public void setMetadataOnly(boolean metadataOnly) {
\r
281 this.metadataOnly = metadataOnly;
\r
283 public boolean isSuspended() {
\r
286 public void setSuspended(boolean suspended) {
\r
287 this.suspended = suspended;
\r
289 public String getSubscriber() {
\r
292 public void setSubscriber(String subscriber) {
\r
293 if (subscriber != null) {
\r
294 if (subscriber.length() > 8)
\r
295 subscriber = subscriber.substring(0, 8);
\r
296 this.subscriber = subscriber;
\r
299 public SubLinks getLinks() {
\r
302 public void setLinks(SubLinks links) {
\r
303 this.links = links;
\r
307 public JSONObject asJSONObject() {
\r
308 JSONObject jo = new JSONObject();
\r
309 jo.put("subid", subid);
\r
310 jo.put("feedid", feedid);
\r
311 jo.put("groupid", groupid); //New field is added - Groups feature Rally:US708115 - 1610
\r
312 jo.put("delivery", delivery.asJSONObject());
\r
313 jo.put("metadataOnly", metadataOnly);
\r
314 jo.put("subscriber", subscriber);
\r
315 jo.put("links", links.asJSONObject());
\r
316 jo.put("suspend", suspended);
\r
317 jo.put("last_mod", last_mod.getTime());
\r
318 jo.put("created_date", created_date.getTime());
\r
321 public JSONObject asLimitedJSONObject() {
\r
322 JSONObject jo = asJSONObject();
\r
323 jo.remove("subid");
\r
324 jo.remove("feedid");
\r
325 jo.remove("last_mod");
\r
328 public JSONObject asJSONObject(boolean hidepasswords) {
\r
329 JSONObject jo = asJSONObject();
\r
330 if (hidepasswords) {
\r
331 jo.remove("subid"); // we no longer hide passwords, however we do hide these
\r
332 jo.remove("feedid");
\r
333 jo.remove("last_mod");
\r
334 jo.remove("created_date");
\r
339 public boolean doInsert(Connection c) {
\r
341 PreparedStatement ps = null;
\r
344 // No feed ID assigned yet, so assign the next available one
\r
345 setSubid(next_subid++);
\r
347 // In case we insert a feed from synchronization
\r
348 if (subid > next_subid)
\r
349 next_subid = subid+1;
\r
351 // Create the SUBSCRIPTIONS row
\r
352 String sql = "insert into SUBSCRIPTIONS (SUBID, FEEDID, DELIVERY_URL, DELIVERY_USER, DELIVERY_PASSWORD, DELIVERY_USE100, METADATA_ONLY, SUBSCRIBER, SUSPENDED, GROUPID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
\r
353 ps = c.prepareStatement(sql, new String[] { "SUBID" });
\r
354 ps.setInt(1, subid);
\r
355 ps.setInt(2, feedid);
\r
356 ps.setString(3, getDelivery().getUrl());
\r
357 ps.setString(4, getDelivery().getUser());
\r
358 ps.setString(5, getDelivery().getPassword());
\r
359 ps.setInt(6, getDelivery().isUse100()?1:0);
\r
360 ps.setInt(7, isMetadataOnly()?1:0);
\r
361 ps.setString(8, getSubscriber());
\r
362 ps.setBoolean(9, isSuspended());
\r
363 ps.setInt(10, groupid); //New field is added - Groups feature Rally:US708115 - 1610
\r
366 // ResultSet rs = ps.getGeneratedKeys();
\r
368 // setSubid(rs.getInt(1)); // side effect - sets the link URLs
\r
371 // Update the row to set the URLs
\r
372 sql = "update SUBSCRIPTIONS set SELF_LINK = ?, LOG_LINK = ? where SUBID = ?";
\r
373 ps = c.prepareStatement(sql);
\r
374 ps.setString(1, getLinks().getSelf());
\r
375 ps.setString(2, getLinks().getLog());
\r
376 ps.setInt(3, subid);
\r
379 } catch (SQLException e) {
\r
381 intlogger.warn("PROV0005 doInsert: "+e.getMessage());
\r
382 e.printStackTrace();
\r
386 } catch (SQLException e) {
\r
387 e.printStackTrace();
\r
393 public boolean doUpdate(Connection c) {
\r
395 PreparedStatement ps = null;
\r
397 String sql = "update SUBSCRIPTIONS set DELIVERY_URL = ?, DELIVERY_USER = ?, DELIVERY_PASSWORD = ?, DELIVERY_USE100 = ?, METADATA_ONLY = ?, SUSPENDED = ?, GROUPID = ? where SUBID = ?";
\r
398 ps = c.prepareStatement(sql);
\r
399 ps.setString(1, delivery.getUrl());
\r
400 ps.setString(2, delivery.getUser());
\r
401 ps.setString(3, delivery.getPassword());
\r
402 ps.setInt(4, delivery.isUse100()?1:0);
\r
403 ps.setInt(5, isMetadataOnly()?1:0);
\r
404 ps.setInt(6, suspended ? 1 : 0);
\r
405 ps.setInt(7, groupid); //New field is added - Groups feature Rally:US708115 - 1610
\r
406 ps.setInt(8, subid);
\r
407 ps.executeUpdate();
\r
408 } catch (SQLException e) {
\r
410 intlogger.warn("PROV0006 doUpdate: "+e.getMessage());
\r
411 e.printStackTrace();
\r
415 } catch (SQLException e) {
\r
416 e.printStackTrace();
\r
425 * Change Ownership of Subscription - 1610
\r
427 public boolean changeOwnerShip() {
\r
429 PreparedStatement ps = null;
\r
433 @SuppressWarnings("resource")
\r
434 Connection c = db.getConnection();
\r
435 String sql = "update SUBSCRIPTIONS set SUBSCRIBER = ? where SUBID = ?";
\r
436 ps = c.prepareStatement(sql);
\r
437 ps.setString(1, this.subscriber);
\r
438 ps.setInt(2, subid);
\r
441 } catch (SQLException e) {
\r
443 intlogger.warn("PROV0006 doUpdate: "+e.getMessage());
\r
444 e.printStackTrace();
\r
448 } catch (SQLException e) {
\r
449 e.printStackTrace();
\r
458 public boolean doDelete(Connection c) {
\r
460 PreparedStatement ps = null;
\r
462 String sql = "delete from SUBSCRIPTIONS where SUBID = ?";
\r
463 ps = c.prepareStatement(sql);
\r
464 ps.setInt(1, subid);
\r
466 } catch (SQLException e) {
\r
468 intlogger.warn("PROV0007 doDelete: "+e.getMessage());
\r
469 e.printStackTrace();
\r
473 } catch (SQLException e) {
\r
474 e.printStackTrace();
\r
480 public String getKey() {
\r
481 return ""+getSubid();
\r
484 public boolean equals(Object obj) {
\r
485 if (!(obj instanceof Subscription))
\r
487 Subscription os = (Subscription) obj;
\r
488 if (subid != os.subid)
\r
490 if (feedid != os.feedid)
\r
492 if (groupid != os.groupid) //New field is added - Groups feature Rally:US708115 - 1610
\r
494 if (!delivery.equals(os.delivery))
\r
496 if (metadataOnly != os.metadataOnly)
\r
498 if (!subscriber.equals(os.subscriber))
\r
500 if (!links.equals(os.links))
\r
502 if (suspended != os.suspended)
\r
508 public String toString() {
\r
509 return "SUB: subid=" + subid + ", feedid=" + feedid;
\r