1 package org.opendaylight.controller.md.sal.dom.store.impl;
3 import static org.opendaylight.controller.md.sal.dom.store.impl.DOMImmutableDataChangeEvent.builder;
4 import static org.opendaylight.controller.md.sal.dom.store.impl.StoreUtils.append;
5 import static org.opendaylight.controller.md.sal.dom.store.impl.tree.TreeNodeUtils.getChild;
7 import java.util.Collection;
8 import java.util.HashSet;
11 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
12 import org.opendaylight.controller.md.sal.dom.store.impl.DOMImmutableDataChangeEvent.Builder;
13 import org.opendaylight.controller.md.sal.dom.store.impl.tree.ListenerRegistrationNode;
14 import org.opendaylight.controller.md.sal.dom.store.impl.tree.NodeModification;
15 import org.opendaylight.controller.md.sal.dom.store.impl.tree.StoreMetadataNode;
16 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
18 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
22 import com.google.common.base.Optional;
23 import com.google.common.collect.ImmutableList;
24 import com.google.common.collect.ImmutableSet;
26 public class DataChangeEventResolver {
27 private static final Logger LOG = LoggerFactory.getLogger(DataChangeEventResolver.class);
28 private static final DOMImmutableDataChangeEvent NO_CHANGE = builder().build();
29 private final ImmutableList.Builder<ChangeListenerNotifyTask> tasks = ImmutableList.builder();
30 private InstanceIdentifier rootPath;
31 private ListenerRegistrationNode listenerRoot;
32 private NodeModification modificationRoot;
33 private Optional<StoreMetadataNode> beforeRoot;
34 private Optional<StoreMetadataNode> afterRoot;
36 protected InstanceIdentifier getRootPath() {
40 protected DataChangeEventResolver setRootPath(final InstanceIdentifier rootPath) {
41 this.rootPath = rootPath;
45 protected ListenerRegistrationNode getListenerRoot() {
49 protected DataChangeEventResolver setListenerRoot(final ListenerRegistrationNode listenerRoot) {
50 this.listenerRoot = listenerRoot;
54 protected NodeModification getModificationRoot() {
55 return modificationRoot;
58 protected DataChangeEventResolver setModificationRoot(final NodeModification modificationRoot) {
59 this.modificationRoot = modificationRoot;
63 protected Optional<StoreMetadataNode> getBeforeRoot() {
67 protected DataChangeEventResolver setBeforeRoot(final Optional<StoreMetadataNode> beforeRoot) {
68 this.beforeRoot = beforeRoot;
72 protected Optional<StoreMetadataNode> getAfterRoot() {
76 protected DataChangeEventResolver setAfterRoot(final Optional<StoreMetadataNode> afterRoot) {
77 this.afterRoot = afterRoot;
81 public Iterable<ChangeListenerNotifyTask> resolve() {
82 LOG.trace("Resolving events for {}" ,modificationRoot);
83 resolveAnyChangeEvent(rootPath, Optional.of(listenerRoot), modificationRoot, beforeRoot, afterRoot);
87 private DOMImmutableDataChangeEvent resolveAnyChangeEvent(final InstanceIdentifier path,
88 final Optional<ListenerRegistrationNode> listeners, final NodeModification modification,
89 final Optional<StoreMetadataNode> before, final Optional<StoreMetadataNode> after) {
90 // No listeners are present in listener registration subtree
91 // no before and after state is present
92 if (!before.isPresent() && !after.isPresent()) {
95 switch (modification.getModificationType()) {
96 case SUBTREE_MODIFIED:
97 return resolveSubtreeChangeEvent(path, listeners, modification, before.get(), after.get());
99 if (before.isPresent()) {
100 return resolveReplacedEvent(path, listeners, modification, before.get(), after.get());
102 return resolveCreateEvent(path, listeners, after.get());
105 return resolveDeleteEvent(path, listeners, before.get());
113 * Resolves create events deep down the interest listener tree.
121 private DOMImmutableDataChangeEvent resolveCreateEvent(final InstanceIdentifier path,
122 final Optional<ListenerRegistrationNode> listeners, final StoreMetadataNode afterState) {
123 final NormalizedNode<?, ?> node = afterState.getData();
124 Builder builder = builder().setAfter(node).addCreated(path, node);
126 for (StoreMetadataNode child : afterState.getChildren()) {
127 PathArgument childId = child.getIdentifier();
128 Optional<ListenerRegistrationNode> childListeners = getChild(listeners, childId);
130 InstanceIdentifier childPath = StoreUtils.append(path, childId);
131 builder.merge(resolveCreateEvent(childPath, childListeners, child));
134 return addNotifyTask(listeners, builder.build());
137 private DOMImmutableDataChangeEvent resolveDeleteEvent(final InstanceIdentifier path,
138 final Optional<ListenerRegistrationNode> listeners, final StoreMetadataNode beforeState) {
139 final NormalizedNode<?, ?> node = beforeState.getData();
140 Builder builder = builder().setBefore(node).addRemoved(path, node);
142 for (StoreMetadataNode child : beforeState.getChildren()) {
143 PathArgument childId = child.getIdentifier();
144 Optional<ListenerRegistrationNode> childListeners = getChild(listeners, childId);
145 InstanceIdentifier childPath = StoreUtils.append(path, childId);
146 builder.merge(resolveDeleteEvent(childPath, childListeners, child));
148 return addNotifyTask(listeners, builder.build());
151 private DOMImmutableDataChangeEvent resolveSubtreeChangeEvent(final InstanceIdentifier path,
152 final Optional<ListenerRegistrationNode> listeners, final NodeModification modification,
153 final StoreMetadataNode before, final StoreMetadataNode after) {
155 Builder one = builder().setBefore(before.getData()).setAfter(after.getData());
157 Builder subtree = builder();
159 for (NodeModification childMod : modification.getModifications()) {
160 PathArgument childId = childMod.getIdentifier();
161 InstanceIdentifier childPath = append(path, childId);
162 Optional<ListenerRegistrationNode> childListen = getChild(listeners, childId);
164 Optional<StoreMetadataNode> childBefore = before.getChild(childId);
165 Optional<StoreMetadataNode> childAfter = after.getChild(childId);
167 switch (childMod.getModificationType()) {
170 one.merge(resolveAnyChangeEvent(childPath, childListen, childMod, childBefore, childAfter));
172 case SUBTREE_MODIFIED:
173 subtree.merge(resolveSubtreeChangeEvent(childPath, childListen, childMod, childBefore.get(),
181 DOMImmutableDataChangeEvent oneChangeEvent = one.build();
182 subtree.merge(oneChangeEvent);
183 DOMImmutableDataChangeEvent subtreeEvent = subtree.build();
184 if (listeners.isPresent()) {
185 addNotifyTask(listeners.get(), DataChangeScope.ONE, oneChangeEvent);
186 addNotifyTask(listeners.get(), DataChangeScope.SUBTREE, subtreeEvent);
191 private DOMImmutableDataChangeEvent resolveReplacedEvent(final InstanceIdentifier path,
192 final Optional<ListenerRegistrationNode> listeners, final NodeModification modification,
193 final StoreMetadataNode before, final StoreMetadataNode after) {
195 return builder().build();
198 private DOMImmutableDataChangeEvent addNotifyTask(final Optional<ListenerRegistrationNode> listeners, final DOMImmutableDataChangeEvent event) {
199 if (listeners.isPresent()) {
200 final Collection<DataChangeListenerRegistration<?>> l = listeners.get().getListeners();
202 tasks.add(new ChangeListenerNotifyTask(ImmutableSet.copyOf(l), event));
209 private void addNotifyTask(final ListenerRegistrationNode listenerRegistrationNode, final DataChangeScope scope,
210 final DOMImmutableDataChangeEvent event) {
211 Collection<DataChangeListenerRegistration<?>> potential = listenerRegistrationNode.getListeners();
212 if(!potential.isEmpty()) {
213 final Set<DataChangeListenerRegistration<?>> toNotify = new HashSet<>(potential.size());
214 for(DataChangeListenerRegistration<?> listener : potential) {
215 if(scope.equals(listener.getScope())) {
216 toNotify.add(listener);
220 if (!toNotify.isEmpty()) {
221 tasks.add(new ChangeListenerNotifyTask(toNotify, event));
226 public static DataChangeEventResolver create() {
227 return new DataChangeEventResolver();