MastershipService.relinquishRole returns CompletableFuture + Block deviceDiconnected until role relinquish is complete

Change-Id: I081df48fc05fdca2e452a937a093d5caa16091ed
diff --git a/core/api/src/main/java/org/onosproject/mastership/MastershipService.java b/core/api/src/main/java/org/onosproject/mastership/MastershipService.java
index 450bcc5..79eef94 100644
--- a/core/api/src/main/java/org/onosproject/mastership/MastershipService.java
+++ b/core/api/src/main/java/org/onosproject/mastership/MastershipService.java
@@ -55,8 +55,9 @@
      * for this device, no master selection will occur.
      *
      * @param deviceId the identifier of the device
+     * @return future that is completed when relinquish is complete
      */
-    void relinquishMastership(DeviceId deviceId);
+    CompletableFuture<Void> relinquishMastership(DeviceId deviceId);
 
     /**
      * Returns the current master for a given device.
diff --git a/core/api/src/test/java/org/onosproject/mastership/MastershipServiceAdapter.java b/core/api/src/test/java/org/onosproject/mastership/MastershipServiceAdapter.java
index aff67cd..7db9b38 100644
--- a/core/api/src/test/java/org/onosproject/mastership/MastershipServiceAdapter.java
+++ b/core/api/src/test/java/org/onosproject/mastership/MastershipServiceAdapter.java
@@ -38,7 +38,8 @@
     }
 
     @Override
-    public void relinquishMastership(DeviceId deviceId) {
+    public CompletableFuture<Void> relinquishMastership(DeviceId deviceId) {
+        return null;
     }
 
     @Override
diff --git a/core/net/src/main/java/org/onosproject/cluster/impl/MastershipManager.java b/core/net/src/main/java/org/onosproject/cluster/impl/MastershipManager.java
index cc33d56..1ac073a 100644
--- a/core/net/src/main/java/org/onosproject/cluster/impl/MastershipManager.java
+++ b/core/net/src/main/java/org/onosproject/cluster/impl/MastershipManager.java
@@ -95,12 +95,13 @@
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected MetricsService metricsService;
 
+    private NodeId localNodeId;
     private Timer requestRoleTimer;
 
     @Activate
     public void activate() {
         requestRoleTimer = createTimer("Mastership", "requestRole", "responseTime");
-
+        localNodeId = clusterService.getLocalNode().id();
         eventDispatcher.addSink(MastershipEvent.class, listenerRegistry);
         store.setDelegate(delegate);
         log.info("Started");
@@ -136,11 +137,8 @@
                 return CompletableFuture.completedFuture(null);
         }
 
-        return eventFuture.whenComplete((event, error) -> {
-            if (event != null) {
-                post(event);
-            }
-        }).thenApply(v -> null);
+        return eventFuture.thenAccept(this::post)
+                          .thenApply(v -> null);
     }
 
     @Override
@@ -152,15 +150,11 @@
     }
 
     @Override
-    public void relinquishMastership(DeviceId deviceId) {
+    public CompletableFuture<Void> relinquishMastership(DeviceId deviceId) {
         checkPermission(Permission.CLUSTER_WRITE);
-
-        store.relinquishRole(clusterService.getLocalNode().id(), deviceId)
-             .whenComplete((event, error) -> {
-                 if (event != null) {
-                     post(event);
-                 }
-             });
+        return store.relinquishRole(localNodeId, deviceId)
+                    .thenAccept(this::post)
+                    .thenApply(v -> null);
     }
 
     @Override
diff --git a/core/net/src/main/java/org/onosproject/net/device/impl/DeviceManager.java b/core/net/src/main/java/org/onosproject/net/device/impl/DeviceManager.java
index 37c431e..73bbe2f 100644
--- a/core/net/src/main/java/org/onosproject/net/device/impl/DeviceManager.java
+++ b/core/net/src/main/java/org/onosproject/net/device/impl/DeviceManager.java
@@ -16,7 +16,6 @@
 package org.onosproject.net.device.impl;
 
 import com.google.common.collect.Lists;
-
 import org.apache.felix.scr.annotations.Activate;
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Deactivate;
@@ -411,8 +410,15 @@
                     }
                 });
             } finally {
-                //relinquish master role and ability to be backup.
-                mastershipService.relinquishMastership(deviceId);
+                try {
+                    //relinquish master role and ability to be backup.
+                    mastershipService.relinquishMastership(deviceId).get();
+                } catch (InterruptedException e) {
+                    log.warn("Interrupted while reliquishing role for {}", deviceId);
+                    Thread.currentThread().interrupt();
+                } catch (ExecutionException e) {
+                    log.error("Exception thrown while relinquishing role for {}", deviceId, e);
+                }
             }
         }
 
diff --git a/core/net/src/test/java/org/onosproject/net/device/impl/DeviceManagerTest.java b/core/net/src/test/java/org/onosproject/net/device/impl/DeviceManagerTest.java
index 0b6c3e0..fdda578 100644
--- a/core/net/src/test/java/org/onosproject/net/device/impl/DeviceManagerTest.java
+++ b/core/net/src/test/java/org/onosproject/net/device/impl/DeviceManagerTest.java
@@ -311,6 +311,11 @@
         }
 
         @Override
+        public CompletableFuture<Void> relinquishMastership(DeviceId deviceId) {
+            return CompletableFuture.completedFuture(null);
+        }
+
+        @Override
         public MastershipTerm getMastershipTerm(DeviceId deviceId) {
             // FIXME: just returning something not null
             return MastershipTerm.of(NID_LOCAL, 1);
diff --git a/core/store/dist/src/test/java/org/onosproject/store/device/impl/GossipDeviceStoreTest.java b/core/store/dist/src/test/java/org/onosproject/store/device/impl/GossipDeviceStoreTest.java
index 68eb73d..dc08e76 100644
--- a/core/store/dist/src/test/java/org/onosproject/store/device/impl/GossipDeviceStoreTest.java
+++ b/core/store/dist/src/test/java/org/onosproject/store/device/impl/GossipDeviceStoreTest.java
@@ -37,6 +37,7 @@
 import org.onosproject.net.DefaultAnnotations;
 import org.onosproject.net.Device;
 import org.onosproject.net.DeviceId;
+import org.onosproject.net.MastershipRole;
 import org.onosproject.net.Port;
 import org.onosproject.net.PortNumber;
 import org.onosproject.net.SparseAnnotations;
@@ -62,6 +63,7 @@
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.TimeUnit;
@@ -854,6 +856,10 @@
         public NodeId getMasterFor(DeviceId deviceId) {
             return NID1;
         }
+        @Override
+        public CompletableFuture<MastershipRole> requestRoleFor(DeviceId deviceId) {
+            return CompletableFuture.completedFuture(null);
+        }
     }
 
     private static final class TestGossipDeviceStore extends GossipDeviceStore {
diff --git a/providers/lldp/src/test/java/org/onosproject/provider/lldp/impl/LLDPLinkProviderTest.java b/providers/lldp/src/test/java/org/onosproject/provider/lldp/impl/LLDPLinkProviderTest.java
index 4395914..f6627af 100644
--- a/providers/lldp/src/test/java/org/onosproject/provider/lldp/impl/LLDPLinkProviderTest.java
+++ b/providers/lldp/src/test/java/org/onosproject/provider/lldp/impl/LLDPLinkProviderTest.java
@@ -502,8 +502,8 @@
         }
 
         @Override
-        public void relinquishMastership(DeviceId deviceId) {
-
+        public CompletableFuture<Void> relinquishMastership(DeviceId deviceId) {
+            return null;
         }
 
         @Override