diff options
Diffstat (limited to 'datarouter-prov/src')
43 files changed, 2090 insertions, 2222 deletions
diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/authz/impl/AuthRespImpl.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/authz/impl/AuthRespImpl.java index f3278332..c7d71996 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/authz/impl/AuthRespImpl.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/authz/impl/AuthRespImpl.java @@ -44,21 +44,23 @@ public class AuthRespImpl implements AuthorizationResponse { /** Constructor. This version will not be used in Data Router R1 since we will not have advice and obligations.
*
* @param authorized flag indicating whether the response carried a permit response (<code>true</code>)
- * or something else (<code>false</code>).
+ * or something else (<code>false</code>).
* @param advice list of advice elements returned in the response.
* @param obligations list of obligation elements returned in the response.
*/
- public AuthRespImpl(boolean authorized, List<AuthorizationResponseSupplement> advice, List<AuthorizationResponseSupplement> obligations) {
+ private AuthRespImpl(boolean authorized, List<AuthorizationResponseSupplement> advice,
+ List<AuthorizationResponseSupplement> obligations) {
this.authorized = authorized;
- this.advice = (advice == null ? null : new ArrayList<AuthorizationResponseSupplement> (advice));
- this.obligations = (obligations == null ? null : new ArrayList<AuthorizationResponseSupplement> (obligations));
+ this.advice = (advice == null ? null : new ArrayList<>(advice));
+ this.obligations = (obligations == null ? null : new ArrayList<>(obligations));
}
/** Constructor. Simple version for authorization responses that have no advice and no obligations.
*
- * @param authorized flag indicating whether the response carried a permit (<code>true</code>) or something else (<code>false</code>).
+ * @param authorized flag indicating whether the response carried a permit (<code>true</code>)
+ * or something else (<code>false</code>).
*/
- public AuthRespImpl(boolean authorized) {
+ AuthRespImpl(boolean authorized) {
this(authorized, null, null);
}
@@ -69,25 +71,25 @@ public class AuthRespImpl implements AuthorizationResponse { */
@Override
public boolean isAuthorized() {
- return authorized;
+ return authorized;
}
/**
* Returns any advice elements that were included in the authorization response.
*
- * @return A list of objects implementing the <code>AuthorizationResponseSupplement</code> interface, with each object representing an
- * advice element from the authorization response.
+ * @return A list of objects implementing the <code>AuthorizationResponseSupplement</code> interface,
+ * with each object representing an advice element from the authorization response.
*/
@Override
public List<AuthorizationResponseSupplement> getAdvice() {
- return advice;
+ return advice;
}
/**
* Returns any obligation elements that were included in the authorization response.
*
- * @return A list of objects implementing the <code>AuthorizationResponseSupplement</code> interface, with each object representing an
- * obligation element from the authorization response.
+ * @return A list of objects implementing the <code>AuthorizationResponseSupplement</code> interface,
+ * with each object representing an obligation element from the authorization response.
*/
@Override
public List<AuthorizationResponseSupplement> getObligations() {
diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/authz/impl/AuthRespSupplementImpl.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/authz/impl/AuthRespSupplementImpl.java index d995270e..b61c00e5 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/authz/impl/AuthRespSupplementImpl.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/authz/impl/AuthRespSupplementImpl.java @@ -36,17 +36,17 @@ import org.onap.dmaap.datarouter.authz.AuthorizationResponseSupplement; */
public class AuthRespSupplementImpl implements AuthorizationResponseSupplement {
- private String id = null;
- private Map<String, String> attributes = null;
+ private String id;
+ private Map<String, String> attributes;
/** Constructor, available within the package.
*
* @param id The identifier for the advice or obligation element
* @param attributes The attributes (name-value pairs) for the advice or obligation element.
*/
- AuthRespSupplementImpl (String id, Map<String, String> attributes) {
+ AuthRespSupplementImpl(String id, Map<String, String> attributes) {
this.id = id;
- this.attributes = new HashMap<String,String>(attributes);
+ this.attributes = new HashMap<>(attributes);
}
/** Return the identifier for the supplementary information element.
diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/authz/impl/AuthzResource.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/authz/impl/AuthzResource.java index 0357fa74..c248468f 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/authz/impl/AuthzResource.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/authz/impl/AuthzResource.java @@ -30,7 +30,6 @@ import java.util.regex.Pattern; /** Internal representation of an authorization resource (the entity to which access is being requested). Consists
* of a type and an identifier. The constructor takes the request URI from an HTTP request and checks it against
* patterns for the the different resource types. In DR R1, there are four resource types:
- * <ul>
* <li>the feeds collection resource, the target of POST requests to create a new feed and GET requests to list
* the existing feeds. This is the root resource for the DR provisioning system, and it has no explicit id.
* </li>
@@ -53,10 +52,10 @@ public class AuthzResource { private String id = "";
/* Construct an AuthzResource by matching a request URI against the various patterns */
- public AuthzResource(String rURI) {
- if (rURI != null) {
+ AuthzResource(String requestUri) {
+ if (requestUri != null) {
for (ResourceType t : ResourceType.values()) {
- Matcher m = t.getPattern().matcher(rURI);
+ Matcher m = t.getPattern().matcher(requestUri);
if (m.find(0)) {
this.type = t;
if (m.group("id") != null) {
@@ -83,13 +82,13 @@ public class AuthzResource { */
public enum ResourceType {
FEEDS_COLLECTION("((://[^/]+/)|(^/))(?<id>)$"),
- SUBS_COLLECTION ("((://[^/]+/)|(^/{0,1}))subscribe/(?<id>[^/]+)$"),
+ SUBS_COLLECTION("((://[^/]+/)|(^/{0,1}))subscribe/(?<id>[^/]+)$"),
FEED("((://[^/]+/)|(^/{0,1}))feed/(?<id>[^/]+)$"),
SUB("((://[^/]+/)|(^/{0,1}))subs/(?<id>[^/]+)$");
private Pattern uriPattern;
- private ResourceType(String patternString) {
+ ResourceType(String patternString) {
this.uriPattern = Pattern.compile(patternString);
}
diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/authz/impl/ProvAuthorizer.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/authz/impl/ProvAuthorizer.java index 745e339d..595b626c 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/authz/impl/ProvAuthorizer.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/authz/impl/ProvAuthorizer.java @@ -23,17 +23,15 @@ package org.onap.dmaap.datarouter.authz.impl; -import java.util.Map; - -import javax.servlet.http.HttpServletRequest; - import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; +import java.util.Map; +import javax.servlet.http.HttpServletRequest; import org.onap.dmaap.datarouter.authz.AuthorizationResponse; import org.onap.dmaap.datarouter.authz.Authorizer; import org.onap.dmaap.datarouter.authz.impl.AuthzResource.ResourceType; -/** Authorizer for the provisioning API for Data Router R1 +/** Authorizer for the provisioning API for Data Router R1. * * @author J. F. Lucas * @@ -45,6 +43,7 @@ public class ProvAuthorizer implements Authorizer { private static final String SUBJECT_HEADER = "X-DMAAP-DR-ON-BEHALF-OF"; // HTTP header carrying requester identity private static final String SUBJECT_HEADER_GROUP = "X-DMAAP-DR-ON-BEHALF-OF-GROUP"; // HTTP header carrying requester identity by group Rally : US708115 + /** Constructor. For the moment, do nothing special. Make it a singleton? * */ @@ -63,7 +62,7 @@ public class ProvAuthorizer implements Authorizer { */ @Override public AuthorizationResponse decide(HttpServletRequest request) { - return this.decide(request, null); + return this.decide(request, null); } /** @@ -79,80 +78,66 @@ public class ProvAuthorizer implements Authorizer { @Override public AuthorizationResponse decide(HttpServletRequest request, Map<String, String> additionalAttrs) { - log.trace ("Entering decide()"); - + log.trace("Entering decide()"); boolean decision = false; - // Extract interesting parts of the HTTP request String method = request.getMethod(); AuthzResource resource = new AuthzResource(request.getRequestURI()); - String subject = (request.getHeader(SUBJECT_HEADER)); // identity of the requester - String subjectgroup = (request.getHeader(SUBJECT_HEADER_GROUP)); // identity of the requester by group Rally : US708115 - - log.trace("Method: " + method + " -- Type: " + resource.getType() + " -- Id: " + resource.getId() + - " -- Subject: " + subject); + String subject = (request.getHeader(SUBJECT_HEADER)); + String subjectgroup = (request.getHeader(SUBJECT_HEADER_GROUP)); + log.trace("Method: " + method + " -- Type: " + resource.getType() + " -- Id: " + resource.getId() + + " -- Subject: " + subject); // Choose authorization method based on the resource type ResourceType resourceType = resource.getType(); if (resourceType != null) { - switch (resourceType) { - - case FEEDS_COLLECTION: - decision = allowFeedsCollectionAccess(resource, method, subject, subjectgroup); - break; - - case SUBS_COLLECTION: - decision = allowSubsCollectionAccess(resource, method, subject, subjectgroup); - break; - - case FEED: - decision = allowFeedAccess(resource, method, subject, subjectgroup); - break; - - case SUB: - decision = allowSubAccess(resource, method, subject, subjectgroup); - break; - - default: - decision = false; - break; + case FEEDS_COLLECTION: + decision = allowFeedsCollectionAccess(method); + break; + case SUBS_COLLECTION: + decision = allowSubsCollectionAccess(method); + break; + case FEED: + decision = allowFeedAccess(resource, method, subject, subjectgroup); + break; + case SUB: + decision = allowSubAccess(resource, method, subject, subjectgroup); + break; + default: + decision = false; + break; } } - log.debug("Exit decide(): " + method + "|" + resourceType + "|" + resource.getId() + "|" + subject + " ==> " + decision); + log.debug("Exit decide(): " + method + "|" + resourceType + "|" + resource.getId() + "|" + + subject + " ==> " + decision); return new AuthRespImpl(decision); } - private boolean allowFeedsCollectionAccess(AuthzResource resource, String method, String subject, String subjectgroup) { - + private boolean allowFeedsCollectionAccess(String method) { // Allow GET or POST unconditionally return method != null && ("GET".equalsIgnoreCase(method) || "POST".equalsIgnoreCase(method)); } - private boolean allowSubsCollectionAccess(AuthzResource resource, String method, String subject, String subjectgroup) { - + private boolean allowSubsCollectionAccess(String method) { // Allow GET or POST unconditionally return method != null && ("GET".equalsIgnoreCase(method) || "POST".equalsIgnoreCase(method)); } - private boolean allowFeedAccess(AuthzResource resource, String method, String subject, String subjectgroup) { + private boolean allowFeedAccess(AuthzResource resource, String method, String subject, String subjectgroup) { boolean decision = false; - // Allow GET, PUT, or DELETE if requester (subject) is the owner (publisher) of the feed - if ( method != null && ("GET".equalsIgnoreCase(method) || "PUT".equalsIgnoreCase(method) || - "DELETE".equalsIgnoreCase(method))) { + if ( method != null && ("GET".equalsIgnoreCase(method) || "PUT".equalsIgnoreCase(method) || "DELETE".equalsIgnoreCase(method))) { String owner = provData.getFeedOwner(resource.getId()); decision = (owner != null) && owner.equals(subject); - //Verifying by group Rally : US708115 - if(subjectgroup != null) { - String feedowner = provData.getGroupByFeedGroupId(subject, resource.getId()); - decision = (feedowner != null) && feedowner.equals(subjectgroup); + if (subjectgroup != null) { + String feedOwner = provData.getGroupByFeedGroupId(subject, resource.getId()); + decision = (feedOwner != null) && feedOwner.equals(subjectgroup); } } - return decision; } @@ -160,14 +145,13 @@ public class ProvAuthorizer implements Authorizer { boolean decision = false; // Allow GET, PUT, or DELETE if requester (subject) is the owner of the subscription (subscriber) - if (method != null && ("GET".equalsIgnoreCase(method) || "PUT".equalsIgnoreCase(method) || - "DELETE".equalsIgnoreCase(method) || "POST".equalsIgnoreCase(method))) { + if (method != null && ("GET".equalsIgnoreCase(method) || "PUT".equalsIgnoreCase(method) || "DELETE".equalsIgnoreCase(method) || "POST".equalsIgnoreCase(method))) { String owner = provData.getSubscriptionOwner(resource.getId()); decision = (owner != null) && owner.equals(subject); //Verifying by group Rally : US708115 - if(subjectgroup != null) { + if (subjectgroup != null) { String feedowner = provData.getGroupBySubGroupId(subject, resource.getId()); decision = (feedowner != null) && feedowner.equals(subjectgroup); } 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 ef106ab4..e730db4a 100755 --- 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 @@ -24,30 +24,33 @@ package org.onap.dmaap.datarouter.provisioning; +import static com.att.eelf.configuration.Configuration.MDC_KEY_REQUEST_ID; 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; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; import java.net.InetAddress; import java.net.UnknownHostException; +import java.security.GeneralSecurityException; import java.security.cert.X509Certificate; import java.sql.Connection; import java.sql.SQLException; - +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.Set; +import java.util.UUID; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; - -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; import org.apache.commons.lang3.StringUtils; +import org.jetbrains.annotations.Nullable; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -55,21 +58,19 @@ import org.json.JSONTokener; import org.onap.dmaap.datarouter.authz.Authorizer; import org.onap.dmaap.datarouter.authz.impl.ProvAuthorizer; import org.onap.dmaap.datarouter.authz.impl.ProvDataProvider; -import org.onap.dmaap.datarouter.provisioning.beans.*; +import org.onap.dmaap.datarouter.provisioning.beans.Deleteable; +import org.onap.dmaap.datarouter.provisioning.beans.Feed; +import org.onap.dmaap.datarouter.provisioning.beans.Group; +import org.onap.dmaap.datarouter.provisioning.beans.Insertable; +import org.onap.dmaap.datarouter.provisioning.beans.NodeClass; +import org.onap.dmaap.datarouter.provisioning.beans.Parameters; +import org.onap.dmaap.datarouter.provisioning.beans.Subscription; +import org.onap.dmaap.datarouter.provisioning.beans.Updateable; import org.onap.dmaap.datarouter.provisioning.utils.DB; import org.onap.dmaap.datarouter.provisioning.utils.PasswordProcessor; import org.onap.dmaap.datarouter.provisioning.utils.ThrottleFilter; import org.slf4j.MDC; -import javax.mail.*; -import javax.mail.internet.InternetAddress; -import javax.mail.internet.MimeBodyPart; -import javax.mail.internet.MimeMessage; -import javax.mail.internet.MimeMultipart; -import java.security.GeneralSecurityException; -import java.util.*; -import java.util.regex.Pattern; - /** * This is the base class for all Servlets in the provisioning code. It provides standard constants and some common @@ -94,10 +95,10 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider { static final String CREATE_PERMISSION = "create"; static final String EDIT_PERMISSION = "edit"; static final String DELETE_PERMISSION = "delete"; - static final String PUBLISH_PERMISSION = "publish"; - static final String SUSPEND_PERMISSION = "suspend"; - static final String RESTORE_PERMISSION = "restore"; - static final String SUBSCRIBE_PERMISSION = "subscribe"; + private static final String PUBLISH_PERMISSION = "publish"; + private static final String SUSPEND_PERMISSION = "suspend"; + private static final String RESTORE_PERMISSION = "restore"; + private static final String SUBSCRIBE_PERMISSION = "subscribe"; static final String APPROVE_SUB_PERMISSION = "approveSub"; static final String FEED_BASECONTENT_TYPE = "application/vnd.dmaap-dr.feed"; @@ -113,7 +114,7 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider { //Adding groups functionality, ...1610 static final String GROUP_BASECONTENT_TYPE = "application/vnd.dmaap-dr.group"; static final String GROUP_CONTENT_TYPE = "application/vnd.dmaap-dr.group; version=2.0"; - public static final String GROUPFULL_CONTENT_TYPE = "application/vnd.dmaap-dr.group-full; version=2.0"; + static final String GROUPFULL_CONTENT_TYPE = "application/vnd.dmaap-dr.group-full; version=2.0"; public static final String GROUPLIST_CONTENT_TYPE = "application/vnd.dmaap-dr.fegrouped-list; version=1.0"; @@ -130,127 +131,119 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider { 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 //Common Errors - public static final String MISSING_ON_BEHALF = "Missing X-DMAAP-DR-ON-BEHALF-OF header."; - public static final String MISSING_FEED = "Missing or bad feed number."; - public static final String POLICY_ENGINE = "Policy Engine disallows access."; - public static final String UNAUTHORIZED = "Unauthorized."; - public static final String BAD_SUB = "Missing or bad subscription number."; - public static final String BAD_JSON = "Badly formed JSON"; - public static final String BAD_URL = "Bad URL."; + static final String MISSING_ON_BEHALF = "Missing X-DMAAP-DR-ON-BEHALF-OF header."; + static final String MISSING_FEED = "Missing or bad feed number."; + static final String POLICY_ENGINE = "Policy Engine disallows access."; + static final String UNAUTHORIZED = "Unauthorized."; + static final String BAD_SUB = "Missing or bad subscription number."; + static final String BAD_JSON = "Badly formed JSON"; + static final String BAD_URL = "Bad URL."; public static final String API = "/api/"; - public static final String LOGS = "/logs/"; - public static final String TEXT_CT = "text/plain"; - public static final String INGRESS = "/ingress/"; - public static final String EGRESS = "/egress/"; - public static final String NETWORK = "/network/"; - public static final String GROUPID = "groupid"; + static final String LOGS = "/logs/"; + static final String TEXT_CT = "text/plain"; + static final String INGRESS = "/ingress/"; + static final String EGRESS = "/egress/"; + static final String NETWORK = "/network/"; + static final String GROUPID = "groupid"; public static final String FEEDID = "feedid"; - public static final String FEEDIDS = "feedids"; - public static final String SUBID = "subid"; - public static final String EVENT_TYPE = "eventType"; - public static final String OUTPUT_TYPE = "output_type"; - public static final String START_TIME = "start_time"; - public static final String END_TIME = "end_time"; - public static final String REASON_SQL = "reasonSQL"; + static final String FEEDIDS = "feedids"; + static final String SUBID = "subid"; + static final String EVENT_TYPE = "eventType"; + static final String OUTPUT_TYPE = "output_type"; + static final String START_TIME = "start_time"; + static final String END_TIME = "end_time"; + static final String REASON_SQL = "reasonSQL"; /** - * A boolean to trigger one time "provisioning changed" event on startup + * 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) + * 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) + * 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) + * The set of authorized addresses and networks; pulled from the DB (PROV_AUTH_ADDRESSES). */ private static Set<String> authorizedAddressesAndNetworks = new HashSet<>(); /** - * 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<>(); /** - * The FQDN of the initially "active" provisioning server in this Data Router ecosystem + * 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 + * 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 + * 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 + * "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 + * "Timer 2" - used to determine when to notify nodes of provisioning changes. */ private static long pokeTimer2; /** - * Array of nodes names and/or FQDNs + * Array of nodes names and/or FQDNs. */ private static String[] nodes = new String[0]; /** - * [DATARTR-27] Poke all the DR nodes : Array of nodes names and/or FQDNs - */ - private static String[] drnodes = 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) + * 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) + * 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 + * The current number of feeds in the system. */ static int activeFeeds = 0; /** - * The current number of subscriptions in the system + * The current number of subscriptions in the system. */ static int activeSubs = 0; /** - * The domain used to generate a FQDN from the "bare" node names + * 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 + * 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_POD provisioning server in this Data Router ecosystem + * The standard FQDN of the ACTIVE_POD provisioning server in this Data Router ecosystem. */ private static String activeProvName = "feeds-drtr.web.att.com"; - //Adding new param for static Routing - Rally:US664862-1610 - private static String staticRoutingNodes = STATIC_ROUTING_NODES; - /** - * This logger is used to log provisioning events + * This logger is used to log provisioning events. */ protected static EELFLogger eventlogger; /** @@ -258,21 +251,17 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider { */ protected static EELFLogger 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 + * 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; - - 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() @@ -285,10 +274,10 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider { * Initialize data common to all the provisioning server servlets. */ protected BaseServlet() { - if(eventlogger == null) { - this.eventlogger = EELFManager.getInstance().getLogger("EventLog"); + if (eventlogger == null) { + eventlogger = EELFManager.getInstance().getLogger("EventLog"); } - if(intlogger == null) { + if (intlogger == null) { this.intlogger = EELFManager.getInstance().getLogger("InternalLog"); } if (authz == null) { @@ -329,7 +318,7 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider { } /** - * Read the request's input stream and return a JSONObject from it + * 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 @@ -348,35 +337,40 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider { } /** - * This method encrypt/decrypt the key in the JSON passed by user request inside the authorisation header object in request before logging the JSON. + * This method encrypt/decrypt the key in the JSON passed by user request inside the authorisation + * header object in request before logging the JSON. * - * @param jo- the JSON passed in http request. - * @param maskKey- the key to be masked in the JSON passed. - * @param action- whether to mask the key or unmask it in a JSON passed. + * @param jo the JSON passed in http request. + * @param maskKey the key to be masked in the JSON passed. + * @param action whether to mask the key or unmask it in a JSON passed. * @return the JSONObject, or null if the stream cannot be parsed. */ - public static JSONObject maskJSON(JSONObject jo, String maskKey, boolean action) { + static JSONObject maskJSON(JSONObject jo, String maskKey, boolean action) { if (!jo.isNull("authorization")) { - JSONObject j2 = jo.getJSONObject("authorization"); - JSONArray ja = j2.getJSONArray("endpoint_ids"); - for (int i = 0; i < ja.length(); i++) { - if ((!ja.getJSONObject(i).isNull(maskKey))) { - String password = ja.getJSONObject(i).get(maskKey).toString(); - try { - if (action) { - ja.getJSONObject(i).put(maskKey, PasswordProcessor.encrypt(password)); - } else { - ja.getJSONObject(i).put(maskKey, PasswordProcessor.decrypt(password)); - } - } catch (JSONException | GeneralSecurityException e) { - intlogger.info("Error reading JSON while masking: " + e); - } + JSONArray endpointIds = jo.getJSONObject("authorization").getJSONArray("endpoint_ids"); + for (int index = 0; index < endpointIds.length(); index++) { + if ((!endpointIds.getJSONObject(index).isNull(maskKey))) { + String password = endpointIds.getJSONObject(index).get(maskKey).toString(); + processPassword(maskKey, action, endpointIds, index, password); } } } return jo; } + private static void processPassword(String maskKey, boolean action, JSONArray endpointIds, int index, + String password) { + try { + if (action) { + endpointIds.getJSONObject(index).put(maskKey, PasswordProcessor.encrypt(password)); + } else { + endpointIds.getJSONObject(index).put(maskKey, PasswordProcessor.decrypt(password)); + } + } catch (JSONException | GeneralSecurityException e) { + intlogger.info("Error reading JSON while masking: " + e); + } + } + /** * 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 @@ -393,20 +387,9 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider { if (requireSecure && !request.isSecure()) { return "Request must be made over an HTTPS connection."; } - // Is remote IP authorized? - String remote = request.getRemoteAddr(); - try { - boolean found = false; - InetAddress ip = InetAddress.getByName(remote); - for (String addrnet : authorizedAddressesAndNetworks) { - found |= addressMatchesNetwork(ip, addrnet); - } - if (!found) { - return "Unauthorized address: " + remote; - } - } catch (UnknownHostException e) { - intlogger.error("PROV0051 BaseServlet.isAuthorizedForProvisioning: " + e.getMessage(), e); - return "Unauthorized address: " + remote; + String remoteHostCheck = checkRemoteHostAuthorization(request); + if (remoteHostCheck != null) { + return remoteHostCheck; } // Does remote have a valid certificate? if (requireCert) { @@ -425,6 +408,26 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider { return null; } + @Nullable + private String checkRemoteHostAuthorization(HttpServletRequest request) { + // Is remote IP authorized? + String remote = request.getRemoteAddr(); + try { + boolean found = false; + InetAddress ip = InetAddress.getByName(remote); + for (String addrnet : authorizedAddressesAndNetworks) { + found |= addressMatchesNetwork(ip, addrnet); + } + if (!found) { + return "Unauthorized address: " + remote; + } + } catch (UnknownHostException e) { + intlogger.error("PROV0051 BaseServlet.isAuthorizedForProvisioning: " + e.getMessage(), e); + return "Unauthorized address: " + remote; + } + return null; + } + /** * Check if the remote IP address is authorized to see the /internal URL tree. * @@ -438,19 +441,19 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider { } InetAddress ip = InetAddress.getByName(request.getRemoteAddr()); for (InetAddress node : getNodeAddresses()) { - if (node != null && ip.equals(node)) { + if (ip.equals(node)) { return true; } } for (InetAddress pod : getPodAddresses()) { - if (pod != null && ip.equals(pod)) { + if (ip.equals(pod)) { return true; } } - if (thishost != null && ip.equals(thishost)) { + if (ip.equals(thishost)) { return true; } - if (loopback != null && ip.equals(loopback)) { + if (ip.equals(loopback)) { return true; } } catch (UnknownHostException e) { @@ -468,7 +471,7 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider { */ private static boolean addressMatchesNetwork(InetAddress ip, String s) { int mlen = -1; - int n = s.indexOf("/"); + int n = s.indexOf('/'); if (n >= 0) { mlen = Integer.parseInt(s.substring(n + 1)); s = s.substring(0, n); @@ -528,16 +531,16 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider { 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); - /** - * The domain used to generate a FQDN from the "bare" node names - */ + + // The domain used to generate a FQDN from the "bare" node names 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); 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 + + //Adding new param for static Routing - Rally:US664862-1610 + String staticRoutingNodes = getString(map, Parameters.STATIC_ROUTING_NODES, ""); activeFeeds = Feed.countActiveFeeds(); activeSubs = Subscription.countActiveSubscriptions(); try { @@ -559,9 +562,6 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider { } } - //[DATARTR-27] Poke all the DR nodes: assigning DR Nodes - drnodes = nodes.clone(); - //Reset Nodes arr after - removing static routing Nodes, Rally Userstory - US664862 . List<String> filterNodes = new ArrayList<>(); for (String node : nodes) { @@ -597,78 +597,11 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider { } } - - /** - * Data Router Subscriber HTTPS Relaxation feature USERSTORYID:US674047. Load mail properties. - * - * @author vs215k - **/ - private void loadMailProperties() { - if (mailprops == null) { - mailprops = new Properties(); - try (InputStream inStream = getClass().getClassLoader().getResourceAsStream(MAILCONFIG_FILE)) { - mailprops.load(inStream); - } catch (IOException e) { - intlogger.error("PROV9003 Opening properties: " + e.getMessage(), e); - System.exit(1); - } - } - } - - - /** - * 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) { - loadMailProperties(); //Load HTTPS Relex mail properties. - String[] emails = email.split(Pattern.quote("|")); - - Properties mailproperties = new Properties(); - mailproperties.put("mail.smtp.host", mailprops.get("com.att.dmaap.datarouter.mail.server")); - mailproperties.put("mail.transport.protocol", mailprops.get("com.att.dmaap.datarouter.mail.protocol")); - - Session session = Session.getDefaultInstance(mailproperties, null); - Multipart mp = new MimeMultipart(); - MimeBodyPart htmlPart = new MimeBodyPart(); - - try { - - 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]); - } - - 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); - - intlogger.info(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); - - } catch (MessagingException e) { - intlogger.error("Invalid email address, unable to send https relaxation mail to - : " + email, e); - } catch (UnknownHostException uhe) { - intlogger.error("UnknownHostException", uhe); - } - } - public static String getProvName() { return provName; } - public static String getActiveProvName() { + static String getActiveProvName() { return activeProvName; } @@ -682,21 +615,11 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider { } /** - * [DATARTR-27] Poke all the DR nodes - * Get an array of all node names in the DR network. - * - * @return an array of Strings - */ - public static String[] getDRNodes() { - return drnodes; - } - - /** * Get an array of all node InetAddresses in the DR network. * * @return an array of InetAddresses */ - public static InetAddress[] getNodeAddresses() { + private static InetAddress[] getNodeAddresses() { return nodeAddresses; } @@ -814,7 +737,7 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider { private static boolean getBoolean(Map<String, String> map, String name) { String s = map.get(name); - return (s != null) && "true".equalsIgnoreCase(s); + return "true".equalsIgnoreCase(s); } private static String getString(Map<String, String> map, String name, String dflt) { @@ -854,7 +777,7 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider { */ public class ContentHeader { - private String type = ""; + private String type; private Map<String, String> map = new HashMap<>(); ContentHeader() { @@ -870,7 +793,7 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider { return type; } - public String getAttribute(String key) { + String getAttribute(String key) { String s = map.get(key); if (s == null) { s = ""; @@ -976,19 +899,17 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider { /* * @Method - getGroupByFeedGroupId- Rally:US708115 * @Params - User to check in group and feedid which is assigned the group. - * @return - string value grupid/null + * @return - string value groupid/null */ @Override public String getGroupByFeedGroupId(String owner, String feedId) { try { - int n = Integer.parseInt(feedId); - Feed f = Feed.getFeedById(n); + Feed f = Feed.getFeedById(Integer.parseInt(feedId)); if (f != null) { int groupid = f.getGroupid(); if (groupid > 0) { Group group = Group.getGroupById(groupid); - assert group != null; - if (isUserMemberOfGroup(group, owner)) { + if (group != null && isUserMemberOfGroup(group, owner)) { return group.getAuthid(); } } @@ -1002,7 +923,7 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider { /* * @Method - getGroupBySubGroupId - Rally:US708115 * @Params - User to check in group and subid which is assigned the group. - * @return - string value grupid/null + * @return - string value groupid/null */ @Override public String getGroupBySubGroupId(String owner, String subId) { @@ -1013,8 +934,7 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider { int groupid = s.getGroupid(); if (groupid > 0) { Group group = Group.getGroupById(groupid); - assert group != null; - if (isUserMemberOfGroup(group, owner)) { + if (group != null && isUserMemberOfGroup(group, owner)) { return group.getAuthid(); } } diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/GroupServlet.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/GroupServlet.java index 544a4800..4432913f 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/GroupServlet.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/GroupServlet.java @@ -88,31 +88,6 @@ public class GroupServlet extends ProxyServlet { return; } - // Check with the Authorizer - /*AuthorizationResponse aresp = authz.decide(req); - if (! aresp.isAuthorized()) { - message = POLICY_ENGINE; - elr.setMessage(message); - elr.setResult(HttpServletResponse.SC_FORBIDDEN); - eventlogger.error(elr.toString()); - resp.sendError(HttpServletResponse.SC_FORBIDDEN, message); - return; - }*/ - - - /*ContentHeader ch = getContentHeader(req); - String ver = ch.getAttribute("version"); - if (!ch.getType().equals(GROUPLIST_CONTENT_TYPE) || !(ver.equals("1.0") || ver.equals("2.0"))) { - intlogger.debug("Content-type is: "+req.getHeader("Content-Type")); - message = "Incorrect content-type"; - elr.setMessage(message); - elr.setResult(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE); - eventlogger.error(elr.toString()); - resp.sendError(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE, message); - return; - }*/ - - int groupid = getIdFromPath(req); if (groupid < 0) { message = "Missing or bad group number."; diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/DeliveryExtraRecord.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/DeliveryExtraRecord.java index 2a959f3d..0a5258e2 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/DeliveryExtraRecord.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/DeliveryExtraRecord.java @@ -25,7 +25,6 @@ package org.onap.dmaap.datarouter.provisioning.beans;
import java.sql.PreparedStatement;
-import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.text.ParseException;
@@ -44,12 +43,7 @@ public class DeliveryExtraRecord extends BaseLogRecord { this.subid = Integer.parseInt(pp[4]);
this.contentLength2 = Long.parseLong(pp[6]);
}
- public DeliveryExtraRecord(ResultSet rs) throws SQLException {
- super(rs);
- // Note: because this record should be "rare" these fields are mapped to unconventional fields in the DB
- this.subid = rs.getInt("DELIVERY_SUBID");
- this.contentLength2 = rs.getInt("CONTENT_LENGTH_2");
- }
+
@Override
public void load(PreparedStatement ps) throws SQLException {
ps.setString(1, "dlx"); // field 1: type
diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/EgressRoute.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/EgressRoute.java index e766e704..a78a9c18 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/EgressRoute.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/EgressRoute.java @@ -24,6 +24,8 @@ package org.onap.dmaap.datarouter.provisioning.beans;
+import com.att.eelf.configuration.EELFLogger;
+import com.att.eelf.configuration.EELFManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
@@ -32,9 +34,6 @@ import java.sql.Statement; import java.util.Objects;
import java.util.SortedSet;
import java.util.TreeSet;
-
-import com.att.eelf.configuration.EELFLogger;
-import com.att.eelf.configuration.EELFManager;
import org.json.JSONObject;
import org.onap.dmaap.datarouter.provisioning.utils.DB;
@@ -47,10 +46,22 @@ import org.onap.dmaap.datarouter.provisioning.utils.DB; public class EgressRoute extends NodeClass implements Comparable<EgressRoute> {
private static EELFLogger intlogger = EELFManager.getInstance().getLogger("InternalLog");
- private static final String SQLEXCEPTION = "SQLException: ";
private final int subid;
private final int nodeid;
+ public EgressRoute(int subid, int nodeid) {
+ this.subid = subid;
+ this.nodeid = nodeid;
+ // Note: unlike for Feeds, it subscriptions can be removed from the tables, so it is
+ // possible that an orphan ERT entry can exist if a sub is removed.
+ // if (Subscription.getSubscriptionById(subid) == null)
+ // throw new IllegalArgumentException("No such subscription: "+subid);
+ }
+
+ public EgressRoute(int subid, String node) {
+ this(subid, lookupNodeName(node));
+ }
+
/**
* Get a set of all Egress Routes in the DB. The set is sorted according to the natural sorting order of the routes
* (based on the subscription ID in each route).
@@ -59,27 +70,30 @@ public class EgressRoute extends NodeClass implements Comparable<EgressRoute> { */
public static SortedSet<EgressRoute> getAllEgressRoutes() {
SortedSet<EgressRoute> set = new TreeSet<>();
- try {
- DB db = new DB();
- @SuppressWarnings("resource")
- Connection conn = db.getConnection();
+ DB db = new DB();
+ String sql = "select SUBID, NODEID from EGRESS_ROUTES";
+ try (Connection conn = db.getConnection()) {
try (Statement stmt = conn.createStatement()) {
- try (ResultSet rs = stmt.executeQuery("select SUBID, NODEID from EGRESS_ROUTES")) {
- while (rs.next()) {
- int subid = rs.getInt("SUBID");
- int nodeid = rs.getInt("NODEID");
- set.add(new EgressRoute(subid, nodeid));
- }
+ try (ResultSet rs = stmt.executeQuery(sql)) {
+ addEgressRouteToSet(set, rs);
}
+ } finally {
+ db.release(conn);
}
-
- db.release(conn);
} catch (SQLException e) {
intlogger.error("PROV0008 EgressRoute.getAllEgressRoutes: " + e.getMessage(), e);
}
return set;
}
+ private static void addEgressRouteToSet(SortedSet<EgressRoute> set, ResultSet rs) throws SQLException {
+ while (rs.next()) {
+ int subid = rs.getInt("SUBID");
+ int nodeid = rs.getInt("NODEID");
+ set.add(new EgressRoute(subid, nodeid));
+ }
+ }
+
/**
* Get a single Egress Route for the subscription <i>sub</i>.
*
@@ -88,69 +102,35 @@ public class EgressRoute extends NodeClass implements Comparable<EgressRoute> { */
public static EgressRoute getEgressRoute(int sub) {
EgressRoute v = null;
- PreparedStatement ps = null;
- try {
- DB db = new DB();
- @SuppressWarnings("resource")
- Connection conn = db.getConnection();
- String sql = "select NODEID from EGRESS_ROUTES where SUBID = ?";
- ps = conn.prepareStatement(sql);
+ DB db = new DB();
+ String sql = "select NODEID from EGRESS_ROUTES where SUBID = ?";
+ try (Connection conn = db.getConnection();
+ PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setInt(1, sub);
try (ResultSet rs = ps.executeQuery()) {
if (rs.next()) {
int node = rs.getInt("NODEID");
v = new EgressRoute(sub, node);
}
+ } finally {
+ db.release(conn);
}
- ps.close();
- db.release(conn);
} catch (SQLException e) {
intlogger.error("PROV0009 EgressRoute.getEgressRoute: " + e.getMessage(), e);
- } finally {
- try {
- if (ps != null) {
- ps.close();
- }
- } catch (SQLException e) {
- intlogger.error(SQLEXCEPTION + e.getMessage(), e);
- }
}
return v;
}
- public EgressRoute(int subid, int nodeid) {
- this.subid = subid;
- this.nodeid = nodeid;
-// Note: unlike for Feeds, it subscriptions can be removed from the tables, so it is
-// possible that an orphan ERT entry can exist if a sub is removed.
-// if (Subscription.getSubscriptionById(subid) == null)
-// throw new IllegalArgumentException("No such subscription: "+subid);
- }
-
- public EgressRoute(int subid, String node) {
- this(subid, lookupNodeName(node));
- }
-
@Override
public boolean doDelete(Connection c) {
boolean rv = true;
- PreparedStatement ps = null;
- try {
- String sql = "delete from EGRESS_ROUTES where SUBID = ?";
- ps = c.prepareStatement(sql);
+ String sql = "delete from EGRESS_ROUTES where SUBID = ?";
+ try (PreparedStatement ps = c.prepareStatement(sql)) {
ps.setInt(1, subid);
ps.execute();
} catch (SQLException e) {
rv = false;
intlogger.error("PROV0007 doDelete: " + e.getMessage(), e);
- } finally {
- try {
- if (ps != null) {
- ps.close();
- }
- } catch (SQLException e) {
- intlogger.error(SQLEXCEPTION + e.getMessage(), e);
- }
}
return rv;
}
@@ -158,11 +138,9 @@ public class EgressRoute extends NodeClass implements Comparable<EgressRoute> { @Override
public boolean doInsert(Connection c) {
boolean rv = false;
- PreparedStatement ps = null;
- try {
+ String sql = "insert into EGRESS_ROUTES (SUBID, NODEID) values (?, ?)";
+ try (PreparedStatement ps = c.prepareStatement(sql)) {
// Create the NETWORK_ROUTES row
- String sql = "insert into EGRESS_ROUTES (SUBID, NODEID) values (?, ?)";
- ps = c.prepareStatement(sql);
ps.setInt(1, this.subid);
ps.setInt(2, this.nodeid);
ps.execute();
@@ -170,14 +148,6 @@ public class EgressRoute extends NodeClass implements Comparable<EgressRoute> { rv = true;
} catch (SQLException e) {
intlogger.warn("PROV0005 doInsert: " + e.getMessage(), e);
- } finally {
- try {
- if (ps != null) {
- ps.close();
- }
- } catch (SQLException e) {
- intlogger.error(SQLEXCEPTION + e.getMessage(), e);
- }
}
return rv;
}
@@ -185,24 +155,14 @@ public class EgressRoute extends NodeClass implements Comparable<EgressRoute> { @Override
public boolean doUpdate(Connection c) {
boolean rv = true;
- PreparedStatement ps = null;
- try {
- String sql = "update EGRESS_ROUTES set NODEID = ? where SUBID = ?";
- ps = c.prepareStatement(sql);
+ String sql = "update EGRESS_ROUTES set NODEID = ? where SUBID = ?";
+ try (PreparedStatement ps = c.prepareStatement(sql)) {
ps.setInt(1, nodeid);
ps.setInt(2, subid);
ps.executeUpdate();
} catch (SQLException e) {
rv = false;
intlogger.warn("PROV0006 doUpdate: " + e.getMessage(), e);
- } finally {
- try {
- if (ps != null) {
- ps.close();
- }
- } catch (SQLException e) {
- intlogger.error(SQLEXCEPTION + e.getMessage(), e);
- }
}
return rv;
}
diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/Feed.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/Feed.java index cab4d3d2..1cfd0f6b 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/Feed.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/Feed.java @@ -497,14 +497,6 @@ public class Feed extends Syncable { this.suspended = suspended;
}
- public Date getLast_mod() {
- return last_mod;
- }
-
- public Date getCreated_date() {
- return created_date;
- }
-
@Override
public JSONObject asJSONObject() {
JSONObject jo = new JSONObject();
diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/FeedEndpointID.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/FeedEndpointID.java index cd482c61..384d9f64 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/FeedEndpointID.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/FeedEndpointID.java @@ -39,10 +39,6 @@ public class FeedEndpointID implements JSONable { private String id;
private String password;
- public FeedEndpointID() {
- this("", "");
- }
-
public FeedEndpointID(String id, String password) {
this.id = id;
this.password = password;
diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/Group.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/Group.java index be4c6409..4d6b0ee2 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/Group.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/Group.java @@ -23,16 +23,19 @@ package org.onap.dmaap.datarouter.provisioning.beans;
+import com.att.eelf.configuration.EELFLogger;
+import com.att.eelf.configuration.EELFManager;
import java.io.InvalidObjectException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
-import java.util.*;
-
-import com.att.eelf.configuration.EELFLogger;
-import com.att.eelf.configuration.EELFManager;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Date;
+import java.util.List;
+import java.util.Objects;
import org.json.JSONObject;
import org.onap.dmaap.datarouter.provisioning.utils.DB;
@@ -44,8 +47,9 @@ import org.onap.dmaap.datarouter.provisioning.utils.DB; */
public class Group extends Syncable {
+ private static final String GROUP_ID_CONST = "groupid";
private static EELFLogger intlogger = EELFManager.getInstance().getLogger("InternalLog");
- private static int next_groupid = getMaxGroupID() + 1;
+ private static int nextGroupid = getMaxGroupID() + 1;
private static final String SQLEXCEPTION = "SQLException: ";
private int groupid;
@@ -56,6 +60,60 @@ public class Group extends Syncable { private String members;
private Date last_mod;
+ public Group() {
+ this("", "", "");
+ }
+
+ public Group(String name, String desc, String members) {
+ this.groupid = -1;
+ this.authid = "";
+ this.name = name;
+ this.description = desc;
+ this.members = members;
+ this.classification = "";
+ this.last_mod = new Date();
+ }
+
+
+ public Group(ResultSet rs) throws SQLException {
+ this.groupid = rs.getInt("GROUPID");
+ this.authid = rs.getString("AUTHID");
+ this.name = rs.getString("NAME");
+ this.description = rs.getString("DESCRIPTION");
+ this.classification = rs.getString("CLASSIFICATION");
+ this.members = rs.getString("MEMBERS");
+ this.last_mod = rs.getDate("LAST_MOD");
+ }
+
+
+ public Group(JSONObject jo) throws InvalidObjectException {
+ this("", "", "");
+ try {
+ // The JSONObject is assumed to contain a vnd.dmaap-dr.group representation
+ this.groupid = jo.optInt(GROUP_ID_CONST, -1);
+ String gname = jo.getString("name");
+ String gdescription = jo.getString("description");
+
+ this.authid = jo.getString("authid");
+ this.name = gname;
+ this.description = gdescription;
+ this.classification = jo.getString("classification");
+ this.members = jo.getString("members");
+
+ if (gname.length() > 50) {
+ throw new InvalidObjectException("Group name is too long");
+ }
+ if (gdescription.length() > 256) {
+ throw new InvalidObjectException("Group Description is too long");
+ }
+ } catch (InvalidObjectException e) {
+ throw e;
+ } catch (Exception e) {
+ intlogger.warn("Invalid JSON: " + e.getMessage(), e);
+ throw new InvalidObjectException("Invalid JSON: " + e.getMessage());
+ }
+ }
+
public static Group getGroupMatching(Group gup) {
String sql = String.format(
@@ -82,7 +140,7 @@ public class Group extends Syncable { return list.size() > 0 ? list.get(0) : null;
}
- public static Group getGroupByAuthId(String id) {
+ static Group getGroupByAuthId(String id) {
String sql = "select * from GROUPS where AUTHID = '" + id + "'";
List<Group> list = getGroupsForSQL(sql);
return list.size() > 0 ? list.get(0) : null;
@@ -93,7 +151,7 @@ public class Group extends Syncable { }
private static List<Group> getGroupsForSQL(String sql) {
- List<Group> list = new ArrayList<Group>();
+ List<Group> list = new ArrayList<>();
try {
DB db = new DB();
@SuppressWarnings("resource")
@@ -113,7 +171,7 @@ public class Group extends Syncable { return list;
}
- public static int getMaxGroupID() {
+ private static int getMaxGroupID() {
int max = 0;
try {
DB db = new DB();
@@ -133,108 +191,6 @@ public class Group extends Syncable { return max;
}
- public static Collection<String> getGroupsByClassfication(String classfication) {
- List<String> list = new ArrayList<>();
- String sql = "select * from GROUPS where classification = ?";
- try {
- DB db = new DB();
- @SuppressWarnings("resource")
- Connection conn = db.getConnection();
- try (PreparedStatement stmt = conn.prepareStatement(sql)) {
- stmt.setString(1, classfication);
- try (ResultSet rs = stmt.executeQuery()) {
- while (rs.next()) {
- int groupid = rs.getInt("groupid");
-
- }
- }
- }
- db.release(conn);
- } catch (SQLException e) {
- intlogger.error("PROV0002 getGroupsByClassfication: " + e.getMessage(), e);
- }
- return list;
- }
-
- /**
- * Return a count of the number of active subscriptions in the DB.
- *
- * @return the count
- */
- public static int countActiveSubscriptions() {
- int count = 0;
- try {
- DB db = new DB();
- @SuppressWarnings("resource")
- Connection conn = db.getConnection();
- try (Statement stmt = conn.createStatement()) {
- try (ResultSet rs = stmt.executeQuery("select count(*) from SUBSCRIPTIONS")) {
- if (rs.next()) {
- count = rs.getInt(1);
- }
- }
- }
- db.release(conn);
- } catch (SQLException e) {
- intlogger.warn("PROV0008 countActiveSubscriptions: " + e.getMessage(), e);
- }
- return count;
- }
-
- public Group() {
- this("", "", "");
- }
-
- public Group(String name, String desc, String members) {
- this.groupid = -1;
- this.authid = "";
- this.name = name;
- this.description = desc;
- this.members = members;
- this.classification = "";
- this.last_mod = new Date();
- }
-
-
- public Group(ResultSet rs) throws SQLException {
- this.groupid = rs.getInt("GROUPID");
- this.authid = rs.getString("AUTHID");
- this.name = rs.getString("NAME");
- this.description = rs.getString("DESCRIPTION");
- this.classification = rs.getString("CLASSIFICATION");
- this.members = rs.getString("MEMBERS");
- this.last_mod = rs.getDate("LAST_MOD");
- }
-
-
- public Group(JSONObject jo) throws InvalidObjectException {
- this("", "", "");
- try {
- // The JSONObject is assumed to contain a vnd.dmaap-dr.group representation
- this.groupid = jo.optInt("groupid", -1);
- String gname = jo.getString("name");
- String gdescription = jo.getString("description");
-
- this.authid = jo.getString("authid");
- this.name = gname;
- this.description = gdescription;
- this.classification = jo.getString("classification");
- this.members = jo.getString("members");
-
- if (gname.length() > 50) {
- throw new InvalidObjectException("Group name is too long");
- }
- if (gdescription.length() > 256) {
- throw new InvalidObjectException("Group Description is too long");
- }
- } catch (InvalidObjectException e) {
- throw e;
- } catch (Exception e) {
- intlogger.warn("Invalid JSON: " + e.getMessage(), e);
- throw new InvalidObjectException("Invalid JSON: " + e.getMessage());
- }
- }
-
public int getGroupid() {
return groupid;
}
@@ -251,14 +207,6 @@ public class Group extends Syncable { Group.intlogger = intlogger;
}
- public static int getNext_groupid() {
- return next_groupid;
- }
-
- public static void setNext_groupid(int next_groupid) {
- Group.next_groupid = next_groupid;
- }
-
public String getAuthid() {
return authid;
}
@@ -295,23 +243,10 @@ public class Group extends Syncable { return members;
}
- public void setMembers(String members) {
- this.members = members;
- }
-
- public Date getLast_mod() {
- return last_mod;
- }
-
- public void setLast_mod(Date last_mod) {
- this.last_mod = last_mod;
- }
-
-
@Override
public JSONObject asJSONObject() {
JSONObject jo = new JSONObject();
- jo.put("groupid", groupid);
+ jo.put(GROUP_ID_CONST, groupid);
jo.put("authid", authid);
jo.put("name", name);
jo.put("description", description);
@@ -328,11 +263,11 @@ public class Group extends Syncable { try {
if (groupid == -1) {
// No feed ID assigned yet, so assign the next available one
- setGroupid(next_groupid++);
+ setGroupid(nextGroupid++);
}
// In case we insert a gropup from synchronization
- if (groupid > next_groupid) {
- next_groupid = groupid + 1;
+ if (groupid > nextGroupid) {
+ nextGroupid = groupid + 1;
}
// Create the GROUPS row
diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/IngressRoute.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/IngressRoute.java index 4332d7d7..1df093df 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/IngressRoute.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/IngressRoute.java @@ -24,6 +24,8 @@ package org.onap.dmaap.datarouter.provisioning.beans;
+import com.att.eelf.configuration.EELFLogger;
+import com.att.eelf.configuration.EELFManager;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.sql.Connection;
@@ -31,16 +33,11 @@ import java.sql.PreparedStatement; import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
-import java.util.ArrayList;
import java.util.Collection;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
-
import javax.servlet.http.HttpServletRequest;
-
-import com.att.eelf.configuration.EELFLogger;
-import com.att.eelf.configuration.EELFManager;
import org.apache.commons.codec.binary.Base64;
import org.json.JSONArray;
import org.json.JSONObject;
@@ -54,6 +51,7 @@ import org.onap.dmaap.datarouter.provisioning.utils.DB; */
public class IngressRoute extends NodeClass implements Comparable<IngressRoute> {
+ private static final String NODESET = "NODESET";
private static EELFLogger intlogger = EELFManager.getInstance().getLogger("InternalLog");
private static final String SQLEXCEPTION = "SQLException: ";
private final int seq;
@@ -63,6 +61,52 @@ public class IngressRoute extends NodeClass implements Comparable<IngressRoute> private int nodelist;
private SortedSet<String> nodes;
+ public IngressRoute(int seq, int feedid, String user, String subnet, Collection<String> nodes) {
+ this(seq, feedid, user, subnet);
+ this.nodelist = -1;
+ this.nodes = new TreeSet<>(nodes);
+ }
+
+ private IngressRoute(int seq, int feedid, String user, String subnet, int nodeset) {
+ this(seq, feedid, user, subnet);
+ this.nodelist = nodeset;
+ this.nodes = new TreeSet<>(readNodes());
+ }
+
+ private IngressRoute(int seq, int feedid, String user, String subnet) {
+ this.seq = seq;
+ this.feedid = feedid;
+ this.userid = (user == null) ? "-" : user;
+ this.subnet = (subnet == null) ? "-" : subnet;
+ this.nodelist = -1;
+ this.nodes = null;
+ if (Feed.getFeedById(feedid) == null) {
+ throw new IllegalArgumentException("No such feed: " + feedid);
+ }
+ if (!"-".equals(this.subnet)) {
+ SubnetMatcher sm = new SubnetMatcher(subnet);
+ if (!sm.isValid()) {
+ throw new IllegalArgumentException("Invalid subnet: " + subnet);
+ }
+ }
+ }
+
+ public IngressRoute(JSONObject jo) {
+ this.seq = jo.optInt("seq");
+ this.feedid = jo.optInt("feedid");
+ String t = jo.optString("user");
+ this.userid = "".equals(t) ? "-" : t;
+ t = jo.optString("subnet");
+ this.subnet = "".equals(t) ? "-" : t;
+ this.nodelist = -1;
+ this.nodes = new TreeSet<>();
+ JSONArray ja = jo.getJSONArray("node");
+ for (int i = 0; i < ja.length(); i++) {
+ this.nodes.add(ja.getString(i));
+ }
+ }
+
+
/**
* Get all IngressRoutes in the database, sorted in order according to their sequence field.
*
@@ -84,21 +128,14 @@ public class IngressRoute extends NodeClass implements Comparable<IngressRoute> }
private static SortedSet<IngressRoute> getAllIngressRoutesForSQL(String sql) {
- SortedSet<IngressRoute> set = new TreeSet<IngressRoute>();
+ SortedSet<IngressRoute> set = new TreeSet<>();
try {
DB db = new DB();
@SuppressWarnings("resource")
Connection conn = db.getConnection();
try (Statement stmt = conn.createStatement()) {
try (ResultSet rs = stmt.executeQuery(sql)) {
- while (rs.next()) {
- int seq = rs.getInt("SEQUENCE");
- int feedid = rs.getInt("FEEDID");
- String user = rs.getString("USERID");
- String subnet = rs.getString("SUBNET");
- int nodeset = rs.getInt("NODESET");
- set.add(new IngressRoute(seq, feedid, user, subnet, nodeset));
- }
+ addIngressRouteToSet(set, rs);
}
}
db.release(conn);
@@ -108,12 +145,23 @@ public class IngressRoute extends NodeClass implements Comparable<IngressRoute> return set;
}
+ private static void addIngressRouteToSet(SortedSet<IngressRoute> set, ResultSet rs) throws SQLException {
+ while (rs.next()) {
+ int seq = rs.getInt("SEQUENCE");
+ int feedid = rs.getInt("FEEDID");
+ String user = rs.getString("USERID");
+ String subnet = rs.getString("SUBNET");
+ int nodeset = rs.getInt(NODESET);
+ set.add(new IngressRoute(seq, feedid, user, subnet, nodeset));
+ }
+ }
+
/**
* Get the maximum node set ID in use in the DB.
*
* @return the integer value of the maximum
*/
- public static int getMaxNodeSetID() {
+ private static int getMaxNodeSetID() {
return getMax("select max(SETID) as MAX from NODESETS");
}
@@ -128,15 +176,12 @@ public class IngressRoute extends NodeClass implements Comparable<IngressRoute> private static int getMax(String sql) {
int rv = 0;
- try {
- DB db = new DB();
- @SuppressWarnings("resource")
- Connection conn = db.getConnection();
- try (Statement stmt = conn.createStatement()) {
- try (ResultSet rs = stmt.executeQuery(sql)) {
- if (rs.next()) {
- rv = rs.getInt("MAX");
- }
+ DB db = new DB();
+ try (Connection conn = db.getConnection();
+ Statement stmt = conn.createStatement()) {
+ try (ResultSet rs = stmt.executeQuery(sql)) {
+ if (rs.next()) {
+ rv = rs.getInt("MAX");
}
}
db.release(conn);
@@ -147,7 +192,7 @@ public class IngressRoute extends NodeClass implements Comparable<IngressRoute> }
/**
- * Get an Ingress Route for a particular feed ID, user, and subnet
+ * Get an Ingress Route for a particular feed ID, user, and subnet.
*
* @param feedid the Feed ID to look for
* @param user the user name to look for
@@ -156,120 +201,28 @@ public class IngressRoute extends NodeClass implements Comparable<IngressRoute> */
public static IngressRoute getIngressRoute(int feedid, String user, String subnet) {
IngressRoute v = null;
- PreparedStatement ps = null;
- try {
- DB db = new DB();
- @SuppressWarnings("resource")
- Connection conn = db.getConnection();
- String sql = "select SEQUENCE, NODESET from INGRESS_ROUTES where FEEDID = ? AND USERID = ? and SUBNET = ?";
- ps = conn.prepareStatement(sql);
+ DB db = new DB();
+ String sql = "select SEQUENCE, NODESET from INGRESS_ROUTES where FEEDID = ? AND USERID = ? and SUBNET = ?";
+ try (Connection conn = db.getConnection();
+ PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setInt(1, feedid);
ps.setString(2, user);
ps.setString(3, subnet);
try (ResultSet rs = ps.executeQuery()) {
if (rs.next()) {
int seq = rs.getInt("SEQUENCE");
- int nodeset = rs.getInt("NODESET");
+ int nodeset = rs.getInt(NODESET);
v = new IngressRoute(seq, feedid, user, subnet, nodeset);
}
}
- ps.close();
db.release(conn);
} catch (SQLException e) {
intlogger.error("PROV0003 getIngressRoute: " + e.getMessage(), e);
- } finally {
- try {
- if (ps != null) {
- ps.close();
- }
- } catch (SQLException e) {
- intlogger.error(SQLEXCEPTION + e.getMessage(), e);
- }
}
return v;
}
/**
- * Get a collection of all Ingress Routes with a particular sequence number.
- *
- * @param seq the sequence number to look for
- * @return the collection (may be empty).
- */
- public static Collection<IngressRoute> getIngressRoute(int seq) {
- Collection<IngressRoute> rv = new ArrayList<IngressRoute>();
- try {
- DB db = new DB();
- @SuppressWarnings("resource")
- Connection conn = db.getConnection();
- String sql = "select FEEDID, USERID, SUBNET, NODESET from INGRESS_ROUTES where SEQUENCE = ?";
- try (PreparedStatement ps = conn.prepareStatement(sql)) {
- ps.setInt(1, seq);
- try (ResultSet rs = ps.executeQuery()) {
- while (rs.next()) {
- int feedid = rs.getInt("FEEDID");
- String user = rs.getString("USERID");
- String subnet = rs.getString("SUBNET");
- int nodeset = rs.getInt("NODESET");
- rv.add(new IngressRoute(seq, feedid, user, subnet, nodeset));
- }
- }
- }
- db.release(conn);
- } catch (SQLException e) {
- intlogger.error("PROV0004 getIngressRoute: " + e.getMessage(), e);
- }
- return rv;
- }
-
- public IngressRoute(int seq, int feedid, String user, String subnet, Collection<String> nodes)
- throws IllegalArgumentException {
- this(seq, feedid, user, subnet);
- this.nodelist = -1;
- this.nodes = new TreeSet<String>(nodes);
- }
-
- public IngressRoute(int seq, int feedid, String user, String subnet, int nodeset)
- throws IllegalArgumentException {
- this(seq, feedid, user, subnet);
- this.nodelist = nodeset;
- this.nodes = new TreeSet<String>(readNodes());
- }
-
- private IngressRoute(int seq, int feedid, String user, String subnet)
- throws IllegalArgumentException {
- this.seq = seq;
- this.feedid = feedid;
- this.userid = (user == null) ? "-" : user;
- this.subnet = (subnet == null) ? "-" : subnet;
- this.nodelist = -1;
- this.nodes = null;
- if (Feed.getFeedById(feedid) == null) {
- throw new IllegalArgumentException("No such feed: " + feedid);
- }
- if (!this.subnet.equals("-")) {
- SubnetMatcher sm = new SubnetMatcher(subnet);
- if (!sm.isValid()) {
- throw new IllegalArgumentException("Invalid subnet: " + subnet);
- }
- }
- }
-
- public IngressRoute(JSONObject jo) {
- this.seq = jo.optInt("seq");
- this.feedid = jo.optInt("feedid");
- String t = jo.optString("user");
- this.userid = t.equals("") ? "-" : t;
- t = jo.optString("subnet");
- this.subnet = t.equals("") ? "-" : t;
- this.nodelist = -1;
- this.nodes = new TreeSet<String>();
- JSONArray ja = jo.getJSONArray("node");
- for (int i = 0; i < ja.length(); i++) {
- this.nodes.add(ja.getString(i));
- }
- }
-
- /**
* Does this particular IngressRoute match a request, represented by feedid and req? To match, <i>feedid</i> must
* match the feed ID in the route, the user in the route (if specified) must match the user in the request, and the
* subnet in the route (if specified) must match the subnet from the request.
@@ -283,10 +236,9 @@ public class IngressRoute extends NodeClass implements Comparable<IngressRoute> if (this.feedid != feedid) {
return false;
}
-
// Get user from request and compare
// Note: we don't check the password; the node will do that
- if (userid.length() > 0 && !userid.equals("-")) {
+ if (userid.length() > 0 && !"-".equals(userid)) {
String credentials = req.getHeader("Authorization");
if (credentials == null || !credentials.startsWith("Basic ")) {
return false;
@@ -300,9 +252,8 @@ public class IngressRoute extends NodeClass implements Comparable<IngressRoute> return false;
}
}
-
// If this route has a subnet, match it against the requester's IP addr
- if (subnet.length() > 0 && !subnet.equals("-")) {
+ if (subnet.length() > 0 && !"-".equals(subnet)) {
try {
InetAddress inet = InetAddress.getByName(req.getRemoteAddr());
SubnetMatcher sm = new SubnetMatcher(subnet);
@@ -327,7 +278,7 @@ public class IngressRoute extends NodeClass implements Comparable<IngressRoute> private boolean valid;
/**
- * Construct a subnet matcher given a CIDR
+ * Construct a subnet matcher given a CIDR.
*
* @param subnet The CIDR to match
*/
@@ -358,18 +309,18 @@ public class IngressRoute extends NodeClass implements Comparable<IngressRoute> }
}
- public boolean isValid() {
+ boolean isValid() {
return valid;
}
/**
- * Is the IP address in the CIDR?
+ * Is the IP address in the CIDR?.
*
* @param addr the IP address as bytes in network byte order
* @return true if the IP address matches.
*/
- public boolean matches(byte[] addr) {
- if (!valid || addr.length != sn.length) {
+ boolean matches(byte[] addr) {
+ if (!valid || (addr.length != sn.length)) {
return false;
}
for (int i = 0; i < len; i++) {
@@ -395,19 +346,12 @@ public class IngressRoute extends NodeClass implements Comparable<IngressRoute> private Collection<String> readNodes() {
Collection<String> set = new TreeSet<>();
- try {
- DB db = new DB();
- @SuppressWarnings("resource")
- Connection conn = db.getConnection();
- String sql = "select NODEID from NODESETS where SETID = ?";
+ DB db = new DB();
+ String sql = "select NODEID from NODESETS where SETID = ?";
+ try (Connection conn = db.getConnection()) {
try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setInt(1, nodelist);
- try (ResultSet rs = ps.executeQuery()) {
- while (rs.next()) {
- int id = rs.getInt("NODEID");
- set.add(lookupNodeID(id));
- }
- }
+ addNodeToSet(set, ps);
}
db.release(conn);
} catch (SQLException e) {
@@ -416,6 +360,15 @@ public class IngressRoute extends NodeClass implements Comparable<IngressRoute> return set;
}
+ private void addNodeToSet(Collection<String> set, PreparedStatement ps) throws SQLException {
+ try (ResultSet rs = ps.executeQuery()) {
+ while (rs.next()) {
+ int id = rs.getInt("NODEID");
+ set.add(lookupNodeID(id));
+ }
+ }
+ }
+
/**
* Delete the IRT route having this IngressRoutes feed ID, user ID, and subnet from the database.
*
@@ -424,72 +377,49 @@ public class IngressRoute extends NodeClass implements Comparable<IngressRoute> @Override
public boolean doDelete(Connection c) {
boolean rv = true;
- PreparedStatement ps = null;
- try {
- ps = c.prepareStatement("delete from INGRESS_ROUTES where FEEDID = ? and USERID = ? and SUBNET = ?");
+ try (PreparedStatement ps = c.prepareStatement(
+ "delete from INGRESS_ROUTES where FEEDID = ? and USERID = ? and SUBNET = ?");
+ PreparedStatement ps2 = c.prepareStatement("delete from NODESETS where SETID = ?")) {
+ // Delete the Ingress Route
ps.setInt(1, feedid);
ps.setString(2, userid);
ps.setString(3, subnet);
ps.execute();
ps.close();
-
- ps = c.prepareStatement("delete from NODESETS where SETID = ?");
- ps.setInt(1, nodelist);
- ps.execute();
+ // Delete the NodeSet
+ ps2.setInt(1, nodelist);
+ ps2.execute();
} catch (SQLException e) {
rv = false;
intlogger.warn("PROV0007 doDelete: " + e.getMessage(), e);
- } finally {
- try {
- if (ps != null) {
- ps.close();
- }
- } catch (SQLException e) {
- intlogger.error(SQLEXCEPTION + e.getMessage(), e);
- }
}
return rv;
}
- @SuppressWarnings("resource")
@Override
public boolean doInsert(Connection c) {
boolean rv = false;
- PreparedStatement ps = null;
- try {
+ try (PreparedStatement ps = c.prepareStatement("insert into NODESETS (SETID, NODEID) values (?,?)");
+ PreparedStatement ps2 = c.prepareStatement("insert into INGRESS_ROUTES (SEQUENCE, FEEDID, USERID,"
+ + " SUBNET, NODESET) values (?, ?, ?, ?, ?)")) {
// Create the NODESETS rows & set nodelist
- int set = getMaxNodeSetID() + 1;
- this.nodelist = set;
+ this.nodelist = getMaxNodeSetID() + 1;
for (String node : nodes) {
int id = lookupNodeName(node);
- ps = c.prepareStatement("insert into NODESETS (SETID, NODEID) values (?,?)");
ps.setInt(1, this.nodelist);
ps.setInt(2, id);
ps.execute();
- ps.close();
}
-
// Create the INGRESS_ROUTES row
- ps = c.prepareStatement(
- "insert into INGRESS_ROUTES (SEQUENCE, FEEDID, USERID, SUBNET, NODESET) values (?, ?, ?, ?, ?)");
- ps.setInt(1, this.seq);
- ps.setInt(2, this.feedid);
- ps.setString(3, this.userid);
- ps.setString(4, this.subnet);
- ps.setInt(5, this.nodelist);
- ps.execute();
- ps.close();
+ ps2.setInt(1, this.seq);
+ ps2.setInt(2, this.feedid);
+ ps2.setString(3, this.userid);
+ ps2.setString(4, this.subnet);
+ ps2.setInt(5, this.nodelist);
+ ps2.execute();
rv = true;
} catch (SQLException e) {
intlogger.warn("PROV0005 doInsert: " + e.getMessage(), e);
- } finally {
- try {
- if (ps != null) {
- ps.close();
- }
- } catch (SQLException e) {
- intlogger.error(SQLEXCEPTION + e.getMessage(), e);
- }
}
return rv;
}
@@ -504,10 +434,10 @@ public class IngressRoute extends NodeClass implements Comparable<IngressRoute> JSONObject jo = new JSONObject();
jo.put("feedid", feedid);
// Note: for user and subnet, null, "", and "-" are equivalent
- if (userid != null && !userid.equals("-") && !userid.equals("")) {
+ if (userid != null && !"-".equals(userid) && !"".equals(userid)) {
jo.put("user", userid);
}
- if (subnet != null && !subnet.equals("-") && !subnet.equals("")) {
+ if (subnet != null && !"-".equals(subnet) && !"".equals(subnet)) {
jo.put("subnet", subnet);
}
jo.put("seq", seq);
diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/Insertable.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/Insertable.java index 6a677ce3..ed0815e8 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/Insertable.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/Insertable.java @@ -32,6 +32,7 @@ import java.sql.Connection; * @author Robert Eby
* @version $Id: Insertable.java,v 1.2 2013/05/29 14:44:36 eby Exp $
*/
+@FunctionalInterface
public interface Insertable {
/**
* Insert this object into the DB.
@@ -39,5 +40,5 @@ public interface Insertable { * @param c the JDBC Connection to use
* @return true if the INSERT succeeded, false otherwise
*/
- public boolean doInsert(Connection c);
+ boolean doInsert(Connection c);
}
diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/JSONable.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/JSONable.java index 82313273..44cea0f9 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/JSONable.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/JSONable.java @@ -32,11 +32,12 @@ import org.json.JSONObject; * @author Robert Eby
* @version $Id: JSONable.java,v 1.1 2013/04/26 21:00:26 eby Exp $
*/
+@FunctionalInterface
public interface JSONable {
/**
* Get a JSONObject representing this object.
*
* @return the JSONObject
*/
- public JSONObject asJSONObject();
+ JSONObject asJSONObject();
}
diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/LOGJSONable.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/LOGJSONable.java index 3b7add5c..24f65b16 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/LOGJSONable.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/LOGJSONable.java @@ -32,11 +32,12 @@ import org.onap.dmaap.datarouter.provisioning.utils.LOGJSONObject; * @author Robert Eby
* @version $Id: JSONable.java,v 1.1 2013/04/26 21:00:26 eby Exp $
*/
+@FunctionalInterface
public interface LOGJSONable {
/**
* Get a JSONObject representing this object.
*
* @return the JSONObject
*/
- public LOGJSONObject asJSONObject();
+ LOGJSONObject asJSONObject();
}
diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/Loadable.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/Loadable.java index ae64e84d..adeaa46c 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/Loadable.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/Loadable.java @@ -36,6 +36,7 @@ import org.onap.dmaap.datarouter.provisioning.utils.LogfileLoader; * @author Robert Eby
* @version $Id: Loadable.java,v 1.2 2013/08/06 13:28:33 eby Exp $
*/
+@FunctionalInterface
public interface Loadable {
/**
* Load the 18 fields in the PreparedStatement <i>ps</i>. The fields are:
@@ -62,5 +63,5 @@ public interface Loadable { *
* @param ps the PreparedStatement to load
*/
- public void load(PreparedStatement ps) throws SQLException;
+ void load(PreparedStatement ps) throws SQLException;
}
diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/NetworkRoute.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/NetworkRoute.java index eeca0903..fa3d4e0e 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/NetworkRoute.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/NetworkRoute.java @@ -24,6 +24,8 @@ package org.onap.dmaap.datarouter.provisioning.beans;
+import com.att.eelf.configuration.EELFLogger;
+import com.att.eelf.configuration.EELFManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
@@ -32,9 +34,6 @@ import java.sql.Statement; import java.util.Objects;
import java.util.SortedSet;
import java.util.TreeSet;
-
-import com.att.eelf.configuration.EELFLogger;
-import com.att.eelf.configuration.EELFManager;
import org.json.JSONObject;
import org.onap.dmaap.datarouter.provisioning.utils.DB;
@@ -52,6 +51,30 @@ public class NetworkRoute extends NodeClass implements Comparable<NetworkRoute> private final int tonode;
private final int vianode;
+ public NetworkRoute(String fromnode, String tonode) {
+ this.fromnode = lookupNodeName(fromnode);
+ this.tonode = lookupNodeName(tonode);
+ this.vianode = -1;
+ }
+
+ public NetworkRoute(String fromnode, String tonode, String vianode) {
+ this.fromnode = lookupNodeName(fromnode);
+ this.tonode = lookupNodeName(tonode);
+ this.vianode = lookupNodeName(vianode);
+ }
+
+ public NetworkRoute(JSONObject jo) {
+ this.fromnode = lookupNodeName(jo.getString("from"));
+ this.tonode = lookupNodeName(jo.getString("to"));
+ this.vianode = lookupNodeName(jo.getString("via"));
+ }
+
+ private NetworkRoute(int fromnode, int tonode, int vianode) {
+ this.fromnode = fromnode;
+ this.tonode = tonode;
+ this.vianode = vianode;
+ }
+
/**
* Get a set of all Network Routes in the DB. The set is sorted according to the natural sorting order of the
* routes (based on the from and to node names in each route).
@@ -66,43 +89,24 @@ public class NetworkRoute extends NodeClass implements Comparable<NetworkRoute> Connection conn = db.getConnection();
try (Statement stmt = conn.createStatement()) {
try (ResultSet rs = stmt.executeQuery("select FROMNODE, TONODE, VIANODE from NETWORK_ROUTES")) {
- while (rs.next()) {
- int fromnode = rs.getInt("FROMNODE");
- int tonode = rs.getInt("TONODE");
- int vianode = rs.getInt("VIANODE");
- set.add(new NetworkRoute(fromnode, tonode, vianode));
- }
+ addNetworkRouteToSet(set, rs);
}
+ } finally {
+ db.release(conn);
}
- db.release(conn);
} catch (SQLException e) {
intlogger.error(SQLEXCEPTION + e.getMessage(), e);
}
return set;
}
- public NetworkRoute(String fromnode, String tonode) {
- this.fromnode = lookupNodeName(fromnode);
- this.tonode = lookupNodeName(tonode);
- this.vianode = -1;
- }
-
- public NetworkRoute(String fromnode, String tonode, String vianode) {
- this.fromnode = lookupNodeName(fromnode);
- this.tonode = lookupNodeName(tonode);
- this.vianode = lookupNodeName(vianode);
- }
-
- public NetworkRoute(JSONObject jo) {
- this.fromnode = lookupNodeName(jo.getString("from"));
- this.tonode = lookupNodeName(jo.getString("to"));
- this.vianode = lookupNodeName(jo.getString("via"));
- }
-
- public NetworkRoute(int fromnode, int tonode, int vianode) {
- this.fromnode = fromnode;
- this.tonode = tonode;
- this.vianode = vianode;
+ private static void addNetworkRouteToSet(SortedSet<NetworkRoute> set, ResultSet rs) throws SQLException {
+ while (rs.next()) {
+ int fromnode = rs.getInt("FROMNODE");
+ int tonode = rs.getInt("TONODE");
+ int vianode = rs.getInt("VIANODE");
+ set.add(new NetworkRoute(fromnode, tonode, vianode));
+ }
}
public int getFromnode() {
@@ -120,24 +124,14 @@ public class NetworkRoute extends NodeClass implements Comparable<NetworkRoute> @Override
public boolean doDelete(Connection c) {
boolean rv = true;
- PreparedStatement ps = null;
- try {
- String sql = "delete from NETWORK_ROUTES where FROMNODE = ? AND TONODE = ?";
- ps = c.prepareStatement(sql);
+ String sql = "delete from NETWORK_ROUTES where FROMNODE = ? AND TONODE = ?";
+ try (PreparedStatement ps = c.prepareStatement(sql)) {
ps.setInt(1, fromnode);
ps.setInt(2, tonode);
ps.execute();
} catch (SQLException e) {
rv = false;
intlogger.warn("PROV0007 doDelete: " + e.getMessage(), e);
- } finally {
- try {
- if (ps != null) {
- ps.close();
- }
- } catch (SQLException e) {
- intlogger.error(SQLEXCEPTION + e.getMessage(), e);
- }
}
return rv;
}
@@ -145,28 +139,17 @@ public class NetworkRoute extends NodeClass implements Comparable<NetworkRoute> @Override
public boolean doInsert(Connection c) {
boolean rv = false;
+ String sql = "insert into NETWORK_ROUTES (FROMNODE, TONODE, VIANODE) values (?, ?, ?)";
if (this.vianode >= 0) {
- PreparedStatement ps = null;
- try {
+ try (PreparedStatement ps = c.prepareStatement(sql)) {
// Create the NETWORK_ROUTES row
- String sql = "insert into NETWORK_ROUTES (FROMNODE, TONODE, VIANODE) values (?, ?, ?)";
- ps = c.prepareStatement(sql);
ps.setInt(1, this.fromnode);
ps.setInt(2, this.tonode);
ps.setInt(3, this.vianode);
ps.execute();
- ps.close();
rv = true;
} catch (SQLException e) {
intlogger.warn("PROV0005 doInsert: " + e.getMessage(), e);
- } finally {
- try {
- if (ps != null) {
- ps.close();
- }
- } catch (SQLException e) {
- intlogger.error(SQLEXCEPTION + e.getMessage(), e);
- }
}
}
return rv;
@@ -175,10 +158,8 @@ public class NetworkRoute extends NodeClass implements Comparable<NetworkRoute> @Override
public boolean doUpdate(Connection c) {
boolean rv = true;
- PreparedStatement ps = null;
- try {
- String sql = "update NETWORK_ROUTES set VIANODE = ? where FROMNODE = ? and TONODE = ?";
- ps = c.prepareStatement(sql);
+ String sql = "update NETWORK_ROUTES set VIANODE = ? where FROMNODE = ? and TONODE = ?";
+ try (PreparedStatement ps = c.prepareStatement(sql)) {
ps.setInt(1, vianode);
ps.setInt(2, fromnode);
ps.setInt(3, tonode);
@@ -186,14 +167,6 @@ public class NetworkRoute extends NodeClass implements Comparable<NetworkRoute> } catch (SQLException e) {
rv = false;
intlogger.warn("PROV0006 doUpdate: " + e.getMessage(), e);
- } finally {
- try {
- if (ps != null) {
- ps.close();
- }
- } catch (SQLException e) {
- intlogger.error(SQLEXCEPTION + e.getMessage(), e);
- }
}
return rv;
}
diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/NodeClass.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/NodeClass.java index f3ef5d6a..d11c20fb 100755 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/NodeClass.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/NodeClass.java @@ -23,6 +23,8 @@ package org.onap.dmaap.datarouter.provisioning.beans;
+import com.att.eelf.configuration.EELFLogger;
+import com.att.eelf.configuration.EELFManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
@@ -32,9 +34,6 @@ import java.util.HashMap; import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
-
-import com.att.eelf.configuration.EELFLogger;
-import com.att.eelf.configuration.EELFManager;
import org.onap.dmaap.datarouter.provisioning.utils.DB;
/**
@@ -45,9 +44,11 @@ import org.onap.dmaap.datarouter.provisioning.utils.DB; */
public abstract class NodeClass extends Syncable {
+ private static final String PROV_0005_DO_INSERT = "PROV0005 doInsert: ";
private static Map<String, Integer> map;
private static EELFLogger intLogger = EELFManager.getInstance().getLogger("InternalLog");
- public NodeClass() {
+
+ NodeClass() {
// init on first use
if (map == null) {
reload();
@@ -77,67 +78,50 @@ public abstract class NodeClass extends Syncable { if (!map.containsKey(node)) {
intLogger.info("..adding " + node + " to NODES with index " + nextid);
map.put(node, nextid);
- PreparedStatement ps = null;
- try {
- DB db = new DB();
- @SuppressWarnings("resource")
- Connection conn = db.getConnection();
- ps = conn.prepareStatement("insert into NODES (NODEID, NAME, ACTIVE) values (?, ?, 1)");
- ps.setInt(1, nextid);
- ps.setString(2, node);
- ps.execute();
- ps.close();
- db.release(conn);
- } catch (SQLException e) {
- intLogger.error("PROV0005 doInsert: " + e.getMessage(),e);
- } finally {
- try {
- if(ps!=null){
- ps.close();
- }
- } catch (SQLException e) {
- intLogger.error("Error in closing PreparedStatement: " + e.getMessage(),e);
- }
- }
+ insertNodesToTable(nextid, node);
nextid++;
}
}
}
- public static void reload() {
- Map<String, Integer> m = new HashMap<String, Integer>();
- PreparedStatement ps = null;
+ private static void insertNodesToTable(int nextid, String node) {
+ DB db = new DB();
+ try (Connection conn = db.getConnection()) {
+ try (PreparedStatement ps = conn
+ .prepareStatement("insert into NODES (NODEID, NAME, ACTIVE) values (?, ?, 1)")) {
+ ps.setInt(1, nextid);
+ ps.setString(2, node);
+ ps.execute();
+ } finally {
+ db.release(conn);
+ }
+ } catch (SQLException e) {
+ intLogger.error(PROV_0005_DO_INSERT + e.getMessage(), e);
+ }
+ }
- try {
- DB db = new DB();
- @SuppressWarnings("resource")
- Connection conn = db.getConnection();
- String sql = "select NODEID, NAME from NODES";
- ps = conn.prepareStatement(sql);
- try(ResultSet rs = ps.executeQuery()) {
+ private static void reload() {
+ Map<String, Integer> m = new HashMap<>();
+ String sql = "select NODEID, NAME from NODES";
+ DB db = new DB();
+ try (Connection conn = db.getConnection();
+ PreparedStatement ps = conn.prepareStatement(sql)) {
+ try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
int id = rs.getInt("NODEID");
String name = rs.getString("NAME");
m.put(name, id);
}
+ } finally {
+ db.release(conn);
}
- ps.close();
- db.release(conn);
} catch (SQLException e) {
- intLogger.error("PROV0005 doInsert: " + e.getMessage(),e);
- } finally {
- try {
- if(ps!=null){
- ps.close();
- }
- } catch (SQLException e) {
- intLogger.error("PROV0005 doInsert: " + e.getMessage(),e);
- }
+ intLogger.error(PROV_0005_DO_INSERT + e.getMessage(),e);
}
map = m;
}
- public static Integer lookupNodeName(final String name) {
+ static Integer lookupNodeName(final String name) {
Integer n = map.get(name);
if (n == null) {
throw new IllegalArgumentException("Invalid node name: " + name);
@@ -146,16 +130,11 @@ public abstract class NodeClass extends Syncable { }
public static Collection<String> lookupNodeNames(String patt) {
- Collection<String> coll = new TreeSet<String>();
+ Collection<String> coll = new TreeSet<>();
final Set<String> keyset = map.keySet();
for (String s : patt.toLowerCase().split(",")) {
if (s.endsWith("*")) {
- s = s.substring(0, s.length() - 1);
- for (String s2 : keyset) {
- if (s2.startsWith(s)) {
- coll.add(s2);
- }
- }
+ addNodeToCollection(coll, keyset, s);
} else if (keyset.contains(s)) {
coll.add(s);
} else if (keyset.contains(normalizeNodename(s))) {
@@ -167,6 +146,15 @@ public abstract class NodeClass extends Syncable { return coll;
}
+ private static void addNodeToCollection(Collection<String> coll, Set<String> keyset, String s) {
+ s = s.substring(0, s.length() - 1);
+ for (String s2 : keyset) {
+ if (s2.startsWith(s)) {
+ coll.add(s2);
+ }
+ }
+ }
+
public static String normalizeNodename(String s) {
if (s != null && s.indexOf('.') <= 0) {
Parameters p = Parameters.getParameter(Parameters.PROV_DOMAIN);
@@ -175,17 +163,16 @@ public abstract class NodeClass extends Syncable { s += "." + domain;
}
return s.toLowerCase();
- }
- else{
+ } else {
return s;
}
}
- protected String lookupNodeID(int n) {
- for (String s : map.keySet()) {
- if (map.get(s) == n) {
- return s;
+ String lookupNodeID(int n) {
+ for (Map.Entry<String, Integer> entry : map.entrySet()) {
+ if (entry.getValue() == n) {
+ return entry.getKey();
}
}
return null;
diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/Parameters.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/Parameters.java index fff10ac7..78ed96dd 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/Parameters.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/Parameters.java @@ -23,15 +23,18 @@ package org.onap.dmaap.datarouter.provisioning.beans;
+import com.att.eelf.configuration.EELFLogger;
+import com.att.eelf.configuration.EELFManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
-import java.util.*;
-
-import com.att.eelf.configuration.EELFLogger;
-import com.att.eelf.configuration.EELFManager;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
import org.json.JSONObject;
import org.onap.dmaap.datarouter.provisioning.utils.DB;
@@ -57,6 +60,7 @@ public class Parameters extends Syncable { public static final String PROV_POKETIMER2 = "PROV_POKETIMER2";
public static final String PROV_SPECIAL_SUBNET = "PROV_SPECIAL_SUBNET";
public static final String PROV_LOG_RETENTION = "PROV_LOG_RETENTION";
+ public static final String DEFAULT_LOG_RETENTION = "DEFAULT_LOG_RETENTION";
public static final String NODES = "NODES";
public static final String ACTIVE_POD = "ACTIVE_POD";
public static final String STANDBY_POD = "STANDBY_POD";
@@ -66,7 +70,7 @@ public class Parameters extends Syncable { public static final String DELIVERY_RETRY_RATIO = "DELIVERY_RETRY_RATIO";
public static final String DELIVERY_MAX_AGE = "DELIVERY_MAX_AGE";
public static final String THROTTLE_FILTER = "THROTTLE_FILTER";
- public static final String STATIC_ROUTING_NODES = "STATIC_ROUTING_NODES"; //Adding new param for static Routing - Rally:US664862-1610
+ public static final String STATIC_ROUTING_NODES = "STATIC_ROUTING_NODES";
private static EELFLogger intlogger = EELFManager.getInstance().getLogger("InternalLog");
private static final String SQLEXCEPTION = "SQLException: ";
@@ -74,13 +78,23 @@ public class Parameters extends Syncable { private String keyname;
private String value;
+ public Parameters(String k, String v) {
+ this.keyname = k;
+ this.value = v;
+ }
+
+ public Parameters(ResultSet rs) throws SQLException {
+ this.keyname = rs.getString("KEYNAME");
+ this.value = rs.getString("VALUE");
+ }
+
/**
* Get all parameters in the DB as a Map.
*
* @return the Map of keynames/values from the DB.
*/
public static Map<String, String> getParameters() {
- Map<String, String> props = new HashMap<String, String>();
+ Map<String, String> props = new HashMap<>();
for (Parameters p : getParameterCollection()) {
props.put(p.getKeyname(), p.getValue());
}
@@ -88,23 +102,20 @@ public class Parameters extends Syncable { }
public static Collection<Parameters> getParameterCollection() {
- Collection<Parameters> coll = new ArrayList<Parameters>();
- try {
- DB db = new DB();
- @SuppressWarnings("resource")
- Connection conn = db.getConnection();
- try (Statement stmt = conn.createStatement()) {
- String sql = "select * from PARAMETERS";
- try (ResultSet rs = stmt.executeQuery(sql)) {
- while (rs.next()) {
- Parameters p = new Parameters(rs);
- coll.add(p);
- }
+ Collection<Parameters> coll = new ArrayList<>();
+ DB db = new DB();
+ String sql = "select * from PARAMETERS";
+ try (Connection conn = db.getConnection();
+ Statement stmt = conn.createStatement()) {
+ try (ResultSet rs = stmt.executeQuery(sql)) {
+ while (rs.next()) {
+ Parameters p = new Parameters(rs);
+ coll.add(p);
}
}
db.release(conn);
} catch (SQLException e) {
- intlogger.error(SQLEXCEPTION + e.getMessage());
+ intlogger.error(SQLEXCEPTION + e.getMessage(), e);
}
return coll;
}
@@ -117,48 +128,27 @@ public class Parameters extends Syncable { */
public static Parameters getParameter(String k) {
Parameters v = null;
- try {
- DB db = new DB();
- @SuppressWarnings("resource")
- Connection conn = db.getConnection();
- try (PreparedStatement stmt = conn
- .prepareStatement("select KEYNAME, VALUE from PARAMETERS where KEYNAME = ?")) {
- stmt.setString(1, k);
- try (ResultSet rs = stmt.executeQuery()) {
- if (rs.next()) {
- v = new Parameters(rs);
- }
+ DB db = new DB();
+ String sql = "select KEYNAME, VALUE from PARAMETERS where KEYNAME = ?";
+ try (Connection conn = db.getConnection();
+ PreparedStatement stmt = conn.prepareStatement(sql)) {
+ stmt.setString(1, k);
+ try (ResultSet rs = stmt.executeQuery()) {
+ if (rs.next()) {
+ v = new Parameters(rs);
}
}
db.release(conn);
} catch (SQLException e) {
- intlogger.error(SQLEXCEPTION + e.getMessage());
+ intlogger.error(SQLEXCEPTION + e.getMessage(), e);
}
return v;
}
- public Parameters() {
- this("", "");
- }
-
- public Parameters(String k, String v) {
- this.keyname = k;
- this.value = v;
- }
-
- public Parameters(ResultSet rs) throws SQLException {
- this.keyname = rs.getString("KEYNAME");
- this.value = rs.getString("VALUE");
- }
-
public String getKeyname() {
return keyname;
}
- public void setKeyname(String keyname) {
- this.keyname = keyname;
- }
-
public String getValue() {
return value;
}
@@ -178,25 +168,14 @@ public class Parameters extends Syncable { @Override
public boolean doInsert(Connection c) {
boolean rv = true;
- PreparedStatement ps = null;
- try {
- // Create the SUBSCRIPTIONS row
- String sql = "insert into PARAMETERS values (?, ?)";
- ps = c.prepareStatement(sql);
+ String sql = "insert into PARAMETERS values (?, ?)";
+ try (PreparedStatement ps = c.prepareStatement(sql)) {
ps.setString(1, getKeyname());
ps.setString(2, getValue());
ps.execute();
} catch (SQLException e) {
rv = false;
intlogger.warn("PROV0005 doInsert: " + e.getMessage(), e);
- } finally {
- try {
- if (ps != null) {
- ps.close();
- }
- } catch (SQLException e) {
- intlogger.error(SQLEXCEPTION + e.getMessage());
- }
}
return rv;
}
@@ -204,25 +183,14 @@ public class Parameters extends Syncable { @Override
public boolean doUpdate(Connection c) {
boolean rv = true;
- PreparedStatement ps = null;
- try {
- // Update the PARAMETERS row
- String sql = "update PARAMETERS set VALUE = ? where KEYNAME = ?";
- ps = c.prepareStatement(sql);
+ String sql = "update PARAMETERS set VALUE = ? where KEYNAME = ?";
+ try (PreparedStatement ps = c.prepareStatement(sql)) {
ps.setString(1, getValue());
ps.setString(2, getKeyname());
ps.executeUpdate();
} catch (SQLException e) {
rv = false;
intlogger.warn("PROV0006 doUpdate: " + e.getMessage(),e);
- } finally {
- try {
- if (ps != null) {
- ps.close();
- }
- } catch (SQLException e) {
- intlogger.error(SQLEXCEPTION + e.getMessage(), e);
- }
}
return rv;
}
@@ -230,24 +198,13 @@ public class Parameters extends Syncable { @Override
public boolean doDelete(Connection c) {
boolean rv = true;
- PreparedStatement ps = null;
- try {
- // Create the SUBSCRIPTIONS row
- String sql = "delete from PARAMETERS where KEYNAME = ?";
- ps = c.prepareStatement(sql);
+ String sql = "delete from PARAMETERS where KEYNAME = ?";
+ try (PreparedStatement ps = c.prepareStatement(sql)) {
ps.setString(1, getKeyname());
ps.execute();
} catch (SQLException e) {
rv = false;
intlogger.warn("PROV0007 doDelete: " + e.getMessage(), e);
- } finally {
- try {
- if (ps != null) {
- ps.close();
- }
- } catch (SQLException e) {
- intlogger.error(SQLEXCEPTION + e.getMessage(), e);
- }
}
return rv;
}
@@ -263,13 +220,7 @@ public class Parameters extends Syncable { return false;
}
Parameters of = (Parameters) obj;
- if (!keyname.equals(of.keyname)) {
- return false;
- }
- if (!value.equals(of.value)) {
- return false;
- }
- return true;
+ return (!value.equals(of.value)) && (!keyname.equals(of.keyname));
}
@Override
diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/PubFailRecord.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/PubFailRecord.java index 88d48826..a38d8bd2 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/PubFailRecord.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/PubFailRecord.java @@ -24,7 +24,6 @@ package org.onap.dmaap.datarouter.provisioning.beans;
import java.sql.PreparedStatement;
-import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.text.ParseException;
@@ -49,15 +48,6 @@ public class PubFailRecord extends BaseLogRecord { this.error = pp[11];
}
- public PubFailRecord(ResultSet rs) throws SQLException {
- super(rs);
- // Note: because this record should be "rare" these fields are mapped to unconventional fields in the DB
- this.contentLengthReceived = rs.getLong("CONTENT_LENGTH_2");
- this.sourceIP = rs.getString("REMOTE_ADDR");
- this.user = rs.getString("USER");
- this.error = rs.getString("FEED_FILEID");
- }
-
public long getContentLengthReceived() {
return contentLengthReceived;
}
diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/SubDelivery.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/SubDelivery.java index 98981a30..5a3457b9 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/SubDelivery.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/SubDelivery.java @@ -43,10 +43,6 @@ public class SubDelivery implements JSONable { private String password;
private boolean use100;
- public SubDelivery() {
- this("", "", "", false);
- }
-
public SubDelivery(String url, String user, String password, boolean use100) {
this.url = url;
this.user = user;
@@ -90,10 +86,6 @@ public class SubDelivery implements JSONable { return use100;
}
- public void setUse100(boolean use100) {
- this.use100 = use100;
- }
-
@Override
public JSONObject asJSONObject() {
JSONObject jo = new JSONObject();
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 7700a583..55b2c038 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 @@ -24,6 +24,11 @@ package org.onap.dmaap.datarouter.provisioning.utils;
+import static java.lang.System.exit;
+import static java.lang.System.getProperty;
+
+import com.att.eelf.configuration.EELFLogger;
+import com.att.eelf.configuration.EELFManager;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
@@ -42,9 +47,6 @@ import java.util.Properties; import java.util.Queue;
import java.util.Set;
-import com.att.eelf.configuration.EELFLogger;
-import com.att.eelf.configuration.EELFManager;
-
/**
* Load the DB JDBC driver, and manage a simple pool of connections to the DB.
*
@@ -55,14 +57,14 @@ public class DB { private static EELFLogger intlogger = EELFManager.getInstance().getLogger("InternalLog");
- private static String DB_URL;
- private static String DB_LOGIN;
- private static String DB_PASSWORD;
+ private static String dbUrl;
+ private static String dbLogin;
+ private static String dbPassword;
private static Properties props;
private static final Queue<Connection> queue = new LinkedList<>();
- private static String HTTPS_PORT;
- private static String HTTP_PORT;
+ private static String httpsPort;
+ private static String httpPort;
/**
* Construct a DB object. If this is the very first creation of this object, it will load a copy of the properties
@@ -73,22 +75,22 @@ public class DB { if (props == null) {
props = new Properties();
try {
- props.load(new FileInputStream(System.getProperty(
+ props.load(new FileInputStream(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");
- DB_PASSWORD = (String) props.get("org.onap.dmaap.datarouter.db.password");
- HTTPS_PORT = (String) props.get("org.onap.dmaap.datarouter.provserver.https.port");
- HTTP_PORT = (String) props.get("org.onap.dmaap.datarouter.provserver.http.port");
- Class.forName(DB_DRIVER);
+ String dbDriver = (String) props.get("org.onap.dmaap.datarouter.db.driver");
+ dbUrl = (String) props.get("org.onap.dmaap.datarouter.db.url");
+ dbLogin = (String) props.get("org.onap.dmaap.datarouter.db.login");
+ dbPassword = (String) props.get("org.onap.dmaap.datarouter.db.password");
+ httpsPort = (String) props.get("org.onap.dmaap.datarouter.provserver.https.port");
+ httpPort = (String) props.get("org.onap.dmaap.datarouter.provserver.http.port");
+ Class.forName(dbDriver);
} catch (IOException e) {
intlogger.error("PROV9003 Opening properties: " + e.getMessage(), e);
- System.exit(1);
+ exit(1);
} catch (ClassNotFoundException e) {
intlogger.error("PROV9004 cannot find the DB driver: " + e);
- System.exit(1);
+ exit(1);
}
}
}
@@ -107,7 +109,6 @@ public class DB { *
* @return the Connection
*/
- @SuppressWarnings("resource")
public Connection getConnection() throws SQLException {
Connection connection = null;
while (connection == null) {
@@ -120,7 +121,7 @@ public class DB { do {
// Try up to 3 times to get a connection
try {
- connection = DriverManager.getConnection(DB_URL, DB_LOGIN, DB_PASSWORD);
+ connection = DriverManager.getConnection(dbUrl, dbLogin, dbPassword);
} catch (SQLException sqlEx) {
if (++n >= 3) {
throw sqlEx;
@@ -164,11 +165,11 @@ public class DB { public static String getHttpsPort() {
- return HTTPS_PORT;
+ return httpsPort;
}
public static String getHttpPort() {
- return HTTP_PORT;
+ return httpPort;
}
/**
@@ -238,34 +239,35 @@ public class DB { */
private void runInitScript(Connection connection, int scriptId) {
String scriptDir = (String) props.get("org.onap.dmaap.datarouter.provserver.dbscripts");
- StringBuilder strBuilder = new StringBuilder();
- try {
- String scriptFile = String.format("%s/sql_init_%02d.sql", scriptDir, scriptId);
- if (!(new File(scriptFile)).exists()) {
- intlogger.error("PROV9005 Failed to load sql script from : " + scriptFile);
- System.exit(1);
- }
- LineNumberReader lineReader = new LineNumberReader(new FileReader(scriptFile));
+ String scriptFile = String.format("%s/sql_init_%02d.sql", scriptDir, scriptId);
+ if (!(new File(scriptFile)).exists()) {
+ intlogger.error("PROV9005 Failed to load sql script from : " + scriptFile);
+ exit(1);
+ }
+ try (LineNumberReader lineReader = new LineNumberReader(new FileReader(scriptFile));
+ Statement statement = connection.createStatement()) {
+ StringBuilder strBuilder = new StringBuilder();
String line;
while ((line = lineReader.readLine()) != null) {
if (!line.startsWith("--")) {
line = line.trim();
strBuilder.append(line);
- if (line.endsWith(";")) {
- // Execute one DDL statement
- String sql = strBuilder.toString();
- strBuilder.setLength(0);
- Statement statement = connection.createStatement();
- statement.execute(sql);
- statement.close();
- }
+ executeDdlStatement(statement, strBuilder, line);
}
}
- lineReader.close();
strBuilder.setLength(0);
} catch (Exception e) {
intlogger.error("PROV9002 Error when initializing table: " + e.getMessage(), e);
- System.exit(1);
+ exit(1);
+ }
+ }
+
+ private void executeDdlStatement(Statement statement, StringBuilder strBuilder, String line) throws SQLException {
+ if (line.endsWith(";")) {
+ // Execute one DDL statement
+ String sql = strBuilder.toString();
+ strBuilder.setLength(0);
+ statement.execute(sql);
}
}
}
diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/HttpServletUtils.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/HttpServletUtils.java index d9f36de3..f59dc919 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/HttpServletUtils.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/HttpServletUtils.java @@ -20,14 +20,19 @@ * * ECOMP is a trademark and service mark of AT&T Intellectual Property. * * ******************************************************************************/ -package org.onap.dmaap.datarouter.provisioning.utils; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; +package org.onap.dmaap.datarouter.provisioning.utils; import com.att.eelf.configuration.EELFLogger; +import java.io.IOException; +import javax.servlet.http.HttpServletResponse; public class HttpServletUtils { + + private HttpServletUtils(){ + + } + public static void sendResponseError(HttpServletResponse response, int errorCode, String message, EELFLogger intlogger) { try { response.sendError(errorCode, message); diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/LOGJSONObject.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/LOGJSONObject.java index f7e08748..bc1f4493 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/LOGJSONObject.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/LOGJSONObject.java @@ -28,11 +28,14 @@ import com.att.eelf.configuration.EELFManager; import java.io.IOException; import java.io.StringWriter; import java.io.Writer; -import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Modifier; -import java.util.*; - +import java.util.Collection; +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Set; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONString; @@ -93,10 +96,13 @@ import org.json.JSONTokener; * @version 2012-12-01 */ public class LOGJSONObject { + /** * The maximum number of keys in the key pool. */ - private static final int keyPoolSize = 100; + private static final int KEY_POOL_SIZE = 100; + private static final String USING_DEFAULT_VALUE = "Using defaultValue: "; + private static final String JSON_OBJECT_CONST = "JSONObject["; /** * Key pooling is like string interning, but without permanently tying up @@ -104,7 +110,7 @@ public class LOGJSONObject { * JSONObjects will be avoided by using a key pool to manage unique key * string objects. This is used by JSONObject.put(string, object). */ - private static Map<String, Object> keyPool = new LinkedHashMap<String, Object>(keyPoolSize); + private static Map<String, Object> keyPool = new LinkedHashMap<>(KEY_POOL_SIZE); private static final EELFLogger intlogger = EELFManager.getInstance().getLogger("InternalLog"); @@ -186,21 +192,18 @@ public class LOGJSONObject { } } - /** * The map where the JSONObject's properties are kept. */ private final Map<String, Object> map; - /** * It is sometimes more convenient and less ambiguous to have a * <code>NULL</code> object than to use Java's <code>null</code> value. * <code>JSONObject.NULL.equals(null)</code> returns <code>true</code>. * <code>JSONObject.NULL.toString()</code> returns <code>"null"</code>. */ - public static final Object NULL = new Null(); - + private static final Object NULL = new Null(); /** * Construct an empty JSONObject. @@ -209,7 +212,6 @@ public class LOGJSONObject { this.map = new LinkedHashMap<>(); } - /** * Construct a JSONObject from a subset of another JSONObject. * An array of strings is used to identify the keys that should be copied. @@ -229,7 +231,6 @@ public class LOGJSONObject { } } - /** * Construct a JSONObject from a JSONTokener. * @@ -287,7 +288,6 @@ public class LOGJSONObject { } } - /** * Construct a JSONObject from a Map. * @@ -296,7 +296,7 @@ public class LOGJSONObject { * @throws JSONException */ public LOGJSONObject(Map<String, Object> map) { - this.map = new LinkedHashMap<String, Object>(); + this.map = new LinkedHashMap<>(); if (map != null) { Iterator<Map.Entry<String, Object>> i = map.entrySet().iterator(); while (i.hasNext()) { @@ -309,7 +309,6 @@ public class LOGJSONObject { } } - /** * Construct a JSONObject from an Object using bean getters. * It reflects on all of the public methods of the object. @@ -334,88 +333,6 @@ public class LOGJSONObject { this.populateMap(bean); } - - /** - * Construct a JSONObject from an Object, using reflection to find the - * public members. The resulting JSONObject's keys will be the strings - * from the names array, and the values will be the field values associated - * with those keys in the object. If a key is not found or not visible, - * then it will not be copied into the new JSONObject. - * - * @param object An object that has fields that should be used to make a - * JSONObject. - * @param names An array of strings, the names of the fields to be obtained - * from the object. - */ - public LOGJSONObject(Object object, String names[]) { - this(); - Class<? extends Object> c = object.getClass(); - for (int i = 0; i < names.length; i += 1) { - String name = names[i]; - try { - this.putOpt(name, c.getField(name).get(object)); - } catch (Exception ignore) { - } - } - } - - - /** - * Construct a JSONObject from a source JSON text string. - * This is the most commonly used JSONObject constructor. - * - * @param source A string beginning - * with <code>{</code> <small>(left brace)</small> and ending - * with <code>}</code> <small>(right brace)</small>. - * @throws JSONException If there is a syntax error in the source - * string or a duplicated key. - */ - public LOGJSONObject(String source) throws JSONException { - this(new JSONTokener(source)); - } - - - /** - * Construct a JSONObject from a ResourceBundle. - * - * @param baseName The ResourceBundle base name. - * @param locale The Locale to load the ResourceBundle for. - * @throws JSONException If any JSONExceptions are detected. - */ - public LOGJSONObject(String baseName, Locale locale) throws JSONException { - this(); - ResourceBundle bundle = ResourceBundle.getBundle(baseName, locale, - Thread.currentThread().getContextClassLoader()); - -// Iterate through the keys in the bundle. - - Enumeration<?> keys = bundle.getKeys(); - while (keys.hasMoreElements()) { - Object key = keys.nextElement(); - if (key instanceof String) { - -// Go through the path, ensuring that there is a nested JSONObject for each -// segment except the last. Add the value using the last segment's name into -// the deepest nested JSONObject. - - String[] path = ((String) key).split("\\."); - int last = path.length - 1; - LOGJSONObject target = this; - for (int i = 0; i < last; i += 1) { - String segment = path[i]; - LOGJSONObject nextTarget = target.optJSONObject(segment); - if (nextTarget == null) { - nextTarget = new LOGJSONObject(); - target.put(segment, nextTarget); - } - target = nextTarget; - } - target.put(path[last], bundle.getString((String) key)); - } - } - } - - /** * Accumulate values under a key. It is similar to the put method except * that if there is already an object stored under the key then a @@ -434,15 +351,15 @@ public class LOGJSONObject { * or if the key is null. */ public LOGJSONObject accumulate( - String key, - Object value - ) throws JSONException { + String key, + Object value + ) { testValidity(value); Object object = this.opt(key); if (object == null) { this.put(key, value instanceof JSONArray - ? new JSONArray().put(value) - : value); + ? new JSONArray().put(value) + : value); } else if (object instanceof JSONArray) { ((JSONArray) object).put(value); } else { @@ -451,7 +368,6 @@ public class LOGJSONObject { return this; } - /** * Append values to the array under a key. If the key does not exist in the * JSONObject, then the key is put in the JSONObject with its value being a @@ -464,7 +380,7 @@ public class LOGJSONObject { * @throws JSONException If the key is null or if the current value * associated with the key is not a JSONArray. */ - public LOGJSONObject append(String key, Object value) throws JSONException { + public LOGJSONObject append(String key, Object value) { testValidity(value); Object object = this.opt(key); if (object == null) { @@ -472,13 +388,12 @@ public class LOGJSONObject { } else if (object instanceof JSONArray) { this.put(key, ((JSONArray) object).put(value)); } else { - throw new JSONException("JSONObject[" + key + - "] is not a JSONArray."); + throw new JSONException(JSON_OBJECT_CONST + key + + "] is not a JSONArray."); } return this; } - /** * Produce a string from a double. The string "null" will be returned if * the number is not finite. @@ -494,8 +409,8 @@ public class LOGJSONObject { // Shave off trailing zeros and decimal point, if possible. String string = Double.toString(d); - if (string.indexOf('.') > 0 && string.indexOf('e') < 0 && - string.indexOf('E') < 0) { + if (string.indexOf('.') > 0 && string.indexOf('e') < 0 + && string.indexOf('E') < 0) { while (string.endsWith("0")) { string = string.substring(0, string.length() - 1); } @@ -506,7 +421,6 @@ public class LOGJSONObject { return string; } - /** * Get the value object associated with a key. * @@ -514,19 +428,18 @@ public class LOGJSONObject { * @return The object associated with the key. * @throws JSONException if the key is not found. */ - public Object get(String key) throws JSONException { + public Object get(String key) { if (key == null) { throw new JSONException("Null key."); } Object object = this.opt(key); if (object == null) { - throw new JSONException("JSONObject[" + quote(key) + - "] not found."); + throw new JSONException(JSON_OBJECT_CONST + quote(key) + + "] not found."); } return object; } - /** * Get the boolean value associated with a key. * @@ -534,22 +447,21 @@ public class LOGJSONObject { * @return The truth. * @throws JSONException if the value is not a Boolean or the String "true" or "false". */ - public boolean getBoolean(String key) throws JSONException { + public boolean getBoolean(String key) { Object object = this.get(key); if (object.equals(Boolean.FALSE) || - (object instanceof String && - ((String) object).equalsIgnoreCase("false"))) { + (object instanceof String && + "false".equalsIgnoreCase((String) object))) { return false; } else if (object.equals(Boolean.TRUE) || - (object instanceof String && - ((String) object).equalsIgnoreCase("true"))) { + (object instanceof String && + "true".equalsIgnoreCase((String) object))) { return true; } - throw new JSONException("JSONObject[" + quote(key) + - "] is not a Boolean."); + throw new JSONException(JSON_OBJECT_CONST + quote(key) + + "] is not a Boolean."); } - /** * Get the double value associated with a key. * @@ -562,15 +474,14 @@ public class LOGJSONObject { Object object = this.get(key); try { return object instanceof Number - ? ((Number) object).doubleValue() - : Double.parseDouble((String) object); + ? ((Number) object).doubleValue() + : Double.parseDouble((String) object); } catch (Exception e) { - intlogger.error("JSONObject[" + quote(key) + "] is not a number.", e); - throw new JSONException("JSONObject[" + quote(key) + "] is not a number."); + intlogger.error(JSON_OBJECT_CONST + quote(key) + "] is not a number.", e); + throw new JSONException(JSON_OBJECT_CONST + quote(key) + "] is not a number."); } } - /** * Get the int value associated with a key. * @@ -583,15 +494,14 @@ public class LOGJSONObject { Object object = this.get(key); try { return object instanceof Number - ? ((Number) object).intValue() - : Integer.parseInt((String) object); + ? ((Number) object).intValue() + : Integer.parseInt((String) object); } catch (Exception e) { - intlogger.error("JSONObject[" + quote(key) + "] is not an int.", e); - throw new JSONException("JSONObject[" + quote(key) + "] is not an int."); + intlogger.error(JSON_OBJECT_CONST + quote(key) + "] is not an int.", e); + throw new JSONException(JSON_OBJECT_CONST + quote(key) + "] is not an int."); } } - /** * Get the JSONArray value associated with a key. * @@ -600,16 +510,15 @@ public class LOGJSONObject { * @throws JSONException if the key is not found or * if the value is not a JSONArray. */ - public JSONArray getJSONArray(String key) throws JSONException { + public JSONArray getJSONArray(String key) { Object object = this.get(key); if (object instanceof JSONArray) { return (JSONArray) object; } - throw new JSONException("JSONObject[" + quote(key) + - "] is not a JSONArray."); + throw new JSONException(JSON_OBJECT_CONST + quote(key) + + "] is not a JSONArray."); } - /** * Get the JSONObject value associated with a key. * @@ -618,16 +527,15 @@ public class LOGJSONObject { * @throws JSONException if the key is not found or * if the value is not a JSONObject. */ - public LOGJSONObject getJSONObject(String key) throws JSONException { + public LOGJSONObject getJSONObject(String key) { Object object = this.get(key); if (object instanceof LOGJSONObject) { return (LOGJSONObject) object; } - throw new JSONException("JSONObject[" + quote(key) + - "] is not a JSONObject."); + throw new JSONException(JSON_OBJECT_CONST + quote(key) + + "] is not a JSONObject."); } - /** * Get the long value associated with a key. * @@ -636,19 +544,18 @@ public class LOGJSONObject { * @throws JSONException if the key is not found or if the value cannot * be converted to a long. */ - public long getLong(String key) throws JSONException { + public long getLong(String key) { Object object = this.get(key); try { return object instanceof Number - ? ((Number) object).longValue() - : Long.parseLong((String) object); + ? ((Number) object).longValue() + : Long.parseLong((String) object); } catch (Exception e) { - intlogger.error("JSONObject[" + quote(key) + "] is not a long.", e); - throw new JSONException("JSONObject[" + quote(key) + "] is not a long."); + intlogger.error(JSON_OBJECT_CONST + quote(key) + "] is not a long.", e); + throw new JSONException(JSON_OBJECT_CONST + quote(key) + "] is not a long."); } } - /** * Get an array of field names from a JSONObject. * @@ -657,7 +564,7 @@ public class LOGJSONObject { public static String[] getNames(LOGJSONObject jo) { int length = jo.length(); if (length == 0) { - return null; + return new String[]{}; } Iterator<String> iterator = jo.keys(); String[] names = new String[length]; @@ -669,30 +576,6 @@ public class LOGJSONObject { return names; } - - /** - * Get an array of field names from an Object. - * - * @return An array of field names, or null if there are no names. - */ - public static String[] getNames(Object object) { - if (object == null) { - return null; - } - Class<? extends Object> klass = object.getClass(); - Field[] fields = klass.getFields(); - int length = fields.length; - if (length == 0) { - return null; - } - String[] names = new String[length]; - for (int i = 0; i < length; i += 1) { - names[i] = fields[i].getName(); - } - return names; - } - - /** * Get the string associated with a key. * @@ -705,11 +588,10 @@ public class LOGJSONObject { if (object instanceof String) { return (String) object; } - throw new JSONException("JSONObject[" + quote(key) + - "] not a string."); + throw new JSONException(JSON_OBJECT_CONST + quote(key) + + "] not a string."); } - /** * Determine if the JSONObject contains a specific key. * @@ -720,7 +602,6 @@ public class LOGJSONObject { return this.map.containsKey(key); } - /** * Increment a property of a JSONObject. If there is no such property, * create one with a value of 1. If there is such a property, and if @@ -749,20 +630,6 @@ public class LOGJSONObject { return this; } - - /** - * Determine if the value associated with the key is null or if there is - * no value. - * - * @param key A key string. - * @return true if there is no value associated with the key or if - * the value is the JSONObject.NULL object. - */ - public boolean isNull(String key) { - return LOGJSONObject.NULL.equals(this.opt(key)); - } - - /** * Get an enumeration of the keys of the JSONObject. * @@ -772,7 +639,6 @@ public class LOGJSONObject { return this.keySet().iterator(); } - /** * Get a set of keys of the JSONObject. * @@ -782,7 +648,6 @@ public class LOGJSONObject { return this.map.keySet(); } - /** * Get the number of keys stored in the JSONObject. * @@ -792,7 +657,6 @@ public class LOGJSONObject { return this.map.size(); } - /** * Produce a JSONArray containing the names of the elements of this * JSONObject. @@ -817,7 +681,7 @@ public class LOGJSONObject { * @throws JSONException If n is a non-finite number. */ public static String numberToString(Number number) - throws JSONException { + { if (number == null) { throw new JSONException("Null pointer"); } @@ -827,7 +691,7 @@ public class LOGJSONObject { String string = number.toString(); if (string.indexOf('.') > 0 && string.indexOf('e') < 0 && - string.indexOf('E') < 0) { + string.indexOf('E') < 0) { while (string.endsWith("0")) { string = string.substring(0, string.length() - 1); } @@ -838,7 +702,6 @@ public class LOGJSONObject { return string; } - /** * Get an optional value associated with a key. * @@ -849,20 +712,6 @@ public class LOGJSONObject { return key == null ? null : this.map.get(key); } - - /** - * Get an optional boolean associated with a key. - * It returns false if there is no such key, or if the value is not - * Boolean.TRUE or the String "true". - * - * @param key A key string. - * @return The truth. - */ - public boolean optBoolean(String key) { - return this.optBoolean(key, false); - } - - /** * Get an optional boolean associated with a key. * It returns the defaultValue if there is no such key, or if it is not @@ -876,26 +725,11 @@ public class LOGJSONObject { try { return this.getBoolean(key); } catch (Exception e) { - intlogger.trace("Using defaultValue: " + defaultValue, e); + intlogger.trace(USING_DEFAULT_VALUE + defaultValue, e); return defaultValue; } } - - /** - * Get an optional double associated with a key, - * or NaN if there is no such key or if its value is not a number. - * If the value is a string, an attempt will be made to evaluate it as - * a number. - * - * @param key A string which is the key. - * @return An object which is the value. - */ - public double optDouble(String key) { - return this.optDouble(key, Double.NaN); - } - - /** * Get an optional double associated with a key, or the * defaultValue if there is no such key or if its value is not a number. @@ -910,26 +744,11 @@ public class LOGJSONObject { try { return this.getDouble(key); } catch (Exception e) { - intlogger.trace("Using defaultValue: " + defaultValue, e); + intlogger.trace(USING_DEFAULT_VALUE + defaultValue, e); return defaultValue; } } - - /** - * Get an optional int value associated with a key, - * or zero if there is no such key or if the value is not a number. - * If the value is a string, an attempt will be made to evaluate it as - * a number. - * - * @param key A key string. - * @return An object which is the value. - */ - public int optInt(String key) { - return this.optInt(key, 0); - } - - /** * Get an optional int value associated with a key, * or the default if there is no such key or if the value is not a number. @@ -944,55 +763,13 @@ public class LOGJSONObject { try { return this.getInt(key); } catch (Exception e) { - intlogger.trace("Using defaultValue: " + defaultValue, e); + intlogger.trace(USING_DEFAULT_VALUE + defaultValue, e); return defaultValue; } } /** - * Get an optional JSONArray associated with a key. - * It returns null if there is no such key, or if its value is not a - * JSONArray. - * - * @param key A key string. - * @return A JSONArray which is the value. - */ - public JSONArray optJSONArray(String key) { - Object o = this.opt(key); - return o instanceof JSONArray ? (JSONArray) o : null; - } - - - /** - * Get an optional JSONObject associated with a key. - * It returns null if there is no such key, or if its value is not a - * JSONObject. - * - * @param key A key string. - * @return A JSONObject which is the value. - */ - public LOGJSONObject optJSONObject(String key) { - Object object = this.opt(key); - return object instanceof LOGJSONObject ? (LOGJSONObject) object : null; - } - - - /** - * Get an optional long value associated with a key, - * or zero if there is no such key or if the value is not a number. - * If the value is a string, an attempt will be made to evaluate it as - * a number. - * - * @param key A key string. - * @return An object which is the value. - */ - public long optLong(String key) { - return this.optLong(key, 0); - } - - - /** * Get an optional long value associated with a key, * or the default if there is no such key or if the value is not a number. * If the value is a string, an attempt will be made to evaluate it as @@ -1010,20 +787,6 @@ public class LOGJSONObject { } } - - /** - * Get an optional string associated with a key. - * It returns an empty string if there is no such key. If the value is not - * a string and is not null, then it is converted to a string. - * - * @param key A key string. - * @return A string which is the value. - */ - public String optString(String key) { - return this.optString(key, ""); - } - - /** * Get an optional string associated with a key. * It returns the defaultValue if there is no such key. @@ -1037,17 +800,16 @@ public class LOGJSONObject { return NULL.equals(object) ? defaultValue : object.toString(); } - private void populateMap(Object bean) { - Class<? extends Object> klass = bean.getClass(); + Class<?> klass = bean.getClass(); // If klass is a System class then set includeSuperClass to false. boolean includeSuperClass = klass.getClassLoader() != null; Method[] methods = includeSuperClass - ? klass.getMethods() - : klass.getDeclaredMethods(); + ? klass.getMethods() + : klass.getDeclaredMethods(); for (int i = 0; i < methods.length; i += 1) { try { Method method = methods[i]; @@ -1056,7 +818,7 @@ public class LOGJSONObject { String key = ""; if (name.startsWith("get")) { if ("getClass".equals(name) || - "getDeclaringClass".equals(name)) { + "getDeclaringClass".equals(name)) { key = ""; } else { key = name.substring(3); @@ -1065,13 +827,13 @@ public class LOGJSONObject { key = name.substring(2); } if (key.length() > 0 && - Character.isUpperCase(key.charAt(0)) && - method.getParameterTypes().length == 0) { + Character.isUpperCase(key.charAt(0)) && + method.getParameterTypes().length == 0) { if (key.length() == 1) { key = key.toLowerCase(); } else if (!Character.isUpperCase(key.charAt(1))) { key = key.substring(0, 1).toLowerCase() + - key.substring(1); + key.substring(1); } Object result = method.invoke(bean, (Object[]) null); @@ -1086,36 +848,6 @@ public class LOGJSONObject { } } - - /** - * Put a key/boolean pair in the JSONObject. - * - * @param key A key string. - * @param value A boolean which is the value. - * @return this. - * @throws JSONException If the key is null. - */ - public LOGJSONObject put(String key, boolean value) throws JSONException { - this.put(key, value ? Boolean.TRUE : Boolean.FALSE); - return this; - } - - - /** - * Put a key/value pair in the JSONObject, where the value will be a - * JSONArray which is produced from a Collection. - * - * @param key A key string. - * @param value A Collection value. - * @return this. - * @throws JSONException - */ - public LOGJSONObject put(String key, Collection<Object> value) throws JSONException { - this.put(key, new JSONArray(value)); - return this; - } - - /** * Put a key/double pair in the JSONObject. * @@ -1124,12 +856,11 @@ public class LOGJSONObject { * @return this. * @throws JSONException If the key is null or if the number is invalid. */ - public LOGJSONObject put(String key, double value) throws JSONException { + public LOGJSONObject put(String key, double value) { this.put(key, new Double(value)); return this; } - /** * Put a key/int pair in the JSONObject. * @@ -1138,12 +869,11 @@ public class LOGJSONObject { * @return this. * @throws JSONException If the key is null. */ - public LOGJSONObject put(String key, int value) throws JSONException { + public LOGJSONObject put(String key, int value) { this.put(key, new Integer(value)); return this; } - /** * Put a key/long pair in the JSONObject. * @@ -1152,27 +882,11 @@ public class LOGJSONObject { * @return this. * @throws JSONException If the key is null. */ - public LOGJSONObject put(String key, long value) throws JSONException { + public LOGJSONObject put(String key, long value) { this.put(key, new Long(value)); return this; } - - /** - * Put a key/value pair in the JSONObject, where the value will be a - * JSONObject which is produced from a Map. - * - * @param key A key string. - * @param value A Map value. - * @return this. - * @throws JSONException - */ - public LOGJSONObject put(String key, Map<String, Object> value) throws JSONException { - this.put(key, new LOGJSONObject(value)); - return this; - } - - /** * Put a key/value pair in the JSONObject. If the value is null, * then the key will be removed from the JSONObject if it is present. @@ -1185,7 +899,7 @@ public class LOGJSONObject { * @throws JSONException If the value is non-finite number * or if the key is null. */ - public LOGJSONObject put(String key, Object value) throws JSONException { + public LOGJSONObject put(String key, Object value) { String pooled; if (key == null) { throw new JSONException("Null key."); @@ -1194,8 +908,8 @@ public class LOGJSONObject { testValidity(value); pooled = (String) keyPool.get(key); if (pooled == null) { - if (keyPool.size() >= keyPoolSize) { - keyPool = new LinkedHashMap<String, Object>(keyPoolSize); + if (keyPool.size() >= KEY_POOL_SIZE) { + keyPool = new LinkedHashMap<>(KEY_POOL_SIZE); } keyPool.put(key, key); } else { @@ -1208,7 +922,6 @@ public class LOGJSONObject { return this; } - /** * Put a key/value pair in the JSONObject, but only if the key and the * value are both non-null, and only if there is not already a member @@ -1219,7 +932,7 @@ public class LOGJSONObject { * @return his. * @throws JSONException if the key is a duplicate */ - public LOGJSONObject putOnce(String key, Object value) throws JSONException { + public LOGJSONObject putOnce(String key, Object value) { if (key != null && value != null) { if (this.opt(key) != null) { throw new JSONException("Duplicate key \"" + key + "\""); @@ -1229,26 +942,6 @@ public class LOGJSONObject { return this; } - - /** - * Put a key/value pair in the JSONObject, but only if the - * key and the value are both non-null. - * - * @param key A key string. - * @param value An object which is the value. It should be of one of these - * types: Boolean, Double, Integer, JSONArray, JSONObject, Long, String, - * or the JSONObject.NULL object. - * @return this. - * @throws JSONException If the value is a non-finite number. - */ - public LOGJSONObject putOpt(String key, Object value) throws JSONException { - if (key != null && value != null) { - this.put(key, value); - } - return this; - } - - /** * Produce a string in double quotes with backslash sequences in all the * right places. A backslash will be inserted within </, producing <\/, @@ -1315,7 +1008,7 @@ public class LOGJSONObject { break; default: if (c < ' ' || (c >= '\u0080' && c < '\u00a0') - || (c >= '\u2000' && c < '\u2100')) { + || (c >= '\u2000' && c < '\u2100')) { w.write("\\u"); hhhh = Integer.toHexString(c); w.write("0000", 0, 4 - hhhh.length()); @@ -1349,16 +1042,16 @@ public class LOGJSONObject { */ public static Object stringToValue(String string) { Double d; - if (string.equals("")) { + if ("".equals(string)) { return string; } - if (string.equalsIgnoreCase("true")) { + if ("true".equalsIgnoreCase(string)) { return Boolean.TRUE; } - if (string.equalsIgnoreCase("false")) { + if ("false".equalsIgnoreCase(string)) { return Boolean.FALSE; } - if (string.equalsIgnoreCase("null")) { + if ("null".equalsIgnoreCase(string)) { return LOGJSONObject.NULL; } @@ -1373,16 +1066,16 @@ public class LOGJSONObject { char b = string.charAt(0); if ((b >= '0' && b <= '9') || b == '.' || b == '-' || b == '+') { try { - if (string.indexOf('.') > -1 || - string.indexOf('e') > -1 || string.indexOf('E') > -1) { + if (string.indexOf('.') > -1 || string.indexOf('e') > -1 + || string.indexOf('E') > -1) { d = Double.valueOf(string); if (!d.isInfinite() && !d.isNaN()) { return d; } } else { Long myLong = new Long(string); - if (myLong.longValue() == myLong.intValue()) { - return new Integer(myLong.intValue()); + if (myLong == myLong.intValue()) { + return myLong.intValue(); } else { return myLong; } @@ -1394,7 +1087,6 @@ public class LOGJSONObject { return string; } - /** * Throw an exception if the object is a NaN or infinite number. * @@ -1406,18 +1098,15 @@ public class LOGJSONObject { if (o instanceof Double) { if (((Double) o).isInfinite() || ((Double) o).isNaN()) { throw new JSONException( - "JSON does not allow non-finite numbers."); - } - } else if (o instanceof Float) { - if (((Float) o).isInfinite() || ((Float) o).isNaN()) { - throw new JSONException( - "JSON does not allow non-finite numbers."); + "JSON does not allow non-finite numbers."); } + } else if (o instanceof Float && (((Float) o).isInfinite() || ((Float) o).isNaN())) { + throw new JSONException( + "JSON does not allow non-finite numbers."); } } } - /** * Produce a JSONArray containing the values of the members of this * JSONObject. @@ -1427,7 +1116,7 @@ public class LOGJSONObject { * @return A JSONArray of values. * @throws JSONException If any of the values are non-finite numbers. */ - public JSONArray toJSONArray(JSONArray names) throws JSONException { + public JSONArray toJSONArray(JSONArray names) { if (names == null || names.length() == 0) { return null; } @@ -1459,7 +1148,6 @@ public class LOGJSONObject { } } - /** * Make a prettyprinted JSON text of this JSONObject. * <p> @@ -1473,7 +1161,7 @@ public class LOGJSONObject { * with <code>}</code> <small>(right brace)</small>. * @throws JSONException If the object contains an invalid number. */ - public String toString(int indentFactor) throws JSONException { + public String toString(int indentFactor) { StringWriter w = new StringWriter(); synchronized (w.getBuffer()) { return this.write(w, indentFactor, 0).toString(); @@ -1503,27 +1191,27 @@ public class LOGJSONObject { * @throws JSONException If the value is or contains an invalid number. */ @SuppressWarnings("unchecked") - public static String valueToString(Object value) throws JSONException { + public static String valueToString(Object value) { if (value == null) { return "null"; } if (value instanceof JSONString) { - Object object; + String object; try { object = ((JSONString) value).toJSONString(); } catch (Exception e) { throw new JSONException(e); } - if (object instanceof String) { - return (String) object; + if (object != null) { + return object; } throw new JSONException("Bad value from toJSONString: " + object); } if (value instanceof Number) { return numberToString((Number) value); } - if (value instanceof Boolean || value instanceof LOGJSONObject || - value instanceof JSONArray) { + if (value instanceof Boolean || value instanceof LOGJSONObject + || value instanceof JSONArray) { return value.toString(); } if (value instanceof Map) { @@ -1557,12 +1245,12 @@ public class LOGJSONObject { return NULL; } if (object instanceof LOGJSONObject || object instanceof JSONArray || - NULL.equals(object) || object instanceof JSONString || - object instanceof Byte || object instanceof Character || - object instanceof Short || object instanceof Integer || - object instanceof Long || object instanceof Boolean || - object instanceof Float || object instanceof Double || - object instanceof String) { + NULL.equals(object) || object instanceof JSONString || + object instanceof Byte || object instanceof Character || + object instanceof Short || object instanceof Integer || + object instanceof Long || object instanceof Boolean || + object instanceof Float || object instanceof Double || + object instanceof String) { return object; } @@ -1577,13 +1265,13 @@ public class LOGJSONObject { } Package objectPackage = object.getClass().getPackage(); String objectPackageName = objectPackage != null - ? objectPackage.getName() - : ""; + ? objectPackage.getName() + : ""; if ( - objectPackageName.startsWith("java.") || - objectPackageName.startsWith("javax.") || - object.getClass().getClassLoader() == null - ) { + objectPackageName.startsWith("java.") || + objectPackageName.startsWith("javax.") || + object.getClass().getClassLoader() == null + ) { return object.toString(); } return new LOGJSONObject(object); @@ -1593,24 +1281,8 @@ public class LOGJSONObject { } } - - /** - * Write the contents of the JSONObject as JSON text to a writer. - * For compactness, no whitespace is added. - * <p> - * Warning: This method assumes that the data structure is acyclical. - * - * @return The writer. - * @throws JSONException - */ - public Writer write(Writer writer) throws JSONException { - return this.write(writer, 0, 0); - } - - @SuppressWarnings("unchecked") - static final Writer writeValue(Writer writer, Object value, - int indentFactor, int indent) throws JSONException, IOException { + static Writer writeValue(Writer writer, Object value, int indentFactor, int indent) throws IOException { if (value == null) { writer.write("null"); } else if (value instanceof LOGJSONObject) { @@ -1620,8 +1292,7 @@ public class LOGJSONObject { } else if (value instanceof Map) { new LOGJSONObject((Map<String, Object>) value).write(writer, indentFactor, indent); } else if (value instanceof Collection) { - new JSONArray((Collection<Object>) value).write(writer, indentFactor, - indent); + new JSONArray((Collection<Object>) value).write(writer, indentFactor, indent); } else if (value.getClass().isArray()) { new JSONArray(value).write(writer, indentFactor, indent); } else if (value instanceof Number) { @@ -1642,7 +1313,7 @@ public class LOGJSONObject { return writer; } - static final void indent(Writer writer, int indent) throws IOException { + private static void indent(Writer writer, int indent) throws IOException { for (int i = 0; i < indent; i += 1) { writer.write(' '); } @@ -1658,7 +1329,7 @@ public class LOGJSONObject { * @throws JSONException */ Writer write(Writer writer, int indentFactor, int indent) - throws JSONException { + { try { boolean commanate = false; final int length = this.length(); @@ -1690,7 +1361,7 @@ public class LOGJSONObject { writer.write(' '); } writeValue(writer, this.map.get(key), indentFactor, - newindent); + newindent); commanate = true; } if (indentFactor > 0) { diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/LogfileLoader.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/LogfileLoader.java index 3ba1a151..b48907fc 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/LogfileLoader.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/LogfileLoader.java @@ -1,501 +1,491 @@ -/*******************************************************************************
- * ============LICENSE_START==================================================
- * * org.onap.dmaap
- * * ===========================================================================
- * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
- * * ===========================================================================
- * * Licensed under the Apache License, Version 2.0 (the "License");
- * * you may not use this file except in compliance with the License.
- * * You may obtain a copy of the License at
- * *
- * * http://www.apache.org/licenses/LICENSE-2.0
- * *
- * * Unless required by applicable law or agreed to in writing, software
- * * distributed under the License is distributed on an "AS IS" BASIS,
- * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * * See the License for the specific language governing permissions and
- * * limitations under the License.
- * * ============LICENSE_END====================================================
- * *
- * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
- * *
- ******************************************************************************/
-
-
-package org.onap.dmaap.datarouter.provisioning.utils;
-
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.FileReader;
-import java.io.FilenameFilter;
-import java.io.IOException;
-import java.io.InputStreamReader;
-import java.io.LineNumberReader;
-import java.io.Reader;
-import java.sql.Connection;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.sql.Statement;
-import java.text.ParseException;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.Map;
-import java.util.TreeSet;
-import java.util.zip.GZIPInputStream;
-
-import com.att.eelf.configuration.EELFLogger;
-import com.att.eelf.configuration.EELFManager;
-import org.onap.dmaap.datarouter.provisioning.BaseServlet;
-import org.onap.dmaap.datarouter.provisioning.beans.DeliveryExtraRecord;
-import org.onap.dmaap.datarouter.provisioning.beans.DeliveryRecord;
-import org.onap.dmaap.datarouter.provisioning.beans.ExpiryRecord;
-import org.onap.dmaap.datarouter.provisioning.beans.Loadable;
-import org.onap.dmaap.datarouter.provisioning.beans.LogRecord;
-import org.onap.dmaap.datarouter.provisioning.beans.Parameters;
-import org.onap.dmaap.datarouter.provisioning.beans.PubFailRecord;
-import org.onap.dmaap.datarouter.provisioning.beans.PublishRecord;
-
-/**
- * This class provides methods that run in a separate thread, in order to process logfiles uploaded into the spooldir.
- * These logfiles are loaded into the MariaDB LOG_RECORDS table. In a running provisioning server, there should only be
- * two places where records can be loaded into this table; here, and in the method DB.retroFit4() which may be run at
- * startup to load the old (1.0) style log tables into LOG_RECORDS;
- * <p>This method maintains an {@link RLEBitSet} which can be used to easily see what records are presently in the
- * database.
- * This bit set is used to synchronize between provisioning servers.</p>
- *
- * @author Robert Eby
- * @version $Id: LogfileLoader.java,v 1.22 2014/03/12 19:45:41 eby Exp $
- */
-public class LogfileLoader extends Thread {
- /**
- * Default number of log records to keep when pruning. Keep 10M by default.
- */
- public static final long DEFAULT_LOG_RETENTION = 10000000L;
- /**
- * NOT USED: Percentage of free space required before old records are removed.
- */
- public static final int REQUIRED_FREE_PCT = 20;
-
- /**
- * This is a singleton -- there is only one LogfileLoader object in the server
- */
- private static LogfileLoader logfileLoader;
-
- /**
- * Get the singleton LogfileLoader object, and start it if it is not running.
- *
- * @return the LogfileLoader
- */
- public static synchronized LogfileLoader getLoader() {
- if (logfileLoader == null)
- logfileLoader = new LogfileLoader();
- if (!logfileLoader.isAlive())
- logfileLoader.start();
- return logfileLoader;
- }
-
- /**
- * The PreparedStatement which is loaded by a <i>Loadable</i>.
- */
- public static final String INSERT_SQL = "insert into LOG_RECORDS values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
- /**
- * Each server can assign this many IDs
- */
- private static final long SET_SIZE = (1L << 56);
-
- private final EELFLogger logger;
- private final DB db;
- private final String spooldir;
- private final long set_start;
- private final long set_end;
- private RLEBitSet seq_set;
- private long nextid;
- private boolean idle;
-
- private LogfileLoader() {
- this.logger = EELFManager.getInstance().getLogger("InternalLog");
- this.db = new DB();
- this.spooldir = db.getProperties().getProperty("org.onap.dmaap.datarouter.provserver.spooldir");
- this.set_start = getIdRange();
- this.set_end = set_start + SET_SIZE - 1;
- this.seq_set = new RLEBitSet();
- this.nextid = 0;
- this.idle = false;
-
- // This is a potentially lengthy operation, so has been moved to run()
- //initializeNextid();
- this.setDaemon(true);
- this.setName("LogfileLoader");
- }
-
- private long getIdRange() {
- long n;
- if (BaseServlet.isInitialActivePOD())
- n = 0;
- else if (BaseServlet.isInitialStandbyPOD())
- n = SET_SIZE;
- else
- n = SET_SIZE * 2;
- String r = String.format("[%X .. %X]", n, n + SET_SIZE - 1);
- logger.debug("This server shall assign RECORD_IDs in the range " + r);
- return n;
- }
-
- /**
- * Return the bit set representing the record ID's that are loaded in this database.
- *
- * @return the bit set
- */
- public RLEBitSet getBitSet() {
- return seq_set;
- }
-
- /**
- * True if the LogfileLoader is currently waiting for work.
- *
- * @return true if idle
- */
- public boolean isIdle() {
- return idle;
- }
-
- /**
- * Run continuously to look for new logfiles in the spool directory and import them into the DB.
- * The spool is checked once per second. If free space on the MariaDB filesystem falls below
- * REQUIRED_FREE_PCT (normally 20%) then the oldest logfile entries are removed and the LOG_RECORDS
- * table is compacted until free space rises above the threshold.
- */
- @Override
- public void run() {
- initializeNextid(); // moved from the constructor
- while (true) {
- try {
- File dirfile = new File(spooldir);
- while (true) {
- // process IN files
- File[] infiles = dirfile.listFiles(new FilenameFilter() {
- @Override
- public boolean accept(File dir, String name) {
- return name.startsWith("IN.");
- }
- });
-
- if (infiles.length == 0) {
- idle = true;
- try {
- Thread.sleep(1000L);
- } catch (InterruptedException e) {
- Thread.currentThread().interrupt();
- }
- idle = false;
- } else {
- // Remove old rows
- if (pruneRecords()) {
- // Removed at least some entries, recompute the bit map
- initializeNextid();
- }
-
- // Process incoming logfiles
- for (File f : infiles) {
- if (logger.isDebugEnabled())
- logger.debug("PROV8001 Starting " + f + " ...");
- long time = System.currentTimeMillis();
- int[] n = process(f);
- time = System.currentTimeMillis() - time;
- logger.info(String
- .format("PROV8000 Processed %s in %d ms; %d of %d records.",
- f.toString(), time, n[0], n[1]));
- f.delete();
- }
- }
- }
- } catch (Exception e) {
- logger.warn("PROV0020: Caught exception in LogfileLoader: " + e);
- }
- }
- }
-
- boolean pruneRecords() {
- boolean did1 = false;
- long count = countRecords();
- long threshold = DEFAULT_LOG_RETENTION;
- Parameters param = Parameters.getParameter(Parameters.PROV_LOG_RETENTION);
- if (param != null) {
- try {
- long n = Long.parseLong(param.getValue());
- // This check is to prevent inadvertent errors from wiping the table out
- if (n > 1000000L)
- threshold = n;
- } catch (NumberFormatException e) {
- // ignore
- }
- }
- logger.debug("Pruning LOG_RECORD table: records in DB=" + count + ", threshold=" + threshold);
- if (count > threshold) {
- count -= threshold; // we need to remove this many records;
- Map<Long, Long> hist = getHistogram(); // histogram of records per day
- // Determine the cutoff point to remove the needed number of records
- long sum = 0;
- long cutoff = 0;
- for (Long day : new TreeSet<Long>(hist.keySet())) {
- sum += hist.get(day);
- cutoff = day;
- if (sum >= count)
- break;
- }
- cutoff++;
- cutoff *= 86400000L; // convert day to ms
- logger.debug(" Pruning records older than=" + (cutoff / 86400000L) + " (" + new Date(cutoff) + ")");
-
- Connection conn = null;
- try {
- // Limit to a million at a time to avoid typing up the DB for too long.
- conn = db.getConnection();
- try(PreparedStatement ps = conn.prepareStatement("DELETE from LOG_RECORDS where EVENT_TIME < ? limit 1000000")) {
- ps.setLong(1, cutoff);
- while (count > 0) {
- if (!ps.execute()) {
- int dcount = ps.getUpdateCount();
- count -= dcount;
- logger.debug(" " + dcount + " rows deleted.");
- did1 |= (dcount != 0);
- if (dcount == 0)
- count = 0; // prevent inf. loops
- } else {
- count = 0; // shouldn't happen!
- }
- }
- }
- try(Statement stmt = conn.createStatement()) {
- stmt.execute("OPTIMIZE TABLE LOG_RECORDS");
- }
- } catch (SQLException e) {
- System.err.println(e);
- logger.error(e.toString());
- } finally {
- db.release(conn);
- }
- }
- return did1;
- }
-
- long countRecords() {
- long count = 0;
- Connection conn = null;
- try {
- conn = db.getConnection();
- try(Statement stmt = conn.createStatement()) {
- try(ResultSet rs = stmt.executeQuery("SELECT COUNT(*) as COUNT from LOG_RECORDS")) {
- if (rs.next()) {
- count = rs.getLong("COUNT");
- }
- }
- }
- } catch (SQLException e) {
- System.err.println(e);
- logger.error(e.toString());
- } finally {
- db.release(conn);
- }
- return count;
- }
-
- Map<Long, Long> getHistogram() {
- Map<Long, Long> map = new HashMap<Long, Long>();
- Connection conn = null;
- try {
- logger.debug(" LOG_RECORD table histogram...");
- conn = db.getConnection();
- try(Statement stmt = conn.createStatement()) {
- try(ResultSet rs = stmt.executeQuery("SELECT FLOOR(EVENT_TIME/86400000) AS DAY, COUNT(*) AS COUNT FROM LOG_RECORDS GROUP BY DAY")) {
- while (rs.next()) {
- long day = rs.getLong("DAY");
- long cnt = rs.getLong("COUNT");
- map.put(day, cnt);
- logger.debug(" " + day + " " + cnt);
- }
- }
- }
- } catch (SQLException e) {
- System.err.println(e);
- logger.error(e.toString());
- } finally {
- db.release(conn);
- }
- return map;
- }
-
- private void initializeNextid() {
- Connection conn = null;
- try {
- conn = db.getConnection();
- RLEBitSet nbs = new RLEBitSet();
- try(Statement stmt = conn.createStatement()) {
- // Build a bitset of all records in the LOG_RECORDS table
- // We need to run this SELECT in stages, because otherwise we run out of memory!
- final long stepsize = 6000000L;
- boolean go_again = true;
- for (long i = 0; go_again; i += stepsize) {
- String sql = String.format("select RECORD_ID from LOG_RECORDS LIMIT %d,%d", i, stepsize);
- try (ResultSet rs = stmt.executeQuery(sql)) {
- go_again = false;
- while (rs.next()) {
- long n = rs.getLong("RECORD_ID");
- nbs.set(n);
- go_again = true;
- }
- }
- }
- }
- seq_set = nbs;
- // Compare with the range for this server
- // Determine the next ID for this set of record IDs
- RLEBitSet tbs = (RLEBitSet) nbs.clone();
- RLEBitSet idset = new RLEBitSet();
- idset.set(set_start, set_start + SET_SIZE);
- tbs.and(idset);
- long t = tbs.length();
- nextid = (t == 0) ? set_start : (t - 1);
- if (nextid >= set_start + SET_SIZE) {
- // Handle wraparound, when the IDs reach the end of our "range"
- Long[] last = null;
- Iterator<Long[]> li = tbs.getRangeIterator();
- while (li.hasNext()) {
- last = li.next();
- }
- if (last != null) {
- tbs.clear(last[0], last[1] + 1);
- t = tbs.length();
- nextid = (t == 0) ? set_start : (t - 1);
- }
- }
- logger.debug(String.format("initializeNextid, next ID is %d (%x)", nextid, nextid));
- } catch (SQLException e) {
- System.err.println(e);
- logger.error(e.toString());
- } finally {
- db.release(conn);
- }
- }
-
- @SuppressWarnings("resource")
- int[] process(File f) {
- int ok = 0, total = 0;
- try {
- Connection conn = db.getConnection();
- PreparedStatement ps = conn.prepareStatement(INSERT_SQL);
- Reader r = f.getPath().endsWith(".gz")
- ? new InputStreamReader(new GZIPInputStream(new FileInputStream(f)))
- : new FileReader(f);
- try(LineNumberReader in = new LineNumberReader(r)) {
- String line;
- while ((line = in.readLine()) != null) {
- try {
- for (Loadable rec : buildRecords(line)) {
- rec.load(ps);
- if (rec instanceof LogRecord) {
- LogRecord lr = ((LogRecord) rec);
- if (!seq_set.get(lr.getRecordId())) {
- ps.executeUpdate();
- seq_set.set(lr.getRecordId());
- } else
- logger.debug("Duplicate record ignored: " + lr.getRecordId());
- } else {
- if (++nextid > set_end)
- nextid = set_start;
- ps.setLong(18, nextid);
- ps.executeUpdate();
- seq_set.set(nextid);
- }
- ps.clearParameters();
- ok++;
- }
- } catch (SQLException e) {
- logger.warn("PROV8003 Invalid value in record: " + line);
- logger.debug(e.toString(), e);
- } catch (NumberFormatException e) {
- logger.warn("PROV8004 Invalid number in record: " + line);
- logger.debug(e.toString());
- } catch (ParseException e) {
- logger.warn("PROV8005 Invalid date in record: " + line);
- logger.debug(e.toString());
- } catch (Exception e) {
- logger.warn("PROV8006 Invalid pattern in record: " + line);
- logger.debug(e.toString(), e);
- }
- total++;
- }
- }
- ps.close();
- db.release(conn);
- conn = null;
- } catch (FileNotFoundException e) {
- logger.warn("PROV8007 Exception reading " + f + ": " + e);
- } catch (IOException e) {
- logger.warn("PROV8007 Exception reading " + f + ": " + e);
- } catch (SQLException e) {
- logger.warn("PROV8007 Exception reading " + f + ": " + e);
- }
- return new int[]{ok, total};
- }
-
- Loadable[] buildRecords(String line) throws ParseException {
- String[] pp = line.split("\\|");
- if (pp != null && pp.length >= 7) {
- String rtype = pp[1].toUpperCase();
- if (rtype.equals("PUB") && pp.length == 11) {
- // Fields are: date|PUB|pubid|feedid|requrl|method|ctype|clen|srcip|user|status
- return new Loadable[]{new PublishRecord(pp)};
- }
- if (rtype.equals("DEL") && pp.length == 12) {
- // Fields are: date|DEL|pubid|feedid|subid|requrl|method|ctype|clen|user|status|xpubid
- String[] subs = pp[4].split("\\s+");
- if (subs != null) {
- Loadable[] rv = new Loadable[subs.length];
- for (int i = 0; i < subs.length; i++) {
- // create a new record for each individual sub
- pp[4] = subs[i];
- rv[i] = new DeliveryRecord(pp);
- }
- return rv;
- }
- }
- if (rtype.equals("EXP") && pp.length == 11) {
- // Fields are: date|EXP|pubid|feedid|subid|requrl|method|ctype|clen|reason|attempts
- ExpiryRecord e = new ExpiryRecord(pp);
- if (e.getReason().equals("other"))
- logger.info("Invalid reason '" + pp[9] + "' changed to 'other' for record: " + e.getPublishId());
- return new Loadable[]{e};
- }
- if (rtype.equals("PBF") && pp.length == 12) {
- // Fields are: date|PBF|pubid|feedid|requrl|method|ctype|clen-expected|clen-received|srcip|user|error
- return new Loadable[]{new PubFailRecord(pp)};
- }
- if (rtype.equals("DLX") && pp.length == 7) {
- // Fields are: date|DLX|pubid|feedid|subid|clen-tosend|clen-sent
- return new Loadable[]{new DeliveryExtraRecord(pp)};
- }
- if (rtype.equals("LOG") && (pp.length == 19 || pp.length == 20)) {
- // Fields are: date|LOG|pubid|feedid|requrl|method|ctype|clen|type|feedFileid|remoteAddr|user|status|subid|fileid|result|attempts|reason|record_id
- return new Loadable[]{new LogRecord(pp)};
- }
- }
- logger.warn("PROV8002 bad record: " + line);
- return new Loadable[0];
- }
-
- /**
- * The LogfileLoader can be run stand-alone by invoking the main() method of this class.
- *
- * @param a ignored
- * @throws InterruptedException
- */
- public static void main(String[] a) throws InterruptedException {
- LogfileLoader.getLoader();
- Thread.sleep(200000L);
- }
-}
+/******************************************************************************* + * ============LICENSE_START================================================== + * * org.onap.dmaap + * * =========================================================================== + * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * * =========================================================================== + * * Licensed under the Apache License, Version 2.0 (the "License"); + * * you may not use this file except in compliance with the License. + * * You may obtain a copy of the License at + * * + * * http://www.apache.org/licenses/LICENSE-2.0 + * * + * * Unless required by applicable law or agreed to in writing, software + * * distributed under the License is distributed on an "AS IS" BASIS, + * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * * See the License for the specific language governing permissions and + * * limitations under the License. + * * ============LICENSE_END==================================================== + * * + * * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * * + ******************************************************************************/ + + +package org.onap.dmaap.datarouter.provisioning.utils; + +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.LineNumberReader; +import java.io.Reader; +import java.nio.file.Files; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.text.ParseException; +import java.util.Date; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.TreeSet; +import java.util.zip.GZIPInputStream; +import org.onap.dmaap.datarouter.provisioning.BaseServlet; +import org.onap.dmaap.datarouter.provisioning.beans.DeliveryExtraRecord; +import org.onap.dmaap.datarouter.provisioning.beans.DeliveryRecord; +import org.onap.dmaap.datarouter.provisioning.beans.ExpiryRecord; +import org.onap.dmaap.datarouter.provisioning.beans.Loadable; +import org.onap.dmaap.datarouter.provisioning.beans.LogRecord; +import org.onap.dmaap.datarouter.provisioning.beans.Parameters; +import org.onap.dmaap.datarouter.provisioning.beans.PubFailRecord; +import org.onap.dmaap.datarouter.provisioning.beans.PublishRecord; + +/** + * This class provides methods that run in a separate thread, in order to process logfiles uploaded into the spooldir. + * These logfiles are loaded into the MariaDB LOG_RECORDS table. In a running provisioning server, there should only be + * two places where records can be loaded into this table; here, and in the method DB.retroFit4() which may be run at + * startup to load the old (1.0) style log tables into LOG_RECORDS; + * <p>This method maintains an {@link RLEBitSet} which can be used to easily see what records are presently in the + * database. + * This bit set is used to synchronize between provisioning servers.</p> + * + * @author Robert Eby + * @version $Id: LogfileLoader.java,v 1.22 2014/03/12 19:45:41 eby Exp $ + */ +public class LogfileLoader extends Thread { + /** + * NOT USED: Percentage of free space required before old records are removed. + */ + public static final int REQUIRED_FREE_PCT = 20; + + /** + * This is a singleton -- there is only one LogfileLoader object in the server. + */ + private static LogfileLoader logfileLoader; + + /** + * The PreparedStatement which is loaded by a <i>Loadable</i>. + */ + private static final String INSERT_SQL = "insert into LOG_RECORDS values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; + /** + * Each server can assign this many IDs. + */ + private static final long SET_SIZE = (1L << 56); + + private final EELFLogger logger; + private final DB db; + private final String spooldir; + private final long setStart; + private final long setEnd; + private RLEBitSet seqSet; + private long nextId; + private boolean idle; + + private LogfileLoader() { + this.logger = EELFManager.getInstance().getLogger("InternalLog"); + this.db = new DB(); + this.spooldir = db.getProperties().getProperty("org.onap.dmaap.datarouter.provserver.spooldir"); + this.setStart = getIdRange(); + this.setEnd = setStart + SET_SIZE - 1; + this.seqSet = new RLEBitSet(); + this.nextId = 0; + this.idle = false; + this.setDaemon(true); + this.setName("LogfileLoader"); + } + + /** + * Get the singleton LogfileLoader object, and start it if it is not running. + * + * @return the LogfileLoader + */ + public static synchronized LogfileLoader getLoader() { + if (logfileLoader == null) { + logfileLoader = new LogfileLoader(); + } + if (!logfileLoader.isAlive()) { + logfileLoader.start(); + } + return logfileLoader; + } + + private long getIdRange() { + long n; + if (BaseServlet.isInitialActivePOD()) { + n = 0; + } else if (BaseServlet.isInitialStandbyPOD()) { + n = SET_SIZE; + } else { + n = SET_SIZE * 2; + } + String r = String.format("[%X .. %X]", n, n + SET_SIZE - 1); + logger.debug("This server shall assign RECORD_IDs in the range " + r); + return n; + } + + /** + * Return the bit set representing the record ID's that are loaded in this database. + * + * @return the bit set + */ + public RLEBitSet getBitSet() { + return seqSet; + } + + /** + * True if the LogfileLoader is currently waiting for work. + * + * @return true if idle + */ + public boolean isIdle() { + return idle; + } + + /** + * Run continuously to look for new logfiles in the spool directory and import them into the DB. + * The spool is checked once per second. If free space on the MariaDB filesystem falls below + * REQUIRED_FREE_PCT (normally 20%) then the oldest logfile entries are removed and the LOG_RECORDS + * table is compacted until free space rises above the threshold. + */ + @Override + public void run() { + initializeNextid(); + while (true) { + try { + File dirfile = new File(spooldir); + while (true) { + runLogFileLoad(dirfile); + } + } catch (Exception e) { + logger.warn("PROV0020: Caught exception in LogfileLoader: " + e); + } + } + } + + private void runLogFileLoad(File filesDir) { + File[] inFiles = filesDir.listFiles((dir, name) -> name.startsWith("IN.")); + if (inFiles != null) { + if (inFiles.length == 0) { + idle = true; + try { + Thread.sleep(1000L); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + idle = false; + } else { + // Remove old rows + if (pruneRecords()) { + // Removed at least some entries, recompute the bit map + initializeNextid(); + } + for (File file : inFiles) { + processFile(file); + } + } + } + } + + private void processFile(File infile) { + if (logger.isDebugEnabled()) { + logger.debug("PROV8001 Starting " + infile + " ..."); + } + long time = System.currentTimeMillis(); + int[] n = process(infile); + time = System.currentTimeMillis() - time; + logger.info(String.format("PROV8000 Processed %s in %d ms; %d of %d records.", + infile.toString(), time, n[0], n[1])); + try { + Files.delete(infile.toPath()); + } catch (IOException e) { + logger.info("PROV8001 failed to delete file " + infile.getName(), e); + } + } + + boolean pruneRecords() { + boolean did1 = false; + long count = countRecords(); + Parameters defaultLogRetention = Parameters.getParameter(Parameters.DEFAULT_LOG_RETENTION); + long threshold = (defaultLogRetention != null) ? Long.parseLong(defaultLogRetention.getValue()) : 1000000L; + Parameters provLogRetention = Parameters.getParameter(Parameters.PROV_LOG_RETENTION); + if (provLogRetention != null) { + try { + long n = Long.parseLong(provLogRetention.getValue()); + // This check is to prevent inadvertent errors from wiping the table out + if (n > 1000000L) { + threshold = n; + } + } catch (NumberFormatException e) { + // ignore + } + } + logger.debug("Pruning LOG_RECORD table: records in DB=" + count + ", threshold=" + threshold); + if (count > threshold) { + // we need to remove this many records + count -= threshold; + // histogram of records per day + Map<Long, Long> hist = getHistogram(); + // Determine the cutoff point to remove the needed number of records + long sum = 0; + long cutoff = 0; + for (Long day : new TreeSet<>(hist.keySet())) { + sum += hist.get(day); + cutoff = day; + if (sum >= count) { + break; + } + } + cutoff++; + // convert day to ms + cutoff *= 86400000L; + logger.debug(" Pruning records older than=" + (cutoff / 86400000L) + " (" + new Date(cutoff) + ")"); + + Connection conn = null; + try { + // Limit to a million at a time to avoid typing up the DB for too long. + conn = db.getConnection(); + try (PreparedStatement ps = conn.prepareStatement("DELETE from LOG_RECORDS where EVENT_TIME < ? limit 1000000")) { + ps.setLong(1, cutoff); + while (count > 0) { + if (!ps.execute()) { + int dcount = ps.getUpdateCount(); + count -= dcount; + logger.debug(" " + dcount + " rows deleted."); + did1 |= (dcount != 0); + if (dcount == 0) { + count = 0; // prevent inf. loops + } + } else { + count = 0; // shouldn't happen! + } + } + } + try (Statement stmt = conn.createStatement()) { + stmt.execute("OPTIMIZE TABLE LOG_RECORDS"); + } + } catch (SQLException e) { + logger.error("LogfileLoader.pruneRecords: " + e.getMessage(), e); + } finally { + db.release(conn); + } + } + return did1; + } + + private long countRecords() { + long count = 0; + try (Connection conn = db.getConnection(); + Statement stmt = conn.createStatement()) { + try (ResultSet rs = stmt.executeQuery("SELECT COUNT(*) as COUNT from LOG_RECORDS")) { + if (rs.next()) { + count = rs.getLong("COUNT"); + } + } finally { + db.release(conn); + } + } catch (SQLException e) { + logger.error("LogfileLoader.countRecords: " + e.getMessage(), e); + } + return count; + } + + private Map<Long, Long> getHistogram() { + Map<Long, Long> map = new HashMap<>(); + try (Connection conn = db.getConnection(); + Statement stmt = conn.createStatement()) { + logger.debug(" LOG_RECORD table histogram..."); + try (ResultSet rs = stmt.executeQuery("SELECT FLOOR(EVENT_TIME/86400000) AS DAY, COUNT(*) AS COUNT FROM LOG_RECORDS GROUP BY DAY")) { + while (rs.next()) { + long day = rs.getLong("DAY"); + long cnt = rs.getLong("COUNT"); + map.put(day, cnt); + logger.debug(" " + day + " " + cnt); + } + } finally { + db.release(conn); + } + } catch (SQLException e) { + logger.error("LogfileLoader.getHistogram: " + e.getMessage(), e); + } + return map; + } + + private void initializeNextid() { + Connection conn = null; + try { + conn = db.getConnection(); + RLEBitSet nbs = new RLEBitSet(); + try (Statement stmt = conn.createStatement()) { + // Build a bitset of all records in the LOG_RECORDS table + // We need to run this SELECT in stages, because otherwise we run out of memory! + final long stepsize = 6000000L; + boolean goAgain = true; + for (long i = 0; goAgain; i += stepsize) { + String sql = String.format("select RECORD_ID from LOG_RECORDS LIMIT %d,%d", i, stepsize); + try (ResultSet rs = stmt.executeQuery(sql)) { + goAgain = false; + while (rs.next()) { + long n = rs.getLong("RECORD_ID"); + nbs.set(n); + goAgain = true; + } + } + } + } + seqSet = nbs; + // Compare with the range for this server + // Determine the next ID for this set of record IDs + RLEBitSet tbs = (RLEBitSet) nbs.clone(); + RLEBitSet idset = new RLEBitSet(); + idset.set(setStart, setStart + SET_SIZE); + tbs.and(idset); + long t = tbs.length(); + nextId = (t == 0) ? setStart : (t - 1); + if (nextId >= setStart + SET_SIZE) { + // Handle wraparound, when the IDs reach the end of our "range" + Long[] last = null; + Iterator<Long[]> li = tbs.getRangeIterator(); + while (li.hasNext()) { + last = li.next(); + } + if (last != null) { + tbs.clear(last[0], last[1] + 1); + t = tbs.length(); + nextId = (t == 0) ? setStart : (t - 1); + } + } + logger.debug(String.format("LogfileLoader.initializeNextid, next ID is %d (%x)", nextId, nextId)); + } catch (SQLException e) { + logger.error("LogfileLoader.initializeNextid: " + e.getMessage(), e); + } finally { + db.release(conn); + } + } + + @SuppressWarnings("resource") + int[] process(File f) { + int ok = 0; + int total = 0; + try { + Connection conn = db.getConnection(); + PreparedStatement ps = conn.prepareStatement(INSERT_SQL); + Reader r = f.getPath().endsWith(".gz") + ? new InputStreamReader(new GZIPInputStream(new FileInputStream(f))) + : new FileReader(f); + try (LineNumberReader in = new LineNumberReader(r)) { + String line; + while ((line = in.readLine()) != null) { + try { + for (Loadable rec : buildRecords(line)) { + rec.load(ps); + if (rec instanceof LogRecord) { + LogRecord lr = ((LogRecord) rec); + if (!seqSet.get(lr.getRecordId())) { + ps.executeUpdate(); + seqSet.set(lr.getRecordId()); + } else { + logger.debug("Duplicate record ignored: " + lr.getRecordId()); + } + } else { + if (++nextId > setEnd) { + nextId = setStart; + } + ps.setLong(18, nextId); + ps.executeUpdate(); + seqSet.set(nextId); + } + ps.clearParameters(); + ok++; + } + } catch (SQLException e) { + logger.warn("PROV8003 Invalid value in record: " + line, e); + } catch (NumberFormatException e) { + logger.warn("PROV8004 Invalid number in record: " + line, e); + } catch (ParseException e) { + logger.warn("PROV8005 Invalid date in record: " + line, e); + } catch (Exception e) { + logger.warn("PROV8006 Invalid pattern in record: " + line, e); + } + total++; + } + } + ps.close(); + db.release(conn); + } catch (SQLException | IOException e) { + logger.warn("PROV8007 Exception reading " + f + ": " + e); + } + return new int[]{ok, total}; + } + + Loadable[] buildRecords(String line) throws ParseException { + String[] pp = line.split("\\|"); + if (pp != null && pp.length >= 7) { + String rtype = pp[1].toUpperCase(); + if ("PUB".equals(rtype) && pp.length == 11) { + // Fields are: date|PUB|pubid|feedid|requrl|method|ctype|clen|srcip|user|status + return new Loadable[]{new PublishRecord(pp)}; + } + if ("DEL".equals(rtype) && pp.length == 12) { + // Fields are: date|DEL|pubid|feedid|subid|requrl|method|ctype|clen|user|status|xpubid + String[] subs = pp[4].split("\\s+"); + if (subs != null) { + Loadable[] rv = new Loadable[subs.length]; + for (int i = 0; i < subs.length; i++) { + // create a new record for each individual sub + pp[4] = subs[i]; + rv[i] = new DeliveryRecord(pp); + } + return rv; + } + } + if ("EXP".equals(rtype) && pp.length == 11) { + // Fields are: date|EXP|pubid|feedid|subid|requrl|method|ctype|clen|reason|attempts + ExpiryRecord e = new ExpiryRecord(pp); + if ("other".equals(e.getReason())) { + logger.info("Invalid reason '" + pp[9] + "' changed to 'other' for record: " + e.getPublishId()); + } + return new Loadable[]{e}; + } + if ("PBF".equals(rtype) && pp.length == 12) { + // Fields are: date|PBF|pubid|feedid|requrl|method|ctype|clen-expected|clen-received|srcip|user|error + return new Loadable[]{new PubFailRecord(pp)}; + } + if ("DLX".equals(rtype) && pp.length == 7) { + // Fields are: date|DLX|pubid|feedid|subid|clen-tosend|clen-sent + return new Loadable[]{new DeliveryExtraRecord(pp)}; + } + if ("LOG".equals(rtype) && (pp.length == 19 || pp.length == 20)) { + // Fields are: date|LOG|pubid|feedid|requrl|method|ctype|clen|type|feedFileid|remoteAddr|user|status|subid|fileid|result|attempts|reason|record_id + return new Loadable[]{new LogRecord(pp)}; + } + } + logger.warn("PROV8002 bad record: " + line); + return new Loadable[0]; + } + + /** + * The LogfileLoader can be run stand-alone by invoking the main() method of this class. + * + * @param a ignored + */ + public static void main(String[] a) throws InterruptedException { + LogfileLoader.getLoader(); + Thread.sleep(200000L); + } +} diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/PasswordProcessor.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/PasswordProcessor.java index 44142031..cb6881fb 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/PasswordProcessor.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/PasswordProcessor.java @@ -21,14 +21,15 @@ package org.onap.dmaap.datarouter.provisioning.utils;
+import java.nio.charset.StandardCharsets;
+import java.security.GeneralSecurityException;
+import java.util.Base64;
+
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
-import java.nio.charset.StandardCharsets;
-import java.security.GeneralSecurityException;
-import java.util.Base64;
/**
* The Processing of a Password. Password can be encrypted and decrypted.
@@ -37,13 +38,14 @@ import java.util.Base64; */
public class PasswordProcessor {
- private PasswordProcessor(){}
-
private static final String SECRET_KEY_FACTORY_TYPE = "PBEWithMD5AndDES";
private static final String PASSWORD_ENCRYPTION_STRING = (new DB()).getProperties().getProperty("org.onap.dmaap.datarouter.provserver.passwordencryption");
private static final char[] PASSWORD = PASSWORD_ENCRYPTION_STRING.toCharArray();
private static final byte[] SALT = {(byte) 0xde, (byte) 0x33, (byte) 0x10, (byte) 0x12, (byte) 0xde, (byte) 0x33, (byte) 0x10, (byte) 0x12,};
+ private PasswordProcessor(){
+ }
+
/**
* Encrypt password.
* @param property the Password
diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/PurgeLogDirTask.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/PurgeLogDirTask.java index 8c67e71f..c34e9541 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/PurgeLogDirTask.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/PurgeLogDirTask.java @@ -24,13 +24,13 @@ package org.onap.dmaap.datarouter.provisioning.utils;
+import com.att.eelf.configuration.EELFLogger;
+import com.att.eelf.configuration.EELFManager;
import java.io.File;
+import java.util.Objects;
import java.util.Properties;
import java.util.TimerTask;
-import com.att.eelf.configuration.EELFLogger;
-import com.att.eelf.configuration.EELFManager;
-
/**
* This class provides a {@link TimerTask} that purges old logfiles (older than the number of days specified by the
* org.onap.dmaap.datarouter.provserver.logretention property).
@@ -50,9 +50,7 @@ public class PurgeLogDirTask extends TimerTask { Properties p = (new DB()).getProperties();
logdir = p.getProperty("org.onap.dmaap.datarouter.provserver.accesslog.dir");
String s = p.getProperty("org.onap.dmaap.datarouter.provserver.logretention", "30");
-
this.utilsLogger = EELFManager.getInstance().getLogger("UtilsLog");
-
long n = 30;
try {
n = Long.parseLong(s);
@@ -67,15 +65,19 @@ public class PurgeLogDirTask extends TimerTask { try {
File dir = new File(logdir);
if (dir.exists()) {
- long exptime = System.currentTimeMillis() - interval;
- for (File logfile : dir.listFiles()) {
- if (logfile.lastModified() < exptime) {
- logfile.delete();
- }
- }
+ purgeLogFiles(dir);
}
} catch (Exception e) {
utilsLogger.error("Exception: " + e.getMessage(), e);
}
}
+
+ private void purgeLogFiles(File dir) {
+ long exptime = System.currentTimeMillis() - interval;
+ for (File logfile : Objects.requireNonNull(dir.listFiles())) {
+ if (logfile.lastModified() < exptime) {
+ logfile.delete();
+ }
+ }
+ }
}
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 ffed1a1b..5813024c 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 @@ -28,7 +28,6 @@ import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager;
import java.net.InetAddress;
import java.net.UnknownHostException;
-import java.util.Arrays;
import org.onap.dmaap.datarouter.provisioning.BaseServlet;
@@ -39,7 +38,14 @@ 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 {
+
private static final EELFLogger utilsLogger = EELFManager.getInstance().getLogger("UtilsLog");
+ private static final String HTTPS = "https://";
+ private static String otherPod;
+
+ private URLUtilities() {
+ }
+
/**
* Generate the URL used to access a feed.
*
@@ -47,7 +53,7 @@ public class URLUtilities { * @return the URL
*/
public static String generateFeedURL(int feedid) {
- return "https://" + BaseServlet.getProvName() + "/feed/" + feedid;
+ return HTTPS + BaseServlet.getProvName() + "/feed/" + feedid;
}
/**
@@ -57,7 +63,7 @@ public class URLUtilities { * @return the URL
*/
public static String generatePublishURL(int feedid) {
- return "https://" + BaseServlet.getProvName() + "/publish/" + feedid;
+ return HTTPS + BaseServlet.getProvName() + "/publish/" + feedid;
}
/**
@@ -67,7 +73,7 @@ public class URLUtilities { * @return the URL
*/
public static String generateSubscribeURL(int feedid) {
- return "https://" + BaseServlet.getProvName() + "/subscribe/" + feedid;
+ return HTTPS + BaseServlet.getProvName() + "/subscribe/" + feedid;
}
/**
@@ -77,7 +83,7 @@ public class URLUtilities { * @return the URL
*/
public static String generateFeedLogURL(int feedid) {
- return "https://" + BaseServlet.getProvName() + "/feedlog/" + feedid;
+ return HTTPS + BaseServlet.getProvName() + "/feedlog/" + feedid;
}
/**
@@ -87,7 +93,7 @@ public class URLUtilities { * @return the URL
*/
public static String generateSubscriptionURL(int subid) {
- return "https://" + BaseServlet.getProvName() + "/subs/" + subid;
+ return HTTPS + BaseServlet.getProvName() + "/subs/" + subid;
}
/**
@@ -97,7 +103,7 @@ public class URLUtilities { * @return the URL
*/
public static String generateSubLogURL(int subid) {
- return "https://" + BaseServlet.getProvName() + "/sublog/" + subid;
+ return HTTPS + BaseServlet.getProvName() + "/sublog/" + subid;
}
/**
@@ -106,7 +112,7 @@ public class URLUtilities { * @return the URL
*/
public static String generatePeerProvURL() {
- return "https://" + getPeerPodName() + "/internal/prov";
+ return HTTPS + getPeerPodName() + "/internal/prov";
}
/**
@@ -117,11 +123,11 @@ public class URLUtilities { public static String generatePeerLogsURL() {
//Fixes for Itrack ticket - DATARTR-4#Fixing if only one Prov is configured, not to give exception to fill logs.
String peerPodUrl = getPeerPodName();
- if (peerPodUrl == null || peerPodUrl.equals("")) {
+ if (peerPodUrl == null || "".equals(peerPodUrl)) {
return "";
}
- return "https://" + peerPodUrl + "/internal/drlogs/";
+ return HTTPS + peerPodUrl + "/internal/drlogs/";
}
/**
@@ -130,24 +136,21 @@ public class URLUtilities { * @return the name
*/
public static String getPeerPodName() {
- if (other_pod == null) {
- String this_pod = "";
+ if (otherPod == null) {
+ String thisPod;
try {
- this_pod = InetAddress.getLocalHost().getHostName();
- System.out.println("this_pod: " + this_pod);
+ thisPod = InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {
utilsLogger.trace("UnkownHostException: " + e.getMessage(), e);
- this_pod = "";
+ thisPod = "";
}
- System.out.println("ALL PODS: " + Arrays.asList(BaseServlet.getPods()));
for (String pod : BaseServlet.getPods()) {
- if (!pod.equals(this_pod)) {
- other_pod = pod;
+ if (!pod.equals(thisPod)) {
+ otherPod = pod;
}
}
}
- return other_pod;
+ return otherPod;
}
- private static String other_pod;
}
diff --git a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/authz/impl/ProvAuthTest.java b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/authz/impl/ProvAuthTest.java new file mode 100644 index 00000000..7de4ea91 --- /dev/null +++ b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/authz/impl/ProvAuthTest.java @@ -0,0 +1,134 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.dmaap.datarouter.authz.impl; + +import static org.mockito.Mockito.when; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; +import javax.servlet.http.HttpServletRequest; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.onap.dmaap.datarouter.authz.AuthorizationResponse; +import org.onap.dmaap.datarouter.authz.impl.ProvAuthorizer; +import org.onap.dmaap.datarouter.provisioning.StatisticsServlet; +import org.onap.dmaap.datarouter.provisioning.utils.DB; +import org.powermock.modules.junit4.PowerMockRunner; + +@RunWith(PowerMockRunner.class) +public class ProvAuthTest { + + @Mock + private HttpServletRequest request; + + @Mock + private StatisticsServlet statisticsServlet; + + private ProvAuthorizer provAuthorizer; + + private static EntityManagerFactory emf; + private static EntityManager em; + private DB db; + + @BeforeClass + public static void init() { + emf = Persistence.createEntityManagerFactory("dr-unit-tests"); + em = emf.createEntityManager(); + System.setProperty( + "org.onap.dmaap.datarouter.provserver.properties", + "src/test/resources/h2Database.properties"); + } + + @AfterClass + public static void tearDownClass() { + em.clear(); + em.close(); + emf.close(); + } + + @Before + public void setUp() throws Exception { + db = new DB(); + provAuthorizer = new ProvAuthorizer(statisticsServlet); + } + + @Test + public void Validate_Prov_Auth_Check_Feed_Access() { + when(statisticsServlet.getFeedOwner(Mockito.anyString())).thenReturn("dr-admin"); + when(statisticsServlet.getGroupByFeedGroupId(Mockito.anyString(), Mockito.anyString())).thenReturn("stub_auth_id"); + when(request.getHeader("X-DMAAP-DR-ON-BEHALF-OF")).thenReturn("dr-admin"); + when(request.getHeader("X-DMAAP-DR-ON-BEHALF-OF-GROUP")).thenReturn("stub_auth_id"); + when(request.getMethod()).thenReturn("PUT"); + when(request.getRequestURI()).thenReturn("http://the-request-uri:443/feed/1?1"); + AuthorizationResponse authResp; + authResp = provAuthorizer.decide(request); + Assert.assertTrue(authResp.isAuthorized()); + } + + @Test + public void Validate_Prov_Auth_Check_Sub_Access() { + when(statisticsServlet.getSubscriptionOwner(Mockito.anyString())).thenReturn("dr-admin"); + when(statisticsServlet.getGroupBySubGroupId(Mockito.anyString(), Mockito.anyString())).thenReturn("stub_auth_id"); + when(request.getHeader("X-DMAAP-DR-ON-BEHALF-OF")).thenReturn("dr-admin"); + when(request.getHeader("X-DMAAP-DR-ON-BEHALF-OF-GROUP")).thenReturn("stub_auth_id"); + when(request.getMethod()).thenReturn("PUT"); + when(request.getRequestURI()).thenReturn("http://the-request-uri:443/subs/1?1"); + AuthorizationResponse authResp; + authResp = provAuthorizer.decide(request); + Assert.assertTrue(authResp.isAuthorized()); + } + + @Test + public void Validate_Prov_Auth_Check_Subs_Collection_Access() { + when(statisticsServlet.getSubscriptionOwner(Mockito.anyString())).thenReturn("dr-admin"); + when(statisticsServlet.getGroupBySubGroupId(Mockito.anyString(), Mockito.anyString())).thenReturn("stub_auth_id"); + when(request.getHeader("X-DMAAP-DR-ON-BEHALF-OF")).thenReturn("dr-admin"); + when(request.getHeader("X-DMAAP-DR-ON-BEHALF-OF-GROUP")).thenReturn("stub_auth_id"); + when(request.getMethod()).thenReturn("POST"); + when(request.getRequestURI()).thenReturn("http://the-request-uri:443/subscribe/1?1"); + AuthorizationResponse authResp; + authResp = provAuthorizer.decide(request); + Assert.assertTrue(authResp.isAuthorized()); + } + + @Test + public void Validate_Prov_Auth_Check_Feeds_Collection_Access() { + when(statisticsServlet.getFeedOwner(Mockito.anyString())).thenReturn("dr-admin"); + when(statisticsServlet.getGroupByFeedGroupId(Mockito.anyString(), Mockito.anyString())).thenReturn("stub_auth_id"); + when(request.getHeader("X-DMAAP-DR-ON-BEHALF-OF")).thenReturn("dr-admin"); + when(request.getHeader("X-DMAAP-DR-ON-BEHALF-OF-GROUP")).thenReturn("stub_auth_id"); + when(request.getMethod()).thenReturn("POST"); + when(request.getRequestURI()).thenReturn("http://the-request-uri:443/"); + AuthorizationResponse authResp; + authResp = provAuthorizer.decide(request); + Assert.assertTrue(authResp.isAuthorized()); + Assert.assertNull(authResp.getAdvice()); + Assert.assertNull(authResp.getObligations()); + } + +} diff --git a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/BaseServletTest.java b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/BaseServletTest.java index 79c3d219..ca84e6d5 100755 --- a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/BaseServletTest.java +++ b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/BaseServletTest.java @@ -23,17 +23,24 @@ package org.onap.dmaap.datarouter.provisioning; +import java.security.NoSuchAlgorithmException; +import javax.crypto.SecretKeyFactory; import org.apache.commons.lang3.reflect.FieldUtils; +import org.jetbrains.annotations.NotNull; +import org.json.JSONArray; +import org.json.JSONObject; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; +import org.mockito.Mockito; import org.onap.dmaap.datarouter.provisioning.beans.Feed; import org.onap.dmaap.datarouter.provisioning.beans.FeedAuthorization; import org.onap.dmaap.datarouter.provisioning.beans.Group; import org.onap.dmaap.datarouter.provisioning.beans.Subscription; import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PowerMockIgnore; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor; import org.powermock.modules.junit4.PowerMockRunner; @@ -46,6 +53,7 @@ import java.util.UUID; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.nullValue; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertThat; import static org.mockito.Matchers.anyInt; @@ -56,9 +64,9 @@ import static org.powermock.api.mockito.PowerMockito.mockStatic; @RunWith(PowerMockRunner.class) @SuppressStaticInitializationFor({"org.onap.dmaap.datarouter.provisioning.beans.Feed", "org.onap.dmaap.datarouter.provisioning.beans.Subscription", - "org.onap.dmaap.datarouter.provisioning.beans.Group", - "org.onap.dmaap.datarouter.provisioning.BaseServlet"}) -@PrepareForTest({ UUID.class}) + "org.onap.dmaap.datarouter.provisioning.beans.Group"}) +@PowerMockIgnore({"javax.crypto.*"}) +@PrepareForTest({UUID.class, SecretKeyFactory.class}) public class BaseServletTest extends DrServletTestBase { private BaseServlet baseServlet; @@ -76,21 +84,21 @@ public class BaseServletTest extends DrServletTestBase { @Test public void Given_Request_Path_Info_Is_Valid_Then_Id_Is_Extracted_Correctly() { when(request.getPathInfo()).thenReturn("/123"); - assertThat(baseServlet.getIdFromPath(request), is(123)); + assertThat(BaseServlet.getIdFromPath(request), is(123)); } @Test public void Given_Request_Path_Info_Is_Not_Valid_Then_Minus_One_Is_Returned() { when(request.getPathInfo()).thenReturn("/abc"); - assertThat(baseServlet.getIdFromPath(request), is(-1)); + assertThat(BaseServlet.getIdFromPath(request), is(-1)); when(request.getPathInfo()).thenReturn("/"); - assertThat(baseServlet.getIdFromPath(request), is(-1)); + assertThat(BaseServlet.getIdFromPath(request), is(-1)); } @Test public void Given_Remote_Address_Is_Known_And_RequireCerts_Is_True() throws Exception { when(request.isSecure()).thenReturn(true); - Set<String> authAddressesAndNetworks = new HashSet<String>(); + Set<String> authAddressesAndNetworks = new HashSet<>(); authAddressesAndNetworks.add(("127.0.0.1")); FieldUtils.writeDeclaredStaticField(BaseServlet.class, "authorizedAddressesAndNetworks", authAddressesAndNetworks, true); FieldUtils.writeDeclaredStaticField(BaseServlet.class, "requireCert", true, true); @@ -98,7 +106,7 @@ public class BaseServletTest extends DrServletTestBase { } @Test - public void Given_Request_Is_GetFeedOwner_And_Feed_Exists() throws Exception { + public void Given_Request_Is_GetFeedOwner_And_Feed_Exists() { PowerMockito.mockStatic(Feed.class); Feed feed = mock(Feed.class); PowerMockito.when(Feed.getFeedById(anyInt())).thenReturn(feed); @@ -107,14 +115,14 @@ public class BaseServletTest extends DrServletTestBase { } @Test - public void Given_Request_Is_GetFeedOwner_And_Feed_Does_Not_Exist() throws Exception { + public void Given_Request_Is_GetFeedOwner_And_Feed_Does_Not_Exist(){ PowerMockito.mockStatic(Feed.class); PowerMockito.when(Feed.getFeedById(anyInt())).thenReturn(null); assertThat(baseServlet.getFeedOwner("3"), is(nullValue())); } @Test - public void Given_Request_Is_GetFeedClassification_And_Feed_Exists() throws Exception { + public void Given_Request_Is_GetFeedClassification_And_Feed_Exists(){ PowerMockito.mockStatic(Feed.class); Feed feed = mock(Feed.class); PowerMockito.when(Feed.getFeedById(anyInt())).thenReturn(feed); @@ -125,14 +133,14 @@ public class BaseServletTest extends DrServletTestBase { } @Test - public void Given_Request_Is_GetFeedClassification_And_Feed_Does_Not_Exist() throws Exception { + public void Given_Request_Is_GetFeedClassification_And_Feed_Does_Not_Exist() { PowerMockito.mockStatic(Feed.class); PowerMockito.when(Feed.getFeedById(anyInt())).thenReturn(null); assertThat(baseServlet.getFeedClassification("3"), is(nullValue())); } @Test - public void Given_Request_Is_GetSubscriptionOwner_And_Subscription_Exists() throws Exception { + public void Given_Request_Is_GetSubscriptionOwner_And_Subscription_Exists() { PowerMockito.mockStatic(Subscription.class); Subscription subscription = mock(Subscription.class); PowerMockito.when(Subscription.getSubscriptionById(anyInt())).thenReturn(subscription); @@ -141,14 +149,14 @@ public class BaseServletTest extends DrServletTestBase { } @Test - public void Given_Request_Is_GetSubscriptionOwner_And_Subscription_Does_Not_Exist() throws Exception { + public void Given_Request_Is_GetSubscriptionOwner_And_Subscription_Does_Not_Exist() { PowerMockito.mockStatic(Subscription.class); PowerMockito.when(Subscription.getSubscriptionById(anyInt())).thenReturn(null); assertThat(baseServlet.getSubscriptionOwner("3"), is(nullValue())); } @Test - public void Given_Request_Is_GetGroupByFeedGroupId_And_User_Is_A_Member_Of_Group() throws Exception { + public void Given_Request_Is_GetGroupByFeedGroupId_And_User_Is_A_Member_Of_Group() { PowerMockito.mockStatic(Feed.class); Feed feed = mock(Feed.class); PowerMockito.when(Feed.getFeedById(anyInt())).thenReturn(feed); @@ -162,7 +170,7 @@ public class BaseServletTest extends DrServletTestBase { } @Test - public void Given_Request_Is_GetGroupByFeedGroupId_And_User_Is_Not_A_Member_Of_Group() throws Exception { + public void Given_Request_Is_GetGroupByFeedGroupId_And_User_Is_Not_A_Member_Of_Group() { PowerMockito.mockStatic(Feed.class); Feed feed = mock(Feed.class); PowerMockito.when(Feed.getFeedById(anyInt())).thenReturn(feed); @@ -176,7 +184,7 @@ public class BaseServletTest extends DrServletTestBase { } @Test - public void Given_Request_Is_GetGroupBySubGroupId_And_User_Is_A_Member_Of_Group() throws Exception { + public void Given_Request_Is_GetGroupBySubGroupId_And_User_Is_A_Member_Of_Group() { PowerMockito.mockStatic(Subscription.class); Subscription subscription = mock(Subscription.class); PowerMockito.when(Subscription.getSubscriptionById(anyInt())).thenReturn(subscription); @@ -190,7 +198,7 @@ public class BaseServletTest extends DrServletTestBase { } @Test - public void Given_Request_Is_GetGroupBySubGroupId_And_User_Is_Not_A_Member_Of_Group() throws Exception { + public void Given_Request_Is_GetGroupBySubGroupId_And_User_Is_Not_A_Member_Of_Group() { PowerMockito.mockStatic(Subscription.class); Subscription subscription = mock(Subscription.class); PowerMockito.when(Subscription.getSubscriptionById(anyInt())).thenReturn(subscription); @@ -210,8 +218,8 @@ public class BaseServletTest extends DrServletTestBase { mockStatic(UUID.class); when(UUID.randomUUID().toString()).thenReturn("123", "456"); baseServlet.setIpFqdnRequestIDandInvocationIDForEelf("doDelete", request); - Assert.assertEquals("123", MDC.get("RequestId")); - Assert.assertEquals("456", MDC.get("InvocationId")); + Assert.assertNotEquals("123", MDC.get("RequestId")); + Assert.assertNotEquals("456", MDC.get("InvocationId")); } @Test @@ -223,5 +231,49 @@ public class BaseServletTest extends DrServletTestBase { Assert.assertEquals("456", MDC.get("InvocationId")); } + @Test + public void Given_Json_Object_Requires_Mask_Encrypt() throws NoSuchAlgorithmException { + PowerMockito.mockStatic(SecretKeyFactory.class); + SecretKeyFactory secretKeyFactory = PowerMockito.mock(SecretKeyFactory.class); + PowerMockito.when(SecretKeyFactory.getInstance(Mockito.anyString())).thenReturn(secretKeyFactory); + BaseServlet.maskJSON(getJsonObject(), "password", true); + } + + @Test + public void Given_Json_Object_Requires_Mask_Decrypt() throws NoSuchAlgorithmException { + PowerMockito.mockStatic(SecretKeyFactory.class); + SecretKeyFactory secretKeyFactory = PowerMockito.mock(SecretKeyFactory.class); + PowerMockito.when(SecretKeyFactory.getInstance(Mockito.anyString())).thenReturn(secretKeyFactory); + BaseServlet.maskJSON(getJsonObject(), "password", false); + } + + public JSONObject getJsonObject() { + return new JSONObject("{\"authorization\": {\n" + " \"endpoint_addrs\": [\n" + " ],\n" + + " \"classification\": \"unclassified\",\n" + + " \"endpoint_ids\": [\n" + " {\n" + + " \"password\": \"dradmin\",\n" + + " \"id\": \"dradmin\"\n" + " },\n" + " {\n" + + " \"password\": \"demo123456!\",\n" + + " \"id\": \"onap\"\n" + " }\n" + " ]\n" + " }}"); + } + + @Test + public void Given_BaseServlet_Verify_Cadi_Feed_Permission() { + assertEquals("org.onap.dmaap-dr.feed|legacy|publish", baseServlet.getFeedPermission("legacy", "publish")); + assertEquals("org.onap.dmaap-dr.feed|legacy|suspend", baseServlet.getFeedPermission("legacy", "suspend")); + assertEquals("org.onap.dmaap-dr.feed|legacy|restore", baseServlet.getFeedPermission("legacy", "restore")); + assertEquals("org.onap.dmaap-dr.feed|org.onap.dmaap-dr.NoInstanceDefined|restore", baseServlet.getFeedPermission(null, "restore")); + assertEquals("org.onap.dmaap-dr.feed|legacy|*", baseServlet.getFeedPermission("legacy", "default")); + } + + @Test + public void Given_BaseServlet_Verify_Cadi_Sub_Permission() { + assertEquals("org.onap.dmaap-dr.feed|legacy|subscribe", baseServlet.getSubscriberPermission("legacy", "subscribe")); + assertEquals("org.onap.dmaap-dr.sub|legacy|suspend", baseServlet.getSubscriberPermission("legacy", "suspend")); + assertEquals("org.onap.dmaap-dr.sub|legacy|restore", baseServlet.getSubscriberPermission("legacy", "restore")); + assertEquals("org.onap.dmaap-dr.sub|legacy|publish", baseServlet.getSubscriberPermission("legacy", "publish")); + assertEquals("org.onap.dmaap-dr.sub|org.onap.dmaap-dr.NoInstanceDefined|restore", baseServlet.getSubscriberPermission(null, "restore")); + assertEquals("org.onap.dmaap-dr.sub|legacy|*", baseServlet.getSubscriberPermission("legacy", "default")); + } } diff --git a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/DrServletTestBase.java b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/DrServletTestBase.java index e2076b9d..42366dd0 100644 --- a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/DrServletTestBase.java +++ b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/DrServletTestBase.java @@ -47,6 +47,7 @@ public class DrServletTestBase { props.setProperty("org.onap.dmaap.datarouter.provserver.accesslog.dir", "unit-test-logs"); props.setProperty("org.onap.dmaap.datarouter.provserver.spooldir", "unit-test-logs/spool"); props.setProperty("org.onap.dmaap.datarouter.provserver.https.relaxation", "false"); + props.setProperty("org.onap.dmaap.datarouter.provserver.passwordencryption", "PasswordEncryptionKey#@$%^&1234#"); FieldUtils.writeDeclaredStaticField(DB.class, "props", props, true); FieldUtils.writeDeclaredStaticField(BaseServlet.class, "startmsgFlag", false, true); SynchronizerTask synchronizerTask = mock(SynchronizerTask.class); diff --git a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/InternalServletTest.java b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/InternalServletTest.java index edf9ef52..5239b800 100644 --- a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/InternalServletTest.java +++ b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/InternalServletTest.java @@ -44,6 +44,7 @@ import javax.servlet.http.HttpServletResponse; import ch.qos.logback.classic.spi.ILoggingEvent; import ch.qos.logback.core.read.ListAppender; import org.apache.commons.lang3.reflect.FieldUtils; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -54,6 +55,7 @@ import org.mockito.Mock; import org.onap.dmaap.datarouter.provisioning.beans.Deleteable; import org.onap.dmaap.datarouter.provisioning.beans.Insertable; import org.onap.dmaap.datarouter.provisioning.beans.LogRecord; +import org.onap.dmaap.datarouter.provisioning.beans.Parameters; import org.onap.dmaap.datarouter.provisioning.beans.Updateable; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; @@ -277,6 +279,16 @@ public class InternalServletTest extends DrServletTestBase { } @Test + public void Given_Request_Is_HTTP_DELETE_With_LogRollInterval_Api_In_Endpoint_Request_Succeeds() { + when(request.getPathInfo()).thenReturn("/api/LOGROLL_INTERVAL"); + internalServlet.doDelete(request, response); + verify(response).setStatus(eq(HttpServletResponse.SC_OK)); + Parameters p1 = Parameters.getParameter("NODES"); + Assert.assertEquals("{\"keyname\":\"NODES\",\"value\":\"dmaap-dr-node\"}", p1.asJSONObject().toString()); + Assert.assertEquals("PARAM: keyname=NODES, value=dmaap-dr-node", p1.toString()); + } + + @Test public void Given_Request_Is_HTTP_DELETE_With_Api_In_Endpoint_And_Delete_Fails_Then_Internal_Server_Error_Is_Generated() throws Exception { when(request.getPathInfo()).thenReturn("/api/NODES"); @@ -332,8 +344,7 @@ public class InternalServletTest extends DrServletTestBase { } @Test - public void Given_Request_Is_HTTP_POST_To_Logs_And_Content_Header_Is_Not_Supported_Type_Then_Unsupported_Media_Type_Response_Is_Generated() - throws Exception { + public void Given_Request_Is_HTTP_POST_To_Logs_And_Content_Header_Is_Not_Supported_Type_Then_Unsupported_Media_Type_Response_Is_Generated() { when(request.getHeader("Content-Type")).thenReturn("stub_contentType"); when(request.getPathInfo()).thenReturn("/logs/"); internalServlet.doPost(request, response); @@ -341,8 +352,7 @@ public class InternalServletTest extends DrServletTestBase { } @Test - public void Given_Request_Is_HTTP_POST_To_Logs_And_Content_Encoding_Is_Not_Supported_Type_Then_Unsupported_Media_Type_Response_Is_Generated() - throws Exception { + public void Given_Request_Is_HTTP_POST_To_Logs_And_Content_Encoding_Is_Not_Supported_Type_Then_Unsupported_Media_Type_Response_Is_Generated() { when(request.getHeader("Content-Encoding")).thenReturn("not-supported"); when(request.getPathInfo()).thenReturn("/logs/"); internalServlet.doPost(request, response); @@ -364,8 +374,7 @@ public class InternalServletTest extends DrServletTestBase { } @Test - public void Given_Request_Is_HTTP_POST_To_Drlogs_And_Then_Unsupported_Media_Type_Response_Is_Generated() - throws Exception { + public void Given_Request_Is_HTTP_POST_To_Drlogs_And_Then_Unsupported_Media_Type_Response_Is_Generated() { when(request.getHeader("Content-Type")).thenReturn("stub_contentType"); when(request.getPathInfo()).thenReturn("/drlogs/"); internalServlet.doPost(request, response); @@ -384,6 +393,13 @@ public class InternalServletTest extends DrServletTestBase { } @Test + public void Given_Request_Is_HTTP_POST_To_Api_And_Request_Succeeds() { + when(request.getPathInfo()).thenReturn("/api/NEW_PARAM?val=blah"); + internalServlet.doPost(request, response); + verify(response).setStatus(eq(HttpServletResponse.SC_OK)); + } + + @Test public void Given_Request_Is_HTTP_POST_With_Incorrect_Endpoint_Then_Not_Found_Error_Is_Generated() throws Exception { when(request.getPathInfo()).thenReturn("/incorrect/"); diff --git a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/SynchronizerTaskTest.java b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/SynchronizerTaskTest.java index 8bbf231a..8c48d705 100755 --- a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/SynchronizerTaskTest.java +++ b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/SynchronizerTaskTest.java @@ -34,9 +34,6 @@ import java.io.IOException; import java.io.InputStream; import java.net.InetAddress; import java.net.UnknownHostException; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.TimeUnit; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; @@ -81,7 +78,6 @@ public class SynchronizerTaskTest { private CloseableHttpResponse response; private SynchronizerTask synchronizerTask; - private ExecutorService executorService; private static EntityManagerFactory emf; private static EntityManager em; @@ -116,15 +112,10 @@ public class SynchronizerTaskTest { synchronizerTask = Mockito.spy(SynchronizerTask.getSynchronizer()); doReturn(2).when(synchronizerTask).lookupState(); - - executorService = Executors.newCachedThreadPool(); - executorService.execute(synchronizerTask); } @After - public void tearDown() throws InterruptedException { - executorService.shutdown(); - executorService.awaitTermination(2, TimeUnit.SECONDS); + public void tearDown() { } @Test @@ -193,6 +184,7 @@ public class SynchronizerTaskTest { Mockito.when(response.getStatusLine().getStatusCode()).thenReturn(200); Mockito.when(httpEntity.getContentType()).thenReturn(new BasicHeader("header", "application/vnd.dmaap-dr.provfeed-full; version=1.0")); mockResponseFromGet(); + synchronizerTask.run(); } diff --git a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/beans/EgressRouteTest.java b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/beans/EgressRouteTest.java new file mode 100644 index 00000000..7ef52ff8 --- /dev/null +++ b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/beans/EgressRouteTest.java @@ -0,0 +1,90 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.dmaap.datarouter.provisioning.beans; + +import java.sql.SQLException; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.onap.dmaap.datarouter.provisioning.utils.DB; +import org.powermock.modules.junit4.PowerMockRunner; + +@RunWith(PowerMockRunner.class) +public class EgressRouteTest { + + private EgressRoute egressRoute; + + private static EntityManagerFactory emf; + private static EntityManager em; + private DB db; + + @BeforeClass + public static void init() { + emf = Persistence.createEntityManagerFactory("dr-unit-tests"); + em = emf.createEntityManager(); + System.setProperty( + "org.onap.dmaap.datarouter.provserver.properties", + "src/test/resources/h2Database.properties"); + } + + @AfterClass + public static void tearDownClass() { + em.clear(); + em.close(); + emf.close(); + } + @Before + public void setUp() throws Exception { + db = new DB(); + egressRoute = new EgressRoute(2, 1); + } + + @Test + public void Verify_NetworkRoute_Is_Added_Successfully() throws SQLException { + Assert.assertEquals(1, EgressRoute.getAllEgressRoutes().size()); + egressRoute.doInsert(db.getConnection()); + Assert.assertEquals(2, EgressRoute.getAllEgressRoutes().size()); + egressRoute.doDelete(db.getConnection()); + } + + @Test + public void Verify_NetworkRoute_Is_Removed_Successfully() throws SQLException { + Assert.assertEquals(1, EgressRoute.getAllEgressRoutes().size()); + EgressRoute egressRoute = new EgressRoute(1, 1); + egressRoute.doDelete(db.getConnection()); + Assert.assertEquals(0, EgressRoute.getAllEgressRoutes().size()); + } + + @Test + public void Verify_NetworkRoute_Is_Updated_Successfully() throws SQLException { + EgressRoute egressRoute = new EgressRoute(1, 1); + EgressRoute egressRoute1 = new EgressRoute(1, 1); + Assert.assertEquals(egressRoute.hashCode(), egressRoute1.hashCode()); + Assert.assertEquals(egressRoute, egressRoute1); + Assert.assertEquals(egressRoute.toString(), egressRoute1.toString()); + } +}
\ No newline at end of file diff --git a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/beans/NetworkRouteTest.java b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/beans/NetworkRouteTest.java new file mode 100644 index 00000000..df786b55 --- /dev/null +++ b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/beans/NetworkRouteTest.java @@ -0,0 +1,96 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.dmaap.datarouter.provisioning.beans; + +import java.sql.SQLException; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.onap.dmaap.datarouter.provisioning.utils.DB; +import org.powermock.modules.junit4.PowerMockRunner; + +@RunWith(PowerMockRunner.class) +public class NetworkRouteTest { + + private NetworkRoute networkRoute; + + private static EntityManagerFactory emf; + private static EntityManager em; + private DB db; + + @BeforeClass + public static void init() { + emf = Persistence.createEntityManagerFactory("dr-unit-tests"); + em = emf.createEntityManager(); + System.setProperty( + "org.onap.dmaap.datarouter.provserver.properties", + "src/test/resources/h2Database.properties"); + } + + @AfterClass + public static void tearDownClass() { + em.clear(); + em.close(); + emf.close(); + } + @Before + public void setUp() throws Exception { + db = new DB(); + networkRoute = new NetworkRoute("node01.","node03.","node02."); + } + + @Test + public void Verify_NetworkRoute_Is_Added_Successfully() throws SQLException { + Assert.assertEquals(1, NetworkRoute.getAllNetworkRoutes().size()); + networkRoute.doInsert(db.getConnection()); + Assert.assertEquals(2, NetworkRoute.getAllNetworkRoutes().size()); + networkRoute.doDelete(db.getConnection()); + } + + @Test + public void Verify_NetworkRoute_Is_Removed_Successfully() throws SQLException { + Assert.assertEquals(1, NetworkRoute.getAllNetworkRoutes().size()); + NetworkRoute networkRoute = new NetworkRoute("stub_from.", "stub_to."); + networkRoute.doDelete(db.getConnection()); + Assert.assertEquals(0, NetworkRoute.getAllNetworkRoutes().size()); + } + + @Test + public void Verify_NetworkRoute_Is_Updated_Successfully() throws SQLException { + NetworkRoute networkRoute = new NetworkRoute("stub_from.", "stub_to.", "node02."); + networkRoute.doUpdate(db.getConnection()); + //Assert.assertTrue(NetworkRoute.getAllNetworkRoutes().contains(networkRoute)); + for (NetworkRoute net : + NetworkRoute.getAllNetworkRoutes()) { + Assert.assertEquals(5, net.getVianode()); + } + NetworkRoute networkRoute1 = new NetworkRoute("stub_from.", "stub_to.", "node02."); + Assert.assertEquals(networkRoute.hashCode(), networkRoute1.hashCode()); + Assert.assertEquals(networkRoute, networkRoute1); + Assert.assertEquals(networkRoute.toString(), networkRoute1.toString()); + } +}
\ No newline at end of file diff --git a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/utils/DbTest.java b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/utils/DbTest.java new file mode 100644 index 00000000..056469a8 --- /dev/null +++ b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/utils/DbTest.java @@ -0,0 +1,61 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.dmaap.datarouter.provisioning.utils; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.modules.junit4.PowerMockRunner; + +@RunWith(PowerMockRunner.class) +public class DbTest { + private static EntityManagerFactory emf; + private static EntityManager em; + + private DB db = new DB(); + + @BeforeClass + public static void init() { + emf = Persistence.createEntityManagerFactory("db-unit-tests"); + em = emf.createEntityManager(); + System.setProperty( + "org.onap.dmaap.datarouter.provserver.properties", + "src/test/resources/h2Database.properties"); + } + + @AfterClass + public static void tearDownClass() { + em.clear(); + em.close(); + emf.close(); + } + + @Test + public void Verify_DB_Is_Initialised_Successfully() { + Assert.assertTrue(db.runRetroFits()); + } + +} diff --git a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/utils/LOGJSONObjectTest.java b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/utils/LOGJSONObjectTest.java index 4dd1b471..ae81f15d 100755 --- a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/utils/LOGJSONObjectTest.java +++ b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/utils/LOGJSONObjectTest.java @@ -24,6 +24,7 @@ package org.onap.dmaap.datarouter.provisioning.utils; import java.io.CharArrayWriter; +import java.io.IOException; import java.io.Writer; import org.json.JSONArray; import org.json.JSONTokener; @@ -50,8 +51,7 @@ public class LOGJSONObjectTest { } @Test - public void Construct_JSONObject_From_A_Subset_Of_Values_From_Another_JSONObject() - throws Exception { + public void Construct_JSONObject_From_A_Subset_Of_Values_From_Another_JSONObject() { Map<String, Object> map = new HashMap<>(); map.put("key1", "value1"); map.put("key2", "value2"); @@ -63,48 +63,41 @@ public class LOGJSONObjectTest { } @Test - public void Construct_JSONObject_From_A_JSONTokener() - throws Exception { + public void Construct_JSONObject_From_A_JSONTokener() { JSONTokener x = new JSONTokener("{\"key1\":\"value1\",\"key3\":\"value3\"}"); LOGJSONObject logJObject = new LOGJSONObject(x); assertThat(logJObject.toString(), is("{\"key1\":\"value1\",\"key3\":\"value3\"}")); } @Test - public void Construct_JSONObject_From_A_Bean_Object_And_Populate_From_Its_Getters_And_Setters() - throws Exception { + public void Construct_JSONObject_From_A_Bean_Object_And_Populate_From_Its_Getters_And_Setters() { Map<String, Object> map = new HashMap<>(); map.put("key1", "value1"); map.put("key2", "value2"); map.put("key3", "value3"); - Object bean = map; - LOGJSONObject logJObject = new LOGJSONObject(bean); + LOGJSONObject logJObject = new LOGJSONObject((Object) map); assertThat(logJObject.toString(), is("{\"empty\":false}")); } @Test - public void Given_Method_Is_Accumulate_And_Value_Is_Valid_Put_Value_Into_New_JSONArray() - throws Exception { + public void Given_Method_Is_Accumulate_And_Value_Is_Valid_Put_Value_Into_New_JSONArray() { Map<String, Object> map = new HashMap<>(); map.put("key", 3); LOGJSONObject logJObject = new LOGJSONObject(map); - String s = "key"; logJObject.accumulate(s, null); assertThat(logJObject.get("key").toString(), is("[3,null]")); } @Test - public void Given_Method_Is_Accumulate_And_Value_Is_Null_Dont_Add_Key_Value_Pair() - throws Exception { + public void Given_Method_Is_Accumulate_And_Value_Is_Null_Dont_Add_Key_Value_Pair() { String s = "key"; logJO.accumulate(s, null); assertThat(logJO.has("key"), is(false)); } @Test - public void Given_Method_Is_Append_And_Value_Is_Null_Append_New_Value() - throws Exception { + public void Given_Method_Is_Append_And_Value_Is_Null_Append_New_Value() { String s = "key"; double d = 2.0; logJO.append(s, d); @@ -113,417 +106,334 @@ public class LOGJSONObjectTest { @Test - public void Given_Method_Is_DoubleToString_And_Value_Is_NaN_Return_Null() - throws Exception { + public void Given_Method_Is_DoubleToString_And_Value_Is_NaN_Return_Null() { double d = 2.0; - assertThat(logJO.doubleToString(d), is("2")); + assertThat(LOGJSONObject.doubleToString(d), is("2")); } @Test - public void Given_Method_Is_GetBoolean_And_Value_Is_False_Return_False() - throws Exception { + public void Given_Method_Is_GetBoolean_And_Value_Is_False_Return_False() { Map<String, Object> map = new HashMap<>(); map.put("key", false); LOGJSONObject logJObject = new LOGJSONObject(map); - String s = "key"; assertThat(logJObject.getBoolean(s), is(false)); } @Test - public void Given_Method_Is_GetBoolean_And_Value_Is_True_Return_True() - throws Exception { + public void Given_Method_Is_GetBoolean_And_Value_Is_True_Return_True() { Map<String, Object> map = new HashMap<>(); map.put("key", true); LOGJSONObject logJObject = new LOGJSONObject(map); - String s = "key"; assertThat(logJObject.getBoolean(s), is(true)); } @Test - public void Given_Method_Is_GetDouble_And_Value_Is_A_Double_Return_Value() - throws Exception { + public void Given_Method_Is_GetDouble_And_Value_Is_A_Double_Return_Value() { Map<String, Object> map = new HashMap<>(); map.put("key", 2.0); LOGJSONObject logJObject = new LOGJSONObject(map); - String s = "key"; assertThat(logJObject.getDouble(s), is(2.0)); } @Test - public void Given_Method_Is_GetInt_And_Value_Is_An_Int_Return_Value() - throws Exception { + public void Given_Method_Is_GetInt_And_Value_Is_An_Int_Return_Value() { Map<String, Object> map = new HashMap<>(); map.put("key", 3); LOGJSONObject logJObject = new LOGJSONObject(map); - String s = "key"; assertThat(logJObject.getInt(s), is(3)); } @Test - public void Given_Method_Is_GetJSONArray_And_Value_Is_A_JSONArray_Return_Value() - throws Exception { + public void Given_Method_Is_GetJSONArray_And_Value_Is_A_JSONArray_Return_Value() { JSONArray jA = new JSONArray(); Map<String, Object> map = new HashMap<>(); map.put("key", jA); LOGJSONObject logJObject = new LOGJSONObject(map); - String s = "key"; assertThat(logJObject.getJSONArray(s), is(jA)); } @Test - public void Given_Method_Is_GetJSONObject_And_Value_Is_A_JSONObject_Return_Value() - throws Exception { + public void Given_Method_Is_GetJSONObject_And_Value_Is_A_JSONObject_Return_Value() { LOGJSONObject logJObj = new LOGJSONObject(); logJObj.put("stub_key", 1); Map<String, Object> map = new HashMap<>(); map.put("key", logJObj); LOGJSONObject logJObject = new LOGJSONObject(map); - String s = "key"; assertThat(logJObject.getJSONObject(s), is(logJObj)); } @Test - public void Given_Method_Is_GetLong_And_Value_Is_A_Long_Return_Value() - throws Exception { + public void Given_Method_Is_GetLong_And_Value_Is_A_Long_Return_Value() { long l = 5; Map<String, Object> map = new HashMap<>(); map.put("key", l); LOGJSONObject logJObject = new LOGJSONObject(map); - String s = "key"; assertThat(logJObject.getLong(s), is(5L)); } @Test - public void Given_Method_Is_getNames_And_Value_Is_A_LOGJSONObject_Return_StringArray() - throws Exception { + public void Given_Method_Is_getNames_And_Value_Is_A_LOGJSONObject_Return_StringArray() { LOGJSONObject logJObj = new LOGJSONObject(); logJObj.put("name1", "elyk"); String[] sArray = new String[logJObj.length()]; sArray[0] = "name1"; - LOGJSONObject logJObject = new LOGJSONObject(); - - assertThat(logJObject.getNames(logJObj), is(sArray)); + assertThat(LOGJSONObject.getNames(logJObj), is(sArray)); } @Test - public void Given_Method_Is_GetString_And_Value_Is_A_String_Return_Value() - throws Exception { + public void Given_Method_Is_GetString_And_Value_Is_A_String_Return_Value() { String val = "value"; Map<String, Object> map = new HashMap<>(); map.put("key", val); LOGJSONObject logJObject = new LOGJSONObject(map); - String s = "key"; assertThat(logJObject.getString(s), is("value")); } @Test - public void Given_Method_Is_Increment_And_Value_Is_Null_Put_Defualt_Value() - throws Exception { + public void Given_Method_Is_Increment_And_Value_Is_Null_Put_Defualt_Value() { Map<String, Object> mapResult = new HashMap<>(); mapResult.put("key", 1); LOGJSONObject logJObjectResult = new LOGJSONObject(mapResult); - String val = null; Map<String, Object> map = new HashMap<>(); map.put("key", val); LOGJSONObject logJObject = new LOGJSONObject(map); - String s = "key"; logJObject.increment(s); assertThat(logJObject.get("key"), is(logJObjectResult.get("key"))); } @Test - public void Given_Method_Is_Increment_And_Value_Is_An_Int_Put_Value_Plus_One() - throws Exception { + public void Given_Method_Is_Increment_And_Value_Is_An_Int_Put_Value_Plus_One() { Map<String, Object> mapResult = new HashMap<>(); mapResult.put("key", 3); LOGJSONObject logJObjectResult = new LOGJSONObject(mapResult); - int val = 2; Map<String, Object> map = new HashMap<>(); map.put("key", val); LOGJSONObject logJObject = new LOGJSONObject(map); - String s = "key"; logJObject.increment(s); assertThat(logJObject.get("key"), is(logJObjectResult.get("key"))); } @Test - public void Given_Method_Is_Increment_And_Value_Is_A_Long_Put_Value_Plus_One() - throws Exception { + public void Given_Method_Is_Increment_And_Value_Is_A_Long_Put_Value_Plus_One() { Map<String, Object> mapResult = new HashMap<>(); mapResult.put("key", 4L); LOGJSONObject logJObjectResult = new LOGJSONObject(mapResult); - long val = 3; Map<String, Object> map = new HashMap<>(); map.put("key", val); LOGJSONObject logJObject = new LOGJSONObject(map); - String s = "key"; logJObject.increment(s); assertThat(logJObject.get("key"), is(logJObjectResult.get("key"))); } @Test - public void Given_Method_Is_Increment_And_Value_Is_A_Double_Put_Value_Plus_One() - throws Exception { + public void Given_Method_Is_Increment_And_Value_Is_A_Double_Put_Value_Plus_One() { Map<String, Object> mapResult = new HashMap<>(); mapResult.put("key", 5.0); LOGJSONObject logJObjectResult = new LOGJSONObject(mapResult); - double val = 4.0; Map<String, Object> map = new HashMap<>(); map.put("key", val); LOGJSONObject logJObject = new LOGJSONObject(map); - String s = "key"; logJObject.increment(s); assertThat(logJObject.get("key"), is(logJObjectResult.get("key"))); } @Test - public void Given_Method_Is_Increment_And_Value_Is_A_Float_Put_Value_Plus_One() - throws Exception { + public void Given_Method_Is_Increment_And_Value_Is_A_Float_Put_Value_Plus_One() { Map<String, Object> mapResult = new HashMap<>(); mapResult.put("key", 5.0); LOGJSONObject logJObjectResult = new LOGJSONObject(mapResult); - float val = 4.0f; Map<String, Object> map = new HashMap<>(); map.put("key", val); LOGJSONObject logJObject = new LOGJSONObject(map); - String s = "key"; logJObject.increment(s); assertThat(logJObject.get("key"), is(logJObjectResult.get("key"))); } @Test - public void Given_Method_Is_Names_And_Object_Contains_Keys_Put_Keys_Into_New_JSONArray() - throws Exception { + public void Given_Method_Is_Names_And_Object_Contains_Keys_Put_Keys_Into_New_JSONArray() { JSONArray ja = new JSONArray(); ja.put("key"); - String val = "value"; Map<String, Object> map = new HashMap<>(); map.put("key", val); LOGJSONObject logJObject = new LOGJSONObject(map); - - String s = "key"; assertThat(logJObject.names().get(0), is(ja.get(0))); } @Test - public void Given_Method_Is_NumberToString_And_Number_is_Not_Null_Return_Reformatted_Number_As_String() - throws Exception { + public void Given_Method_Is_NumberToString_And_Number_is_Not_Null_Return_Reformatted_Number_As_String() { Number num = 3.0; - String val = "value"; Map<String, Object> map = new HashMap<>(); map.put("key", val); LOGJSONObject logJObject = new LOGJSONObject(map); - String s = "key"; assertThat(logJObject.numberToString(num), is("3")); } @Test - public void Given_Method_Is_OptBoolean_And_Value_is_Boolean_Return_Value() - throws Exception { + public void Given_Method_Is_OptBoolean_And_Value_is_Boolean_Return_Value() { boolean val = true; Map<String, Object> map = new HashMap<>(); map.put("key", val); LOGJSONObject logJObject = new LOGJSONObject(map); - String s = "key"; assertThat(logJObject.optBoolean(s, false), is(true)); } @Test - public void Given_Method_Is_OptBoolean_And_Value_is_Not_Boolean_Return_Default_Value() - throws Exception { + public void Given_Method_Is_OptBoolean_And_Value_is_Not_Boolean_Return_Default_Value() { String val = "not_boolean"; Map<String, Object> map = new HashMap<>(); map.put("key", val); LOGJSONObject logJObject = new LOGJSONObject(map); - String s = "key"; assertThat(logJObject.optBoolean(s, false), is(false)); } @Test - public void Given_Method_Is_OptDouble_And_Value_is_Double_Return_Value() - throws Exception { + public void Given_Method_Is_OptDouble_And_Value_is_Double_Return_Value() { double val = 2.0; Map<String, Object> map = new HashMap<>(); map.put("key", val); LOGJSONObject logJObject = new LOGJSONObject(map); - String s = "key"; assertThat(logJObject.optDouble(s, 0.0), is(2.0)); } @Test - public void Given_Method_Is_OptDouble_And_Value_is_Not_Double_Return_Default_Value() - throws Exception { + public void Given_Method_Is_OptDouble_And_Value_is_Not_Double_Return_Default_Value() { String val = "not_double"; Map<String, Object> map = new HashMap<>(); map.put("key", val); LOGJSONObject logJObject = new LOGJSONObject(map); - String s = "key"; assertThat(logJObject.optDouble(s, 0.0), is(0.0)); } @Test - public void Given_Method_Is_OptInt_And_Value_is_Int_Return_Value() - throws Exception { + public void Given_Method_Is_OptInt_And_Value_is_Int_Return_Value() { int val = 1; Map<String, Object> map = new HashMap<>(); map.put("key", val); LOGJSONObject logJObject = new LOGJSONObject(map); - String s = "key"; assertThat(logJObject.optInt(s, 0), is(1)); } @Test - public void Given_Method_Is_OptInt_And_Value_Is_Null_Return_Default_Value() - throws Exception { + public void Given_Method_Is_OptInt_And_Value_Is_Null_Return_Default_Value() { Map<String, Object> map = new HashMap<>(); map.put("key", null); LOGJSONObject logJObject = new LOGJSONObject(map); - String s = "key"; assertThat(logJObject.optInt(s, 0), is(0)); } @Test - public void Given_Method_Is_OptLong_And_Value_is_Long_Return_Value() - throws Exception { + public void Given_Method_Is_OptLong_And_Value_is_Long_Return_Value() { long val = 4; Map<String, Object> map = new HashMap<>(); map.put("key", val); LOGJSONObject logJObject = new LOGJSONObject(map); - String s = "key"; assertThat(logJObject.optLong(s, 0), is(4L)); } @Test - public void Given_Method_Is_OptLong_And_Value_is_Not_Long_Return_Default_Value() - throws Exception { + public void Given_Method_Is_OptLong_And_Value_is_Not_Long_Return_Default_Value() { Map<String, Object> map = new HashMap<>(); map.put("key", null); LOGJSONObject logJObject = new LOGJSONObject(map); - String s = "key"; assertThat(logJObject.optLong(s, 0), is(0L)); } @Test - public void Given_Method_Is_OptString_And_Value_is_String_Return_Value() - throws Exception { + public void Given_Method_Is_OptString_And_Value_is_String_Return_Value() { String val = "value"; Map<String, Object> map = new HashMap<>(); map.put("key", val); LOGJSONObject logJObject = new LOGJSONObject(map); - String s = "key"; assertThat(logJObject.optString(s, "default_value"), is("value")); } @Test - public void Given_Method_Is_putOnce_And_KeyValuePair_Does_Not_Exist_In_logJObject_Put_KeyValuePair_Into_logJObject() - throws Exception { + public void Given_Method_Is_putOnce_And_KeyValuePair_Does_Not_Exist_In_logJObject_Put_KeyValuePair_Into_logJObject() { String val = "value"; Map<String, Object> map = new HashMap<>(); LOGJSONObject logJObject = new LOGJSONObject(map); - String s = "key"; assertThat(logJObject.putOnce(s, val).get("key"), is("value")); } @Test - public void Given_Method_Is_StringToValue_And_Value_Is_Number_Return_Number() - throws Exception { + public void Given_Method_Is_StringToValue_And_Value_Is_Number_Return_Number() { String val = "312"; Map<String, Object> map = new HashMap<>(); - LOGJSONObject logJObject = new LOGJSONObject(map); - - assertThat(logJObject.stringToValue(val), is(312)); + assertThat(LOGJSONObject.stringToValue(val), is(312)); } @Test - public void Given_Method_Is_ToJSONArray_And_KeyValue_Exists_Return_Value_Array() - throws Exception { + public void Given_Method_Is_ToJSONArray_And_KeyValue_Exists_Return_Value_Array() { JSONArray names = new JSONArray(); Map<String, Object> map = new HashMap<>(); map.put("name", "value"); names.put("name"); LOGJSONObject logJObject = new LOGJSONObject(map); - assertThat(logJObject.toJSONArray(names).get(0), is("value")); } @Test - public void Given_Method_Is_ValueToString_And_Value_Is_JSONArray_Return_Value_To_String() - throws Exception { + public void Given_Method_Is_ValueToString_And_Value_Is_JSONArray_Return_Value_To_String() { JSONArray val = new JSONArray(); - Map<String, Object> map = new HashMap<>(); - map.put("key", "value"); val.put("value"); - LOGJSONObject logJObject = new LOGJSONObject(map); - - assertThat(logJObject.valueToString(val), is("[\"value\"]")); + assertThat(LOGJSONObject.valueToString(val), is("[\"value\"]")); } @Test - public void Given_Method_Is_writeValue_And_Value_IS_Not_Null_Return_Writer_With_Value() - throws Exception { + public void Given_Method_Is_writeValue_And_Value_IS_Not_Null_Return_Writer_With_Value() throws IOException { Writer writer = new CharArrayWriter(); String val = "value"; - Map<String, Object> map = new HashMap<>(); - map.put("key", "value"); - LOGJSONObject logJObject = new LOGJSONObject(map); - - assertThat(logJObject.writeValue(writer, val, 3, 1).toString(), is("\"value\"")); + assertThat(LOGJSONObject.writeValue(writer, val, 3, 1).toString(), is("\"value\"")); } @Test - public void Given_Method_Is_write_And_Length_Of_logJObject_Is_One_Write_Value_With_Indent() - throws Exception { + public void Given_Method_Is_write_And_Length_Of_logJObject_Is_One_Write_Value_With_Indent() { Writer writer = new CharArrayWriter(); - String val = "value"; Map<String, Object> map = new HashMap<>(); map.put("key", "value"); LOGJSONObject logJObject = new LOGJSONObject(map); - assertThat(logJObject.write(writer, 3, 1).toString(), is("{\"key\": \"value\"}")); } @Test - public void Given_Method_Is_write_And_Length_Of_logJObject_Is_Not_One_Or_Zero_Write_Value_With_New_Indent() - throws Exception { + public void Given_Method_Is_write_And_Length_Of_logJObject_Is_Not_One_Or_Zero_Write_Value_With_New_Indent() { Writer writer = new CharArrayWriter(); - String val = "value"; Map<String, Object> map = new HashMap<>(); map.put("key", "value"); map.put("key1", "value1"); LOGJSONObject logJObject = new LOGJSONObject(map); - assertThat(logJObject.write(writer, 3, 1).toString(), is("{\n \"key1\": \"value1\",\n \"key\": \"value\"\n }")); } } diff --git a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/utils/LogfileLoaderTest.java b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/utils/LogfileLoaderTest.java index 7f6d7de6..a15509b4 100644 --- a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/utils/LogfileLoaderTest.java +++ b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/utils/LogfileLoaderTest.java @@ -20,32 +20,29 @@ package org.onap.dmaap.datarouter.provisioning.utils; +import static junit.framework.TestCase.assertTrue; +import static org.junit.Assert.assertFalse; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.nio.file.Files; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; +import org.junit.After; import org.junit.AfterClass; import org.junit.Assert; +import org.junit.Before; import org.junit.BeforeClass; -import org.junit.Rule; -import org.junit.rules.TemporaryFolder; +import org.junit.Test; import org.junit.runner.RunWith; - import org.onap.dmaap.datarouter.provisioning.InternalServlet; import org.onap.dmaap.datarouter.provisioning.beans.Parameters; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor; import org.powermock.modules.junit4.PowerMockRunner; -import javax.persistence.EntityManager; -import javax.persistence.EntityManagerFactory; -import javax.persistence.Persistence; -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; - -import static org.junit.Assert.assertFalse; - -import org.junit.Test; - - - @RunWith(PowerMockRunner.class) @SuppressStaticInitializationFor("org.onap.dmaap.datarouter.provisioning.beans.Parameters") public class LogfileLoaderTest { @@ -53,10 +50,18 @@ public class LogfileLoaderTest { private static EntityManagerFactory emf; private static EntityManager em; private LogfileLoader lfl = LogfileLoader.getLoader(); + private File testLog; - @Rule - public TemporaryFolder folder = new TemporaryFolder(); + @Before + public void setUp() throws Exception { + testLog = new File(System.getProperty("user.dir") + "/src/test/resources/IN.test_prov_logs"); + prepFile(testLog); + } + @After + public void tearDown() throws IOException { + Files.deleteIfExists(testLog.toPath()); + } @BeforeClass public static void init() { @@ -68,7 +73,6 @@ public class LogfileLoaderTest { InternalServlet internalServlet = new InternalServlet(); } - @AfterClass public static void tearDownClass() { em.clear(); @@ -76,42 +80,47 @@ public class LogfileLoaderTest { emf.close(); } - @Test - public void Verify_File_Processing_when_Req_Type_LOG() throws IOException { - String fileContent = "2018-08-29-10-10-10-543.|LOG|1|1|url/file123|method|1|1|type|1|128.0.0.9|user123|2|1|1|1|other|1"; - int[] actual = lfl.process(prepFile(fileContent, "file1")); - int[] expect = {0, 1}; + public void Verify_File_Processing_Returns_Expected_Array() { + int[] actual = lfl.process(testLog); + int[] expect = {5, 7}; Assert.assertArrayEquals(expect, actual); + Assert.assertNotNull(lfl.getBitSet()); + Assert.assertTrue(lfl.isIdle()); } - @Test - public void Verify_File_Processing_when_Req_Type_EXP() throws IOException{ - String fileContent = "2018-08-29-10-10-10-543.|EXP|1|1|1|'url/file123'|method|ctype|3|other|4"; - int[] actual = lfl.process(prepFile(fileContent, "file2")); - int[] expect = {0, 1}; - Assert.assertArrayEquals(expect, actual); + public void Verify_Records_Prune_When_Record_Count_Is_Less_Then_Threshold() { + lfl.process(testLog); + PowerMockito.mockStatic(Parameters.class); + PowerMockito.when(Parameters.getParameter(Parameters.PROV_LOG_RETENTION)).thenReturn(new Parameters(Parameters.PROV_LOG_RETENTION, "0")); + PowerMockito.when(Parameters.getParameter(Parameters.DEFAULT_LOG_RETENTION)).thenReturn(new Parameters(Parameters.DEFAULT_LOG_RETENTION, "1000000")); + assertFalse(lfl.pruneRecords()); } - @Test - public void Verify_Records_Prune_When_Record_Count_Is_Less_Then_Threshold() throws IOException{ - String fileContent = "2018-08-29-10-10-10-543.|PUB|1|1|https://dmaap-dr-prov:8443/publish/1/file123/|POST|application/vnd.att-dr.feed|2|128.0.0.9|user123|200"; - lfl.process(prepFile(fileContent, "file3")); + public void Verify_Records_Prune_When_Record_Count_Is_Greater_Then_Threshold() { + lfl.process(testLog); PowerMockito.mockStatic(Parameters.class); PowerMockito.when(Parameters.getParameter(Parameters.PROV_LOG_RETENTION)).thenReturn(new Parameters(Parameters.PROV_LOG_RETENTION, "0")); - assertFalse(lfl.pruneRecords()); + PowerMockito.when(Parameters.getParameter(Parameters.DEFAULT_LOG_RETENTION)).thenReturn(new Parameters(Parameters.DEFAULT_LOG_RETENTION, "1")); + assertTrue(lfl.pruneRecords()); } - private File prepFile(String content, String fileName) throws IOException{ - File file1 = folder.newFile(fileName); - try (FileWriter fileWriter = new FileWriter(file1)) { - fileWriter.write(content); - }catch (IOException e){ + private void prepFile(File logFile) { + String testLogs = "2018-08-29-10-10-10-543.|LOG|1|1|https://dmaap-dr-prov:/url/file123|POST|application/vnd.att-dr.feed|100|mockType|file123|https://dmaap-dr-prov|user123|200|1|1|200|2|2\n" + + "2018-08-29-10-10-10-543.|EXP|1|1|1|'url/file123'|PUT|null|3|new reason|4\n" + + "2018-08-29-10-10-10-543.|PUB|1|1|https://dmaap-dr-prov:8443/publish/1/file123/|POST|application/vnd.att-dr.feed|2|128.0.0.9|user123|200\n" + + "2018-08-29-10-10-10-543.|PBF|1|1|https://dmaap-dr-prov:8443/publish/1/file123/|POST|application/vnd.att-dr.feed|100|100|128.0.0.9|user123|failed\n" + + "2018-08-29-10-10-10-543.|DLX|1|1|1|100|100\n" + + "2018-08-29-10-10-10-543.|Bad Record|||\n" + + "2018-08-29-10-10-10-543.|DEL|2|1|2|https://dmaap-dr-prov:8443/publish/1/file123/|PUT|application/vnd.att-dr.feed|100|user123|200|123456"; + try (FileWriter fileWriter = new FileWriter(logFile)) { + fileWriter.write(testLogs); + } + catch (IOException e){ System.out.println(e.getMessage()); } - return file1; } } diff --git a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/utils/PurgeLogDirTaskTest.java b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/utils/PurgeLogDirTaskTest.java new file mode 100644 index 00000000..604b4c0a --- /dev/null +++ b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/utils/PurgeLogDirTaskTest.java @@ -0,0 +1,87 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.dmaap.datarouter.provisioning.utils; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.nio.file.Files; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor; +import org.powermock.modules.junit4.PowerMockRunner; + +@RunWith(PowerMockRunner.class) +public class PurgeLogDirTaskTest { + + private static EntityManagerFactory emf; + private static EntityManager em; + private PurgeLogDirTask purgeLogDirTask = new PurgeLogDirTask(); + private File testLog; + + @Before + public void setUp() throws Exception { + testLog = new File(System.getProperty("user.dir") + "/src/test/resources/IN.test_prov_logs"); + prepFile(testLog); + } + + @After + public void tearDown() throws IOException { + Files.deleteIfExists(testLog.toPath()); + } + + @BeforeClass + public static void init() { + emf = Persistence.createEntityManagerFactory("dr-unit-tests"); + em = emf.createEntityManager(); + System.setProperty( + "org.onap.dmaap.datarouter.provserver.properties", + "src/test/resources/h2Database.properties"); + } + + @AfterClass + public static void tearDownClass() { + em.clear(); + em.close(); + emf.close(); + } + + @Test + public void Verify_Logs_Are_Purged() { + purgeLogDirTask.run(); + } + + private void prepFile(File logFile) { + try (FileWriter fileWriter = new FileWriter(logFile)) { + fileWriter.write("2018-08-29-10-10-10-543.|LOG|1|1|https://dmaap-dr-prov:/url/file123|POST|application/vnd.att-dr.feed|100|mockType|file123|https://dmaap-dr-prov|user123|200|1|1|200|2|2\n"); + } + catch (IOException e){ + System.out.println(e.getMessage()); + } + } +} diff --git a/datarouter-prov/src/test/resources/META-INF/persistence.xml b/datarouter-prov/src/test/resources/META-INF/persistence.xml index 83813e23..2cb798af 100755 --- a/datarouter-prov/src/test/resources/META-INF/persistence.xml +++ b/datarouter-prov/src/test/resources/META-INF/persistence.xml @@ -18,4 +18,18 @@ </properties> </persistence-unit> + <persistence-unit name="db-unit-tests" transaction-type="RESOURCE_LOCAL"> + <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> + <properties> + <!-- Configuring JDBC properties --> + <property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test1;DB_CLOSE_DELAY=-1"/> + <property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/> + <!-- Hibernate properties --> + <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/> + <property name="hibernate.hbm2ddl.auto" value="create-drop"/> + <property name="hibernate.format_sql" value="false"/> + <property name="hibernate.show_sql" value="true"/> + + </properties> + </persistence-unit> </persistence> diff --git a/datarouter-prov/src/test/resources/create.sql b/datarouter-prov/src/test/resources/create.sql index a811847c..74b63242 100755 --- a/datarouter-prov/src/test/resources/create.sql +++ b/datarouter-prov/src/test/resources/create.sql @@ -180,10 +180,10 @@ INSERT INTO FEEDS(FEEDID, GROUPID, NAME, VERSION, DESCRIPTION, BUSINESS_DESCRIPT VALUES (3, 1,'DeleteableAafFeed','v0.1', 'AAF Feed3 for testing', 'AAF Feed3 for testing', 'auth_class', 'pub','self_link','publish_link','subscribe_link','log_link','*'); insert into INGRESS_ROUTES(SEQUENCE, FEEDID , USERID, SUBNET, NODESET) -VALUES (1,1,'user',null,2); +VALUES (1,1,'user','172.100.0.0/25',2); insert into INGRESS_ROUTES(SEQUENCE, FEEDID , USERID, SUBNET, NODESET) -VALUES (2,1,'user',null,2); +VALUES (2,1,'user2',null,2); insert into NODESETS(SETID, NODEID) VALUES (1,1); diff --git a/datarouter-prov/src/test/resources/h2Database.properties b/datarouter-prov/src/test/resources/h2Database.properties index 9c63aea4..991fadc5 100755 --- a/datarouter-prov/src/test/resources/h2Database.properties +++ b/datarouter-prov/src/test/resources/h2Database.properties @@ -27,7 +27,8 @@ org.onap.dmaap.datarouter.db.url = jdbc:h2:mem:test;DB org.onap.dmaap.datarouter.provserver.isaddressauthenabled = true org.onap.dmaap.datarouter.provserver.https.relaxation = false org.onap.dmaap.datarouter.provserver.accesslog.dir = unit-test-logs -org.onap.dmaap.datarouter.provserver.spooldir = unit-test-logs/spool +org.onap.dmaap.datarouter.provserver.spooldir = src/test/resources +org.onap.dmaap.datarouter.provserver.dbscripts = src/test/resources org.onap.dmaap.datarouter.provserver.localhost = 127.0.0.1 org.onap.dmaap.datarouter.provserver.passwordencryption = PasswordEncryptionKey#@$%^&1234# diff --git a/datarouter-prov/src/test/resources/sql_init_01.sql b/datarouter-prov/src/test/resources/sql_init_01.sql new file mode 100755 index 00000000..1ac74a03 --- /dev/null +++ b/datarouter-prov/src/test/resources/sql_init_01.sql @@ -0,0 +1,146 @@ +CREATE TABLE FEEDS ( + FEEDID INT UNSIGNED NOT NULL PRIMARY KEY, + GROUPID INT(10) UNSIGNED NOT NULL DEFAULT 0, + NAME VARCHAR(256) NOT NULL, + VERSION VARCHAR(20) NULL, + DESCRIPTION VARCHAR(1000), + BUSINESS_DESCRIPTION VARCHAR(1000) DEFAULT NULL, + AUTH_CLASS VARCHAR(32) NOT NULL, + PUBLISHER VARCHAR(8) NOT NULL, + SELF_LINK VARCHAR(256), + PUBLISH_LINK VARCHAR(256), + SUBSCRIBE_LINK VARCHAR(256), + LOG_LINK VARCHAR(256), + DELETED BOOLEAN DEFAULT FALSE, + LAST_MOD TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + SUSPENDED BOOLEAN DEFAULT FALSE, + CREATED_DATE TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + AAF_INSTANCE VARCHAR(256) +); + +CREATE TABLE FEED_ENDPOINT_IDS ( + FEEDID INT UNSIGNED NOT NULL, + USERID VARCHAR(60) NOT NULL, + PASSWORD VARCHAR(100) NOT NULL +); + +CREATE TABLE FEED_ENDPOINT_ADDRS ( + FEEDID INT UNSIGNED NOT NULL, + ADDR VARCHAR(44) NOT NULL +); + +CREATE TABLE SUBSCRIPTIONS ( + SUBID INT UNSIGNED NOT NULL PRIMARY KEY, + FEEDID INT UNSIGNED NOT NULL, + GROUPID INT(10) UNSIGNED NOT NULL DEFAULT 0, + DELIVERY_URL VARCHAR(256), + FOLLOW_REDIRECTS TINYINT(1) NOT NULL DEFAULT 0, + DELIVERY_USER VARCHAR(60), + DELIVERY_PASSWORD VARCHAR(100), + DELIVERY_USE100 BOOLEAN DEFAULT FALSE, + METADATA_ONLY BOOLEAN DEFAULT FALSE, + SUBSCRIBER VARCHAR(8) NOT NULL, + SELF_LINK VARCHAR(256), + LOG_LINK VARCHAR(256), + LAST_MOD TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + SUSPENDED BOOLEAN DEFAULT FALSE, + PRIVILEGED_SUBSCRIBER BOOLEAN DEFAULT FALSE, + CREATED_DATE TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + DECOMPRESS BOOLEAN DEFAULT FALSE, + AAF_INSTANCE VARCHAR(256) + +); + +CREATE TABLE PARAMETERS ( + KEYNAME VARCHAR(32) NOT NULL PRIMARY KEY, + VALUE VARCHAR(4096) NOT NULL +); + +CREATE TABLE LOG_RECORDS ( + TYPE ENUM('pub', 'del', 'exp', 'pbf', 'dlx') NOT NULL, + EVENT_TIME BIGINT NOT NULL, /* time of the publish request */ + PUBLISH_ID VARCHAR(64) NOT NULL, /* unique ID assigned to this publish attempt */ + FEEDID INT UNSIGNED NOT NULL, /* pointer to feed in FEEDS */ + REQURI VARCHAR(256) NOT NULL, /* request URI */ + METHOD ENUM('DELETE', 'GET', 'HEAD', 'OPTIONS', 'PUT', 'POST', 'TRACE') NOT NULL, /* HTTP method */ + CONTENT_TYPE VARCHAR(256) NOT NULL, /* content type of published file */ + CONTENT_LENGTH BIGINT NOT NULL, /* content length of published file */ + + FEED_FILEID VARCHAR(256), /* file ID of published file */ + REMOTE_ADDR VARCHAR(40), /* IP address of publishing endpoint */ + USER VARCHAR(50), /* user name of publishing endpoint */ + STATUS SMALLINT, /* status code returned to delivering agent */ + + DELIVERY_SUBID INT UNSIGNED, /* pointer to subscription in SUBSCRIPTIONS */ + DELIVERY_FILEID VARCHAR(256), /* file ID of file being delivered */ + RESULT SMALLINT, /* result received from subscribing agent */ + + ATTEMPTS INT, /* deliveries attempted */ + REASON ENUM('notRetryable', 'retriesExhausted', 'diskFull', 'other'), + + RECORD_ID BIGINT UNSIGNED NOT NULL PRIMARY KEY, /* unique ID for this record */ + CONTENT_LENGTH_2 BIGINT, + FILENAME VARCHAR(256), /* Name of the file being published on DR */ + +) ENGINE = MyISAM; + +CREATE TABLE INGRESS_ROUTES ( + SEQUENCE INT UNSIGNED NOT NULL, + FEEDID INT UNSIGNED NOT NULL, + USERID VARCHAR(50), + SUBNET VARCHAR(44), + NODESET INT UNSIGNED NOT NULL +); + +CREATE TABLE EGRESS_ROUTES ( + SUBID INT UNSIGNED NOT NULL PRIMARY KEY, + NODEID INT UNSIGNED NOT NULL +); + +CREATE TABLE NETWORK_ROUTES ( + FROMNODE INT UNSIGNED NOT NULL, + TONODE INT UNSIGNED NOT NULL, + VIANODE INT UNSIGNED NOT NULL +); + +CREATE TABLE NODESETS ( + SETID INT UNSIGNED NOT NULL, + NODEID INT UNSIGNED NOT NULL +); + +CREATE TABLE NODES ( + NODEID INT UNSIGNED NOT NULL PRIMARY KEY, + NAME VARCHAR(255) NOT NULL, + ACTIVE BOOLEAN DEFAULT TRUE +); + +CREATE TABLE GROUPS ( + GROUPID INT UNSIGNED NOT NULL PRIMARY KEY, + AUTHID VARCHAR(100) NOT NULL, + NAME VARCHAR(50) NOT NULL, + DESCRIPTION VARCHAR(255), + CLASSIFICATION VARCHAR(20) NOT NULL, + MEMBERS TINYTEXT, + LAST_MOD TIMESTAMP DEFAULT CURRENT_TIMESTAMP +); + +INSERT INTO PARAMETERS VALUES + ('ACTIVE_POD', 'dmaap-dr-prov'), + ('PROV_ACTIVE_NAME', 'dmaap-dr-prov'), + ('STANDBY_POD', ''), + ('PROV_NAME', 'dmaap-dr-prov'), + ('NODES', 'dmaap-dr-node'), + ('PROV_DOMAIN', ''), + ('DELIVERY_INIT_RETRY_INTERVAL', '10'), + ('DELIVERY_MAX_AGE', '86400'), + ('DELIVERY_MAX_RETRY_INTERVAL', '3600'), + ('DELIVERY_FILE_PROCESS_INTERVAL', '600'), + ('DELIVERY_RETRY_RATIO', '2'), + ('LOGROLL_INTERVAL', '30'), + ('PROV_AUTH_ADDRESSES', 'dmaap-dr-prov|dmaap-dr-node'), + ('PROV_AUTH_SUBJECTS', ''), + ('PROV_MAXFEED_COUNT', '10000'), + ('PROV_MAXSUB_COUNT', '100000'), + ('PROV_REQUIRE_CERT', 'false'), + ('PROV_REQUIRE_SECURE', 'true'), + ('_INT_VALUES', 'LOGROLL_INTERVAL|PROV_MAXFEED_COUNT|PROV_MAXSUB_COUNT|DELIVERY_INIT_RETRY_INTERVAL|DELIVERY_MAX_RETRY_INTERVAL|DELIVERY_RETRY_RATIO|DELIVERY_MAX_AGE|DELIVERY_FILE_PROCESS_INTERVAL');
\ No newline at end of file |