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.ArrayList;
8 import java.util.Collection;
10 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
11 import org.opendaylight.controller.md.sal.dom.store.impl.DOMImmutableDataChangeEvent.Builder;
12 import org.opendaylight.controller.md.sal.dom.store.impl.tree.ListenerRegistrationNode;
13 import org.opendaylight.controller.md.sal.dom.store.impl.tree.NodeModification;
14 import org.opendaylight.controller.md.sal.dom.store.impl.tree.StoreMetadataNode;
15 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
16 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
21 import com.google.common.base.Optional;
22 import com.google.common.collect.ImmutableList;
23 import com.google.common.collect.ImmutableSet;
25 public class DataChangeEventResolver {
28 private static final Logger LOG = LoggerFactory.getLogger(DataChangeEventResolver.class);
30 private static final DOMImmutableDataChangeEvent NO_CHANGE = builder().build();
31 private InstanceIdentifier rootPath;
32 private ListenerRegistrationNode listenerRoot;
33 private NodeModification modificationRoot;
34 private Optional<StoreMetadataNode> beforeRoot;
35 private Optional<StoreMetadataNode> afterRoot;
36 private final ImmutableList.Builder<ChangeListenerNotifyTask> tasks = ImmutableList.builder();
38 protected InstanceIdentifier getRootPath() {
42 protected DataChangeEventResolver setRootPath(final InstanceIdentifier rootPath) {
43 this.rootPath = rootPath;
47 protected ListenerRegistrationNode getListenerRoot() {
51 protected DataChangeEventResolver setListenerRoot(final ListenerRegistrationNode listenerRoot) {
52 this.listenerRoot = listenerRoot;
56 protected NodeModification getModificationRoot() {
57 return modificationRoot;
60 protected DataChangeEventResolver setModificationRoot(final NodeModification modificationRoot) {
61 this.modificationRoot = modificationRoot;
65 protected Optional<StoreMetadataNode> getBeforeRoot() {
69 protected DataChangeEventResolver setBeforeRoot(final Optional<StoreMetadataNode> beforeRoot) {
70 this.beforeRoot = beforeRoot;
74 protected Optional<StoreMetadataNode> getAfterRoot() {
78 protected DataChangeEventResolver setAfterRoot(final Optional<StoreMetadataNode> afterRoot) {
79 this.afterRoot = afterRoot;
83 public Iterable<ChangeListenerNotifyTask> resolve() {
84 LOG.trace("Resolving events for {}" ,modificationRoot);
85 resolveAnyChangeEvent(rootPath, Optional.of(listenerRoot), modificationRoot, beforeRoot, afterRoot);
89 private DOMImmutableDataChangeEvent resolveAnyChangeEvent(final InstanceIdentifier path,
90 final Optional<ListenerRegistrationNode> listeners, final NodeModification modification,
91 final Optional<StoreMetadataNode> before, final Optional<StoreMetadataNode> after) {
92 // No listeners are present in listener registration subtree
93 // no before and after state is present
94 if (!before.isPresent() && !after.isPresent()) {
97 switch (modification.getModificationType()) {
98 case SUBTREE_MODIFIED:
99 return resolveSubtreeChangeEvent(path, listeners, modification, before.get(), after.get());
101 if (before.isPresent()) {
102 return resolveReplacedEvent(path, listeners, modification, before.get(), after.get());
104 return resolveCreateEvent(path, listeners, after.get());
107 return resolveDeleteEvent(path, listeners, before.get());
115 * Resolves create events deep down the interest listener tree.
123 private DOMImmutableDataChangeEvent resolveCreateEvent(final InstanceIdentifier path,
124 final Optional<ListenerRegistrationNode> listeners, final StoreMetadataNode afterState) {
125 final NormalizedNode<?, ?> node = afterState.getData();
126 Builder builder = builder().setAfter(node).addCreated(path, node);
128 for (StoreMetadataNode child : afterState.getChildren()) {
129 PathArgument childId = child.getIdentifier();
130 Optional<ListenerRegistrationNode> childListeners = getChild(listeners, childId);
132 InstanceIdentifier childPath = StoreUtils.append(path, childId);
133 builder.merge(resolveCreateEvent(childPath, childListeners, child));
136 DOMImmutableDataChangeEvent event = builder.build();
137 if (listeners.isPresent()) {
138 addNotifyTask(listeners.get().getListeners(), event);
143 private DOMImmutableDataChangeEvent resolveDeleteEvent(final InstanceIdentifier path,
144 final Optional<ListenerRegistrationNode> listeners, final StoreMetadataNode beforeState) {
145 final NormalizedNode<?, ?> node = beforeState.getData();
146 Builder builder = builder().setBefore(node).addRemoved(path, node);
148 for (StoreMetadataNode child : beforeState.getChildren()) {
149 PathArgument childId = child.getIdentifier();
150 Optional<ListenerRegistrationNode> childListeners = getChild(listeners, childId);
151 InstanceIdentifier childPath = StoreUtils.append(path, childId);
152 builder.merge(resolveDeleteEvent(childPath, childListeners, child));
154 DOMImmutableDataChangeEvent event = builder.build();
155 if (listeners.isPresent()) {
156 addNotifyTask(listeners.get().getListeners(), event);
162 private DOMImmutableDataChangeEvent resolveSubtreeChangeEvent(final InstanceIdentifier path,
163 final Optional<ListenerRegistrationNode> listeners, final NodeModification modification,
164 final StoreMetadataNode before, final StoreMetadataNode after) {
166 Builder one = builder().setBefore(before.getData()).setAfter(after.getData());
168 Builder subtree = builder();
170 for (NodeModification childMod : modification.getModifications()) {
171 PathArgument childId = childMod.getIdentifier();
172 InstanceIdentifier childPath = append(path, childId);
173 Optional<ListenerRegistrationNode> childListen = getChild(listeners, childId);
175 Optional<StoreMetadataNode> childBefore = before.getChild(childId);
176 Optional<StoreMetadataNode> childAfter = after.getChild(childId);
178 switch (childMod.getModificationType()) {
181 one.merge(resolveAnyChangeEvent(childPath, childListen, childMod, childBefore, childAfter));
183 case SUBTREE_MODIFIED:
184 subtree.merge(resolveSubtreeChangeEvent(childPath, childListen, childMod, childBefore.get(),
192 DOMImmutableDataChangeEvent oneChangeEvent = one.build();
193 subtree.merge(oneChangeEvent);
194 DOMImmutableDataChangeEvent subtreeEvent = subtree.build();
195 if (listeners.isPresent()) {
196 addNotifyTask(listeners.get(), DataChangeScope.ONE, oneChangeEvent);
197 addNotifyTask(listeners.get(), DataChangeScope.SUBTREE, subtreeEvent);
202 private DOMImmutableDataChangeEvent resolveReplacedEvent(final InstanceIdentifier path,
203 final Optional<ListenerRegistrationNode> listeners, final NodeModification modification,
204 final StoreMetadataNode before, final StoreMetadataNode after) {
206 return builder().build();
209 private void addNotifyTask(final ListenerRegistrationNode listenerRegistrationNode, final DataChangeScope scope,
210 final DOMImmutableDataChangeEvent event) {
211 Collection<DataChangeListenerRegistration<?>> potential = listenerRegistrationNode.getListeners();
212 if(potential.isEmpty()) {
215 ArrayList<DataChangeListenerRegistration<?>> toNotify = new ArrayList<>(potential.size());
216 for(DataChangeListenerRegistration<?> listener : potential) {
217 if(scope.equals(listener.getScope())) {
218 toNotify.add(listener);
221 addNotifyTask(toNotify, event);
225 private void addNotifyTask(final Collection<DataChangeListenerRegistration<?>> listeners,
226 final DOMImmutableDataChangeEvent event) {
227 if(!listeners.isEmpty()) {
228 tasks.add(new ChangeListenerNotifyTask(ImmutableSet.copyOf(listeners),event));
232 public static DataChangeEventResolver create() {
233 return new DataChangeEventResolver();