Remove major and minor code smells in dr-node
[dmaap/datarouter.git] / datarouter-node / src / main / java / org / onap / dmaap / datarouter / node / StatusLog.java
index 1be3408..e616558 100644 (file)
@@ -25,20 +25,27 @@ package org.onap.dmaap.datarouter.node;
 
 import com.att.eelf.configuration.EELFLogger;
 import com.att.eelf.configuration.EELFManager;
-import java.util.regex.*;
-import java.util.*;
-import java.io.*;
-import java.nio.file.*;
-import java.text.*;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 /**
  * Logging for data router delivery events (PUB/DEL/EXP)
  */
 public class StatusLog {
+
+    private static final String EXCEPTION = "Exception";
     private static EELFLogger eelfLogger = EELFManager.getInstance().getLogger(StatusLog.class);
     private static StatusLog instance = new StatusLog();
-    private HashSet<String> toship = new HashSet<String>();
-    private SimpleDateFormat filedate;
+    private SimpleDateFormat filedate = new SimpleDateFormat("-yyyyMMddHHmm");
+
     private String prefix = "logs/events";
     private String suffix = ".log";
     private String plainfile;
@@ -48,85 +55,75 @@ public class StatusLog {
     private long intvl;
     private NodeConfigManager config = NodeConfigManager.getInstance();
 
-    {
-        try {
-            filedate = new SimpleDateFormat("-yyyyMMddHHmm");
-        } catch (Exception e) {
-            eelfLogger.error("Exception", e);
-        }
+    private StatusLog() {
     }
 
     /**
-     * Parse an interval of the form xxhyymzzs and round it to the nearest whole fraction of 24 hours.If no units are specified, assume seconds.
+     * Parse an interval of the form xxhyymzzs and round it to the nearest whole fraction of 24 hours.If no units are
+     * specified, assume seconds.
      */
     public static long parseInterval(String interval, int def) {
         try {
             Matcher m = Pattern.compile("(?:(\\d+)[Hh])?(?:(\\d+)[Mm])?(?:(\\d+)[Ss]?)?").matcher(interval);
             if (m.matches()) {
-                int dur = 0;
-                String x = m.group(1);
-                if (x != null) {
-                    dur += 3600 * Integer.parseInt(x);
-                }
-                x = m.group(2);
-                if (x != null) {
-                    dur += 60 * Integer.parseInt(x);
-                }
-                x = m.group(3);
-                if (x != null) {
-                    dur += Integer.parseInt(x);
-                }
-                if (dur < 60) {
-                    dur = 60;
-                }
+                int dur = getDur(m);
                 int best = 86400;
                 int dist = best - dur;
                 if (dur > best) {
                     dist = dur - best;
                 }
-                int base = 1;
-                for (int i = 0; i < 8; i++) {
-                    int base2 = base;
-                    base *= 2;
-                    for (int j = 0; j < 4; j++) {
-                        int base3 = base2;
-                        base2 *= 3;
-                        for (int k = 0; k < 3; k++) {
-                            int cur = base3;
-                            base3 *= 5;
-                            int ndist = cur - dur;
-                            if (dur > cur) {
-                                ndist = dur - cur;
-                            }
-                            if (ndist < dist) {
-                                best = cur;
-                                dist = ndist;
-                            }
-                        }
-                    }
-                }
+                best = getBest(dur, best, dist);
                 def = best * 1000;
             }
         } catch (Exception e) {
-            eelfLogger.error("Exception", e);
+            eelfLogger.error(EXCEPTION, e);
         }
         return (def);
     }
 
-    private synchronized void checkRoll(long now) throws IOException {
-        if (now >= nexttime) {
-            if (os != null) {
-                os.close();
-                os = null;
+    private static int getBest(int dur, int best, int dist) {
+        int base = 1;
+        for (int i = 0; i < 8; i++) {
+            int base2 = base;
+            base *= 2;
+            for (int j = 0; j < 4; j++) {
+                int base3 = base2;
+                base2 *= 3;
+                for (int k = 0; k < 3; k++) {
+                    int cur = base3;
+                    base3 *= 5;
+                    int ndist = cur - dur;
+                    if (dur > cur) {
+                        ndist = dur - cur;
+                    }
+                    if (ndist < dist) {
+                        best = cur;
+                        dist = ndist;
+                    }
+                }
             }
-            intvl = parseInterval(config.getEventLogInterval(), 300000);
-            prefix = config.getEventLogPrefix();
-            suffix = config.getEventLogSuffix();
-            nexttime = now - now % intvl + intvl;
-            curfile = prefix + filedate.format(new Date(nexttime - intvl)) + suffix;
-            plainfile = prefix + suffix;
-            notify();
         }
+        return best;
+    }
+
+    private static int getDur(Matcher m) {
+        int dur = 0;
+        String x = m.group(1);
+        if (x != null) {
+            dur += 3600 * Integer.parseInt(x);
+        }
+        x = m.group(2);
+        if (x != null) {
+            dur += 60 * Integer.parseInt(x);
+        }
+        x = m.group(3);
+        if (x != null) {
+            dur += Integer.parseInt(x);
+        }
+        if (dur < 60) {
+            dur = 60;
+        }
+        return dur;
     }
 
     /**
@@ -138,111 +135,107 @@ public class StatusLog {
         try {
             instance.checkRoll(System.currentTimeMillis());
         } catch (Exception e) {
-            eelfLogger.error("Exception", e);
+            eelfLogger.error(EXCEPTION, e);
         }
         return (instance.curfile);
     }
 
-    private synchronized void log(String s) {
-        try {
-            long now = System.currentTimeMillis();
-            checkRoll(now);
-            if (os == null) {
-                os = new FileOutputStream(curfile, true);
-                (new File(plainfile)).delete();
-                Files.createLink(Paths.get(plainfile), Paths.get(curfile));
-            }
-            os.write((NodeUtils.logts(new Date(now)) + '|' + s + '\n').getBytes());
-            os.flush();
-        } catch (IOException ioe) {
-            eelfLogger.error("IOException", ioe);
-        }
-    }
-
     /**
      * Log a received publication attempt.
      *
-     * @param pubid  The publish ID assigned by the node
+     * @param pubid The publish ID assigned by the node
      * @param feedid The feed id given by the publisher
      * @param requrl The URL of the received request
      * @param method The method (DELETE or PUT) in the received request
-     * @param ctype  The content type (if method is PUT and clen > 0)
-     * @param clen   The content length (if method is PUT)
-     * @param srcip  The IP address of the publisher
-     * @param user   The identity of the publisher
+     * @param ctype The content type (if method is PUT and clen > 0)
+     * @param clen The content length (if method is PUT)
+     * @param srcip The IP address of the publisher
+     * @param user The identity of the publisher
      * @param status The status returned to the publisher
      */
-    public static void logPub(String pubid, String feedid, String requrl, String method, String ctype, long clen, String srcip, String user, int status) {
-        instance.log("PUB|" + pubid + "|" + feedid + "|" + requrl + "|" + method + "|" + ctype + "|" + clen + "|" + srcip + "|" + user + "|" + status);
+    public static void logPub(String pubid, String feedid, String requrl, String method, String ctype, long clen,
+            String srcip, String user, int status) {
+        instance.log(
+                "PUB|" + pubid + "|" + feedid + "|" + requrl + "|" + method + "|" + ctype + "|" + clen + "|" + srcip
+                        + "|" + user + "|" + status);
     }
 
     /**
      * Log a data transfer error receiving a publication attempt
      *
-     * @param pubid  The publish ID assigned by the node
+     * @param pubid The publish ID assigned by the node
      * @param feedid The feed id given by the publisher
      * @param requrl The URL of the received request
      * @param method The method (DELETE or PUT) in the received request
-     * @param ctype  The content type (if method is PUT and clen > 0)
-     * @param clen   The expected content length (if method is PUT)
-     * @param rcvd   The content length received
-     * @param srcip  The IP address of the publisher
-     * @param user   The identity of the publisher
-     * @param error  The error message from the IO exception
+     * @param ctype The content type (if method is PUT and clen > 0)
+     * @param clen The expected content length (if method is PUT)
+     * @param rcvd The content length received
+     * @param srcip The IP address of the publisher
+     * @param user The identity of the publisher
+     * @param error The error message from the IO exception
      */
-    public static void logPubFail(String pubid, String feedid, String requrl, String method, String ctype, long clen, long rcvd, String srcip, String user, String error) {
-        instance.log("PBF|" + pubid + "|" + feedid + "|" + requrl + "|" + method + "|" + ctype + "|" + clen + "|" + rcvd + "|" + srcip + "|" + user + "|" + error);
+    public static void logPubFail(String pubid, String feedid, String requrl, String method, String ctype, long clen,
+            long rcvd, String srcip, String user, String error) {
+        instance.log("PBF|" + pubid + "|" + feedid + "|" + requrl + "|" + method + "|" + ctype + "|" + clen + "|" + rcvd
+                + "|" + srcip + "|" + user + "|" + error);
     }
 
     /**
      * Log a delivery attempt.
      *
-     * @param pubid  The publish ID assigned by the node
+     * @param pubid The publish ID assigned by the node
      * @param feedid The feed ID
-     * @param subid  The (space delimited list of) subscription ID
+     * @param subid The (space delimited list of) subscription ID
      * @param requrl The URL used in the attempt
      * @param method The method (DELETE or PUT) in the attempt
-     * @param ctype  The content type (if method is PUT, not metaonly, and clen > 0)
-     * @param clen   The content length (if PUT and not metaonly)
-     * @param user   The identity given to the subscriber
+     * @param ctype The content type (if method is PUT, not metaonly, and clen > 0)
+     * @param clen The content length (if PUT and not metaonly)
+     * @param user The identity given to the subscriber
      * @param status The status returned by the subscriber or -1 if an exeception occured trying to connect
      * @param xpubid The publish ID returned by the subscriber
      */
-    public static void logDel(String pubid, String feedid, String subid, String requrl, String method, String ctype, long clen, String user, int status, String xpubid) {
+    public static void logDel(String pubid, String feedid, String subid, String requrl, String method, String ctype,
+            long clen, String user, int status, String xpubid) {
         if (feedid == null) {
             return;
         }
-        instance.log("DEL|" + pubid + "|" + feedid + "|" + subid + "|" + requrl + "|" + method + "|" + ctype + "|" + clen + "|" + user + "|" + status + "|" + xpubid);
+        instance.log(
+                "DEL|" + pubid + "|" + feedid + "|" + subid + "|" + requrl + "|" + method + "|" + ctype + "|" + clen
+                        + "|" + user + "|" + status + "|" + xpubid);
     }
 
     /**
      * Log delivery attempts expired
      *
-     * @param pubid    The publish ID assigned by the node
-     * @param feedid   The feed ID
-     * @param subid    The (space delimited list of) subscription ID
-     * @param requrl   The URL that would be delivered to
-     * @param method   The method (DELETE or PUT) in the request
-     * @param ctype    The content type (if method is PUT, not metaonly, and clen > 0)
-     * @param clen     The content length (if PUT and not metaonly)
-     * @param reason   The reason the attempts were discontinued
+     * @param pubid The publish ID assigned by the node
+     * @param feedid The feed ID
+     * @param subid The (space delimited list of) subscription ID
+     * @param requrl The URL that would be delivered to
+     * @param method The method (DELETE or PUT) in the request
+     * @param ctype The content type (if method is PUT, not metaonly, and clen > 0)
+     * @param clen The content length (if PUT and not metaonly)
+     * @param reason The reason the attempts were discontinued
      * @param attempts The number of attempts made
      */
-    public static void logExp(String pubid, String feedid, String subid, String requrl, String method, String ctype, long clen, String reason, int attempts) {
+    public static void logExp(String pubid, String feedid, String subid, String requrl, String method, String ctype,
+            long clen, String reason, int attempts) {
         if (feedid == null) {
             return;
         }
-        instance.log("EXP|" + pubid + "|" + feedid + "|" + subid + "|" + requrl + "|" + method + "|" + ctype + "|" + clen + "|" + reason + "|" + attempts);
+        instance.log(
+                "EXP|" + pubid + "|" + feedid + "|" + subid + "|" + requrl + "|" + method + "|" + ctype + "|" + clen
+                        + "|" + reason + "|" + attempts);
     }
 
     /**
      * Log extra statistics about unsuccessful delivery attempts.
      *
-     * @param pubid  The publish ID assigned by the node
+     * @param pubid The publish ID assigned by the node
      * @param feedid The feed ID
-     * @param subid  The (space delimited list of) subscription ID
-     * @param clen   The content length
-     * @param sent   The # of bytes sent or -1 if subscriber returned an error instead of 100 Continue, otherwise, the number of bytes sent before an error occurred.
+     * @param subid The (space delimited list of) subscription ID
+     * @param clen The content length
+     * @param sent The # of bytes sent or -1 if subscriber returned an error instead of 100 Continue, otherwise, the
+     * number of bytes sent before an error occurred.
      */
     public static void logDelExtra(String pubid, String feedid, String subid, long clen, long sent) {
         if (feedid == null) {
@@ -251,6 +244,35 @@ public class StatusLog {
         instance.log("DLX|" + pubid + "|" + feedid + "|" + subid + "|" + clen + "|" + sent);
     }
 
-    private StatusLog() {
+    private synchronized void checkRoll(long now) throws IOException {
+        if (now >= nexttime) {
+            if (os != null) {
+                os.close();
+                os = null;
+            }
+            intvl = parseInterval(config.getEventLogInterval(), 300000);
+            prefix = config.getEventLogPrefix();
+            suffix = config.getEventLogSuffix();
+            nexttime = now - now % intvl + intvl;
+            curfile = prefix + filedate.format(new Date(nexttime - intvl)) + suffix;
+            plainfile = prefix + suffix;
+            notify();
+        }
+    }
+
+    private synchronized void log(String s) {
+        try {
+            long now = System.currentTimeMillis();
+            checkRoll(now);
+            if (os == null) {
+                os = new FileOutputStream(curfile, true);
+                (new File(plainfile)).delete();
+                Files.createLink(Paths.get(plainfile), Paths.get(curfile));
+            }
+            os.write((NodeUtils.logts(new Date(now)) + '|' + s + '\n').getBytes());
+            os.flush();
+        } catch (IOException ioe) {
+            eelfLogger.error("IOException", ioe);
+        }
     }
 }