ONOS-4018 - Enhance the DistributedRegionStore to make sure that a
device ID appears in at most one Region's list of devices.

Change-Id: I6d30fab2c09544c68f49b11682f08ee8ded060fe
diff --git a/core/store/dist/src/main/java/org/onosproject/store/region/impl/DistributedRegionStore.java b/core/store/dist/src/main/java/org/onosproject/store/region/impl/DistributedRegionStore.java
index 6243dac..009f5d0 100644
--- a/core/store/dist/src/main/java/org/onosproject/store/region/impl/DistributedRegionStore.java
+++ b/core/store/dist/src/main/java/org/onosproject/store/region/impl/DistributedRegionStore.java
@@ -169,6 +169,16 @@
 
     @Override
     public void addDevices(RegionId regionId, Collection<DeviceId> deviceIds) {
+        // Devices can only be a member in one region.  Remove the device if it belongs to
+        // a different region than the region for which we are attempting to add it.
+        for (DeviceId deviceId : deviceIds) {
+            Region region = getRegionForDevice(deviceId);
+            if ((region != null) && (!regionId.id().equals(region.id().id()))) {
+                Set<DeviceId> deviceIdSet1 = ImmutableSet.of(deviceId);
+                removeDevices(region.id(), deviceIdSet1);
+            }
+        }
+
         membershipRepo.compute(regionId, (id, existingDevices) -> {
             if (existingDevices == null) {
                 return ImmutableSet.copyOf(deviceIds);