Add a set of synchronous version of methods for MastershipService

Change-Id: I61da650eb545c14b5fa824ac92d1ebe62126dec3
diff --git a/core/api/src/main/java/org/onosproject/mastership/MastershipAdminService.java b/core/api/src/main/java/org/onosproject/mastership/MastershipAdminService.java
index 4d21233..091cf8e 100644
--- a/core/api/src/main/java/org/onosproject/mastership/MastershipAdminService.java
+++ b/core/api/src/main/java/org/onosproject/mastership/MastershipAdminService.java
@@ -16,7 +16,9 @@
 package org.onosproject.mastership;
 
 import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.TimeUnit;
 
+import org.onlab.util.Tools;
 import org.onosproject.cluster.NodeId;
 import org.onosproject.net.DeviceId;
 import org.onosproject.net.MastershipRole;
@@ -26,6 +28,8 @@
  */
 public interface MastershipAdminService {
 
+    long TIMEOUT_MILLIS = 3000;
+
     /**
      * Applies the current mastership role for the specified device.
      *
@@ -37,6 +41,18 @@
     CompletableFuture<Void> setRole(NodeId instance, DeviceId deviceId, MastershipRole role);
 
     /**
+     * Synchronous version of setRole.
+     * Applies the current mastership role for the specified device.
+     *
+     * @param instance controller instance identifier
+     * @param deviceId device identifier
+     * @param role     requested role
+     */
+    default void setRoleSync(NodeId instance, DeviceId deviceId, MastershipRole role) {
+        Tools.futureGetOrElse(setRole(instance, deviceId, role), TIMEOUT_MILLIS, TimeUnit.MILLISECONDS, null);
+    }
+
+    /**
      * Balances the mastership to be shared as evenly as possibly by all
      * online instances.
      */
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 30b8f12..9f9257d 100644
--- a/core/api/src/main/java/org/onosproject/mastership/MastershipService.java
+++ b/core/api/src/main/java/org/onosproject/mastership/MastershipService.java
@@ -16,10 +16,13 @@
 package org.onosproject.mastership;
 
 import static org.onosproject.net.MastershipRole.MASTER;
+import static org.onosproject.net.MastershipRole.NONE;
 
 import java.util.Set;
 import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.TimeUnit;
 
+import org.onlab.util.Tools;
 import org.onosproject.cluster.NodeId;
 import org.onosproject.cluster.RoleInfo;
 import org.onosproject.event.ListenerService;
@@ -35,11 +38,13 @@
 public interface MastershipService
     extends ListenerService<MastershipEvent, MastershipListener> {
 
+    long TIMEOUT_MILLIS = 3000;
+
     /**
      * Returns the role of the local node for the specified device, without
      * triggering master selection.
      *
-     * @param deviceId the the identifier of the device
+     * @param deviceId the identifier of the device
      * @return role of the current node
      */
     MastershipRole getLocalRole(DeviceId deviceId);
@@ -47,7 +52,7 @@
     /**
      * Returns true if the local controller is the Master for the specified deviceId.
      *
-     * @param deviceId the the identifier of the device
+     * @param deviceId the identifier of the device
      * @return true if local node is master; false otherwise
      */
     default boolean isLocalMaster(DeviceId deviceId) {
@@ -58,12 +63,24 @@
      * Returns the mastership status of the local controller for a given
      * device forcing master selection if necessary.
      *
-     * @param deviceId the the identifier of the device
-     * @return the role of this controller instance
+     * @param deviceId the identifier of the device
+     * @return future object of this controller instance role
      */
     CompletableFuture<MastershipRole> requestRoleFor(DeviceId deviceId);
 
     /**
+     * Synchronous version of requestRoleFor. Returns the mastership status of
+     * the local controller for a given device forcing master selection if necessary.
+     *
+     * @param deviceId the identifier of the device
+     * @return the role of this controller instance
+     */
+    default MastershipRole requestRoleForSync(DeviceId deviceId) {
+        return Tools.futureGetOrElse(requestRoleFor(deviceId),
+                TIMEOUT_MILLIS, TimeUnit.MILLISECONDS, NONE);
+    }
+
+    /**
      * Abandons mastership of the specified device on the local node thus
      * forcing selection of a new master. If the local node is not a master
      * for this device, no master selection will occur.
@@ -74,6 +91,18 @@
     CompletableFuture<Void> relinquishMastership(DeviceId deviceId);
 
     /**
+     * Synchronous version of relinquishMastership. Abandons mastership of the
+     * specified device on the local node thus forcing selection of a new master.
+     * If the local node is not a master for this device, no master selection will occur.
+     *
+     * @param deviceId the identifier of the device
+     */
+    default void relinquishMastershipSync(DeviceId deviceId) {
+        Tools.futureGetOrElse(relinquishMastership(deviceId),
+                TIMEOUT_MILLIS, TimeUnit.MILLISECONDS, null);
+    }
+
+    /**
      * Returns the current master for a given device.
      *
      * @param deviceId the identifier of the device