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 com.att.eelf.configuration.EELFLogger;
\r
27 import com.att.eelf.configuration.EELFManager;
\r
28 import java.io.InvalidObjectException;
\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.util.ArrayList;
\r
34 import java.util.Collection;
\r
35 import java.util.Date;
\r
36 import java.util.List;
\r
37 import java.util.Properties;
\r
38 import org.json.JSONObject;
\r
39 import org.onap.dmaap.datarouter.provisioning.ProvRunner;
\r
40 import org.onap.dmaap.datarouter.provisioning.utils.ProvDbUtils;
\r
41 import org.onap.dmaap.datarouter.provisioning.utils.URLUtilities;
\r
45 * The representation of a Subscription. Subscriptions can be retrieved from the DB, or stored/updated in the DB.
\r
47 * @author Robert Eby
\r
48 * @version $Id: Subscription.java,v 1.9 2013/10/28 18:06:53 eby Exp $
\r
51 public class Subscription extends Syncable {
\r
53 private static final String SQLEXCEPTION = "SQLException: ";
\r
54 private static final String SUBID_KEY = "subid";
\r
55 private static final String SUBID_COL = "SUBID";
\r
56 private static final String FEEDID_KEY = "feedid";
\r
57 private static final String GROUPID_KEY = "groupid";
\r
58 private static final String LAST_MOD_KEY = "last_mod";
\r
59 private static final String CREATED_DATE = "created_date";
\r
60 private static EELFLogger intlogger = EELFManager.getInstance().getLogger("InternalLog");
\r
61 private static int nextSubid = getMaxSubID() + 1;
\r
65 private int groupid; //New field is added - Groups feature Rally:US708115 - 1610
\r
66 private SubDelivery delivery;
\r
67 private boolean followRedirect;
\r
68 private boolean metadataOnly;
\r
69 private String subscriber;
\r
70 private SubLinks links;
\r
71 private boolean suspended;
\r
72 private Date lastMod;
\r
73 private Date createdDate;
\r
74 private boolean privilegedSubscriber;
\r
75 private boolean decompress;
\r
77 public Subscription() {
\r
82 * Subscription constructor.
\r
83 * @param url url string
\r
84 * @param user user string
\r
85 * @param password password string
\r
87 public Subscription(String url, String user, String password) {
\r
90 this.groupid = -1; //New field is added - Groups feature Rally:US708115 - 1610
\r
91 this.delivery = new SubDelivery(url, user, password, false);
\r
92 this.metadataOnly = false;
\r
93 this.followRedirect = false;
\r
94 this.subscriber = "";
\r
95 this.links = new SubLinks();
\r
96 this.suspended = false;
\r
97 this.lastMod = new Date();
\r
98 this.createdDate = new Date();
\r
99 this.privilegedSubscriber = false;
\r
100 this.decompress = false;
\r
104 * Subscription constructor.
\r
105 * @param rs resultset from SQL
\r
106 * @throws SQLException in case of SQL error
\r
108 public Subscription(ResultSet rs) throws SQLException {
\r
109 this.subid = rs.getInt(SUBID_COL);
\r
110 this.feedid = rs.getInt("FEEDID");
\r
111 this.groupid = rs.getInt("GROUPID"); //New field is added - Groups feature Rally:US708115 - 1610
\r
112 this.delivery = new SubDelivery(rs);
\r
113 this.metadataOnly = rs.getBoolean("METADATA_ONLY");
\r
114 this.followRedirect = rs.getBoolean("FOLLOW_REDIRECTS");
\r
115 this.subscriber = rs.getString("SUBSCRIBER");
\r
116 this.links = new SubLinks(rs.getString("SELF_LINK"), URLUtilities.generateFeedURL(feedid),
\r
117 rs.getString("LOG_LINK"));
\r
118 this.suspended = rs.getBoolean("SUSPENDED");
\r
119 this.lastMod = rs.getDate("LAST_MOD");
\r
120 this.createdDate = rs.getDate("CREATED_DATE");
\r
121 this.privilegedSubscriber = rs.getBoolean("PRIVILEGED_SUBSCRIBER");
\r
122 this.decompress = rs.getBoolean("DECOMPRESS");
\r
126 * Subscription constructor.
\r
127 * @param jo JSONObject
\r
128 * @throws InvalidObjectException in case of object error
\r
130 public Subscription(JSONObject jo) throws InvalidObjectException {
\r
133 // The JSONObject is assumed to contain a vnd.dmaap-dr.subscription representation
\r
134 this.subid = jo.optInt(SUBID_KEY, -1);
\r
135 this.feedid = jo.optInt(FEEDID_KEY, -1);
\r
136 this.groupid = jo.optInt(GROUPID_KEY, -1); //New field is added - Groups feature Rally:US708115 - 1610
\r
137 JSONObject jdeli = jo.getJSONObject("delivery");
\r
138 String url = jdeli.getString("url");
\r
139 String user = jdeli.getString("user");
\r
140 final String password = jdeli.getString("password");
\r
141 final boolean use100 = jdeli.getBoolean("use100");
\r
143 //Data Router Subscriber HTTPS Relaxation feature USERSTORYID:US674047.
\r
144 Properties prop = ProvRunner.getProvProperties();
\r
145 if (!url.startsWith("https://") && isHttpsRelaxationFalseAndHasSyncKey(jo, prop)) {
\r
146 throw new InvalidObjectException("delivery URL is not HTTPS");
\r
149 if (url.length() > 256) {
\r
150 throw new InvalidObjectException("delivery url field is too long");
\r
152 if (user.length() > 60) {
\r
153 throw new InvalidObjectException("delivery user field is too long");
\r
155 if (password.length() > 32) {
\r
156 throw new InvalidObjectException("delivery password field is too long");
\r
158 this.delivery = new SubDelivery(url, user, password, use100);
\r
159 this.metadataOnly = jo.getBoolean("metadataOnly");
\r
160 this.followRedirect = jo.optBoolean("follow_redirect", false);
\r
161 this.suspended = jo.optBoolean("suspend", false);
\r
162 this.privilegedSubscriber = jo.optBoolean("privilegedSubscriber", false);
\r
163 this.decompress = jo.optBoolean("decompress", false);
\r
164 this.subscriber = jo.optString("subscriber", "");
\r
165 JSONObject jol = jo.optJSONObject("links");
\r
166 this.links = (jol == null) ? (new SubLinks()) : (new SubLinks(jol));
\r
167 } catch (InvalidObjectException e) {
\r
169 } catch (Exception e) {
\r
170 intlogger.warn("Invalid JSON: " + e.getMessage(), e);
\r
171 throw new InvalidObjectException("Invalid JSON: " + e.getMessage());
\r
176 * Get specific subscription.
\r
177 * @param sub subscription object
\r
178 * @return subscription
\r
180 public static Subscription getSubscriptionMatching(Subscription sub) {
\r
181 SubDelivery deli = sub.getDelivery();
\r
182 String sql = String.format(
\r
183 "select * from SUBSCRIPTIONS where FEEDID = %d and DELIVERY_URL = \"%s\" and DELIVERY_USER = \"%s\" "
\r
184 + "and DELIVERY_PASSWORD = \"%s\" and DELIVERY_USE100 = %d and METADATA_ONLY = %d "
\r
185 + "and FOLLOW_REDIRECTS = %d",
\r
189 deli.getPassword(),
\r
190 deli.isUse100() ? 1 : 0,
\r
191 sub.isMetadataOnly() ? 1 : 0,
\r
192 sub.isFollowRedirect() ? 1 : 0
\r
194 List<Subscription> list = getSubscriptionsForSQL(sql);
\r
195 return !list.isEmpty() ? list.get(0) : null;
\r
199 * Get subscription by id.
\r
200 * @param id subscription id string
\r
201 * @return subscription
\r
203 public static Subscription getSubscriptionById(int id) {
\r
204 String sql = "select * from SUBSCRIPTIONS where SUBID = " + id;
\r
205 List<Subscription> list = getSubscriptionsForSQL(sql);
\r
206 return !list.isEmpty() ? list.get(0) : null;
\r
209 public static Collection<Subscription> getAllSubscriptions() {
\r
210 return getSubscriptionsForSQL("select * from SUBSCRIPTIONS");
\r
214 * Get subscriptions from SQL.
\r
215 * @param sql SQL statement
\r
216 * @return List of subscriptions
\r
218 private static List<Subscription> getSubscriptionsForSQL(String sql) {
\r
219 List<Subscription> list = new ArrayList<>();
\r
220 try (Connection conn = ProvDbUtils.getInstance().getConnection();
\r
221 PreparedStatement ps = conn.prepareStatement(sql);
\r
222 ResultSet rs = ps.executeQuery()) {
\r
223 while (rs.next()) {
\r
224 Subscription sub = new Subscription(rs);
\r
227 } catch (SQLException e) {
\r
228 intlogger.error("PROV0001 getSubscriptionsForSQL: " + e.toString(), e);
\r
235 * @return subid int
\r
237 public static int getMaxSubID() {
\r
239 try (Connection conn = ProvDbUtils.getInstance().getConnection();
\r
240 PreparedStatement ps = conn.prepareStatement("select MAX(subid) from SUBSCRIPTIONS");
\r
241 ResultSet rs = ps.executeQuery()) {
\r
243 max = rs.getInt(1);
\r
245 } catch (SQLException e) {
\r
246 intlogger.info("getMaxSubID: " + e.getMessage(), e);
\r
252 * Get subscription URL list.
\r
253 * @param feedid feedid int
\r
254 * @return collection of subscription URL
\r
256 public static Collection<String> getSubscriptionUrlList(int feedid) {
\r
257 List<String> list = new ArrayList<>();
\r
258 try (Connection conn = ProvDbUtils.getInstance().getConnection();
\r
259 PreparedStatement stmt = conn.prepareStatement("select SUBID from SUBSCRIPTIONS where FEEDID = ?")) {
\r
260 stmt.setString(1, String.valueOf(feedid));
\r
261 try (ResultSet rs = stmt.executeQuery()) {
\r
262 while (rs.next()) {
\r
263 int subid = rs.getInt(SUBID_COL);
\r
264 list.add(URLUtilities.generateSubscriptionURL(subid));
\r
267 } catch (SQLException e) {
\r
268 intlogger.error(SQLEXCEPTION + e.getMessage(), e);
\r
274 * Return a count of the number of active subscriptions in the DB.
\r
276 * @return the count
\r
278 public static int countActiveSubscriptions() {
\r
280 try (Connection conn = ProvDbUtils.getInstance().getConnection();
\r
281 PreparedStatement ps = conn.prepareStatement("select count(*) from SUBSCRIPTIONS");
\r
282 ResultSet rs = ps.executeQuery()) {
\r
284 count = rs.getInt(1);
\r
286 } catch (SQLException e) {
\r
287 intlogger.warn("PROV0008 countActiveSubscriptions: " + e.getMessage(), e);
\r
292 private boolean isHttpsRelaxationFalseAndHasSyncKey(JSONObject jo, Properties prop) {
\r
293 return prop.get("org.onap.dmaap.datarouter.provserver.https.relaxation").toString().equals("false") && !jo
\r
297 public int getSubid() {
\r
303 * @param subid subid string
\r
305 public void setSubid(int subid) {
\r
306 this.subid = subid;
\r
308 // Create link URLs
\r
309 SubLinks sl = getLinks();
\r
310 sl.setSelf(URLUtilities.generateSubscriptionURL(subid));
\r
311 sl.setLog(URLUtilities.generateSubLogURL(subid));
\r
314 public int getFeedid() {
\r
320 * @param feedid feedid string
\r
322 public void setFeedid(int feedid) {
\r
323 this.feedid = feedid;
\r
325 // Create link URLs
\r
326 SubLinks sl = getLinks();
\r
327 sl.setFeed(URLUtilities.generateFeedURL(feedid));
\r
330 //New getter setters for Groups feature Rally:US708115 - 1610
\r
331 public int getGroupid() {
\r
335 public void setGroupid(int groupid) {
\r
336 this.groupid = groupid;
\r
339 public SubDelivery getDelivery() {
\r
343 public void setDelivery(SubDelivery delivery) {
\r
344 this.delivery = delivery;
\r
347 public boolean isMetadataOnly() {
\r
348 return metadataOnly;
\r
351 public void setMetadataOnly(boolean metadataOnly) {
\r
352 this.metadataOnly = metadataOnly;
\r
355 private boolean isFollowRedirect() {
\r
356 return followRedirect;
\r
359 public void setFollowRedirect(boolean followRedirect) {
\r
360 this.followRedirect = followRedirect;
\r
363 boolean isSuspended() {
\r
367 public void setSuspended(boolean suspended) {
\r
368 this.suspended = suspended;
\r
371 public boolean isPrivilegedSubscriber() {
\r
372 return privilegedSubscriber;
\r
375 public void setPrivilegedSubscriber(boolean privilegedSubscriber) {
\r
376 this.privilegedSubscriber = privilegedSubscriber;
\r
379 public String getSubscriber() {
\r
384 * Subscriber setter.
\r
385 * @param subscriber subscriber string
\r
387 public void setSubscriber(String subscriber) {
\r
388 if (subscriber != null) {
\r
389 if (subscriber.length() > 8) {
\r
390 subscriber = subscriber.substring(0, 8);
\r
392 this.subscriber = subscriber;
\r
396 public SubLinks getLinks() {
\r
400 void setLinks(SubLinks links) {
\r
401 this.links = links;
\r
404 public boolean isDecompress() {
\r
408 public void setDecompress(boolean decompress) {
\r
409 this.decompress = decompress;
\r
413 public JSONObject asJSONObject() {
\r
414 JSONObject jo = new JSONObject();
\r
415 jo.put(SUBID_KEY, subid);
\r
416 jo.put(FEEDID_KEY, feedid);
\r
417 jo.put(GROUPID_KEY, groupid); //New field is added - Groups feature Rally:US708115 - 1610
\r
418 jo.put("delivery", delivery.asJSONObject());
\r
419 jo.put("metadataOnly", metadataOnly);
\r
420 jo.put("follow_redirect", followRedirect);
\r
421 jo.put("subscriber", subscriber);
\r
422 jo.put("links", links.asJSONObject());
\r
423 jo.put("suspend", suspended);
\r
424 jo.put(LAST_MOD_KEY, lastMod.getTime());
\r
425 jo.put(CREATED_DATE, createdDate.getTime());
\r
426 jo.put("privilegedSubscriber", privilegedSubscriber);
\r
427 jo.put("decompress", decompress);
\r
432 * Method to hide attributes.
\r
433 * @param hidepasswords true/false
\r
434 * @return JSONObject
\r
436 public JSONObject asJSONObject(boolean hidepasswords) {
\r
437 JSONObject jo = asJSONObject();
\r
438 if (hidepasswords) {
\r
439 jo.remove(SUBID_KEY); // we no longer hide passwords, however we do hide these
\r
440 jo.remove(FEEDID_KEY);
\r
441 jo.remove(LAST_MOD_KEY);
\r
442 jo.remove(CREATED_DATE);
\r
448 * Method to remove some attributes from JSON.
\r
451 public JSONObject asLimitedJSONObject() {
\r
452 JSONObject jo = asJSONObject();
\r
453 jo.remove(SUBID_KEY);
\r
454 jo.remove(FEEDID_KEY);
\r
455 jo.remove(LAST_MOD_KEY);
\r
461 public boolean doInsert(Connection conn) {
\r
463 PreparedStatement ps = null;
\r
466 // No feed ID assigned yet, so assign the next available one
\r
467 setSubid(nextSubid++);
\r
469 // In case we insert a feed from synchronization
\r
470 if (subid > nextSubid) {
\r
471 nextSubid = subid + 1;
\r
474 // Create the SUBSCRIPTIONS row
\r
475 String sql = "insert into SUBSCRIPTIONS (SUBID, FEEDID, DELIVERY_URL, DELIVERY_USER, DELIVERY_PASSWORD, "
\r
476 + "DELIVERY_USE100, METADATA_ONLY, SUBSCRIBER, SUSPENDED, GROUPID, "
\r
477 + "PRIVILEGED_SUBSCRIBER, FOLLOW_REDIRECTS, DECOMPRESS) "
\r
478 + "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
\r
479 ps = conn.prepareStatement(sql, new String[]{SUBID_COL});
\r
480 ps.setInt(1, subid);
\r
481 ps.setInt(2, feedid);
\r
482 ps.setString(3, getDelivery().getUrl());
\r
483 ps.setString(4, getDelivery().getUser());
\r
484 ps.setString(5, getDelivery().getPassword());
\r
485 ps.setInt(6, getDelivery().isUse100() ? 1 : 0);
\r
486 ps.setInt(7, isMetadataOnly() ? 1 : 0);
\r
487 ps.setString(8, getSubscriber());
\r
488 ps.setBoolean(9, isSuspended());
\r
489 ps.setInt(10, groupid); //New field is added - Groups feature Rally:US708115 - 1610
\r
490 ps.setBoolean(11, isPrivilegedSubscriber());
\r
491 ps.setInt(12, isFollowRedirect() ? 1 : 0);
\r
492 ps.setBoolean(13, isDecompress());
\r
495 // Update the row to set the URLs
\r
496 sql = "update SUBSCRIPTIONS set SELF_LINK = ?, LOG_LINK = ? where SUBID = ?";
\r
497 ps = conn.prepareStatement(sql);
\r
498 ps.setString(1, getLinks().getSelf());
\r
499 ps.setString(2, getLinks().getLog());
\r
500 ps.setInt(3, subid);
\r
503 } catch (SQLException e) {
\r
505 intlogger.warn("PROV0005 doInsert: " + e.getMessage(), e);
\r
511 } catch (SQLException e) {
\r
512 intlogger.error(SQLEXCEPTION + e.getMessage(), e);
\r
519 public boolean doUpdate(Connection conn) {
\r
521 try (PreparedStatement ps = conn.prepareStatement(
\r
522 "update SUBSCRIPTIONS set DELIVERY_URL = ?, DELIVERY_USER = ?, DELIVERY_PASSWORD = ?, "
\r
523 + "DELIVERY_USE100 = ?, METADATA_ONLY = ?, SUSPENDED = ?, GROUPID = ?, PRIVILEGED_SUBSCRIBER = ?, "
\r
524 + "FOLLOW_REDIRECTS = ?, DECOMPRESS = ? where SUBID = ?")) {
\r
525 ps.setString(1, delivery.getUrl());
\r
526 ps.setString(2, delivery.getUser());
\r
527 ps.setString(3, delivery.getPassword());
\r
528 ps.setInt(4, delivery.isUse100() ? 1 : 0);
\r
529 ps.setInt(5, isMetadataOnly() ? 1 : 0);
\r
530 ps.setInt(6, suspended ? 1 : 0);
\r
531 ps.setInt(7, groupid); //New field is added - Groups feature Rally:US708115 - 1610
\r
532 ps.setInt(8, privilegedSubscriber ? 1 : 0);
\r
533 ps.setInt(9, isFollowRedirect() ? 1 : 0);
\r
534 ps.setInt(10, isDecompress() ? 1 : 0);
\r
535 ps.setInt(11, subid);
\r
536 ps.executeUpdate();
\r
537 } catch (SQLException e) {
\r
539 intlogger.warn("PROV0006 doUpdate: " + e.getMessage(), e);
\r
546 * Rally US708115 Change Ownership of Subscription - 1610.
\r
548 public boolean changeOwnerShip() {
\r
550 try (Connection conn = ProvDbUtils.getInstance().getConnection();
\r
551 PreparedStatement ps = conn.prepareStatement(
\r
552 "update SUBSCRIPTIONS set SUBSCRIBER = ? where SUBID = ?")) {
\r
553 ps.setString(1, this.subscriber);
\r
554 ps.setInt(2, subid);
\r
556 } catch (SQLException e) {
\r
558 intlogger.warn("PROV0006 doUpdate: " + e.getMessage(), e);
\r
565 public boolean doDelete(Connection conn) {
\r
567 try (PreparedStatement ps = conn.prepareStatement("delete from SUBSCRIPTIONS where SUBID = ?")) {
\r
568 ps.setInt(1, subid);
\r
570 } catch (SQLException e) {
\r
572 intlogger.warn("PROV0007 doDelete: " + e.getMessage(), e);
\r
578 public String getKey() {
\r
579 return "" + getSubid();
\r
583 public boolean equals(Object obj) {
\r
584 if (!(obj instanceof Subscription)) {
\r
587 Subscription os = (Subscription) obj;
\r
588 if (subid != os.subid) {
\r
591 if (feedid != os.feedid) {
\r
594 if (groupid != os.groupid) {
\r
595 //New field is added - Groups feature Rally:US708115 - 1610
\r
598 if (!delivery.equals(os.delivery)) {
\r
601 if (metadataOnly != os.metadataOnly) {
\r
604 if (followRedirect != os.followRedirect) {
\r
607 if (!subscriber.equals(os.subscriber)) {
\r
610 if (!links.equals(os.links)) {
\r
613 if (suspended != os.suspended) {
\r
620 public int hashCode() {
\r
621 return super.hashCode();
\r
625 public String toString() {
\r
626 return "SUB: subid=" + subid + ", feedid=" + feedid;
\r