Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagejava
themeEclipse
firstline1
titleIHAWorkerService Example
linenumberstrue
package net.floodlightcontroller.mactracker;
 
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentSkipListSet;
 
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.hasupport.IHAWorker;
import net.floodlightcontroller.hasupport.IHAWorkerService;
import net.floodlightcontroller.packet.Ethernet;
 
import org.projectfloodlight.openflow.protocol.OFMessage;
import org.projectfloodlight.openflow.protocol.OFType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
public class MacTracker implements IFloodlightModule, IOFMessageListener {
	
	protected IHAWorkerService haworker;
	protected IHAWorker ldhaworker;
	protected List<String> updates = new ArrayList<String>();
	protected static Logger logger = LoggerFactory.getLogger(MacTracker.class);
	protected IFloodlightProviderService floodlightProvider;
	protected Set<Long> macAddresses;
 
	@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 stubs
		return null;
	}
 
	@Override
	public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
		// TODO Auto-generated method stub
	    Collection<Class<? extends IFloodlightService>> l =
	            new ArrayList<Class<? extends IFloodlightService>>();
	        l.add(IFloodlightProviderService.class);
	        l.add(IHAWorkerService.class);
		return l;
	}
 
	@Override
	public void init(FloodlightModuleContext context) throws FloodlightModuleException {
		// TODO Auto-generated method stub
		haworker = context.getServiceImpl(IHAWorkerService.class);
	    floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);
	    macAddresses = new ConcurrentSkipListSet<Long>();
	}
 
	@Override
	public void startUp(FloodlightModuleContext context) throws FloodlightModuleException {
		// TODO Auto-generated method stub
		floodlightProvider.addOFMessageListener(OFType.PACKET_IN, this);
	}
 
	@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 net.floodlightcontroller.core.IListener.Command receive(IOFSwitch sw, OFMessage msg,
			FloodlightContext cntx) {
		
		// haworker.getService() => Get the various HAWorker objects of all modules which have HAWorker enabled.
		// For Example, get the HAWorker associated with LinkDiscoveryManager
		// Implemented so far { LinkDiscoveryManager => "LDHAWorker" , TopologyManagaer => "TopoHAWorker" }
		ldhaworker = haworker.getService("LDHAWorker");
		
		
		// Print LDUpdates, this is triggered by a PacketIn message.
		// Publish and Subscribe to your own (Controller 1) LDUpdates.
		try {
			ldhaworker.publishHook();
			updates = ldhaworker.subscribeHook("C1");
			for (String update: updates){
				logger.info("Subscribe: {}", new Object[] {update} );
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		// TODO Auto-generated method stub
        Ethernet eth =
                IFloodlightProviderService.bcStore.get(cntx,
                                            IFloodlightProviderService.CONTEXT_PI_PAYLOAD);
 
        Long sourceMACHash = eth.getSourceMACAddress().getLong();
        if (!macAddresses.contains(sourceMACHash)) {
            macAddresses.add(sourceMACHash);
            logger.info("MAC Address: {} seen on switch: {}",
                    eth.getSourceMACAddress().toString(),
                    sw.getId().toString());
        }
        return Command.CONTINUE;
	}
 
}


No Format
Compile:
 
ant
 
Run:
  
java -jar target/floodlight.jar 
 
Start a mininet topology:
mininet@mininet-vm:~$ sudo python IslandTest1.py
 
This will print:
 
2017-03-15 00:12:17.445 INFO  [n.f.m.MacTracker] Subscribe: {"dstPort":"2","dst":"00:00:00:00:00:00:00:02","src":"00:00:00:00:00:00:00:01","latency":"0x0000000000000011","srcPort":"2","type":"internal","operation":"Link Updated","timestamp":"148955113153000000"}
2017-03-15 00:12:17.445 INFO  [n.f.m.MacTracker] Subscribe: {"dstPort":"2","dst":"00:00:00:00:00:00:00:01","src":"00:00:00:00:00:00:00:02","latency":"0x0000000000000002, 0x0000000000000002","srcPort":"2","type":"internal","operation":"Link Updated, Port Up","timestamp":"148955113154000000, 148955113157000000"}
2017-03-15 00:12:17.445 INFO  [n.f.m.MacTracker] Subscribe: {"dstPort":"2","dst":"00:00:00:00:00:00:00:01","src":"00:00:00:00:00:00:00:02","latency":"0x0000000000000002","srcPort":"1","type":"internal","operation":"Port Up","timestamp":"148955113155000000"}
...

...