Merge "BUG-509: Encapsulate ListenerTree"
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / store / impl / DataChangeEventResolver.java
index c2faf86bce73427c11217fd2ef7c2bfdb905a864..df2725d0206f1bb91716e8505b61214de6a16d65 100644 (file)
@@ -4,29 +4,35 @@ import static org.opendaylight.controller.md.sal.dom.store.impl.DOMImmutableData
 import static org.opendaylight.controller.md.sal.dom.store.impl.StoreUtils.append;
 import static org.opendaylight.controller.md.sal.dom.store.impl.tree.TreeNodeUtils.getChild;
 
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Set;
+
 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
 import org.opendaylight.controller.md.sal.dom.store.impl.DOMImmutableDataChangeEvent.Builder;
-import org.opendaylight.controller.md.sal.dom.store.impl.tree.ListenerRegistrationNode;
+import org.opendaylight.controller.md.sal.dom.store.impl.tree.ListenerTree;
+import org.opendaylight.controller.md.sal.dom.store.impl.tree.ListenerTree.Walker;
 import org.opendaylight.controller.md.sal.dom.store.impl.tree.NodeModification;
 import org.opendaylight.controller.md.sal.dom.store.impl.tree.StoreMetadataNode;
-import org.opendaylight.controller.md.sal.dom.store.impl.tree.ListenerRegistrationNode.DataChangeListenerRegistration;
 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import com.google.common.base.Optional;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableSet;
 
 public class DataChangeEventResolver {
-
+    private static final Logger LOG = LoggerFactory.getLogger(DataChangeEventResolver.class);
     private static final DOMImmutableDataChangeEvent NO_CHANGE = builder().build();
+    private final ImmutableList.Builder<ChangeListenerNotifyTask> tasks = ImmutableList.builder();
     private InstanceIdentifier rootPath;
-    private ListenerRegistrationNode listenerRoot;
+    private ListenerTree listenerRoot;
     private NodeModification modificationRoot;
     private Optional<StoreMetadataNode> beforeRoot;
     private Optional<StoreMetadataNode> afterRoot;
-    private final ImmutableList.Builder<ChangeListenerNotifyTask> tasks = ImmutableList.builder();
 
     protected InstanceIdentifier getRootPath() {
         return rootPath;
@@ -37,11 +43,11 @@ public class DataChangeEventResolver {
         return this;
     }
 
-    protected ListenerRegistrationNode getListenerRoot() {
+    protected ListenerTree getListenerRoot() {
         return listenerRoot;
     }
 
-    protected DataChangeEventResolver setListenerRoot(final ListenerRegistrationNode listenerRoot) {
+    protected DataChangeEventResolver setListenerRoot(final ListenerTree listenerRoot) {
         this.listenerRoot = listenerRoot;
         return this;
     }
@@ -74,12 +80,16 @@ public class DataChangeEventResolver {
     }
 
     public Iterable<ChangeListenerNotifyTask> resolve() {
-        resolveAnyChangeEvent(rootPath, Optional.of(listenerRoot), modificationRoot, beforeRoot, afterRoot);
-        return tasks.build();
+        LOG.trace("Resolving events for {}", modificationRoot);
+
+        try (final Walker w = listenerRoot.getWalker()) {
+            resolveAnyChangeEvent(rootPath, Optional.of(w.getRootNode()), modificationRoot, beforeRoot, afterRoot);
+            return tasks.build();
+        }
     }
 
     private DOMImmutableDataChangeEvent resolveAnyChangeEvent(final InstanceIdentifier path,
-            final Optional<ListenerRegistrationNode> listeners, final NodeModification modification,
+            final Optional<ListenerTree.Node> listeners, final NodeModification modification,
             final Optional<StoreMetadataNode> before, final Optional<StoreMetadataNode> after) {
         // No listeners are present in listener registration subtree
         // no before and after state is present
@@ -113,45 +123,37 @@ public class DataChangeEventResolver {
      * @return
      */
     private DOMImmutableDataChangeEvent resolveCreateEvent(final InstanceIdentifier path,
-            final Optional<ListenerRegistrationNode> listeners, final StoreMetadataNode afterState) {
+            final Optional<ListenerTree.Node> listeners, final StoreMetadataNode afterState) {
         final NormalizedNode<?, ?> node = afterState.getData();
         Builder builder = builder().setAfter(node).addCreated(path, node);
 
         for (StoreMetadataNode child : afterState.getChildren()) {
             PathArgument childId = child.getIdentifier();
-            Optional<ListenerRegistrationNode> childListeners = getChild(listeners, childId);
+            Optional<ListenerTree.Node> childListeners = getChild(listeners, childId);
 
             InstanceIdentifier childPath = StoreUtils.append(path, childId);
             builder.merge(resolveCreateEvent(childPath, childListeners, child));
         }
-        DOMImmutableDataChangeEvent event = builder.build();
-        if (listeners.isPresent()) {
-            addNotifyTask(listeners.get().getListeners(), event);
-        }
-        return event;
+
+        return addNotifyTask(listeners, builder.build());
     }
 
     private DOMImmutableDataChangeEvent resolveDeleteEvent(final InstanceIdentifier path,
-            final Optional<ListenerRegistrationNode> listeners, final StoreMetadataNode beforeState) {
+            final Optional<ListenerTree.Node> listeners, final StoreMetadataNode beforeState) {
         final NormalizedNode<?, ?> node = beforeState.getData();
         Builder builder = builder().setBefore(node).addRemoved(path, node);
 
         for (StoreMetadataNode child : beforeState.getChildren()) {
             PathArgument childId = child.getIdentifier();
-            Optional<ListenerRegistrationNode> childListeners = getChild(listeners, childId);
+            Optional<ListenerTree.Node> childListeners = getChild(listeners, childId);
             InstanceIdentifier childPath = StoreUtils.append(path, childId);
             builder.merge(resolveDeleteEvent(childPath, childListeners, child));
         }
-        DOMImmutableDataChangeEvent event = builder.build();
-        if (listeners.isPresent()) {
-            addNotifyTask(listeners.get().getListeners(), event);
-        }
-        return event;
-
+        return addNotifyTask(listeners, builder.build());
     }
 
     private DOMImmutableDataChangeEvent resolveSubtreeChangeEvent(final InstanceIdentifier path,
-            final Optional<ListenerRegistrationNode> listeners, final NodeModification modification,
+            final Optional<ListenerTree.Node> listeners, final NodeModification modification,
             final StoreMetadataNode before, final StoreMetadataNode after) {
 
         Builder one = builder().setBefore(before.getData()).setAfter(after.getData());
@@ -161,7 +163,7 @@ public class DataChangeEventResolver {
         for (NodeModification childMod : modification.getModifications()) {
             PathArgument childId = childMod.getIdentifier();
             InstanceIdentifier childPath = append(path, childId);
-            Optional<ListenerRegistrationNode> childListen = getChild(listeners, childId);
+            Optional<ListenerTree.Node> childListen = getChild(listeners, childId);
 
             Optional<StoreMetadataNode> childBefore = before.getChild(childId);
             Optional<StoreMetadataNode> childAfter = after.getChild(childId);
@@ -169,12 +171,15 @@ public class DataChangeEventResolver {
             switch (childMod.getModificationType()) {
             case WRITE:
             case DELETE:
-                one.merge(resolveAnyChangeEvent(childPath, childListen, childMod, childBefore, childBefore));
+                one.merge(resolveAnyChangeEvent(childPath, childListen, childMod, childBefore, childAfter));
                 break;
             case SUBTREE_MODIFIED:
                 subtree.merge(resolveSubtreeChangeEvent(childPath, childListen, childMod, childBefore.get(),
                         childAfter.get()));
                 break;
+            case UNMODIFIED:
+                // no-op
+                break;
             }
         }
         DOMImmutableDataChangeEvent oneChangeEvent = one.build();
@@ -188,28 +193,41 @@ public class DataChangeEventResolver {
     }
 
     private DOMImmutableDataChangeEvent resolveReplacedEvent(final InstanceIdentifier path,
-            final Optional<ListenerRegistrationNode> listeners, final NodeModification modification,
+            final Optional<ListenerTree.Node> listeners, final NodeModification modification,
             final StoreMetadataNode before, final StoreMetadataNode after) {
         // FIXME Add task
         return builder().build();
     }
 
-    private void addNotifyTask(final ListenerRegistrationNode listenerRegistrationNode, final DataChangeScope one,
-            final DOMImmutableDataChangeEvent event) {
-
-
+    private DOMImmutableDataChangeEvent addNotifyTask(final Optional<ListenerTree.Node> listeners, final DOMImmutableDataChangeEvent event) {
+        if (listeners.isPresent()) {
+            final Collection<DataChangeListenerRegistration<?>> l = listeners.get().getListeners();
+            if (!l.isEmpty()) {
+                tasks.add(new ChangeListenerNotifyTask(ImmutableSet.copyOf(l), event));
+            }
+        }
 
+        return event;
     }
 
-    private void addNotifyTask(final Iterable<DataChangeListenerRegistration<?>> listeners,
+    private void addNotifyTask(final ListenerTree.Node listenerRegistrationNode, final DataChangeScope scope,
             final DOMImmutableDataChangeEvent event) {
-        tasks .add(new ChangeListenerNotifyTask(ImmutableSet.copyOf(listeners),event));
+        Collection<DataChangeListenerRegistration<?>> potential = listenerRegistrationNode.getListeners();
+        if(!potential.isEmpty()) {
+            final Set<DataChangeListenerRegistration<?>> toNotify = new HashSet<>(potential.size());
+            for(DataChangeListenerRegistration<?> listener : potential) {
+                if(scope.equals(listener.getScope())) {
+                    toNotify.add(listener);
+                }
+            }
+
+            if (!toNotify.isEmpty()) {
+                tasks.add(new ChangeListenerNotifyTask(toNotify, event));
+            }
+        }
     }
 
     public static DataChangeEventResolver create() {
         return new DataChangeEventResolver();
     }
-
-
-
 }