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;
* @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;
}
*/
public void start() {
LOG.debug("starting ..");
- LOG.debug("switchConnectionProvider: "+switchConnectionProvider);
+ LOG.debug("switchConnectionProvider: " + switchConnectionProvider);
// setup handler
SwitchConnectionHandler switchConnectionHandler = new SwitchConnectionHandlerImpl();
switchConnectionProvider.setSwitchConnectionHandler(switchConnectionHandler);
/**
* @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);
}
*/
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);
+ }
}
/**
// 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);
+ }
+ }
+
+
}