BUG-8733: switch to using DOMDataTreeListener-based APIs
[mdsal.git] / dom / mdsal-dom-broker / src / main / java / org / opendaylight / mdsal / dom / broker / ShardedDOMDataTree.java
index 4ef05c9e40a3a8162f92c8554088a4b7ffba9662..21d160d79a5f4e0fcf9e2bb54121c80d75d5a9f3 100644 (file)
@@ -8,7 +8,11 @@
 package org.opendaylight.mdsal.dom.broker;
 
 import com.google.common.base.Preconditions;
+import com.google.common.base.Verify;
+import com.google.common.collect.ArrayListMultimap;
+import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Iterables;
+import com.google.common.collect.ListMultimap;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
@@ -25,6 +29,8 @@ import org.opendaylight.mdsal.dom.api.DOMDataTreeShardingConflictException;
 import org.opendaylight.mdsal.dom.api.DOMDataTreeShardingService;
 import org.opendaylight.mdsal.dom.spi.DOMDataTreePrefixTable;
 import org.opendaylight.mdsal.dom.spi.DOMDataTreePrefixTableEntry;
+import org.opendaylight.mdsal.dom.spi.shard.DOMDataTreeListenerAggregator;
+import org.opendaylight.mdsal.dom.spi.shard.ListenableDOMDataTreeShard;
 import org.opendaylight.mdsal.dom.spi.store.DOMStoreTreeChangePublisher;
 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
 import org.opendaylight.yangtools.concepts.ListenerRegistration;
@@ -35,13 +41,13 @@ public final class ShardedDOMDataTree implements DOMDataTreeService, DOMDataTree
     private static final Logger LOG = LoggerFactory.getLogger(ShardedDOMDataTree.class);
 
     @GuardedBy("this")
-    private final DOMDataTreePrefixTable<ShardRegistration<?>> shards = DOMDataTreePrefixTable.create();
+    private final DOMDataTreePrefixTable<DOMDataTreeShardRegistration<?>> shards = DOMDataTreePrefixTable.create();
     @GuardedBy("this")
     private final DOMDataTreePrefixTable<DOMDataTreeProducer> producers = DOMDataTreePrefixTable.create();
 
