Better logs and stats reporting

Change-Id: I1ef0d75398c8e0ff3fa3ac6a140c24d0394945cb
diff --git a/app/src/main/java/org/opencord/aaa/cli/AaaShowCountersCommand.java b/app/src/main/java/org/opencord/aaa/cli/AaaShowCountersCommand.java
index 3c08464..42e0c17 100644
--- a/app/src/main/java/org/opencord/aaa/cli/AaaShowCountersCommand.java
+++ b/app/src/main/java/org/opencord/aaa/cli/AaaShowCountersCommand.java
@@ -37,9 +37,15 @@
 
         AaaStatisticsSnapshot stats = aaaStatisticsManager.getClusterStatistics();
 
-        for (String name : AaaStatistics.COUNTER_NAMES) {
+        print("-------------------------- Expected transitions ----------------------------");
+        for (String name : AaaStatistics.EAPOL_SM_NAMES) {
+            print("%30s %10d", name, stats.get(name));
+        }
+
+        print("-------------------------------- Other stats ----------------------------------");
+        for (String name : AaaStatistics.EAPOL_STATS_NAMES) {
             print("%30s %10d", name, stats.get(name));
         }
 
     }
-}
+}
\ No newline at end of file
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 2f1a7e5..660ad33 100644
--- a/app/src/main/java/org/opencord/aaa/impl/AaaManager.java
+++ b/app/src/main/java/org/opencord/aaa/impl/AaaManager.java
@@ -25,6 +25,7 @@
 import static org.slf4j.LoggerFactory.getLogger;
 
 import com.google.common.collect.Sets;
+
 import java.net.InetAddress;
 import java.net.UnknownHostException;
 import java.nio.ByteBuffer;
