Giant patch of changes to support OpenFlow 1.3

The following people have contributed to this patch:
- Ali Al-Shabibi <alshabibi.ali@gmail.com>
- Ayaka Koshibe <ayaka@onlab.us>
- Brian O'Connor <bocon@onlab.us>
- Jonathan Hart <jono@onlab.us>
- Matteo Gerola <mgerola@create-net.org>
- Michele Santuari <michele.santuari@create-net.org>
- Pavlin Radoslavov <pavlin@onlab.us>
- Saurav Das <sauravdas@alumni.stanford.edu>
- Toshio Koide <t-koide@onlab.us>
- Yuta HIGUCHI <y-higuchi@onlab.us>

The patch includes the following changes:
- New Floodlight I/O loop / state machine
- New switch/port handling
- New role management (incl. Role.EQUAL)
- Added Floodlight debug framework
- Updates to Controller.java
- Move to Loxigen's OpenflowJ library
- Added OF1.3 support
- Added support for different switches (via DriverManager)
- Updated ONOS modules to use new APIs
- Added and updated unit tests

Change-Id: Ic70a8d50f7136946193d2ba2e4dc0b4bfac5f599
diff --git a/src/main/java/net/floodlightcontroller/core/IFloodlightProviderService.java b/src/main/java/net/floodlightcontroller/core/IFloodlightProviderService.java
index 1194e18..7f4f77d 100644
--- a/src/main/java/net/floodlightcontroller/core/IFloodlightProviderService.java
+++ b/src/main/java/net/floodlightcontroller/core/IFloodlightProviderService.java
@@ -1,5 +1,5 @@
 /**
- *    Copyright 2011, Big Switch Networks, Inc. 
+ *    Copyright 2011, Big Switch Networks, Inc.
  *    Originally created by David Erickson, Stanford University
  *
  *    Licensed under the Apache License, Version 2.0 (the "License"); you may
@@ -19,15 +19,16 @@
 
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
+import net.floodlightcontroller.core.internal.Controller.Counters;
 import net.floodlightcontroller.core.module.IFloodlightService;
-import net.onrc.onos.api.registry.ILocalSwitchMastershipListener;
 import net.onrc.onos.core.packet.Ethernet;
 import net.onrc.onos.core.util.OnosInstanceId;
 
-import org.openflow.protocol.OFMessage;
-import org.openflow.protocol.OFType;
-import org.openflow.protocol.factory.BasicFactory;
+import org.projectfloodlight.openflow.protocol.OFControllerRole;
+import org.projectfloodlight.openflow.protocol.OFFactory;
+import org.projectfloodlight.openflow.protocol.OFType;
 
 /**
  * The interface exposed by the core bundle that allows you to interact
@@ -43,13 +44,41 @@
      */
     public static final String CONTEXT_PI_PAYLOAD =
             "net.floodlightcontroller.core.IFloodlightProvider.piPayload";
-
+    public static final String CONTEXT_PI_INPORT =
+            "net.floodlightcontroller.core.IFloodlightProvider.piInPort";;
     /**
-     * The role of the controller as used by the OF 1.2 and OVS failover and
-     * load-balancing mechanism.
+     * The role of the controller as it pertains to a particular switch.
+     * Note that this definition of the role enum is different from the
+     * OF1.3 definition. It is maintained here to be backward compatible to
+     * earlier versions of the controller code. This enum is translated
+     * to the OF1.3 enum, before role messages are sent to the switch.
+     * See sendRoleRequestMessage method in OFSwitchImpl
      */
     public static enum Role {
-        EQUAL, MASTER, SLAVE
+        EQUAL(OFControllerRole.ROLE_EQUAL),
+        MASTER(OFControllerRole.ROLE_MASTER),
+        SLAVE(OFControllerRole.ROLE_SLAVE);
+
+        private final int nxRole;
+
+        private Role(OFControllerRole nxRole) {
+            this.nxRole = nxRole.ordinal();
+        }
+        /*
+        private static Map<Integer,Role> nxRoleToEnum
+                = new HashMap<Integer,Role>();
+        static {
+            for(Role r: Role.values())
+                nxRoleToEnum.put(r.toNxRole(), r);
+        }
+        public int toNxRole() {
+            return nxRole;
+        }
+        // Return the enum representing the given nxRole or null if no
+        // such role exists
+        public static Role fromNxRole(int nxRole) {
+            return nxRoleToEnum.get(nxRole);
+        }*/
     }
 
     /**
@@ -59,6 +88,40 @@
     public static final FloodlightContextStore<Ethernet> bcStore =
             new FloodlightContextStore<Ethernet>();
 
+
+    //************************
+    //  Controller related
+    //************************
+
+    /**
+     * Get the current mapping of controller IDs to their IP addresses
+     * Returns a copy of the current mapping.
+     *
+     * @see IHAListener
+     */
+    public Map<String, String> getControllerNodeIPs();
+
+    /**
+     * Return the controller start time in  milliseconds
+     *
+     * @return
+     */
+    public long getSystemStartTime();
+
+    /**
+     * Run the main I/O loop of the Controller.
+     */
+    public void run();
+
+//    /**
+//     * Terminate the process
+//     */
+//    public void terminate();
+
+    //************************
+    //  OF Message Listener related
+    //************************
+
     /**
      * Adds an OpenFlow message listener
      *
@@ -82,23 +145,9 @@
      */
     public Map<OFType, List<IOFMessageListener>> getListeners();
 
