Brian Stanke
Committed by Gerrit Code Review

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
...@@ -169,6 +169,16 @@ public class DistributedRegionStore ...@@ -169,6 +169,16 @@ public class DistributedRegionStore
169 169
170 @Override 170 @Override
171 public void addDevices(RegionId regionId, Collection<DeviceId> deviceIds) { 171 public void addDevices(RegionId regionId, Collection<DeviceId> deviceIds) {
172 + // Devices can only be a member in one region. Remove the device if it belongs to
173 + // a different region than the region for which we are attempting to add it.
174 + for (DeviceId deviceId : deviceIds) {
175 + Region region = getRegionForDevice(deviceId);
176 + if ((region != null) && (!regionId.id().equals(region.id().id()))) {
177 + Set<DeviceId> deviceIdSet1 = ImmutableSet.of(deviceId);
178 + removeDevices(region.id(), deviceIdSet1);
179 + }
180 + }
181 +
172 membershipRepo.compute(regionId, (id, existingDevices) -> { 182 membershipRepo.compute(regionId, (id, existingDevices) -> {
173 if (existingDevices == null) { 183 if (existingDevices == null) {
174 return ImmutableSet.copyOf(deviceIds); 184 return ImmutableSet.copyOf(deviceIds);
......
...@@ -137,6 +137,15 @@ public class DistributedRegionStoreTest { ...@@ -137,6 +137,15 @@ public class DistributedRegionStoreTest {
137 deviceIds = store.getRegionDevices(RID1); 137 deviceIds = store.getRegionDevices(RID1);
138 assertEquals("incorrect device count", 3, deviceIds.size()); 138 assertEquals("incorrect device count", 3, deviceIds.size());
139 139
140 + // Test adding DID3 to RID2 but it is already in RID1.
141 + // DID3 will be removed from RID1 and added to RID2.
142 + Region r2 = store.createRegion(RID2, "R2", CAMPUS, MASTERS);
143 + store.addDevices(RID2, ImmutableSet.of(DID3));
144 + deviceIds = store.getRegionDevices(RID1);
145 + assertEquals("incorrect device count", 2, deviceIds.size());
146 + deviceIds = store.getRegionDevices(RID2);
147 + assertEquals("incorrect device count", 1, deviceIds.size());
148 +
140 store.removeDevices(RID1, ImmutableSet.of(DID2, DID3)); 149 store.removeDevices(RID1, ImmutableSet.of(DID2, DID3));
141 deviceIds = store.getRegionDevices(RID1); 150 deviceIds = store.getRegionDevices(RID1);
142 assertEquals("incorrect device count", 1, deviceIds.size()); 151 assertEquals("incorrect device count", 1, deviceIds.size());
......