From d7f6fa62d052d0c248697220c17de3808cf9ab7d Mon Sep 17 00:00:00 2001
From: Alex Reimers <alex@bigswitch.com>
Date: Tue, 21 Feb 2012 14:04:36 -0800
Subject: [PATCH] Modify LearningSwitch and Hub to be proper loadable
 IFloodlightModules.

---
 .../net/floodlightcontroller/hub/Hub.java     | 53 +++++++++---
 .../learningswitch/LearningSwitch.java        | 80 ++++++++++++++-----
 ...htcontroller.core.module.IFloodlightModule |  4 +-
 3 files changed, 105 insertions(+), 32 deletions(-)

diff --git a/src/main/java/net/floodlightcontroller/hub/Hub.java b/src/main/java/net/floodlightcontroller/hub/Hub.java
index db20e43f0..78cff1bf5 100644
--- a/src/main/java/net/floodlightcontroller/hub/Hub.java
+++ b/src/main/java/net/floodlightcontroller/hub/Hub.java
@@ -18,12 +18,19 @@
 package net.floodlightcontroller.hub;
 
 import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Collections;
+import java.util.Map;
 
 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 org.openflow.protocol.OFMessage;
 import org.openflow.protocol.OFPacketIn;
@@ -40,7 +47,7 @@ import org.slf4j.LoggerFactory;
  *
  * @author David Erickson (daviderickson@cs.stanford.edu) - 04/04/10
  */
