diff options
author | Fiachra Corcoran <fiachra.corcoran@ericsson.com> | 2018-08-09 00:04:29 +0100 |
---|---|---|
committer | Ronan Keogh <ronan.keogh@ericsson.com> | 2018-08-17 17:58:55 +0100 |
commit | e06737d701ff5b3dcab311f4337ce40be52c966e (patch) | |
tree | 81d76de3d400c99c531147c242fc702506329140 /datarouter-prov/src/main/java/org/onap | |
parent | 49fbf9a09d244bbdc19783d9ff10cd03cd89640a (diff) |
Update for OOM integration
Issue-ID: DMAAP-107
Change-Id: Iff9f93040f7b3120cffb5755adc693e24de991a7
Signed-off-by: Fiachra Corcoran <fiachra.corcoran@ericsson.com>
Diffstat (limited to 'datarouter-prov/src/main/java/org/onap')
12 files changed, 961 insertions, 721 deletions
diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/BaseServlet.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/BaseServlet.java index 92e52fce..6ed5d8b6 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/BaseServlet.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/BaseServlet.java @@ -79,132 +79,194 @@ import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; + /** - * This is the base class for all Servlets in the provisioning code. - * It provides standard constants and some common methods. + * This is the base class for all Servlets in the provisioning code. It provides standard constants and some common + * methods. * * @author Robert Eby * @version $Id: BaseServlet.java,v 1.16 2014/03/12 19:45:40 eby Exp $ */ @SuppressWarnings("serial") public class BaseServlet extends HttpServlet implements ProvDataProvider { - public static final String BEHALF_HEADER = "X-ATT-DR-ON-BEHALF-OF"; - public static final String FEED_BASECONTENT_TYPE = "application/vnd.att-dr.feed"; - public static final String FEED_CONTENT_TYPE = "application/vnd.att-dr.feed; version=2.0"; + + public static final String BEHALF_HEADER = "X-ATT-DR-ON-BEHALF-OF"; + static final String FEED_BASECONTENT_TYPE = "application/vnd.att-dr.feed"; + public static final String FEED_CONTENT_TYPE = "application/vnd.att-dr.feed; version=2.0"; public static final String FEEDFULL_CONTENT_TYPE = "application/vnd.att-dr.feed-full; version=2.0"; public static final String FEEDLIST_CONTENT_TYPE = "application/vnd.att-dr.feed-list; version=1.0"; - public static final String SUB_BASECONTENT_TYPE = "application/vnd.att-dr.subscription"; - public static final String SUB_CONTENT_TYPE = "application/vnd.att-dr.subscription; version=2.0"; - public static final String SUBFULL_CONTENT_TYPE = "application/vnd.att-dr.subscription-full; version=2.0"; - public static final String SUBLIST_CONTENT_TYPE = "application/vnd.att-dr.subscription-list; version=1.0"; + static final String SUB_BASECONTENT_TYPE = "application/vnd.att-dr.subscription"; + public static final String SUB_CONTENT_TYPE = "application/vnd.att-dr.subscription; version=2.0"; + public static final String SUBFULL_CONTENT_TYPE = "application/vnd.att-dr.subscription-full; version=2.0"; + static final String SUBLIST_CONTENT_TYPE = "application/vnd.att-dr.subscription-list; version=1.0"; //Adding groups functionality, ...1610 - public static final String GROUP_BASECONTENT_TYPE = "application/vnd.att-dr.group"; - public static final String GROUP_CONTENT_TYPE = "application/vnd.att-dr.group; version=2.0"; - public static final String GROUPFULL_CONTENT_TYPE = "application/vnd.att-dr.group-full; version=2.0"; + static final String GROUP_BASECONTENT_TYPE = "application/vnd.att-dr.group"; + public static final String GROUP_CONTENT_TYPE = "application/vnd.att-dr.group; version=2.0"; + static final String GROUPFULL_CONTENT_TYPE = "application/vnd.att-dr.group-full; version=2.0"; public static final String GROUPLIST_CONTENT_TYPE = "application/vnd.att-dr.fegrouped-list; version=1.0"; - public static final String LOGLIST_CONTENT_TYPE = "application/vnd.att-dr.log-list; version=1.0"; + public static final String LOGLIST_CONTENT_TYPE = "application/vnd.att-dr.log-list; version=1.0"; public static final String PROVFULL_CONTENT_TYPE1 = "application/vnd.att-dr.provfeed-full; version=1.0"; public static final String PROVFULL_CONTENT_TYPE2 = "application/vnd.att-dr.provfeed-full; version=2.0"; - public static final String CERT_ATTRIBUTE = "javax.servlet.request.X509Certificate"; - - public static final String DB_PROBLEM_MSG = "There has been a problem with the DB. It is suggested you try the operation again."; - - public static final int DEFAULT_MAX_FEEDS = 10000; - public static final int DEFAULT_MAX_SUBS = 100000; - public static final int DEFAULT_POKETIMER1 = 5; - public static final int DEFAULT_POKETIMER2 = 30; - public static final String DEFAULT_DOMAIN = "web.att.com"; - public static final String DEFAULT_PROVSRVR_NAME = "feeds-drtr.web.att.com"; - public static final String RESEARCH_SUBNET = "135.207.136.128/25"; - public static final String STATIC_ROUTING_NODES = ""; //Adding new param for static Routing - Rally:US664862-1610 - - /** A boolean to trigger one time "provisioning changed" event on startup */ - private static boolean startmsg_flag = true; - /** This POD should require SSL connections from clients; pulled from the DB (PROV_REQUIRE_SECURE) */ - private static boolean require_secure = true; - /** This POD should require signed, recognized certificates from clients; pulled from the DB (PROV_REQUIRE_CERT) */ - private static boolean require_cert = true; - /** The set of authorized addresses and networks; pulled from the DB (PROV_AUTH_ADDRESSES) */ + public static final String CERT_ATTRIBUTE = "javax.servlet.request.X509Certificate"; + + static final String DB_PROBLEM_MSG = "There has been a problem with the DB. It is suggested you try the operation again."; + + private static final int DEFAULT_MAX_FEEDS = 10000; + private static final int DEFAULT_MAX_SUBS = 100000; + private static final int DEFAULT_POKETIMER1 = 5; + private static final int DEFAULT_POKETIMER2 = 30; + private static final String DEFAULT_DOMAIN = "onap"; + private static final String DEFAULT_PROVSRVR_NAME = "dmaap-dr-prov"; + private static final String RESEARCH_SUBNET = "10.42.0.0/16"; + private static final String STATIC_ROUTING_NODES = ""; //Adding new param for static Routing - Rally:US664862-1610 + + /** + * A boolean to trigger one time "provisioning changed" event on startup + */ + private static boolean startmsgFlag = true; + /** + * This POD should require SSL connections from clients; pulled from the DB (PROV_REQUIRE_SECURE) + */ + private static boolean requireSecure = true; + /** + * This POD should require signed, recognized certificates from clients; pulled from the DB (PROV_REQUIRE_CERT) + */ + private static boolean requireCert = true; + /** + * The set of authorized addresses and networks; pulled from the DB (PROV_AUTH_ADDRESSES) + */ private static Set<String> authorizedAddressesAndNetworks = new HashSet<String>(); - /** The set of authorized names; pulled from the DB (PROV_AUTH_SUBJECTS) */ + /** + * The set of authorized names; pulled from the DB (PROV_AUTH_SUBJECTS) + */ private static Set<String> authorizedNames = new HashSet<String>(); - /** The FQDN of the initially "active" provisioning server in this Data Router ecosystem */ - private static String initial_active_pod; - /** The FQDN of the initially "standby" provisioning server in this Data Router ecosystem */ - private static String initial_standby_pod; - /** The FQDN of this provisioning server in this Data Router ecosystem */ - private static String this_pod; - /** "Timer 1" - used to determine when to notify nodes of provisioning changes */ - private static long poke_timer1; - /** "Timer 2" - used to determine when to notify nodes of provisioning changes */ - private static long poke_timer2; - /** Array of nodes names and/or FQDNs */ + /** + * The FQDN of the initially "active" provisioning server in this Data Router ecosystem + */ + private static String initialActivePod; + /** + * The FQDN of the initially "standby" provisioning server in this Data Router ecosystem + */ + private static String initialStandbyPod; + /** + * The FQDN of this provisioning server in this Data Router ecosystem + */ + private static String thisPod; + /** + * "Timer 1" - used to determine when to notify nodes of provisioning changes + */ + private static long pokeTimer1; + /** + * "Timer 2" - used to determine when to notify nodes of provisioning changes + */ + private static long pokeTimer2; + /** + * Array of nodes names and/or FQDNs + */ private static String[] nodes = new String[0]; - /** Array of node IP addresses */ + /** + * Array of node IP addresses + */ private static InetAddress[] nodeAddresses = new InetAddress[0]; - /** Array of POD IP addresses */ + /** + * Array of POD IP addresses + */ private static InetAddress[] podAddresses = new InetAddress[0]; - /** The maximum number of feeds allowed; pulled from the DB (PROV_MAXFEED_COUNT) */ - protected static int max_feeds = 0; - /** The maximum number of subscriptions allowed; pulled from the DB (PROV_MAXSUB_COUNT) */ - protected static int max_subs = 0; - /** The current number of feeds in the system */ - protected static int active_feeds = 0; - /** The current number of subscriptions in the system */ - protected static int active_subs = 0; - /** The domain used to generate a FQDN from the "bare" node names */ - public static String prov_domain = "web.att.com"; - /** The standard FQDN of the provisioning server in this Data Router ecosystem */ - public static String prov_name = "feeds-drtr.web.att.com"; - /** The standard FQDN of the ACTIVE provisioning server in this Data Router ecosystem */ - public static String active_prov_name = "feeds-drtr.web.att.com"; - /** Special subnet that is allowed access to /internal */ - protected static String special_subnet = RESEARCH_SUBNET; - - /** Special subnet that is allowed access to /internal to Lab Machine */ - protected static String special_subnet_secondary = RESEARCH_SUBNET; - protected static String static_routing_nodes = STATIC_ROUTING_NODES; //Adding new param for static Routing - Rally:US664862-1610 - - /** This logger is used to log provisioning events */ + /** + * The maximum number of feeds allowed; pulled from the DB (PROV_MAXFEED_COUNT) + */ + static int maxFeeds = 0; + /** + * The maximum number of subscriptions allowed; pulled from the DB (PROV_MAXSUB_COUNT) + */ + static int maxSubs = 0; + /** + * The current number of feeds in the system + */ + static int activeFeeds = 0; + /** + * The current number of subscriptions in the system + */ + static int activeSubs = 0; + /** + * The domain used to generate a FQDN from the "bare" node names + */ + private static String provDomain = "web.att.com"; + /** + * The standard FQDN of the provisioning server in this Data Router ecosystem + */ + public static String provName = "feeds-drtr.web.att.com"; + /** + * The standard FQDN of the ACTIVE provisioning server in this Data Router ecosystem + */ + public static String activeProvName = "feeds-drtr.web.att.com"; + /** + * Special subnet that is allowed access to /internal + */ + private static String researchSubnet = RESEARCH_SUBNET; + /** + * Special subnet that is allowed access to /internal to Lab Machine + */ + private static String researchSubnet1 = RESEARCH_SUBNET; + private static String staticRoutingNodes = STATIC_ROUTING_NODES; //Adding new param for static Routing - Rally:US664862-1610 + + /** + * This logger is used to log provisioning events + */ protected static Logger eventlogger; - /** This logger is used to log internal events (errors, etc.) */ + /** + * This logger is used to log internal events (errors, etc.) + */ protected static Logger intlogger; - /** Authorizer - interface to the Policy Engine */ + /** + * Authorizer - interface to the Policy Engine + */ protected static Authorizer authz; - /** The Synchronizer used to sync active DB to standby one */ - protected static SynchronizerTask synctask = null; + /** + * The Synchronizer used to sync active DB to standby one + */ + private static SynchronizerTask synctask = null; //Data Router Subscriber HTTPS Relaxation feature USERSTORYID:US674047. private InetAddress thishost; private InetAddress loopback; private static Boolean mailSendFlag = false; - public static final String MAILCONFIG_FILE = "mail.properties"; + private static final String MAILCONFIG_FILE = "mail.properties"; private static Properties mailprops; + + //DMAAP-597 (Tech Dept) REST request source IP auth relaxation to accommodate OOM kubernetes deploy + private static String isAddressAuthEnabled = (new DB()).getProperties() + .getProperty("org.onap.dmaap.datarouter.provserver.isaddressauthenabled", "false"); + /** * Initialize data common to all the provisioning server servlets. */ protected BaseServlet() { - if (eventlogger == null) + if (eventlogger == null) { eventlogger = Logger.getLogger("org.onap.dmaap.datarouter.provisioning.events"); - if (intlogger == null) - intlogger = Logger.getLogger("org.onap.dmaap.datarouter.provisioning.internal"); - if (authz == null) + } + if (intlogger == null) { + intlogger = Logger.getLogger("org.onap.dmaap.datarouter.provisioning.internal"); + } + if (authz == null) { authz = new ProvAuthorizer(this); - if (startmsg_flag) { - startmsg_flag = false; + } + if (startmsgFlag) { + startmsgFlag = false; provisioningParametersChanged(); } if (synctask == null) { synctask = SynchronizerTask.getSynchronizer(); } String name = this.getClass().getName(); - intlogger.info("PROV0002 Servlet "+name+" started."); + intlogger.info("PROV0002 Servlet " + name + " started."); } + @Override public void init(ServletConfig config) throws ServletException { super.init(config); @@ -216,43 +278,52 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider { // ignore } } - protected int getIdFromPath(HttpServletRequest req) { + + int getIdFromPath(HttpServletRequest req) { String path = req.getPathInfo(); - if (path == null || path.length() < 2) + if (path == null || path.length() < 2) { return -1; + } try { return Integer.parseInt(path.substring(1)); } catch (NumberFormatException e) { return -1; } } + /** * Read the request's input stream and return a JSONObject from it + * * @param req the HTTP request * @return the JSONObject, or null if the stream cannot be parsed */ - protected JSONObject getJSONfromInput(HttpServletRequest req) { + JSONObject getJSONfromInput(HttpServletRequest req) { JSONObject jo = null; try { jo = new JSONObject(new JSONTokener(req.getInputStream())); - if (intlogger.isDebugEnabled()) - intlogger.debug("JSON: "+jo.toString()); + if (intlogger.isDebugEnabled()) { + intlogger.debug("JSON: " + jo.toString()); + } } catch (Exception e) { - intlogger.info("Error reading JSON: "+e); + intlogger.info("Error reading JSON: " + e); } return jo; } + /** - * Check if the remote host is authorized to perform provisioning. - * Is the request secure? - * Is it coming from an authorized IP address or network (configured via PROV_AUTH_ADDRESSES)? - * Does it have a valid client certificate (configured via PROV_AUTH_SUBJECTS)? + * Check if the remote host is authorized to perform provisioning. Is the request secure? Is it coming from an + * authorized IP address or network (configured via PROV_AUTH_ADDRESSES)? Does it have a valid client certificate + * (configured via PROV_AUTH_SUBJECTS)? + * * @param request the request * @return an error string, or null if all is OK */ - protected String isAuthorizedForProvisioning(HttpServletRequest request) { + String isAuthorizedForProvisioning(HttpServletRequest request) { + if (Boolean.parseBoolean(isAddressAuthEnabled)) { + return null; + } // Is the request https? - if (require_secure && !request.isSecure()) { + if (requireSecure && !request.isSecure()) { return "Request must be made over an HTTPS connection."; } @@ -265,14 +336,14 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider { found |= addressMatchesNetwork(ip, addrnet); } if (!found) { - return "Unauthorized address: "+remote; + return "Unauthorized address: " + remote; } } catch (UnknownHostException e) { - return "Unauthorized address: "+remote; + return "Unauthorized address: " + remote; } // Does remote have a valid certificate? - if (require_cert) { + if (requireCert) { X509Certificate certs[] = (X509Certificate[]) request.getAttribute(CERT_ATTRIBUTE); if (certs == null || certs.length == 0) { return "Client certificate is missing."; @@ -288,139 +359,159 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider { // No problems! return null; } + /** * Check if the remote IP address is authorized to see the /internal URL tree. + * * @param request the HTTP request * @return true iff authorized */ - protected boolean isAuthorizedForInternal(HttpServletRequest request) { + boolean isAuthorizedForInternal(HttpServletRequest request) { + try { + if (Boolean.parseBoolean(isAddressAuthEnabled)) { + return true; + } InetAddress ip = InetAddress.getByName(request.getRemoteAddr()); for (InetAddress node : getNodeAddresses()) { - if (node != null && ip.equals(node)) + if (node != null && ip.equals(node)) { return true; + } } for (InetAddress pod : getPodAddresses()) { - if (pod != null && ip.equals(pod)) + if (pod != null && ip.equals(pod)) { return true; + } } - if (thishost != null && ip.equals(thishost)) + if (thishost != null && ip.equals(thishost)) { return true; - if (loopback != null && ip.equals(loopback)) + } + if (loopback != null && ip.equals(loopback)) { return true; + } // Also allow the "special subnet" access - if (addressMatchesNetwork(ip, special_subnet_secondary)) + if (addressMatchesNetwork(ip, researchSubnet1)) { return true; - if (addressMatchesNetwork(ip, special_subnet)) + } + if (addressMatchesNetwork(ip, researchSubnet)) { return true; + } } catch (UnknownHostException e) { // ignore } return false; } + /** * Check if an IP address matches a network address. + * * @param ip the IP address * @param s the network address; a bare IP address may be matched also * @return true if they intersect */ - protected static boolean addressMatchesNetwork(InetAddress ip, String s) { + private static boolean addressMatchesNetwork(InetAddress ip, String s) { int mlen = -1; int n = s.indexOf("/"); if (n >= 0) { - mlen = Integer.parseInt(s.substring(n+1)); + mlen = Integer.parseInt(s.substring(n + 1)); s = s.substring(0, n); } try { InetAddress i2 = InetAddress.getByName(s); byte[] b1 = ip.getAddress(); byte[] b2 = i2.getAddress(); - if (b1.length != b2.length) + if (b1.length != b2.length) { return false; + } if (mlen > 0) { byte[] masks = { - (byte)0x00, (byte)0x80, (byte)0xC0, (byte)0xE0, - (byte)0xF0, (byte)0xF8, (byte)0xFC, (byte)0xFE + (byte) 0x00, (byte) 0x80, (byte) 0xC0, (byte) 0xE0, + (byte) 0xF0, (byte) 0xF8, (byte) 0xFC, (byte) 0xFE }; - byte mask = masks[mlen%8]; - for (n = mlen/8; n < b1.length; n++) { + byte mask = masks[mlen % 8]; + for (n = mlen / 8; n < b1.length; n++) { b1[n] &= mask; b2[n] &= mask; mask = 0; } } - for (n = 0; n < b1.length; n++) - if (b1[n] != b2[n]) + for (n = 0; n < b1.length; n++) { + if (b1[n] != b2[n]) { return false; + } + } } catch (UnknownHostException e) { return false; } return true; } + /** - * Something has changed in the provisioning data. - * Start the timers that will cause the pre-packaged JSON string to be regenerated, - * and cause nodes and the other provisioning server to be notified. + * Something has changed in the provisioning data. Start the timers that will cause the pre-packaged JSON string to + * be regenerated, and cause nodes and the other provisioning server to be notified. */ public static void provisioningDataChanged() { long now = System.currentTimeMillis(); Poker p = Poker.getPoker(); - p.setTimers(now + (poke_timer1 * 1000L), now + (poke_timer2 * 1000L)); + p.setTimers(now + (pokeTimer1 * 1000L), now + (pokeTimer2 * 1000L)); } + /** * Something in the parameters has changed, reload all parameters from the DB. */ public static void provisioningParametersChanged() { - Map<String,String> map = Parameters.getParameters(); - require_secure = getBoolean(map, Parameters.PROV_REQUIRE_SECURE); - require_cert = getBoolean(map, Parameters.PROV_REQUIRE_CERT); + Map<String, String> map = Parameters.getParameters(); + requireSecure = getBoolean(map, Parameters.PROV_REQUIRE_SECURE); + requireCert = getBoolean(map, Parameters.PROV_REQUIRE_CERT); authorizedAddressesAndNetworks = getSet(map, Parameters.PROV_AUTH_ADDRESSES); - authorizedNames = getSet (map, Parameters.PROV_AUTH_SUBJECTS); - nodes = getSet (map, Parameters.NODES).toArray(new String[0]); - max_feeds = getInt (map, Parameters.PROV_MAXFEED_COUNT, DEFAULT_MAX_FEEDS); - max_subs = getInt (map, Parameters.PROV_MAXSUB_COUNT, DEFAULT_MAX_SUBS); - poke_timer1 = getInt (map, Parameters.PROV_POKETIMER1, DEFAULT_POKETIMER1); - poke_timer2 = getInt (map, Parameters.PROV_POKETIMER2, DEFAULT_POKETIMER2); - prov_domain = getString (map, Parameters.PROV_DOMAIN, DEFAULT_DOMAIN); - prov_name = getString (map, Parameters.PROV_NAME, DEFAULT_PROVSRVR_NAME); - active_prov_name = getString (map, Parameters.PROV_ACTIVE_NAME, prov_name); - special_subnet = getString (map, Parameters.PROV_SPECIAL_SUBNET, RESEARCH_SUBNET); - static_routing_nodes = getString (map, Parameters.STATIC_ROUTING_NODES, ""); //Adding new param for static Routing - Rally:US664862-1610 - initial_active_pod = getString (map, Parameters.ACTIVE_POD, ""); - initial_standby_pod = getString (map, Parameters.STANDBY_POD, ""); - static_routing_nodes = getString (map, Parameters.STATIC_ROUTING_NODES, ""); //Adding new param for static Routing - Rally:US664862-1610 - active_feeds = Feed.countActiveFeeds(); - active_subs = Subscription.countActiveSubscriptions(); + authorizedNames = getSet(map, Parameters.PROV_AUTH_SUBJECTS); + nodes = getSet(map, Parameters.NODES).toArray(new String[0]); + maxFeeds = getInt(map, Parameters.PROV_MAXFEED_COUNT, DEFAULT_MAX_FEEDS); + maxSubs = getInt(map, Parameters.PROV_MAXSUB_COUNT, DEFAULT_MAX_SUBS); + pokeTimer1 = getInt(map, Parameters.PROV_POKETIMER1, DEFAULT_POKETIMER1); + pokeTimer2 = getInt(map, Parameters.PROV_POKETIMER2, DEFAULT_POKETIMER2); + provDomain = getString(map, Parameters.PROV_DOMAIN, DEFAULT_DOMAIN); + provName = getString(map, Parameters.PROV_NAME, DEFAULT_PROVSRVR_NAME); + activeProvName = getString(map, Parameters.PROV_ACTIVE_NAME, provName); + researchSubnet = getString(map, Parameters.PROV_SPECIAL_SUBNET, RESEARCH_SUBNET); + staticRoutingNodes = getString(map, Parameters.STATIC_ROUTING_NODES, + ""); //Adding new param for static Routing - Rally:US664862-1610 + initialActivePod = getString(map, Parameters.ACTIVE_POD, ""); + initialStandbyPod = getString(map, Parameters.STANDBY_POD, ""); + staticRoutingNodes = getString(map, Parameters.STATIC_ROUTING_NODES, + ""); //Adding new param for static Routing - Rally:US664862-1610 + activeFeeds = Feed.countActiveFeeds(); + activeSubs = Subscription.countActiveSubscriptions(); try { - this_pod = InetAddress.getLocalHost().getHostName(); + thisPod = InetAddress.getLocalHost().getHostName(); } catch (UnknownHostException e) { - this_pod = ""; + thisPod = ""; intlogger.warn("PROV0014 Cannot determine the name of this provisioning server."); } // Normalize the nodes, and fill in nodeAddresses InetAddress[] na = new InetAddress[nodes.length]; for (int i = 0; i < nodes.length; i++) { - if (nodes[i].indexOf('.') < 0) - nodes[i] += "." + prov_domain; + if (nodes[i].indexOf('.') < 0) { + nodes[i] += "." + provDomain; + } try { na[i] = InetAddress.getByName(nodes[i]); - intlogger.debug("PROV0003 DNS lookup: "+nodes[i]+" => "+na[i].toString()); + intlogger.debug("PROV0003 DNS lookup: " + nodes[i] + " => " + na[i].toString()); } catch (UnknownHostException e) { na[i] = null; - intlogger.warn("PROV0004 Cannot lookup "+nodes[i]+": "+e); + intlogger.warn("PROV0004 Cannot lookup " + nodes[i] + ": " + e); } } //Reset Nodes arr after - removing static routing Nodes, Rally Userstory - US664862 . List<String> filterNodes = new ArrayList<>(); - for (int i = 0; i < nodes.length; i++) { - if(!static_routing_nodes.contains(nodes[i])){ - filterNodes.add(nodes[i]); + for (String node : nodes) { + if (!staticRoutingNodes.contains(node)) { + filterNodes.add(node); } } - String [] filteredNodes = filterNodes.toArray(new String[filterNodes.size()]); - nodes = filteredNodes; + nodes = filterNodes.toArray(new String[filterNodes.size()]); nodeAddresses = na; NodeClass.setNodes(nodes); // update NODES table @@ -429,14 +520,15 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider { String[] pods = getPods(); na = new InetAddress[pods.length]; for (int i = 0; i < pods.length; i++) { - if (pods[i].indexOf('.') < 0) - pods[i] += "." + prov_domain; + if (pods[i].indexOf('.') < 0) { + pods[i] += "." + provDomain; + } try { na[i] = InetAddress.getByName(pods[i]); - intlogger.debug("PROV0003 DNS lookup: "+pods[i]+" => "+na[i].toString()); + intlogger.debug("PROV0003 DNS lookup: " + pods[i] + " => " + na[i].toString()); } catch (UnknownHostException e) { na[i] = null; - intlogger.warn("PROV0004 Cannot lookup "+pods[i]+": "+e); + intlogger.warn("PROV0004 Cannot lookup " + pods[i] + ": " + e); } } podAddresses = na; @@ -445,16 +537,17 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider { ThrottleFilter.configure(); // Check if we are active or standby POD - if (!isInitialActivePOD() && !isInitialStandbyPOD()) + if (!isInitialActivePOD() && !isInitialStandbyPOD()) { intlogger.warn("PROV0015 This machine is neither the active nor the standby POD."); + } } - /**Data Router Subscriber HTTPS Relaxation feature USERSTORYID:US674047. - * Load mail properties. - * @author vs215k + /** + * Data Router Subscriber HTTPS Relaxation feature USERSTORYID:US674047. Load mail properties. * - **/ + * @author vs215k + **/ private void loadMailProperties() { if (mailprops == null) { mailprops = new Properties(); @@ -462,46 +555,45 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider { try { mailprops.load(inStream); } catch (IOException e) { - intlogger.fatal("PROV9003 Opening properties: "+e.getMessage()); + intlogger.fatal("PROV9003 Opening properties: " + e.getMessage()); e.printStackTrace(); System.exit(1); - } - finally { + } finally { try { inStream.close(); - } - catch (IOException e) { + } catch (IOException e) { } } } } - /**Data Router Subscriber HTTPS Relaxation feature USERSTORYID:US674047. - * Check if HTTPS Relexaction is enabled - * @author vs215k + /** + * Data Router Subscriber HTTPS Relaxation feature USERSTORYID:US674047. Check if HTTPS Relexaction is enabled * - **/ + * @author vs215k + **/ private void checkHttpsRelaxation() { - if(mailSendFlag == false) { + if (!mailSendFlag) { Properties p = (new DB()).getProperties(); - intlogger.info("HTTPS relaxatio: "+p.get("org.onap.dmaap.datarouter.provserver.https.relaxation")); + intlogger.info("HTTPS relaxatio: " + p.get("org.onap.dmaap.datarouter.provserver.https.relaxation")); - if(p.get("org.onap.dmaap.datarouter.provserver.https.relaxation").equals("true")) { + if (p.get("org.onap.dmaap.datarouter.provserver.https.relaxation").equals("true")) { try { - notifyPSTeam(p.get("org.onap.dmaap.datarouter.provserver.https.relax.notify").toString()); - } - catch (Exception e) { + notifyPSTeam(p.get("org.onap.dmaap.datarouter.provserver.https.relax.notify").toString()); + } catch (Exception e) { e.printStackTrace(); } - } + } mailSendFlag = true; } } - /**Data Router Subscriber HTTPS Relaxation feature USERSTORYID:US674047. - * @author vs215k + /** + * Data Router Subscriber HTTPS Relaxation feature USERSTORYID:US674047. + * * @param email - list of email ids to notify if HTTP relexcation is enabled. - **/ + * @author vs215k + **/ private void notifyPSTeam(String email) throws Exception { loadMailProperties(); //Load HTTPS Relex mail properties. String[] emails = email.split(Pattern.quote("|")); @@ -516,88 +608,101 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider { try { - Message msg = new MimeMessage(session); - msg.setFrom(new InternetAddress(mailprops.get("com.att.dmaap.datarouter.mail.from").toString())); + Message msg = new MimeMessage(session); + msg.setFrom(new InternetAddress(mailprops.get("com.att.dmaap.datarouter.mail.from").toString())); - InternetAddress[] addressTo = new InternetAddress[emails.length]; - for ( int x =0 ; x < emails.length; x++) { - addressTo[x] = new InternetAddress(emails[x]); - } + InternetAddress[] addressTo = new InternetAddress[emails.length]; + for (int x = 0; x < emails.length; x++) { + addressTo[x] = new InternetAddress(emails[x]); + } - msg.addRecipients(Message.RecipientType.TO, addressTo); - msg.setSubject(mailprops.get("com.att.dmaap.datarouter.mail.subject").toString()); - htmlPart.setContent(mailprops.get("com.att.dmaap.datarouter.mail.body").toString().replace("[SERVER]", InetAddress.getLocalHost().getHostName()), "text/html"); - mp.addBodyPart(htmlPart); - msg.setContent(mp); + msg.addRecipients(Message.RecipientType.TO, addressTo); + msg.setSubject(mailprops.get("com.att.dmaap.datarouter.mail.subject").toString()); + htmlPart.setContent(mailprops.get("com.att.dmaap.datarouter.mail.body").toString() + .replace("[SERVER]", InetAddress.getLocalHost().getHostName()), "text/html"); + mp.addBodyPart(htmlPart); + msg.setContent(mp); - System.out.println(mailprops.get("com.att.dmaap.datarouter.mail.body").toString().replace("[SERVER]", InetAddress.getLocalHost().getHostName())); + System.out.println(mailprops.get("com.att.dmaap.datarouter.mail.body").toString() + .replace("[SERVER]", InetAddress.getLocalHost().getHostName())); - Transport.send(msg); - intlogger.info("HTTPS relaxation mail is sent to - : "+email); + Transport.send(msg); + intlogger.info("HTTPS relaxation mail is sent to - : " + email); } catch (AddressException e) { - intlogger.error("Invalid email address, unable to send https relaxation mail to - : "+email); + intlogger.error("Invalid email address, unable to send https relaxation mail to - : " + email); } catch (MessagingException e) { - intlogger.error("Invalid email address, unable to send https relaxation mail to - : "+email); + intlogger.error("Invalid email address, unable to send https relaxation mail to - : " + email); } } /** * Get an array of all node names in the DR network. + * * @return an array of Strings */ public static String[] getNodes() { return nodes; } + /** * Get an array of all node InetAddresses in the DR network. + * * @return an array of InetAddresses */ public static InetAddress[] getNodeAddresses() { return nodeAddresses; } + /** * Get an array of all POD names in the DR network. + * * @return an array of Strings */ public static String[] getPods() { - return new String[] { initial_active_pod, initial_standby_pod }; + return new String[]{initialActivePod, initialStandbyPod}; } + /** * Get an array of all POD InetAddresses in the DR network. + * * @return an array of InetAddresses */ - public static InetAddress[] getPodAddresses() { + private static InetAddress[] getPodAddresses() { return podAddresses; } + /** - * Gets the FQDN of the initially ACTIVE provisioning server (POD). - * Note: this used to be called isActivePOD(), however, that is a misnomer, as the active status - * could shift to the standby POD without these parameters changing. Hence, the function names - * have been changed to more accurately reflect their purpose. + * Gets the FQDN of the initially ACTIVE provisioning server (POD). Note: this used to be called isActivePOD(), + * however, that is a misnomer, as the active status could shift to the standby POD without these parameters + * changing. Hence, the function names have been changed to more accurately reflect their purpose. + * * @return the FQDN */ public static boolean isInitialActivePOD() { - return this_pod.equals(initial_active_pod); + return thisPod.equals(initialActivePod); } + /** - * Gets the FQDN of the initially STANDBY provisioning server (POD). - * Note: this used to be called isStandbyPOD(), however, that is a misnomer, as the standby status - * could shift to the active POD without these parameters changing. Hence, the function names - * have been changed to more accurately reflect their purpose. + * Gets the FQDN of the initially STANDBY provisioning server (POD). Note: this used to be called isStandbyPOD(), + * however, that is a misnomer, as the standby status could shift to the active POD without these parameters + * changing. Hence, the function names have been changed to more accurately reflect their purpose. + * * @return the FQDN */ public static boolean isInitialStandbyPOD() { - return this_pod.equals(initial_standby_pod); + return thisPod.equals(initialStandbyPod); } + /** * INSERT an {@link Insertable} bean into the database. + * * @param bean the bean representing a row to insert * @return true if the INSERT was successful */ protected boolean doInsert(Insertable bean) { - boolean rv = false; + boolean rv; DB db = new DB(); Connection conn = null; try { @@ -605,21 +710,24 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider { rv = bean.doInsert(conn); } catch (SQLException e) { rv = false; - intlogger.warn("PROV0005 doInsert: "+e.getMessage()); + intlogger.warn("PROV0005 doInsert: " + e.getMessage()); e.printStackTrace(); } finally { - if (conn != null) + if (conn != null) { db.release(conn); + } } return rv; } + /** * UPDATE an {@link Updateable} bean in the database. + * * @param bean the bean representing a row to update * @return true if the UPDATE was successful */ protected boolean doUpdate(Updateable bean) { - boolean rv = false; + boolean rv; DB db = new DB(); Connection conn = null; try { @@ -627,21 +735,24 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider { rv = bean.doUpdate(conn); } catch (SQLException e) { rv = false; - intlogger.warn("PROV0006 doUpdate: "+e.getMessage()); + intlogger.warn("PROV0006 doUpdate: " + e.getMessage()); e.printStackTrace(); } finally { - if (conn != null) + if (conn != null) { db.release(conn); + } } return rv; } + /** * DELETE an {@link Deleteable} bean from the database. + * * @param bean the bean representing a row to delete * @return true if the DELETE was successful */ protected boolean doDelete(Deleteable bean) { - boolean rv = false; + boolean rv; DB db = new DB(); Connection conn = null; try { @@ -649,23 +760,27 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider { rv = bean.doDelete(conn); } catch (SQLException e) { rv = false; - intlogger.warn("PROV0007 doDelete: "+e.getMessage()); + intlogger.warn("PROV0007 doDelete: " + e.getMessage()); e.printStackTrace(); } finally { - if (conn != null) + if (conn != null) { db.release(conn); + } } return rv; } - private static boolean getBoolean(Map<String,String> map, String name) { + + private static boolean getBoolean(Map<String, String> map, String name) { String s = map.get(name); return (s != null) && s.equalsIgnoreCase("true"); } - private static String getString(Map<String,String> map, String name, String dflt) { + + private static String getString(Map<String, String> map, String name, String dflt) { String s = map.get(name); return (s != null) ? s : dflt; } - private static int getInt(Map<String,String> map, String name, int dflt) { + + private static int getInt(Map<String, String> map, String name, int dflt) { try { String s = map.get(name); return Integer.parseInt(s); @@ -673,7 +788,8 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider { return dflt; } } - private static Set<String> getSet(Map<String,String> map, String name) { + + private static Set<String> getSet(Map<String, String> map, String name) { Set<String> set = new HashSet<String>(); String s = map.get(name); if (s != null) { @@ -681,8 +797,9 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider { if (pp != null) { for (String t : pp) { String t2 = t.trim(); - if (t2.length() > 0) + if (t2.length() > 0) { set.add(t2); + } } } } @@ -690,36 +807,43 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider { } /** - * A class used to encapsulate a Content-type header, separating out the "version" attribute - * (which defaults to "1.0" if missing). + * A class used to encapsulate a Content-type header, separating out the "version" attribute (which defaults to + * "1.0" if missing). */ public class ContentHeader { + private String type = ""; private Map<String, String> map = new HashMap<String, String>(); - public ContentHeader() { + + ContentHeader() { this("", "1.0"); } - public ContentHeader(String t, String v) { + + ContentHeader(String t, String v) { type = t.trim(); map.put("version", v); } + public String getType() { return type; } + public String getAttribute(String key) { String s = map.get(key); - if (s == null) + if (s == null) { s = ""; + } return s; } } /** * Get the ContentHeader from an HTTP request. + * * @param req the request * @return the header, encapsulated in a ContentHeader object */ - public ContentHeader getContentHeader(HttpServletRequest req) { + ContentHeader getContentHeader(HttpServletRequest req) { ContentHeader ch = new ContentHeader(); String s = req.getHeader("Content-Type"); if (s != null) { @@ -729,8 +853,8 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider { int ix = pp[i].indexOf('='); if (ix > 0) { String k = pp[i].substring(0, ix).trim(); - String v = pp[i].substring(ix+1).trim(); - ch.map.put(k, v); + String v = pp[i].substring(ix + 1).trim(); + ch.map.put(k, v); } else { ch.map.put(pp[i].trim(), ""); } @@ -738,38 +862,44 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider { } return ch; } + // Methods for the Policy Engine classes - ProvDataProvider interface @Override public String getFeedOwner(String feedId) { try { int n = Integer.parseInt(feedId); Feed f = Feed.getFeedById(n); - if (f != null) + if (f != null) { return f.getPublisher(); + } } catch (NumberFormatException e) { // ignore } return null; } + @Override public String getFeedClassification(String feedId) { try { int n = Integer.parseInt(feedId); Feed f = Feed.getFeedById(n); - if (f != null) + if (f != null) { return f.getAuthorization().getClassification(); + } } catch (NumberFormatException e) { // ignore } return null; } + @Override public String getSubscriptionOwner(String subId) { try { int n = Integer.parseInt(subId); Subscription s = Subscription.getSubscriptionById(n); - if (s != null) + if (s != null) { return s.getSubscriber(); + } } catch (NumberFormatException e) { // ignore } @@ -783,18 +913,19 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider { */ private boolean isUserMemberOfGroup(Group group, String user) { - String groupdetails = group.getMembers().replace("]", "").replace("[", ""); - String s[] = groupdetails.split("},"); + String groupDetails = group.getMembers().replace("]", "").replace("[", ""); + String[] s = groupDetails.split("},"); - for(int i=0; i < s.length; i++) { - JSONObject jsonObj = null; - try { - jsonObj = new JSONObject(s[i]+"}"); - if(jsonObj.get("id").equals(user)) - return true; - } catch (JSONException e) { - e.printStackTrace(); + for (String value : s) { + JSONObject jsonObj; + try { + jsonObj = new JSONObject(value + "}"); + if (jsonObj.get("id").equals(user)) { + return true; } + } catch (JSONException e) { + e.printStackTrace(); + } } return false; @@ -812,9 +943,10 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider { Feed f = Feed.getFeedById(n); if (f != null) { int groupid = f.getGroupid(); - if(groupid > 0) { + if (groupid > 0) { Group group = Group.getGroupById(groupid); - if(isUserMemberOfGroup(group, owner)) { + assert group != null; + if (isUserMemberOfGroup(group, owner)) { return group.getAuthid(); } } @@ -837,9 +969,10 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider { Subscription s = Subscription.getSubscriptionById(n); if (s != null) { int groupid = s.getGroupid(); - if(groupid > 0) { + if (groupid > 0) { Group group = Group.getGroupById(groupid); - if(isUserMemberOfGroup(group, owner)) { + assert group != null; + if (isUserMemberOfGroup(group, owner)) { return group.getAuthid(); } } @@ -854,7 +987,7 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider { * @Method - setIpAndFqdnForEelf - Rally:US664892 * @Params - method, prints method name in EELF log. */ - protected void setIpAndFqdnForEelf(String method) { + void setIpAndFqdnForEelf(String method) { MDC.clear(); MDC.put(MDC_SERVICE_NAME, method); try { diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/DRFeedsServlet.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/DRFeedsServlet.java index dc9ec3ad..47156d28 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/DRFeedsServlet.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/DRFeedsServlet.java @@ -42,17 +42,18 @@ import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; /** - * This servlet handles provisioning for the <drFeedsURL> which is the URL on the - * provisioning server used to create new feeds. It supports POST to create new feeds, - * and GET to support the Feeds Collection Query function. + * This servlet handles provisioning for the <drFeedsURL> which is the URL on the provisioning server used to + * create new feeds. It supports POST to create new feeds, and GET to support the Feeds Collection Query function. * * @author Robert Eby * @version $Id$ */ @SuppressWarnings("serial") public class DRFeedsServlet extends ProxyServlet { + //Adding EELF Logger Rally:US664892 - private static EELFLogger eelflogger = EELFManager.getInstance().getLogger("org.onap.dmaap.datarouter.provisioning.DRFeedsServlet"); + private static EELFLogger eelflogger = EELFManager.getInstance() + .getLogger("org.onap.dmaap.datarouter.provisioning.DRFeedsServlet"); /** * DELETE on the <drFeedsURL> -- not supported. @@ -60,7 +61,7 @@ public class DRFeedsServlet extends ProxyServlet { @Override public void doDelete(HttpServletRequest req, HttpServletResponse resp) throws IOException { setIpAndFqdnForEelf("doDelete"); - eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(BEHALF_HEADER),getIdFromPath(req)+""); + eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(BEHALF_HEADER), getIdFromPath(req) + ""); String message = "DELETE not allowed for the drFeedsURL."; EventLogRecord elr = new EventLogRecord(req); elr.setMessage(message); @@ -68,15 +69,15 @@ public class DRFeedsServlet extends ProxyServlet { eventlogger.info(elr); resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, message); } + /** - * GET on the <drFeedsURL> -- query the list of feeds already existing in the DB. - * See the <i>Feeds Collection Queries</i> section in the <b>Provisioning API</b> - * document for details on how this method should be invoked. + * GET on the <drFeedsURL> -- query the list of feeds already existing in the DB. See the <i>Feeds Collection + * Queries</i> section in the <b>Provisioning API</b> document for details on how this method should be invoked. */ @Override public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { setIpAndFqdnForEelf("doGet"); - eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(BEHALF_HEADER),getIdFromPath(req)+""); + eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(BEHALF_HEADER), getIdFromPath(req) + ""); EventLogRecord elr = new EventLogRecord(req); String message = isAuthorizedForProvisioning(req); if (message != null) { @@ -92,14 +93,15 @@ public class DRFeedsServlet extends ProxyServlet { } String bhdr = req.getHeader(BEHALF_HEADER); if (bhdr == null) { - message = "Missing "+BEHALF_HEADER+" header."; + message = "Missing " + BEHALF_HEADER + " header."; elr.setMessage(message); elr.setResult(HttpServletResponse.SC_BAD_REQUEST); eventlogger.info(elr); resp.sendError(HttpServletResponse.SC_BAD_REQUEST, message); return; } - String path = req.getRequestURI(); // Note: I think this should be getPathInfo(), but that doesn't work (Jetty bug?) + String path = req + .getRequestURI(); // Note: I think this should be getPathInfo(), but that doesn't work (Jetty bug?) if (path != null && !path.equals("/")) { message = "Bad URL."; elr.setMessage(message); @@ -110,7 +112,7 @@ public class DRFeedsServlet extends ProxyServlet { } // Check with the Authorizer AuthorizationResponse aresp = authz.decide(req); - if (! aresp.isAuthorized()) { + if (!aresp.isAuthorized()) { message = "Policy Engine disallows access."; elr.setMessage(message); elr.setResult(HttpServletResponse.SC_FORBIDDEN); @@ -161,13 +163,14 @@ public class DRFeedsServlet extends ProxyServlet { resp.getOutputStream().print(t); } } + /** * PUT on the <drFeedsURL> -- not supported. */ @Override public void doPut(HttpServletRequest req, HttpServletResponse resp) throws IOException { setIpAndFqdnForEelf("doPut"); - eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(BEHALF_HEADER),getIdFromPath(req)+""); + eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(BEHALF_HEADER), getIdFromPath(req) + ""); String message = "PUT not allowed for the drFeedsURL."; EventLogRecord elr = new EventLogRecord(req); elr.setMessage(message); @@ -175,10 +178,10 @@ public class DRFeedsServlet extends ProxyServlet { eventlogger.info(elr); resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, message); } + /** - * POST on the <drFeedsURL> -- create a new feed. - * See the <i>Creating a Feed</i> section in the <b>Provisioning API</b> - * document for details on how this method should be invoked. + * POST on the <drFeedsURL> -- create a new feed. See the <i>Creating a Feed</i> section in the + * <b>Provisioning API</b> document for details on how this method should be invoked. */ @Override public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { @@ -199,14 +202,15 @@ public class DRFeedsServlet extends ProxyServlet { } String bhdr = req.getHeader(BEHALF_HEADER); if (bhdr == null) { - message = "Missing "+BEHALF_HEADER+" header."; + message = "Missing " + BEHALF_HEADER + " header."; elr.setMessage(message); elr.setResult(HttpServletResponse.SC_BAD_REQUEST); eventlogger.info(elr); resp.sendError(HttpServletResponse.SC_BAD_REQUEST, message); return; } - String path = req.getRequestURI(); // Note: I think this should be getPathInfo(), but that doesn't work (Jetty bug?) + String path = req + .getRequestURI(); // Note: I think this should be getPathInfo(), but that doesn't work (Jetty bug?) if (path != null && !path.equals("/")) { message = "Bad URL."; elr.setMessage(message); @@ -228,7 +232,7 @@ public class DRFeedsServlet extends ProxyServlet { } // Check with the Authorizer AuthorizationResponse aresp = authz.decide(req); - if (! aresp.isAuthorized()) { + if (!aresp.isAuthorized()) { message = "Policy Engine disallows access."; elr.setMessage(message); elr.setResult(HttpServletResponse.SC_FORBIDDEN); @@ -245,10 +249,11 @@ public class DRFeedsServlet extends ProxyServlet { resp.sendError(HttpServletResponse.SC_BAD_REQUEST, message); return; } - if (intlogger.isDebugEnabled()) + if (intlogger.isDebugEnabled()) { intlogger.debug(jo.toString()); - if (++active_feeds > max_feeds) { - active_feeds--; + } + if (++activeFeeds > maxFeeds) { + activeFeeds--; message = "Cannot create feed; the maximum number of feeds has been configured."; elr.setMessage(message); elr.setResult(HttpServletResponse.SC_CONFLICT); diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/FeedLogServlet.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/FeedLogServlet.java index 7ac4bbee..2a1a8424 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/FeedLogServlet.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/FeedLogServlet.java @@ -24,14 +24,15 @@ package org.onap.dmaap.datarouter.provisioning;
/**
- * This servlet handles requests to the <feedLogURL>
- * which are generated by the provisioning server to handle the log query API.
+ * This servlet handles requests to the <feedLogURL> which are generated by the provisioning server to handle the
+ * log query API.
*
* @author Robert Eby
* @version $Id: FeedLogServlet.java,v 1.1 2013/04/26 21:00:24 eby Exp $
*/
@SuppressWarnings("serial")
public class FeedLogServlet extends LogServlet {
+
public FeedLogServlet() {
super(true);
}
diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/FeedServlet.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/FeedServlet.java index d0f529c9..3f8929e7 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/FeedServlet.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/FeedServlet.java @@ -116,7 +116,7 @@ public class FeedServlet extends ProxyServlet { // Delete FEED table entry (set DELETED flag) feed.setDeleted(true); if (doUpdate(feed)) { - active_feeds--; + activeFeeds--; // send response elr.setResult(HttpServletResponse.SC_NO_CONTENT); eventlogger.info(elr); diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/InternalServlet.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/InternalServlet.java index 27192089..61845cef 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/InternalServlet.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/InternalServlet.java @@ -53,81 +53,86 @@ import com.att.eelf.configuration.EELFManager; /** * <p> - * This servlet handles requests to URLs under /internal on the provisioning server. - * These include: + * This servlet handles requests to URLs under /internal on the provisioning server. These include: * </p> * <div class="contentContainer"> * <table class="packageSummary" border="0" cellpadding="3" cellspacing="0"> * <caption><span>URL Path Summary</span><span class="tabEnd"> </span></caption> * <tr> - * <th class="colFirst" width="15%">URL Path</th> - * <th class="colOne">Method</th> - * <th class="colLast">Purpose</th> + * <th class="colFirst" width="15%">URL Path</th> + * <th class="colOne">Method</th> + * <th class="colLast">Purpose</th> * </tr> * <tr class="altColor"> - * <td class="colFirst">/internal/prov</td> - * <td class="colOne">GET</td> - * <td class="colLast">used to GET a full JSON copy of the provisioning data.</td> + * <td class="colFirst">/internal/prov</td> + * <td class="colOne">GET</td> + * <td class="colLast">used to GET a full JSON copy of the provisioning data.</td> * </tr> * <tr class="rowColor"> - * <td class="colFirst">/internal/fetchProv</td> - * <td class="colOne">GET</td> - * <td class="colLast">used to signal to a standby POD that the provisioning data should be fetched from the active POD.</td> + * <td class="colFirst">/internal/fetchProv</td> + * <td class="colOne">GET</td> + * <td class="colLast">used to signal to a standby POD that the provisioning data should be fetched from the active + * POD.</td> * </tr> * <tr class="altColor"> - * <td class="colFirst" rowspan="2">/internal/logs</td> - * <td class="colOne">GET</td> - * <td class="colLast">used to GET an index of log files and individual logs for this provisioning server.</td> + * <td class="colFirst" rowspan="2">/internal/logs</td> + * <td class="colOne">GET</td> + * <td class="colLast">used to GET an index of log files and individual logs for this provisioning server.</td> * </tr> * <tr class="altColor"> - * <td class="colOne">POST</td> - * <td class="colLast">used to POST log files from the individual nodes to this provisioning server.</td> + * <td class="colOne">POST</td> + * <td class="colLast">used to POST log files from the individual nodes to this provisioning server.</td> * </tr> * <tr class="rowColor"> - * <td class="colFirst" rowspan="4">/internal/api</td> - * <td class="colOne">GET</td> - * <td class="colLast">used to GET an individual parameter value. The parameter name is specified by the path after /api/.</td> + * <td class="colFirst" rowspan="4">/internal/api</td> + * <td class="colOne">GET</td> + * <td class="colLast">used to GET an individual parameter value. The parameter name is specified by the path after + * /api/.</td> * </tr> * <tr class="rowColor"> - * <td class="colOne">PUT</td> - * <td class="colLast">used to set an individual parameter value. The parameter name is specified by the path after /api/.</td> + * <td class="colOne">PUT</td> + * <td class="colLast">used to set an individual parameter value. The parameter name is specified by the path after + * /api/.</td> * </tr> * <tr class="rowColor"> - * <td class="colOne">DELETE</td> - * <td class="colLast">used to remove an individual parameter value. The parameter name is specified by the path after /api/.</td> + * <td class="colOne">DELETE</td> + * <td class="colLast">used to remove an individual parameter value. The parameter name is specified by the path after + * /api/.</td> * </tr> * <tr class="rowColor"> - * <td class="colOne">POST</td> - * <td class="colLast">used to create a new individual parameter value. The parameter name is specified by the path after /api/.</td> + * <td class="colOne">POST</td> + * <td class="colLast">used to create a new individual parameter value. The parameter name is specified by the path + * after /api/.</td> * </tr> * <tr class="altColor"> - * <td class="colFirst">/internal/halt</td> - * <td class="colOne">GET</td> - * <td class="colLast">used to halt the server (must be accessed from 127.0.0.1).</td> + * <td class="colFirst">/internal/halt</td> + * <td class="colOne">GET</td> + * <td class="colLast">used to halt the server (must be accessed from 127.0.0.1).</td> * </tr> * <tr class="rowColor"> - * <td class="colFirst" rowspan="2">/internal/drlogs</td> - * <td class="colOne">GET</td> - * <td class="colLast">used to get a list of DR log entries available for retrieval. - * Note: these are the actual data router log entries sent to the provisioning server - * by the nodes, not the provisioning server's internal logs (access via /internal/logs above). - * The range is returned as a list of record sequence numbers.</td> + * <td class="colFirst" rowspan="2">/internal/drlogs</td> + * <td class="colOne">GET</td> + * <td class="colLast">used to get a list of DR log entries available for retrieval. + * Note: these are the actual data router log entries sent to the provisioning server by the nodes, not the provisioning + * server's internal logs (access via /internal/logs above). The range is returned as a list of record sequence + * numbers.</td> * </tr> * <tr class="rowColor"> - * <td class="colOne">POST</td> - * <td class="colLast">used to retrieve specific log entries. - * The sequence numbers of the records to fetch are POST-ed; the records matching the sequence numbers are returned.</td> + * <td class="colOne">POST</td> + * <td class="colLast">used to retrieve specific log entries. + * The sequence numbers of the records to fetch are POST-ed; the records matching the sequence numbers are + * returned.</td> * </tr> * <tr class="altColor"> - * <td class="colFirst">/internal/route/*</td> - * <td class="colOne">*</td> - * <td class="colLast">URLs under this path are handled via the {@link org.onap.dmaap.datarouter.provisioning.RouteServlet}</td> + * <td class="colFirst">/internal/route/*</td> + * <td class="colOne">*</td> + * <td class="colLast">URLs under this path are handled via the {@link org.onap.dmaap.datarouter.provisioning.RouteServlet}</td> * </tr> * </table> * </div> * <p> - * Authorization to use these URLs is a little different than for other URLs on the provisioning server. - * For the most part, the IP address that the request comes from should be either: + * Authorization to use these URLs is a little different than for other URLs on the provisioning server. For the most + * part, the IP address that the request comes from should be either: * </p> * <ol> * <li>an IP address of a provisioning server, or</li> @@ -139,8 +144,8 @@ import com.att.eelf.configuration.EELFManager; * In addition, requests to /internal/halt can ONLY come from localhost (127.0.0.1) on the HTTP port. * </p> * <p> - * All DELETE/GET/PUT/POST requests made to /internal/api on this servlet on the standby server are - * proxied to the active server (using the {@link ProxyServlet}) if it is up and reachable. + * All DELETE/GET/PUT/POST requests made to /internal/api on this servlet on the standby server are proxied to the + * active server (using the {@link ProxyServlet}) if it is up and reachable. * </p> * * @author Robert Eby @@ -148,18 +153,20 @@ import com.att.eelf.configuration.EELFManager; */ @SuppressWarnings("serial") public class InternalServlet extends ProxyServlet { - private static Integer logseq = new Integer(0); // another piece of info to make log spool file names unique + + private static Integer logseq = 0; // another piece of info to make log spool file names unique //Adding EELF Logger Rally:US664892 - private static EELFLogger eelflogger = EELFManager.getInstance().getLogger("org.onap.dmaap.datarouter.provisioning.InternalServlet"); + private static EELFLogger eelflogger = EELFManager.getInstance() + .getLogger("org.onap.dmaap.datarouter.provisioning.InternalServlet"); /** - * Delete a parameter at the address /internal/api/<parameter>. - * See the <b>Internal API</b> document for details on how this method should be invoked. + * Delete a parameter at the address /internal/api/<parameter>. See the <b>Internal API</b> document for + * details on how this method should be invoked. */ @Override public void doDelete(HttpServletRequest req, HttpServletResponse resp) throws IOException { setIpAndFqdnForEelf("doDelete"); - eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(BEHALF_HEADER),getIdFromPath(req)+""); + eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(BEHALF_HEADER), getIdFromPath(req) + ""); EventLogRecord elr = new EventLogRecord(req); if (!isAuthorizedForInternal(req)) { elr.setMessage("Unauthorized."); @@ -197,14 +204,15 @@ public class InternalServlet extends ProxyServlet { } resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Bad URL."); } + /** - * Get some information (such as a parameter) underneath the /internal/ namespace. - * See the <b>Internal API</b> document for details on how this method should be invoked. + * Get some information (such as a parameter) underneath the /internal/ namespace. See the <b>Internal API</b> + * document for details on how this method should be invoked. */ @Override public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { setIpAndFqdnForEelf("doGet"); - eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(BEHALF_HEADER),getIdFromPath(req)+""); + eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(BEHALF_HEADER), getIdFromPath(req) + ""); String path = req.getPathInfo(); if (path.equals("/halt") && !req.isSecure()) { // request to halt the server - can ONLY come from localhost @@ -214,7 +222,7 @@ public class InternalServlet extends ProxyServlet { resp.setStatus(HttpServletResponse.SC_OK); Main.shutdown(); } else { - intlogger.info("PROV0010 Disallowed request to HALT received from "+remote); + intlogger.info("PROV0010 Disallowed request to HALT received from " + remote); resp.setStatus(HttpServletResponse.SC_FORBIDDEN); } return; @@ -237,8 +245,9 @@ public class InternalServlet extends ProxyServlet { } if (path.equals("/prov")) { if (isProxyOK(req) && isProxyServer()) { - if (super.doGetWithFallback(req, resp)) + if (super.doGetWithFallback(req, resp)) { return; + } // fall back to returning the local data if the remote is unreachable intlogger.info("Active server unavailable; falling back to local copy."); } @@ -297,14 +306,15 @@ public class InternalServlet extends ProxyServlet { } resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Bad URL."); } + /** - * Modify a parameter at the address /internal/api/<parameter>. - * See the <b>Internal API</b> document for details on how this method should be invoked. + * Modify a parameter at the address /internal/api/<parameter>. See the <b>Internal API</b> document for + * details on how this method should be invoked. */ @Override public void doPut(HttpServletRequest req, HttpServletResponse resp) throws IOException { setIpAndFqdnForEelf("doPut"); - eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(BEHALF_HEADER),getIdFromPath(req)+""); + eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(BEHALF_HEADER), getIdFromPath(req) + ""); EventLogRecord elr = new EventLogRecord(req); if (!isAuthorizedForInternal(req)) { elr.setMessage("Unauthorized."); @@ -343,9 +353,10 @@ public class InternalServlet extends ProxyServlet { } resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Bad URL."); } + /** - * Create some new information (such as a parameter or log entries) underneath the /internal/ namespace. - * See the <b>Internal API</b> document for details on how this method should be invoked. + * Create some new information (such as a parameter or log entries) underneath the /internal/ namespace. See the + * <b>Internal API</b> document for details on how this method should be invoked. */ @SuppressWarnings("resource") @Override @@ -394,7 +405,7 @@ public class InternalServlet extends ProxyServlet { String ctype = req.getHeader("Content-Type"); if (ctype == null || !ctype.equals("text/plain")) { elr.setResult(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE); - elr.setMessage("Bad media type: "+ctype); + elr.setMessage("Bad media type: " + ctype); resp.setStatus(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE); eventlogger.info(elr); return; @@ -421,11 +432,14 @@ public class InternalServlet extends ProxyServlet { FileSystem fs = (Paths.get(spooldir)).getFileSystem(); long total = 0; long avail = 0; - for (FileStore store: fs.getFileStores()) { + for (FileStore store : fs.getFileStores()) { total += store.getTotalSpace(); avail += store.getUsableSpace(); } - try { fs.close(); } catch (Exception e) { } + try { + fs.close(); + } catch (Exception e) { + } if (((avail * 100) / total) < 5) { elr.setResult(HttpServletResponse.SC_SERVICE_UNAVAILABLE); resp.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE); @@ -433,7 +447,7 @@ public class InternalServlet extends ProxyServlet { return; } Path tmppath = Paths.get(spooldir, spoolname); - Path donepath = Paths.get(spooldir, "IN."+spoolname); + Path donepath = Paths.get(spooldir, "IN." + spoolname); Files.copy(req.getInputStream(), Paths.get(spooldir, spoolname), StandardCopyOption.REPLACE_EXISTING); Files.move(tmppath, donepath, StandardCopyOption.REPLACE_EXISTING); elr.setResult(HttpServletResponse.SC_CREATED); @@ -448,7 +462,7 @@ public class InternalServlet extends ProxyServlet { String ctype = req.getHeader("Content-Type"); if (ctype == null || !ctype.equals("text/plain")) { elr.setResult(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE); - elr.setMessage("Bad media type: "+ctype); + elr.setMessage("Bad media type: " + ctype); resp.setStatus(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE); eventlogger.info(elr); return; @@ -456,8 +470,9 @@ public class InternalServlet extends ProxyServlet { InputStream is = req.getInputStream(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); int ch = 0; - while ((ch = is.read()) >= 0) + while ((ch = is.read()) >= 0) { bos.write(ch); + } RLEBitSet bs = new RLEBitSet(bos.toString()); // The set of records to retrieve elr.setResult(HttpServletResponse.SC_OK); resp.setStatus(HttpServletResponse.SC_OK); @@ -484,6 +499,7 @@ public class InternalServlet extends ProxyServlet { } return sb.toString(); } + private JSONArray generateLogfileList() { JSONArray ja = new JSONArray(); Properties p = (new DB()).getProperties(); @@ -495,8 +511,9 @@ public class InternalServlet extends ProxyServlet { String[] list = f.list(); if (list != null) { for (String s2 : list) { - if (!s2.startsWith(".")) + if (!s2.startsWith(".")) { ja.put(s2); + } } } } diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/Main.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/Main.java index 3e3f45fe..3afce990 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/Main.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/Main.java @@ -55,14 +55,14 @@ import javax.servlet.DispatcherType; /** * <p> - * A main class which may be used to start the provisioning server with an "embedded" Jetty server. - * Configuration is done via the properties file <i>provserver.properties</i>, which should be in the CLASSPATH. - * The provisioning server may also be packaged with a web.xml and started as a traditional webapp. + * A main class which may be used to start the provisioning server with an "embedded" Jetty server. Configuration is + * done via the properties file <i>provserver.properties</i>, which should be in the CLASSPATH. The provisioning server + * may also be packaged with a web.xml and started as a traditional webapp. * </p> * <p> - * Most of the work of the provisioning server is carried out within the eight servlets (configured below) - * that are used to handle each of the eight types of requests the server may receive. - * In addition, there are background threads started to perform other tasks: + * Most of the work of the provisioning server is carried out within the eight servlets (configured below) that are used + * to handle each of the eight types of requests the server may receive. In addition, there are background threads + * started to perform other tasks: * </p> * <ul> * <li>One background Thread runs the {@link LogfileLoader} in order to process incoming logfiles. @@ -75,14 +75,15 @@ import javax.servlet.DispatcherType; * /opt/app/datartr/logs directory.</li> * </ul> * <p> - * The provisioning server is stopped by issuing a GET to the URL http://127.0.0.1/internal/halt - * using <i>curl</i> or some other such tool. + * The provisioning server is stopped by issuing a GET to the URL http://127.0.0.1/internal/halt using <i>curl</i> or + * some other such tool. * </p> * * @author Robert Eby * @version $Id: Main.java,v 1.12 2014/03/12 19:45:41 eby Exp $ */ public class Main { + /** * The truststore to use if none is specified */ @@ -109,8 +110,9 @@ public class Main { Logger logger = Logger.getLogger("org.onap.dmaap.datarouter.provisioning.internal"); // Check DB is accessible and contains the expected tables - if (!checkDatabase(logger)) + if (!checkDatabase(logger)) { System.exit(1); + } logger.info("PROV0000 **** AT&T Data Router Provisioning Server starting...."); @@ -139,101 +141,109 @@ public class Main { server = new Server(queuedThreadPool); // HTTP connector - ServerConnector httpServerConnector = new ServerConnector(server, new HttpConnectionFactory(httpConfiguration)); - httpServerConnector.setPort(httpPort); - httpServerConnector.setAcceptQueueSize(2); - - // HTTPS configuration - HttpConfiguration httpsConfiguration = new HttpConfiguration(httpConfiguration); - httpsConfiguration.setRequestHeaderSize(8192); - - // HTTPS connector - SslContextFactory sslContextFactory = new SslContextFactory(); - sslContextFactory.setKeyStorePath(p.getProperty(KEYSTORE_PATH_PROPERTY)); - sslContextFactory.setKeyStorePassword(p.getProperty(KEYSTORE_PASSWORD_PROPERTY)); - sslContextFactory.setKeyManagerPassword(p.getProperty("org.onap.dmaap.datarouter.provserver.keymanager.password")); - // SSL stuff - /* Skip SSLv3 Fixes */ - sslContextFactory.addExcludeProtocols("SSLv3"); - logger.info("Excluded protocols prov-" + sslContextFactory.getExcludeProtocols()); - /* End of SSLv3 Fixes */ - - ServerConnector httpsServerConnector = new ServerConnector(server, - new SslConnectionFactory(sslContextFactory,HttpVersion.HTTP_1_1.asString()), + HandlerCollection hc; + try (ServerConnector httpServerConnector = new ServerConnector(server, + new HttpConnectionFactory(httpConfiguration))) { + httpServerConnector.setPort(httpPort); + httpServerConnector.setAcceptQueueSize(2); + + // HTTPS configuration + HttpConfiguration httpsConfiguration = new HttpConfiguration(httpConfiguration); + httpsConfiguration.setRequestHeaderSize(8192); + + // HTTPS connector + SslContextFactory sslContextFactory = new SslContextFactory(); + sslContextFactory.setKeyStorePath(p.getProperty(KEYSTORE_PATH_PROPERTY)); + sslContextFactory.setKeyStorePassword(p.getProperty(KEYSTORE_PASSWORD_PROPERTY)); + sslContextFactory + .setKeyManagerPassword(p.getProperty("org.onap.dmaap.datarouter.provserver.keymanager.password")); + // SSL stuff + /* Skip SSLv3 Fixes */ + sslContextFactory.addExcludeProtocols("SSLv3"); + logger.info("Excluded protocols prov-" + Arrays.toString(sslContextFactory.getExcludeProtocols())); + /* End of SSLv3 Fixes */ + + ServerConnector httpsServerConnector = new ServerConnector(server, + new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()), new HttpConnectionFactory(httpsConfiguration)); - httpsServerConnector.setPort(httpsPort); - httpsServerConnector.setIdleTimeout(30000); - httpsServerConnector.setAcceptQueueSize(2); - - sslContextFactory.setKeyStoreType(p.getProperty(KEYSTORE_TYPE_PROPERTY, "jks")); - sslContextFactory.setKeyStorePath(p.getProperty(KEYSTORE_PATH_PROPERTY)); - sslContextFactory.setKeyStorePassword(p.getProperty(KEYSTORE_PASSWORD_PROPERTY)); - sslContextFactory.setKeyManagerPassword(p.getProperty("org.onap.dmaap.datarouter.provserver.keymanager.password")); - - String ts = p.getProperty(TRUSTSTORE_PATH_PROPERTY); - if (ts != null && ts.length() > 0) { - System.out.println("@@ TS -> " + ts); - sslContextFactory.setTrustStorePath(ts); - sslContextFactory.setTrustStorePassword(p.getProperty(TRUSTSTORE_PASSWORD_PROPERTY)); - } else { - sslContextFactory.setTrustStorePath(DEFAULT_TRUSTSTORE); + httpsServerConnector.setPort(httpsPort); + httpsServerConnector.setIdleTimeout(30000); + httpsServerConnector.setAcceptQueueSize(2); + + sslContextFactory.setKeyStoreType(p.getProperty(KEYSTORE_TYPE_PROPERTY, "jks")); + sslContextFactory.setKeyStorePath(p.getProperty(KEYSTORE_PATH_PROPERTY)); + sslContextFactory.setKeyStorePassword(p.getProperty(KEYSTORE_PASSWORD_PROPERTY)); + sslContextFactory + .setKeyManagerPassword(p.getProperty("org.onap.dmaap.datarouter.provserver.keymanager.password")); + + String ts = p.getProperty(TRUSTSTORE_PATH_PROPERTY); + if (ts != null && ts.length() > 0) { + System.out.println("@@ TS -> " + ts); + sslContextFactory.setTrustStorePath(ts); + sslContextFactory.setTrustStorePassword(p.getProperty(TRUSTSTORE_PASSWORD_PROPERTY)); + } else { + sslContextFactory.setTrustStorePath(DEFAULT_TRUSTSTORE); + sslContextFactory.setTrustStorePassword("changeit"); + } + sslContextFactory.setTrustStorePath("/opt/app/datartr/self_signed/cacerts.jks"); sslContextFactory.setTrustStorePassword("changeit"); - } - sslContextFactory.setTrustStorePath("/opt/app/datartr/self_signed/cacerts.jks"); - sslContextFactory.setTrustStorePassword("changeit"); - sslContextFactory.setWantClientAuth(true); - - // Servlet and Filter configuration - ServletContextHandler ctxt = new ServletContextHandler(0); - ctxt.setContextPath("/"); - ctxt.addServlet(new ServletHolder(new FeedServlet()), "/feed/*"); - ctxt.addServlet(new ServletHolder(new FeedLogServlet()), "/feedlog/*"); - ctxt.addServlet(new ServletHolder(new PublishServlet()), "/publish/*"); - ctxt.addServlet(new ServletHolder(new SubscribeServlet()), "/subscribe/*"); - ctxt.addServlet(new ServletHolder(new StatisticsServlet()), "/statistics/*"); - ctxt.addServlet(new ServletHolder(new SubLogServlet()), "/sublog/*"); - ctxt.addServlet(new ServletHolder(new GroupServlet()), "/group/*"); //Provision groups - Rally US708115 -1610 - ctxt.addServlet(new ServletHolder(new SubscriptionServlet()), "/subs/*"); - ctxt.addServlet(new ServletHolder(new InternalServlet()), "/internal/*"); - ctxt.addServlet(new ServletHolder(new RouteServlet()), "/internal/route/*"); - ctxt.addServlet(new ServletHolder(new DRFeedsServlet()), "/"); - ctxt.addFilter(new FilterHolder(new ThrottleFilter()), "/publish/*", EnumSet.of(DispatcherType.REQUEST)); - - ContextHandlerCollection contexts = new ContextHandlerCollection(); - contexts.addHandler(ctxt); - - // Request log configuration - NCSARequestLog nrl = new NCSARequestLog(); - nrl.setFilename(p.getProperty("org.onap.dmaap.datarouter.provserver.accesslog.dir") + "/request.log.yyyy_mm_dd"); - nrl.setFilenameDateFormat("yyyyMMdd"); - nrl.setRetainDays(90); - nrl.setAppend(true); - nrl.setExtended(false); - nrl.setLogCookies(false); - nrl.setLogTimeZone("GMT"); - - RequestLogHandler reqlog = new RequestLogHandler(); - reqlog.setRequestLog(nrl); - - // Server's Handler collection - HandlerCollection hc = new HandlerCollection(); - hc.setHandlers(new Handler[]{contexts, new DefaultHandler()}); - hc.addHandler(reqlog); - - // Daemon to clean up the log directory on a daily basis - Timer rolex = new Timer(); - rolex.scheduleAtFixedRate(new PurgeLogDirTask(), 0, 86400000L); // run once per day - - // Start LogfileLoader - LogfileLoader.getLoader(); - - ServerConnector serverConnector = new ServerConnector(server, - new SslConnectionFactory(sslContextFactory,HttpVersion.HTTP_1_1.asString()), - new HttpConnectionFactory(httpsConfiguration)); - serverConnector.setPort(httpsPort); - serverConnector.setIdleTimeout(500000); + sslContextFactory.setWantClientAuth(true); + + // Servlet and Filter configuration + ServletContextHandler ctxt = new ServletContextHandler(0); + ctxt.setContextPath("/"); + ctxt.addServlet(new ServletHolder(new FeedServlet()), "/feed/*"); + ctxt.addServlet(new ServletHolder(new FeedLogServlet()), "/feedlog/*"); + ctxt.addServlet(new ServletHolder(new PublishServlet()), "/publish/*"); + ctxt.addServlet(new ServletHolder(new SubscribeServlet()), "/subscribe/*"); + ctxt.addServlet(new ServletHolder(new StatisticsServlet()), "/statistics/*"); + ctxt.addServlet(new ServletHolder(new SubLogServlet()), "/sublog/*"); + ctxt.addServlet(new ServletHolder(new GroupServlet()), + "/group/*"); //Provision groups - Rally US708115 -1610 + ctxt.addServlet(new ServletHolder(new SubscriptionServlet()), "/subs/*"); + ctxt.addServlet(new ServletHolder(new InternalServlet()), "/internal/*"); + ctxt.addServlet(new ServletHolder(new RouteServlet()), "/internal/route/*"); + ctxt.addServlet(new ServletHolder(new DRFeedsServlet()), "/"); + ctxt.addFilter(new FilterHolder(new ThrottleFilter()), "/publish/*", EnumSet.of(DispatcherType.REQUEST)); + + ContextHandlerCollection contexts = new ContextHandlerCollection(); + contexts.addHandler(ctxt); + + // Request log configuration + NCSARequestLog nrl = new NCSARequestLog(); + nrl.setFilename( + p.getProperty("org.onap.dmaap.datarouter.provserver.accesslog.dir") + "/request.log.yyyy_mm_dd"); + nrl.setFilenameDateFormat("yyyyMMdd"); + nrl.setRetainDays(90); + nrl.setAppend(true); + nrl.setExtended(false); + nrl.setLogCookies(false); + nrl.setLogTimeZone("GMT"); + + RequestLogHandler reqlog = new RequestLogHandler(); + reqlog.setRequestLog(nrl); + + // Server's Handler collection + hc = new HandlerCollection(); + hc.setHandlers(new Handler[]{contexts, new DefaultHandler()}); + hc.addHandler(reqlog); + + // Daemon to clean up the log directory on a daily basis + Timer rolex = new Timer(); + rolex.scheduleAtFixedRate(new PurgeLogDirTask(), 0, 86400000L); // run once per day + + // Start LogfileLoader + LogfileLoader.getLoader(); + + try (ServerConnector serverConnector = new ServerConnector(server, + new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()), + new HttpConnectionFactory(httpsConfiguration))) { + serverConnector.setPort(httpsPort); + serverConnector.setIdleTimeout(500000); + } - server.setConnectors(new Connector[]{httpServerConnector, httpsServerConnector}); + server.setConnectors(new Connector[]{httpServerConnector, httpsServerConnector}); + } server.setHandler(hc); server.setStopAtShutdown(true); server.setStopTimeout(5000); diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/Poker.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/Poker.java index 4250624b..f2c511c1 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/Poker.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/Poker.java @@ -23,9 +23,7 @@ package org.onap.dmaap.datarouter.provisioning; -import java.io.FileInputStream; import java.io.IOException; -import java.io.InputStream; import java.net.HttpURLConnection; import java.net.InetAddress; import java.net.MalformedURLException; @@ -34,14 +32,11 @@ import java.net.UnknownHostException; import java.util.Arrays; import java.util.HashSet; import java.util.Map; -import java.util.Properties; import java.util.Set; import java.util.Timer; import java.util.TimerTask; import java.util.TreeSet; -import javax.servlet.ServletException; - import org.apache.log4j.Logger; import org.json.JSONException; import org.json.JSONObject; @@ -56,84 +51,92 @@ import org.onap.dmaap.datarouter.provisioning.beans.Subscription; import org.onap.dmaap.datarouter.provisioning.utils.*; /** - * This class handles the two timers (described in R1 Design Notes), and takes care of issuing - * the GET to each node of the URL to "poke". + * This class handles the two timers (described in R1 Design Notes), and takes care of issuing the GET to each node of + * the URL to "poke". * * @author Robert Eby * @version $Id: Poker.java,v 1.11 2014/01/08 16:13:47 eby Exp $ */ public class Poker extends TimerTask { - /** Template used to generate the URL to issue the GET against */ - public static final String POKE_URL_TEMPLATE = "http://%s/internal/fetchProv"; - - + /** + * Template used to generate the URL to issue the GET against + */ + private static final String POKE_URL_TEMPLATE = "http://%s/internal/fetchProv"; - /** This is a singleton -- there is only one Poker object in the server */ - private static Poker p; + /** + * This is a singleton -- there is only one Poker object in the server + */ + private static Poker poker; /** * Get the singleton Poker object. + * * @return the Poker */ public static synchronized Poker getPoker() { - if (p == null) - p = new Poker(); - return p; + if (poker == null) { + poker = new Poker(); + } + return poker; } private long timer1; private long timer2; - private Timer rolex; - private String this_pod; // DNS name of this machine + private String thisPod; // DNS name of this machine private Logger logger; - private String provstring; + private String provString; private Poker() { timer1 = timer2 = 0; - rolex = new Timer(); + Timer rolex = new Timer(); logger = Logger.getLogger("org.onap.dmaap.datarouter.provisioning.internal"); try { - this_pod = InetAddress.getLocalHost().getHostName(); + thisPod = InetAddress.getLocalHost().getHostName(); } catch (UnknownHostException e) { - this_pod = "*UNKNOWN*"; // not a major problem + thisPod = "*UNKNOWN*"; // not a major problem } - provstring = buildProvisioningString(); + provString = buildProvisioningString(); rolex.scheduleAtFixedRate(this, 0L, 1000L); // Run once a second to check the timers } /** * This method sets the two timers described in the design notes. - * @param t1 the first timer controls how long to wait after a provisioning request before poking each node - * This timer can be reset if it has not "gone off". + * + * @param t1 the first timer controls how long to wait after a provisioning request before poking each node This + * timer can be reset if it has not "gone off". * @param t2 the second timer set the outer bound on how long to wait. It cannot be reset. */ public void setTimers(long t1, long t2) { - synchronized (this_pod) { - if (timer1 == 0 || t1 > timer1) + synchronized (thisPod) { + if (timer1 == 0 || t1 > timer1) { timer1 = t1; - if (timer2 == 0) + } + if (timer2 == 0) { timer2 = t2; + } } - if (logger.isDebugEnabled()) + if (logger.isDebugEnabled()) { logger.debug("Poker timers set to " + timer1 + " and " + timer2); + } } /** * Return the last provisioning string built. + * * @return the last provisioning string built. */ public String getProvisioningString() { - return provstring; + return provString; } /** - * The method to run at the predefined interval (once per second). This method checks - * to see if either of the two timers has expired, and if so, will rebuild the provisioning - * string, and poke all the nodes and other PODs. The timers are then reset to 0. + * The method to run at the predefined interval (once per second). This method checks to see if either of the two + * timers has expired, and if so, will rebuild the provisioning string, and poke all the nodes and other PODs. The + * timers are then reset to 0. */ @Override public void run() { @@ -141,62 +144,61 @@ public class Poker extends TimerTask { if (timer1 > 0) { long now = System.currentTimeMillis(); boolean fire = false; - synchronized (this_pod) { + synchronized (thisPod) { if (now > timer1 || now > timer2) { timer1 = timer2 = 0; fire = true; } } if (fire) { - // Rebuild the prov string - provstring = buildProvisioningString(); - - // Only the active POD should poke nodes, etc. - boolean active = SynchronizerTask.getSynchronizer().isActive(); - if (active) { - // Poke all the DR nodes - for (String n : BaseServlet.getNodes()) { - pokeNode(n); - } - // Poke the pod that is not us - for (String n : BaseServlet.getPods()) { - if (n.length() > 0 && !n.equals(this_pod)) - pokeNode(n); - } - } + pokeNodes(); } } } catch (Exception e) { - logger.warn("PROV0020: Caught exception in Poker: "+e); + logger.warn("PROV0020: Caught exception in Poker: " + e); e.printStackTrace(); } } - private void pokeNode(final String nodename) { - logger.debug("PROV0012 Poking node " + nodename + " ..."); - Runnable r = new Runnable() { - @Override - public void run() { - try { - String u = String.format(POKE_URL_TEMPLATE, nodename+":"+DB.HTTP_PORT); - URL url = new URL(u); - HttpURLConnection conn = (HttpURLConnection) url.openConnection(); - conn.setConnectTimeout(60000); //Fixes for Itrack DATARTR-3, poke timeout - conn.connect(); - conn.getContentLength(); // Force the GET through - conn.disconnect(); - } catch (MalformedURLException e) { - logger.warn("PROV0013 MalformedURLException Error poking node "+nodename+": " + e.getMessage()); - } catch (IOException e) { - logger.warn("PROV0013 IOException Error poking node "+nodename+": " + e.getMessage()); + private void pokeNodes() { + // Rebuild the prov string + provString = buildProvisioningString(); + // Only the active POD should poke nodes, etc. + boolean active = SynchronizerTask.getSynchronizer().isActive(); + if (active) { + // Poke all the DR nodes + for (String n : BaseServlet.getNodes()) { + pokeNode(n); + } + // Poke the pod that is not us + for (String n : BaseServlet.getPods()) { + if (n.length() > 0 && !n.equals(thisPod)) { + pokeNode(n); } } + } + } + + private void pokeNode(final String nodename) { + logger.debug("PROV0012 Poking node " + nodename + " ..."); + String nodeUrl = String.format(POKE_URL_TEMPLATE, nodename + ":" + DB.HTTP_PORT); + Runnable r = () -> { + try { + URL url = new URL(nodeUrl); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn.setConnectTimeout(60000); //Fixes for Itrack DATARTR-3, poke timeout + conn.connect(); + conn.getContentLength(); // Force the GET through + conn.disconnect(); + } catch (MalformedURLException e) { + logger.warn("PROV0013 MalformedURLException Error poking node at " + nodeUrl + " : " + e.getMessage()); + } catch (IOException e) { + logger.warn("PROV0013 IOException Error poking node at " + nodeUrl + " : " + e.getMessage()); + } }; -// Thread t = new Thread(r); -// t.start(); r.run(); } - @SuppressWarnings("unused") + private String buildProvisioningString() { StringBuilder sb = new StringBuilder("{\n"); @@ -220,14 +222,14 @@ public class Poker extends TimerTask { } sb.append("\n],\n"); - // Append Subscriptions to the string pfx = "\n"; sb.append("\"subscriptions\": ["); for (Subscription s : Subscription.getAllSubscriptions()) { sb.append(pfx); - if(s!=null) - sb.append(s.asJSONObject().toString()); + if (s != null) { + sb.append(s.asJSONObject().toString()); + } pfx = ",\n"; } sb.append("\n],\n"); @@ -235,11 +237,12 @@ public class Poker extends TimerTask { // Append Parameters to the string pfx = "\n"; sb.append("\"parameters\": {"); - Map<String,String> props = Parameters.getParameters(); + Map<String, String> props = Parameters.getParameters(); Set<String> ivals = new HashSet<String>(); String intv = props.get("_INT_VALUES"); - if (intv != null) + if (intv != null) { ivals.addAll(Arrays.asList(intv.split("\\|"))); + } for (String key : new TreeSet<String>(props.keySet())) { String v = props.get(key); sb.append(pfx); @@ -279,7 +282,7 @@ public class Poker extends TimerTask { for (EgressRoute eg : EgressRoute.getAllEgressRoutes()) { sb.append(pfx); String t = eg.asJSONObject().toString(); - t = t.substring(1, t.length()-1); + t = t.substring(1, t.length() - 1); sb.append(t); pfx = ",\n"; } @@ -296,14 +299,15 @@ public class Poker extends TimerTask { sb.append("\n}"); // Convert to string and verify it is valid JSON - String provstring = sb.toString(); + String tempProvString = sb.toString(); try { - new JSONObject(new JSONTokener(provstring)); + new JSONObject(new JSONTokener(tempProvString)); } catch (JSONException e) { - logger.warn("PROV0016: Possible invalid prov string: "+e); + logger.warn("PROV0016: Possible invalid prov string: " + e); } - return provstring; + return tempProvString; } + private String quote(String s) { StringBuilder sb = new StringBuilder(); for (char ch : s.toCharArray()) { diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/SubscribeServlet.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/SubscribeServlet.java index 3ad22427..21d391e5 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/SubscribeServlet.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/SubscribeServlet.java @@ -43,8 +43,8 @@ import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; /** - * This servlet handles provisioning for the <subscribeURL> which is generated by the provisioning - * server to handle the creation and inspection of subscriptions to a specific feed. + * This servlet handles provisioning for the <subscribeURL> which is generated by the provisioning server to + * handle the creation and inspection of subscriptions to a specific feed. * * @author Robert Eby * @version $Id$ @@ -53,7 +53,8 @@ import com.att.eelf.configuration.EELFManager; public class SubscribeServlet extends ProxyServlet { //Adding EELF Logger Rally:US664892 - private static EELFLogger eelflogger = EELFManager.getInstance().getLogger("org.onap.dmaap.datarouter.provisioning.SubscribeServlet"); + private static EELFLogger eelflogger = EELFManager.getInstance() + .getLogger("org.onap.dmaap.datarouter.provisioning.SubscribeServlet"); /** * DELETE on the <subscribeUrl> -- not supported. @@ -61,7 +62,7 @@ public class SubscribeServlet extends ProxyServlet { @Override public void doDelete(HttpServletRequest req, HttpServletResponse resp) throws IOException { setIpAndFqdnForEelf("doDelete"); - eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_SUBID, req.getHeader(BEHALF_HEADER),getIdFromPath(req)+""); + eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_SUBID, req.getHeader(BEHALF_HEADER), getIdFromPath(req) + ""); String message = "DELETE not allowed for the subscribeURL."; EventLogRecord elr = new EventLogRecord(req); elr.setMessage(message); @@ -69,15 +70,15 @@ public class SubscribeServlet extends ProxyServlet { eventlogger.info(elr); resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, message); } + /** - * GET on the <subscribeUrl> -- get the list of subscriptions to a feed. - * See the <i>Subscription Collection Query</i> section in the <b>Provisioning API</b> - * document for details on how this method should be invoked. + * GET on the <subscribeUrl> -- get the list of subscriptions to a feed. See the <i>Subscription Collection + * Query</i> section in the <b>Provisioning API</b> document for details on how this method should be invoked. */ @Override public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { setIpAndFqdnForEelf("doGet"); - eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_SUBID, req.getHeader(BEHALF_HEADER),getIdFromPath(req)+""); + eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_SUBID, req.getHeader(BEHALF_HEADER), getIdFromPath(req) + ""); EventLogRecord elr = new EventLogRecord(req); String message = isAuthorizedForProvisioning(req); if (message != null) { @@ -93,7 +94,7 @@ public class SubscribeServlet extends ProxyServlet { } String bhdr = req.getHeader(BEHALF_HEADER); if (bhdr == null) { - message = "Missing "+BEHALF_HEADER+" header."; + message = "Missing " + BEHALF_HEADER + " header."; elr.setMessage(message); elr.setResult(HttpServletResponse.SC_BAD_REQUEST); eventlogger.info(elr); @@ -120,7 +121,7 @@ public class SubscribeServlet extends ProxyServlet { } // Check with the Authorizer AuthorizationResponse aresp = authz.decide(req); - if (! aresp.isAuthorized()) { + if (!aresp.isAuthorized()) { message = "Policy Engine disallows access."; elr.setMessage(message); elr.setResult(HttpServletResponse.SC_FORBIDDEN); @@ -140,13 +141,14 @@ public class SubscribeServlet extends ProxyServlet { resp.setContentType(SUBLIST_CONTENT_TYPE); resp.getOutputStream().print(t); } + /** * PUT on the <subscribeUrl> -- not supported. */ @Override public void doPut(HttpServletRequest req, HttpServletResponse resp) throws IOException { setIpAndFqdnForEelf("doPut"); - eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_SUBID, req.getHeader(BEHALF_HEADER),getIdFromPath(req)+""); + eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_SUBID, req.getHeader(BEHALF_HEADER), getIdFromPath(req) + ""); String message = "PUT not allowed for the subscribeURL."; EventLogRecord elr = new EventLogRecord(req); elr.setMessage(message); @@ -154,10 +156,10 @@ public class SubscribeServlet extends ProxyServlet { eventlogger.info(elr); resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, message); } + /** - * POST on the <subscribeUrl> -- create a new subscription to a feed. - * See the <i>Creating a Subscription</i> section in the <b>Provisioning API</b> - * document for details on how this method should be invoked. + * POST on the <subscribeUrl> -- create a new subscription to a feed. See the <i>Creating a Subscription</i> + * section in the <b>Provisioning API</b> document for details on how this method should be invoked. */ @Override public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { @@ -178,7 +180,7 @@ public class SubscribeServlet extends ProxyServlet { } String bhdr = req.getHeader(BEHALF_HEADER); if (bhdr == null) { - message = "Missing "+BEHALF_HEADER+" header."; + message = "Missing " + BEHALF_HEADER + " header."; elr.setMessage(message); elr.setResult(HttpServletResponse.SC_BAD_REQUEST); eventlogger.info(elr); @@ -205,7 +207,7 @@ public class SubscribeServlet extends ProxyServlet { } // Check with the Authorizer AuthorizationResponse aresp = authz.decide(req); - if (! aresp.isAuthorized()) { + if (!aresp.isAuthorized()) { message = "Policy Engine disallows access."; elr.setMessage(message); elr.setResult(HttpServletResponse.SC_FORBIDDEN); @@ -218,7 +220,7 @@ public class SubscribeServlet extends ProxyServlet { ContentHeader ch = getContentHeader(req); String ver = ch.getAttribute("version"); if (!ch.getType().equals(SUB_BASECONTENT_TYPE) || !(ver.equals("1.0") || ver.equals("2.0"))) { - intlogger.debug("Content-type is: "+req.getHeader("Content-Type")); + intlogger.debug("Content-type is: " + req.getHeader("Content-Type")); message = "Incorrect content-type"; elr.setMessage(message); elr.setResult(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE); @@ -235,10 +237,11 @@ public class SubscribeServlet extends ProxyServlet { resp.sendError(HttpServletResponse.SC_BAD_REQUEST, message); return; } - if (intlogger.isDebugEnabled()) + if (intlogger.isDebugEnabled()) { intlogger.debug(jo.toString()); - if (++active_subs > max_subs) { - active_subs--; + } + if (++activeSubs > maxSubs) { + activeSubs--; message = "Cannot create subscription; the maximum number of subscriptions has been configured."; elr.setMessage(message); elr.setResult(HttpServletResponse.SC_CONFLICT); @@ -250,7 +253,7 @@ public class SubscribeServlet extends ProxyServlet { try { sub = new Subscription(jo); } catch (InvalidObjectException e) { - active_subs--; + activeSubs--; message = e.getMessage(); elr.setMessage(message); elr.setResult(HttpServletResponse.SC_BAD_REQUEST); @@ -263,8 +266,11 @@ public class SubscribeServlet extends ProxyServlet { // Check if this subscription already exists; not an error (yet), just warn Subscription sub2 = Subscription.getSubscriptionMatching(sub); - if (sub2 != null) - intlogger.warn("PROV0011 Creating a duplicate subscription: new subid="+sub.getSubid()+", old subid="+sub2.getSubid()); + if (sub2 != null) { + intlogger.warn( + "PROV0011 Creating a duplicate subscription: new subid=" + sub.getSubid() + ", old subid=" + sub2 + .getSubid()); + } // Create SUBSCRIPTIONS table entries if (doInsert(sub)) { @@ -279,7 +285,7 @@ public class SubscribeServlet extends ProxyServlet { provisioningDataChanged(); } else { // Something went wrong with the INSERT - active_subs--; + activeSubs--; elr.setResult(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); eventlogger.info(elr); resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, DB_PROBLEM_MSG); diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/SubscriptionServlet.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/SubscriptionServlet.java index 8e49e304..3294580b 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/SubscriptionServlet.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/SubscriptionServlet.java @@ -45,30 +45,30 @@ import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager;
/**
- * This servlet handles provisioning for the <subscriptionURL> which is generated by the provisioning
- * server to handle the inspection, modification, and deletion of a particular subscription to a feed.
- * It supports DELETE to delete a subscription, GET to retrieve information about the subscription,
- * and PUT to modify the subscription. In DR 3.0, POST is also supported in order to reset the subscription
- * timers for individual subscriptions.
+ * This servlet handles provisioning for the <subscriptionURL> which is generated by the provisioning server to
+ * handle the inspection, modification, and deletion of a particular subscription to a feed. It supports DELETE to
+ * delete a subscription, GET to retrieve information about the subscription, and PUT to modify the subscription. In DR
+ * 3.0, POST is also supported in order to reset the subscription timers for individual subscriptions.
*
* @author Robert Eby
* @version $Id$
*/
@SuppressWarnings("serial")
public class SubscriptionServlet extends ProxyServlet {
+
public static final String SUBCNTRL_CONTENT_TYPE = "application/vnd.att-dr.subscription-control";
//Adding EELF Logger Rally:US664892
- private static EELFLogger eelflogger = EELFManager.getInstance().getLogger("org.onap.dmaap.datarouter.provisioning.SubscriptionServlet");
+ private static EELFLogger eelflogger = EELFManager.getInstance()
+ .getLogger("org.onap.dmaap.datarouter.provisioning.SubscriptionServlet");
/**
- * DELETE on the <subscriptionUrl> -- delete a subscription.
- * See the <i>Deleting a Subscription</i> section in the <b>Provisioning API</b>
- * document for details on how this method should be invoked.
+ * DELETE on the <subscriptionUrl> -- delete a subscription. See the <i>Deleting a Subscription</i> section in
+ * the <b>Provisioning API</b> document for details on how this method should be invoked.
*/
@Override
public void doDelete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
setIpAndFqdnForEelf("doDelete");
- eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_SUBID, req.getHeader(BEHALF_HEADER),getIdFromPath(req)+"");
+ eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_SUBID, req.getHeader(BEHALF_HEADER), getIdFromPath(req) + "");
EventLogRecord elr = new EventLogRecord(req);
String message = isAuthorizedForProvisioning(req);
if (message != null) {
@@ -84,7 +84,7 @@ public class SubscriptionServlet extends ProxyServlet { }
String bhdr = req.getHeader(BEHALF_HEADER);
if (bhdr == null) {
- message = "Missing "+BEHALF_HEADER+" header.";
+ message = "Missing " + BEHALF_HEADER + " header.";
elr.setMessage(message);
elr.setResult(HttpServletResponse.SC_BAD_REQUEST);
eventlogger.info(elr);
@@ -111,7 +111,7 @@ public class SubscriptionServlet extends ProxyServlet { }
// Check with the Authorizer
AuthorizationResponse aresp = authz.decide(req);
- if (! aresp.isAuthorized()) {
+ if (!aresp.isAuthorized()) {
message = "Policy Engine disallows access.";
elr.setMessage(message);
elr.setResult(HttpServletResponse.SC_FORBIDDEN);
@@ -122,7 +122,7 @@ public class SubscriptionServlet extends ProxyServlet { // Delete Subscription
if (doDelete(sub)) {
- active_subs--;
+ activeSubs--;
// send response
elr.setResult(HttpServletResponse.SC_NO_CONTENT);
eventlogger.info(elr);
@@ -135,15 +135,16 @@ public class SubscriptionServlet extends ProxyServlet { resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, DB_PROBLEM_MSG);
}
}
+
/**
- * GET on the <subscriptionUrl> -- get information about a subscription.
- * See the <i>Retreiving Information about a Subscription</i> section in the <b>Provisioning API</b>
- * document for details on how this method should be invoked.
+ * GET on the <subscriptionUrl> -- get information about a subscription. See the <i>Retreiving Information
+ * about a Subscription</i> section in the <b>Provisioning API</b> document for details on how this method should be
+ * invoked.
*/
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
setIpAndFqdnForEelf("doGet");
- eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_SUBID, req.getHeader(BEHALF_HEADER),getIdFromPath(req)+"");
+ eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_SUBID, req.getHeader(BEHALF_HEADER), getIdFromPath(req) + "");
EventLogRecord elr = new EventLogRecord(req);
String message = isAuthorizedForProvisioning(req);
if (message != null) {
@@ -159,7 +160,7 @@ public class SubscriptionServlet extends ProxyServlet { }
String bhdr = req.getHeader(BEHALF_HEADER);
if (bhdr == null) {
- message = "Missing "+BEHALF_HEADER+" header.";
+ message = "Missing " + BEHALF_HEADER + " header.";
elr.setMessage(message);
elr.setResult(HttpServletResponse.SC_BAD_REQUEST);
eventlogger.info(elr);
@@ -186,7 +187,7 @@ public class SubscriptionServlet extends ProxyServlet { }
// Check with the Authorizer
AuthorizationResponse aresp = authz.decide(req);
- if (! aresp.isAuthorized()) {
+ if (!aresp.isAuthorized()) {
message = "Policy Engine disallows access.";
elr.setMessage(message);
elr.setResult(HttpServletResponse.SC_FORBIDDEN);
@@ -202,15 +203,15 @@ public class SubscriptionServlet extends ProxyServlet { resp.setContentType(SUBFULL_CONTENT_TYPE);
resp.getOutputStream().print(sub.asJSONObject(true).toString());
}
+
/**
- * PUT on the <subscriptionUrl> -- modify a subscription.
- * See the <i>Modifying a Subscription</i> section in the <b>Provisioning API</b>
- * document for details on how this method should be invoked.
+ * PUT on the <subscriptionUrl> -- modify a subscription. See the <i>Modifying a Subscription</i> section in
+ * the <b>Provisioning API</b> document for details on how this method should be invoked.
*/
@Override
public void doPut(HttpServletRequest req, HttpServletResponse resp) throws IOException {
setIpAndFqdnForEelf("doPut");
- eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_SUBID, req.getHeader(BEHALF_HEADER),getIdFromPath(req)+"");
+ eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_SUBID, req.getHeader(BEHALF_HEADER), getIdFromPath(req) + "");
EventLogRecord elr = new EventLogRecord(req);
String message = isAuthorizedForProvisioning(req);
if (message != null) {
@@ -226,7 +227,7 @@ public class SubscriptionServlet extends ProxyServlet { }
String bhdr = req.getHeader(BEHALF_HEADER);
if (bhdr == null) {
- message = "Missing "+BEHALF_HEADER+" header.";
+ message = "Missing " + BEHALF_HEADER + " header.";
elr.setMessage(message);
elr.setResult(HttpServletResponse.SC_BAD_REQUEST);
eventlogger.info(elr);
@@ -253,7 +254,7 @@ public class SubscriptionServlet extends ProxyServlet { }
// Check with the Authorizer
AuthorizationResponse aresp = authz.decide(req);
- if (! aresp.isAuthorized()) {
+ if (!aresp.isAuthorized()) {
message = "Policy Engine disallows access.";
elr.setMessage(message);
elr.setResult(HttpServletResponse.SC_FORBIDDEN);
@@ -281,8 +282,9 @@ public class SubscriptionServlet extends ProxyServlet { resp.sendError(HttpServletResponse.SC_BAD_REQUEST, message);
return;
}
- if (intlogger.isDebugEnabled())
+ if (intlogger.isDebugEnabled()) {
intlogger.debug(jo.toString());
+ }
Subscription sub = null;
try {
sub = new Subscription(jo);
@@ -335,10 +337,10 @@ public class SubscriptionServlet extends ProxyServlet { resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, DB_PROBLEM_MSG);
}
}
+
/**
- * POST on the <subscriptionUrl> -- control a subscription.
- * See the <i>Resetting a Subscription's Retry Schedule</i> section in the <b>Provisioning API</b>
- * document for details on how this method should be invoked.
+ * POST on the <subscriptionUrl> -- control a subscription. See the <i>Resetting a Subscription's Retry
+ * Schedule</i> section in the <b>Provisioning API</b> document for details on how this method should be invoked.
*/
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
@@ -367,7 +369,7 @@ public class SubscriptionServlet extends ProxyServlet { }
String bhdr = req.getHeader(BEHALF_HEADER);
if (bhdr == null) {
- message = "Missing "+BEHALF_HEADER+" header.";
+ message = "Missing " + BEHALF_HEADER + " header.";
elr.setMessage(message);
elr.setResult(HttpServletResponse.SC_BAD_REQUEST);
eventlogger.info(elr);
@@ -396,7 +398,7 @@ public class SubscriptionServlet extends ProxyServlet { }
// Check with the Authorizer
AuthorizationResponse aresp = authz.decide(req);
- if (! aresp.isAuthorized()) {
+ if (!aresp.isAuthorized()) {
message = "Policy Engine disallows access.";
elr.setMessage(message);
elr.setResult(HttpServletResponse.SC_FORBIDDEN);
@@ -437,22 +439,25 @@ public class SubscriptionServlet extends ProxyServlet { }
/**
- * A Thread class used to serially send reset notifications to all nodes in the DR network,
- * when a POST is received for a subscription.
+ * A Thread class used to serially send reset notifications to all nodes in the DR network, when a POST is received
+ * for a subscription.
*/
public class SubscriberNotifyThread extends Thread {
+
public static final String URL_TEMPLATE = "http://%s/internal/resetSubscription/%d";
private List<String> urls = new Vector<String>();
public SubscriberNotifyThread() {
setName("SubscriberNotifyThread");
}
+
public void resetSubscription(int subid) {
for (String nodename : BaseServlet.getNodes()) {
String u = String.format(URL_TEMPLATE, nodename, subid);
urls.add(u);
}
}
+
public void run() {
try {
while (!urls.isEmpty()) {
@@ -464,11 +469,11 @@ public class SubscriptionServlet extends ProxyServlet { conn.getContentLength(); // Force the GET through
conn.disconnect();
} catch (IOException e) {
- intlogger.info("IOException Error accessing URL: "+u+": " + e.getMessage());
+ intlogger.info("IOException Error accessing URL: " + u + ": " + e.getMessage());
}
}
} catch (Exception e) {
- intlogger.warn("Caught exception in SubscriberNotifyThread: "+e);
+ intlogger.warn("Caught exception in SubscriberNotifyThread: " + e);
e.printStackTrace();
}
}
diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/SynchronizerTask.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/SynchronizerTask.java index 898a3f05..9eeac236 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/SynchronizerTask.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/SynchronizerTask.java @@ -89,23 +89,32 @@ import org.onap.dmaap.datarouter.provisioning.utils.URLUtilities; * </ol> * <p>For this to work correctly, the following code needs to be placed at the beginning of main().</p> * <code> - * Security.setProperty("networkaddress.cache.ttl", "10"); + * Security.setProperty("networkaddress.cache.ttl", "10"); * </code> * * @author Robert Eby * @version $Id: SynchronizerTask.java,v 1.10 2014/03/21 13:50:10 eby Exp $ */ public class SynchronizerTask extends TimerTask { - /** This is a singleton -- there is only one SynchronizerTask object in the server */ + + /** + * This is a singleton -- there is only one SynchronizerTask object in the server + */ private static SynchronizerTask synctask; - /** This POD is unknown -- not on the list of PODs */ + /** + * This POD is unknown -- not on the list of PODs + */ public static final int UNKNOWN = 0; - /** This POD is active -- on the list of PODs, and the DNS CNAME points to us */ + /** + * This POD is active -- on the list of PODs, and the DNS CNAME points to us + */ public static final int ACTIVE = 1; - /** This POD is standby -- on the list of PODs, and the DNS CNAME does not point to us */ + /** + * This POD is standby -- on the list of PODs, and the DNS CNAME does not point to us + */ public static final int STANDBY = 2; - private static final String[] stnames = { "UNKNOWN", "ACTIVE", "STANDBY" }; + private static final String[] stnames = {"UNKNOWN", "ACTIVE", "STANDBY"}; private static final long ONE_HOUR = 60 * 60 * 1000L; private final Logger logger; @@ -118,11 +127,13 @@ public class SynchronizerTask extends TimerTask { /** * Get the singleton SynchronizerTask object. + * * @return the SynchronizerTask */ public static synchronized SynchronizerTask getSynchronizer() { - if (synctask == null) + if (synctask == null) { synctask = new SynchronizerTask(); + } return synctask; } @@ -138,16 +149,16 @@ public class SynchronizerTask extends TimerTask { logger.info("PROV5000: Sync task starting, server state is UNKNOWN"); try { Properties props = (new DB()).getProperties(); - String type = props.getProperty(Main.KEYSTORE_TYPE_PROPERTY, "jks"); + String type = props.getProperty(Main.KEYSTORE_TYPE_PROPERTY, "jks"); String store = props.getProperty(Main.KEYSTORE_PATH_PROPERTY); - String pass = props.getProperty(Main.KEYSTORE_PASSWORD_PROPERTY); + String pass = props.getProperty(Main.KEYSTORE_PASSWORD_PROPERTY); KeyStore keyStore = KeyStore.getInstance(type); FileInputStream instream = new FileInputStream(new File(store)); keyStore.load(instream, pass.toCharArray()); instream.close(); store = props.getProperty(Main.TRUSTSTORE_PATH_PROPERTY); - pass = props.getProperty(Main.TRUSTSTORE_PASSWORD_PROPERTY); + pass = props.getProperty(Main.TRUSTSTORE_PASSWORD_PROPERTY); KeyStore trustStore = null; if (store != null && store.length() > 0) { trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); @@ -158,12 +169,13 @@ public class SynchronizerTask extends TimerTask { // We are connecting with the node name, but the certificate will have the CNAME // So we need to accept a non-matching certificate name - String keystorepass = props.getProperty(Main.KEYSTORE_PASSWORD_PROPERTY); //itrack.web.att.com/browse/DATARTR-6 for changing hard coded passphase ref + String keystorepass = props.getProperty( + Main.KEYSTORE_PASSWORD_PROPERTY); //itrack.web.att.com/browse/DATARTR-6 for changing hard coded passphase ref AbstractHttpClient hc = new DefaultHttpClient(); SSLSocketFactory socketFactory = (trustStore == null) - ? new SSLSocketFactory(keyStore, keystorepass) - : new SSLSocketFactory(keyStore, keystorepass, trustStore); + ? new SSLSocketFactory(keyStore, keystorepass) + : new SSLSocketFactory(keyStore, keystorepass, trustStore); socketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); Scheme sch = new Scheme("https", 443, socketFactory); hc.getConnectionManager().getSchemeRegistry().register(sch); @@ -179,12 +191,13 @@ public class SynchronizerTask extends TimerTask { } rolex.scheduleAtFixedRate(this, 0L, interval); } catch (Exception e) { - logger.warn("PROV5005: Problem starting the synchronizer: "+e); + logger.warn("PROV5005: Problem starting the synchronizer: " + e); } } /** * What is the state of this POD? + * * @return one of ACTIVE, STANDBY, UNKNOWN */ public int getState() { @@ -193,6 +206,7 @@ public class SynchronizerTask extends TimerTask { /** * Is this the active POD? + * * @return true if we are active (the master), false otherwise */ public boolean isActive() { @@ -200,8 +214,8 @@ public class SynchronizerTask extends TimerTask { } /** - * This method is used to signal that another POD (the active POD) has sent us a /fetchProv request, - * and that we should re-synchronize with the master. + * This method is used to signal that another POD (the active POD) has sent us a /fetchProv request, and that we + * should re-synchronize with the master. */ public void doFetch() { doFetch = true; @@ -226,20 +240,23 @@ public class SynchronizerTask extends TimerTask { JSONObject jo = readProvisioningJSON(); if (jo != null) { doFetch = false; - syncFeeds( jo.getJSONArray("feeds")); - syncSubs( jo.getJSONArray("subscriptions")); - syncGroups( jo.getJSONArray("groups")); //Rally:US708115 - 1610 + syncFeeds(jo.getJSONArray("feeds")); + syncSubs(jo.getJSONArray("subscriptions")); + syncGroups(jo.getJSONArray("groups")); //Rally:US708115 - 1610 syncParams(jo.getJSONObject("parameters")); // The following will not be present in a version=1.0 provfeed JSONArray ja = jo.optJSONArray("ingress"); - if (ja != null) + if (ja != null) { syncIngressRoutes(ja); + } JSONObject j2 = jo.optJSONObject("egress"); - if (j2 != null) - syncEgressRoutes( j2); + if (j2 != null) { + syncEgressRoutes(j2); + } ja = jo.optJSONArray("routing"); - if (ja != null) + if (ja != null) { syncNetworkRoutes(ja); + } } logger.info("PROV5013: Sync completed."); nextsynctime = System.currentTimeMillis() + ONE_HOUR; @@ -254,53 +271,61 @@ public class SynchronizerTask extends TimerTask { if (lfl.isIdle()) { // Only fetch new logs if the loader is waiting for them. logger.trace("Checking for logs to replicate..."); - RLEBitSet local = lfl.getBitSet(); + RLEBitSet local = lfl.getBitSet(); RLEBitSet remote = readRemoteLoglist(); remote.andNot(local); if (!remote.isEmpty()) { - logger.debug(" Replicating logs: "+remote); + logger.debug(" Replicating logs: " + remote); replicateDRLogs(remote); } } } catch (Exception e) { - logger.warn("PROV0020: Caught exception in SynchronizerTask: "+e); + logger.warn("PROV0020: Caught exception in SynchronizerTask: " + e); e.printStackTrace(); } } /** - * This method is used to lookup the CNAME that points to the active server. - * It returns 0 (UNKNOWN), 1(ACTIVE), or 2 (STANDBY) to indicate the state of this server. + * This method is used to lookup the CNAME that points to the active server. It returns 0 (UNKNOWN), 1(ACTIVE), or 2 + * (STANDBY) to indicate the state of this server. + * * @return the current state */ private int lookupState() { int newstate = UNKNOWN; try { InetAddress myaddr = InetAddress.getLocalHost(); - if (logger.isTraceEnabled()) - logger.trace("My address: "+myaddr); - String this_pod = myaddr.getHostName(); - Set<String> pods = new TreeSet<String>(Arrays.asList(BaseServlet.getPods())); - if (pods.contains(this_pod)) { - InetAddress pserver = InetAddress.getByName(BaseServlet.active_prov_name); + if (logger.isTraceEnabled()) { + logger.trace("My address: " + myaddr); + } + String thisPod = myaddr.getHostName(); + Set<String> pods = new TreeSet<>(Arrays.asList(BaseServlet.getPods())); + if (pods.contains(thisPod)) { + InetAddress pserver = InetAddress.getByName(BaseServlet.activeProvName); newstate = myaddr.equals(pserver) ? ACTIVE : STANDBY; - if (logger.isDebugEnabled() && System.currentTimeMillis() >= next_msg) { - logger.debug("Active POD = "+pserver+", Current state is "+stnames[newstate]); - next_msg = System.currentTimeMillis() + (5 * 60 * 1000L); + if (logger.isDebugEnabled() && System.currentTimeMillis() >= nextMsg) { + logger.debug("Active POD = " + pserver + ", Current state is " + stnames[newstate]); + nextMsg = System.currentTimeMillis() + (5 * 60 * 1000L); } } else { - logger.warn("PROV5003: My name ("+this_pod+") is missing from the list of provisioning servers."); + logger.warn("PROV5003: My name (" + thisPod + ") is missing from the list of provisioning servers."); } } catch (UnknownHostException e) { logger.warn("PROV5002: Cannot determine the name of this provisioning server."); } - if (newstate != state) - logger.info(String.format("PROV5001: Server state changed from %s to %s", stnames[state], stnames[newstate])); + if (newstate != state) { + logger + .info(String.format("PROV5001: Server state changed from %s to %s", stnames[state], stnames[newstate])); + } return newstate; } - private static long next_msg = 0; // only display the "Current state" msg every 5 mins. - /** Synchronize the Feeds in the JSONArray, with the Feeds in the DB. */ + + private static long nextMsg = 0; // only display the "Current state" msg every 5 mins. + + /** + * Synchronize the Feeds in the JSONArray, with the Feeds in the DB. + */ private void syncFeeds(JSONArray ja) { Collection<Syncable> coll = new ArrayList<Syncable>(); for (int n = 0; n < ja.length(); n++) { @@ -308,13 +333,17 @@ public class SynchronizerTask extends TimerTask { Feed f = new Feed(ja.getJSONObject(n)); coll.add(f); } catch (Exception e) { - logger.warn("PROV5004: Invalid object in feed: "+ja.optJSONObject(n)); + logger.warn("PROV5004: Invalid object in feed: " + ja.optJSONObject(n)); } } - if (sync(coll, Feed.getAllFeeds())) + if (sync(coll, Feed.getAllFeeds())) { BaseServlet.provisioningDataChanged(); + } } - /** Synchronize the Subscriptions in the JSONArray, with the Subscriptions in the DB. */ + + /** + * Synchronize the Subscriptions in the JSONArray, with the Subscriptions in the DB. + */ private void syncSubs(JSONArray ja) { Collection<Syncable> coll = new ArrayList<Syncable>(); for (int n = 0; n < ja.length(); n++) { @@ -325,14 +354,17 @@ public class SynchronizerTask extends TimerTask { Subscription s = new Subscription(j); coll.add(s); } catch (Exception e) { - logger.warn("PROV5004: Invalid object in subscription: "+ja.optJSONObject(n)); + logger.warn("PROV5004: Invalid object in subscription: " + ja.optJSONObject(n)); } } - if (sync(coll, Subscription.getAllSubscriptions())) + if (sync(coll, Subscription.getAllSubscriptions())) { BaseServlet.provisioningDataChanged(); + } } - /** Rally:US708115 - Synchronize the Groups in the JSONArray, with the Groups in the DB. */ + /** + * Rally:US708115 - Synchronize the Groups in the JSONArray, with the Groups in the DB. + */ private void syncGroups(JSONArray ja) { Collection<Syncable> coll = new ArrayList<Syncable>(); for (int n = 0; n < ja.length(); n++) { @@ -340,15 +372,18 @@ public class SynchronizerTask extends TimerTask { Group g = new Group(ja.getJSONObject(n)); coll.add(g); } catch (Exception e) { - logger.warn("PROV5004: Invalid object in subscription: "+ja.optJSONObject(n)); + logger.warn("PROV5004: Invalid object in subscription: " + ja.optJSONObject(n)); } } - if (sync(coll, Group.getAllgroups())) + if (sync(coll, Group.getAllgroups())) { BaseServlet.provisioningDataChanged(); + } } - /** Synchronize the Parameters in the JSONObject, with the Parameters in the DB. */ + /** + * Synchronize the Parameters in the JSONObject, with the Parameters in the DB. + */ private void syncParams(JSONObject jo) { Collection<Syncable> coll = new ArrayList<Syncable>(); for (String k : jo.keySet()) { @@ -357,12 +392,13 @@ public class SynchronizerTask extends TimerTask { v = jo.getString(k); } catch (JSONException e) { try { - v = ""+jo.getInt(k); + v = "" + jo.getInt(k); } catch (JSONException e1) { JSONArray ja = jo.getJSONArray(k); for (int i = 0; i < ja.length(); i++) { - if (i > 0) + if (i > 0) { v += "|"; + } v += ja.getString(i); } } @@ -374,6 +410,7 @@ public class SynchronizerTask extends TimerTask { BaseServlet.provisioningParametersChanged(); } } + private void syncIngressRoutes(JSONArray ja) { Collection<Syncable> coll = new ArrayList<Syncable>(); for (int n = 0; n < ja.length(); n++) { @@ -381,12 +418,14 @@ public class SynchronizerTask extends TimerTask { IngressRoute in = new IngressRoute(ja.getJSONObject(n)); coll.add(in); } catch (NumberFormatException e) { - logger.warn("PROV5004: Invalid object in ingress routes: "+ja.optJSONObject(n)); + logger.warn("PROV5004: Invalid object in ingress routes: " + ja.optJSONObject(n)); } } - if (sync(coll, IngressRoute.getAllIngressRoutes())) + if (sync(coll, IngressRoute.getAllIngressRoutes())) { BaseServlet.provisioningDataChanged(); + } } + private void syncEgressRoutes(JSONObject jo) { Collection<Syncable> coll = new ArrayList<Syncable>(); for (String key : jo.keySet()) { @@ -396,14 +435,16 @@ public class SynchronizerTask extends TimerTask { EgressRoute er = new EgressRoute(sub, node); coll.add(er); } catch (NumberFormatException e) { - logger.warn("PROV5004: Invalid subid in egress routes: "+key); + logger.warn("PROV5004: Invalid subid in egress routes: " + key); } catch (IllegalArgumentException e) { - logger.warn("PROV5004: Invalid node name in egress routes: "+key); + logger.warn("PROV5004: Invalid node name in egress routes: " + key); } } - if (sync(coll, EgressRoute.getAllEgressRoutes())) + if (sync(coll, EgressRoute.getAllEgressRoutes())) { BaseServlet.provisioningDataChanged(); + } } + private void syncNetworkRoutes(JSONArray ja) { Collection<Syncable> coll = new ArrayList<Syncable>(); for (int n = 0; n < ja.length(); n++) { @@ -411,12 +452,14 @@ public class SynchronizerTask extends TimerTask { NetworkRoute nr = new NetworkRoute(ja.getJSONObject(n)); coll.add(nr); } catch (JSONException e) { - logger.warn("PROV5004: Invalid object in network routes: "+ja.optJSONObject(n)); + logger.warn("PROV5004: Invalid object in network routes: " + ja.optJSONObject(n)); } } - if (sync(coll, NetworkRoute.getAllNetworkRoutes())) + if (sync(coll, NetworkRoute.getAllNetworkRoutes())) { BaseServlet.provisioningDataChanged(); + } } + private boolean sync(Collection<? extends Syncable> newc, Collection<? extends Syncable> oldc) { boolean changes = false; try { @@ -431,18 +474,21 @@ public class SynchronizerTask extends TimerTask { Syncable newobj = newmap.get(n); Syncable oldobj = oldmap.get(n); if (oldobj == null) { - if (logger.isDebugEnabled()) - logger.debug(" Inserting record: "+newobj); + if (logger.isDebugEnabled()) { + logger.debug(" Inserting record: " + newobj); + } newobj.doInsert(conn); changes = true; } else if (newobj == null) { - if (logger.isDebugEnabled()) - logger.debug(" Deleting record: "+oldobj); + if (logger.isDebugEnabled()) { + logger.debug(" Deleting record: " + oldobj); + } oldobj.doDelete(conn); changes = true; } else if (!newobj.equals(oldobj)) { - if (logger.isDebugEnabled()) - logger.debug(" Updating record: "+newobj); + if (logger.isDebugEnabled()) { + logger.debug(" Updating record: " + newobj); + } newobj.doUpdate(conn); /**Rally US708115 @@ -455,11 +501,12 @@ public class SynchronizerTask extends TimerTask { } db.release(conn); } catch (SQLException e) { - logger.warn("PROV5009: problem during sync, exception: "+e); + logger.warn("PROV5009: problem during sync, exception: " + e); e.printStackTrace(); } return changes; } + private Map<String, Syncable> getMap(Collection<? extends Syncable> c) { Map<String, Syncable> map = new HashMap<String, Syncable>(); for (Syncable v : c) { @@ -468,29 +515,28 @@ public class SynchronizerTask extends TimerTask { return map; } - /**Change owner of FEED/SUBSCRIPTION*/ - /**Rally US708115 - * Change Ownership of FEED - 1610 - * - * */ + /** + * Rally US708115 Change Ownership of FEED - 1610 + */ private void checkChnageOwner(Syncable newobj, Syncable oldobj) { - if(newobj instanceof Feed) { + if (newobj instanceof Feed) { Feed oldfeed = (Feed) oldobj; Feed newfeed = (Feed) newobj; - if(!oldfeed.getPublisher().equals(newfeed.getPublisher())){ - logger.info("PROV5013 - Previous publisher: "+oldfeed.getPublisher() +": New publisher-"+newfeed.getPublisher()); + if (!oldfeed.getPublisher().equals(newfeed.getPublisher())) { + logger.info("PROV5013 - Previous publisher: " + oldfeed.getPublisher() + ": New publisher-" + newfeed + .getPublisher()); oldfeed.setPublisher(newfeed.getPublisher()); oldfeed.changeOwnerShip(); } - } - else if(newobj instanceof Subscription) { + } else if (newobj instanceof Subscription) { Subscription oldsub = (Subscription) oldobj; Subscription newsub = (Subscription) newobj; - if(!oldsub.getSubscriber().equals(newsub.getSubscriber())){ - logger.info("PROV5013 - Previous subscriber: "+oldsub.getSubscriber() +": New subscriber-"+newsub.getSubscriber()); + if (!oldsub.getSubscriber().equals(newsub.getSubscriber())) { + logger.info("PROV5013 - Previous subscriber: " + oldsub.getSubscriber() + ": New subscriber-" + newsub + .getSubscriber()); oldsub.setSubscriber(newsub.getSubscriber()); oldsub.changeOwnerShip(); } @@ -500,43 +546,47 @@ public class SynchronizerTask extends TimerTask { /** * Issue a GET on the peer POD's /internal/prov/ URL to get a copy of its provisioning data. + * * @return the provisioning data (as a JONObject) */ private synchronized JSONObject readProvisioningJSON() { - String url = URLUtilities.generatePeerProvURL(); + String url = URLUtilities.generatePeerProvURL(); HttpGet get = new HttpGet(url); try { HttpResponse response = httpclient.execute(get); int code = response.getStatusLine().getStatusCode(); if (code != HttpServletResponse.SC_OK) { - logger.warn("PROV5010: readProvisioningJSON failed, bad error code: "+code); + logger.warn("PROV5010: readProvisioningJSON failed, bad error code: " + code); return null; } HttpEntity entity = response.getEntity(); String ctype = entity.getContentType().getValue().trim(); - if (!ctype.equals(BaseServlet.PROVFULL_CONTENT_TYPE1) && !ctype.equals(BaseServlet.PROVFULL_CONTENT_TYPE2)) { - logger.warn("PROV5011: readProvisioningJSON failed, bad content type: "+ctype); + if (!ctype.equals(BaseServlet.PROVFULL_CONTENT_TYPE1) && !ctype + .equals(BaseServlet.PROVFULL_CONTENT_TYPE2)) { + logger.warn("PROV5011: readProvisioningJSON failed, bad content type: " + ctype); return null; } return new JSONObject(new JSONTokener(entity.getContent())); } catch (Exception e) { - logger.warn("PROV5012: readProvisioningJSON failed, exception: "+e); + logger.warn("PROV5012: readProvisioningJSON failed, exception: " + e); return null; } finally { get.releaseConnection(); } } + /** - * Issue a GET on the peer POD's /internal/drlogs/ URL to get an RELBitSet representing the - * log records available in the remote database. + * Issue a GET on the peer POD's /internal/drlogs/ URL to get an RELBitSet representing the log records available in + * the remote database. + * * @return the bitset */ private RLEBitSet readRemoteLoglist() { RLEBitSet bs = new RLEBitSet(); - String url = URLUtilities.generatePeerLogsURL(); + String url = URLUtilities.generatePeerLogsURL(); //Fixing if only one Prov is configured, not to give exception to fill logs, return empty bitset. - if(url.equals("")) { + if (url.equals("")) { return bs; } //End of fix. @@ -546,66 +596,70 @@ public class SynchronizerTask extends TimerTask { HttpResponse response = httpclient.execute(get); int code = response.getStatusLine().getStatusCode(); if (code != HttpServletResponse.SC_OK) { - logger.warn("PROV5010: readRemoteLoglist failed, bad error code: "+code); + logger.warn("PROV5010: readRemoteLoglist failed, bad error code: " + code); return bs; } HttpEntity entity = response.getEntity(); String ctype = entity.getContentType().getValue().trim(); if (!ctype.equals("text/plain")) { - logger.warn("PROV5011: readRemoteLoglist failed, bad content type: "+ctype); + logger.warn("PROV5011: readRemoteLoglist failed, bad content type: " + ctype); return bs; } InputStream is = entity.getContent(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); int ch = 0; - while ((ch = is.read()) >= 0) + while ((ch = is.read()) >= 0) { bos.write(ch); + } bs.set(bos.toString()); is.close(); } catch (Exception e) { - logger.warn("PROV5012: readRemoteLoglist failed, exception: "+e); + logger.warn("PROV5012: readRemoteLoglist failed, exception: " + e); return bs; } finally { get.releaseConnection(); } return bs; } + /** - * Issue a POST on the peer POD's /internal/drlogs/ URL to fetch log records available - * in the remote database that we wish to copy to the local database. + * Issue a POST on the peer POD's /internal/drlogs/ URL to fetch log records available in the remote database that + * we wish to copy to the local database. + * * @param bs the bitset (an RELBitSet) of log records to fetch */ private void replicateDRLogs(RLEBitSet bs) { - String url = URLUtilities.generatePeerLogsURL(); + String url = URLUtilities.generatePeerLogsURL(); HttpPost post = new HttpPost(url); try { String t = bs.toString(); HttpEntity body = new ByteArrayEntity(t.getBytes(), ContentType.create("text/plain")); post.setEntity(body); - if (logger.isDebugEnabled()) - logger.debug("Requesting records: "+t); + if (logger.isDebugEnabled()) { + logger.debug("Requesting records: " + t); + } HttpResponse response = httpclient.execute(post); int code = response.getStatusLine().getStatusCode(); if (code != HttpServletResponse.SC_OK) { - logger.warn("PROV5010: replicateDRLogs failed, bad error code: "+code); + logger.warn("PROV5010: replicateDRLogs failed, bad error code: " + code); return; } HttpEntity entity = response.getEntity(); String ctype = entity.getContentType().getValue().trim(); if (!ctype.equals("text/plain")) { - logger.warn("PROV5011: replicateDRLogs failed, bad content type: "+ctype); + logger.warn("PROV5011: replicateDRLogs failed, bad content type: " + ctype); return; } String spoolname = "" + System.currentTimeMillis(); Path tmppath = Paths.get(spooldir, spoolname); - Path donepath = Paths.get(spooldir, "IN."+spoolname); + Path donepath = Paths.get(spooldir, "IN." + spoolname); Files.copy(entity.getContent(), Paths.get(spooldir, spoolname), StandardCopyOption.REPLACE_EXISTING); Files.move(tmppath, donepath, StandardCopyOption.REPLACE_EXISTING); - logger.info("Approximately "+bs.cardinality()+" records replicated."); + logger.info("Approximately " + bs.cardinality() + " records replicated."); } catch (Exception e) { - logger.warn("PROV5012: replicateDRLogs failed, exception: "+e); + logger.warn("PROV5012: replicateDRLogs failed, exception: " + e); } finally { post.releaseConnection(); } diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/DB.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/DB.java index c65ee267..a907a03b 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/DB.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/DB.java @@ -37,11 +37,10 @@ import java.util.*; * @version $Id$
*/
public class DB {
+
/**
* The name of the properties file (in CLASSPATH)
*/
- private static final String CONFIG_FILE = "provserver.properties";
-
private static String DB_URL;
private static String DB_LOGIN;
private static String DB_PASSWORD;
@@ -53,15 +52,17 @@ public class DB { public static String HTTP_PORT;
/**
- * Construct a DB object. If this is the very first creation of this object, it will load a copy
- * of the properties for the server, and attempt to load the JDBC driver for the database. If a fatal
- * error occurs (e.g. either the properties file or the DB driver is missing), the JVM will exit.
+ * Construct a DB object. If this is the very first creation of this object, it will load a copy of the properties
+ * for the server, and attempt to load the JDBC driver for the database. If a fatal error occurs (e.g. either the
+ * properties file or the DB driver is missing), the JVM will exit.
*/
public DB() {
if (props == null) {
props = new Properties();
- try (InputStream inStream = getClass().getClassLoader().getResourceAsStream(CONFIG_FILE)) {
- props.load(inStream);
+ try {
+ props.load(new FileInputStream(System.getProperty(
+ "org.onap.dmaap.datarouter.provserver.properties",
+ "/opt/app/datartr/etc/provserver.properties")));
String DB_DRIVER = (String) props.get("org.onap.dmaap.datarouter.db.driver");
DB_URL = (String) props.get("org.onap.dmaap.datarouter.db.url");
DB_LOGIN = (String) props.get("org.onap.dmaap.datarouter.db.login");
@@ -94,7 +95,6 @@ public class DB { * Get a JDBC connection to the DB from the pool. Creates a new one if none are available.
*
* @return the Connection
- * @throws SQLException
*/
@SuppressWarnings("resource")
public Connection getConnection() throws SQLException {
@@ -110,8 +110,9 @@ public class DB { try {
connection = DriverManager.getConnection(DB_URL, DB_LOGIN, DB_PASSWORD);
} catch (SQLException sqlEx) {
- if (++n >= 3)
+ if (++n >= 3) {
throw sqlEx;
+ }
}
} while (connection == null);
}
@@ -132,15 +133,16 @@ public class DB { public void release(Connection connection) {
if (connection != null) {
synchronized (queue) {
- if (!queue.contains(connection))
+ if (!queue.contains(connection)) {
queue.add(connection);
+ }
}
}
}
/**
- * Run all necessary retrofits required to bring the database up to the level required for this version
- * of the provisioning server. This should be run before the server itself is started.
+ * Run all necessary retrofits required to bring the database up to the level required for this version of the
+ * provisioning server. This should be run before the server itself is started.
*
* @return true if all retrofits worked, false otherwise
*/
@@ -149,16 +151,15 @@ public class DB { }
/**
- * Retrofit 1 - Make sure the expected tables are in DB and are initialized.
- * Uses sql_init_01.sql to setup the DB.
+ * Retrofit 1 - Make sure the expected tables are in DB and are initialized. Uses sql_init_01.sql to setup the DB.
*
* @return true if the retrofit worked, false otherwise
*/
private boolean retroFit1() {
final String[] expectedTables = {
- "FEEDS", "FEED_ENDPOINT_ADDRS", "FEED_ENDPOINT_IDS", "PARAMETERS",
- "SUBSCRIPTIONS", "LOG_RECORDS", "INGRESS_ROUTES", "EGRESS_ROUTES",
- "NETWORK_ROUTES", "NODESETS", "NODES", "GROUPS"
+ "FEEDS", "FEED_ENDPOINT_ADDRS", "FEED_ENDPOINT_IDS", "PARAMETERS",
+ "SUBSCRIPTIONS", "LOG_RECORDS", "INGRESS_ROUTES", "EGRESS_ROUTES",
+ "NETWORK_ROUTES", "NODESETS", "NODES", "GROUPS"
};
Connection connection = null;
try {
@@ -166,7 +167,7 @@ public class DB { Set<String> actualTables = getTableSet(connection);
boolean initialize = false;
for (String table : expectedTables) {
- initialize |= !actualTables.contains(table);
+ initialize |= !actualTables.contains(table.toLowerCase());
}
if (initialize) {
intlogger.info("PROV9001: First time startup; The database is being initialized.");
@@ -176,8 +177,9 @@ public class DB { intlogger.fatal("PROV9000: The database credentials are not working: " + e.getMessage());
return false;
} finally {
- if (connection != null)
+ if (connection != null) {
release(connection);
+ }
}
return true;
}
@@ -192,7 +194,7 @@ public class DB { Set<String> tables = new HashSet<String>();
try {
DatabaseMetaData md = connection.getMetaData();
- ResultSet rs = md.getTables("datarouter", "", "", null);
+ ResultSet rs = md.getTables(null, null, "%", null);
if (rs != null) {
while (rs.next()) {
tables.add(rs.getString("TABLE_NAME"));
@@ -200,44 +202,45 @@ public class DB { rs.close();
}
} catch (SQLException e) {
+ intlogger.fatal("PROV9010: Failed to get TABLE data from DB: " + e.getMessage());
}
return tables;
}
/**
- * Initialize the tables by running the initialization scripts located in the directory specified
- * by the property <i>org.onap.dmaap.datarouter.provserver.dbscripts</i>. Scripts have names of
- * the form sql_init_NN.sql
+ * Initialize the tables by running the initialization scripts located in the directory specified by the property
+ * <i>org.onap.dmaap.datarouter.provserver.dbscripts</i>. Scripts have names of the form sql_init_NN.sql
*
* @param connection a DB connection
- * @param scriptId the number of the sql_init_NN.sql script to run
+ * @param scriptId the number of the sql_init_NN.sql script to run
*/
private void runInitScript(Connection connection, int scriptId) {
String scriptDir = (String) props.get("org.onap.dmaap.datarouter.provserver.dbscripts");
- StringBuilder sb = new StringBuilder();
+ StringBuilder strBuilder = new StringBuilder();
try {
String scriptFile = String.format("%s/sql_init_%02d.sql", scriptDir, scriptId);
- if (!(new File(scriptFile)).exists())
- return;
-
- LineNumberReader in = new LineNumberReader(new FileReader(scriptFile));
+ if (!(new File(scriptFile)).exists()) {
+ intlogger.fatal("PROV9005 Failed to load sql script from : " + scriptFile);
+ System.exit(1);
+ }
+ LineNumberReader lineReader = new LineNumberReader(new FileReader(scriptFile));
String line;
- while ((line = in.readLine()) != null) {
+ while ((line = lineReader.readLine()) != null) {
if (!line.startsWith("--")) {
line = line.trim();
- sb.append(line);
+ strBuilder.append(line);
if (line.endsWith(";")) {
// Execute one DDL statement
- String sql = sb.toString();
- sb.setLength(0);
- Statement s = connection.createStatement();
- s.execute(sql);
- s.close();
+ String sql = strBuilder.toString();
+ strBuilder.setLength(0);
+ Statement statement = connection.createStatement();
+ statement.execute(sql);
+ statement.close();
}
}
}
- in.close();
- sb.setLength(0);
+ lineReader.close();
+ strBuilder.setLength(0);
} catch (Exception e) {
intlogger.fatal("PROV9002 Error when initializing table: " + e.getMessage());
System.exit(1);
diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/URLUtilities.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/URLUtilities.java index c3661ba0..b58ab5a0 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/URLUtilities.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/URLUtilities.java @@ -37,6 +37,7 @@ import org.onap.dmaap.datarouter.provisioning.BaseServlet; * @version $Id: URLUtilities.java,v 1.2 2014/03/12 19:45:41 eby Exp $
*/
public class URLUtilities {
+
/**
* Generate the URL used to access a feed.
*
@@ -44,7 +45,7 @@ public class URLUtilities { * @return the URL
*/
public static String generateFeedURL(int feedid) {
- return "https://" + BaseServlet.prov_name + "/feed/" + feedid;
+ return "https://" + BaseServlet.provName + "/feed/" + feedid;
}
/**
@@ -54,7 +55,7 @@ public class URLUtilities { * @return the URL
*/
public static String generatePublishURL(int feedid) {
- return "https://" + BaseServlet.prov_name + "/publish/" + feedid;
+ return "https://" + BaseServlet.provName + "/publish/" + feedid;
}
/**
@@ -64,7 +65,7 @@ public class URLUtilities { * @return the URL
*/
public static String generateSubscribeURL(int feedid) {
- return "https://" + BaseServlet.prov_name + "/subscribe/" + feedid;
+ return "https://" + BaseServlet.provName + "/subscribe/" + feedid;
}
/**
@@ -74,7 +75,7 @@ public class URLUtilities { * @return the URL
*/
public static String generateFeedLogURL(int feedid) {
- return "https://" + BaseServlet.prov_name + "/feedlog/" + feedid;
+ return "https://" + BaseServlet.provName + "/feedlog/" + feedid;
}
/**
@@ -84,7 +85,7 @@ public class URLUtilities { * @return the URL
*/
public static String generateSubscriptionURL(int subid) {
- return "https://" + BaseServlet.prov_name + "/subs/" + subid;
+ return "https://" + BaseServlet.provName + "/subs/" + subid;
}
/**
@@ -94,7 +95,7 @@ public class URLUtilities { * @return the URL
*/
public static String generateSubLogURL(int subid) {
- return "https://" + BaseServlet.prov_name + "/sublog/" + subid;
+ return "https://" + BaseServlet.provName + "/sublog/" + subid;
}
/**
@@ -137,8 +138,9 @@ public class URLUtilities { }
System.out.println("ALL PODS: " + Arrays.asList(BaseServlet.getPods()));
for (String pod : BaseServlet.getPods()) {
- if (!pod.equals(this_pod))
+ if (!pod.equals(this_pod)) {
other_pod = pod;
+ }
}
}
return other_pod;
|