Merge "Bug 8873 - Bundle based reconciliation to enable bundling of messages"
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / session / SessionManagerOFImpl.java
index baf0f21c238603c356ad28d2dc3a8f1b476c2e03..67cc3efce7e2abb1867c77a02ca0dd48e38ce6b2 100644 (file)
@@ -99,13 +99,18 @@ public class SessionManagerOFImpl implements ConjunctSessionManager {
             LOG.info("context for invalidation not found");
         } else {
             synchronized (context) {
-                for (Entry<SwitchConnectionDistinguisher, ConnectionConductor> auxEntry : context.getAuxiliaryConductors()) {
-                    invalidateAuxiliary(sessionKey, auxEntry.getKey());
+                if (context.isValid()) {
+                    for (Entry<SwitchConnectionDistinguisher, ConnectionConductor> auxEntry : context.getAuxiliaryConductors()) {
+                        invalidateAuxiliary(sessionKey, auxEntry.getKey());
+                    }
+                    context.getPrimaryConductor().disconnect();
+                    context.setValid(false);
+                    removeSessionContext(context);
+                    // TODO:: notify listeners
+                } else {
+                    LOG.warn("Ignore invalid session context: {}",
+                             Arrays.toString(sessionKey.getId()));
                 }
-                context.getPrimaryConductor().disconnect();
-                context.setValid(false);
-                removeSessionContext(context);
-                // TODO:: notify listeners
             }
         }
     }
@@ -115,13 +120,19 @@ public class SessionManagerOFImpl implements ConjunctSessionManager {
             LOG.info("context for invalidation not found");
         } else {
             synchronized (sessionContext) {
-                for (Entry<SwitchConnectionDistinguisher, ConnectionConductor> auxEntry : sessionContext
-                        .getAuxiliaryConductors()) {
-                    invalidateAuxiliary(sessionContext, auxEntry.getKey(), true);
+                if (sessionContext.isValid()) {
+                    for (Entry<SwitchConnectionDistinguisher, ConnectionConductor> auxEntry : sessionContext
+                             .getAuxiliaryConductors()) {
+                        invalidateAuxiliary(sessionContext, auxEntry.getKey(), true);
+                    }
+                    sessionContext.setValid(false);
+                    removeSessionContext(sessionContext);
+                    // TODO:: notify listeners
+                } else {
+                    LOG.warn("Ignore invalid dead session context: {}",
+                             Arrays.toString(
+                                 sessionContext.getSessionKey().getId()));
                 }
-                sessionContext.setValid(false);
-                removeSessionContext(sessionContext);
-                // TODO:: notify listeners
             }
         }
     }
@@ -130,8 +141,13 @@ public class SessionManagerOFImpl implements ConjunctSessionManager {
         if (LOG.isDebugEnabled()) {
             LOG.debug("removing session: {}", Arrays.toString(sessionContext.getSessionKey().getId()));
         }
-        sessionLot.remove(sessionContext.getSessionKey(), sessionContext);
-        sessionNotifier.onSessionRemoved(sessionContext);
+        if (sessionLot.remove(sessionContext.getSessionKey(), sessionContext)) {
+            sessionNotifier.onSessionRemoved(sessionContext);
+        } else {
+            // This should never happen.
+            LOG.warn("Ignore session context that was already removed: {}",
+                     Arrays.toString(sessionContext.getSessionKey().getId()));
+        }
     }
 
     @Override
@@ -143,6 +159,10 @@ public class SessionManagerOFImpl implements ConjunctSessionManager {
         }
     }
 
+    @Override
+    public void setRole(SessionContext context) {
+       sessionNotifier.setRole(context);
+    }
     @Override
     public void invalidateAuxiliary(SwitchSessionKeyOF sessionKey,
                                     SwitchConnectionDistinguisher connectionCookie) {
@@ -205,6 +225,17 @@ public class SessionManagerOFImpl implements ConjunctSessionManager {
             }
         }
 
+        @Override
+        public void setRole(SessionContext context) {
+            for (ListenerRegistration<SessionListener> listener : sessionListeners) {
+                try {
+                    listener.getInstance().setRole(context);
+                } catch (Exception e) {
+                    LOG.error("Unhandled exeption occured while invoking setRole on listener", e);
+                }
+            }
+        }
+
         @Override
         public void onSessionRemoved(SessionContext context) {
             for (ListenerRegistration<SessionListener> listener : sessionListeners) {