-public class Hub implements IOFMessageListener {
+public class Hub implements IFloodlightModule, IOFMessageListener {
     protected static Logger log = LoggerFactory.getLogger(Hub.class);
 
     protected IFloodlightProviderService floodlightProvider;
@@ -52,14 +59,6 @@ public class Hub implements IOFMessageListener {
         this.floodlightProvider = floodlightProvider;
     }
 
-    public void startUp() {
-        floodlightProvider.addOFMessageListener(OFType.PACKET_IN, this);
-    }
-
-    public void shutDown() {
-        floodlightProvider.removeOFMessageListener(OFType.PACKET_IN, this);
-    }
-
     @Override
     public String getName() {
         return Hub.class.getPackage().getName();
@@ -112,4 +111,40 @@ public class Hub implements IOFMessageListener {
     public boolean isCallbackOrderingPostreq(OFType type, String name) {
         return false;
     }
+
+    // IFloodlightModule
+    
+    @Override
+    public Collection<Class<? extends IFloodlightService>> getModuleServices() {
+        // We don't provide any services, return null
+        return null;
+    }
+
+    @Override
+    public Map<Class<? extends IFloodlightService>, IFloodlightService>
+            getServiceImpls() {
+        // We don't provide any services, return null
+        return null;
+    }
+
+    @Override
+    public Collection<Class<? extends IFloodlightService>>
+            getModuleDependencies() {
+        Collection<Class<? extends IFloodlightService>> l = 
+                new ArrayList<Class<? extends IFloodlightService>>();
+        l.add(IFloodlightProviderService.class);
+        return l;
+    }
+
+    @Override
+    public void init(FloodlightModuleContext context)
+            throws FloodlightModuleException {
+        floodlightProvider =
+                context.getServiceImpl(IFloodlightProviderService.class);
+    }
+
+    @Override
+    public void startUp(FloodlightModuleContext context) {
+        floodlightProvider.addOFMessageListener(OFType.PACKET_IN, this);
+    }
 }
diff --git a/src/main/java/net/floodlightcontroller/learningswitch/LearningSwitch.java b/src/main/java/net/floodlightcontroller/learningswitch/LearningSwitch.java
index f677a76f7..8ecd7a803 100644
--- a/src/main/java/net/floodlightcontroller/learningswitch/LearningSwitch.java
+++ b/src/main/java/net/floodlightcontroller/learningswitch/LearningSwitch.java
@@ -32,12 +32,18 @@ package net.floodlightcontroller.learningswitch;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collection;
 import java.util.List;
+import java.util.Map;
 
 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.counter.CounterStore;
 import net.floodlightcontroller.counter.CounterValue;
 import net.floodlightcontroller.counter.ICounter;
@@ -61,7 +67,7 @@ import org.openflow.util.HexString;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class LearningSwitch implements IOFMessageListener {
+public class LearningSwitch implements IFloodlightModule, IOFMessageListener {
     protected static Logger log = LoggerFactory.getLogger(LearningSwitch.class);
     protected IFloodlightProviderService floodlightProvider;
     protected ICounterStoreService counterStore;
@@ -96,22 +102,6 @@ public class LearningSwitch implements IOFMessageListener {
     public void setCounterStore(ICounterStoreService counterStore) {
         this.counterStore = counterStore;
     }
-    
-    public void startUp() {
-        log.trace("Starting");
-        floodlightProvider.addOFMessageListener(OFType.PACKET_IN, this);
-        //floodlightProvider.addOFMessageListener(OFType.PORT_STATUS, this);
-        floodlightProvider.addOFMessageListener(OFType.FLOW_REMOVED, this);
-        floodlightProvider.addOFMessageListener(OFType.ERROR, this);
-    }
-
-    public void shutDown() {
-        log.trace("Stopping");
-        floodlightProvider.removeOFMessageListener(OFType.PACKET_IN, this);
-        //floodlightProvider.removeOFMessageListener(OFType.PORT_STATUS, this);
-        floodlightProvider.removeOFMessageListener(OFType.FLOW_REMOVED, this);
-        floodlightProvider.removeOFMessageListener(OFType.ERROR, this);
-    }
 
     @Override
     public String getName() {
@@ -202,7 +192,10 @@ public class LearningSwitch implements IOFMessageListener {
         flowMod.setActions(Arrays.asList((OFAction) new OFActionOutput(outPort, (short) 0xffff)));
         flowMod.setLength((short) (OFFlowMod.MINIMUM_LENGTH + OFActionOutput.MINIMUM_LENGTH));
 
-        log.trace("{} {} flow mod {}", new Object[]{ sw, (command == OFFlowMod.OFPFC_DELETE) ? "deleting" : "adding", flowMod });
+        if (log.isTraceEnabled()) {
+            log.trace("{} {} flow mod {}", 
+                      new Object[]{ sw, (command == OFFlowMod.OFPFC_DELETE) ? "deleting" : "adding", flowMod });
+        }
 
         updateCounterStore(sw, flowMod);
         
@@ -328,7 +321,6 @@ public class LearningSwitch implements IOFMessageListener {
     private Command processPortStatusMessage(IOFSwitch sw, OFPortStatus portStatusMessage) {
         // FIXME This is really just an optimization, speeding up removal of flow
         // entries for a disabled port; think about whether it's really needed
-        log.info("learning switch got a port_status");
         OFPhysicalPort port = portStatusMessage.getDesc();
         log.info("received port status: " + portStatusMessage.getReason() + " for port " + port.getPortNumber());
         // LOOK! should be using the reason enums - but how?
@@ -348,7 +340,9 @@ public class LearningSwitch implements IOFMessageListener {
         if (flowRemovedMessage.getCookie() != LearningSwitch.LEARNING_SWITCH_COOKIE) {
             return Command.CONTINUE;
         }
-        log.trace("{} flow entry removed {}", sw, flowRemovedMessage);
+        if (log.isTraceEnabled()) {
+            log.trace("{} flow entry removed {}", sw, flowRemovedMessage);
+        }
         OFMatch match = flowRemovedMessage.getMatch();
         // When a flow entry expires, it means the device with the matching source
         // MAC address and VLAN either stopped sending packets or moved to a different
@@ -394,12 +388,54 @@ public class LearningSwitch implements IOFMessageListener {
 
     @Override
     public boolean isCallbackOrderingPrereq(OFType type, String name) {
-        return (type == OFType.PACKET_IN && 
-                (name.equals("devicemanager") || name.equals("forwarding")));
+        // TODO - change this
+        return false;
     }
 
     @Override
     public boolean isCallbackOrderingPostreq(OFType type, String name) {
         return false;
     }
+
+    // IFloodlightModule
+    
+    @Override
+    public Collection<Class<? extends IFloodlightService>> getModuleServices() {
+        // We don't provide any services, return null
+        return null;
+    }
+
+    @Override
+    public Map<Class<? extends IFloodlightService>, IFloodlightService>
+            getServiceImpls() {
+        // We don't provide any services, return null
+        return null;
+    }
+
+    @Override
+    public Collection<Class<? extends IFloodlightService>>
+            getModuleDependencies() {
+        Collection<Class<? extends IFloodlightService>> l = 
+                new ArrayList<Class<? extends IFloodlightService>>();
+        l.add(IFloodlightProviderService.class);
+        l.add(ICounterStoreService.class);
+        return l;
+    }
+
+    @Override
+    public void init(FloodlightModuleContext context)
+            throws FloodlightModuleException {
+        floodlightProvider =
+                context.getServiceImpl(IFloodlightProviderService.class);
+        counterStore =
+                context.getServiceImpl(ICounterStoreService.class);
+    }
+
+    @Override
+    public void startUp(FloodlightModuleContext context) {
+        floodlightProvider.addOFMessageListener(OFType.PACKET_IN, this);
+        //floodlightProvider.addOFMessageListener(OFType.PORT_STATUS, this);
+        floodlightProvider.addOFMessageListener(OFType.FLOW_REMOVED, this);
+        floodlightProvider.addOFMessageListener(OFType.ERROR, this);
+    }
 }
diff --git a/src/main/resources/META-INF/services/net.floodlightcontroller.core.module.IFloodlightModule b/src/main/resources/META-INF/services/net.floodlightcontroller.core.module.IFloodlightModule
index 120763860..7be40a18d 100644
--- a/src/main/resources/META-INF/services/net.floodlightcontroller.core.module.IFloodlightModule
+++ b/src/main/resources/META-INF/services/net.floodlightcontroller.core.module.IFloodlightModule
@@ -7,4 +7,6 @@ net.floodlightcontroller.forwarding.Forwarding
 net.floodlightcontroller.core.OFMessageFilterManager
 net.floodlightcontroller.staticflowentry.StaticFlowEntryPusher
 net.floodlightcontroller.perfmon.PktInProcessingTime
-net.floodlightcontroller.restserver.RestApiServer
\ No newline at end of file
+net.floodlightcontroller.restserver.RestApiServer
+net.floodlightcontroller.learningswitch.LearningSwitch
+net.floodlightcontroller.hub.Hub
\ No newline at end of file
-- 
GitLab