Skip to content

Instantly share code, notes, and snippets.

@mavenugo
Last active August 29, 2015 13:58
Show Gist options
  • Save mavenugo/10364922 to your computer and use it in GitHub Desktop.
Save mavenugo/10364922 to your computer and use it in GitHub Desktop.
diff --git a/opendaylight/protocol_plugins/openflow/pom.xml b/opendaylight/protocol_plugins/openflow/pom.xml
index 48555de..1580f33 100644
--- a/opendaylight/protocol_plugins/openflow/pom.xml
+++ b/opendaylight/protocol_plugins/openflow/pom.xml
@@ -39,6 +39,7 @@
org.opendaylight.controller.sal.match,
org.opendaylight.controller.sal.utils,
org.opendaylight.controller.sal.connection,
+ org.opendaylight.controller.ofbroker,
org.apache.commons.lang3.builder,
org.apache.commons.lang3.tuple,
org.apache.felix.dm,
@@ -75,6 +76,11 @@
<artifactId>sal.connection</artifactId>
</dependency>
<dependency>
+ <groupId>org.opendaylight.controller</groupId>
+ <artifactId>ofbroker</artifactId>
+ <version>0.1.0-SNAPSHOT</version>
+ </dependency>
+ <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
diff --git a/opendaylight/protocol_plugins/openflow/src/main/java/org/opendaylight/controller/protocol_plugin/openflow/core/internal/Controller.java b/opendaylight/protocol_plugins/op
index 63dd0bc..4850bdb 100644
--- a/opendaylight/protocol_plugins/openflow/src/main/java/org/opendaylight/controller/protocol_plugin/openflow/core/internal/Controller.java
+++ b/opendaylight/protocol_plugins/openflow/src/main/java/org/opendaylight/controller/protocol_plugin/openflow/core/internal/Controller.java
@@ -9,6 +9,7 @@
package org.opendaylight.controller.protocol_plugin.openflow.core.internal;
import java.io.IOException;
+import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
@@ -16,6 +17,7 @@ import java.nio.channels.SocketChannel;
import java.util.Comparator;
import java.util.Date;
import java.util.Iterator;
+import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
@@ -26,6 +28,7 @@ import java.util.concurrent.atomic.AtomicInteger;
import org.eclipse.osgi.framework.console.CommandInterpreter;
import org.eclipse.osgi.framework.console.CommandProvider;
+import org.opendaylight.controller.ofbroker.IOFPlugin;
import org.opendaylight.controller.protocol_plugin.openflow.core.IController;
import org.opendaylight.controller.protocol_plugin.openflow.core.IMessageListener;
import org.opendaylight.controller.protocol_plugin.openflow.core.ISwitch;
@@ -35,15 +38,17 @@ import org.opendaylight.controller.sal.connection.IPluginInConnectionService;
import org.opendaylight.controller.sal.core.Node;
import org.opendaylight.controller.sal.utils.Status;
import org.opendaylight.controller.sal.utils.StatusCode;
+import org.openflow.protocol.OFHello;
import org.openflow.protocol.OFMessage;
import org.openflow.protocol.OFType;
+import org.openflow.protocol.factory.BasicFactory;
import org.openflow.util.HexString;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-public class Controller implements IController, CommandProvider, IPluginInConnectionService {
+public class Controller implements IController, CommandProvider, IPluginInConnectionService, IOFPlugin {
private static final Logger logger = LoggerFactory
.getLogger(Controller.class);
private ControllerIO controllerIO;
@@ -155,11 +160,16 @@ public class Controller implements IController, CommandProvider, IPluginInConnec
switchEventThread.start();
// spawn a thread to start to listen on the open flow port
- controllerIO = new ControllerIO(this);
- try {
- controllerIO.start();
- } catch (IOException ex) {
- logger.error("Caught exception while starting:", ex);
+ if (Boolean.getBoolean("of.reserve_of_port")) {
+ logger.info("of.reserve_of_port enabled. Trying to register to the OpenFlow port");
+ controllerIO = new ControllerIO(this);
+ try {
+ controllerIO.start();
+ } catch (IOException ex) {
+ logger.error("Caught exception while starting:", ex);
+ }
+ } else {
+ logger.info("of.reserve_of_port NOT enabled. Hoping to work with a OF Broker");
}
}
@@ -241,21 +251,25 @@ public class Controller implements IController, CommandProvider, IPluginInConnec
try {
sc = ssc.accept();
// create new switch
- int i = this.switchInstanceNumber.addAndGet(1);
- String instanceName = "SwitchHandler-" + i;
- SwitchHandler switchHandler = new SwitchHandler(this, sc, instanceName);
- switchHandler.start();
- if (sc.isConnected()) {
- logger.info("Switch:{} is connected to the Controller",
- sc.socket().getRemoteSocketAddress()
- .toString().split("/")[1]);
- }
-
+ this.createSwitchHandler(sc);
} catch (IOException e) {
return;
}
}
+ public void createSwitchHandler(SocketChannel sc) {
+ // create new switch
+ int i = this.switchInstanceNumber.addAndGet(1);
+ String instanceName = "SwitchHandler-" + i;
+ SwitchHandler switchHandler = new SwitchHandler(this, sc, instanceName);
+ switchHandler.start();
+ if (sc.isConnected()) {
+ logger.info("Switch:{} is connected to the Controller",
+ sc.socket().getRemoteSocketAddress()
+ .toString().split("/")[1]);
+ }
+ }
+
private void disconnectSwitch(ISwitch sw) {
if (((SwitchHandler) sw).isOperational()) {
Long sid = sw.getId();
@@ -427,4 +441,34 @@ public class Controller implements IController, CommandProvider, IPluginInConnec
ISwitch sw = switches.get((Long)node.getID());
if (sw != null) notifySwitchAdded(sw);
}
+
+ @Override
+ public boolean isSupported(ByteBuffer buffer) {
+ try {
+ BasicFactory factory = new BasicFactory();
+ List<OFMessage> msgs = factory.parseMessages(buffer);
+ for (OFMessage msg : msgs) {
+ logger.trace("Message received: {}", msg);
+ OFType type = msg.getType();
+ switch (type) {
+ case HELLO:
+ OFHello hello = (OFHello)msg;
+ byte version = hello.getVersion();
+ if (version == 1) return true;
+ break;
+ default:
+ break;
+ }
+ }
+ } catch (Exception e) {
+ logger.warn("Error parsing buffer using OF1.0 plugin for message : "+buffer.toString());
+ }
+ return false;
+ }
+
+ @Override
+ public boolean takeOver(SocketChannel sc) {
+ this.createSwitchHandler(sc);
+ return true;
+ }
}
diff --git a/opendaylight/protocol_plugins/openflow/src/main/java/org/opendaylight/controller/protocol_plugin/openflow/internal/Activator.java b/opendaylight/protocol_plugins/openflow
index 4a6a291..6b65140 100644
--- a/opendaylight/protocol_plugins/openflow/src/main/java/org/opendaylight/controller/protocol_plugin/openflow/internal/Activator.java
+++ b/opendaylight/protocol_plugins/openflow/src/main/java/org/opendaylight/controller/protocol_plugin/openflow/internal/Activator.java
@@ -12,6 +12,7 @@ import java.util.Dictionary;
import java.util.Hashtable;
import org.apache.felix.dm.Component;
+import org.opendaylight.controller.ofbroker.IOFPlugin;
import org.opendaylight.controller.protocol_plugin.openflow.IDataPacketListen;
import org.opendaylight.controller.protocol_plugin.openflow.IDataPacketMux;
import org.opendaylight.controller.protocol_plugin.openflow.IDiscoveryListener;
@@ -246,7 +247,8 @@ public class Activator extends ComponentActivatorAbstractBase {
props.put("name", "Controller");
props.put(GlobalConstants.PROTOCOLPLUGINTYPE.toString(), Node.NodeIDType.OPENFLOW);
c.setInterface(new String[] { IController.class.getName(),
- IPluginInConnectionService.class.getName()},
+ IPluginInConnectionService.class.getName(),
+ IOFPlugin.class.getName()},
props);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment