Deduplicate candidates when constructing MastershipInfo in mastership store

Change-Id: If0a1343ea79798b2d7f2945f5246e99676e1bdb3
diff --git a/core/store/dist/src/main/java/org/onosproject/store/mastership/impl/ConsistentDeviceMastershipStore.java b/core/store/dist/src/main/java/org/onosproject/store/mastership/impl/ConsistentDeviceMastershipStore.java
index 826665d..70f20a0 100644
--- a/core/store/dist/src/main/java/org/onosproject/store/mastership/impl/ConsistentDeviceMastershipStore.java
+++ b/core/store/dist/src/main/java/org/onosproject/store/mastership/impl/ConsistentDeviceMastershipStore.java
@@ -22,7 +22,9 @@
 import static org.slf4j.LoggerFactory.getLogger;
 import static com.google.common.base.Preconditions.checkArgument;
 
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 import java.util.Objects;
 import java.util.Optional;
 import java.util.Set;
@@ -301,24 +303,24 @@
     }
 
     private MastershipInfo buildMastershipFromLeadership(Leadership leadership) {
-        ImmutableMap.Builder<NodeId, MastershipRole> builder = ImmutableMap.builder();
+        Map<NodeId, MastershipRole> roles = new HashMap<>();
         if (leadership.leaderNodeId() != null) {
-            builder.put(leadership.leaderNodeId(), MastershipRole.MASTER);
+            roles.put(leadership.leaderNodeId(), MastershipRole.MASTER);
         }
         leadership.candidates().stream()
             .filter(nodeId -> !Objects.equals(leadership.leaderNodeId(), nodeId))
-            .forEach(nodeId -> builder.put(nodeId, MastershipRole.STANDBY));
+            .forEach(nodeId -> roles.putIfAbsent(nodeId, MastershipRole.STANDBY));
         clusterService.getNodes().stream()
             .filter(node -> !Objects.equals(leadership.leaderNodeId(), node.id()))
             .filter(node -> !leadership.candidates().contains(node.id()))
-            .forEach(node -> builder.put(node.id(), MastershipRole.NONE));
+            .forEach(node -> roles.putIfAbsent(node.id(), MastershipRole.NONE));
 
         return new MastershipInfo(
             leadership.leader() != null ? leadership.leader().term() : 0,
             leadership.leader() != null
                 ? Optional.of(leadership.leader().nodeId())
                 : Optional.empty(),
-            builder.build());
+            ImmutableMap.copyOf(roles));
     }
 
     private class InternalDeviceMastershipEventListener implements LeadershipEventListener {