@@ -235,8 +236,8 @@
     // Configuration properties factory
     private final ConfigFactory factory =
             new ConfigFactory<ApplicationId, AaaConfig>(APP_SUBJECT_FACTORY,
-                                                         AaaConfig.class,
-                                                         "AAA") {
+                                                        AaaConfig.class,
+                                                        "AAA") {
                 @Override
                 public AaaConfig createConfig() {
                     return new AaaConfig();
@@ -252,6 +253,7 @@
 
     protected ExecutorService packetProcessorExecutor;
     protected ScheduledExecutorService serverStatusAndStateMachineTimeoutExecutor;
+
     /**
      * Builds an EAPOL packet based on the given parameters.
      *
@@ -327,7 +329,7 @@
         getConfiguredAaaServerAddress();
         radiusOperationalStatusService.initialize(nasIpAddress.getAddress(), radiusSecret, impl);
         serverStatusAndStateMachineTimeoutExecutor = Executors.newScheduledThreadPool(STATE_MACHINE_THREADS,
-                           groupedThreads("onos/aaa", "aaa-machine-%d", log));
+                                              groupedThreads("onos/aaa", "aaa-machine-%d", log));
 
         scheduledStatusServerChecker = serverStatusAndStateMachineTimeoutExecutor.scheduleAtFixedRate(
                 new ServerStatusChecker(), 0,
@@ -353,6 +355,7 @@
 
         log.info("Stopped");
     }
+
     @Modified
     public void modified(ComponentContext context) {
         Dictionary<String, Object> properties = context.getProperties();
@@ -360,7 +363,7 @@
         String s = Tools.get(properties, "operationalStatusEventGenerationPeriodInSeconds");
         operationalStatusEventGenerationPeriodInSeconds = Strings.isNullOrEmpty(s)
                 ? OPERATIONAL_STATUS_SERVER_EVENT_GENERATION_DEFAULT
-                    : Integer.parseInt(s.trim());
+                : Integer.parseInt(s.trim());
 
         s = Tools.get(properties, "operationalStatusServerTimeoutInSeconds");
         operationalStatusServerTimeoutInSeconds = Strings.isNullOrEmpty(s) ? OPERATIONAL_STATUS_SERVER_TIMEOUT_DEFAULT
@@ -370,7 +373,7 @@
         String newEvaluationModeString = Strings.isNullOrEmpty(s) ? STATUS_SERVER_MODE_DEFAULT : s.trim();
 
         radiusOperationalStatusService
-            .setOperationalStatusServerTimeoutInMillis(operationalStatusServerTimeoutInSeconds * 1000);
+                .setOperationalStatusServerTimeoutInMillis(operationalStatusServerTimeoutInSeconds * 1000);
         RadiusOperationalStatusEvaluationMode newEvaluationMode =
                 RadiusOperationalStatusEvaluationMode.getValue(newEvaluationModeString);
         if (newEvaluationMode != null) {
@@ -388,8 +391,8 @@
             if (packetProcessorExecutor != null) {
                 packetProcessorExecutor.shutdown();
             }
-            packetProcessorExecutor = newSingleThreadExecutor(groupedThreads("onos/aaa", "aaa-packet-%d", log));
-
+            packetProcessorExecutor = newSingleThreadExecutor(
+                    groupedThreads("onos/aaa", "aaa-packet-%d", log));
         }
     }
 
@@ -398,7 +401,7 @@
             impl = new SocketBasedRadiusCommunicator(appId, packetService, this);
         } else {
             impl = new PortBasedRadiusCommunicator(appId, packetService, mastershipService,
-                    deviceService, subsService, pktCustomizer, this);
+                                                   deviceService, subsService, pktCustomizer, this);
         }
     }
 
@@ -425,7 +428,7 @@
             if (newCfg.radiusHostName() != null) {
                 address = InetAddress.getByName(newCfg.radiusHostName());
             } else {
-                 address = newCfg.radiusIp();
+                address = newCfg.radiusIp();
             }
 
             configuredAaaServerAddress = address.getHostAddress();
@@ -461,13 +464,14 @@
         // Compare the calculated Message-Authenticator with the one in the message
         return Arrays.equals(newHash, messageAuthenticator);
     }
+
     public void checkForPacketFromUnknownServer(String hostAddress) {
+        if (!hostAddress.equals(configuredAaaServerAddress)) {
+            getConfiguredAaaServerAddress();
             if (!hostAddress.equals(configuredAaaServerAddress)) {
-                 getConfiguredAaaServerAddress();
-                 if (!hostAddress.equals(configuredAaaServerAddress)) {
-                     aaaStatisticsManager.getAaaStats().incrementUnknownServerRx();
-                 }
+                aaaStatisticsManager.getAaaStats().incrementUnknownServerRx();
             }
+        }
     }
 
     /**
@@ -544,8 +548,9 @@
         ConnectPoint supplicantCp = stateMachine.supplicantConnectpoint();
         switch (radiusPacket.getCode()) {
             case RADIUS.RADIUS_CODE_ACCESS_CHALLENGE:
-                log.debug("RADIUS packet: RADIUS_CODE_ACCESS_CHALLENGE for dev/port: {}/{} with MacAddress {}",
-                          supplicantCp.deviceId(), supplicantCp.port(), dstMac);
+                log.debug("RADIUS packet: RADIUS_CODE_ACCESS_CHALLENGE for dev/port: {}/{} " +
+                                  "with MacAddress {} and Identifier {}",
+                          supplicantCp.deviceId(), supplicantCp.port(), dstMac, radiusPacket.getIdentifier() & 0xff);
                 RADIUSAttribute radiusAttrState = radiusPacket.getAttribute(RADIUSAttribute.RADIUS_ATTR_STATE);
                 byte[] challengeState = null;
                 if (radiusAttrState != null) {
@@ -563,8 +568,9 @@
                     log.error(e.getMessage());
                     break;
                 }
-                log.debug("Send EAP challenge response to supplicant {} on dev/port: {}/{} with MacAddress {}",
-                          supplicantCp.deviceId(), supplicantCp.port(), dstMac);
+                log.debug("Send EAP challenge response to supplicant on dev/port: {}/{}" +
+                                  " with MacAddress {} and Identifier {}",
+                          supplicantCp.deviceId(), supplicantCp.port(), dstMac, radiusPacket.getIdentifier() & 0xff);
                 sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint(), true);
                 aaaStatisticsManager.getAaaStats().increaseChallengeResponsesRx();
                 outPacketSupp.add(eapPayload.getIdentifier());
@@ -574,8 +580,9 @@
                 machineStats.incrementTotalOctetSent(eapPayload.getLength());
                 break;
             case RADIUS.RADIUS_CODE_ACCESS_ACCEPT:
-                log.debug("RADIUS packet: RADIUS_CODE_ACCESS_ACCEPT for dev/port: {}/{} with MacAddress {}",
-                          supplicantCp.deviceId(), supplicantCp.port(), dstMac);
+                log.debug("RADIUS packet: RADIUS_CODE_ACCESS_ACCEPT for dev/port: {}/{}" +
+                                  " with MacAddress {} and Identifier {}",
+                          supplicantCp.deviceId(), supplicantCp.port(), dstMac, radiusPacket.getIdentifier() & 0xff);
                 //send an EAPOL - Success to the supplicant.
                 byte[] eapMessageSuccess =
                         radiusPacket.getAttribute(RADIUSAttribute.RADIUS_ATTR_EAP_MESSAGE).getValue();
@@ -592,8 +599,9 @@
                                          stateMachine.vlanId(),
                                          EAPOL.EAPOL_PACKET,
                                          eapPayload, stateMachine.priorityCode());
-                log.info("Send EAP success message to supplicant {} on dev/port: {}/{} with MacAddress {}",
-                         supplicantCp.deviceId(), supplicantCp.port(), dstMac);
+                log.info("Send EAP success message to supplicant on dev/port: {}/{}" +
+                                 " with MacAddress {} and Identifier {}",
+                         supplicantCp.deviceId(), supplicantCp.port(), dstMac, radiusPacket.getIdentifier() & 0xff);
                 sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint(), false);
                 aaaStatisticsManager.getAaaStats().incrementEapolAuthSuccessTrans();
 
@@ -604,8 +612,9 @@
                 machineStats.incrementTotalOctetSent(eapPayload.getLength());
                 break;
             case RADIUS.RADIUS_CODE_ACCESS_REJECT:
-                log.debug("RADIUS packet: RADIUS_CODE_ACCESS_REJECT for dev/port: {}/{} with MacAddress {}",
-                          supplicantCp.deviceId(), supplicantCp.port(), dstMac);
+                log.debug("RADIUS packet: RADIUS_CODE_ACCESS_REJECT for dev/port: {}/{}" +
+                                  " with MacAddress {} and Identifier {}",
+                          supplicantCp.deviceId(), supplicantCp.port(), dstMac, radiusPacket.getIdentifier() & 0xff);
                 //send an EAPOL - Failure to the supplicant.
                 byte[] eapMessageFailure;
                 eapPayload = new EAP();
@@ -629,8 +638,9 @@
                                          stateMachine.vlanId(),
                                          EAPOL.EAPOL_PACKET,
                                          eapPayload, stateMachine.priorityCode());
-                log.warn("Send EAP failure message to supplicant {} on dev/port: {}/{} with MacAddress {}",
-                         supplicantCp.deviceId(), supplicantCp.port(), dstMac);
+                log.warn("Send EAP failure message to supplicant {} on dev/port: {}/{}" +
+                                 " with MacAddress {} and Identifier {}", supplicantCp.deviceId(), supplicantCp.port(),
+                         dstMac, stateMachine.challengeIdentifier() & 0xff);
                 sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint(), false);
                 aaaStatisticsManager.getAaaStats().incrementEapolauthFailureTrans();
 
@@ -646,8 +656,10 @@
                                                               machineObj));
                 break;
             default:
-                log.warn("Unknown RADIUS message received with code: {} for dev/port: {}/{} with MacAddress {}",
-                         radiusPacket.getCode(), supplicantCp.deviceId(), supplicantCp.port(), dstMac);
+                log.warn("Unknown RADIUS message received with code: {} for dev/port: {}/{}" +
+                                 " with MacAddress {} and Identifier {}",
+                         radiusPacket.getCode(), supplicantCp.deviceId(), supplicantCp.port(), dstMac,
+                         radiusPacket.getIdentifier() & 0xff);
                 aaaStatisticsManager.getAaaStats().increaseUnknownTypeRx();
                 //increasing packets received to server
                 machineStats.incrementTotalPacketsReceived();
@@ -730,17 +742,31 @@
             packetProcessorExecutor.execute(() -> {
                 // Extract the original Ethernet frame from the packet information
                 InboundPacket pkt = context.inPacket();
+                if (pkt == null) {
+                    log.error("InboundPacket is null when parsed from {}", context);
+                    return;
+                }
                 Ethernet ethPkt = pkt.parsed();
                 if (ethPkt == null) {
+                    log.error("EthPkt is null when parsed from {}", pkt);
                     return;
                 }
 
                 // identify if incoming packet comes from supplicant (EAP) or RADIUS
                 switch (EthType.EtherType.lookup(ethPkt.getEtherType())) {
                     case EAPOL:
+                        if (log.isTraceEnabled()) {
+                            log.trace("Received EAPOL supplicant packet from dev/port: {} with MacAddress {}",
+                                      context.inPacket().receivedFrom(), ethPkt.getSourceMAC());
+                        }
                         handleSupplicantPacket(context.inPacket());
                         break;
                     default:
+                        if (log.isTraceEnabled()) {
+                            log.trace("Received packet-in from RADIUS server {} in enclosing packet {} from "
+                                              + "dev/port: {} with MacAddress {}", ethPkt, context.inPacket(),
+                                      context.inPacket().receivedFrom(), ethPkt.getSourceMAC());
+                        }
                         // any other packets let the specific implementation handle
                         impl.handlePacketFromServer(context);
                 }
@@ -751,7 +777,7 @@
          * Creates and initializes common fields of a RADIUS packet.
          *
          * @param stateMachine state machine for the request
-         * @param eapPacket  EAP packet
+         * @param eapPacket    EAP packet
          * @return RADIUS packet
          */
         private RADIUS getRadiusPayload(StateMachine stateMachine, byte identifier, EAP eapPacket) {
@@ -767,7 +793,7 @@
                                        stateMachine.username());
 
             radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_NAS_IP,
-                    AaaManager.this.nasIpAddress.getAddress());
+                                       AaaManager.this.nasIpAddress.getAddress());
 
             radiusPayload.encapsulateMessage(eapPacket);
 
