5546

Change-Id: I148096bed14ef7b67f8cfa6c9cdaf9422a6a6d35
(cherry picked from commit 76eeab38c9164abc4166e0b84a96c66aa06a48b4)
diff --git a/web/api/src/main/java/org/onosproject/rest/resources/MastershipWebResource.java b/web/api/src/main/java/org/onosproject/rest/resources/MastershipWebResource.java
index dcbf394..90a2159 100644
--- a/web/api/src/main/java/org/onosproject/rest/resources/MastershipWebResource.java
+++ b/web/api/src/main/java/org/onosproject/rest/resources/MastershipWebResource.java
@@ -24,6 +24,7 @@
 import org.onosproject.mastership.MastershipService;
 import org.onosproject.net.DeviceId;
 import org.onosproject.net.MastershipRole;
+import org.onosproject.net.device.DeviceService;
 import org.onosproject.rest.AbstractWebResource;
 
 import javax.ws.rs.Consumes;
@@ -53,10 +54,12 @@
     private static final String DEVICE_ID_INVALID = "Invalid deviceId for setting role";
     private static final String NODE_ID_INVALID = "Invalid nodeId for setting role";
 
+    private static final String DEVICE_ID_NOT_FOUND = "Device Id is not found";
     private static final String NODE_ID_NOT_FOUND = "Node id is not found";
     private static final String ROLE_INFO_NOT_FOUND = "Role info is not found";
     private static final String MASTERSHIP_ROLE_NOT_FOUND = "Mastership role is not found";
 
+    private final DeviceService deviceService = get(DeviceService.class);
     private final MastershipService mastershipService = get(MastershipService.class);
     private final MastershipAdminService mastershipAdminService =
                                          get(MastershipAdminService.class);
@@ -148,8 +151,11 @@
     @Produces(MediaType.APPLICATION_JSON)
     @Path("{deviceId}/request")
     public Response requestRoleFor(@PathParam("deviceId") String deviceId) {
-        MastershipRole role = nullIsNotFound(mastershipService.requestRoleForSync(
-                                DeviceId.deviceId(deviceId)), MASTERSHIP_ROLE_NOT_FOUND);
+        DeviceId id = DeviceId.deviceId(deviceId);
+        nullIsNotFound(deviceService.getDevice(id), DEVICE_ID_NOT_FOUND);
+
+        MastershipRole role = nullIsNotFound(mastershipService.requestRoleForSync(id),
+                        MASTERSHIP_ROLE_NOT_FOUND);
         ObjectNode root = codec(MastershipRole.class).encode(role, this);
         return ok(root).build();
     }
diff --git a/web/api/src/test/java/org/onosproject/rest/resources/MastershipResourceTest.java b/web/api/src/test/java/org/onosproject/rest/resources/MastershipResourceTest.java
index 994e138..17737a1 100644
--- a/web/api/src/test/java/org/onosproject/rest/resources/MastershipResourceTest.java
+++ b/web/api/src/test/java/org/onosproject/rest/resources/MastershipResourceTest.java
@@ -35,8 +35,11 @@
 import org.onosproject.codec.impl.CodecManager;
 import org.onosproject.mastership.MastershipAdminService;
 import org.onosproject.mastership.MastershipService;
+import org.onosproject.net.DefaultDevice;
+import org.onosproject.net.Device;
 import org.onosproject.net.DeviceId;
 import org.onosproject.net.MastershipRole;
+import org.onosproject.net.device.DeviceService;
 
 import javax.ws.rs.client.Entity;
 import javax.ws.rs.client.WebTarget;
@@ -64,6 +67,7 @@
 public final class MastershipResourceTest extends ResourceTest {
 
     private final MastershipService mockService = createMock(MastershipService.class);
+    private final DeviceService mockDeviceService = createMock(DeviceService.class);
     private final MastershipAdminService mockAdminService =
                   createMock(MastershipAdminService.class);
 
@@ -71,6 +75,8 @@
     private final DeviceId deviceId2 = DeviceId.deviceId("dev:2");
     private final DeviceId deviceId3 = DeviceId.deviceId("dev:3");
 
+    final Device device1 = new DefaultDevice(null, deviceId1, Device.Type.OTHER,
+            "", "", "", "", null);
     private final NodeId nodeId1 = NodeId.nodeId("node:1");
     private final NodeId nodeId2 = NodeId.nodeId("node:2");
     private final NodeId nodeId3 = NodeId.nodeId("node:3");
@@ -161,6 +167,7 @@
                 new TestServiceDirectory()
                         .add(MastershipService.class, mockService)
                         .add(MastershipAdminService.class, mockAdminService)
+                        .add(DeviceService.class, mockDeviceService)
                         .add(CodecService.class, codecService);
 
         BaseResource.setServiceDirectory(testDirectory);
@@ -287,6 +294,9 @@
         expect(mockService.requestRoleForSync(anyObject())).andReturn(role1).anyTimes();
         replay(mockService);
 
+        expect(mockDeviceService.getDevice(deviceId1)).andReturn(device1);
+        replay(mockDeviceService);
+
         final WebTarget wt = target();
         final String response = wt.path("mastership/" + deviceId1.toString() +
                 "/request").request().get(String.class);