-    /**
-     * Adds a switch mastership listener for controller role changes for
-     * local switches.
-     *
-     * @param listener the listener to add.
-     */
-    public void addLocalSwitchMastershipListener(
-                ILocalSwitchMastershipListener listener);
-
-    /**
-     * Removes a switch mastership listener for controller role changes for
-     * local switches.
-     *
-     * @param listener the listener to remove.
-     */
-    public void removeLocalSwitchMastershipListener(
-                ILocalSwitchMastershipListener listener);
+    //************************
+    //  Switch & SwitchListener related
+    //************************
 
     /**
      * Returns an unmodifiable map of all actively connected OpenFlow switches. This doesn't
@@ -109,12 +158,12 @@
     public Map<Long, IOFSwitch> getSwitches();
 
     /**
-     * Get the current mapping of controller IDs to their IP addresses
-     * Returns a copy of the current mapping.
-     *
-     * @see IHAListener
+     * Configure controller to always clear the flow table on the switch,
+     * when it connects to controller. This will be true for first time switch
+     * reconnect, as well as a switch re-attaching to Controller after HA
+     * switch over to ACTIVE role
      */
-    public Map<String, String> getControllerNodeIPs();
+    public void setAlwaysClearFlowsOnSwAdd(boolean value);
 
     /**
      * Gets the unique ID used to identify this ONOS instance in the cluster.
@@ -137,67 +186,19 @@
      */
     public void removeOFSwitchListener(IOFSwitchListener listener);
 
-    /**
-     * Terminate the process
-     */
-    public void terminate();
+
+
+    //************************
+    //  Utility methods
+    //************************
 
     /**
-     * Re-injects an OFMessage back into the packet processing chain
-     *
-     * @param sw  The switch to use for the message
-     * @param msg the message to inject
-     * @return True if successfully re-injected, false otherwise
-     */
-    public boolean injectOfMessage(IOFSwitch sw, OFMessage msg);
-
-    /**
-     * Re-injects an OFMessage back into the packet processing chain
-     *
-     * @param sw       The switch to use for the message
-     * @param msg      the message to inject
-     * @param bContext a floodlight context to use if required
-     * @return True if successfully re-injected, false otherwise
-     */
-    public boolean injectOfMessage(IOFSwitch sw, OFMessage msg,
-                                   FloodlightContext bContext);
-
-    /**
-     * Process written messages through the message listeners for the controller
-     *
-     * @param sw The switch being written to
-     * @param m  the message
-     * @param bc any accompanying context object
-     */
-    public void handleOutgoingMessage(IOFSwitch sw, OFMessage m,
-                                      FloodlightContext bc);
-
-    /**
-     * Gets the BasicFactory
+     * Gets the Factory
      *
      * @return an OpenFlow message factory
      */
-    public BasicFactory getOFMessageFactory();
-
-    /**
-     * Run the main I/O loop of the Controller.
-     */
-    public void run();
-
-    /**
-     * Return the controller start time in  milliseconds
-     *
-     * @return
-     */
-    public long getSystemStartTime();
-
-    /**
-     * Configure controller to always clear the flow table on the switch,
-     * when it connects to controller. This will be true for first time switch
-     * reconnect, as well as a switch re-attaching to Controller after HA
-     * switch over to ACTIVE role
-     */
-    public void setAlwaysClearFlowsOnSwAdd(boolean value);
+    public OFFactory getOFMessageFactory_13();
+    public OFFactory getOFMessageFactory_10();
 
     /**
      * Publish updates to Controller updates queue
@@ -206,4 +207,66 @@
      */
     public void publishUpdate(IUpdate update);
 
+//    /**
+//     * Re-injects an OFMessage back into the packet processing chain
+//     *
+//     * @param sw  The switch to use for the message
+//     * @param msg the message to inject
+//     * @return True if successfully re-injected, false otherwise
+//     */
+//    public boolean injectOfMessage(IOFSwitch sw, OFMessage msg);
+//
+//    /**
+//     * Re-injects an OFMessage back into the packet processing chain
+//     *
+//     * @param sw       The switch to use for the message
+//     * @param msg      the message to inject
+//     * @param bContext a floodlight context to use if required
+//     * @return True if successfully re-injected, false otherwise
+//     */
+//    public boolean injectOfMessage(IOFSwitch sw, OFMessage msg,
+//                                   FloodlightContext bContext);
+//
+//    /**
+//     * Process written messages through the message listeners for the controller
+//     *
+//     * @param sw The switch being written to
+//     * @param m  the message
+//     * @param bc any accompanying context object
+//     */
+//    public void handleOutgoingMessage(IOFSwitch sw, OFMessage m,
+//                                      FloodlightContext bc);
+
+
+    /**
+     * Return the default set of counters
+     * @return
+     */
+    public Counters getCounters();
+
+    void setAlwaysClearFlowsOnSwActivate(boolean value);
+
+    Map<String, Long> getMemory();
+
+    Long getUptime();
+
+    Set<Long> getAllSwitchDpids();
+
+    IOFSwitch getSwitch(long dpid);
+
+    /**
+     * Record a switch event in in-memory debug-event
+     * @param switchDPID
+     * @param reason Reason for this event
+     * @param flushNow see debug-event flushing in IDebugEventService
+     */
+    public void addSwitchEvent(long switchDPID, String reason, boolean flushNow);
+
+    Set<Long> getAllMasterSwitchDpids();
+
+    Set<Long> getAllEqualSwitchDpids();
+
+    IOFSwitch getMasterSwitch(long dpid);
+
+    IOFSwitch getEqualSwitch(long dpid);
 }