preparing QueueKeeper and message translation
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / session / SessionManagerOFImpl.java
index 351b1b00316e970d147de19581fe608475edaace..73e101c08a42b8e9dfee0a03704526b67aa3ee0a 100644 (file)
@@ -8,15 +8,17 @@
 
 package org.opendaylight.openflowplugin.openflow.md.core.session;
 
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.concurrent.ConcurrentHashMap;
 
-import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
 import org.opendaylight.openflowplugin.openflow.md.core.ConnectionConductor;
-import org.opendaylight.openflowplugin.openflow.md.core.IMDMessageListener;
+import org.opendaylight.openflowplugin.openflow.md.core.IMDMessageTranslator;
 import org.opendaylight.openflowplugin.openflow.md.core.SwitchConnectionDistinguisher;
+import org.opendaylight.openflowplugin.openflow.md.core.TranslatorKey;
+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.yang.binding.DataObject;
@@ -28,12 +30,12 @@ import org.slf4j.LoggerFactory;
  */
 public class SessionManagerOFImpl implements SessionManager {
 
-    private static final Logger LOG = LoggerFactory.getLogger(SessionManagerOFImpl.class);
+    protected static final Logger LOG = LoggerFactory.getLogger(SessionManagerOFImpl.class);
     private static SessionManagerOFImpl instance;
     private ConcurrentHashMap<SwitchConnectionDistinguisher, SessionContext> sessionLot;
-    private Map<Class<? extends DataObject>, Collection<IMDMessageListener>> listenerMapping;
+    private Map<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, DataObject>>> translatorMapping;
 
-    private final ListenerRegistry<SessionListener> sessionListeners = new ListenerRegistry<>();
+    protected final ListenerRegistry<SessionListener> sessionListeners = new ListenerRegistry<>();
 
     /**
      * @return singleton instance
@@ -65,7 +67,7 @@ public class SessionManagerOFImpl implements SessionManager {
             }
             context.getPrimaryConductor().disconnect();
             context.setValid(false);
-            sessionLot.remove(sessionKey);
+            removeSessionContext(context);
             // TODO:: notify listeners
         }
     }
@@ -79,11 +81,19 @@ public class SessionManagerOFImpl implements SessionManager {
                 invalidateAuxiliary(sessionContext, auxEntry.getKey(), true);
             }
             sessionContext.setValid(false);
-            sessionLot.remove(sessionContext.getSessionKey(), sessionContext);
+            removeSessionContext(sessionContext);
             // TODO:: notify listeners
         }
     }
 
+    private void removeSessionContext(SessionContext sessionContext) {
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("removing session: {}", Arrays.toString(sessionContext.getSessionKey().getId()));
+        }
+        sessionLot.remove(sessionContext.getSessionKey(), sessionContext);
+        sessionNotifier.onSessionRemoved(sessionContext);
+    }
+
     @Override
     public void addSessionContext(SwitchConnectionDistinguisher sessionKey, SessionContext context) {
         sessionLot.put(sessionKey, context);
@@ -131,12 +141,9 @@ public class SessionManagerOFImpl implements SessionManager {
         }
     }
 
-    /**
-     * @param listenerMapping
-     *            the listenerMapping to set
-     */
-    public void setListenerMapping(Map<Class<? extends DataObject>, Collection<IMDMessageListener>> listenerMapping) {
-        this.listenerMapping = listenerMapping;
+    @Override
+    public void setTranslatorMapping(Map<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, DataObject>>> translatorMapping) {
+        this.translatorMapping = translatorMapping;
     }
 
     @Override
@@ -156,6 +163,21 @@ public class SessionManagerOFImpl implements SessionManager {
                 }
             }
         }
+
+        public void onSessionRemoved(SessionContext context) {
+            for (ListenerRegistration<SessionListener> listener : sessionListeners) {
+                try {
+                    listener.getInstance().onSessionRemoved(context);
+                } catch (Exception e) {
+                    LOG.error("Unhandled exeption occured while invoking onSessionRemoved on listener", e);
+                }
+            }
+        }
     };
 
+    @Override
+    public Map<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, DataObject>>> getTranslatorMapping() {
+        return this.translatorMapping;
+    }
+
 }