@@ -780,6 +806,7 @@
          * @param inPacket Ethernet packet coming from the supplicant
          */
         private void handleSupplicantPacket(InboundPacket inPacket) {
+
             Ethernet ethPkt = inPacket.parsed();
             // Where does it come from?
             MacAddress srcMac = ethPkt.getSourceMAC();
@@ -788,28 +815,31 @@
             PortNumber portNumber = inPacket.receivedFrom().port();
             String sessionId = sessionId(inPacket.receivedFrom());
             EAPOL eapol = (EAPOL) ethPkt.getPayload();
+
             if (log.isTraceEnabled()) {
                 log.trace("Received EAPOL packet {} in enclosing packet {} from "
-                        + "dev/port: {}/{} with MacAddress {}",
-                          eapol, ethPkt, deviceId,
-                          portNumber, srcMac);
+                                  + "dev/port: {}/{} with MacAddress {} and type {}",
+                          eapol, ethPkt, deviceId, portNumber, srcMac, eapol.getEapolType());
             }
 
             short pktlen = eapol.getPacketLength();
             byte[] eapPayLoadBuffer = eapol.serialize();
             int len = eapPayLoadBuffer.length;
             if (len != (HEADER_LENGTH + pktlen)) {
+                log.error("Invalid EAPOL pkt length {} (shoudl be {}) for packet {} from dev/port: {}/{} " +
+                          "with MacAddress {}", len, HEADER_LENGTH + pktlen, eapol, deviceId, portNumber, srcMac);
                 aaaStatisticsManager.getAaaStats().incrementInvalidBodyLength();
                 return;
             }
             if (!VALID_EAPOL_TYPE.contains(eapol.getEapolType())) {
+                log.error("Invalid EAPOL Type {} for packet {} from dev/port: {}/{} with MacAddress {}",
+                          eapol.getEapolType(), eapol, deviceId, portNumber, srcMac);
                 aaaStatisticsManager.getAaaStats().incrementInvalidPktType();
                 return;
             }
             if (pktlen >= 0 && ethPkt.getEtherType() == EthType.EtherType.EAPOL.ethType().toShort()) {
                 aaaStatisticsManager.getAaaStats().incrementValidEapolFramesRx();
             }
