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.util.concurrent.locks.ReadWriteLock;
11 import java.util.concurrent.locks.ReentrantReadWriteLock;
13 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
14 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
15 import org.opendaylight.controller.md.sal.dom.store.impl.DataChangeListenerRegistration;
16 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
19 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
24 * A set of listeners organized as a tree by node to which they listen. This class
25 * allows for efficient lookup of listeners when we walk the DataTreeCandidate.
27 * @author Robert Varga
29 public final class ListenerTree {
30 private static final Logger LOG = LoggerFactory.getLogger(ListenerTree.class);
31 private final ReadWriteLock rwLock = new ReentrantReadWriteLock(true);
32 private final ListenerNode rootNode = new ListenerNode(null, null);
34 private ListenerTree() {
35 // Private to disallow direct instantiation
39 * Create a new empty instance of the listener tree.
41 * @return An empty instance.
43 public static ListenerTree create() {
44 return new ListenerTree();
48 * Registers listener on this node.
50 * @param path Full path on which listener is registered.
51 * @param listener Listener
52 * @param scope Scope of triggering event.
53 * @return Listener registration
55 public <L extends AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>> DataChangeListenerRegistration<L> registerDataChangeListener(final YangInstanceIdentifier path,
56 final L listener, final DataChangeScope scope) {
58 // Take the write lock
59 rwLock.writeLock().lock();
62 ListenerNode walkNode = rootNode;
63 for (final PathArgument arg : path.getPathArguments()) {
64 walkNode = walkNode.ensureChild(arg);
67 final ListenerNode node = walkNode;
68 DataChangeListenerRegistration<L> reg = new DataChangeListenerRegistrationImpl<L>(listener) {
70 public DataChangeScope getScope() {
75 public YangInstanceIdentifier getPath() {
80 protected void removeRegistration() {
82 * TODO: Here's an interesting problem. The way the datastore works, it
83 * enqueues requests towards the listener, so the listener will be
84 * notified at some point in the future. Now if the registration is
85 * closed, we will prevent any new events from being delivered, but
86 * we have no way to purge that queue.
88 * While this does not directly violate the ListenerRegistration
89 * contract, it is probably not going to be liked by the users.
92 // Take the write lock
93 ListenerTree.this.rwLock.writeLock().lock();
95 node.removeListener(this);
97 // Always release the lock
98 ListenerTree.this.rwLock.writeLock().unlock();
103 node.addListener(reg);
106 // Always release the lock
107 rwLock.writeLock().unlock();
112 * Obtain a tree walking context. This context ensures a consistent view of
113 * the listener registrations. The context should be closed as soon as it
114 * is not required, because each unclosed instance blocks modification of
117 * @return A walker instance.
119 public ListenerWalker getWalker() {
121 * TODO: The only current user of this method is local to the datastore.
122 * Since this class represents a read-lock, losing a reference to
123 * it is a _major_ problem, as the registration process will get
124 * wedged, eventually grinding the system to a halt. Should an
125 * external user exist, make the Walker a phantom reference, which
126 * will cleanup the lock if not told to do so.
128 final ListenerWalker ret = new ListenerWalker(rwLock.readLock(), rootNode);
129 rwLock.readLock().lock();
133 abstract static class DataChangeListenerRegistrationImpl<T extends AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>> extends AbstractListenerRegistration<T> //
134 implements DataChangeListenerRegistration<T> {
135 public DataChangeListenerRegistrationImpl(final T listener) {