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.tree;
10 import java.lang.ref.Reference;
11 import java.lang.ref.WeakReference;
12 import java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.HashMap;
16 import java.util.concurrent.locks.Lock;
17 import java.util.concurrent.locks.ReadWriteLock;
18 import java.util.concurrent.locks.ReentrantReadWriteLock;
20 import javax.annotation.concurrent.GuardedBy;
22 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
23 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
24 import org.opendaylight.controller.md.sal.dom.store.impl.DataChangeListenerRegistration;
25 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
26 import org.opendaylight.yangtools.concepts.Identifiable;
27 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 import com.google.common.base.Optional;
34 import com.google.common.base.Preconditions;
36 public final class ListenerTree {
37 private static final Logger LOG = LoggerFactory.getLogger(ListenerTree.class);
38 private final ReadWriteLock rwLock = new ReentrantReadWriteLock(true);
39 private final Node rootNode = new Node(null, null);
41 private ListenerTree() {
45 public static ListenerTree create() {
46 return new ListenerTree();
50 * Registers listener on this node.
52 * @param path Full path on which listener is registered.
53 * @param listener Listener
54 * @param scope Scope of triggering event.
55 * @return Listener registration
57 public <L extends AsyncDataChangeListener<InstanceIdentifier, NormalizedNode<?, ?>>> DataChangeListenerRegistration<L> registerDataChangeListener(final InstanceIdentifier path,
58 final L listener, final DataChangeScope scope) {
60 // Take the write lock
61 rwLock.writeLock().lock();
64 Node walkNode = rootNode;
65 for (final PathArgument arg : path.getPath()) {
66 walkNode = walkNode.ensureChild(arg);
69 final Node node = walkNode;
70 DataChangeListenerRegistration<L> reg = new DataChangeListenerRegistrationImpl<L>(listener) {
72 public DataChangeScope getScope() {
77 public InstanceIdentifier getPath() {
82 protected void removeRegistration() {
84 * TODO: Here's an interesting problem. The way the datastore works, it
85 * enqueues requests towards the listener, so the listener will be
86 * notified at some point in the future. Now if the registration is
87 * closed, we will prevent any new events from being delivered, but
88 * we have no way to purge that queue.
90 * While this does not directly violate the ListenerRegistration
91 * contract, it is probably not going to be liked by the users.
94 // Take the write lock
95 ListenerTree.this.rwLock.writeLock().lock();
97 node.removeListener(this);
99 // Always release the lock
100 ListenerTree.this.rwLock.writeLock().unlock();
105 node.addListener(reg);
108 // Always release the lock
109 rwLock.writeLock().unlock();
113 public Walker getWalker() {
115 * TODO: The only current user of this method is local to the datastore.
116 * Since this class represents a read-lock, losing a reference to
117 * it is a _major_ problem, as the registration process will get
118 * wedged, eventually grinding the system to a halt. Should an
119 * external user exist, make the Walker a phantom reference, which
120 * will cleanup the lock if not told to do so.
122 final Walker ret = new Walker(rwLock.readLock(), rootNode);
123 rwLock.readLock().lock();
127 public static final class Walker implements AutoCloseable {
128 private final Lock lock;
129 private final Node node;
132 private boolean valid = true;
134 private Walker(final Lock lock, final Node node) {
135 this.lock = Preconditions.checkNotNull(lock);
136 this.node = Preconditions.checkNotNull(node);
139 public Node getRootNode() {
144 public synchronized void close() {
153 * This is a single node within the listener tree. Note that the data returned from
154 * and instance of this class is guaranteed to have any relevance or consistency
155 * only as long as the {@link Walker} instance through which it is reached remains
158 public static final class Node implements StoreTreeNode<Node>, Identifiable<PathArgument> {
159 private final Collection<DataChangeListenerRegistration<?>> listeners = new ArrayList<>();
160 private final Map<PathArgument, Node> children = new HashMap<>();
161 private final PathArgument identifier;
162 private final Reference<Node> parent;
164 private Node(final Node parent, final PathArgument identifier) {
165 this.parent = new WeakReference<>(parent);
166 this.identifier = identifier;
170 public PathArgument getIdentifier() {
175 public Optional<Node> getChild(final PathArgument child) {
176 return Optional.fromNullable(children.get(child));
180 * Return the list of current listeners. This collection is guaranteed
181 * to be immutable only while the walker, through which this node is
182 * reachable remains unclosed.
184 * @return the list of current listeners
186 public Collection<DataChangeListenerRegistration<?>> getListeners() {
190 private Node ensureChild(final PathArgument child) {
191 Node potential = children.get(child);
192 if (potential == null) {
193 potential = new Node(this, child);
194 children.put(child, potential);
199 private void addListener(final DataChangeListenerRegistration<?> listener) {
200 listeners.add(listener);
201 LOG.debug("Listener {} registered", listener);
204 private void removeListener(final DataChangeListenerRegistrationImpl<?> listener) {
205 listeners.remove(listener);
206 LOG.debug("Listener {} unregistered", listener);
208 // We have been called with the write-lock held, so we can perform some cleanup.
209 removeThisIfUnused();
212 private void removeThisIfUnused() {
213 final Node p = parent.get();
214 if (p != null && listeners.isEmpty() && children.isEmpty()) {
215 p.removeChild(identifier);
219 private void removeChild(final PathArgument arg) {
220 children.remove(arg);
221 removeThisIfUnused();
225 public String toString() {
226 return "Node [identifier=" + identifier + ", listeners=" + listeners.size() + ", children=" + children.size() + "]";
232 private abstract static class DataChangeListenerRegistrationImpl<T extends AsyncDataChangeListener<InstanceIdentifier, NormalizedNode<?, ?>>> extends AbstractListenerRegistration<T> //
233 implements DataChangeListenerRegistration<T> {
234 public DataChangeListenerRegistrationImpl(final T listener) {