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.controller.md.sal.dom.store.impl;
10 import java.util.ArrayList;
11 import java.util.Collection;
12 import java.util.Collections;
13 import java.util.HashMap;
14 import java.util.List;
16 import java.util.Map.Entry;
18 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
19 import org.opendaylight.controller.md.sal.dom.store.impl.DOMImmutableDataChangeEvent.Builder;
20 import org.opendaylight.controller.md.sal.dom.store.impl.tree.ListenerNode;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 import com.google.common.base.Optional;
31 import com.google.common.base.Preconditions;
32 import com.google.common.collect.Iterables;
33 import com.google.common.collect.Multimap;
36 * Recursion state used in {@link ResolveDataChangeEventsTask}. Instances of this
37 * method track which listeners are affected by a particular change node. It takes
38 * care of properly inheriting SUB/ONE listeners and also provides a means to
39 * understand when actual processing need not occur.
41 final class ResolveDataChangeState {
42 private static final Logger LOG = LoggerFactory.getLogger(ResolveDataChangeState.class);
44 * Inherited from all parents
46 private final Iterable<Builder> inheritedSub;
48 * Inherited from immediate parent
50 private final Collection<Builder> inheritedOne;
51 private final YangInstanceIdentifier nodeId;
52 private final Collection<ListenerNode> nodes;
54 private final Map<DataChangeListenerRegistration<?>, Builder> subBuilders;
55 private final Map<DataChangeListenerRegistration<?>, Builder> oneBuilders;
56 private final Map<DataChangeListenerRegistration<?>, Builder> baseBuilders;
58 private ResolveDataChangeState(final YangInstanceIdentifier nodeId,
59 final Iterable<Builder> inheritedSub, final Collection<Builder> inheritedOne,
60 final Collection<ListenerNode> nodes) {
61 this.nodeId = Preconditions.checkNotNull(nodeId);
62 this.nodes = Preconditions.checkNotNull(nodes);
63 this.inheritedSub = Preconditions.checkNotNull(inheritedSub);
64 this.inheritedOne = Preconditions.checkNotNull(inheritedOne);
67 * Collect the nodes which need to be propagated from us to the child.
69 final Map<DataChangeListenerRegistration<?>, Builder> sub = new HashMap<>();
70 final Map<DataChangeListenerRegistration<?>, Builder> one = new HashMap<>();
71 final Map<DataChangeListenerRegistration<?>, Builder> base = new HashMap<>();
72 for (ListenerNode n : nodes) {
73 for (DataChangeListenerRegistration<?> l : n.getListeners()) {
74 final Builder b = DOMImmutableDataChangeEvent.builder(DataChangeScope.BASE);
75 switch (l.getScope()) {
89 baseBuilders = maybeEmpty(base);
90 oneBuilders = maybeEmpty(one);
91 subBuilders = maybeEmpty(sub);
94 private static <K, V> Map<K, V> maybeEmpty(final Map<K, V> map) {
96 return Collections.emptyMap();
102 * Create an initial state handle at a particular root node.
104 * @param rootId root instance identifier
105 * @param root root node
108 public static ResolveDataChangeState initial(final YangInstanceIdentifier rootId, final ListenerNode root) {
109 return new ResolveDataChangeState(rootId, Collections.<Builder>emptyList(),
110 Collections.<Builder>emptyList(), Collections.singletonList(root));
114 * Create a state handle for iterating over a particular child.
116 * @param childId ID of the child
117 * @return State handle
119 public ResolveDataChangeState child(final PathArgument childId) {
121 * We instantiate a concatenation only when needed:
123 * 1) If our collection is empty, we reuse the parent's. This is typically the case
124 * for intermediate node, which should be the vast majority.
125 * 2) If the parent's iterable is a Collection and it is empty, reuse our collection.
126 * This is the case for the first node which defines a subtree listener in a
127 * particular subtree.
128 * 3) Concatenate the two collections. This happens when we already have some
129 * subtree listeners and we encounter a node which adds a few more.
131 * This allows us to lower number of objects allocated and also
132 * speeds up Iterables.isEmpty() in needsProcessing().
134 * Note that the check for Collection in 2) relies on precisely this logic, which
135 * ensures that we simply cannot see an empty concatenation, but rather start off with
136 * an empty collection, then switch to a non-empty collection and finally switch to
137 * a concatenation. This saves us from instantiating iterators, which a trivial
138 * Iterables.isEmpty() would do as soon as we cross case 3).
140 final Iterable<Builder> sb;
141 if (!subBuilders.isEmpty()) {
142 if (inheritedSub instanceof Collection && ((Collection<?>) inheritedSub).isEmpty()) {
143 sb = subBuilders.values();
145 sb = Iterables.concat(inheritedSub, subBuilders.values());
151 return new ResolveDataChangeState(nodeId.node(childId), sb,
152 oneBuilders.values(), getListenerChildrenWildcarded(nodes, childId));
156 * Get the current path
158 * @return Current path.
160 public YangInstanceIdentifier getPath() {
165 * Check if this child needs processing.
167 * @return True if processing needs to occur, false otherwise.
169 public boolean needsProcessing() {
170 // May have underlying listeners, so we need to process
171 if (!nodes.isEmpty()) {
174 // Have ONE listeners
175 if (!inheritedOne.isEmpty()) {
180 * Have SUBTREE listeners
182 * This is slightly magical replacement for !Iterables.isEmpty(inheritedSub).
183 * It relies on the logic in child(), which gives us the guarantee that when
184 * inheritedSub is not a Collection, it is guaranteed to be non-empty (which
185 * means we need to process). If it is a collection, we still need to check
188 * Unlike Iterables.isEmpty() this code does not instantiate any temporary
189 * objects and is thus more efficient.
191 if (inheritedSub instanceof Collection) {
192 return !((Collection<?>) inheritedSub).isEmpty();
195 // Non-Collection => non-empty => have to process
200 * Add an event to all current listeners.
204 public void addEvent(final DOMImmutableDataChangeEvent event) {
205 // Subtree builders get always notified
206 for (Builder b : subBuilders.values()) {
209 for (Builder b : inheritedSub) {
213 if (event.getScope() == DataChangeScope.ONE || event.getScope() == DataChangeScope.BASE) {
214 for (Builder b : oneBuilders.values()) {
219 if (event.getScope() == DataChangeScope.BASE) {
220 for (Builder b : inheritedOne) {
223 for (Builder b : baseBuilders.values()) {
230 * Gather all non-empty events into the provided map.
232 * @param before before-image
233 * @param after after-image
234 * @param map target map
236 public void collectEvents(final NormalizedNode<?, ?> before, final NormalizedNode<?, ?> after,
237 final Multimap<DataChangeListenerRegistration<?>, DOMImmutableDataChangeEvent> map) {
238 for (Entry<DataChangeListenerRegistration<?>, Builder> e : baseBuilders.entrySet()) {
239 final Builder b = e.getValue();
241 map.put(e.getKey(), b.setBefore(before).setAfter(after).build());
244 for (Entry<DataChangeListenerRegistration<?>, Builder> e : oneBuilders.entrySet()) {
245 final Builder b = e.getValue();
247 map.put(e.getKey(), b.setBefore(before).setAfter(after).build());
250 for (Entry<DataChangeListenerRegistration<?>, Builder> e : subBuilders.entrySet()) {
251 final Builder b = e.getValue();
253 map.put(e.getKey(), b.setBefore(before).setAfter(after).build());
257 LOG.trace("Collected events {}", map);
260 private static Collection<ListenerNode> getListenerChildrenWildcarded(final Collection<ListenerNode> parentNodes,
261 final PathArgument child) {
262 if (parentNodes.isEmpty()) {
263 return Collections.emptyList();
266 final List<ListenerNode> result = new ArrayList<>();
267 if (child instanceof NodeWithValue || child instanceof NodeIdentifierWithPredicates) {
268 NodeIdentifier wildcardedIdentifier = new NodeIdentifier(child.getNodeType());
269 addChildNodes(result, parentNodes, wildcardedIdentifier);
271 addChildNodes(result, parentNodes, child);
275 private static void addChildNodes(final List<ListenerNode> result, final Collection<ListenerNode> parentNodes, final PathArgument childIdentifier) {
276 for (ListenerNode node : parentNodes) {
277 Optional<ListenerNode> child = node.getChild(childIdentifier);
278 if (child.isPresent()) {
279 result.add(child.get());