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
36 import java.util.Objects;
\r
37 import java.util.Properties;
\r
38 import org.apache.log4j.Logger;
\r
39 import org.json.JSONObject;
\r
40 import org.onap.dmaap.datarouter.provisioning.utils.DB;
\r
41 import org.onap.dmaap.datarouter.provisioning.utils.URLUtilities;
\r
44 * 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
51 private static final String SQLEXCEPTION = "SQLException: ";
\r
52 private static final String SUBID_KEY = "subid";
\r
53 private static final String SUBID_COL = "SUBID";
\r
54 private static final String FEEDID_KEY = "feedid";
\r
55 private static final String GROUPID_KEY = "groupid";
\r
56 private static final String LAST_MOD_KEY = "last_mod";
\r
57 private static final String CREATED_DATE = "created_date";
\r
58 private static Logger intlogger = Logger.getLogger("org.onap.dmaap.datarouter.provisioning.internal");
\r
59 private static int nextSubid = getMaxSubID() + 1;
\r
63 private int groupid; //New field is added - Groups feature Rally:US708115 - 1610
\r
64 private SubDelivery delivery;
\r
65 private boolean metadataOnly;
\r
66 private String subscriber;
\r
67 private SubLinks links;
\r
68 private boolean suspended;
\r
69 private Date lastMod;
\r
70 private Date createdDate;
\r
71 private boolean privilegedSubscriber;
\r
72 private boolean decompress;
\r
74 public static Subscription getSubscriptionMatching(Subscription sub) {
\r
75 SubDelivery deli = sub.getDelivery();
\r
76 String sql = String.format(
\r
77 "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
82 deli.isUse100() ? 1 : 0,
\r
83 sub.isMetadataOnly() ? 1 : 0
\r
85 List<Subscription> list = getSubscriptionsForSQL(sql);
\r
86 return !list.isEmpty() ? list.get(0) : null;
\r
89 public static Subscription getSubscriptionById(int id) {
\r
90 String sql = "select * from SUBSCRIPTIONS where SUBID = " + id;
\r
91 List<Subscription> list = getSubscriptionsForSQL(sql);
\r
92 return !list.isEmpty() ? list.get(0) : null;
\r
95 public static Collection<Subscription> getAllSubscriptions() {
\r
96 return getSubscriptionsForSQL("select * from SUBSCRIPTIONS");
\r
99 private static List<Subscription> getSubscriptionsForSQL(String sql) {
\r
100 List<Subscription> list = new ArrayList<>();
\r
103 @SuppressWarnings("resource")
\r
104 Connection conn = db.getConnection();
\r
105 try (Statement stmt = conn.createStatement()) {
\r
106 try (ResultSet rs = stmt.executeQuery(sql)) {
\r
107 while (rs.next()) {
\r
108 Subscription sub = new Subscription(rs);
\r
114 } catch (SQLException e) {
\r
115 intlogger.error(e);
\r
120 public static int getMaxSubID() {
\r
124 @SuppressWarnings("resource")
\r
125 Connection conn = db.getConnection();
\r
126 try (Statement stmt = conn.createStatement()) {
\r
127 try (ResultSet rs = stmt.executeQuery("select MAX(subid) from SUBSCRIPTIONS")) {
\r
129 max = rs.getInt(1);
\r
134 } catch (SQLException e) {
\r
135 intlogger.info("getMaxSubID: " + e.getMessage());
\r
140 public static Collection<String> getSubscriptionUrlList(int feedid) {
\r
141 List<String> list = new ArrayList<>();
\r
142 String sql = "select SUBID from SUBSCRIPTIONS where FEEDID = ?";
\r
146 @SuppressWarnings("resource")
\r
147 Connection conn = db.getConnection();
\r
149 try (PreparedStatement stmt = conn.prepareStatement(sql)) {
\r
150 stmt.setString(1, String.valueOf(feedid));
\r
151 try (ResultSet rs = stmt.executeQuery()) {
\r
152 while (rs.next()) {
\r
153 int subid = rs.getInt(SUBID_COL);
\r
154 list.add(URLUtilities.generateSubscriptionURL(subid));
\r
159 } catch (SQLException e) {
\r
160 intlogger.error(SQLEXCEPTION + e.getMessage());
\r
166 * Return a count of the number of active subscriptions in the DB.
\r
168 * @return the count
\r
170 public static int countActiveSubscriptions() {
\r
174 @SuppressWarnings("resource")
\r
175 Connection conn = db.getConnection();
\r
176 try (Statement stmt = conn.createStatement()) {
\r
177 try (ResultSet rs = stmt.executeQuery("select count(*) from SUBSCRIPTIONS")) {
\r
179 count = rs.getInt(1);
\r
184 } catch (SQLException e) {
\r
185 intlogger.warn("PROV0008 countActiveSubscriptions: " + e.getMessage());
\r
190 public Subscription() {
\r
194 public Subscription(String url, String user, String password) {
\r
197 this.groupid = -1; //New field is added - Groups feature Rally:US708115 - 1610
\r
198 this.delivery = new SubDelivery(url, user, password, false);
\r
199 this.metadataOnly = false;
\r
200 this.subscriber = "";
\r
201 this.links = new SubLinks();
\r
202 this.suspended = false;
\r
203 this.lastMod = new Date();
\r
204 this.createdDate = new Date();
\r
205 this.privilegedSubscriber = false;
\r
206 this.decompress = false;
\r
209 public Subscription(ResultSet rs) throws SQLException {
\r
210 this.subid = rs.getInt(SUBID_COL);
\r
211 this.feedid = rs.getInt("FEEDID");
\r
212 this.groupid = rs.getInt("GROUPID"); //New field is added - Groups feature Rally:US708115 - 1610
\r
213 this.delivery = new SubDelivery(rs);
\r
214 this.metadataOnly = rs.getBoolean("METADATA_ONLY");
\r
215 this.subscriber = rs.getString("SUBSCRIBER");
\r
216 this.links = new SubLinks(rs.getString("SELF_LINK"), URLUtilities.generateFeedURL(feedid),
\r
217 rs.getString("LOG_LINK"));
\r
218 this.suspended = rs.getBoolean("SUSPENDED");
\r
219 this.lastMod = rs.getDate("LAST_MOD");
\r
220 this.createdDate = rs.getDate("CREATED_DATE");
\r
221 this.privilegedSubscriber = rs.getBoolean("PRIVILEGED_SUBSCRIBER");
\r
222 this.decompress = rs.getBoolean("DECOMPRESS");
\r
225 public Subscription(JSONObject jo) throws InvalidObjectException {
\r
228 // The JSONObject is assumed to contain a vnd.dmaap-dr.subscription representation
\r
229 this.subid = jo.optInt(SUBID_KEY, -1);
\r
230 this.feedid = jo.optInt(FEEDID_KEY, -1);
\r
231 this.groupid = jo.optInt(GROUPID_KEY, -1); //New field is added - Groups feature Rally:US708115 - 1610
\r
233 JSONObject jdeli = jo.getJSONObject("delivery");
\r
234 String url = jdeli.getString("url");
\r
235 String user = jdeli.getString("user");
\r
236 String password = jdeli.getString("password");
\r
237 boolean use100 = jdeli.getBoolean("use100");
\r
239 //Data Router Subscriber HTTPS Relaxation feature USERSTORYID:US674047.
\r
240 Properties p = (new DB()).getProperties();
\r
241 if (!url.startsWith("https://") && isHttpsRelaxationFalseAndHasSyncKey(jo, p)) {
\r
242 throw new InvalidObjectException("delivery URL is not HTTPS");
\r
245 if (url.length() > 256) {
\r
246 throw new InvalidObjectException("delivery url field is too long");
\r
248 if (user.length() > 20) {
\r
249 throw new InvalidObjectException("delivery user field is too long");
\r
251 if (password.length() > 32) {
\r
252 throw new InvalidObjectException("delivery password field is too long");
\r
254 this.delivery = new SubDelivery(url, user, password, use100);
\r
256 this.metadataOnly = jo.getBoolean("metadataOnly");
\r
257 this.suspended = jo.optBoolean("suspend", false);
\r
258 this.privilegedSubscriber = jo.optBoolean("privilegedSubscriber", false);
\r
259 this.decompress = jo.optBoolean("decompress", false);
\r
260 this.subscriber = jo.optString("subscriber", "");
\r
261 JSONObject jol = jo.optJSONObject("links");
\r
262 this.links = (jol == null) ? (new SubLinks()) : (new SubLinks(jol));
\r
263 } catch (InvalidObjectException e) {
\r
265 } catch (Exception e) {
\r
266 throw new InvalidObjectException("invalid JSON: " + e.getMessage());
\r
270 private boolean isHttpsRelaxationFalseAndHasSyncKey(JSONObject jo, Properties p) {
\r
271 return p.get("org.onap.dmaap.datarouter.provserver.https.relaxation").toString().equals("false") && !jo
\r
275 public int getSubid() {
\r
279 public void setSubid(int subid) {
\r
280 this.subid = subid;
\r
282 // Create link URLs
\r
283 SubLinks sl = getLinks();
\r
284 sl.setSelf(URLUtilities.generateSubscriptionURL(subid));
\r
285 sl.setLog(URLUtilities.generateSubLogURL(subid));
\r
288 public int getFeedid() {
\r
292 public void setFeedid(int feedid) {
\r
293 this.feedid = feedid;
\r
295 // Create link URLs
\r
296 SubLinks sl = getLinks();
\r
297 sl.setFeed(URLUtilities.generateFeedURL(feedid));
\r
300 //New getter setters for Groups feature Rally:US708115 - 1610
\r
301 public int getGroupid() {
\r
305 public void setGroupid(int groupid) {
\r
306 this.groupid = groupid;
\r
309 public SubDelivery getDelivery() {
\r
313 public void setDelivery(SubDelivery delivery) {
\r
314 this.delivery = delivery;
\r
317 public boolean isMetadataOnly() {
\r
318 return metadataOnly;
\r
321 public void setMetadataOnly(boolean metadataOnly) {
\r
322 this.metadataOnly = metadataOnly;
\r
325 public boolean isSuspended() {
\r
329 public void setSuspended(boolean suspended) {
\r
330 this.suspended = suspended;
\r
333 public boolean isPrivilegedSubscriber() {
\r
334 return privilegedSubscriber;
\r
337 public void setPrivilegedSubscriber(boolean privilegedSubscriber) {
\r
338 this.privilegedSubscriber = privilegedSubscriber;
\r
341 public String getSubscriber() {
\r
345 public void setSubscriber(String subscriber) {
\r
346 if (subscriber != null) {
\r
347 if (subscriber.length() > 8) {
\r
348 subscriber = subscriber.substring(0, 8);
\r
350 this.subscriber = subscriber;
\r
354 public SubLinks getLinks() {
\r
358 public void setLinks(SubLinks links) {
\r
359 this.links = links;
\r
362 public boolean isDecompress() {
\r
366 public void setDecompress(boolean decompress) {
\r
367 this.decompress = decompress;
\r
371 public JSONObject asJSONObject() {
\r
372 JSONObject jo = new JSONObject();
\r
373 jo.put(SUBID_KEY, subid);
\r
374 jo.put(FEEDID_KEY, feedid);
\r
375 jo.put(GROUPID_KEY, groupid); //New field is added - Groups feature Rally:US708115 - 1610
\r
376 jo.put("delivery", delivery.asJSONObject());
\r
377 jo.put("metadataOnly", metadataOnly);
\r
378 jo.put("subscriber", subscriber);
\r
379 jo.put("links", links.asJSONObject());
\r
380 jo.put("suspend", suspended);
\r
381 jo.put(LAST_MOD_KEY, lastMod.getTime());
\r
382 jo.put(CREATED_DATE, createdDate.getTime());
\r
383 jo.put("privilegedSubscriber", privilegedSubscriber);
\r
384 jo.put("decompress", decompress);
\r
388 public JSONObject asLimitedJSONObject() {
\r
389 JSONObject jo = asJSONObject();
\r
390 jo.remove(SUBID_KEY);
\r
391 jo.remove(FEEDID_KEY);
\r
392 jo.remove(LAST_MOD_KEY);
\r
396 public JSONObject asJSONObject(boolean hidepasswords) {
\r
397 JSONObject jo = asJSONObject();
\r
398 if (hidepasswords) {
\r
399 jo.remove(SUBID_KEY); // we no longer hide passwords, however we do hide these
\r
400 jo.remove(FEEDID_KEY);
\r
401 jo.remove(LAST_MOD_KEY);
\r
402 jo.remove(CREATED_DATE);
\r
408 public boolean doInsert(Connection c) {
\r
410 PreparedStatement ps = null;
\r
413 // No feed ID assigned yet, so assign the next available one
\r
414 setSubid(nextSubid++);
\r
416 // In case we insert a feed from synchronization
\r
417 if (subid > nextSubid) {
\r
418 nextSubid = subid + 1;
\r
421 // Create the SUBSCRIPTIONS row
\r
422 String sql = "insert into SUBSCRIPTIONS (SUBID, FEEDID, DELIVERY_URL, DELIVERY_USER, DELIVERY_PASSWORD, DELIVERY_USE100, METADATA_ONLY, SUBSCRIBER, SUSPENDED, GROUPID, PRIVILEGED_SUBSCRIBER, DECOMPRESS) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
\r
423 ps = c.prepareStatement(sql, new String[]{SUBID_COL});
\r
424 ps.setInt(1, subid);
\r
425 ps.setInt(2, feedid);
\r
426 ps.setString(3, getDelivery().getUrl());
\r
427 ps.setString(4, getDelivery().getUser());
\r
428 ps.setString(5, getDelivery().getPassword());
\r
429 ps.setInt(6, getDelivery().isUse100() ? 1 : 0);
\r
430 ps.setInt(7, isMetadataOnly() ? 1 : 0);
\r
431 ps.setString(8, getSubscriber());
\r
432 ps.setBoolean(9, isSuspended());
\r
433 ps.setInt(10, groupid); //New field is added - Groups feature Rally:US708115 - 1610
\r
434 ps.setBoolean(11, isPrivilegedSubscriber());
\r
435 ps.setBoolean(12, isDecompress());
\r
438 // Update the row to set the URLs
\r
439 sql = "update SUBSCRIPTIONS set SELF_LINK = ?, LOG_LINK = ? where SUBID = ?";
\r
440 ps = c.prepareStatement(sql);
\r
441 ps.setString(1, getLinks().getSelf());
\r
442 ps.setString(2, getLinks().getLog());
\r
443 ps.setInt(3, subid);
\r
446 } catch (SQLException e) {
\r
448 intlogger.warn("PROV0005 doInsert: " + e.getMessage());
\r
454 } catch (SQLException e) {
\r
455 intlogger.error(SQLEXCEPTION + e.getMessage());
\r
462 public boolean doUpdate(Connection c) {
\r
464 PreparedStatement ps = null;
\r
466 String sql = "update SUBSCRIPTIONS set DELIVERY_URL = ?, DELIVERY_USER = ?, DELIVERY_PASSWORD = ?, DELIVERY_USE100 = ?, METADATA_ONLY = ?, SUSPENDED = ?, GROUPID = ?, PRIVILEGED_SUBSCRIBER = ?, DECOMPRESS = ? where SUBID = ?";
\r
467 ps = c.prepareStatement(sql);
\r
468 ps.setString(1, delivery.getUrl());
\r
469 ps.setString(2, delivery.getUser());
\r
470 ps.setString(3, delivery.getPassword());
\r
471 ps.setInt(4, delivery.isUse100() ? 1 : 0);
\r
472 ps.setInt(5, isMetadataOnly() ? 1 : 0);
\r
473 ps.setInt(6, suspended ? 1 : 0);
\r
474 ps.setInt(7, groupid); //New field is added - Groups feature Rally:US708115 - 1610
\r
475 ps.setInt(8, privilegedSubscriber ? 1 : 0);
\r
476 ps.setInt(9, decompress ? 1 : 0);
\r
477 ps.setInt(10, subid);
\r
478 ps.executeUpdate();
\r
479 } catch (SQLException e) {
\r
481 intlogger.warn("PROV0006 doUpdate: " + e.getMessage());
\r
487 } catch (SQLException e) {
\r
488 intlogger.error(SQLEXCEPTION + e.getMessage());
\r
496 * Rally US708115 Change Ownership of Subscription - 1610
\r
498 public boolean changeOwnerShip() {
\r
500 PreparedStatement ps = null;
\r
504 @SuppressWarnings("resource")
\r
505 Connection c = db.getConnection();
\r
506 String sql = "update SUBSCRIPTIONS set SUBSCRIBER = ? where SUBID = ?";
\r
507 ps = c.prepareStatement(sql);
\r
508 ps.setString(1, this.subscriber);
\r
509 ps.setInt(2, subid);
\r
512 } catch (SQLException e) {
\r
514 intlogger.warn("PROV0006 doUpdate: " + e.getMessage());
\r
520 } catch (SQLException e) {
\r
521 intlogger.error(SQLEXCEPTION + e.getMessage());
\r
529 public boolean doDelete(Connection c) {
\r
531 PreparedStatement ps = null;
\r
533 String sql = "delete from SUBSCRIPTIONS where SUBID = ?";
\r
534 ps = c.prepareStatement(sql);
\r
535 ps.setInt(1, subid);
\r
537 } catch (SQLException e) {
\r
539 intlogger.warn("PROV0007 doDelete: " + e.getMessage());
\r
545 } catch (SQLException e) {
\r
546 intlogger.error(SQLEXCEPTION + e.getMessage());
\r
553 public String getKey() {
\r
554 return "" + getSubid();
\r
558 public boolean equals(Object obj) {
\r
559 if (!(obj instanceof Subscription)) {
\r
562 Subscription os = (Subscription) obj;
\r
563 if (subid != os.subid) {
\r
566 if (feedid != os.feedid) {
\r
569 if (groupid != os.groupid) //New field is added - Groups feature Rally:US708115 - 1610
\r
573 if (!delivery.equals(os.delivery)) {
\r
576 if (metadataOnly != os.metadataOnly) {
\r
579 if (!subscriber.equals(os.subscriber)) {
\r
582 if (!links.equals(os.links)) {
\r
585 return suspended == os.suspended;
\r
589 public int hashCode() {
\r
590 return Objects.hash(subid, feedid, groupid, delivery, metadataOnly, subscriber, links, suspended, lastMod,
\r
595 public String toString() {
\r
596 return "SUB: subid=" + subid + ", feedid=" + feedid;
\r