fixes for mastership handoff race conditions

Change-Id: Ifed733df1bdc3b144b6a341a9322838ea2aacd72
diff --git a/core/net/src/main/java/org/onlab/onos/net/link/impl/LinkManager.java b/core/net/src/main/java/org/onlab/onos/net/link/impl/LinkManager.java
index 779e1ee..bab9955 100644
--- a/core/net/src/main/java/org/onlab/onos/net/link/impl/LinkManager.java
+++ b/core/net/src/main/java/org/onlab/onos/net/link/impl/LinkManager.java
@@ -16,6 +16,7 @@
 import org.onlab.onos.net.ConnectPoint;
 import org.onlab.onos.net.DeviceId;
 import org.onlab.onos.net.Link;
+import org.onlab.onos.net.MastershipRole;
 import org.onlab.onos.net.device.DeviceEvent;
 import org.onlab.onos.net.device.DeviceListener;
 import org.onlab.onos.net.device.DeviceService;
@@ -139,11 +140,17 @@
 
     @Override
     public void removeLinks(ConnectPoint connectPoint) {
+        if (deviceService.getRole(connectPoint.deviceId()) != MastershipRole.MASTER) {
+            return;
+        }
         removeLinks(getLinks(connectPoint));
     }
 
     @Override
     public void removeLinks(DeviceId deviceId) {
+        if (deviceService.getRole(deviceId) != MastershipRole.MASTER) {
+            return;
+        }
         removeLinks(getDeviceLinks(deviceId));
     }
 
@@ -189,6 +196,15 @@
         public void linkDetected(LinkDescription linkDescription) {
             checkNotNull(linkDescription, LINK_DESC_NULL);
             checkValidity();
+
+            ConnectPoint src = linkDescription.src();
+            ConnectPoint dst = linkDescription.dst();
+            // if we aren't master for the device associated with the ConnectPoint
+            // we probably shouldn't be doing this.
+            if ((deviceService.getRole(src.deviceId()) != MastershipRole.MASTER) ||
+                    (deviceService.getRole(dst.deviceId()) != MastershipRole.MASTER)) {
+                return;
+            }
             LinkEvent event = store.createOrUpdateLink(provider().id(),
                                                        linkDescription);
             if (event != null) {
@@ -201,6 +217,15 @@
         public void linkVanished(LinkDescription linkDescription) {
             checkNotNull(linkDescription, LINK_DESC_NULL);
             checkValidity();
+
+            ConnectPoint src = linkDescription.src();
+            ConnectPoint dst = linkDescription.dst();
+            // if we aren't master for the device associated with the ConnectPoint
+            // we probably shouldn't be doing this.
+            if ((deviceService.getRole(src.deviceId()) != MastershipRole.MASTER) ||
+                    (deviceService.getRole(dst.deviceId()) != MastershipRole.MASTER)) {
+                return;
+            }
             LinkEvent event = store.removeLink(linkDescription.src(),
                                                linkDescription.dst());
             if (event != null) {
@@ -213,6 +238,11 @@
         public void linksVanished(ConnectPoint connectPoint) {
             checkNotNull(connectPoint, "Connect point cannot be null");
             checkValidity();
+            // if we aren't master for the device associated with the ConnectPoint
+            // we probably shouldn't be doing this.
+            if (deviceService.getRole(connectPoint.deviceId()) != MastershipRole.MASTER) {
+                return;
+            }
             log.info("Links for connection point {} vanished", connectPoint);
             removeLinks(getLinks(connectPoint));
         }
@@ -221,6 +251,11 @@
         public void linksVanished(DeviceId deviceId) {
             checkNotNull(deviceId, DEVICE_ID_NULL);
             checkValidity();
+            // if we aren't master for the device associated with the ConnectPoint
+            // we probably shouldn't be doing this.
+            if (deviceService.getRole(deviceId) != MastershipRole.MASTER) {
+                return;
+            }
             log.info("Links for device {} vanished", deviceId);
             removeLinks(getDeviceLinks(deviceId));
         }