-
             StateMachine stateMachine = stateMachines.computeIfAbsent(sessionId, id ->
                     new StateMachine(id, serverStatusAndStateMachineTimeoutExecutor));
             stateMachine.setEapolTypeVal(eapol.getEapolType());
@@ -826,17 +856,16 @@
                     //send an EAP Request/Identify to the supplicant
                     EAP eapPayload = new EAP(EAP.REQUEST, stateMachine.identifier(), EAP.ATTR_IDENTITY, null);
                     if (ethPkt.getVlanID() != Ethernet.VLAN_UNTAGGED) {
-                       stateMachine.setPriorityCode(ethPkt.getPriorityCode());
+                        stateMachine.setPriorityCode(ethPkt.getPriorityCode());
                     }
                     Ethernet eth = buildEapolResponse(srcMac, MacAddress.valueOf(nasMacAddress),
-                                                      ethPkt.getVlanID(), EAPOL.EAPOL_PACKET,
-                                                      eapPayload, stateMachine.priorityCode());
+                                      ethPkt.getVlanID(), EAPOL.EAPOL_PACKET, eapPayload, stateMachine.priorityCode());
 
                     stateMachine.setVlanId(ethPkt.getVlanID());
-                    log.debug("Getting EAP identity from supplicant {}", stateMachine.supplicantAddress().toString());
+                    log.debug("Getting EAP identity from supplicant {} and Identifier {}",
+                              stateMachine.supplicantAddress().toString(), stateMachine.identifier() & 0xff);
                     sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint(), false);
                     aaaStatisticsManager.getAaaStats().incrementRequestIdFramesTx();
