-
Notifications
You must be signed in to change notification settings - Fork 77
OpenFlow Java library API Cookbook
(work in progress)
The main idea is to create an event-driven application. What develeloper does have to do is to implement his own event handler and launch the Controller listening a tcp port. When Controller connects with a Switch and gets a version information from the Hello message, it starts an appropriate OpenFlow events handler, supporting a given OpenFlow vesrion which deals with OpenFlow messages and generates events against developer-defined event handler.
You may operate with OpenFlow protocol messages and structures via MessageProvider class.
import org.flowforwarding.of.protocol.ofmessages.IOFMessageProvider;
import org.flowforwarding.of.protocol.ofmessages.IOFMessageProviderFactory;
import org.flowforwarding.of.protocol.ofmessages.OFMessageProviderFactoryAvroProtocol;
IOFMessageProviderFactory factory = new OFMessageProviderFactoryAvroProtocol();
IOFMessageProvider provider = factory.getMessageProvider("1.3");Now you can use the Provider to operate OpenFlow messages. Get messages in binary format ready to put into a channel:
byte [] helloMessage = provider.encodeHelloMessage();
/*...*/
byte [] configRequestMessage = provider.encodeSwitchConfigRequest();
/*...*/
byte[] switchFeatureRequestMessage = encodeSwitchFeaturesRequest();To get more complicated messages first you need to build it, and then add fields via Ref classes. Let's consider Flow Modification message:
import org.flowforwarding.of.protocol.ofmessages.OFMessageFlowMod.OFMessageFlowModeRef;
OFMessageFlowModehandler fmHandler = provider.buildFlowModMsg();
fmHandler.addTableId("1");
fmHandler.addPriority("256");
/*...*/
fmHandler.addMatchInPort("128");
/*...*/
OFStructureInstructionHandler instrHandler= provider.buildInstructionApplyAction();
instrHandler.addActionOutput("12");
/*...*/
fmHandler.addInstuction(instrHandler);
byte [] fmBuffer = provider.encodeFlowMod(fmRef);The controller infrastructure is started the next way:
import org.flowforwarding.of.controller.Controller;
/*................*/
Controller.launch (SessionHandler.class); // This launches a controller listening tcp port 6633
Controller.launch (SessionHandler.class, configuration); // This launches a controller listening given tcp port It's a POJO containing some configuration information as tcp port number etc.
import org.flowforwarding.of.controller.Configuration
/*................*/
Configuration config1 = new Configuration(); // tcp port 6633 by default
Configuration config2 = new Configuration(6633);During the Controller-Sessions some events are generated. An application handles it via OF Events handler.
You have to implement your session handler to interact with Controller. You may manage of incoming messages handling or send messages you want. Session handlers extend the class OFSessionHandler:
import org.flowforwarding.of.controller.SwitchState.SwitchRef;
import org.flowforwarding.of.controller.session.OFSessionHandler;
public class SimpleHandler extends OFSessionHandler {
/*
* User-defined Switch event handlers
*/
@Override
protected void handshaked(SwitchRef swRef) {
super.handshaked(swRef);
/*You have to implement your own logic here*/
}
/*.................................................*/
@Override
protected void connected(SwitchRef swRef) {
super.connected(swRef);
/*You have to implement your own logic here*/
}
/*.................................................*/
@Override
protected void packetIn(SwitchRef swRef) {
super.connected(swRef);
/*You have to implement your own logic here*/
}
}The SwitchRef class refers to a Switch connected to the Controller. It's unique, you have to use it to communicate with a Switch, send commands or get a Switch state:
SwitchRef swRef = SwitchRef.create();
Long SwitchRef.getDpid();
void SwitchRef.setDpid(Long dpid);Some code examples:
import org.flowforwarding.of.ofswitch.SwitchState.SwitchRef;
/*.................................................*/
public class SimpleHandler extends OFSessionHandler {
@Override
protected void handshaked(SwitchRef swRef) {
super.handshaked(swRef);
Long dpid = swRef.getDpid();
/*.........................................*/
}
/*.........................................*/
}