[SEBA-935] Including session timeout scenario of authentication session level statistics

Change-Id: Ibed87f4d028104f79ab5337ff369277a5882b266
diff --git a/app/src/main/java/org/opencord/aaa/impl/AaaManager.java b/app/src/main/java/org/opencord/aaa/impl/AaaManager.java
index 2ba48ae..ee339e4 100644
--- a/app/src/main/java/org/opencord/aaa/impl/AaaManager.java
+++ b/app/src/main/java/org/opencord/aaa/impl/AaaManager.java
@@ -951,6 +951,14 @@
     private void handleStateMachineTimeout(ConnectPoint supplicantConnectPoint) {
         StateMachine stateMachine = stateMachines.remove(sessionId(supplicantConnectPoint));
 
+        //pushing captured machine stats to kafka
+        stateMachine.setSessionTerminateReason("Time out");
+        AaaSupplicantMachineStats obj = aaaSupplicantStatsManager
+                .getSupplicantStats(stateMachine);
+        aaaSupplicantStatsManager.getMachineStatsDelegate()
+                .notify(new AaaMachineStatisticsEvent(
+                        AaaMachineStatisticsEvent.Type.STATS_UPDATE, obj));
+
         if (stateMachine.state() == StateMachine.STATE_PENDING && stateMachine.isWaitingForRadiusResponse()) {
             aaaStatisticsManager.getAaaStats().increaseTimedOutPackets();
         }
diff --git a/app/src/test/java/org/opencord/aaa/impl/AaaStatisticsTest.java b/app/src/test/java/org/opencord/aaa/impl/AaaStatisticsTest.java
index 93245ae..573e4de 100644
--- a/app/src/test/java/org/opencord/aaa/impl/AaaStatisticsTest.java
+++ b/app/src/test/java/org/opencord/aaa/impl/AaaStatisticsTest.java
@@ -509,6 +509,55 @@
 
     }
 
+
+    /** Tests the authentication path through the AAA application.
+     *  And counts the aaa Stats for timeout.
+     *   @throws DeserializationException
+     *  if packed deserialization fails.
+     */
+    @Test
+    public void testAaaStatisticsForTimeoutPackets() throws Exception {
+
+        // (1) Supplicant start up
+        Ethernet startPacket = constructSupplicantStartPacket();
+        sendPacket(startPacket);
+
+        Ethernet responsePacket = (Ethernet) fetchPacket(0);
+        checkRadiusPacket(aaaManager, responsePacket, EAP.ATTR_IDENTITY);
+
+        // (2) Supplicant identify
+
+        Ethernet identifyPacket = constructSupplicantIdentifyPacket(null, EAP.ATTR_IDENTITY, (byte) 1, null);
+        sendPacket(identifyPacket);
+
+        RADIUS radiusIdentifyPacket = (RADIUS) fetchPacket(1);
+        checkRadiusPacketFromSupplicant(radiusIdentifyPacket);
+
+        assertThat(radiusIdentifyPacket.getCode(), is(RADIUS.RADIUS_CODE_ACCESS_REQUEST));
+        assertThat(new String(radiusIdentifyPacket.getAttribute(RADIUSAttribute.RADIUS_ATTR_USERNAME).getValue()),
+                is("testuser"));
+        IpAddress nasIp = IpAddress.valueOf(IpAddress.Version.INET,
+                  radiusIdentifyPacket.getAttribute(RADIUSAttribute.RADIUS_ATTR_NAS_IP).getValue());
+        assertThat(nasIp.toString(), is(aaaManager.nasIpAddress.getHostAddress()));
+
+        // State machine should have been created by now
+
+        StateMachine stateMachine = aaaManager.getStateMachine(SESSION_ID);
+        assertThat(stateMachine, notNullValue());
+        assertThat(stateMachine.state(), is(StateMachine.STATE_PENDING));
+        Thread.sleep((aaaManager.cleanupTimerTimeOutInMins / 2) + 1);
+
+        // State machine should be in timeout state
+        assertThat(stateMachine, notNullValue());
+        assertThat(stateMachine.state(), is(StateMachine.STATE_PENDING));
+
+        //Check for increase in stats
+        assertNotEquals(aaaStatisticsManager.getAaaStats().getEapolResIdentityMsgTrans(), ZERO);
+        assertNotEquals(aaaStatisticsManager.getAaaStats().getEapolStartReqTrans(), ZERO);
+       countAaaStatistics();
+
+    }
+
     // Calculates the AAA statistics count.
     public void countAaaStatistics() {
         assertThat(aaaStatisticsManager.getAaaStats().getAcceptResponsesRx(), notNullValue());