-
                     break;
                 case EAPOL.EAPOL_LOGOFF:
                     log.debug("EAP packet: EAPOL_LOGOFF from dev/port: {}/{} with MacAddress {}",
@@ -857,117 +886,39 @@
                     if (stateMachine.state() == StateMachine.STATE_IDLE) {
                         aaaStatisticsManager.getAaaStats().incrementAuthStateIdle();
                     }
-
                     break;
                 case EAPOL.EAPOL_PACKET:
-                    RADIUS radiusPayload;
+
                     // check if this is a Response/Identify or  a Response/TLS
                     EAP eapPacket = (EAP) eapol.getPayload();
                     Byte identifier = new Byte(eapPacket.getIdentifier());
-
+                    log.debug("EAP packet: EAPOL_PACKET from dev/port: {}/{} with MacAddress {} with Identifier {}",
+                              deviceId, portNumber, srcMac, identifier.doubleValue());
                     // get identifier for request and store mapping to session ID
                     RequestIdentifier radiusIdentifier = idManager.getNewIdentifier(sessionId);
+                    if (radiusIdentifier == null) {
+                        log.error("Cannot get RADIUS Identifier, dropping packet");
+                        return;
+                    }
 
                     byte dataType = eapPacket.getDataType();
+
                     switch (dataType) {
-
                         case EAP.ATTR_IDENTITY:
-                            log.debug("EAP packet: EAPOL_PACKET ATTR_IDENTITY from dev/port: {}/{} with MacAddress {}",
-                                      deviceId, portNumber, srcMac);
-                            //Setting the time of this response from RG, only when its not a re-transmission.
-                            if (stateMachine.getLastPacketReceivedTime() == 0) {
-                               stateMachine.setLastPacketReceivedTime(System.currentTimeMillis());
-                            }
-                            // request id access to RADIUS
-                            stateMachine.setUsername(eapPacket.getData());
-
-                            radiusPayload = getRadiusPayload(stateMachine, radiusIdentifier.identifier(), eapPacket);
-                            radiusPayload = pktCustomizer.customizePacket(radiusPayload, inPacket);
-                            radiusPayload.addMessageAuthenticator(radiusSecret);
-
-                            if (log.isTraceEnabled()) {
-                                log.trace("Sending ATTR_IDENTITY packet to RADIUS for supplicant at dev/port: " +
-                                                  "{}/{} with MacAddress {}", deviceId, portNumber, srcMac);
-                            }
-
-                            sendRadiusPacket(radiusPayload, inPacket);
-                            stateMachine.setWaitingForRadiusResponse(true);
-                            aaaStatisticsManager.getAaaStats().incrementRadiusReqIdTx();
-                            aaaStatisticsManager.getAaaStats().incrementEapolAtrrIdentity();
-                            // change the state to "PENDING"
-                            if (stateMachine.state() == StateMachine.STATE_PENDING) {
-                                aaaStatisticsManager.getAaaStats().increaseRequestReTx();
-                                stateMachine.incrementTotalPacketsSent();
-                                stateMachine.incrementTotalOctetSent(eapol.getPacketLength());
-                            }
-                            stateMachine.requestAccess();
+                            handleAttrIdentity(inPacket, srcMac, deviceId, portNumber,
+                                               eapol, stateMachine, eapPacket, radiusIdentifier);
                             break;
                         case EAP.ATTR_MD5:
-                            log.debug("EAP packet: EAPOL_PACKET ATTR_MD5 from dev/port: {}/{} with MacAddress {}",
-                                      deviceId, portNumber, srcMac);
-                            // verify if the EAP identifier corresponds to the
-                            // challenge identifier from the client state
-                            // machine.
-                            stateMachine.setLastPacketReceivedTime(System.currentTimeMillis());
-                            if (eapPacket.getIdentifier() == stateMachine.challengeIdentifier()) {
-                                //send the RADIUS challenge response
-                                radiusPayload = getRadiusPayload(stateMachine,
-                                        radiusIdentifier.identifier(), eapPacket);
-                                radiusPayload = pktCustomizer.customizePacket(radiusPayload, inPacket);
-
-                                if (stateMachine.challengeState() != null) {
-                                    radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_STATE,
-                                            stateMachine.challengeState());
-                                }
-                                radiusPayload.addMessageAuthenticator(radiusSecret);
-                                if (outPacketSupp.contains(eapPacket.getIdentifier())) {
-                                    aaaStatisticsManager.getAaaStats().decrementPendingReqSupp();
-                                    outPacketSupp.remove(identifier);
-                                }
-                                if (log.isTraceEnabled()) {
-                                    log.trace("Sending ATTR_MD5 packet to RADIUS for supplicant at dev/port: " +
-                                                      "{}/{} with MacAddress {}", deviceId, portNumber, srcMac);
-                                }
-                                sendRadiusPacket(radiusPayload, inPacket);
-                                stateMachine.setWaitingForRadiusResponse(true);
-                                aaaStatisticsManager.getAaaStats().incrementRadiusReqChallengeTx();
-                                aaaStatisticsManager.getAaaStats().incrementEapolMd5RspChall();
-                            }
+                            handleMD5(inPacket, srcMac, deviceId, portNumber, stateMachine,
+                                      eapPacket, identifier, radiusIdentifier);
                             break;
                         case EAP.ATTR_TLS:
-                            log.debug("EAP packet: EAPOL_PACKET ATTR_TLS from dev/port: {}/{} with MacAddress {}",
-                                      deviceId, portNumber, srcMac);
-                            // request id access to RADIUS
-                            radiusPayload = getRadiusPayload(stateMachine, radiusIdentifier.identifier(), eapPacket);
-                            radiusPayload = pktCustomizer.customizePacket(radiusPayload, inPacket);
-
-                            if (stateMachine.challengeState() != null) {
-                                radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_STATE,
-                                        stateMachine.challengeState());
-                            }
-                            stateMachine.setRequestAuthenticator(radiusPayload.generateAuthCode());
-                            radiusPayload.addMessageAuthenticator(radiusSecret);
-                            if (outPacketSupp.contains(eapPacket.getIdentifier())) {
-                                aaaStatisticsManager.getAaaStats().decrementPendingReqSupp();
-                                outPacketSupp.remove(identifier);
-                            }
-                            if (log.isTraceEnabled()) {
-                                log.trace("Sending ATTR_TLS packet to RADIUS for supplicant at dev/port: " +
-                                                  "{}/{} with MacAddress {}", deviceId, portNumber, srcMac);
-                            }
-                            sendRadiusPacket(radiusPayload, inPacket);
-                            stateMachine.setWaitingForRadiusResponse(true);
-                            aaaStatisticsManager.getAaaStats().incrementRadiusReqChallengeTx();
-                            aaaStatisticsManager.getAaaStats().incrementEapolTlsRespChall();
-
-                            if (stateMachine.state() != StateMachine.STATE_PENDING) {
-                                stateMachine.requestAccess();
-                            }
-
+                            handleTls(inPacket, srcMac, deviceId, portNumber, stateMachine,
+                                      eapPacket, identifier, radiusIdentifier);
                             break;
                         default:
