[SEBA-719] Display counters in onos cli and reset counters

Change-Id: Ia9fc1865d13a366c9a7cd3e7ae11c39dcc284fd9
diff --git a/api/src/main/java/org/opencord/aaa/AaaStatistics.java b/api/src/main/java/org/opencord/aaa/AaaStatistics.java
index 9764427..921de5f 100644
--- a/api/src/main/java/org/opencord/aaa/AaaStatistics.java
+++ b/api/src/main/java/org/opencord/aaa/AaaStatistics.java
@@ -16,36 +16,61 @@
 
 package org.opencord.aaa;
 
+import java.util.LinkedList;
 import java.util.concurrent.atomic.AtomicLong;
 
 public class AaaStatistics {
-    //Number of access accept packets sent to the server
+    // Number of access accept packets sent to the server
     private AtomicLong acceptResponsesRx = new AtomicLong();
-    //Number of access reject packets sent to the server
+    // Number of access reject packets sent to the server
     private AtomicLong rejectResponsesRx = new AtomicLong();
-    //Number of access challenge packets sent to the server
+    // Number of access challenge packets sent to the server
     private AtomicLong challengeResponsesRx = new AtomicLong();
-    //Number of access request packets sent to the server
+    // Number of access request packets sent to the server
     private AtomicLong accessRequestsTx = new AtomicLong();
-    //Number of access request packets pending a response from the server
+    // Number of access request packets pending a response from the server
     private AtomicLong pendingRequests = new AtomicLong();
-    //Number of packets of an unknown RADIUS type received from the accounting server
+    // Number of packets of an unknown RADIUS type received from the accounting
+    // server
     private AtomicLong unknownTypeRx = new AtomicLong();
-    //Number of access response packets received from the server with an invalid validator
+    // Number of access response packets received from the server with an invalid
+    // validator
     private AtomicLong invalidValidatorsRx = new AtomicLong();
-    //Number of dropped packets received from the accounting server
+    // Number of dropped packets received from the accounting server
     private AtomicLong droppedResponsesRx = new AtomicLong();
-    //Number of malformed access response packets received from the server
+    // Number of malformed access response packets received from the server
     private AtomicLong malformedResponsesRx = new AtomicLong();
-    //Number of packets received from an unknown server
+    // Number of packets received from an unknown server
     private AtomicLong unknownServerRx = new AtomicLong();
-    //Roundtrip packet time to the accounting server
+    // Roundtrip packet time to the accounting server
     private AtomicLong requestRttMilis = new AtomicLong();
-    //Number of access request packets retransmitted to the server
+    // Number of access request packets retransmitted to the server
     private AtomicLong requestReTx = new AtomicLong();
-    //Number of sessions expired
+    // Number of sessions expired
     private AtomicLong numberOfSessionsExpired = new AtomicLong();
 
+    private LinkedList<Long> packetRoundTripTimeList = new LinkedList<Long>();
+
+    public LinkedList<Long> getPacketRoundTripTimeList() {
+        return packetRoundTripTimeList;
+    }
+
+    public int getPacketRoundTripTimeListSize() {
+        return packetRoundTripTimeList.size();
+    }
+
+    public void clearPacketRoundTripTimeList() {
+        packetRoundTripTimeList.clear();
+    }
+
+    public void getPacketRoundTripTimeListRemoveFirst() {
+        packetRoundTripTimeList.removeFirst();
+    }
+
+    public void getPacketRoundTripTimeListAdd(long time) {
+        packetRoundTripTimeList.add(time);
+    }
+
     public Long getRequestReTx() {
         return requestReTx.get();
     }
@@ -153,4 +178,21 @@
         numberOfDroppedPackets += numberOfSessionsExpired.get();
         this.droppedResponsesRx = new AtomicLong(numberOfDroppedPackets);
     }
+
+    public void resetAllCounters() {
+        clearPacketRoundTripTimeList();
+
+        accessRequestsTx.set(0);
+        acceptResponsesRx.set(0);
+        challengeResponsesRx.set(0);
+        droppedResponsesRx.set(0);
+        invalidValidatorsRx.set(0);
+        malformedResponsesRx.set(0);
+        pendingRequests.set(0);
+        rejectResponsesRx.set(0);
+        requestReTx.set(0);
+        requestRttMilis.set(0);
+        unknownServerRx.set(0);
+        unknownTypeRx.set(0);
+    }
 }
diff --git a/api/src/main/java/org/opencord/aaa/AuthenticationStatisticsService.java b/api/src/main/java/org/opencord/aaa/AuthenticationStatisticsService.java
index bdb9f2d..37a2205 100644
--- a/api/src/main/java/org/opencord/aaa/AuthenticationStatisticsService.java
+++ b/api/src/main/java/org/opencord/aaa/AuthenticationStatisticsService.java
@@ -23,38 +23,42 @@
  */
 
 public interface AuthenticationStatisticsService extends
