[ONOS-5192]Make devices unavailable when master fails

If the devices has no standby nodes and if the master of the
device is down, then this patch makes the device unavailable

Change-Id: I470187e13c93a7aa83c961a3d037e65b2627fc29
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 6fe1ca6..7a98a5f 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
@@ -332,6 +332,21 @@
                     } catch (ExecutionException e) {
                         log.error("Exception thrown while relinquishing role for {}", deviceId, e);
                     }
+                } else {
+                    // check if the device has master, if not, mark it offline
+                    NodeId masterId = mastershipService.getMasterFor(deviceId);
+                    // only the nodes which has mastership role can mark any device offline.
+                    CompletableFuture<MastershipRole> roleFuture = mastershipService.requestRoleFor(deviceId);
+                    roleFuture.thenAccept(role -> {
+                        MastershipTerm term = termService.getMastershipTerm(deviceId);
+                        if (term != null && localNodeId.equals(term.master())) {
+                            log.info("Marking unreachable device {} offline", deviceId);
+                            post(store.markOffline(deviceId));
+                        } else {
+                            log.info("Failed marking {} offline. {}", deviceId, role);
+                        }
+                        mastershipService.relinquishMastership(deviceId);
+                    });
                 }
                 continue;
             }