2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.mdsal.dom.store.inmemory;
10 import org.opendaylight.mdsal.dom.store.inmemory.DOMImmutableDataChangeEvent.Builder;
12 import org.opendaylight.mdsal.dom.spi.RegistrationTreeNode;
13 import org.opendaylight.mdsal.common.api.AsyncDataBroker.DataChangeScope;
14 import com.google.common.base.Preconditions;
15 import com.google.common.collect.Iterables;
16 import com.google.common.collect.Multimap;
17 import java.util.ArrayList;
18 import java.util.Collection;
19 import java.util.Collections;
20 import java.util.HashMap;
21 import java.util.List;
23 import java.util.Map.Entry;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * Recursion state used in {@link ResolveDataChangeEventsTask}. Instances of this
35 * method track which listeners are affected by a particular change node. It takes
36 * care of properly inheriting SUB/ONE listeners and also provides a means to
37 * understand when actual processing need not occur.
39 final class ResolveDataChangeState {
40 private static final Logger LOG = LoggerFactory.getLogger(ResolveDataChangeState.class);
42 * Inherited from all parents
44 private final Iterable<Builder> inheritedSub;
46 * Inherited from immediate parent
48 private final Collection<Builder> inheritedOne;
49 private final YangInstanceIdentifier nodeId;
50 private final Collection<RegistrationTreeNode<DataChangeListenerRegistration<?>>> nodes;
52 private final Map<DataChangeListenerRegistration<?>, Builder> subBuilders;
53 private final Map<DataChangeListenerRegistration<?>, Builder> oneBuilders;
54 private final Map<DataChangeListenerRegistration<?>, Builder> baseBuilders;
56 private ResolveDataChangeState(final YangInstanceIdentifier nodeId,
57 final Iterable<Builder> inheritedSub, final Collection<Builder> inheritedOne,
58 final Collection<RegistrationTreeNode<DataChangeListenerRegistration<?>>> nodes) {
59 this.nodeId = Preconditions.checkNotNull(nodeId);
60 this.nodes = Preconditions.checkNotNull(nodes);
61 this.inheritedSub = Preconditions.checkNotNull(inheritedSub);
62 this.inheritedOne = Preconditions.checkNotNull(inheritedOne);
65 * Collect the nodes which need to be propagated from us to the child.
67 final Map<DataChangeListenerRegistration<?>, Builder> sub = new HashMap<>();
68 final Map<DataChangeListenerRegistration<?>, Builder> one = new HashMap<>();
69 final Map<DataChangeListenerRegistration<?>, Builder> base = new HashMap<>();
70 for (RegistrationTreeNode<DataChangeListenerRegistration<?>> n : nodes) {
71 for (DataChangeListenerRegistration<?> l : n.getRegistrations()) {
72 final Builder b = DOMImmutableDataChangeEvent.builder(DataChangeScope.BASE);
73 switch (l.getScope()) {
87 baseBuilders = maybeEmpty(base);
88 oneBuilders = maybeEmpty(one);
89 subBuilders = maybeEmpty(sub);
92 private static <K, V> Map<K, V> maybeEmpty(final Map<K, V> map) {
94 return Collections.emptyMap();
100 * Create an initial state handle at a particular root node.
102 * @param rootId root instance identifier
103 * @param registrationTreeNode root node
106 public static ResolveDataChangeState initial(final YangInstanceIdentifier rootId, final RegistrationTreeNode<DataChangeListenerRegistration<?>> registrationTreeNode) {
107 return new ResolveDataChangeState(rootId, Collections.<Builder>emptyList(),
108 Collections.<Builder>emptyList(), Collections.singletonList(registrationTreeNode));
112 * Create a state handle for iterating over a particular child.
114 * @param childId ID of the child
115 * @return State handle
117 public ResolveDataChangeState child(final PathArgument childId) {
119 * We instantiate a concatenation only when needed:
121 * 1) If our collection is empty, we reuse the parent's. This is typically the case
122 * for intermediate node, which should be the vast majority.
123 * 2) If the parent's iterable is a Collection and it is empty, reuse our collection.
124 * This is the case for the first node which defines a subtree listener in a
125 * particular subtree.
126 * 3) Concatenate the two collections. This happens when we already have some
127 * subtree listeners and we encounter a node which adds a few more.
129 * This allows us to lower number of objects allocated and also
130 * speeds up Iterables.isEmpty() in needsProcessing().
132 * Note that the check for Collection in 2) relies on precisely this logic, which
133 * ensures that we simply cannot see an empty concatenation, but rather start off with
134 * an empty collection, then switch to a non-empty collection and finally switch to
135 * a concatenation. This saves us from instantiating iterators, which a trivial
136 * Iterables.isEmpty() would do as soon as we cross case 3).
138 final Iterable<Builder> sb;
139 if (!subBuilders.isEmpty()) {
140 if (inheritedSub instanceof Collection && ((Collection<?>) inheritedSub).isEmpty()) {
141 sb = subBuilders.values();
143 sb = Iterables.concat(inheritedSub, subBuilders.values());
149 return new ResolveDataChangeState(nodeId.node(childId), sb,
150 oneBuilders.values(), getListenerChildrenWildcarded(nodes, childId));
154 * Get the current path
156 * @return Current path.
158 public YangInstanceIdentifier getPath() {
163 * Check if this child needs processing.
165 * @return True if processing needs to occur, false otherwise.
167 public boolean needsProcessing() {
168 // May have underlying listeners, so we need to process
169 if (!nodes.isEmpty()) {
172 // Have ONE listeners
173 if (!inheritedOne.isEmpty()) {
178 * Have SUBTREE listeners
180 * This is slightly magical replacement for !Iterables.isEmpty(inheritedSub).
181 * It relies on the logic in child(), which gives us the guarantee that when
182 * inheritedSub is not a Collection, it is guaranteed to be non-empty (which
183 * means we need to process). If it is a collection, we still need to check
186 * Unlike Iterables.isEmpty() this code does not instantiate any temporary
187 * objects and is thus more efficient.
189 if (inheritedSub instanceof Collection) {
190 return !((Collection<?>) inheritedSub).isEmpty();
193 // Non-Collection => non-empty => have to process
198 * Add an event to all current listeners.
202 public void addEvent(final DOMImmutableDataChangeEvent event) {
203 // Subtree builders get always notified
204 for (Builder b : subBuilders.values()) {
207 for (Builder b : inheritedSub) {
211 if (event.getScope() == DataChangeScope.ONE || event.getScope() == DataChangeScope.BASE) {
212 for (Builder b : oneBuilders.values()) {
217 if (event.getScope() == DataChangeScope.BASE) {
218 for (Builder b : inheritedOne) {
221 for (Builder b : baseBuilders.values()) {
228 * Gather all non-empty events into the provided map.
230 * @param before before-image
231 * @param after after-image
232 * @param map target map
234 public void collectEvents(final NormalizedNode<?, ?> before, final NormalizedNode<?, ?> after,
235 final Multimap<DataChangeListenerRegistration<?>, DOMImmutableDataChangeEvent> map) {
236 for (Entry<DataChangeListenerRegistration<?>, Builder> e : baseBuilders.entrySet()) {
237 final Builder b = e.getValue();
239 map.put(e.getKey(), b.setBefore(before).setAfter(after).build());
242 for (Entry<DataChangeListenerRegistration<?>, Builder> e : oneBuilders.entrySet()) {
243 final Builder b = e.getValue();
245 map.put(e.getKey(), b.setBefore(before).setAfter(after).build());
248 for (Entry<DataChangeListenerRegistration<?>, Builder> e : subBuilders.entrySet()) {
249 final Builder b = e.getValue();
251 map.put(e.getKey(), b.setBefore(before).setAfter(after).build());
255 LOG.trace("Collected events {}", map);
258 private static Collection<RegistrationTreeNode<DataChangeListenerRegistration<?>>> getListenerChildrenWildcarded(final Collection<RegistrationTreeNode<DataChangeListenerRegistration<?>>> parentNodes,
259 final PathArgument child) {
260 if (parentNodes.isEmpty()) {
261 return Collections.emptyList();
264 final List<RegistrationTreeNode<DataChangeListenerRegistration<?>>> result = new ArrayList<>();
265 if (child instanceof NodeWithValue || child instanceof NodeIdentifierWithPredicates) {
266 NodeIdentifier wildcardedIdentifier = new NodeIdentifier(child.getNodeType());
267 addChildNodes(result, parentNodes, wildcardedIdentifier);
269 addChildNodes(result, parentNodes, child);
273 private static void addChildNodes(final List<RegistrationTreeNode<DataChangeListenerRegistration<?>>> result, final Collection<RegistrationTreeNode<DataChangeListenerRegistration<?>>> parentNodes, final PathArgument childIdentifier) {
274 for (RegistrationTreeNode<DataChangeListenerRegistration<?>> node : parentNodes) {
275 RegistrationTreeNode<DataChangeListenerRegistration<?>> child = node.getExactChild(childIdentifier);