Mastership related minor fixes on DeviceManager

Change-Id: I8aefedd22b285f132d8b0cd2e7b76bf422dcf444
diff --git a/core/net/src/main/java/org/onlab/onos/net/device/impl/DeviceManager.java b/core/net/src/main/java/org/onlab/onos/net/device/impl/DeviceManager.java
index 8ee6f55..fd47dc7 100644
--- a/core/net/src/main/java/org/onlab/onos/net/device/impl/DeviceManager.java
+++ b/core/net/src/main/java/org/onlab/onos/net/device/impl/DeviceManager.java
@@ -153,8 +153,11 @@
             DeviceProvider provider = getProvider(device.providerId());
             if (provider != null) {
                 provider.roleChanged(device, newRole);
+
+                // only trigger event when request was sent to provider
+                // TODO: consider removing this from Device event type?
+                post(new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, device));
             }
-            post(new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, device));
         }
     }
 
@@ -164,6 +167,7 @@
         // XXX is this intended to apply to the full global topology?
         // if so, we probably don't want the fact that we aren't
         // MASTER to get in the way, as it would do now.
+        // FIXME: forward or broadcast and let the Master handler the event.
         DeviceEvent event = store.removeDevice(deviceId);
         if (event != null) {
             log.info("Device {} administratively removed", deviceId);
@@ -347,6 +351,9 @@
                     deviceId);
             if (role == MastershipRole.MASTER) {
                 mastershipService.relinquishMastership(deviceId);
+                // TODO: Shouldn't we be triggering event?
+                //final Device device = getDevice(deviceId);
+                //post(new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, device));
             }
         }
     }
@@ -367,7 +374,7 @@
             final NodeId myNodeId = clusterService.getLocalNode().id();
 
             log.info("## got Mastershipevent for dev {}", did);
-            if (myNodeId.equals(event.node())) {
+            if (myNodeId.equals(event.roleInfo().master())) {
                 MastershipTerm term = termService.getMastershipTerm(did);
 
                 if (!myNodeId.equals(term.master())) {
@@ -398,7 +405,7 @@
                 }
                 //TODO re-collect device information to fix potential staleness
                 applyRole(did, MastershipRole.MASTER);
-            } else {
+            } else if (event.roleInfo().backups().contains(myNodeId)) {
                 applyRole(did, MastershipRole.STANDBY);
             }
         }