-        ListenerService<AuthenticationStatisticsEvent, AuthenticationStatisticsEventListener> {
+ListenerService<AuthenticationStatisticsEvent, AuthenticationStatisticsEventListener> {
 
     /**
      * Returns AaaStatistics object.
      *
      * @return AaaStatistics
-    */
+     */
     public AaaStatistics getAaaStats();
-
     /**
      * Returns AuthenticationStatisticsDelegate object.
      *
      * @return AuthenticationStatisticsDelegate
-    */
+     */
     public AuthenticationStatisticsDelegate getStatsDelegate();
 
     /**
      * Handle the roundTrip time of Radius Packet.
      *
      * @param identifier identifier of incoming radius packet
-    */
+     */
     public void handleRoundtripTime(byte identifier);
 
     /**
      * Calculate average roundTrip time of multiple Packets.
-    */
+     */
     public void calculatePacketRoundtripTime();
 
     /**
      * Put the identifier value to map.
      *
      * @param identifier identifier of incoming radius packet
-    */
+     */
     public void putOutgoingIdentifierToMap(byte identifier);
+
+   /**
+    * Reset all the values of aaa counters to 0.
+    */
+    public void resetAllCounters();
 }
diff --git a/app/src/main/java/org/opencord/aaa/impl/AaaResetCountersCommand.java b/app/src/main/java/org/opencord/aaa/impl/AaaResetCountersCommand.java
new file mode 100644
index 0000000..79ada3b
--- /dev/null
+++ b/app/src/main/java/org/opencord/aaa/impl/AaaResetCountersCommand.java
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2016-present Open Networking 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.
+ */
+package org.opencord.aaa.impl;
+
+import org.apache.karaf.shell.commands.Command;
+import org.onosproject.cli.AbstractShellCommand;
+import org.opencord.aaa.AuthenticationStatisticsService;
+
+/**
+ * Reset value of all aaa statistics counters to 0.
+ */
+@Command(scope = "onos", name = "reset-aaa-counters", description = "Reset value of all aaa statistics counters to 0 ")
+public class AaaResetCountersCommand extends AbstractShellCommand {
+
+    @Override
+    protected void execute() {
+        AuthenticationStatisticsService aaaStatisticsManager = AbstractShellCommand
+                .get(AuthenticationStatisticsService.class);
+        aaaStatisticsManager.resetAllCounters();
+    }
+}
diff --git a/app/src/main/java/org/opencord/aaa/impl/AaaShowCountersCommand.java b/app/src/main/java/org/opencord/aaa/impl/AaaShowCountersCommand.java
new file mode 100644
index 0000000..0717f9a
--- /dev/null
+++ b/app/src/main/java/org/opencord/aaa/impl/AaaShowCountersCommand.java
@@ -0,0 +1,52 @@
+/*
+ * Copyright 2016-present Open Networking 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.
+ */
+package org.opencord.aaa.impl;
+
+import org.apache.karaf.shell.commands.Command;
+import org.onosproject.cli.AbstractShellCommand;
+import org.opencord.aaa.AaaStatistics;
+import org.opencord.aaa.AuthenticationStatisticsService;
+
+/**
+ * Display current value of all aaa statistics counters.
+ */
+@Command(scope = "onos", name = "show-aaa-counters",
+description = "Display current value of all aaa statistics counters")
+public class AaaShowCountersCommand extends AbstractShellCommand {
+    @Override
+    protected void execute() {
+
+        AaaStatistics aaaStats = new AaaStatistics();
+
+        AuthenticationStatisticsService aaaStatisticsManager =
+                AbstractShellCommand.get(AuthenticationStatisticsService.class);
+        aaaStats = aaaStatisticsManager.getAaaStats();
+
+        System.out.format("%30s %10d\n", "AccessRequestsTx", aaaStats.getAccessRequestsTx());
+        System.out.format("%30s %10d\n", "ChallengeResponsesRx", aaaStats.getChallengeResponsesRx());
+        System.out.format("%30s %10d\n", "RequestReTx", aaaStats.getRequestReTx());
+        System.out.format("%30s %10d\n", "AcceptResponsesRx", aaaStats.getAcceptResponsesRx());
+        System.out.format("%30s %10d\n", "RejectResponsesRx", aaaStats.getRejectResponsesRx());
+        System.out.format("%30s %10d\n", "PendingRequests", aaaStats.getPendingRequests());
+        System.out.format("%30s %10d\n", "DroppedResponsesRx", aaaStats.getDroppedResponsesRx());
+        System.out.format("%30s %10d\n", "InvalidValidatorsRx", aaaStats.getInvalidValidatorsRx());
+        System.out.format("%30s %10d\n", "MalformedResponsesRx", aaaStats.getMalformedResponsesRx());
+        System.out.format("%30s %10d\n", "UnknownServerRx", aaaStats.getUnknownServerRx());
+        System.out.format("%30s %10d\n", "UnknownTypeRx", aaaStats.getUnknownTypeRx());
+        System.out.format("%30s %10d\n", "RequestRttMillis", aaaStats.getRequestRttMilis());
+
+  }
+}
diff --git a/app/src/main/java/org/opencord/aaa/impl/AaaStatisticsManager.java b/app/src/main/java/org/opencord/aaa/impl/AaaStatisticsManager.java
index b28d53e..31aad5b 100644
--- a/app/src/main/java/org/opencord/aaa/impl/AaaStatisticsManager.java
+++ b/app/src/main/java/org/opencord/aaa/impl/AaaStatisticsManager.java
@@ -19,7 +19,6 @@
 import static org.slf4j.LoggerFactory.getLogger;
 
 import java.util.HashMap;
-import java.util.LinkedList;
 import java.util.Map;
 import java.util.concurrent.atomic.AtomicLong;
 
@@ -39,21 +38,22 @@
 @Service
 @Component(immediate = true)
 public class AaaStatisticsManager
-        extends AbstractListenerManager<AuthenticationStatisticsEvent, AuthenticationStatisticsEventListener>
-        implements AuthenticationStatisticsService {
+extends AbstractListenerManager<AuthenticationStatisticsEvent, AuthenticationStatisticsEventListener>
+implements AuthenticationStatisticsService {
 
     private AuthenticationStatisticsDelegate statsDelegate;
 
+    @Override
     public AuthenticationStatisticsDelegate getStatsDelegate() {
         return statsDelegate;
     }
 
     private final Logger log = getLogger(getClass());
     private AaaStatistics aaaStats;
-    private LinkedList<Long> packetRoundTripTimeList = new LinkedList<Long>();
     public Map<Byte, Long> outgoingPacketMap = new HashMap<Byte, Long>();
     private static final int PACKET_COUNT_FOR_AVERAGE_RTT_CALCULATION = 5;
 
+    @Override
     public AaaStatistics getAaaStats() {
         return aaaStats;
     }
@@ -71,23 +71,31 @@
         eventDispatcher.removeSink(AuthenticationStatisticsEvent.class);
     }
 
+    @Override
     public void handleRoundtripTime(byte inPacketIdentifier) {
         long inTimeInMilis = System.currentTimeMillis();
         if (outgoingPacketMap.containsKey(inPacketIdentifier)) {
-            if (packetRoundTripTimeList.size() > PACKET_COUNT_FOR_AVERAGE_RTT_CALCULATION) {
-                packetRoundTripTimeList.removeFirst();
+            if (aaaStats.getPacketRoundTripTimeListSize() > PACKET_COUNT_FOR_AVERAGE_RTT_CALCULATION) {
+                aaaStats.getPacketRoundTripTimeListRemoveFirst();
             }
-            packetRoundTripTimeList.add(inTimeInMilis - outgoingPacketMap.get(inPacketIdentifier));
+            aaaStats.getPacketRoundTripTimeListAdd(inTimeInMilis - outgoingPacketMap.get(inPacketIdentifier));
         }
     }
 
+    @Override
+    public void resetAllCounters() {
+        aaaStats.resetAllCounters();
+    }
+
+    @Override
     public void calculatePacketRoundtripTime() {
-        if (packetRoundTripTimeList.size() > 0) {
-            long avg = (long) packetRoundTripTimeList.stream().mapToLong(i -> i).average().getAsDouble();
+        if (aaaStats.getPacketRoundTripTimeListSize() > 0) {
+            long avg = (long) aaaStats.getPacketRoundTripTimeList().stream().mapToLong(i -> i).average().getAsDouble();
             aaaStats.setRequestRttMilis(new AtomicLong(avg));
         }
     }
 
+    @Override
     public void putOutgoingIdentifierToMap(byte outPacketIdentifier) {
         outgoingPacketMap.put(outPacketIdentifier, System.currentTimeMillis());
     }
diff --git a/app/src/main/resources/OSGI-INF/blueprint/shell-config.xml b/app/src/main/resources/OSGI-INF/blueprint/shell-config.xml
index b83f750..d9c9d24 100644
--- a/app/src/main/resources/OSGI-INF/blueprint/shell-config.xml
+++ b/app/src/main/resources/OSGI-INF/blueprint/shell-config.xml
@@ -1,5 +1,5 @@
 <!--
-  ~ Copyright 2016-present Open Networking Foundation
+  ~Copyright 2016-present Open Networking Foundation
   ~
   ~ Licensed under the Apache License, Version 2.0 (the "License");
   ~ you may not use this file except in compliance with the License.
@@ -22,5 +22,11 @@
         <command>
             <action class="org.opencord.aaa.impl.AaaResetDeviceCommand"/>
         </command>
+        <command>
+            <action class="org.opencord.aaa.impl.AaaResetCountersCommand"/>
+        </command>
+        <command>
+            <action class="org.opencord.aaa.impl.AaaShowCountersCommand"/>
+        </command>
     </command-bundle>
 </blueprint>