-                            log.warn("Unknown EAP packet type from dev/port: {}/{} with MacAddress {}",
-                                     deviceId, portNumber, srcMac);
+                            log.warn("Unknown EAP packet type from dev/port: {}/{} with MacAddress {} and " +
+                                     "Identifier {}", deviceId, portNumber, srcMac, eapPacket.getIdentifier() & 0xff);
                             return;
                     }
                     break;
@@ -978,6 +929,124 @@
             aaaStatisticsManager.getAaaStats().countTransRespNotNak();
             aaaStatisticsManager.getAaaStats().countEapolResIdentityMsgTrans();
         }
+
+        private void handleAttrIdentity(InboundPacket inPacket, MacAddress srcMac, DeviceId deviceId,
+                                        PortNumber portNumber, EAPOL eapol, StateMachine stateMachine,
+                                        EAP eapPacket, RequestIdentifier radiusIdentifier) {
+            RADIUS radiusPayload;
+            log.debug("EAP packet: EAPOL_PACKET ATTR_IDENTITY from dev/port: {}/{} with MacAddress {}" +
+                              " and Identifier {}", deviceId, portNumber, srcMac, eapPacket.getIdentifier() & 0xff);
+            //Setting the time of this response from RG, only when its not a re-transmission.
+            if (stateMachine.getLastPacketReceivedTime() == 0) {
+                stateMachine.setLastPacketReceivedTime(System.currentTimeMillis());
+            }
+            // request id access to RADIUS
+            stateMachine.setUsername(eapPacket.getData());
+
+            radiusPayload = getRadiusPayload(stateMachine, radiusIdentifier.identifier(), eapPacket);
+            radiusPayload = pktCustomizer.customizePacket(radiusPayload, inPacket);
+            radiusPayload.addMessageAuthenticator(radiusSecret);
+
+            if (log.isTraceEnabled()) {
+                log.trace("Sending ATTR_IDENTITY packet to RADIUS for supplicant at dev/port: " +
+                                  "{}/{} with MacAddress {} and Identifier {}", deviceId, portNumber,
+                          srcMac, radiusIdentifier.identifier() & 0xff);
+            }
+
+            sendRadiusPacket(radiusPayload, inPacket);
+            stateMachine.setWaitingForRadiusResponse(true);
+            aaaStatisticsManager.getAaaStats().incrementRadiusReqIdTx();
+            aaaStatisticsManager.getAaaStats().incrementEapolAtrrIdentity();
+            // change the state to "PENDING"
+            if (stateMachine.state() == StateMachine.STATE_PENDING) {
+                aaaStatisticsManager.getAaaStats().increaseRequestReTx();
+                stateMachine.incrementTotalPacketsSent();
+                stateMachine.incrementTotalOctetSent(eapol.getPacketLength());
+            }
+            stateMachine.requestAccess();
+        }
+
+        private void handleMD5(InboundPacket inPacket, MacAddress srcMac, DeviceId deviceId,
+                               PortNumber portNumber, StateMachine stateMachine, EAP eapPacket,
+                               Byte identifier, RequestIdentifier radiusIdentifier) {
+            RADIUS radiusPayload;
+            log.debug("EAP packet: EAPOL_PACKET ATTR_MD5 from dev/port: {}/{} with MacAddress {}" +
+                              " and Identifier {}", deviceId, portNumber, srcMac, eapPacket.getIdentifier() & 0xff);
+            // verify if the EAP identifier corresponds to the
+            // challenge identifier from the client state
+            // machine.
+            stateMachine.setLastPacketReceivedTime(System.currentTimeMillis());
+            if (eapPacket.getIdentifier() == stateMachine.challengeIdentifier()) {
+                //send the RADIUS challenge response
+                radiusPayload = getRadiusPayload(stateMachine,
+                                                 radiusIdentifier.identifier(), eapPacket);
+                radiusPayload = pktCustomizer.customizePacket(radiusPayload, inPacket);
+
+                if (stateMachine.challengeState() != null) {
+                    radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_STATE,
+                                               stateMachine.challengeState());
+                }
+                radiusPayload.addMessageAuthenticator(radiusSecret);
+                if (outPacketSupp.contains(eapPacket.getIdentifier())) {
+                    aaaStatisticsManager.getAaaStats().decrementPendingReqSupp();
+                    outPacketSupp.remove(identifier);
+                }
+                if (log.isTraceEnabled()) {
+                    log.trace("Sending ATTR_MD5 packet to RADIUS for supplicant at dev/port: {}/{}" +
+                                      " with MacAddress {} and Identifier {}", deviceId, portNumber, srcMac,
+                              radiusIdentifier.identifier() & 0xff);
+                }
+                sendRadiusPacket(radiusPayload, inPacket);
+                stateMachine.setWaitingForRadiusResponse(true);
+                aaaStatisticsManager.getAaaStats().incrementRadiusReqChallengeTx();
+                aaaStatisticsManager.getAaaStats().incrementEapolMd5RspChall();
+            } else {
+                log.error("eapolIdentifier {} and stateMachine Identifier {} do not " +
+                                  "correspond for packet from dev/port: {}/{} with MacAddress {}",
+                          eapPacket.getIdentifier() & 0xff, stateMachine.challengeIdentifier() & 0xff,
+                          deviceId, portNumber, srcMac);
+                aaaStatisticsManager.getAaaStats().incrementEapolMd5RspChall();
+                if (outPacketSupp.contains(eapPacket.getIdentifier())) {
+                    aaaStatisticsManager.getAaaStats().decrementPendingReqSupp();
+                    outPacketSupp.remove(identifier);
+                }
+                aaaStatisticsManager.getAaaStats().incrementEapolauthFailureTrans();
+            }
+        }
+
+
+        private void handleTls(InboundPacket inPacket, MacAddress srcMac, DeviceId deviceId,
+                               PortNumber portNumber, StateMachine stateMachine, EAP eapPacket,
+                               Byte identifier, RequestIdentifier radiusIdentifier) {
+            RADIUS radiusPayload;
+            log.debug("EAP packet: EAPOL_PACKET ATTR_TLS from dev/port: {}/{} with MacAddress {} " +
+                              "and Identifier {}", deviceId, portNumber, srcMac, eapPacket.getIdentifier() & 0xff);
+            // request id access to RADIUS
+            radiusPayload = getRadiusPayload(stateMachine, radiusIdentifier.identifier(), eapPacket);
+            radiusPayload = pktCustomizer.customizePacket(radiusPayload, inPacket);
+            if (stateMachine.challengeState() != null) {
+                radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_STATE,
+                                           stateMachine.challengeState());
+            }
+            stateMachine.setRequestAuthenticator(radiusPayload.generateAuthCode());
+            radiusPayload.addMessageAuthenticator(radiusSecret);
+            if (outPacketSupp.contains(eapPacket.getIdentifier())) {
+                aaaStatisticsManager.getAaaStats().decrementPendingReqSupp();
+                outPacketSupp.remove(identifier);
+            }
+            if (log.isTraceEnabled()) {
+                log.trace("Sending ATTR_TLS packet to RADIUS for supplicant at dev/port: {}/{} with " +
+                                  "MacAddress {} and Identifier {}", deviceId, portNumber, srcMac,
+                          radiusIdentifier.identifier() & 0xff);
+            }
+            sendRadiusPacket(radiusPayload, inPacket);
+            stateMachine.setWaitingForRadiusResponse(true);
+            aaaStatisticsManager.getAaaStats().incrementRadiusReqChallengeTx();
+            aaaStatisticsManager.getAaaStats().incrementEapolTlsRespChall();
+            if (stateMachine.state() != StateMachine.STATE_PENDING) {
+                stateMachine.requestAccess();
+            }
+        }
     }
 
     /**
@@ -988,7 +1057,7 @@
         @Override
         public void notify(AuthenticationEvent authenticationEvent) {
             log.info("Auth event {} for {}",
-                    authenticationEvent.type(), authenticationEvent.subject());
+                     authenticationEvent.type(), authenticationEvent.subject());
 
             if (authenticationEvent.type() == AuthenticationEvent.Type.TIMEOUT) {
                 handleStateMachineTimeout(authenticationEvent.subject());
@@ -1121,7 +1190,7 @@
                     log.debug("Received PORT_REMOVED event. Clearing AAA Session with Id {}", sessionId);
 
                     flushStateMachineSession(sessionId,
-                            StateMachine.SessionTerminationReasons.PORT_REMOVED.getReason());
+                                             StateMachine.SessionTerminationReasons.PORT_REMOVED.getReason());
 
                     break;
                 case DEVICE_REMOVED:
@@ -1146,7 +1215,7 @@
             for (String session : associatedSessions) {
                 log.debug("Clearing AAA Session {} associated with Removed Device", session);
                 flushStateMachineSession(session,
-                       StateMachine.SessionTerminationReasons.DEVICE_REMOVED.getReason());
+                                         StateMachine.SessionTerminationReasons.DEVICE_REMOVED.getReason());
             }
         }
 
@@ -1164,7 +1233,7 @@
             //pushing captured machine stats to kafka
             AaaSupplicantMachineStats obj = aaaSupplicantStatsManager.getSupplicantStats(stateMachine);
             aaaSupplicantStatsManager.getMachineStatsDelegate()
-                   .notify(new AaaMachineStatisticsEvent(AaaMachineStatisticsEvent.Type.STATS_UPDATE, obj));
+                    .notify(new AaaMachineStatisticsEvent(AaaMachineStatisticsEvent.Type.STATS_UPDATE, obj));
             StateMachine removed = stateMachines.remove(sessionId);
 
             if (removed != null) {
@@ -1180,10 +1249,10 @@
             radiusOperationalStatusService.checkServerOperationalStatus();
             log.trace("--POSTING--" + radiusOperationalStatusService.getRadiusServerOperationalStatus());
             radiusOperationalStatusService.getRadiusOprStDelegate()
-                .notify(new RadiusOperationalStatusEvent(
-                        RadiusOperationalStatusEvent.Type.RADIUS_OPERATIONAL_STATUS,
-                        radiusOperationalStatusService.
-                        getRadiusServerOperationalStatus()));
+                    .notify(new RadiusOperationalStatusEvent(
+                            RadiusOperationalStatusEvent.Type.RADIUS_OPERATIONAL_STATUS,
+                            radiusOperationalStatusService.
+                                    getRadiusServerOperationalStatus()));
         }
 
     }
diff --git a/app/src/main/java/org/opencord/aaa/impl/RadiusOperationalStatusManager.java b/app/src/main/java/org/opencord/aaa/impl/RadiusOperationalStatusManager.java
index 77e58c0..377cd0a 100644
--- a/app/src/main/java/org/opencord/aaa/impl/RadiusOperationalStatusManager.java
+++ b/app/src/main/java/org/opencord/aaa/impl/RadiusOperationalStatusManager.java
@@ -51,6 +51,7 @@
     private long serverStatusOutTimeInMillis;
 
     private OperationalStatus radiusServerOperationalStatus;
+
     public static final byte AAA_REQUEST_ID_STATUS_REQUEST = 0;
     public static final byte AAA_REQUEST_ID_FAKE_ACCESS_REQUEST = 1;
 
diff --git a/app/src/main/java/org/opencord/aaa/impl/SocketBasedRadiusCommunicator.java b/app/src/main/java/org/opencord/aaa/impl/SocketBasedRadiusCommunicator.java
index 99c9396..0414cac 100755
--- a/app/src/main/java/org/opencord/aaa/impl/SocketBasedRadiusCommunicator.java
+++ b/app/src/main/java/org/opencord/aaa/impl/SocketBasedRadiusCommunicator.java
@@ -105,6 +105,7 @@
 
     @Override
     public void clearLocalState() {
+        log.info("Closing RADIUS socket: {}:{}", radiusIpAddress, radiusServerPort);
         radiusSocket.close();
         executor.shutdownNow();
     }
@@ -154,7 +155,7 @@
                 aaaManager.radiusOperationalStatusService.setStatusServerReqSent(false);
             }
         } catch (IOException e) {
-            log.info("Cannot send packet to RADIUS server", e);
+            log.warn("Cannot send packet to RADIUS server", e);
         }
     }
 
@@ -193,6 +194,10 @@
                                         .deserialize(inboundBasePacket.getData(),
                                                 0,
                                                 inboundBasePacket.getLength());
+                        if (log.isTraceEnabled()) {
+                            log.trace("Received RADIUS packet with Identifier {}",
+                                      inboundRadiusPacket.getIdentifier() & 0xff);
+                        }
                         aaaManager.aaaStatisticsManager.handleRoundtripTime(inboundRadiusPacket.getIdentifier());
                         aaaManager.handleRadiusPacket(inboundRadiusPacket);
                     } catch (DeserializationException dex) {
@@ -200,10 +205,11 @@
                         log.error("Cannot deserialize packet", dex);
                     }
                 } catch (IOException e) {
-                    log.info("Socket was closed, exiting listener thread");
+                    log.warn("Socket was closed, exiting listener thread");
                     done = true;
                 }
             }
+            log.info("UDP listener thread shutting down");
         }
     }