Hooking up SessionManager to Conductor for ListenerMap
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / MDController.java
index d450573cf89f18d7c8f8b6c803119ce7d83654ea..1ba75ed1c32a1c537e5a4d6356f1ae0fd7e3b44e 100644 (file)
@@ -8,13 +8,22 @@
 
 package org.opendaylight.openflowplugin.openflow.md.core;
 
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
 
 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionConfiguration;
 import org.opendaylight.openflowjava.protocol.api.connection.SwitchConnectionHandler;
 import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProvider;
+import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
+import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -24,26 +33,39 @@ import com.google.common.collect.Lists;
  * @author mirehak
  *
  */
-public class MDController {
+public class MDController implements IMDController {
 
-    private static final Logger LOG = LoggerFactory
-            .getLogger(MDController.class);
+    private static final Logger LOG = LoggerFactory.getLogger(MDController.class);
 
     private SwitchConnectionProvider switchConnectionProvider;
 
+    private ConcurrentMap<Class<? extends DataObject>, Collection<IMDMessageListener>> messageListeners;
+
+    public Map<Class<? extends DataObject>, Collection<IMDMessageListener>> getMessageListeners() {
+        return messageListeners;
+    }
+
+
+    public void init() {
+        LOG.debug("Initializing!");
+        this.messageListeners = new ConcurrentHashMap<Class<? extends DataObject>, Collection<IMDMessageListener>>();
+        // Push the updated Listeners to Session Manager which will be then picked up by ConnectionConductor eventually
+        OFSessionUtil.getSessionManager().setListenerMapping(messageListeners);
+    }
+
     /**
-     * @param switchConnectionProvider the switchConnectionProvider to set
+     * @param switchConnectionProvider
+     *            the switchConnectionProvider to set
      */
-    public void setSwitchConnectionProvider(
-            SwitchConnectionProvider switchConnectionProvider) {
+    public void setSwitchConnectionProvider(SwitchConnectionProvider switchConnectionProvider) {
         this.switchConnectionProvider = switchConnectionProvider;
     }
 
     /**
-     * @param switchConnectionProviderToUnset the switchConnectionProvider to unset
+     * @param switchConnectionProviderToUnset
+     *            the switchConnectionProvider to unset
      */
-    public void unsetSwitchConnectionProvider(
-            SwitchConnectionProvider switchConnectionProviderToUnset) {
+    public void unsetSwitchConnectionProvider(SwitchConnectionProvider switchConnectionProviderToUnset) {
         if (this.switchConnectionProvider == switchConnectionProviderToUnset) {
             this.switchConnectionProvider = null;
         }
@@ -56,7 +78,7 @@ public class MDController {
      */
     public void start() {
         LOG.debug("starting ..");
-        LOG.debug("switchConnectionProvider: "+switchConnectionProvider);
+        LOG.debug("switchConnectionProvider: " + switchConnectionProvider);
         // setup handler
         SwitchConnectionHandler switchConnectionHandler = new SwitchConnectionHandlerImpl();
         switchConnectionProvider.setSwitchConnectionHandler(switchConnectionHandler);
@@ -68,8 +90,8 @@ public class MDController {
     /**
      * @return wished connections configurations
      */
-    private Collection<ConnectionConfiguration> getConnectionConfiguration() {
-        //TODO:: get config from state manager
+    private static Collection<ConnectionConfiguration> getConnectionConfiguration() {
+        // TODO:: get config from state manager
         ConnectionConfiguration configuration = ConnectionConfigurationFactory.getDefault();
         return Lists.newArrayList(configuration);
     }
@@ -82,6 +104,12 @@ public class MDController {
      */
     public void stop() {
         LOG.debug("stopping");
+        Future<List<Boolean>> srvStopped = switchConnectionProvider.shutdown();
+        try {
+            srvStopped.get(5000, TimeUnit.MILLISECONDS);
+        } catch (InterruptedException | ExecutionException | TimeoutException e) {
+            LOG.error(e.getMessage(), e);
+        }
     }
 
     /**
@@ -94,4 +122,30 @@ public class MDController {
         // do nothing
     }
 
+    @Override
+    public void addMessageListener(Class<? extends DataObject> messageType, IMDMessageListener listener) {
+
+        Collection<IMDMessageListener> existingValues = messageListeners.get(messageType);
+        if (existingValues == null) {
+               existingValues = new ArrayList<IMDMessageListener>();
+               messageListeners.put(messageType, existingValues);
+        }
+        existingValues.add(listener);
+        LOG.debug("{} is now listened by {}", messageType, listener);
+    }
+
+    @Override
+    public void removeMessageListener(Class<? extends DataObject> messageType, IMDMessageListener listener) {
+
+        Collection<IMDMessageListener> values = messageListeners.get(messageType);
+        if (values != null) {
+                    values.remove(listener);
+                    if (values.size() == 0) {
+                        messageListeners.remove(messageType);
+                    }
+                    LOG.debug("{} is now removed", listener);
+         }
+    }
+
+
 }