Fix for Mastership exception fetching timestamp - from Madan

Change-Id: Ifec4fe8a559a7d5832afc8eb62c936b28fffc186
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 5b92489..9045200 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
@@ -312,13 +312,15 @@
         log.debug("Checking mastership");
         for (Device device : getDevices()) {
             final DeviceId deviceId = device.id();
-            log.trace("Checking device {}", deviceId);
-
+            MastershipRole myRole = mastershipService.getLocalRole(deviceId);
+            log.trace("Checking device {}. Current role is {}", deviceId, myRole);
             if (!isReachable(deviceId)) {
-                if (mastershipService.getLocalRole(deviceId) != NONE) {
+                if (myRole != NONE) {
                     // can't be master if device is not reachable
                     try {
-                        post(store.markOffline(deviceId));
+                        if (myRole == MASTER) {
+                            post(store.markOffline(deviceId));
+                        }
                         //relinquish master role and ability to be backup.
                         mastershipService.relinquishMastership(deviceId).get();
                     } catch (InterruptedException e) {
@@ -331,7 +333,7 @@
                 continue;
             }
 
-            if (mastershipService.getLocalRole(deviceId) != NONE) {
+            if (myRole != NONE) {
                 continue;
             }