Sho SHIMIZU
Committed by Ray Milkey

ONOS-2717: Handle duplicate resource unregistration in resource store

Change-Id: I49648be9c26ba66218172d570632d54584a908d8
...@@ -26,7 +26,7 @@ public interface ResourceStore { ...@@ -26,7 +26,7 @@ public interface ResourceStore {
26 /** 26 /**
27 * Unregisters the resources in transactional way. 27 * Unregisters the resources in transactional way.
28 * The state after completion of this method is all the resources are unregistered, 28 * The state after completion of this method is all the resources are unregistered,
29 - * or no resource is unregistered. The whole unregistration fails when any one of the 29 + * or none of the given resources is unregistered. The whole unregistration fails when any one of the
30 * resource can't be unregistered. 30 * resource can't be unregistered.
31 * 31 *
32 * @param resources resources to be unregistered 32 * @param resources resources to be unregistered
......
...@@ -305,6 +305,7 @@ public class ConsistentResourceStore implements ResourceStore { ...@@ -305,6 +305,7 @@ public class ConsistentResourceStore implements ResourceStore {
305 305
306 /** 306 /**
307 * Removes teh values from the existing values associated with the specified key. 307 * Removes teh values from the existing values associated with the specified key.
308 + * If the map doesn't contain the given values, removal will not happen.
308 * 309 *
309 * @param map map holding multiple values for a key 310 * @param map map holding multiple values for a key
310 * @param key key specifying values 311 * @param key key specifying values
...@@ -315,16 +316,18 @@ public class ConsistentResourceStore implements ResourceStore { ...@@ -315,16 +316,18 @@ public class ConsistentResourceStore implements ResourceStore {
315 */ 316 */
316 private <K, V> boolean removeValues(TransactionalMap<K, List<V>> map, K key, List<V> values) { 317 private <K, V> boolean removeValues(TransactionalMap<K, List<V>> map, K key, List<V> values) {
317 List<V> oldValues = map.get(key); 318 List<V> oldValues = map.get(key);
318 - List<V> newValues;
319 if (oldValues == null) { 319 if (oldValues == null) {
320 - newValues = new ArrayList<>(); 320 + return map.replace(key, oldValues, new ArrayList<>());
321 - } else {
322 - LinkedHashSet<V> newSet = new LinkedHashSet<>(oldValues);
323 - newSet.removeAll(values);
324 - newValues = new ArrayList<>(newSet);
325 } 321 }
326 322
327 - return map.replace(key, oldValues, newValues); 323 + LinkedHashSet<V> oldSet = new LinkedHashSet<>(oldValues);
324 + if (values.stream().allMatch(x -> !oldSet.contains(x))) {
325 + // don't write map because none of the values are stored
326 + return true;
327 + }
328 +
329 + oldSet.removeAll(values);
330 + return map.replace(key, oldValues, new ArrayList<>(oldSet));
328 } 331 }
329 332
330 /** 333 /**
......