Info |
---|
This tutorial is for Floodlight v1.0 and later. If you need to use Floodlight v0.91 or earlier, you can find the v0.91 and v0.90 documentation here. |
...
There are a number of different types of OpenFlow messages for which events are generated, but most of the action happens in the PacketIn handlers. A PacketIn message is the OpenFlow message that is sent from the switch to the controller if the switch does not have a flow table rule that matches the packet. The controller is expected to handle the packet and to install any necessary flows table entries (using a set of FlowMod messages). In this tutorial, we'll be adding a new PacketIn listener will store the PacketIn messages. We will then make these messages available via the REST API.
Creating the Class
Add class in Eclipse
- Expand the "Floodlight" item in the Package Explorer and find the "src/main/java" folder.
- Right-click on the "src/main/java" folder and choose "New/Class."
- Enter "net.floodlightcontroller.pktinhistory" in the "Package" box.
- Enter "PktInHistory" in the "name" box.
- Next to the "Interfaces" box, choose "Add..."
- Type "IFloodlightModule" into the search box, and choose "OK."
- Repeat steps above for "IOFMessageListener"
- Click "Finish" in the dialog.
...
Code Block |
---|
package net.floodlightcontroller.pktinhistory; import java.util.Collection; import java.util.Map; import org.projectfloodlight.openflow.protocol.OFMessage; import org.projectfloodlight.openflow.protocol.OFType; import net.floodlightcontroller.core.FloodlightContext; import net.floodlightcontroller.core.IOFMessageListener; import net.floodlightcontroller.core.IOFSwitch; import net.floodlightcontroller.core.module.FloodlightModuleContext; import net.floodlightcontroller.core.module.FloodlightModuleException; import net.floodlightcontroller.core.module.IFloodlightModule; import net.floodlightcontroller.core.module.IFloodlightService; public class PktInHistory implements IFloodlightModule, IOFMessageListener { @Override public String getName() { // TODO Auto-generated method stub return null; } @Override public boolean isCallbackOrderingPrereq(OFType type, String name) { // TODO Auto-generated method stub return false; } @Override public boolean isCallbackOrderingPostreq(OFType type, String name) { // TODO Auto-generated method stub return false; } @Override public net.floodlightcontroller.core.IListener.Command receive( IOFSwitch sw, OFMessage msg, FloodlightContext cntx) { // TODO Auto-generated method stub return null; } @Override public Collection<Class<? extends IFloodlightService>> getModuleServices() { // TODO Auto-generated method stub return null; } @Override public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() { // TODO Auto-generated method stub return null; } @Override public Collection<Class<? extends IFloodlightService>> getModuleDependencies() { // TODO Auto-generated method stub return null; } @Override public void init(FloodlightModuleContext context) throws FloodlightModuleException { // TODO Auto-generated method stub } @Override public void startUp(FloodlightModuleContext context) { // TODO Auto-generated method stub } } |
Setting up Module Dependencies
Now that we have our skeleton class, we have to implement the correct functions to make the module loadable. Since we are listening to OpenFlow messages we need to register with the FloodlightProvider (IFloodlightProviderService class). First we need to add it as a dependency. We create a member variable as so.
...
Code Block |
---|
@Override public void init(FloodlightModuleContext context) throws FloodlightModuleException { floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class); } |
Handling OpenFlow messages
Now that's we got a reference to the Floodlight provider we need to tell it we want to handle OpenFlow's PacketIn messages. We do this in the startUp() method.
...
For each PacketIn we will add the message along with the switch it came in on. Returning Command.CONTINUE tells the IFloodlightProvider to pass the PacketIn to the next module in the system. Returning Command.STOP will tell it to stop processing here.
Adding a REST API
...
Info |
---|
You can find a more in-depth REST API tutorial here if you are curious about the 'how' and 'why' behind some things you're required to do below. |
We now have a complete module implementation, but no way to get information out of it! For this we'll need to do two things. Have our module export a service then tie it into the REST API Module.
...
Note: After you complete this tutorial, you should to change the serializer for IOFSwitch back to @JsonSerialize(using=IOFSwitchSerializer.class) in order to restore the original serializer. |
...