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 d6b3579a6b1824fa80639611bb7a951102e25999..67cc3efce7e2abb1867c77a02ca0dd48e38ce6b2 100644 (file)
@@ -17,19 +17,19 @@ import java.util.Map.Entry;
 import java.util.concurrent.ConcurrentHashMap;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
+import org.opendaylight.openflowplugin.api.openflow.md.core.ConnectionConductor;
+import org.opendaylight.openflowplugin.api.openflow.md.core.IMDMessageTranslator;
 import org.opendaylight.openflowplugin.api.openflow.md.core.SwitchConnectionDistinguisher;
 import org.opendaylight.openflowplugin.api.openflow.md.core.TranslatorKey;
 import org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionContext;
 import org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionListener;
 import org.opendaylight.openflowplugin.api.openflow.md.core.session.SwitchSessionKeyOF;
-import org.opendaylight.openflowplugin.api.statistics.MessageSpy;
-import org.opendaylight.openflowplugin.api.openflow.md.core.ConnectionConductor;
-import org.opendaylight.openflowplugin.api.openflow.md.core.IMDMessageTranslator;
-import org.opendaylight.openflowplugin.extension.api.core.extension.ExtensionConverterProvider;
 import org.opendaylight.openflowplugin.api.openflow.md.queue.PopListener;
+import org.opendaylight.openflowplugin.api.openflow.statistics.MessageSpy;
+import org.opendaylight.openflowplugin.extension.api.core.extension.ExtensionConverterProvider;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
 import org.opendaylight.yangtools.concepts.ListenerRegistration;
-import org.opendaylight.yangtools.concepts.util.ListenerRegistry;
+import org.opendaylight.yangtools.util.ListenerRegistry;
 import org.opendaylight.yangtools.yang.binding.DataContainer;
 import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.slf4j.Logger;
@@ -68,7 +68,7 @@ public class SessionManagerOFImpl implements ConjunctSessionManager {
     }
 
     /**
-     * close and release singleton instace
+     * close and release singleton instance
      */
     public static void releaseInstance() {
         if (instance != null) {
@@ -96,32 +96,43 @@ public class SessionManagerOFImpl implements ConjunctSessionManager {
     public void invalidateSessionContext(SwitchSessionKeyOF sessionKey) {
         SessionContext context = getSessionContext(sessionKey);
         if (context == null) {
-            LOG.warn("context for invalidation not found");
+            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
             }
         }
     }
 
     private void invalidateDeadSessionContext(SessionContext sessionContext) {
         if (sessionContext == null) {
-            LOG.warn("context for invalidation not found");
+            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
@@ -139,11 +155,14 @@ public class SessionManagerOFImpl implements ConjunctSessionManager {
         synchronized (context) {
             sessionLot.put(sessionKey, context);
             sessionNotifier.onSessionAdded(sessionKey, context);
-
             context.setValid(true);
         }
     }
 
+    @Override
+    public void setRole(SessionContext context) {
+       sessionNotifier.setRole(context);
+    }
     @Override
     public void invalidateAuxiliary(SwitchSessionKeyOF sessionKey,
                                     SwitchConnectionDistinguisher connectionCookie) {
@@ -159,7 +178,7 @@ public class SessionManagerOFImpl implements ConjunctSessionManager {
     private static void invalidateAuxiliary(SessionContext context, SwitchConnectionDistinguisher connectionCookie,
                                             boolean disconnect) {
         if (context == null) {
-            LOG.warn("context for invalidation not found");
+            LOG.info("context for invalidation not found");
         } else {
             ConnectionConductor auxiliaryConductor = context.removeAuxiliaryConductor(connectionCookie);
             if (auxiliaryConductor == null) {
@@ -206,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) {
@@ -263,7 +293,6 @@ public class SessionManagerOFImpl implements ConjunctSessionManager {
     @Override
     public void close() {
         LOG.debug("close");
-        sessionListeners = null;
         synchronized (sessionLot) {
             for (SessionContext sessionContext : sessionLot.values()) {
                 sessionContext.getPrimaryConductor().disconnect();
@@ -271,6 +300,17 @@ public class SessionManagerOFImpl implements ConjunctSessionManager {
             // TODO: handle timeouted shutdown
             rpcPool.shutdown();
         }
+
+        for (ListenerRegistration<SessionListener> listenerRegistration : sessionListeners) {
+            SessionListener listener = listenerRegistration.getInstance();
+            if (listener instanceof AutoCloseable) {
+                try {
+                    ((AutoCloseable) listener).close();
+                } catch (Exception e) {
+                    LOG.warn("closing of sessionListenerRegistration failed", e);
+                }
+            }
+        }
     }
 
     @Override
@@ -306,4 +346,9 @@ public class SessionManagerOFImpl implements ConjunctSessionManager {
     public ExtensionConverterProvider getExtensionConverterProvider() {
         return extensionConverterProvider;
     }
+
+    @Override
+    public Collection<SessionContext> getAllSessions() {
+        return sessionLot.values();
+    }
 }