Remove trailing whitespace
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / statistics / StatisticsCounters.java
index 34f072b0839abbc6664625957d5b689d51854b6b..1f4d7ca34b4669c4d9b752271ddb2701ac7a9461 100644 (file)
@@ -12,6 +12,7 @@ import java.util.Timer;
 import java.util.TimerTask;
 import java.util.concurrent.ConcurrentHashMap;
 
+import org.opendaylight.openflowjava.protocol.spi.statistics.StatisticsHandler;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -20,7 +21,7 @@ import org.slf4j.LoggerFactory;
  * @author madamjak
  *
  */
-public final class StatisticsCounters {
+public final class StatisticsCounters implements StatisticsHandler {
 
     /**
      * Default delay between two writings into log (milliseconds)
@@ -40,14 +41,14 @@ public final class StatisticsCounters {
     private boolean runCounting;
     // array to hold enabled counter types
     private CounterEventTypes[] enabledCounters = {
-                    CounterEventTypes.DS_ENCODE_FAIL, 
+                    CounterEventTypes.DS_ENCODE_FAIL,
                     CounterEventTypes.DS_ENCODE_SUCCESS,
                     CounterEventTypes.DS_ENTERED_OFJAVA,
                     CounterEventTypes.DS_FLOW_MODS_ENTERED,
                     CounterEventTypes.DS_FLOW_MODS_SENT,
-                    CounterEventTypes.US_DECODE_FAIL, 
-                    CounterEventTypes.US_DECODE_SUCCESS, 
-                    CounterEventTypes.US_MESSAGE_PASS, 
+                    CounterEventTypes.US_DECODE_FAIL,
+                    CounterEventTypes.US_DECODE_SUCCESS,
+                    CounterEventTypes.US_MESSAGE_PASS,
                     CounterEventTypes.US_RECEIVED_IN_OFJAVA};
 
     /**
@@ -67,43 +68,28 @@ public final class StatisticsCounters {
             countersMap.put(cet, new Counter());
         }
         runCounting = false;
-        this.logReportPeriod = -1;
+        this.logReportPeriod = 0;
         this.runLogReport = false;
         LOGGER.debug("StaticsCounters has been created");
     }
 
     /**
-     * Start counting
-     * @param resetCounters - true = statistics counters will be reset before start counting
-     * @param reportToLogs - true = statistics counters will periodically write to log
-     * @param logReportDelay - delay between two writings into logs in milliseconds (for details see startLogReport(int logReportDelay))
+     * Start counting (counters are set to 0 before counting starts)
+     * @param reportToLogs - true = statistic counters will periodically log
+     * @param logReportDelay - delay between two logs (in milliseconds)
      */
-    public void startCounting(boolean resetCounters, boolean reportToLogs, int logReportDelay){
+    public void startCounting(boolean reportToLogs, int logReportDelay){
         if (runCounting) {
             return;
         }
-        LOGGER.debug("Start counting...");
+        resetCounters();
+        LOGGER.debug("Counting started...");
         if(reportToLogs){
             startLogReport(logReportDelay);
         }
-        if(resetCounters){
-            resetCounters();
-        }
         runCounting = true;
     }
 
-    /**
-     * Start counting (counters are set to 0 before start counting)
-     * @param reportToLogs - true = statistics counters will periodically write to log
-     * @param logReportDelay - delay between two writings into logs in milliseconds (for details see startLogReport(int logReportDelay))
-     */
-    public void startCounting(boolean reportToLogs, int logReportDelay){
-        if (runCounting) {
-            return;
-        }
-        startCounting(true,reportToLogs,logReportDelay);
-    }
-
     /**
      * Stop counting, values in counters are untouched, log reporter is stopped
      */
@@ -122,49 +108,30 @@ public final class StatisticsCounters {
     }
 
     /**
-     * Start write statistics into logs, if writing is run calling has no effect. 
-     * If method is called without previous setting of report delay than DEFAULT_LOG_REPORT_PERIOD will be used.
-     */
-    public void startLogReport(){
-        if(runLogReport){
-            return;
-        }
-        if(this.logReportPeriod <= 0){
-            this.logReportPeriod = DEFAULT_LOG_REPORT_PERIOD;
-        }
-        if(this.logReportPeriod <= MINIMAL_LOG_REPORT_PERIOD){
-            this.logReportPeriod = MINIMAL_LOG_REPORT_PERIOD;
-        }
-        logReporter = new Timer("SC_Timer");
-        logReporter.schedule(new LogReporterTask(this), this.logReportPeriod,this.logReportPeriod);
-        runLogReport = true;
-        LOGGER.debug("Statistics log reporter has been scheduled with period {} ms", this.logReportPeriod);
-    }
-
-    /**
-     * Start write statistics into logs with given delay between writings, if writing is run calling has no effect.
-     * @param logReportDelay - delay between two writings into logs (milliseconds). 
-     *            It is mandatory if reportToLogs is true, value have to be greater than 0 (zero)
-     *            If value is smaller than MINIMAL_LOG_REPORT_PERIOD, the value MINIMAL_LOG_REPORT_PERIOD will be used.
-     * @exception IllegalArgumentException if logReportDelay is not greater than 0 (zero)
+     * Prints statistics with given delay between logs
+     * @param logReportDelay - delay between two logs (in milliseconds)
+     * @exception IllegalArgumentException if logReportDelay is less than 0
      */
     public void startLogReport(int logReportDelay){
         if(runLogReport){
             return;
         }
         if(logReportDelay <= 0){
-            throw new IllegalArgumentException("logReportPeriod have to bee greater than 0 zero");
+            throw new IllegalArgumentException("logReportDelay has to be greater than 0");
         }
         if(logReportDelay < MINIMAL_LOG_REPORT_PERIOD){
             this.logReportPeriod = MINIMAL_LOG_REPORT_PERIOD;
         } else {
             this.logReportPeriod = logReportDelay;
         }
-        startLogReport();
+        logReporter = new Timer("SC_Timer");
+        logReporter.schedule(new LogReporterTask(this), this.logReportPeriod, this.logReportPeriod);
+        runLogReport = true;
+        LOGGER.debug("Statistics log reporter has been scheduled with period {} ms", this.logReportPeriod);
     }
 
     /**
-     * Stop  write statistics into logs, counting does not stop
+     * Stops logging, counting continues
      */
     public void stopLogReport(){
         if(runLogReport){
@@ -217,7 +184,7 @@ public final class StatisticsCounters {
     }
 
     /**
-     * Get counter by counter event type
+     * Get counter by CounterEventType
      * @param counterEventKey key to identify counter (can not be null)
      * @return - Counter object or null if counter has not been enabled
      * @exception - IllegalArgumentException if counterEventKey is null
@@ -241,9 +208,7 @@ public final class StatisticsCounters {
         }
     }
 
-    /**
-     * Set values of all counter to 0 (zero)
-     */
+    @Override
     public void resetCounters() {
         for(CounterEventTypes cet : enabledCounters){
             countersMap.get(cet).reset();
@@ -251,6 +216,15 @@ public final class StatisticsCounters {
         LOGGER.debug("StaticsCounters has been reset");
     }
 
+    @Override
+    public String printStatistics() {
+        StringBuilder strBuilder = new StringBuilder();
+        for(CounterEventTypes cet : getEnabledCounters()){
+            strBuilder.append(cet.name() + ": " + getCountersMap().get(cet).getStat() + "\n");
+        }
+        return strBuilder.toString();
+    }
+
     /**
      * internal class to process logReporter
      * @author madamjak
@@ -266,9 +240,9 @@ public final class StatisticsCounters {
 
         @Override
         public void run() {
-                for(CounterEventTypes cet : sc.getEnabledCounters()){
-                    LOG.debug(cet.toString() + ": " + sc.getCountersMap().get(cet).toString());
-                }
+            for(CounterEventTypes cet : sc.getEnabledCounters()){
+                LOG.debug(cet.name() + ": " + sc.getCountersMap().get(cet).getStat());
+            }
         }
     }
-}
+}
\ No newline at end of file