65.9K
CodeProject is changing. Read more.
Home

A Sample Named "MACTracker" of Floodlight Controller

starIconstarIconstarIconstarIconstarIcon

5.00/5 (4 votes)

Dec 7, 2015

CPOL
viewsIcon

11274

An instance of Floodlight Controller

Introduction

Some of you may have tried using the official guide of floodlight. And when you were trying to develop your own applications, some unexpected problem might pop out. Just like the following pictures.

This part of the code could be used to get all MAC addresses of the whole SDN network. And now it can also show the route information of the whole network.

And the final results are shown below:

Using the Code

Step 1

Create a folder named "mactracker" in the path: floodlight/src/main/java/net/floodlightcontroller.

Step 2

Create a Java file named "MACTraker" in the folder "mactraker".

Step3

Rebuilt floodlight controller.  under the path : floodlight/. Using command "ant".

Step 4

Add "net.floodlightcontroller.mactracker.MACTracker" to startUp mouldes profile

//
// 

package net.floodlightcontroller.mactracker;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentSkipListSet;

import org.projectfloodlight.openflow.protocol.OFMessage;
import org.projectfloodlight.openflow.protocol.OFType;
import org.projectfloodlight.openflow.util.HexString;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.floodlightcontroller.core.FloodlightContext;
import net.floodlightcontroller.core.IFloodlightProviderService;
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;
import net.floodlightcontroller.packet.Ethernet;

public class MACTracker implements IOFMessageListener, IFloodlightModule {

    protected IFloodlightProviderService floodlightProvider;
    protected Set macAddresses;
    protected static Logger logger;
    @Override
    public String getName() {
        // TODO Auto-generated method stub
        return MACTracker.class.getSimpleName();
    
    }

    @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 Collection<Class<? extends IFloodlightService>> getModuleServices() {
        // TODO Auto-generated method stub
          Collection<Class<? extends IFloodlightService>> 
          	l = new ArrayList<Class<? extends IFloodlightService>>();
          l.add(IFloodlightProviderService.class);
          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
         floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);
         macAddresses = new ConcurrentSkipListSet<Long>();
         logger = LoggerFactory.getLogger(MACTracker.class);

    }

    @Override
    public void startUp(FloodlightModuleContext context) throws FloodlightModuleException {
        // TODO Auto-generated method stub
        floodlightProvider.addOFMessageListener(OFType.PACKET_IN, this);
    }

    @Override
    public net.floodlightcontroller.core.IListener.Command receive(IOFSwitch sw, OFMessage msg,
            FloodlightContext cntx) {
        
        // TODO Auto-generated method stub
        Ethernet eth = IFloodlightProviderService.bcStore.get
        			(cntx,IFloodlightProviderService.CONTEXT_PI_PAYLOAD);

        Long sourceMACHash = eth.getSourceMACAddress().toLong();
        macAddresses.add(sourceMACHash);
        logger.info("MAC Address: {} seen on switch: {}",
                    HexString.toHexString(sourceMACHash),
                    sw.getId());
        logger.info("PcakageType: {} Rounte: {}",
                     eth.toString() ,
                     HexString.toHexString(eth.getSourceMACAddress())+"->>"+HexString.toHexString                           (eth.getDestinationMACAddress()));
               return Command.CONTINUE;

    }

}

Any Comments or Suggestions would be Embraced

Leave your messages and comments. Or send an email to jinwenqiang6668@gmail.com.