Sho SHIMIZU
Committed by Thomas Vachuska

ONOS-2716: Handle duplicate registration in resource store

Change-Id: Ic46ad17359d76a23fa9d9c78afc4fc65a41d1dbc
...@@ -15,7 +15,7 @@ public interface ResourceStore { ...@@ -15,7 +15,7 @@ public interface ResourceStore {
15 /** 15 /**
16 * Registers the resources in transactional way. 16 * Registers the resources in transactional way.
17 * Resource registration is must be done before resource allocation. The state after completion 17 * Resource registration is must be done before resource allocation. The state after completion
18 - * of this method is all the resources are registered, or no resource is registered. 18 + * of this method is all the resources are registered, or none of the given resources is registered.
19 * The whole registration fails when any one of the resource can't be registered. 19 * The whole registration fails when any one of the resource can't be registered.
20 * 20 *
21 * @param resources resources to be registered 21 * @param resources resources to be registered
......
...@@ -278,6 +278,7 @@ public class ConsistentResourceStore implements ResourceStore { ...@@ -278,6 +278,7 @@ public class ConsistentResourceStore implements ResourceStore {
278 278
279 /** 279 /**
280 * Appends the values to the existing values associated with the specified key. 280 * Appends the values to the existing values associated with the specified key.
281 + * If the map already has all the given values, appending will not happen.
281 * 282 *
282 * @param map map holding multiple values for a key 283 * @param map map holding multiple values for a key
283 * @param key key specifying values 284 * @param key key specifying values
...@@ -288,16 +289,18 @@ public class ConsistentResourceStore implements ResourceStore { ...@@ -288,16 +289,18 @@ public class ConsistentResourceStore implements ResourceStore {
288 */ 289 */
289 private <K, V> boolean appendValues(TransactionalMap<K, List<V>> map, K key, List<V> values) { 290 private <K, V> boolean appendValues(TransactionalMap<K, List<V>> map, K key, List<V> values) {
290 List<V> oldValues = map.get(key); 291 List<V> oldValues = map.get(key);
291 - List<V> newValues;
292 if (oldValues == null) { 292 if (oldValues == null) {
293 - newValues = new ArrayList<>(values); 293 + return map.replace(key, oldValues, new ArrayList<>(values));
294 - } else {
295 - LinkedHashSet<V> newSet = new LinkedHashSet<>(oldValues);
296 - newSet.addAll(values);
297 - newValues = new ArrayList<>(newSet);
298 } 294 }
299 295
300 - return map.replace(key, oldValues, newValues); 296 + LinkedHashSet<V> oldSet = new LinkedHashSet<>(oldValues);
297 + if (oldSet.containsAll(values)) {
298 + // don't write to map because all values are already stored
299 + return true;
300 + }
301 +
302 + oldSet.addAll(values);
303 + return map.replace(key, oldValues, new ArrayList<>(oldSet));
301 } 304 }
302 305
303 /** 306 /**
......