Removing code smells
[dmaap/datarouter.git] / datarouter-node / src / main / java / org / onap / dmaap / datarouter / node / NodeConfigManager.java
index 8a0b0b8..0283f5c 100644 (file)
 
 package org.onap.dmaap.datarouter.node;
 
+import static java.lang.System.exit;
+
 import com.att.eelf.configuration.EELFLogger;
 import com.att.eelf.configuration.EELFManager;
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.Reader;
 import java.net.URL;
+import java.nio.file.Files;
+import java.util.Objects;
 import java.util.Properties;
 import java.util.Timer;
 import org.onap.dmaap.datarouter.node.eelf.EelfMsgs;
@@ -38,13 +43,13 @@ import org.onap.dmaap.datarouter.node.eelf.EelfMsgs;
 
 /**
  * Maintain the configuration of a Data Router node
- * <p>
- * The NodeConfigManager is the single point of contact for servlet, delivery, event logging, and log retention
+ *
+ * <p>The NodeConfigManager is the single point of contact for servlet, delivery, event logging, and log retention
  * subsystems to access configuration information.
- * <p>
- * There are two basic sets of configuration data.  The static local configuration data, stored in a local configuration
- * file (created as part of installation by SWM), and the dynamic global configuration data fetched from the data router
- * provisioning server.
+ *
+ * <p>There are two basic sets of configuration data.  The static local configuration data, stored in a local
+ * configuration file (created as part of installation by SWM), and the dynamic global configuration data fetched from
+ * the data router provisioning server.
  */
 public class NodeConfigManager implements DeliveryQueueHelper {
 
@@ -103,13 +108,13 @@ public class NodeConfigManager implements DeliveryQueueHelper {
 
 
     /**
-     * Initialize the configuration of a Data Router node
+     * Initialize the configuration of a Data Router node.
      */
     private NodeConfigManager() {
 
         Properties drNodeProperties = new Properties();
         try {
-            eelfLogger.info("NODE0301 Loading local config file node.properties");
+            eelfLogger.debug("NODE0301 Loading local config file node.properties");
             drNodeProperties.load(new FileInputStream(System
                     .getProperty("org.onap.dmaap.datarouter.node.properties", "/opt/app/datartr/etc/node.properties")));
         } catch (Exception e) {
@@ -134,28 +139,27 @@ public class NodeConfigManager implements DeliveryQueueHelper {
          */
         //Disable and enable protocols*/
         enabledprotocols = ((drNodeProperties.getProperty("NodeHttpsProtocols")).trim()).split("\\|");
-
         try {
             provhost = (new URL(provurl)).getHost();
         } catch (Exception e) {
             NodeUtils.setIpAndFqdnForEelf(NODE_CONFIG_MANAGER);
             eelfLogger.error(EelfMsgs.MESSAGE_BAD_PROV_URL, e, provurl);
-            System.exit(1);
+            exit(1);
         }
-        eelfLogger.info("NODE0303 Provisioning server is " + provhost);
+        eelfLogger.debug("NODE0303 Provisioning server is " + provhost);
         eventlogurl = drNodeProperties.getProperty("LogUploadURL", "https://feeds-drtr.web.att.com/internal/logs");
         provcheck = new IsFrom(provhost);
         gfport = Integer.parseInt(drNodeProperties.getProperty("IntHttpPort", "8080"));
         svcport = Integer.parseInt(drNodeProperties.getProperty("IntHttpsPort", "8443"));
         port = Integer.parseInt(drNodeProperties.getProperty("ExtHttpsPort", "443"));
-        long minpfinterval = Long.parseLong(drNodeProperties.getProperty("MinProvFetchInterval", "10000"));
-        long minrsinterval = Long.parseLong(drNodeProperties.getProperty("MinRedirSaveInterval", "10000"));
         spooldir = drNodeProperties.getProperty("SpoolDir", "spool");
         File fdir = new File(spooldir + "/f");
         fdir.mkdirs();
-        for (File junk : fdir.listFiles()) {
-            if (junk.isFile()) {
-                junk.delete();
+        for (File junk : Objects.requireNonNull(fdir.listFiles())) {
+            try {
+                Files.deleteIfExists(junk.toPath());
+            } catch (IOException e) {
+                eelfLogger.error("NODE0313 Failed to clear junk files from " + fdir.getPath(), e);
             }
         }
         logdir = drNodeProperties.getProperty("LogDir", "logs");
@@ -183,22 +187,24 @@ public class NodeConfigManager implements DeliveryQueueHelper {
             NodeUtils.setIpAndFqdnForEelf(NODE_CONFIG_MANAGER);
             eelfLogger.error(EelfMsgs.MESSAGE_KEYSTORE_FETCH_ERROR, ksfile);
             eelfLogger.error("NODE0309 Unable to fetch canonical name from keystore file " + ksfile);
-            System.exit(1);
+            exit(1);
         }
-        eelfLogger.info("NODE0304 My certificate says my name is " + myname);
+        eelfLogger.debug("NODE0304 My certificate says my name is " + myname);
         pid = new PublishId(myname);
+        long minrsinterval = Long.parseLong(drNodeProperties.getProperty("MinRedirSaveInterval", "10000"));
+        long minpfinterval = Long.parseLong(drNodeProperties.getProperty("MinProvFetchInterval", "10000"));
         rdmgr = new RedirManager(redirfile, minrsinterval, timer);
         pfetcher = new RateLimitedOperation(minpfinterval, timer) {
             public void run() {
                 fetchconfig();
             }
         };
-        eelfLogger.info("NODE0305 Attempting to fetch configuration at " + provurl);
+        eelfLogger.debug("NODE0305 Attempting to fetch configuration at " + provurl);
         pfetcher.request();
     }
 
     /**
-     * Get the default node configuration manager
+     * Get the default node configuration manager.
      */
     public static NodeConfigManager getInstance() {
         return base;
@@ -284,9 +290,9 @@ public class NodeConfigManager implements DeliveryQueueHelper {
 
     private void fetchconfig() {
         try {
-            eelfLogger.info("NodeConfigMan.fetchConfig: provurl:: " + provurl);
-            Reader r = new InputStreamReader((new URL(provurl)).openStream());
-            config = new NodeConfig(new ProvData(r), myname, spooldir, port, nak);
+            eelfLogger.debug("NodeConfigMan.fetchConfig: provurl:: " + provurl);
+            Reader reader = new InputStreamReader((new URL(provurl)).openStream());
+            config = new NodeConfig(new ProvData(reader), myname, spooldir, port, nak);
             localconfig();
             configtasks.startRun();
             runTasks();
@@ -316,22 +322,22 @@ public class NodeConfigManager implements DeliveryQueueHelper {
      */
     public synchronized void gofetch(String remoteAddr) {
         if (provcheck.isReachable(remoteAddr)) {
-            eelfLogger.info("NODE0307 Received configuration fetch request from provisioning server " + remoteAddr);
+            eelfLogger.debug("NODE0307 Received configuration fetch request from provisioning server " + remoteAddr);
             pfetcher.request();
         } else {
-            eelfLogger.info("NODE0308 Received configuration fetch request from unexpected server " + remoteAddr);
+            eelfLogger.debug("NODE0308 Received configuration fetch request from unexpected server " + remoteAddr);
         }
     }
 
     /**
-     * Am I configured?
+     * Am I configured.
      */
     public boolean isConfigured() {
         return (config != null);
     }
 
     /**
-     * Am I shut down?
+     * Am I shut down.
      */
     public boolean isShutdown() {
         return (quiesce.exists());
@@ -348,7 +354,7 @@ public class NodeConfigManager implements DeliveryQueueHelper {
     }
 
     /**
-     * Given a set of credentials and an IP address, is this request from another node?
+     * Given a set of credentials and an IP address, is this request from another node.
      *
      * @param credentials Credentials offered by the supposed node
      * @param ip IP address the request came from
@@ -370,16 +376,6 @@ public class NodeConfigManager implements DeliveryQueueHelper {
         return (config.isPublishPermitted(feedid, credentials, ip));
     }
 
-    /**
-     * Check whether delete file is allowed.
-     *
-     * @param subId The ID of the subscription being requested
-     * @return True if the delete file is permitted for the subscriber.
-     */
-    public boolean isDeletePermitted(String subId) {
-        return (config.isDeletePermitted(subId));
-    }
-
     /**
      * Check whether publication is allowed for AAF Feed.
      *
@@ -391,6 +387,16 @@ public class NodeConfigManager implements DeliveryQueueHelper {
         return (config.isPublishPermitted(feedid, ip));
     }
 
+    /**
+     * Check whether delete file is allowed.
+     *
+     * @param subId The ID of the subscription being requested
+     * @return True if the delete file is permitted for the subscriber.
+     */
+    public boolean isDeletePermitted(String subId) {
+        return (config.isDeletePermitted(subId));
+    }
+
     /**
      * Check who the user is given the feed ID and the offered credentials.
      *
@@ -403,7 +409,7 @@ public class NodeConfigManager implements DeliveryQueueHelper {
     }
 
     /**
-     * AAF changes: TDP EPIC US# 307413 Check AAF_instance for feed ID in NodeConfig
+     * AAF changes: TDP EPIC US# 307413 Check AAF_instance for feed ID in NodeConfig.
      *
      * @param feedid The ID of the feed specified
      */
@@ -411,6 +417,10 @@ public class NodeConfigManager implements DeliveryQueueHelper {
         return (config.getAafInstance(feedid));
     }
 
+    public String getAafInstance() {
+        return aafInstance;
+    }
+
     /**
      * Check if the publish request should be sent to another node based on the feedid, user, and source IP address.
      *
@@ -424,7 +434,7 @@ public class NodeConfigManager implements DeliveryQueueHelper {
     }
 
     /**
-     * Get a provisioned configuration parameter (from the provisioning server configuration)
+     * Get a provisioned configuration parameter (from the provisioning server configuration).
      *
      * @param name The name of the parameter
      * @return The value of the parameter or null if it is not defined.
@@ -434,7 +444,7 @@ public class NodeConfigManager implements DeliveryQueueHelper {
     }
 
     /**
-     * Get a provisioned configuration parameter (from the provisioning server configuration)
+     * Get a provisioned configuration parameter (from the provisioning server configuration).
      *
      * @param name The name of the parameter
      * @param defaultValue The value to use if the parameter is not defined
@@ -449,7 +459,7 @@ public class NodeConfigManager implements DeliveryQueueHelper {
     }
 
     /**
-     * Generate a publish ID
+     * Generate a publish ID.
      */
     public String getPublishId() {
         return (pid.next());
@@ -463,14 +473,14 @@ public class NodeConfigManager implements DeliveryQueueHelper {
     }
 
     /**
-     * Register a task to run whenever the configuration changes
+     * Register a task to run whenever the configuration changes.
      */
     public void registerConfigTask(Runnable task) {
         configtasks.addTask(task);
     }
 
     /**
-     * Deregister a task to run whenever the configuration changes
+     * Deregister a task to run whenever the configuration changes.
      */
     public void deregisterConfigTask(Runnable task) {
         configtasks.removeTask(task);
@@ -493,14 +503,7 @@ public class NodeConfigManager implements DeliveryQueueHelper {
     }
 
     /**
-     * Is a destination redirected?
-     */
-    public boolean isDestRedirected(DestInfo destinfo) {
-        return (followredirects && rdmgr.isRedirected(destinfo.getSubId()));
-    }
-
-    /**
-     * Set up redirection on receipt of a 3XX from a target URL
+     * Set up redirection on receipt of a 3XX from a target URL.
      */
     public boolean handleRedirection(DestInfo destinationInfo, String redirto, String fileid) {
         fileid = "/" + fileid;
@@ -517,24 +520,7 @@ public class NodeConfigManager implements DeliveryQueueHelper {
     }
 
     /**
-     * Set up redirection on receipt of a 3XX from a target URL
-     */
-    public boolean handleRedirectionSubLevel(DeliveryTask task, DestInfo destinfo, String redirto, String fileid) {
-        fileid = "/" + fileid;
-        String subid = destinfo.getSubId();
-        String purl = destinfo.getURL();
-        if (task.getFollowRedirects() && subid != null && redirto.endsWith(fileid)) {
-            redirto = redirto.substring(0, redirto.length() - fileid.length());
-            if (!redirto.equals(purl)) {
-                rdmgr.redirect(subid, purl, redirto);
-                return true;
-            }
-        }
-        return false;
-    }
-
-    /**
-     * Handle unreachable target URL
+     * Handle unreachable target URL.
      */
     public void handleUnreachable(DestInfo destinationInfo) {
         String subid = destinationInfo.getSubId();
@@ -544,35 +530,35 @@ public class NodeConfigManager implements DeliveryQueueHelper {
     }
 
     /**
-     * Get the timeout before retrying after an initial delivery failure
+     * Get the timeout before retrying after an initial delivery failure.
      */
     public long getInitFailureTimer() {
         return (initfailuretimer);
     }
 
     /**
-     * Get the timeout before retrying after delivery and wait for file processing
+     * Get the timeout before retrying after delivery and wait for file processing.
      */
     public long getWaitForFileProcessFailureTimer() {
         return (waitForFileProcessFailureTimer);
     }
 
     /**
-     * Get the maximum timeout between delivery attempts
+     * Get the maximum timeout between delivery attempts.
      */
     public long getMaxFailureTimer() {
         return (maxfailuretimer);
     }
 
     /**
-     * Get the ratio between consecutive delivery attempts
+     * Get the ratio between consecutive delivery attempts.
      */
     public double getFailureBackoff() {
         return (failurebackoff);
     }
 
     /**
-     * Get the expiration timer for deliveries
+     * Get the expiration timer for deliveries.
      */
     public long getExpirationTimer() {
         return (expirationtimer);
@@ -593,7 +579,7 @@ public class NodeConfigManager implements DeliveryQueueHelper {
     }
 
     /**
-     * Get the targets for a feed
+     * Get the targets for a feed.
      *
      * @param feedid The feed ID
      * @return The targets this feed should be delivered to
@@ -603,150 +589,160 @@ public class NodeConfigManager implements DeliveryQueueHelper {
     }
 
     /**
-     * Get the creation date for a feed
-     *
-     * @param feedid The feed ID
-     * @return the timestamp of creation date of feed id passed
+     * Get the spool directory for temporary files.
      */
-    public String getCreatedDate(String feedid) {
-        return (config.getCreatedDate(feedid));
+    public String getSpoolDir() {
+        return (spooldir + "/f");
     }
 
     /**
-     * Get the spool directory for temporary files
+     * Get the spool directory for a subscription.
      */
-    public String getSpoolDir() {
-        return (spooldir + "/f");
+    public String getSpoolDir(String subid, String remoteaddr) {
+        if (provcheck.isFrom(remoteaddr)) {
+            String sdir = config.getSpoolDir(subid);
+            if (sdir != null) {
+                eelfLogger.debug("NODE0310 Received subscription reset request for subscription " + subid
+                        + " from provisioning server " + remoteaddr);
+            } else {
+                eelfLogger.debug("NODE0311 Received subscription reset request for unknown subscription " + subid
+                        + " from provisioning server " + remoteaddr);
+            }
+            return (sdir);
+        } else {
+            eelfLogger.debug("NODE0312 Received subscription reset request from unexpected server " + remoteaddr);
+            return (null);
+        }
     }
 
     /**
-     * Get the base directory for spool directories
+     * Get the base directory for spool directories.
      */
     public String getSpoolBase() {
         return (spooldir);
     }
 
     /**
-     * Get the key store type
+     * Get the key store type.
      */
     public String getKSType() {
         return (kstype);
     }
 
     /**
-     * Get the key store file
+     * Get the key store file.
      */
     public String getKSFile() {
         return (ksfile);
     }
 
     /**
-     * Get the key store password
+     * Get the key store password.
      */
     public String getKSPass() {
         return (kspass);
     }
 
     /**
-     * Get the key password
+     * Get the key password.
      */
     public String getKPass() {
         return (kpass);
     }
 
     /**
-     * Get the http port
+     * Get the http port.
      */
     public int getHttpPort() {
         return (gfport);
     }
 
     /**
-     * Get the https port
+     * Get the https port.
      */
     public int getHttpsPort() {
         return (svcport);
     }
 
     /**
-     * Get the externally visible https port
+     * Get the externally visible https port.
      */
     public int getExtHttpsPort() {
         return (port);
     }
 
     /**
-     * Get the external name of this machine
+     * Get the external name of this machine.
      */
     public String getMyName() {
         return (myname);
     }
 
     /**
-     * Get the number of threads to use for delivery
+     * Get the number of threads to use for delivery.
      */
     public int getDeliveryThreads() {
         return (deliverythreads);
     }
 
     /**
-     * Get the URL for uploading the event log data
+     * Get the URL for uploading the event log data.
      */
     public String getEventLogUrl() {
         return (eventlogurl);
     }
 
     /**
-     * Get the prefix for the names of event log files
+     * Get the prefix for the names of event log files.
      */
     public String getEventLogPrefix() {
         return (eventlogprefix);
     }
 
     /**
-     * Get the suffix for the names of the event log files
+     * Get the suffix for the names of the event log files.
      */
     public String getEventLogSuffix() {
         return (eventlogsuffix);
     }
 
     /**
-     * Get the interval between event log file rollovers
+     * Get the interval between event log file rollovers.
      */
     public String getEventLogInterval() {
         return (eventloginterval);
     }
 
     /**
-     * Should I follow redirects from subscribers?
+     * Should I follow redirects from subscribers.
      */
     public boolean isFollowRedirects() {
         return (followredirects);
     }
 
     /**
-     * Get the directory where the event and node log files live
+     * Get the directory where the event and node log files live.
      */
     public String getLogDir() {
         return (logdir);
     }
 
     /**
-     * How long do I keep log files (in milliseconds)
+     * How long do I keep log files (in milliseconds).
      */
     public long getLogRetention() {
         return (logretention);
     }
 
     /**
-     * Get the timer
+     * Get the timer.
      */
     public Timer getTimer() {
         return (timer);
     }
 
     /**
-     * Get the feed ID for a subscription
+     * Get the feed ID for a subscription.
      *
      * @param subid The subscription ID
      * @return The feed ID
@@ -756,7 +752,7 @@ public class NodeConfigManager implements DeliveryQueueHelper {
     }
 
     /**
-     * Get the authorization string this node uses
+     * Get the authorization string this node uses.
      *
      * @return The Authorization string for this node
      */
@@ -781,60 +777,20 @@ public class NodeConfigManager implements DeliveryQueueHelper {
     }
 
     /**
-     * Disable and enable protocols
+     * Disable and enable protocols.
      */
     public String[] getEnabledprotocols() {
         return enabledprotocols;
     }
 
-    public void setEnabledprotocols(String[] enabledprotocols) {
-        this.enabledprotocols = enabledprotocols.clone();
-    }
-
-    /**
-     * Get the spool directory for a subscription
-     */
-    public String getSpoolDir(String subid, String remoteaddr) {
-        if (provcheck.isFrom(remoteaddr)) {
-            String sdir = config.getSpoolDir(subid);
-            if (sdir != null) {
-                eelfLogger.info("NODE0310 Received subscription reset request for subscription " + subid
-                        + " from provisioning server " + remoteaddr);
-            } else {
-                eelfLogger.info("NODE0311 Received subscription reset request for unknown subscription " + subid
-                        + " from provisioning server " + remoteaddr);
-            }
-            return (sdir);
-        } else {
-            eelfLogger.info("NODE0312 Received subscription reset request from unexpected server " + remoteaddr);
-            return (null);
-        }
-    }
-
     public String getAafType() {
         return aafType;
     }
 
-    public void setAafType(String aafType) {
-        this.aafType = aafType;
-    }
-
-    public String getAafInstance() {
-        return aafInstance;
-    }
-
-    public void setAafInstance(String aafInstance) {
-        this.aafInstance = aafInstance;
-    }
-
     public String getAafAction() {
         return aafAction;
     }
 
-    public void setAafAction(String aafAction) {
-        this.aafAction = aafAction;
-    }
-
     /*
      * Get aafURL from SWM variable
      * */
@@ -842,20 +798,12 @@ public class NodeConfigManager implements DeliveryQueueHelper {
         return aafURL;
     }
 
-    public void setAafURL(String aafURL) {
-        this.aafURL = aafURL;
-    }
-
     public boolean getCadiEnabled() {
         return cadiEnabled;
     }
 
-    public void setCadiEnabled(boolean cadiEnabled) {
-        this.cadiEnabled = cadiEnabled;
-    }
-
     /**
-     * Builds the permissions string to be verified
+     * Builds the permissions string to be verified.
      *
      * @param aafInstance The aaf instance
      * @return The permissions