X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=datarouter-prov%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fdmaap%2Fdatarouter%2Fprovisioning%2Fbeans%2FFeed.java;h=506821e2d5e38b0ceb9932291442c88e03db673e;hb=b60213dc26540543f500b3442b061565907c3cf8;hp=852321a94b485b0cb3538cfc68708a12c4595539;hpb=8cbe8a88bc6dfe8673a33a017fe6a5a3e7ce86c3;p=dmaap%2Fdatarouter.git diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/Feed.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/Feed.java index 852321a9..506821e2 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/Feed.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/Feed.java @@ -24,27 +24,20 @@ package org.onap.dmaap.datarouter.provisioning.beans; -import java.io.InvalidObjectException; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; - -import org.apache.log4j.Logger; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; import org.json.JSONArray; +import org.json.JSONException; import org.json.JSONObject; import org.onap.dmaap.datarouter.provisioning.utils.DB; import org.onap.dmaap.datarouter.provisioning.utils.JSONUtilities; import org.onap.dmaap.datarouter.provisioning.utils.URLUtilities; +import java.io.InvalidObjectException; +import java.sql.*; +import java.util.Date; +import java.util.*; + /** * The representation of a Feed. Feeds can be retrieved from the DB, or stored/updated in the DB. * @@ -52,7 +45,7 @@ import org.onap.dmaap.datarouter.provisioning.utils.URLUtilities; * @version $Id: Feed.java,v 1.13 2013/10/28 18:06:52 eby Exp $ */ public class Feed extends Syncable { - private static Logger intlogger = Logger.getLogger("org.onap.dmaap.datarouter.provisioning.internal"); + private static EELFLogger intlogger = EELFManager.getInstance().getLogger("InternalLog"); private static int next_feedid = getMaxFeedID() + 1; private int feedid; @@ -68,6 +61,7 @@ public class Feed extends Syncable { private boolean suspended; private Date last_mod; private Date created_date; + private String aaf_instance; /** * Check if a feed ID is valid. @@ -81,16 +75,17 @@ public class Feed extends Syncable { try { DB db = new DB(); Connection conn = db.getConnection(); - Statement stmt = conn.createStatement(); - ResultSet rs = stmt.executeQuery("select COUNT(*) from FEEDS where FEEDID = " + id); - if (rs.next()) { - count = rs.getInt(1); + try(PreparedStatement stmt = conn.prepareStatement("select COUNT(*) from FEEDS where FEEDID = ?")) { + stmt.setInt(1, id); + try(ResultSet rs = stmt.executeQuery()) { + if (rs.next()) { + count = rs.getInt(1); + } + } } - rs.close(); - stmt.close(); db.release(conn); } catch (SQLException e) { - e.printStackTrace(); + intlogger.warn("PROV0024 Feed.isFeedValid: ", e.getMessage()); } return count != 0; } @@ -131,17 +126,16 @@ public class Feed extends Syncable { DB db = new DB(); @SuppressWarnings("resource") Connection conn = db.getConnection(); - Statement stmt = conn.createStatement(); - ResultSet rs = stmt.executeQuery("select count(*) from FEEDS where DELETED = 0"); - if (rs.next()) { - count = rs.getInt(1); + try(Statement stmt = conn.createStatement()) { + try (ResultSet rs = stmt.executeQuery("select count(*) from FEEDS where DELETED = 0")) { + if (rs.next()) { + count = rs.getInt(1); + } + } } - rs.close(); - stmt.close(); db.release(conn); } catch (SQLException e) { - intlogger.info("countActiveFeeds: " + e.getMessage()); - e.printStackTrace(); + intlogger.warn("PROV0025 Feed.countActiveFeeds: ", e.getMessage()); } return count; } @@ -152,17 +146,16 @@ public class Feed extends Syncable { DB db = new DB(); @SuppressWarnings("resource") Connection conn = db.getConnection(); - Statement stmt = conn.createStatement(); - ResultSet rs = stmt.executeQuery("select MAX(feedid) from FEEDS"); - if (rs.next()) { - max = rs.getInt(1); + try(Statement stmt = conn.createStatement()) { + try (ResultSet rs = stmt.executeQuery("select MAX(feedid) from FEEDS")) { + if (rs.next()) { + max = rs.getInt(1); + } + } } - rs.close(); - stmt.close(); db.release(conn); } catch (SQLException e) { - intlogger.info("getMaxFeedID: " + e.getMessage()); - e.printStackTrace(); + intlogger.warn("PROV0026 Feed.getMaxFeedID: ", e.getMessage()); } return max; } @@ -173,43 +166,42 @@ public class Feed extends Syncable { DB db = new DB(); @SuppressWarnings("resource") Connection conn = db.getConnection(); - Statement stmt = conn.createStatement(); - ResultSet rs = stmt.executeQuery("select * from FEEDS"); - while (rs.next()) { - Feed feed = new Feed(rs); - map.put(feed.getFeedid(), feed); - } - rs.close(); + try(Statement stmt = conn.createStatement()) { + try(ResultSet rs = stmt.executeQuery("select * from FEEDS")) { + while (rs.next()) { + Feed feed = new Feed(rs); + map.put(feed.getFeedid(), feed); + } + } - String sql = "select * from FEED_ENDPOINT_IDS"; - rs = stmt.executeQuery(sql); - while (rs.next()) { - int id = rs.getInt("FEEDID"); - Feed feed = map.get(id); - if (feed != null) { - FeedEndpointID epi = new FeedEndpointID(rs); - Collection ecoll = feed.getAuthorization().getEndpoint_ids(); - ecoll.add(epi); + String sql = "select * from FEED_ENDPOINT_IDS"; + try(ResultSet rs = stmt.executeQuery(sql)){ + while (rs.next()) { + int id = rs.getInt("FEEDID"); + Feed feed = map.get(id); + if (feed != null) { + FeedEndpointID epi = new FeedEndpointID(rs); + Collection ecoll = feed.getAuthorization().getEndpoint_ids(); + ecoll.add(epi); + } + } } - } - rs.close(); - sql = "select * from FEED_ENDPOINT_ADDRS"; - rs = stmt.executeQuery(sql); - while (rs.next()) { - int id = rs.getInt("FEEDID"); - Feed feed = map.get(id); - if (feed != null) { - Collection acoll = feed.getAuthorization().getEndpoint_addrs(); - acoll.add(rs.getString("ADDR")); + sql = "select * from FEED_ENDPOINT_ADDRS"; + try(ResultSet rs = stmt.executeQuery(sql)) { + while (rs.next()) { + int id = rs.getInt("FEEDID"); + Feed feed = map.get(id); + if (feed != null) { + Collection acoll = feed.getAuthorization().getEndpoint_addrs(); + acoll.add(rs.getString("ADDR")); + } + } } } - rs.close(); - - stmt.close(); db.release(conn); } catch (SQLException e) { - e.printStackTrace(); + intlogger.warn("PROV0027 Feed.getAllFeeds: ", e.getMessage()); } return map.values(); } @@ -231,19 +223,19 @@ public class Feed extends Syncable { DB db = new DB(); @SuppressWarnings("resource") Connection conn = db.getConnection(); - PreparedStatement ps = conn.prepareStatement(sql); - if (sql.indexOf('?') >= 0) - ps.setString(1, val); - ResultSet rs = ps.executeQuery(); - while (rs.next()) { - String t = rs.getString(1); - list.add(t.trim()); + try(PreparedStatement ps = conn.prepareStatement(sql)) { + if (sql.indexOf('?') >= 0) + ps.setString(1, val); + try(ResultSet rs = ps.executeQuery()) { + while (rs.next()) { + String t = rs.getString(1); + list.add(t.trim()); + } + } } - rs.close(); - ps.close(); db.release(conn); } catch (SQLException e) { - e.printStackTrace(); + intlogger.warn("PROV0028 Feed.getFilteredFeedUrlList: ", e.getMessage()); } return list; } @@ -254,33 +246,33 @@ public class Feed extends Syncable { try { DB db = new DB(); Connection conn = db.getConnection(); - Statement stmt = conn.createStatement(); - ResultSet rs = stmt.executeQuery(sql); - if (rs.next()) { - feed = new Feed(rs); - rs.close(); - - sql = "select * from FEED_ENDPOINT_IDS where FEEDID = " + feed.feedid; - rs = stmt.executeQuery(sql); - Collection ecoll = feed.getAuthorization().getEndpoint_ids(); - while (rs.next()) { - FeedEndpointID epi = new FeedEndpointID(rs); - ecoll.add(epi); + try (Statement stmt = conn.createStatement()) { + try (ResultSet rs = stmt.executeQuery(sql)) { + if (rs.next()) { + feed = new Feed(rs); + } } - rs.close(); - - sql = "select * from FEED_ENDPOINT_ADDRS where FEEDID = " + feed.feedid; - rs = stmt.executeQuery(sql); - Collection acoll = feed.getAuthorization().getEndpoint_addrs(); - while (rs.next()) { - acoll.add(rs.getString("ADDR")); + if (feed != null) { + sql = "select * from FEED_ENDPOINT_IDS where FEEDID = " + feed.feedid; + try (ResultSet rs = stmt.executeQuery(sql)) { + Collection ecoll = feed.getAuthorization().getEndpoint_ids(); + while (rs.next()) { + FeedEndpointID epi = new FeedEndpointID(rs); + ecoll.add(epi); + } + } + sql = "select * from FEED_ENDPOINT_ADDRS where FEEDID = " + feed.feedid; + try (ResultSet rs = stmt.executeQuery(sql)) { + Collection acoll = feed.getAuthorization().getEndpoint_addrs(); + while (rs.next()) { + acoll.add(rs.getString("ADDR")); + } + } } } - rs.close(); - stmt.close(); db.release(conn); } catch (SQLException e) { - e.printStackTrace(); + intlogger.warn("PROV0029 Feed.getFeedBySQL: ", e.getMessage()); } return feed; } @@ -303,6 +295,7 @@ public class Feed extends Syncable { this.suspended = false; this.last_mod = new Date(); this.created_date = new Date(); + this.aaf_instance = ""; } public Feed(ResultSet rs) throws SQLException { @@ -324,48 +317,57 @@ public class Feed extends Syncable { this.suspended = rs.getBoolean("SUSPENDED"); this.last_mod = rs.getDate("LAST_MOD"); this.created_date = rs.getTimestamp("CREATED_DATE"); + this.aaf_instance = rs.getString("AAF_INSTANCE"); } public Feed(JSONObject jo) throws InvalidObjectException { this("", "", "", ""); try { - // The JSONObject is assumed to contain a vnd.att-dr.feed representation + // The JSONObject is assumed to contain a vnd.dmaap-dr.feed representation this.feedid = jo.optInt("feedid", -1); - this.groupid = jo.optInt("groupid"); //New field is added - Groups feature Rally:US708115 - 1610 + this.groupid = jo.optInt("groupid"); this.name = jo.getString("name"); + this.aaf_instance = jo.optString("aaf_instance", "legacy"); + if(!(aaf_instance.equalsIgnoreCase("legacy"))){ + if (aaf_instance.length() > 255){ + throw new InvalidObjectException("aaf_instance field is too long"); + } + } if (name.length() > 255) throw new InvalidObjectException("name field is too long"); - this.version = jo.getString("version"); - if (version.length() > 20) + try { + this.version = jo.getString("version"); + } catch (JSONException e) { + this.version = null; + } + if(version != null && version.length() > 20) throw new InvalidObjectException("version field is too long"); this.description = jo.optString("description"); - this.business_description = jo.optString("business_description"); // New field is added - Groups feature Rally:US708102 - 1610 + this.business_description = jo.optString("business_description"); if (description.length() > 1000) throw new InvalidObjectException("technical description field is too long"); - - if (business_description.length() > 1000) // New field is added - Groups feature Rally:US708102 - 1610 + if (business_description.length() > 1000) throw new InvalidObjectException("business description field is too long"); - this.authorization = new FeedAuthorization(); JSONObject jauth = jo.getJSONObject("authorization"); this.authorization.setClassification(jauth.getString("classification")); if (this.authorization.getClassification().length() > 32) throw new InvalidObjectException("classification field is too long"); - JSONArray ja = jauth.getJSONArray("endpoint_ids"); - for (int i = 0; i < ja.length(); i++) { - JSONObject id = ja.getJSONObject(i); + JSONArray endPointIds = jauth.getJSONArray("endpoint_ids"); + for (int i = 0; i < endPointIds.length(); i++) { + JSONObject id = endPointIds.getJSONObject(i); FeedEndpointID fid = new FeedEndpointID(id.getString("id"), id.getString("password")); - if (fid.getId().length() > 20) + if (fid.getId().length() > 60) throw new InvalidObjectException("id field is too long (" + fid.getId() + ")"); if (fid.getPassword().length() > 32) - throw new InvalidObjectException("password field is too long (" + fid.getPassword() + ")"); + throw new InvalidObjectException("password field is too long ("+ fid.getPassword()+")"); //Fortify scan fixes - Privacy Violation this.authorization.getEndpoint_ids().add(fid); } if (this.authorization.getEndpoint_ids().size() < 1) throw new InvalidObjectException("need to specify at least one endpoint_id"); - ja = jauth.getJSONArray("endpoint_addrs"); - for (int i = 0; i < ja.length(); i++) { - String addr = ja.getString(i); + endPointIds = jauth.getJSONArray("endpoint_addrs"); + for (int i = 0; i < endPointIds.length(); i++) { + String addr = endPointIds.getString(i); if (!JSONUtilities.validIPAddrOrSubnet(addr)) throw new InvalidObjectException("bad IP addr or subnet mask: " + addr); this.authorization.getEndpoint_addrs().add(addr); @@ -377,8 +379,10 @@ public class Feed extends Syncable { JSONObject jol = jo.optJSONObject("links"); this.links = (jol == null) ? (new FeedLinks()) : (new FeedLinks(jol)); } catch (InvalidObjectException e) { + intlogger.warn("PROV0030 Feed.Feed: ", e.getMessage()); throw e; } catch (Exception e) { + intlogger.error("PROV0031 Feed.Feed: invalid JSON: "+e); throw new InvalidObjectException("invalid JSON: " + e.getMessage()); } } @@ -398,6 +402,14 @@ public class Feed extends Syncable { fl.setLog(URLUtilities.generateFeedLogURL(feedid)); } + public String getAafInstance() { + return aaf_instance; + } + + public void setAaf_instance(String aaf_instance) { + this.aaf_instance = aaf_instance; + } + //new getter setters for groups- Rally:US708115 - 1610 public int getGroupid() { return groupid; @@ -508,6 +520,7 @@ public class Feed extends Syncable { jo.put("suspend", suspended); jo.put("last_mod", last_mod.getTime()); jo.put("created_date", created_date.getTime()); + jo.put("aaf_instance", aaf_instance); return jo; } @@ -543,12 +556,14 @@ public class Feed extends Syncable { } catch (SQLException e) { rv = false; intlogger.warn("PROV0007 doDelete: " + e.getMessage()); - e.printStackTrace(); + intlogger.error("SQLException " + e.getMessage()); } finally { try { - ps.close(); + if(ps!=null) { + ps.close(); + } } catch (SQLException e) { - e.printStackTrace(); + intlogger.error("SQLException " + e.getMessage()); } } return rv; @@ -557,17 +572,8 @@ public class Feed extends Syncable { @Override public synchronized boolean doInsert(Connection c) { boolean rv = true; -// PreparedStatement ps = null; try { if (feedid == -1) { -// // Get the next feedid -// String sql = "insert into FEEDS_UNIQUEID (FEEDID) values (0)"; -// ps = c.prepareStatement(sql, new String[] { "FEEDID" }); -// ps.execute(); -// ResultSet rs = ps.getGeneratedKeys(); -// rs.first(); -// setFeedid(rs.getInt(1)); - // No feed ID assigned yet, so assign the next available one setFeedid(next_feedid++); } // In case we insert a feed from synchronization @@ -577,54 +583,49 @@ public class Feed extends Syncable { // Create FEED_ENDPOINT_IDS rows FeedAuthorization auth = getAuthorization(); String sql = "insert into FEED_ENDPOINT_IDS values (?, ?, ?)"; - PreparedStatement ps2 = c.prepareStatement(sql); - for (FeedEndpointID fid : auth.getEndpoint_ids()) { - ps2.setInt(1, feedid); - ps2.setString(2, fid.getId()); - ps2.setString(3, fid.getPassword()); - ps2.executeUpdate(); + try(PreparedStatement ps2 = c.prepareStatement(sql)) { + for (FeedEndpointID fid : auth.getEndpoint_ids()) { + ps2.setInt(1, feedid); + ps2.setString(2, fid.getId()); + ps2.setString(3, fid.getPassword()); + ps2.executeUpdate(); + } } - ps2.close(); // Create FEED_ENDPOINT_ADDRS rows sql = "insert into FEED_ENDPOINT_ADDRS values (?, ?)"; - ps2 = c.prepareStatement(sql); - for (String t : auth.getEndpoint_addrs()) { - ps2.setInt(1, feedid); - ps2.setString(2, t); - ps2.executeUpdate(); + try(PreparedStatement ps2 = c.prepareStatement(sql)) { + for (String t : auth.getEndpoint_addrs()) { + ps2.setInt(1, feedid); + ps2.setString(2, t); + ps2.executeUpdate(); + } } - ps2.close(); // Finally, create the FEEDS row - sql = "insert into FEEDS (FEEDID, NAME, VERSION, DESCRIPTION, AUTH_CLASS, PUBLISHER, SELF_LINK, PUBLISH_LINK, SUBSCRIBE_LINK, LOG_LINK, DELETED, SUSPENDED,BUSINESS_DESCRIPTION, GROUPID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?, ?)"; - ps2 = c.prepareStatement(sql); - ps2.setInt(1, feedid); - ps2.setString(2, getName()); - ps2.setString(3, getVersion()); - ps2.setString(4, getDescription()); - ps2.setString(5, getAuthorization().getClassification()); - ps2.setString(6, getPublisher()); - ps2.setString(7, getLinks().getSelf()); - ps2.setString(8, getLinks().getPublish()); - ps2.setString(9, getLinks().getSubscribe()); - ps2.setString(10, getLinks().getLog()); - ps2.setBoolean(11, isDeleted()); - ps2.setBoolean(12, isSuspended()); - ps2.setString(13, getBusiness_description()); // New field is added - Groups feature Rally:US708102 - 1610 - ps2.setInt(14, groupid); //New field is added - Groups feature Rally:US708115 - 1610 - ps2.executeUpdate(); - ps2.close(); + sql = "insert into FEEDS (FEEDID, NAME, VERSION, DESCRIPTION, AUTH_CLASS, PUBLISHER, SELF_LINK, PUBLISH_LINK, SUBSCRIBE_LINK, LOG_LINK, DELETED, SUSPENDED,BUSINESS_DESCRIPTION, GROUPID, AAF_INSTANCE) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; + try(PreparedStatement ps2 = c.prepareStatement(sql)) { + ps2.setInt(1, feedid); + ps2.setString(2, getName()); + ps2.setString(3, getVersion()); + ps2.setString(4, getDescription()); + ps2.setString(5, getAuthorization().getClassification()); + ps2.setString(6, getPublisher()); + ps2.setString(7, getLinks().getSelf()); + ps2.setString(8, getLinks().getPublish()); + ps2.setString(9, getLinks().getSubscribe()); + ps2.setString(10, getLinks().getLog()); + ps2.setBoolean(11, isDeleted()); + ps2.setBoolean(12, isSuspended()); + ps2.setString(13, getBusiness_description()); + ps2.setInt(14, groupid); + ps2.setString(15, getAafInstance()); + ps2.executeUpdate(); + } } catch (SQLException e) { rv = false; intlogger.warn("PROV0005 doInsert: " + e.getMessage()); - e.printStackTrace(); -// } finally { -// try { -// ps.close(); -// } catch (SQLException e) { -// e.printStackTrace(); -// } + intlogger.error("SQLException " + e.getMessage()); } return rv; } @@ -697,21 +698,20 @@ public class Feed extends Syncable { ps.setString(2, getAuthorization().getClassification()); ps.setInt(3, deleted ? 1 : 0); ps.setInt(4, suspended ? 1 : 0); - ps.setString(5, getBusiness_description()); // New field is added - Groups feature Rally:US708102 - 1610 - ps.setInt(6, groupid); //New field is added - Groups feature Rally:US708115 - 1610 + ps.setString(5, getBusiness_description()); + ps.setInt(6, groupid); ps.setInt(7, feedid); ps.executeUpdate(); ps.close(); } catch (SQLException e) { rv = false; intlogger.warn("PROV0006 doUpdate: " + e.getMessage()); - e.printStackTrace(); } finally { try { if (ps != null) ps.close(); } catch (SQLException e) { - e.printStackTrace(); + intlogger.error("SQLException " + e.getMessage()); } } return rv; @@ -738,12 +738,13 @@ public class Feed extends Syncable { } catch (SQLException e) { rv = false; intlogger.warn("PROV0006 doUpdate: " + e.getMessage()); - e.printStackTrace(); } finally { try { - ps.close(); + if(ps!=null) { + ps.close(); + } } catch (SQLException e) { - e.printStackTrace(); + intlogger.error("SQLException " + e.getMessage()); } } return rv; @@ -782,6 +783,8 @@ public class Feed extends Syncable { return false; if (suspended != of.suspended) return false; + if (!aaf_instance.equals(of.aaf_instance)) + return false; return true; } @@ -789,4 +792,9 @@ public class Feed extends Syncable { public String toString() { return "FEED: feedid=" + feedid + ", name=" + name + ", version=" + version; } -} + + @Override + public int hashCode() { + return super.hashCode(); + } +} \ No newline at end of file