-    void removeShard(final ShardRegistration<?> reg) {
+    void removeShard(final DOMDataTreeShardRegistration<?> reg) {
         final DOMDataTreeIdentifier prefix = reg.getPrefix();
-        final ShardRegistration<?> parentReg;
+        final DOMDataTreeShardRegistration<?> parentReg;
 
         synchronized (this) {
             shards.remove(prefix);
@@ -71,8 +77,8 @@ public final class ShardedDOMDataTree implements DOMDataTreeService, DOMDataTree
         Preconditions.checkArgument(prefix.equals(firstSubtree), "Trying to register shard to a different namespace"
                 + " than the producer has claimed");
 
-        final ShardRegistration<T> reg;
-        final ShardRegistration<?> parentReg;
+        final DOMDataTreeShardRegistration<T> reg;
+        final DOMDataTreeShardRegistration<?> parentReg;
 
         synchronized (this) {
             /*
@@ -80,7 +86,7 @@ public final class ShardedDOMDataTree implements DOMDataTreeService, DOMDataTree
              * and if it exists, check if its registration prefix does not collide with
              * this registration.
              */
-            final DOMDataTreePrefixTableEntry<ShardRegistration<?>> parent = shards.lookup(prefix);
+            final DOMDataTreePrefixTableEntry<DOMDataTreeShardRegistration<?>> parent = shards.lookup(prefix);
             if (parent != null) {
                 parentReg = parent.getValue();
                 if (parentReg != null && prefix.equals(parentReg.getPrefix())) {
@@ -93,7 +99,7 @@ public final class ShardedDOMDataTree implements DOMDataTreeService, DOMDataTree
 
             // FIXME: wrap the shard in a proper adaptor based on implemented interface
 
-            reg = new ShardRegistration<T>(this, prefix, shard);
+            reg = new DOMDataTreeShardRegistration<>(this, prefix, shard);
 
             shards.store(prefix, reg);
 
@@ -146,7 +152,8 @@ public final class ShardedDOMDataTree implements DOMDataTreeService, DOMDataTree
             final DOMDataTreeProducer producer = findProducer(subtree);
             Preconditions.checkArgument(producer == null, "Subtree %s is attached to producer %s", subtree, producer);
 
-            final DOMDataTreePrefixTableEntry<ShardRegistration<?>> possibleShardReg = shards.lookup(subtree);
+            final DOMDataTreePrefixTableEntry<DOMDataTreeShardRegistration<?>> possibleShardReg =
+                    shards.lookup(subtree);
             if (possibleShardReg != null && possibleShardReg.getValue() != null) {
                 shardMap.put(subtree, possibleShardReg.getValue().getInstance());
             }
@@ -173,40 +180,79 @@ public final class ShardedDOMDataTree implements DOMDataTreeService, DOMDataTree
             final Collection<DOMDataTreeIdentifier> subtrees, final boolean allowRxMerges,
             final Collection<DOMDataTreeProducer> producers) throws DOMDataTreeLoopException {
         Preconditions.checkNotNull(listener, "listener");
-        Preconditions.checkArgument(!subtrees.isEmpty(), "Subtrees must not be empty.");
-        final ShardedDOMDataTreeListenerContext<T> listenerContext =
-                ShardedDOMDataTreeListenerContext.create(listener, subtrees, allowRxMerges);
-        try {
-            // FIXME: Add attachment of producers
-            for (final DOMDataTreeProducer producer : producers) {
-                Preconditions.checkArgument(producer instanceof ShardedDOMDataTreeProducer);
-                final ShardedDOMDataTreeProducer castedProducer = ((ShardedDOMDataTreeProducer) producer);
-                simpleLoopCheck(subtrees, castedProducer.getSubtrees());
-                // FIXME: We should also unbound listeners
-                castedProducer.bindToListener(listenerContext);
-            }
 
-            for (final DOMDataTreeIdentifier subtree : subtrees) {
-                final DOMDataTreeShard shard = shards.lookup(subtree).getValue().getInstance();
-                // FIXME: What should we do if listener is wildcard? And shards are on per
-                // node basis?
-                Preconditions.checkArgument(shard instanceof DOMStoreTreeChangePublisher,
-                        "Subtree %s does not point to listenable subtree.", subtree);
+        // Cross-check specified trees for exclusivity and eliminate duplicates, noDupSubtrees is effectively a Set
+        final Collection<DOMDataTreeIdentifier> noDupSubtrees;
+        switch (subtrees.size()) {
+            case 0:
+                throw new IllegalArgumentException("Subtrees must not be empty.");
+            case 1:
+                noDupSubtrees = subtrees;
+                break;
+            default:
+                // Check subtrees for mutual inclusion, this is an O(N**2) operation
+                for (DOMDataTreeIdentifier toCheck : subtrees) {
+                    for (DOMDataTreeIdentifier against : subtrees) {
+                        if (!toCheck.equals(against)) {
+                            Preconditions.checkArgument(!toCheck.contains(against), "Subtree %s contains subtree %s",
+                                toCheck, against);
+                        }
+                    }
+                }
 
-                listenerContext.register(subtree, (DOMStoreTreeChangePublisher) shard);
-            }
-        } catch (final Exception e) {
-            listenerContext.close();
-            throw e;
+                noDupSubtrees = ImmutableSet.copyOf(subtrees);
+        }
+
+        LOG.trace("Requested registration of listener {} to subtrees {}", listener, noDupSubtrees);
+
+        // Lookup shards corresponding to subtrees and construct a map of which subtrees we want from which shard
+        final ListMultimap<DOMDataTreeShardRegistration<?>, DOMDataTreeIdentifier> needed =
+                ArrayListMultimap.create();
+        for (final DOMDataTreeIdentifier subtree : subtrees) {
+            final DOMDataTreeShardRegistration<?> reg = Verify.verifyNotNull(shards.lookup(subtree).getValue());
+            needed.put(reg, subtree);
+        }
+
+        LOG.trace("Listener {} is attaching to shards {}", listener, needed);
+
+        // Sanity check: all selected shards have to support one of the listening interfaces
+        needed.asMap().forEach((reg, trees) -> {
+            final DOMDataTreeShard shard = reg.getInstance();
+            Preconditions.checkArgument(shard instanceof ListenableDOMDataTreeShard
+                || shard instanceof DOMStoreTreeChangePublisher, "Subtrees %s do not point to listenable subtree.",
+                trees);
+        });
+
+        // Sanity check: all producers have to come from this implementation and must not form loops
+        for (DOMDataTreeProducer producer : producers) {
+            Preconditions.checkArgument(producer instanceof ShardedDOMDataTreeProducer);
+            simpleLoopCheck(subtrees, ((ShardedDOMDataTreeProducer) producer).getSubtrees());
         }
+
+        final ListenerRegistration<?> underlyingRegistration = createRegisteredListener(listener, needed.asMap(),
+            allowRxMerges, producers);
         return new AbstractListenerRegistration<T>(listener) {
             @Override
             protected void removeRegistration() {
-                ShardedDOMDataTree.this.removeListener(listenerContext);
+                ShardedDOMDataTree.this.removeListener(listener);
+                underlyingRegistration.close();
             }
         };
     }
 
+    private static ListenerRegistration<?> createRegisteredListener(final DOMDataTreeListener userListener,
+            final Map<DOMDataTreeShardRegistration<?>, Collection<DOMDataTreeIdentifier>> needed,
+            final boolean allowRxMerges, final Collection<DOMDataTreeProducer> producers) {
+        // FIXME: Add attachment of producers
+        for (final DOMDataTreeProducer producer : producers) {
+            // FIXME: We should also unbound listeners
+            ((ShardedDOMDataTreeProducer) producer).bindToListener(userListener);
+        }
+
+        return DOMDataTreeListenerAggregator.aggregateIfNeeded(userListener, needed, allowRxMerges,
+            DOMDataTreeShardRegistration::getInstance);
+    }
+
     private static void simpleLoopCheck(final Collection<DOMDataTreeIdentifier> listen,
             final Set<DOMDataTreeIdentifier> writes) throws DOMDataTreeLoopException {
         for (final DOMDataTreeIdentifier listenPath : listen) {
@@ -224,8 +270,7 @@ public final class ShardedDOMDataTree implements DOMDataTreeService, DOMDataTree
         }
     }
 
-    void removeListener(final ShardedDOMDataTreeListenerContext<?> listener) {
+    void removeListener(final DOMDataTreeListener listener) {
         // FIXME: detach producers
-        listener.close();
     }
 }