[VOL-2578] Handling DEVICE_AVALIABILITY_CHANGED event

Change-Id: I3b3b9567b5be8e563627c425f222c9537160b957
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 ee339e4..c2e62eb 100644
--- a/app/src/main/java/org/opencord/aaa/impl/AaaManager.java
+++ b/app/src/main/java/org/opencord/aaa/impl/AaaManager.java
@@ -1056,37 +1056,26 @@
     private class InternalDeviceListener implements DeviceListener {
         @Override
         public void event(DeviceEvent event) {
-
+            DeviceId deviceId = event.subject().id();
             switch (event.type()) {
                 case PORT_REMOVED:
-                    DeviceId devId = event.subject().id();
                     PortNumber portNumber = event.port().number();
-                    String sessionId = devId.toString() + portNumber.toString();
-
+                    String sessionId = deviceId.toString() + portNumber.toString();
                     log.debug("Received PORT_REMOVED event. Clearing AAA Session with Id {}", sessionId);
                     flushStateMachineSession(sessionId,
                             StateMachine.SessionTerminationReasons.PORT_REMOVED.getReason());
 
                     break;
-
+                case DEVICE_AVAILABILITY_CHANGED:
+                    if (!deviceService.isAvailable(deviceId)) {
+                        log.debug("Received DEVICE_AVAILABILITY_CHANGED event for {}, " +
+                                          "went available to un-available", deviceId);
+                        clearAllSessionStateForDevice(deviceId);
+                    }
+                    break;
                 case DEVICE_REMOVED:
-                    DeviceId deviceId = event.subject().id();
                     log.debug("Received DEVICE_REMOVED event for {}", deviceId);
-
-                    Set<String> associatedSessions = Sets.newHashSet();
-                    for (Entry<String, StateMachine> stateMachineEntry : stateMachines.entrySet()) {
-                        ConnectPoint cp = stateMachineEntry.getValue().supplicantConnectpoint();
-                        if (cp != null && cp.deviceId().toString().equals(deviceId.toString())) {
-                            associatedSessions.add(stateMachineEntry.getKey());
-                        }
-                    }
-
-                    for (String session : associatedSessions) {
-                        log.debug("Clearing AAA Session {} associated with Removed Device", session);
-                        flushStateMachineSession(session,
-                               StateMachine.SessionTerminationReasons.DEVICE_REMOVED.getReason());
-                    }
-
+                    clearAllSessionStateForDevice(deviceId);
                     break;
 
                 default:
@@ -1094,6 +1083,22 @@
             }
         }
 
+        private void clearAllSessionStateForDevice(DeviceId deviceId) {
+            Set<String> associatedSessions = Sets.newHashSet();
+            for (Entry<String, StateMachine> stateMachineEntry : stateMachines.entrySet()) {
+                ConnectPoint cp = stateMachineEntry.getValue().supplicantConnectpoint();
+                if (cp != null && cp.deviceId().toString().equals(deviceId.toString())) {
+                    associatedSessions.add(stateMachineEntry.getKey());
+                }
+            }
+
+            for (String session : associatedSessions) {
+                log.debug("Clearing AAA Session {} associated with Removed Device", session);
+                flushStateMachineSession(session,
+                       StateMachine.SessionTerminationReasons.DEVICE_REMOVED.getReason());
+            }
+        }
+
         private void flushStateMachineSession(String sessionId, String terminationReason) {
             StateMachine stateMachine = stateMachines.get(sessionId);
             if (stateMachine != null) {