Pushed Switch Mastership Events into the Topology event channel.
Needed for ONOS-1729
For now those events are not used.

Also, added new TopologyElement attribute type: TYPE_ALL_LAYERS
which applies to the Mastership Events.

NOTE: Currently, those events are intercepted within the
Floodlight Controller.java class. The interception point might be moved
once it becomes clear whether the event origin should be the mastership
election mechanism or the "role change request" accepted by the switch.
In addition, the interception point needs to be moved/ported as appropriate
once we move to the newer 1.3 OpenFlow driver implementation and eventloop.

Change-Id: Iff06ed5aee867c428a8378e31f9d51dbe3e6b978
diff --git a/src/main/java/net/onrc/onos/core/topology/TopologyManager.java b/src/main/java/net/onrc/onos/core/topology/TopologyManager.java
index b12bc0f..e733c68 100644
--- a/src/main/java/net/onrc/onos/core/topology/TopologyManager.java
+++ b/src/main/java/net/onrc/onos/core/topology/TopologyManager.java
@@ -263,6 +263,10 @@
             Map<ByteBuffer, LinkEvent> removedLinkEvents = new HashMap<>();
             Map<ByteBuffer, HostEvent> addedHostEvents = new HashMap<>();
             Map<ByteBuffer, HostEvent> removedHostEvents = new HashMap<>();
+            Map<ByteBuffer, MastershipEvent> addedMastershipEvents =
+                new HashMap<>();
+            Map<ByteBuffer, MastershipEvent> removedMastershipEvents =
+                new HashMap<>();
 
             //
             // Classify and suppress matching events
@@ -273,6 +277,7 @@
                 PortEvent portEvent = topologyEvent.portEvent;
                 LinkEvent linkEvent = topologyEvent.linkEvent;
                 HostEvent hostEvent = topologyEvent.hostEvent;
+                MastershipEvent mastershipEvent = topologyEvent.mastershipEvent;
 
                 //
                 // Extract the events
@@ -306,6 +311,11 @@
                             removedHostEvents.remove(id);
                             reorderedAddedHostEvents.remove(id);
                         }
+                        if (mastershipEvent != null) {
+                            ByteBuffer id = mastershipEvent.getIDasByteBuffer();
+                            addedMastershipEvents.put(id, mastershipEvent);
+                            removedMastershipEvents.remove(id);
+                        }
                         break;
                     case ENTRY_REMOVE:
                         log.debug("Topology event ENTRY_REMOVE: {}", topologyEvent);
@@ -333,6 +343,11 @@
                             removedHostEvents.put(id, hostEvent);
                             reorderedAddedHostEvents.remove(id);
                         }
+                        if (mastershipEvent != null) {
+                            ByteBuffer id = mastershipEvent.getIDasByteBuffer();
+                            addedMastershipEvents.remove(id);
+                            removedMastershipEvents.put(id, mastershipEvent);
+                        }
                         break;
                     default:
                         log.error("Unknown topology event {}",
@@ -350,8 +365,15 @@
                 // Apply the classified events.
                 //
                 // Apply the "add" events in the proper order:
-                //   switch, port, link, host
+                //   mastership, switch, port, link, host
                 //
+                // TODO: Currently, the Mastership events are not used,
+                // so their processing ordering is not important (undefined).
+                //
+                for (MastershipEvent mastershipEvent :
+                         addedMastershipEvents.values()) {
+                    processAddedMastershipEvent(mastershipEvent);
+                }
                 for (SwitchEvent switchEvent : addedSwitchEvents.values()) {
                     addSwitch(switchEvent);
                 }
@@ -366,7 +388,7 @@
                 }
                 //
                 // Apply the "remove" events in the reverse order:
-                //   host, link, port, switch
+                //   host, link, port, switch, mastership
                 //
                 for (HostEvent hostEvent : removedHostEvents.values()) {
                     removeHost(hostEvent);
@@ -380,6 +402,10 @@
                 for (SwitchEvent switchEvent : removedSwitchEvents.values()) {
                     removeSwitch(switchEvent);
                 }
+                for (MastershipEvent mastershipEvent :
+                         removedMastershipEvents.values()) {
+                    processRemovedMastershipEvent(mastershipEvent);
+                }
 
                 //
                 // Apply reordered events
@@ -888,6 +914,29 @@
     //
 
     /**
+     * Mastership updated event.
+     *
+     * @param mastershipEvent the mastership event.
+     */
+    @Override
+    public void putSwitchMastershipEvent(MastershipEvent mastershipEvent) {
+        // Send out notification
+        TopologyEvent topologyEvent = new TopologyEvent(mastershipEvent);
+        eventChannel.addEntry(topologyEvent.getID(), topologyEvent);
+    }
+
+    /**
+     * Mastership removed event.
+     *
+     * @param mastershipEvent the mastership event.
+     */
+    @Override
+    public void removeSwitchMastershipEvent(MastershipEvent mastershipEvent) {
+        // Send out notification
+        eventChannel.removeEntry(mastershipEvent.getID());
+    }
+
+    /**
      * Adds a switch to the topology replica.
      *
      * @param switchEvent the SwitchEvent with the switch to add.
@@ -1272,6 +1321,30 @@
     }
 
     /**
+     * Processes added Switch Mastership event.
+     *
+     * @param mastershipEvent the MastershipEvent to process.
+     */
+    @GuardedBy("topology.writeLock")
+    private void processAddedMastershipEvent(MastershipEvent mastershipEvent) {
+        log.debug("Processing added Mastership event {}",
+                  mastershipEvent);
+        // TODO: Not implemented/used yet.
+    }
+
+    /**
+     * Processes removed Switch Mastership event.
+     *
+     * @param mastershipEvent the MastershipEvent to process.
+     */
+    @GuardedBy("topology.writeLock")
+    private void processRemovedMastershipEvent(MastershipEvent mastershipEvent) {
+        log.debug("Processing removed Mastership event {}",
+                  mastershipEvent);
+        // TODO: Not implemented/used yet.
+    }
+
+    /**
      * Read the whole topology from the database.
      *
      * @return a collection of EventEntry-encapsulated Topology Events for