X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=datarouter-prov%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fdmaap%2Fdatarouter%2Fprovisioning%2FBaseServlet.java;h=d916a21c0622c2680ef5e10378b309b0157a5d6b;hb=14b8d9552808063686e0c22760cec6e35b960b59;hp=92e52fce83c122e0bd13c6cdc157a2b21bcfaa19;hpb=8cbe8a88bc6dfe8673a33a017fe6a5a3e7ce86c3;p=dmaap%2Fdatarouter.git 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 old mode 100644 new mode 100755 index 92e52fce..d916a21c --- 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 @@ -28,6 +28,9 @@ import static com.att.eelf.configuration.Configuration.MDC_SERVER_FQDN; import static com.att.eelf.configuration.Configuration.MDC_SERVER_IP_ADDRESS; import static com.att.eelf.configuration.Configuration.MDC_SERVICE_NAME; +import static com.att.eelf.configuration.Configuration.MDC_KEY_REQUEST_ID; + + import java.io.IOException; import java.io.InputStream; @@ -36,18 +39,13 @@ import java.net.UnknownHostException; import java.security.cert.X509Certificate; import java.sql.Connection; import java.sql.SQLException; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; -import java.util.List; -import java.util.ArrayList; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; +import org.apache.commons.lang3.StringUtils; import org.apache.log4j.Logger; import org.json.JSONObject; import org.json.JSONTokener; @@ -66,8 +64,17 @@ import org.onap.dmaap.datarouter.provisioning.utils.DB; import org.onap.dmaap.datarouter.provisioning.utils.ThrottleFilter; import org.json.JSONException; import org.slf4j.MDC; +import org.slf4j.Marker; +import org.slf4j.MarkerFactory; +import java.util.HashSet; +import java.util.Map; import java.util.Properties; +import java.util.Set; +import java.util.List; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.UUID; import java.util.regex.Pattern; import javax.mail.Message; import javax.mail.MessagingException; @@ -79,180 +86,242 @@ 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"; - public static final String GROUPLIST_CONTENT_TYPE = "application/vnd.att-dr.fegrouped-list; version=1.0"; + static final String GROUP_BASECONTENT_TYPE = "application/vnd.att-dr.group"; + static final String GROUPFULL_CONTENT_TYPE = "application/vnd.att-dr.group-full; version=2.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) */ - private static Set authorizedAddressesAndNetworks = new HashSet(); - /** The set of authorized names; pulled from the DB (PROV_AUTH_SUBJECTS) */ - private static Set authorizedNames = new HashSet(); - /** 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 */ + 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 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 authorizedAddressesAndNetworks = new HashSet<>(); + /** + * The set of authorized names; pulled from the DB (PROV_AUTH_SUBJECTS) + */ + private static Set authorizedNames = new HashSet<>(); + /** + * 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 + */ + private static String provName = "feeds-drtr.web.att.com"; + + /** + * The standard FQDN of the ACTIVE provisioning server in this Data Router ecosystem + */ + private static String activeProvName = "feeds-drtr.web.att.com"; + + 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); try { thishost = InetAddress.getLocalHost(); loopback = InetAddress.getLoopbackAddress(); - checkHttpsRelaxation(); //Data Router Subscriber HTTPS Relaxation feature USERSTORYID:US674047. + //checkHttpsRelaxation(); //Data Router Subscriber HTTPS Relaxation feature USERSTORYID:US674047. } catch (UnknownHostException e) { // 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 +334,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 +357,148 @@ 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)) - return true; - // Also allow the "special subnet" access - if (addressMatchesNetwork(ip, special_subnet_secondary)) - return true; - if (addressMatchesNetwork(ip, special_subnet)) + } + if (loopback != null && ip.equals(loopback)) { 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 map = Parameters.getParameters(); - require_secure = getBoolean(map, Parameters.PROV_REQUIRE_SECURE); - require_cert = getBoolean(map, Parameters.PROV_REQUIRE_CERT); + Map 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); + 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; 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 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 +507,12 @@ 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; 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 +521,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 +539,44 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider { try { mailprops.load(inStream); } catch (IOException e) { - intlogger.fatal("PROV9003 Opening properties: "+e.getMessage()); - e.printStackTrace(); + intlogger.fatal("PROV9003 Opening properties: " + e.getMessage()); 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 relaxation: " + 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) { - e.printStackTrace(); + notifyPSTeam(p.get("org.onap.dmaap.datarouter.provserver.https.relax.notify").toString()); + } catch (Exception e) { + intlogger.warn("Exception: " + e.getMessage()); } - } + } 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 +591,108 @@ 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); } } + public static String getProvName() { + return provName; + } + + public static String getActiveProvName() { + return activeProvName; + } /** * 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 +700,23 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider { rv = bean.doInsert(conn); } catch (SQLException e) { rv = false; - intlogger.warn("PROV0005 doInsert: "+e.getMessage()); - e.printStackTrace(); + intlogger.warn("PROV0005 doInsert: " + e.getMessage()); } 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 +724,23 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider { rv = bean.doUpdate(conn); } catch (SQLException e) { rv = false; - intlogger.warn("PROV0006 doUpdate: "+e.getMessage()); - e.printStackTrace(); + intlogger.warn("PROV0006 doUpdate: " + e.getMessage()); } 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 +748,26 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider { rv = bean.doDelete(conn); } catch (SQLException e) { rv = false; - intlogger.warn("PROV0007 doDelete: "+e.getMessage()); - e.printStackTrace(); + intlogger.warn("PROV0007 doDelete: " + e.getMessage()); } finally { - if (conn != null) + if (conn != null) { db.release(conn); + } } return rv; } - private static boolean getBoolean(Map map, String name) { + + private static boolean getBoolean(Map map, String name) { String s = map.get(name); return (s != null) && s.equalsIgnoreCase("true"); } - private static String getString(Map map, String name, String dflt) { + + private static String getString(Map map, String name, String dflt) { String s = map.get(name); return (s != null) ? s : dflt; } - private static int getInt(Map map, String name, int dflt) { + + private static int getInt(Map map, String name, int dflt) { try { String s = map.get(name); return Integer.parseInt(s); @@ -673,16 +775,18 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider { return dflt; } } - private static Set getSet(Map map, String name) { - Set set = new HashSet(); + + private static Set getSet(Map map, String name) { + Set set = new HashSet<>(); String s = map.get(name); if (s != null) { String[] pp = s.split("\\|"); if (pp != null) { for (String t : pp) { String t2 = t.trim(); - if (t2.length() > 0) + if (t2.length() > 0) { set.add(t2); + } } } } @@ -690,36 +794,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 map = new HashMap(); - public ContentHeader() { + private Map map = new HashMap<>(); + + 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 +840,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 +849,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 +900,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) { + intlogger.error("JSONException: " + e.getMessage()); + } } return false; @@ -812,9 +930,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 +956,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(); } } @@ -851,17 +971,36 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider { } /* - * @Method - setIpAndFqdnForEelf - Rally:US664892 + * @Method - setIpFqdnRequestIDandInvocationIDForEelf + * @Params - method, prints method name in EELF log. + * @Params- Req, Request used to get RequestId and InvocationId + */ + void setIpFqdnRequestIDandInvocationIDForEelf(String method, HttpServletRequest req) { + setIpFqdnForEelf(method); + setMDC(req, "X-ONAP-RequestID", MDC_KEY_REQUEST_ID); + setMDC(req, "X-InvocationID", "InvocationId"); + } + + void setMDC(HttpServletRequest req, String headerName, String keyName) { + String mdcId = req.getHeader(headerName); + if (StringUtils.isBlank(mdcId)) { + mdcId = UUID.randomUUID().toString(); + } + MDC.put(keyName, mdcId); + } + + /* + * @Method - setIpFqdnRequestIdForEelf - Rally:US664892 * @Params - method, prints method name in EELF log. */ - protected void setIpAndFqdnForEelf(String method) { + void setIpFqdnForEelf(String method) { MDC.clear(); MDC.put(MDC_SERVICE_NAME, method); try { MDC.put(MDC_SERVER_FQDN, InetAddress.getLocalHost().getHostName()); MDC.put(MDC_SERVER_IP_ADDRESS, InetAddress.getLocalHost().getHostAddress()); } catch (Exception e) { - e.printStackTrace(); + intlogger.error("Exception: " + e.getMessage()); } }