Refactor AAA app in preparation for clustered operation.

* Add formal API for accessing auth state information rather than directly
looking up static maps.
* Move static maps in StateMachine to non-static maps in AaaManager
* Manage identifier space used for requests/replies better
* Refactored state machine timeout mechansim

Change-Id: Ie53c3a66ac1619e10607d9926b71747a333317f3
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 f1f5c05..67cfb75 100644
--- a/app/src/main/java/org/opencord/aaa/impl/AaaManager.java
+++ b/app/src/main/java/org/opencord/aaa/impl/AaaManager.java
@@ -16,6 +16,7 @@
 package org.opencord.aaa.impl;
 
 import com.google.common.base.Strings;
+import com.google.common.collect.Maps;
 import org.apache.commons.lang3.builder.ToStringBuilder;
 import org.onlab.packet.DeserializationException;
 import org.onlab.packet.EAP;
@@ -55,6 +56,7 @@
 import org.opencord.aaa.AaaSupplicantMachineStats;
 import org.opencord.aaa.AuthenticationEvent;
 import org.opencord.aaa.AuthenticationEventListener;
+import org.opencord.aaa.AuthenticationRecord;
 import org.opencord.aaa.AuthenticationService;
 import org.opencord.aaa.AuthenticationStatisticsEvent;
 import org.opencord.aaa.AuthenticationStatisticsService;
@@ -85,10 +87,12 @@
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.concurrent.ConcurrentMap;
 import java.util.concurrent.Executors;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.ScheduledFuture;
 import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
 
 import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY;
 import static org.opencord.aaa.impl.OsgiPropertyConstants.OPERATIONAL_STATUS_SERVER_EVENT_GENERATION;
@@ -158,6 +162,10 @@
     private int operationalStatusServerTimeoutInSeconds = OPERATIONAL_STATUS_SERVER_TIMEOUT_DEFAULT;
     protected String operationalStatusEvaluationMode = STATUS_SERVER_MODE_DEFAULT;
 
+    private IdentifierManager idManager;
+
+    private ConcurrentMap<String, StateMachine> stateMachines;
+
     // NAS IP address
     protected InetAddress nasIpAddress;
 
@@ -262,6 +270,8 @@
 
     @Activate
     public void activate(ComponentContext context) {
+        idManager = new IdentifierManager();
+        stateMachines = Maps.newConcurrentMap();
         appId = coreService.registerApplication(APP_NAME);
         eventDispatcher.addSink(AuthenticationEvent.class, listenerRegistry);
         netCfgService.addListener(cfgListener);
@@ -275,7 +285,6 @@
         configureRadiusCommunication();
         // register our event handler
         packetService.addProcessor(processor, PacketProcessor.director(2));
-        StateMachine.initializeMaps();
         StateMachine.setDelegate(delegate);
         cleanupTimerTimeOutInMins = newCfg.sessionCleanupTimer();
         StateMachine.setcleanupTimerTimeOutInMins(cleanupTimerTimeOutInMins);
@@ -303,7 +312,6 @@
         netCfgService.removeListener(cfgListener);
         cfgService.unregisterProperties(getClass(), false);
         StateMachine.unsetDelegate(delegate);
-        StateMachine.destroyMaps();
         impl.deactivate();
         deviceService.removeListener(deviceListener);
         eventDispatcher.removeSink(AuthenticationEvent.class);
@@ -435,30 +443,13 @@
         impl.sendRadiusPacket(radiusPacket, inPkt);
     }
 
-   /**
-     * For scheduling the timer required for cleaning up StateMachine
-     * when no response
-     * from RADIUS SERVER.
-     *
-     * @param sessionId    SessionId of the current session
-     * @param stateMachine StateMachine for the id
-     */
-    public void scheduleStateMachineCleanupTimer(String sessionId, StateMachine stateMachine) {
-        StateMachine.CleanupTimerTask cleanupTask = stateMachine.new CleanupTimerTask(sessionId, this);
-        ScheduledFuture<?> cleanupTimer = executor.schedule(cleanupTask, cleanupTimerTimeOutInMins, TimeUnit.MINUTES);
-        stateMachine.setCleanupTimer(cleanupTimer);
-
-    }
-
-   /**
+    /**
      * Handles RADIUS packets.
      *
      * @param radiusPacket RADIUS packet coming from the RADIUS server.
-     * @throws StateMachineException if an illegal state transition is triggered
      * @throws DeserializationException if packet deserialization fails
      */
