When relinquishing mastership, wait for LeadershipService::withdraw to complete before returning a MastershipEvent

Change-Id: Ieb0816bf3d17fee28815ea59959ed6d4a4efb478
diff --git a/core/store/dist/src/main/java/org/onosproject/store/mastership/impl/ConsistentDeviceMastershipStore.java b/core/store/dist/src/main/java/org/onosproject/store/mastership/impl/ConsistentDeviceMastershipStore.java
index 43af1b1..a791a7c 100644
--- a/core/store/dist/src/main/java/org/onosproject/store/mastership/impl/ConsistentDeviceMastershipStore.java
+++ b/core/store/dist/src/main/java/org/onosproject/store/mastership/impl/ConsistentDeviceMastershipStore.java
@@ -127,7 +127,7 @@
         transferExecutor =
                 Executors.newSingleThreadScheduledExecutor(
                         groupedThreads("onos/store/device/mastership", "mastership-transfer-executor"));
-        clusterCommunicator.<DeviceId, MastershipEvent>addSubscriber(ROLE_RELINQUISH_SUBJECT,
+        clusterCommunicator.addSubscriber(ROLE_RELINQUISH_SUBJECT,
                 SERIALIZER::decode,
                 this::relinquishLocalRole,
                 SERIALIZER::encode,
@@ -310,25 +310,26 @@
         checkArgument(nodeId != null, NODE_ID_NULL);
         checkArgument(deviceId != null, DEVICE_ID_NULL);
 
-        if (!nodeId.equals(localNodeId)) {
-            log.debug("Forwarding request to relinquish "
-                    + "role for device {} to {}", deviceId, nodeId);
-            return clusterCommunicator.sendAndReceive(
-                    deviceId,
-                    ROLE_RELINQUISH_SUBJECT,
-                    SERIALIZER::encode,
-                    SERIALIZER::decode,
-                    nodeId);
+        if (nodeId.equals(localNodeId)) {
+            return relinquishLocalRole(deviceId);
         }
-        return CompletableFuture.completedFuture(relinquishLocalRole(deviceId));
+
+        log.debug("Forwarding request to relinquish "
+                + "role for device {} to {}", deviceId, nodeId);
+        return clusterCommunicator.sendAndReceive(
+                deviceId,
+                ROLE_RELINQUISH_SUBJECT,
+                SERIALIZER::encode,
+                SERIALIZER::decode,
+                nodeId);
     }
 
-    private MastershipEvent relinquishLocalRole(DeviceId deviceId) {
+    private CompletableFuture<MastershipEvent> relinquishLocalRole(DeviceId deviceId) {
         checkArgument(deviceId != null, DEVICE_ID_NULL);
 
         // Check if this node is can be managed by this node.
         if (!connectedDevices.contains(deviceId)) {
-            return null;
+            return CompletableFuture.completedFuture(null);
         }
 
         String leadershipTopic = createDeviceMastershipTopic(deviceId);
@@ -339,9 +340,8 @@
             : MastershipEvent.Type.BACKUPS_CHANGED;
 
         connectedDevices.remove(deviceId);
-        leadershipService.withdraw(leadershipTopic);
-
-        return new MastershipEvent(eventType, deviceId, getNodes(deviceId));
+        return leadershipService.withdraw(leadershipTopic)
+                                .thenApply(v -> new MastershipEvent(eventType, deviceId, getNodes(deviceId)));
     }
 
     private MastershipEvent transitionFromMasterToStandby(DeviceId deviceId) {