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 org.apache.log4j.Level;
\r
27 import org.apache.log4j.Logger;
\r
28 import org.json.JSONObject;
\r
29 import org.onap.dmaap.datarouter.provisioning.utils.DB;
\r
30 import org.onap.dmaap.datarouter.provisioning.utils.PasswordProcessor;
\r
31 import org.onap.dmaap.datarouter.provisioning.utils.URLUtilities;
\r
33 import java.io.InvalidObjectException;
\r
34 import java.security.GeneralSecurityException;
\r
36 import java.util.Date;
\r
40 * The representation of a Subscription. Subscriptions can be retrieved from the DB, or stored/updated in the DB.
\r
42 * @author Robert Eby
\r
43 * @version $Id: Subscription.java,v 1.9 2013/10/28 18:06:53 eby Exp $
\r
45 public class Subscription extends Syncable {
\r
47 private static final String SQLEXCEPTION = "SQLException: ";
\r
48 private static final String SUBID_KEY = "subid";
\r
49 private static final String SUBID_COL = "SUBID";
\r
50 private static final String FEEDID_KEY = "feedid";
\r
51 private static final String GROUPID_KEY = "groupid";
\r
52 private static final String LAST_MOD_KEY = "last_mod";
\r
53 private static final String CREATED_DATE = "created_date";
\r
54 private static Logger intlogger = Logger.getLogger("org.onap.dmaap.datarouter.provisioning.internal");
\r
55 private static int nextSubid = getMaxSubID() + 1;
\r
59 private int groupid; //New field is added - Groups feature Rally:US708115 - 1610
\r
60 private SubDelivery delivery;
\r
61 private boolean followRedirect;
\r
62 private boolean metadataOnly;
\r
63 private String subscriber;
\r
64 private SubLinks links;
\r
65 private boolean suspended;
\r
66 private Date lastMod;
\r
67 private Date createdDate;
\r
68 private boolean privilegedSubscriber;
\r
69 private String aafInstance;
\r
70 private boolean decompress;
\r
72 public static Subscription getSubscriptionMatching(Subscription sub) {
\r
73 SubDelivery deli = sub.getDelivery();
\r
74 String sql = String.format(
\r
75 "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 and FOLLOW_REDIRECTS = %d",
\r
80 deli.isUse100() ? 1 : 0,
\r
81 sub.isMetadataOnly() ? 1 : 0,
\r
82 sub.isFollowRedirect() ? 1 :0
\r
84 List<Subscription> list = getSubscriptionsForSQL(sql);
\r
85 return !list.isEmpty() ? list.get(0) : null;
\r
88 public static Subscription getSubscriptionById(int id) {
\r
89 String sql = "select * from SUBSCRIPTIONS where SUBID = " + id;
\r
90 List<Subscription> list = getSubscriptionsForSQL(sql);
\r
91 return !list.isEmpty() ? list.get(0) : null;
\r
94 public static Collection<Subscription> getAllSubscriptions() {
\r
95 return getSubscriptionsForSQL("select * from SUBSCRIPTIONS");
\r
98 private static List<Subscription> getSubscriptionsForSQL(String sql) {
\r
99 List<Subscription> list = new ArrayList<>();
\r
102 @SuppressWarnings("resource")
\r
103 Connection conn = db.getConnection();
\r
104 try (Statement stmt = conn.createStatement()) {
\r
105 try (ResultSet rs = stmt.executeQuery(sql)) {
\r
106 while (rs.next()) {
\r
107 Subscription sub = new Subscription(rs);
\r
113 } catch (SQLException e) {
\r
114 intlogger.error(e);
\r
119 public static int getMaxSubID() {
\r
123 @SuppressWarnings("resource")
\r
124 Connection conn = db.getConnection();
\r
125 try (Statement stmt = conn.createStatement()) {
\r
126 try (ResultSet rs = stmt.executeQuery("select MAX(subid) from SUBSCRIPTIONS")) {
\r
128 max = rs.getInt(1);
\r
133 } catch (SQLException e) {
\r
134 intlogger.info("getMaxSubID: " + e.getMessage());
\r
139 public static Collection<String> getSubscriptionUrlList(int feedid) {
\r
140 List<String> list = new ArrayList<>();
\r
141 String sql = "select SUBID from SUBSCRIPTIONS where FEEDID = ?";
\r
145 @SuppressWarnings("resource")
\r
146 Connection conn = db.getConnection();
\r
147 try (PreparedStatement stmt = conn.prepareStatement(sql)) {
\r
148 stmt.setString(1, String.valueOf(feedid));
\r
149 try (ResultSet rs = stmt.executeQuery()) {
\r
150 while (rs.next()) {
\r
151 int subid = rs.getInt(SUBID_COL);
\r
152 list.add(URLUtilities.generateSubscriptionURL(subid));
\r
157 } catch (SQLException e) {
\r
158 intlogger.error(SQLEXCEPTION + e.getMessage());
\r
164 * Return a count of the number of active subscriptions in the DB.
\r
166 * @return the count
\r
168 public static int countActiveSubscriptions() {
\r
172 @SuppressWarnings("resource")
\r
173 Connection conn = db.getConnection();
\r
174 try (Statement stmt = conn.createStatement()) {
\r
175 try (ResultSet rs = stmt.executeQuery("select count(*) from SUBSCRIPTIONS")) {
\r
177 count = rs.getInt(1);
\r
182 } catch (SQLException e) {
\r
183 intlogger.warn("PROV0008 countActiveSubscriptions: " + e.getMessage());
\r
188 public Subscription() {
\r
192 public Subscription(String url, String user, String password) {
\r
195 this.groupid = -1; //New field is added - Groups feature Rally:US708115 - 1610
\r
196 this.delivery = new SubDelivery(url, user, password, false);
\r
197 this.metadataOnly = false;
\r
198 this.followRedirect = false;
\r
199 this.subscriber = "";
\r
200 this.links = new SubLinks();
\r
201 this.suspended = false;
\r
202 this.lastMod = new Date();
\r
203 this.createdDate = new Date();
\r
204 this.privilegedSubscriber = false;
\r
205 this.aafInstance = "";
\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.followRedirect = rs.getBoolean("FOLLOW_REDIRECTS");
\r
216 this.subscriber = rs.getString("SUBSCRIBER");
\r
217 this.links = new SubLinks(rs.getString("SELF_LINK"), URLUtilities.generateFeedURL(feedid), 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.aafInstance = rs.getString("AAF_INSTANCE");
\r
223 this.decompress = rs.getBoolean("DECOMPRESS");
\r
226 public Subscription(JSONObject jo) throws InvalidObjectException {
\r
229 // The JSONObject is assumed to contain a vnd.dmaap-dr.subscription representation
\r
230 this.subid = jo.optInt(SUBID_KEY, -1);
\r
231 this.feedid = jo.optInt(FEEDID_KEY, -1);
\r
232 this.groupid = jo.optInt(GROUPID_KEY, -1); //New field is added - Groups feature Rally:US708115 - 1610
\r
233 this.aafInstance = jo.optString("aaf_instance", "legacy");
\r
234 if(!(aafInstance.equalsIgnoreCase("legacy"))){
\r
235 if (aafInstance.length() > 255)
\r
236 throw new InvalidObjectException("aaf_instance field is too long");
\r
238 JSONObject jdeli = jo.getJSONObject("delivery");
\r
239 String url = jdeli.getString("url");
\r
240 String user = jdeli.getString("user");
\r
241 String password = jdeli.getString("password");
\r
242 boolean use100 = jdeli.getBoolean("use100");
\r
244 //Data Router Subscriber HTTPS Relaxation feature USERSTORYID:US674047.
\r
245 Properties p = (new DB()).getProperties();
\r
246 if (!url.startsWith("https://") && isHttpsRelaxationFalseAndHasSyncKey(jo, p)) {
\r
247 throw new InvalidObjectException("delivery URL is not HTTPS");
\r
250 if (url.length() > 256) {
\r
251 throw new InvalidObjectException("delivery url field is too long");
\r
253 if (user.length() > 60) {
\r
254 throw new InvalidObjectException("delivery user field is too long");
\r
256 if (password.length() > 32) {
\r
257 throw new InvalidObjectException("delivery password field is too long");
\r
259 this.delivery = new SubDelivery(url, user, password, use100);
\r
260 this.metadataOnly = jo.getBoolean("metadataOnly");
\r
261 this.followRedirect = jo.optBoolean("follow_redirect", false);
\r
262 this.suspended = jo.optBoolean("suspend", false);
\r
263 this.privilegedSubscriber = jo.optBoolean("privilegedSubscriber", false);
\r
264 this.decompress = jo.optBoolean("decompress", false);
\r
265 this.subscriber = jo.optString("subscriber", "");
\r
266 JSONObject jol = jo.optJSONObject("links");
\r
267 this.links = (jol == null) ? (new SubLinks()) : (new SubLinks(jol));
\r
268 } catch (InvalidObjectException e) {
\r
270 } catch (Exception e) {
\r
271 throw new InvalidObjectException("invalid JSON: " + e.getMessage());
\r
275 private boolean isHttpsRelaxationFalseAndHasSyncKey(JSONObject jo, Properties p) {
\r
276 return p.get("org.onap.dmaap.datarouter.provserver.https.relaxation").toString().equals("false") && !jo
\r
280 public int getSubid() {
\r
284 public void setSubid(int subid) {
\r
285 this.subid = subid;
\r
287 // Create link URLs
\r
288 SubLinks sl = getLinks();
\r
289 sl.setSelf(URLUtilities.generateSubscriptionURL(subid));
\r
290 sl.setLog(URLUtilities.generateSubLogURL(subid));
\r
293 public int getFeedid() {
\r
297 public void setFeedid(int feedid) {
\r
298 this.feedid = feedid;
\r
300 // Create link URLs
\r
301 SubLinks sl = getLinks();
\r
302 sl.setFeed(URLUtilities.generateFeedURL(feedid));
\r
304 public String getAafInstance() {
\r
305 return aafInstance;
\r
308 public void setAafInstance(String aafInstance) {
\r
309 this.aafInstance = aafInstance;
\r
312 //New getter setters for Groups feature Rally:US708115 - 1610
\r
313 public int getGroupid() {
\r
317 public void setGroupid(int groupid) {
\r
318 this.groupid = groupid;
\r
321 public SubDelivery getDelivery() {
\r
325 public void setDelivery(SubDelivery delivery) {
\r
326 this.delivery = delivery;
\r
329 public boolean isMetadataOnly() {
\r
330 return metadataOnly;
\r
333 public void setMetadataOnly(boolean metadataOnly) {
\r
334 this.metadataOnly = metadataOnly;
\r
337 private boolean isFollowRedirect() {
\r
338 return followRedirect;
\r
340 public void setFollowRedirect(boolean followRedirect) {
\r
341 this.followRedirect = followRedirect;
\r
344 boolean isSuspended() {
\r
348 public void setSuspended(boolean suspended) {
\r
349 this.suspended = suspended;
\r
352 public boolean isPrivilegedSubscriber() {
\r
353 return privilegedSubscriber;
\r
356 public void setPrivilegedSubscriber(boolean privilegedSubscriber) {
\r
357 this.privilegedSubscriber = privilegedSubscriber;
\r
360 public String getSubscriber() {
\r
364 public void setSubscriber(String subscriber) {
\r
365 if (subscriber != null) {
\r
366 if (subscriber.length() > 8) {
\r
367 subscriber = subscriber.substring(0, 8);
\r
369 this.subscriber = subscriber;
\r
373 public SubLinks getLinks() {
\r
377 void setLinks(SubLinks links) {
\r
378 this.links = links;
\r
381 public boolean isDecompress() {
\r
385 public void setDecompress(boolean decompress) {
\r
386 this.decompress = decompress;
\r
390 public JSONObject asJSONObject() {
\r
391 JSONObject jo = new JSONObject();
\r
392 jo.put(SUBID_KEY, subid);
\r
393 jo.put(FEEDID_KEY, feedid);
\r
394 jo.put(GROUPID_KEY, groupid); //New field is added - Groups feature Rally:US708115 - 1610
\r
395 jo.put("delivery", delivery.asJSONObject());
\r
396 jo.put("metadataOnly", metadataOnly);
\r
397 jo.put("follow_redirect", followRedirect);
\r
398 jo.put("subscriber", subscriber);
\r
399 jo.put("links", links.asJSONObject());
\r
400 jo.put("suspend", suspended);
\r
401 jo.put(LAST_MOD_KEY, lastMod.getTime());
\r
402 jo.put(CREATED_DATE, createdDate.getTime());
\r
403 jo.put("privilegedSubscriber", privilegedSubscriber);
\r
404 jo.put("aaf_instance", aafInstance);
\r
405 jo.put("decompress", decompress);
\r
409 public JSONObject asLimitedJSONObject() {
\r
410 JSONObject jo = asJSONObject();
\r
411 jo.remove(SUBID_KEY);
\r
412 jo.remove(FEEDID_KEY);
\r
413 jo.remove(LAST_MOD_KEY);
\r
417 public JSONObject asJSONObject(boolean hidepasswords) {
\r
418 JSONObject jo = asJSONObject();
\r
419 if (hidepasswords) {
\r
420 jo.remove(SUBID_KEY); // we no longer hide passwords, however we do hide these
\r
421 jo.remove(FEEDID_KEY);
\r
422 jo.remove(LAST_MOD_KEY);
\r
423 jo.remove(CREATED_DATE);
\r
429 public boolean doInsert(Connection c) {
\r
431 PreparedStatement ps = null;
\r
434 // No feed ID assigned yet, so assign the next available one
\r
435 setSubid(nextSubid++);
\r
437 // In case we insert a feed from synchronization
\r
438 if (subid > nextSubid) {
\r
439 nextSubid = subid + 1;
\r
442 // Create the SUBSCRIPTIONS row
\r
443 String sql = "insert into SUBSCRIPTIONS (SUBID, FEEDID, DELIVERY_URL, DELIVERY_USER, DELIVERY_PASSWORD, DELIVERY_USE100, METADATA_ONLY, SUBSCRIBER, SUSPENDED, GROUPID, PRIVILEGED_SUBSCRIBER, FOLLOW_REDIRECTS, DECOMPRESS, AAF_INSTANCE) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
\r
444 ps = c.prepareStatement(sql, new String[]{SUBID_COL});
\r
445 ps.setInt(1, subid);
\r
446 ps.setInt(2, feedid);
\r
447 ps.setString(3, getDelivery().getUrl());
\r
448 ps.setString(4, getDelivery().getUser());
\r
449 ps.setString(5, getDelivery().getPassword());
\r
450 ps.setInt(6, getDelivery().isUse100() ? 1 : 0);
\r
451 ps.setInt(7, isMetadataOnly() ? 1 : 0);
\r
452 ps.setString(8, getSubscriber());
\r
453 ps.setBoolean(9, isSuspended());
\r
454 ps.setInt(10, groupid); //New field is added - Groups feature Rally:US708115 - 1610
\r
455 ps.setBoolean(11, isPrivilegedSubscriber());
\r
456 ps.setInt(12, isFollowRedirect() ? 1 : 0);
\r
457 ps.setBoolean(13, isDecompress());
\r
458 ps.setString(14, getAafInstance());
\r
461 // Update the row to set the URLs
\r
462 sql = "update SUBSCRIPTIONS set SELF_LINK = ?, LOG_LINK = ? where SUBID = ?";
\r
463 ps = c.prepareStatement(sql);
\r
464 ps.setString(1, getLinks().getSelf());
\r
465 ps.setString(2, getLinks().getLog());
\r
466 ps.setInt(3, subid);
\r
469 } catch (SQLException e) {
\r
471 intlogger.warn("PROV0005 doInsert: " + e.getMessage());
\r
472 intlogger.log(Level.WARN, "PROV0005 Subscription.doInsert(1): ", e);
\r
478 } catch (SQLException e) {
\r
479 intlogger.error(SQLEXCEPTION + e.getMessage());
\r
486 public boolean doUpdate(Connection c) {
\r
488 PreparedStatement ps = null;
\r
490 String sql = "update SUBSCRIPTIONS set DELIVERY_URL = ?, DELIVERY_USER = ?, DELIVERY_PASSWORD = ?, DELIVERY_USE100 = ?, METADATA_ONLY = ?, SUSPENDED = ?, GROUPID = ?, PRIVILEGED_SUBSCRIBER = ?, FOLLOW_REDIRECTS = ?, DECOMPRESS = ? where SUBID = ?";
\r
491 ps = c.prepareStatement(sql);
\r
492 ps.setString(1, delivery.getUrl());
\r
493 ps.setString(2, delivery.getUser());
\r
494 ps.setString(3, delivery.getPassword());
\r
495 ps.setInt(4, delivery.isUse100() ? 1 : 0);
\r
496 ps.setInt(5, isMetadataOnly() ? 1 : 0);
\r
497 ps.setInt(6, suspended ? 1 : 0);
\r
498 ps.setInt(7, groupid); //New field is added - Groups feature Rally:US708115 - 1610
\r
499 ps.setInt(8, privilegedSubscriber ? 1 : 0);
\r
500 ps.setInt(9, isFollowRedirect() ? 1 : 0);
\r
501 ps.setInt(10, isDecompress() ? 1 : 0);
\r
502 ps.setInt(11, subid);
\r
503 ps.executeUpdate();
\r
504 } catch (SQLException e) {
\r
506 intlogger.warn("PROV0006 doUpdate: " + e.getMessage());
\r
512 } catch (SQLException e) {
\r
513 intlogger.error(SQLEXCEPTION + e.getMessage());
\r
521 * Rally US708115 Change Ownership of Subscription - 1610
\r
523 public boolean changeOwnerShip() {
\r
525 PreparedStatement ps = null;
\r
529 @SuppressWarnings("resource")
\r
530 Connection c = db.getConnection();
\r
531 String sql = "update SUBSCRIPTIONS set SUBSCRIBER = ? where SUBID = ?";
\r
532 ps = c.prepareStatement(sql);
\r
533 ps.setString(1, this.subscriber);
\r
534 ps.setInt(2, subid);
\r
537 } catch (SQLException e) {
\r
539 intlogger.warn("PROV0006 doUpdate: " + e.getMessage());
\r
545 } catch (SQLException e) {
\r
546 intlogger.error(SQLEXCEPTION + e.getMessage());
\r
554 public boolean doDelete(Connection c) {
\r
556 PreparedStatement ps = null;
\r
558 String sql = "delete from SUBSCRIPTIONS where SUBID = ?";
\r
559 ps = c.prepareStatement(sql);
\r
560 ps.setInt(1, subid);
\r
562 } catch (SQLException e) {
\r
564 intlogger.warn("PROV0007 doDelete: " + e.getMessage());
\r
570 } catch (SQLException e) {
\r
571 intlogger.error(SQLEXCEPTION + e.getMessage());
\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) //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
616 if (!aafInstance.equals(os.aafInstance)) {
\r
623 public int hashCode() {
\r
624 return super.hashCode();
\r
628 public String toString() {
\r
629 return "SUB: subid=" + subid + ", feedid=" + feedid;
\r