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/TopologyPublisher.java b/src/main/java/net/onrc/onos/core/topology/TopologyPublisher.java
index f11c06d..3b6a5e9 100644
--- a/src/main/java/net/onrc/onos/core/topology/TopologyPublisher.java
+++ b/src/main/java/net/onrc/onos/core/topology/TopologyPublisher.java
@@ -7,6 +7,7 @@
 import java.util.concurrent.TimeUnit;
 
 import net.floodlightcontroller.core.IFloodlightProviderService;
+import net.floodlightcontroller.core.IFloodlightProviderService.Role;
 import net.floodlightcontroller.core.IOFSwitch;
 import net.floodlightcontroller.core.module.FloodlightModuleContext;
 import net.floodlightcontroller.core.module.FloodlightModuleException;
@@ -14,6 +15,7 @@
 import net.floodlightcontroller.core.module.IFloodlightService;
 import net.floodlightcontroller.core.util.SingletonTask;
 import net.floodlightcontroller.threadpool.IThreadPoolService;
+import net.onrc.onos.api.registry.ILocalSwitchMastershipListener;
 import net.onrc.onos.core.hostmanager.Host;
 import net.onrc.onos.core.hostmanager.IHostListener;
 import net.onrc.onos.core.hostmanager.IHostService;
@@ -42,7 +44,8 @@
         IOFSwitchPortListener,
         ILinkDiscoveryListener,
         IFloodlightModule,
-        IHostListener {
+        IHostListener,
+        ILocalSwitchMastershipListener {
     private static final Logger log =
             LoggerFactory.getLogger(TopologyPublisher.class);
 
@@ -331,6 +334,8 @@
         hostService = context.getServiceImpl(IHostService.class);
 
         topologyService = context.getServiceImpl(ITopologyService.class);
+
+        floodlightProvider.addLocalSwitchMastershipListener(this);
     }
 
     @Override
@@ -387,4 +392,35 @@
         event.freeze();
         topologyDiscoveryInterface.removeHostDiscoveryEvent(event);
     }
+
+    @Override
+    public void controllerRoleChanged(Dpid dpid, Role role) {
+        log.debug("Local switch controller mastership role changed: dpid = {} role = {}", dpid, role);
+        MastershipEvent mastershipEvent =
+            new MastershipEvent(dpid, registryService.getControllerId(), role);
+        // FIXME should be merging, with existing attrs, etc..
+        // TODO define attr name as constant somewhere.
+        // TODO populate appropriate attributes.
+        mastershipEvent.createStringAttribute(TopologyElement.TYPE,
+                                              TopologyElement.TYPE_ALL_LAYERS);
+        mastershipEvent.freeze();
+        topologyDiscoveryInterface.putSwitchMastershipEvent(mastershipEvent);
+    }
+
+    @Override
+    public void switchDisconnected(Dpid dpid) {
+        log.debug("Local switch disconnected: dpid = {} role = {}", dpid);
+
+        Role role = Role.SLAVE;         // TODO: Should be Role.UNKNOWN
+
+        MastershipEvent mastershipEvent =
+            new MastershipEvent(dpid, registryService.getControllerId(), role);
+        // FIXME should be merging, with existing attrs, etc..
+        // TODO define attr name as constant somewhere.
+        // TODO populate appropriate attributes.
+        mastershipEvent.createStringAttribute(TopologyElement.TYPE,
+                                              TopologyElement.TYPE_ALL_LAYERS);
+        mastershipEvent.freeze();
+        topologyDiscoveryInterface.removeSwitchMastershipEvent(mastershipEvent);
+    }
 }