-    public void handleRadiusPacket(RADIUS radiusPacket)
-            throws StateMachineException, DeserializationException {
+    public void handleRadiusPacket(RADIUS radiusPacket) throws DeserializationException {
         if (log.isTraceEnabled()) {
             log.trace("Received RADIUS packet {}", radiusPacket);
         }
@@ -466,7 +457,20 @@
             radiusOperationalStatusService.handleRadiusPacketForOperationalStatus(radiusPacket);
             return;
         }
-        StateMachine stateMachine = StateMachine.lookupStateMachineById(radiusPacket.getIdentifier());
+
+        RequestIdentifier identifier = RequestIdentifier.of(radiusPacket.getIdentifier());
+        String sessionId = idManager.getSessionId(identifier);
+
+        if (sessionId == null) {
+            log.error("Invalid packet identifier {}, could not find corresponding "
+                    + "state machine ... exiting", radiusPacket.getIdentifier());
+            aaaStatisticsManager.getAaaStats().incrementNumberOfSessionsExpired();
+            aaaStatisticsManager.getAaaStats().countDroppedResponsesRx();
+            return;
+        }
+
+        idManager.releaseIdentifier(identifier);
+        StateMachine stateMachine = stateMachines.get(sessionId);
         if (stateMachine == null) {
             log.error("Invalid packet identifier {}, could not find corresponding "
                     + "state machine ... exiting", radiusPacket.getIdentifier());
@@ -476,7 +480,7 @@
         }
 
         //instance of StateMachine using the sessionId for updating machine stats
-        StateMachine machineStats = StateMachine.lookupStateMachineBySessionId(stateMachine.sessionId());
+        StateMachine machineStats = stateMachines.get(stateMachine.sessionId());
 
         EAP eapPayload;
         Ethernet eth;
@@ -591,7 +595,6 @@
         OutboundPacket packet = new DefaultOutboundPacket(connectPoint.deviceId(),
                                                           treatment, ByteBuffer.wrap(ethernetPkt.serialize()));
         EAPOL eap = ((EAPOL) ethernetPkt.getPayload());
-        EAP eapPkt = (EAP) eap.getPayload();
         if (log.isTraceEnabled()) {
             log.trace("Sending eapol payload {} enclosed in {} to supplicant at {}",
                       eap, ethernetPkt, connectPoint);
@@ -609,6 +612,47 @@
         return ToStringBuilder.reflectionToString(this);
     }
 
+    @Override
+    public List<AuthenticationRecord> getAuthenticationRecords() {
+        return stateMachines.values().stream()
+                .map(this::toAuthRecord)
+                .collect(Collectors.toList());
+    }
+
+    private AuthenticationRecord toAuthRecord(StateMachine stateMachine) {
+        return new AuthenticationRecord(stateMachine.supplicantConnectpoint(),
+                stateMachine.username(), stateMachine.supplicantAddress(), stateMachine.stateString());
+    }
+
+    @Override
+    public boolean removeAuthenticationStateByMac(MacAddress mac) {
+
+        StateMachine stateMachine = null;
+
+        for (Map.Entry<String, StateMachine> e : stateMachines.entrySet()) {
+            if (e.getValue().supplicantAddress() != null &&
+                    e.getValue().supplicantAddress().equals(mac)) {
+                stateMachine = stateMachines.remove(e.getKey());
+                break;
+            }
+        }
+
+        if (stateMachine != null) {
+            stateMachine.stop();
+            return true;
+        }
+
+        return false;
+    }
+
+    public StateMachine getStateMachine(String sessionId) {
+        return stateMachines.get(sessionId);
+    }
+
+    private String sessionId(ConnectPoint cp) {
+        return cp.deviceId().toString() + cp.port().toString();
+    }
+
     // our handler defined as a private inner class
 
     /**
@@ -625,18 +669,14 @@
                 return;
             }
 
-            try {
-                // identify if incoming packet comes from supplicant (EAP) or RADIUS
-                switch (EthType.EtherType.lookup(ethPkt.getEtherType())) {
-                    case EAPOL:
-                        handleSupplicantPacket(context.inPacket());
-                        break;
-                    default:
-                        // any other packets let the specific implementation handle
-                        impl.handlePacketFromServer(context);
-                }
-            } catch (StateMachineException e) {
-                log.warn("Unable to process packet:", e);
+            // identify if incoming packet comes from supplicant (EAP) or RADIUS
+            switch (EthType.EtherType.lookup(ethPkt.getEtherType())) {
+                case EAPOL:
+                    handleSupplicantPacket(context.inPacket());
+                    break;
+                default:
+                    // any other packets let the specific implementation handle
+                    impl.handlePacketFromServer(context);
             }
         }
 
@@ -672,7 +712,7 @@
          *
          * @param inPacket Ethernet packet coming from the supplicant
          */
-        private void handleSupplicantPacket(InboundPacket inPacket) throws StateMachineException {
+        private void handleSupplicantPacket(InboundPacket inPacket) {
             Ethernet ethPkt = inPacket.parsed();
             // Where does it come from?
             MacAddress srcMac = ethPkt.getSourceMAC();
@@ -701,24 +741,16 @@
             if (pktlen >= 0 && ethPkt.getEtherType() == EthType.EtherType.EAPOL.ethType().toShort()) {
                 aaaStatisticsManager.getAaaStats().incrementValidEapolFramesRx();
             }
-            StateMachine stateMachine = StateMachine.lookupStateMachineBySessionId(sessionId);
-            if (stateMachine == null) {
-                log.debug("Creating new state machine for sessionId: {} for "
-                                + "dev/port: {}/{}", sessionId, deviceId, portNumber);
-                stateMachine = new StateMachine(sessionId);
-            } else {
-                log.debug("Using existing state-machine for sessionId: {}", sessionId);
-                stateMachine.setEapolTypeVal(eapol.getEapolType());
-            }
+
+            StateMachine stateMachine = stateMachines.computeIfAbsent(sessionId, id -> new StateMachine(id, executor));
+            stateMachine.setEapolTypeVal(eapol.getEapolType());
 
             switch (eapol.getEapolType()) {
                 case EAPOL.EAPOL_START:
                     log.debug("EAP packet: EAPOL_START");
                     stateMachine.setSupplicantConnectpoint(inPacket.receivedFrom());
-                    if (stateMachine.getCleanupTimer() == null) {
-                        scheduleStateMachineCleanupTimer(sessionId, stateMachine);
-                    }
                     stateMachine.start();
+
                     aaaStatisticsManager.getAaaStats().incrementEapolStartReqTrans();
                     //send an EAP Request/Identify to the supplicant
                     EAP eapPayload = new EAP(EAP.REQUEST, stateMachine.identifier(), EAP.ATTR_IDENTITY, null);
@@ -762,6 +794,9 @@
                     EAP eapPacket = (EAP) eapol.getPayload();
                     Byte identifier = new Byte(eapPacket.getIdentifier());
 
+                    // get identifier for request and store mapping to session ID
+                    RequestIdentifier radiusIdentifier = idManager.getNewIdentifier(sessionId);
+
                     byte dataType = eapPacket.getDataType();
                     switch (dataType) {
 
@@ -774,7 +809,7 @@
                             // request id access to RADIUS
                             stateMachine.setUsername(eapPacket.getData());
 
-                            radiusPayload = getRadiusPayload(stateMachine, stateMachine.identifier(), eapPacket);
+                            radiusPayload = getRadiusPayload(stateMachine, radiusIdentifier.identifier(), eapPacket);
                             radiusPayload = pktCustomizer.customizePacket(radiusPayload, inPacket);
                             radiusPayload.addMessageAuthenticator(AaaManager.this.radiusSecret);
 
@@ -797,10 +832,8 @@
                             // machine.
                             if (eapPacket.getIdentifier() == stateMachine.challengeIdentifier()) {
                                 //send the RADIUS challenge response
-                                radiusPayload =
-                                        getRadiusPayload(stateMachine,
-                                                         stateMachine.identifier(),
-                                                         eapPacket);
+                                radiusPayload = getRadiusPayload(stateMachine,
+                                        radiusIdentifier.identifier(), eapPacket);
                                 radiusPayload = pktCustomizer.customizePacket(radiusPayload, inPacket);
 
                                 if (stateMachine.challengeState() != null) {
@@ -820,7 +853,7 @@
                         case EAP.ATTR_TLS:
                             log.debug("EAP packet: EAPOL_PACKET ATTR_TLS");
                             // request id access to RADIUS
-                            radiusPayload = getRadiusPayload(stateMachine, stateMachine.identifier(), eapPacket);
+                            radiusPayload = getRadiusPayload(stateMachine, radiusIdentifier.identifier(), eapPacket);
                             radiusPayload = pktCustomizer.customizePacket(radiusPayload, inPacket);
 
                             if (stateMachine.challengeState() != null) {
@@ -865,10 +898,25 @@
         public void notify(AuthenticationEvent authenticationEvent) {
             log.info("Auth event {} for {}",
                     authenticationEvent.type(), authenticationEvent.subject());
+
+            if (authenticationEvent.type() == AuthenticationEvent.Type.TIMEOUT) {
+                handleStateMachineTimeout(authenticationEvent.subject());
+            }
+
             post(authenticationEvent);
         }
     }
 
+    private void handleStateMachineTimeout(ConnectPoint supplicantConnectPoint) {
+        StateMachine stateMachine = stateMachines.remove(sessionId(supplicantConnectPoint));
+
+        if (stateMachine.state() == StateMachine.STATE_PENDING && stateMachine.isWaitingForRadiusResponse()) {
+            aaaStatisticsManager.getAaaStats().increaseTimedOutPackets();
+        }
+
+        StateMachine.deleteStateMachineMapping(stateMachine);
+    }
+
     /**
      * Configuration Listener, handles change in configuration.
      */
@@ -953,7 +1001,7 @@
                     PortNumber portNumber = event.port().number();
                     String sessionId = devId.toString() + portNumber.toString();
 
-                    StateMachine stateMachine = StateMachine.lookupStateMachineBySessionId(sessionId);
+                    StateMachine stateMachine = stateMachines.get(sessionId);
                     if (stateMachine != null) {
                         stateMachine.setSessionTerminateReason(
                                 StateMachine.SessionTerminationReasons.PORT_REMOVED.getReason());
@@ -963,8 +1011,7 @@
                     aaaSupplicantStatsManager.getMachineStatsDelegate()
                             .notify(new AaaMachineStatisticsEvent(AaaMachineStatisticsEvent.Type.STATS_UPDATE, obj));
 
-                    Map<String, StateMachine> sessionIdMap = StateMachine.sessionIdMap();
-                    StateMachine removed = sessionIdMap.remove(sessionId);
+                    StateMachine removed = stateMachines.remove(sessionId);
                     if (removed != null) {
                         StateMachine.deleteStateMachineMapping(removed);
                     }
@@ -1034,4 +1081,4 @@
         }
 
     }
-}
\ No newline at end of file
+}
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 5b3e439..125898e 100644
--- a/app/src/main/java/org/opencord/aaa/impl/AaaStatisticsManager.java
+++ b/app/src/main/java/org/opencord/aaa/impl/AaaStatisticsManager.java
@@ -16,13 +16,6 @@
 
 package org.opencord.aaa.impl;
 
-import static org.slf4j.LoggerFactory.getLogger;
-
-import java.util.HashMap;
-import java.util.Map;
-import java.util.concurrent.atomic.AtomicLong;
-
-import org.osgi.service.component.annotations.Component;
 import org.onosproject.event.AbstractListenerManager;
 import org.opencord.aaa.AaaStatistics;
 import org.opencord.aaa.AuthenticationStatisticsDelegate;
@@ -30,9 +23,16 @@
 import org.opencord.aaa.AuthenticationStatisticsEventListener;
 import org.opencord.aaa.AuthenticationStatisticsService;
 import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
 import org.osgi.service.component.annotations.Deactivate;
 import org.slf4j.Logger;
 
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicLong;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
 
 
 @Component(immediate = true)
diff --git a/app/src/main/java/org/opencord/aaa/impl/IdentifierManager.java b/app/src/main/java/org/opencord/aaa/impl/IdentifierManager.java
new file mode 100644
index 0000000..27f824f
--- /dev/null
+++ b/app/src/main/java/org/opencord/aaa/impl/IdentifierManager.java
@@ -0,0 +1,95 @@
+/*
+ * Copyright 2020-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 com.google.common.collect.Maps;
+
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.LinkedBlockingQueue;
+
+/**
+ * Manages allocating request identifiers and mapping them to sessions.
+ */
+public class IdentifierManager {
+
+    private static final int MAX_IDENTIFIER = 256;
+
+    private BlockingQueue<Integer> freeIdNumbers;
+
+    private ConcurrentMap<RequestIdentifier, String> idToSession;
+
+    /**
+     * Creates and initializes a new identifier manager.
+     */
+    public IdentifierManager() {
+        idToSession = Maps.newConcurrentMap();
+        freeIdNumbers = new LinkedBlockingQueue<>();
+
+        // Starts at 2 because ids 0 and 1 are reserved for RADIUS server status requests.
+        for (int i = 2; i < MAX_IDENTIFIER; i++) {
+            freeIdNumbers.add(i);
+        }
+    }
+
+    /**
+     * Gets a new identifier and maps it to the given session ID.
+     *
+     * @param sessionId session this identifier is associated with
+     * @return identifier
+     */
+    public synchronized RequestIdentifier getNewIdentifier(String sessionId) {
+        int idNum;
+        try {
+            idNum = freeIdNumbers.take();
+        } catch (InterruptedException e) {
+            return null;
+        }
+
+        RequestIdentifier id = RequestIdentifier.of((byte) idNum);
+
+        idToSession.put(id, sessionId);
+
+        return id;
+    }
+
+    /**
+     * Gets the session ID associated with a given request ID.
+     *
+     * @param id request ID
+     * @return session ID
+     */
+    public String getSessionId(RequestIdentifier id) {
+        return idToSession.get(id);
+    }
+
+    /**
+     * Releases a request identifier and removes session mapping.
+     *
+     * @param id request identifier to release
+     */
+    public synchronized void releaseIdentifier(RequestIdentifier id) {
+        String session = idToSession.remove(id);
+        if (session == null) {
+            // this id wasn't mapped to a session so is still free
+            return;
+        }
+
+        // add id number back to set of free ids
+        freeIdNumbers.add((int) id.identifier());
+    }
+}
diff --git a/app/src/main/java/org/opencord/aaa/impl/PortBasedRadiusCommunicator.java b/app/src/main/java/org/opencord/aaa/impl/PortBasedRadiusCommunicator.java
index ad00b5f..4cdf958 100755
--- a/app/src/main/java/org/opencord/aaa/impl/PortBasedRadiusCommunicator.java
+++ b/app/src/main/java/org/opencord/aaa/impl/PortBasedRadiusCommunicator.java
@@ -391,12 +391,8 @@
                                     .deserialize(udpPacket.serialize(),
                                             8,
                                             udpPacket.getLength() - 8);
-                    try {
-                        aaaManager.aaaStatisticsManager.handleRoundtripTime(radiusMsg.getIdentifier());
-                        aaaManager.handleRadiusPacket(radiusMsg);
-                    }  catch (StateMachineException sme) {
-                        log.error("Illegal state machine operation", sme);
-                    }
+                    aaaManager.aaaStatisticsManager.handleRoundtripTime(radiusMsg.getIdentifier());
+                    aaaManager.handleRadiusPacket(radiusMsg);
                 } catch (DeserializationException dex) {
                     log.error("Cannot deserialize packet", dex);
                 }
diff --git a/app/src/main/java/org/opencord/aaa/impl/RequestIdentifier.java b/app/src/main/java/org/opencord/aaa/impl/RequestIdentifier.java
new file mode 100644
index 0000000..9869af1
--- /dev/null
+++ b/app/src/main/java/org/opencord/aaa/impl/RequestIdentifier.java
@@ -0,0 +1,69 @@
+/*
+ * Copyright 2020-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 java.util.Objects;
+
+/**
+ * An identifier for an authentication request.
+ */
+public final class RequestIdentifier {
+
+    private byte identifier;
+
+    /**
+     * Creates a new request identifier.
+     *
+     * @param identifier id number
+     */
+    private RequestIdentifier(byte identifier) {
+        this.identifier = identifier;
+    }
+
+    /**
+     * Returns the id number.
+     *
+     * @return id
+     */
+    public byte identifier() {
+        return this.identifier;
+    }
+
+    /**
+     * Creates a new request identifier.
+     *
+     * @param identifier id number
+     * @return identifier
+     */
+    public static RequestIdentifier of(byte identifier) {
+        return new RequestIdentifier(identifier);
+    }
+
+    public boolean equals(Object other) {
+        if (!(other instanceof RequestIdentifier)) {
+            return false;
+        }
+
+        RequestIdentifier that = (RequestIdentifier) other;
+
+        return identifier == that.identifier;
+    }
+
+    public int hashCode() {
+        return Objects.hashCode(identifier);
+    }
+}
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 d3c49c2..99c9396 100755
--- a/app/src/main/java/org/opencord/aaa/impl/SocketBasedRadiusCommunicator.java
+++ b/app/src/main/java/org/opencord/aaa/impl/SocketBasedRadiusCommunicator.java
@@ -198,10 +198,7 @@
                     } catch (DeserializationException dex) {
                         aaaManager.aaaStatisticsManager.getAaaStats().increaseMalformedResponsesRx();
                         log.error("Cannot deserialize packet", dex);
-                    } catch (StateMachineException sme) {
-                        log.error("Illegal state machine operation", sme);
                     }
-
                 } catch (IOException e) {
                     log.info("Socket was closed, exiting listener thread");
                     done = true;
diff --git a/app/src/main/java/org/opencord/aaa/impl/StateMachine.java b/app/src/main/java/org/opencord/aaa/impl/StateMachine.java
index 8d73e15..744b0ae 100644
--- a/app/src/main/java/org/opencord/aaa/impl/StateMachine.java
+++ b/app/src/main/java/org/opencord/aaa/impl/StateMachine.java
@@ -17,7 +17,6 @@
 
 package org.opencord.aaa.impl;
 
-import com.google.common.collect.Maps;
 import org.onlab.packet.MacAddress;
 import org.onosproject.net.ConnectPoint;
 import org.opencord.aaa.AuthenticationEvent;
@@ -25,15 +24,15 @@
 import org.slf4j.Logger;
 
 import java.util.HashSet;
-import java.util.Map;
 import java.util.Set;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
 
 import static org.slf4j.LoggerFactory.getLogger;
 
 /**
  * AAA Finite State Machine.
  */
-
 public class StateMachine {
     //INDEX to identify the state in the transition table
     static final int STATE_IDLE = 0;
@@ -43,7 +42,7 @@
     static final int STATE_UNAUTHORIZED = 4;
 
     // Defining the states where timeout can happen
-    static final Set<Integer> TIMEOUT_ELIGIBLE_STATES = new HashSet();
+    static final Set<Integer> TIMEOUT_ELIGIBLE_STATES = new HashSet<>();
     static {
         TIMEOUT_ELIGIBLE_STATES.add(STATE_STARTED);
         TIMEOUT_ELIGIBLE_STATES.add(STATE_PENDING);
@@ -55,7 +54,7 @@
     static final int TRANSITION_DENY_ACCESS = 3;
     static final int TRANSITION_LOGOFF = 4;
 
-    private static int identifier = 1;
+    private static int identifier = -1;
     private byte challengeIdentifier;
     private byte[] challengeState;
     private byte[] username;
@@ -120,6 +119,7 @@
     private State[] states = {new Idle(), new Started(), new Pending(), new Authorized(), new Unauthorized() };
 
     // Cleanup Timer instance created for this session
+    private ScheduledExecutorService executor;
     private java.util.concurrent.ScheduledFuture<?> cleanupTimer = null;
 
     // TimeStamp of last EAPOL or RADIUS message received.
@@ -158,24 +158,8 @@
 
     private int currentState = STATE_IDLE;
 
-    // Maps of state machines. Each state machine is represented by an
-    // unique identifier on the switch: dpid + port number
-    private static Map<String, StateMachine> sessionIdMap;
-    private static Map<Integer, StateMachine> identifierMap;
-
     private static StateMachineDelegate delegate;
 
-    public static void initializeMaps() {
-        sessionIdMap = Maps.newConcurrentMap();
-        identifierMap = Maps.newConcurrentMap();
-        identifier = 1;
-    }
-
-    public static void destroyMaps() {
-        sessionIdMap = null;
-        identifierMap = null;
-    }
-
     public static void setDelegate(StateMachineDelegate delegate) {
         StateMachine.delegate = delegate;
     }
@@ -184,38 +168,27 @@
         cleanupTimerTimeOutInMins = cleanupTimerTimeoutInMins;
     }
 
+    private void scheduleTimeout() {
+        cleanupTimer = executor.schedule(this::timeout, cleanupTimerTimeOutInMins, TimeUnit.MINUTES);
+    }
+
     public static void unsetDelegate(StateMachineDelegate delegate) {
         if (StateMachine.delegate == delegate) {
             StateMachine.delegate = null;
         }
     }
 
-    public static Map<String, StateMachine> sessionIdMap() {
-        return sessionIdMap;
-    }
-
-    public static StateMachine lookupStateMachineById(byte identifier) {
-        return identifierMap.get((int) identifier);
-    }
-
-    public static StateMachine lookupStateMachineBySessionId(String sessionId) {
-        return sessionIdMap.get(sessionId);
-    }
-
-    public static void deleteStateMachineId(String sessionId) {
-        sessionIdMap.remove(sessionId);
-    }
-
     public static void deleteStateMachineMapping(StateMachine machine) {
-        identifierMap.entrySet().removeIf(e -> e.getValue().equals(machine));
         if (machine.cleanupTimer != null) {
             machine.cleanupTimer.cancel(false);
             machine.cleanupTimer = null;
         }
     }
 
-    public java.util.concurrent.ScheduledFuture<?> getCleanupTimer() {
-        return cleanupTimer;
+    public void stop() {
+        if (cleanupTimer != null) {
+            cleanupTimer.cancel(false);
+        }
     }
 
     public boolean isWaitingForRadiusResponse() {
@@ -226,43 +199,16 @@
         this.waitingForRadiusResponse = waitingForRadiusResponse;
     }
 
-    public void setCleanupTimer(java.util.concurrent.ScheduledFuture<?> cleanupTimer) {
-        this.cleanupTimer = cleanupTimer;
-    }
-
-    /**
-     * Deletes authentication state machine records for a given MAC address.
-     *
-     * @param mac mac address of the suppliant who's state machine should be removed
-     */
-    public static void deleteByMac(MacAddress mac) {
-
-        // Walk the map from session IDs to state machines looking for a MAC match
-        for (Map.Entry<String, StateMachine> e : sessionIdMap.entrySet()) {
-
-            // If a MAC match is found then delete the entry from the session ID
-            // and identifier map as well as call delete identifier to clean up
-            // the identifier bit set.
-            if (e.getValue() != null && e.getValue().supplicantAddress != null
-                    && e.getValue().supplicantAddress.equals(mac)) {
-                sessionIdMap.remove(e.getValue().sessionId);
-                if (e.getValue().identifier != 1) {
-                    deleteStateMachineMapping(e.getValue());
-                }
-                break;
-            }
-        }
-    }
-
     /**
      * Creates a new StateMachine with the given session ID.
      *
      * @param sessionId session Id represented by the switch dpid + port number
+     * @param executor executor to run background tasks on
      */
-    public StateMachine(String sessionId) {
+    public StateMachine(String sessionId, ScheduledExecutorService executor) {
         log.info("Creating a new state machine for {}", sessionId);
         this.sessionId = sessionId;
-        sessionIdMap.put(sessionId, this);
+        this.executor = executor;
     }
 
     /**
@@ -538,11 +484,8 @@
      * @return The state machine identifier.
      */
     public synchronized byte identifier() {
-        //identifier 0 is for statusServerrequest
-        //identifier 1 is for fake accessRequest
-        identifier = (identifier + 1) % 253;
-        identifierMap.put((identifier + 2), this);
-        return (byte) (identifier + 2);
+        identifier = (identifier + 1) % 255;
+        return (byte) identifier;
     }
 
     /**
@@ -557,26 +500,23 @@
 
     /**
      * Client has requested the start action to allow network access.
-     *
-     * @throws StateMachineException if authentication protocol is violated
      */
-    public void start() throws StateMachineException {
+    public void start() {
+        this.scheduleTimeout();
+
         states[currentState].start();
 
         delegate.notify(new AuthenticationEvent(AuthenticationEvent.Type.STARTED, supplicantConnectpoint));
 
         // move to the next state
         next(TRANSITION_START);
-        identifier = this.identifier();
     }
 
     /**
      * An Identification information has been sent by the supplicant. Move to the
      * next state if possible.
-     *
-     * @throws StateMachineException if authentication protocol is violated
      */
-    public void requestAccess() throws StateMachineException {
+    public void requestAccess() {
         states[currentState].requestAccess();
 
         delegate.notify(new AuthenticationEvent(AuthenticationEvent.Type.REQUESTED, supplicantConnectpoint));
@@ -587,10 +527,8 @@
 
     /**
      * RADIUS has accepted the identification. Move to the next state if possible.
-     *
-     * @throws StateMachineException if authentication protocol is violated
      */
-    public void authorizeAccess() throws StateMachineException {
+    public void authorizeAccess() {
         states[currentState].radiusAccepted();
         // move to the next state
         next(TRANSITION_AUTHORIZE_ACCESS);
@@ -603,10 +541,8 @@
 
     /**
      * RADIUS has denied the identification. Move to the next state if possible.
-     *
-     * @throws StateMachineException if authentication protocol is violated
      */
-    public void denyAccess() throws StateMachineException {
+    public void denyAccess() {
         states[currentState].radiusDenied();
         // move to the next state
         next(TRANSITION_DENY_ACCESS);
@@ -619,10 +555,8 @@
 
     /**
      * Logoff request has been requested. Move to the next state if possible.
-     *
-     * @throws StateMachineException if authentication protocol is violated
      */
-    public void logoff() throws StateMachineException {
+    public void logoff() {
         states[currentState].logoff();
         // move to the next state
         next(TRANSITION_LOGOFF);
@@ -638,46 +572,54 @@
         return currentState;
     }
 
-    @Override
-    public String toString() {
-        return ("sessionId: " + this.sessionId) + "\t" + ("identifier: " + this.identifier) + "\t"
-                + ("state: " + this.currentState);
+    public String stateString() {
+        return states[currentState].name();
     }
 
-    abstract class State {
+    @Override
+    public String toString() {
+        return ("sessionId: " + this.sessionId) + "\t" + ("state: " + this.currentState);
+    }
+
+    abstract static class State {
         private final Logger log = getLogger(getClass());
 
-        private String name = "State";
+        abstract String name();
 
-        public void start() throws StateMachineInvalidTransitionException {
+        public void start() {
             log.warn("START transition from this state is not allowed.");
         }
 
-        public void requestAccess() throws StateMachineInvalidTransitionException {
+        public void requestAccess() {
             log.warn("REQUEST ACCESS transition from this state is not allowed.");
         }
 
-        public void radiusAccepted() throws StateMachineInvalidTransitionException {
+        public void radiusAccepted() {
             log.warn("AUTHORIZE ACCESS transition from this state is not allowed.");
         }
 
-        public void radiusDenied() throws StateMachineInvalidTransitionException {
+        public void radiusDenied() {
             log.warn("DENY ACCESS transition from this state is not allowed.");
         }
 
-        public void logoff() throws StateMachineInvalidTransitionException {
+        public void logoff() {
             log.warn("LOGOFF transition from this state is not allowed.");
         }
     }
 
     /**
-     * Idle state: supplicant is logged of from the network.
+     * Idle state: supplicant is logged off from the network.
      */
-    class Idle extends State {
+    static class Idle extends State {
         private final Logger log = getLogger(getClass());
         private String name = "IDLE_STATE";
 
         @Override
+        String name() {
+            return this.name;
+        }
+
+        @Override
         public void start() {
             log.info("Moving from IDLE state to STARTED state.");
         }
@@ -687,11 +629,16 @@
      * Started state: supplicant has entered the network and informed the
      * authenticator.
      */
-    class Started extends State {
+    static class Started extends State {
         private final Logger log = getLogger(getClass());
         private String name = "STARTED_STATE";
 
         @Override
+        String name() {
+            return this.name;
+        }
+
+        @Override
         public void requestAccess() {
             log.info("Moving from STARTED state to PENDING state.");
         }
@@ -701,11 +648,16 @@
      * Pending state: supplicant has been identified by the authenticator but has
      * not access yet.
      */
-    class Pending extends State {
+    static class Pending extends State {
         private final Logger log = getLogger(getClass());
         private String name = "PENDING_STATE";
 
         @Override
+        String name() {
+            return this.name;
+        }
+
+        @Override
         public void radiusAccepted() {
             log.info("Moving from PENDING state to AUTHORIZED state.");
         }
@@ -719,11 +671,16 @@
     /**
      * Authorized state: supplicant port has been accepted, access is granted.
      */
-    class Authorized extends State {
+    static class Authorized extends State {
         private final Logger log = getLogger(getClass());
         private String name = "AUTHORIZED_STATE";
 
         @Override
+        String name() {
+            return this.name;
+        }
+
+        @Override
         public void start() {
             log.info("Moving from AUTHORIZED state to STARTED state.");
         }
@@ -738,11 +695,16 @@
     /**
      * Unauthorized state: supplicant port has been rejected, access is denied.
      */
-    class Unauthorized extends State {
+    static class Unauthorized extends State {
         private final Logger log = getLogger(getClass());
         private String name = "UNAUTHORIZED_STATE";
 
         @Override
+        String name() {
+            return this.name;
+        }
+
+        @Override
         public void start() {
             log.info("Moving from UNAUTHORIZED state to STARTED state.");
         }
@@ -753,53 +715,15 @@
         }
     }
 
-    /**
-     * Class for cleaning the StateMachine for those session for which no response
-     * is coming--implementing timeout.
-     */
-    class CleanupTimerTask implements Runnable {
-        private final Logger log = getLogger(getClass());
-        private String sessionId;
-        private AaaManager aaaManager;
+    private void timeout() {
+        boolean noTrafficWithinThreshold =
+                (System.currentTimeMillis() - lastPacketReceivedTime) > ((cleanupTimerTimeOutInMins * 60 * 1000) / 2);
 
-        CleanupTimerTask(String sessionId, AaaManager aaaManager) {
-            this.sessionId = sessionId;
-            this.aaaManager = aaaManager;
-        }
-
-        @Override
-        public void run() {
-            StateMachine stateMachine = StateMachine.lookupStateMachineBySessionId(sessionId);
-            if (null != stateMachine) {
-                // Asserting if last packet received for this stateMachine session was beyond half of timeout period.
-                // StateMachine is considered eligible for cleanup when no packets has been exchanged by it with AAA
-                // Server or RG during a long period (half of timeout period). For example, when cleanup timer has
-                // been configured as 10 minutes, StateMachine would be cleaned up at the end of 10 minutes if
-                // the authentication is still pending and no packet was exchanged for this session during last 5
-                // minutes.
-
-                boolean noTrafficWithinThreshold = (System.currentTimeMillis()
-                        - stateMachine.getLastPacketReceivedTime()) > ((cleanupTimerTimeOutInMins * 60 * 1000) / 2);
-
-                        if ((TIMEOUT_ELIGIBLE_STATES.contains(stateMachine.state())) && noTrafficWithinThreshold) {
-                            log.info("Deleting StateMachineMapping for sessionId: {}", sessionId);
-                            cleanupTimer = null;
-                            if (stateMachine.state() == STATE_PENDING && stateMachine.isWaitingForRadiusResponse()) {
-                                aaaManager.aaaStatisticsManager.getAaaStats().increaseTimedOutPackets();
-                            }
-                            deleteStateMachineId(sessionId);
-                            deleteStateMachineMapping(stateMachine);
-
-                            // If StateMachine is not eligible for cleanup yet, reschedule cleanupTimer further.
-                        } else {
-                            aaaManager.scheduleStateMachineCleanupTimer(sessionId, stateMachine);
-                        }
-            } else {
-                // This statement should not be logged; cleanupTimer should be cancelled for stateMachine
-                // instances which have been authenticated successfully.
-                log.warn("state-machine not found for sessionId: {}", sessionId);
-            }
-
+        if (TIMEOUT_ELIGIBLE_STATES.contains(currentState) && noTrafficWithinThreshold) {
+            delegate.notify(new AuthenticationEvent(AuthenticationEvent.Type.TIMEOUT, this.supplicantConnectpoint));
+            // If StateMachine is not eligible for cleanup yet, reschedule cleanupTimer further.
+        } else {
+            this.scheduleTimeout();
         }
     }
 
diff --git a/app/src/main/java/org/opencord/aaa/impl/StateMachineException.java b/app/src/main/java/org/opencord/aaa/impl/StateMachineException.java
deleted file mode 100644
index 6dd7bb4..0000000
--- a/app/src/main/java/org/opencord/aaa/impl/StateMachineException.java
+++ /dev/null
@@ -1,27 +0,0 @@
-/*
- * Copyright 2017-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;
-
-/**
- * Exception for the State Machine.
- */
-class StateMachineException extends Exception {
-    public StateMachineException(String message) {
-        super(message);
-
-    }
-}
diff --git a/app/src/main/java/org/opencord/aaa/impl/StateMachineInvalidTransitionException.java b/app/src/main/java/org/opencord/aaa/impl/StateMachineInvalidTransitionException.java
deleted file mode 100644
index 6ccd3fc..0000000
--- a/app/src/main/java/org/opencord/aaa/impl/StateMachineInvalidTransitionException.java
+++ /dev/null
@@ -1,26 +0,0 @@
-/*
- * Copyright 2017-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;
-
-/**
- * Exception raised when the transition from one state to another is invalid.
- */
-class StateMachineInvalidTransitionException extends StateMachineException {
-    public StateMachineInvalidTransitionException(String message) {
-        super(message);
-    }
-}