Check for in-progress upgrade before attempting to change NONE role in DeviceManager

Change-Id: I3f8ef2ecb5ec57685fdb687bfcb65bd774e031ce
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 ae8018e..59129c7 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
@@ -71,6 +71,7 @@
 import org.onosproject.store.cluster.messaging.MessageSubject;
 import org.onosproject.store.serializers.KryoNamespaces;
 import org.onosproject.store.service.Serializer;
+import org.onosproject.upgrade.UpgradeService;
 import org.slf4j.Logger;
 
 import java.time.Instant;
@@ -144,6 +145,9 @@
     protected MastershipTermService termService;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected UpgradeService upgradeService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected NetworkConfigService networkConfigService;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
@@ -463,7 +467,7 @@
 
             // isReachable but was not MASTER or STANDBY, get a role and apply
             // Note: NONE triggers request to MastershipService
-            reassertRole(deviceId, mastershipService.getLocalRole(deviceId));
+            reassertRole(deviceId, NONE);
         }
     }
 
@@ -819,25 +823,43 @@
     private void reassertRole(final DeviceId did,
                               final MastershipRole nextRole) {
 
-        switch (nextRole) {
+        MastershipRole myNextRole = nextRole;
+        if (myNextRole == NONE && upgradeService.isLocalActive()) {
+            try {
+                mastershipService.requestRoleFor(did).get();
+                MastershipTerm term = termService.getMastershipTerm(did);
+                if (term != null && localNodeId.equals(term.master())) {
+                    myNextRole = MASTER;
+                } else {
+                    myNextRole = STANDBY;
+                }
+            } catch (InterruptedException e) {
+                Thread.currentThread().interrupt();
+                log.error("Interrupted waiting for Mastership", e);
+            } catch (ExecutionException e) {
+                log.error("Encountered an error waiting for Mastership", e);
+            }
+        }
+
+        switch (myNextRole) {
             case MASTER:
                 final Device device = getDevice(did);
                 if ((device != null) && !isAvailable(did)) {
                     store.markOnline(did);
                 }
                 // TODO: should apply role only if there is mismatch
-                log.debug("Applying role {} to {}", nextRole, did);
+                log.debug("Applying role {} to {}", myNextRole, did);
                 if (!applyRoleAndProbe(did, MASTER)) {
-                    log.warn("Unsuccessful applying role {} to {}", nextRole, did);
+                    log.warn("Unsuccessful applying role {} to {}", myNextRole, did);
                     // immediately failed to apply role
                     mastershipService.relinquishMastership(did);
                     // FIXME disconnect?
                 }
                 break;
             case STANDBY:
-                log.debug("Applying role {} to {}", nextRole, did);
+                log.debug("Applying role {} to {}", myNextRole, did);
                 if (!applyRoleAndProbe(did, STANDBY)) {
-                    log.warn("Unsuccessful applying role {} to {}", nextRole, did);
+                    log.warn("Unsuccessful applying role {} to {}", myNextRole, did);
                     // immediately failed to apply role
                     mastershipService.relinquishMastership(did);
                     // FIXME disconnect?