cb985aa91af8b7ad822fa199312399cf9bcdbc8e
[mdsal.git] / dom / mdsal-dom-inmemory-datastore / src / main / java / org / opendaylight / controller / md / sal / dom / store / impl / tree / ListenerTree.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.md.sal.dom.store.impl.tree;
9
10 import org.opendaylight.mdsal.dom.spi.AbstractRegistrationTree;
11 import org.opendaylight.mdsal.dom.spi.RegistrationTreeNode;
12
13 import org.opendaylight.mdsal.common.api.AsyncDataChangeListener;
14 import org.opendaylight.mdsal.common.api.AsyncDataBroker.DataChangeScope;
15 import org.opendaylight.controller.md.sal.dom.store.impl.DataChangeListenerRegistration;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
18
19 /**
20  * A set of listeners organized as a tree by node to which they listen. This class
21  * allows for efficient lookup of listeners when we walk the DataTreeCandidate.
22  *
23  * @author Robert Varga
24  */
25 public final class ListenerTree extends AbstractRegistrationTree<DataChangeListenerRegistration<?>> {
26     private ListenerTree() {
27         // Private to disallow direct instantiation
28     }
29
30     /**
31      * Create a new empty instance of the listener tree.
32      *
33      * @return An empty instance.
34      */
35     public static ListenerTree create() {
36         return new ListenerTree();
37     }
38
39     /**
40      * Registers listener on this node.
41      *
42      * @param path Full path on which listener is registered.
43      * @param listener Listener
44      * @param scope Scope of triggering event.
45      * @return Listener registration
46      */
47     public <L extends AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>> DataChangeListenerRegistration<L> registerDataChangeListener(final YangInstanceIdentifier path,
48             final L listener, final DataChangeScope scope) {
49
50         // Take the write lock
51         takeLock();
52         try {
53             final RegistrationTreeNode<DataChangeListenerRegistration<?>> node = findNodeFor(path.getPathArguments());
54             DataChangeListenerRegistration<L> reg = new DataChangeListenerRegistrationImpl<L>(listener) {
55                 @Override
56                 public DataChangeScope getScope() {
57                     return scope;
58                 }
59
60                 @Override
61                 public YangInstanceIdentifier getPath() {
62                     return path;
63                 }
64
65                 @Override
66                 protected void removeRegistration() {
67                     /*
68                      * TODO: Here's an interesting problem. The way the datastore works, it
69                      *       enqueues requests towards the listener, so the listener will be
70                      *       notified at some point in the future. Now if the registration is
71                      *       closed, we will prevent any new events from being delivered, but
72                      *       we have no way to purge that queue.
73                      *
74                      *       While this does not directly violate the ListenerRegistration
75                      *       contract, it is probably not going to be liked by the users.
76                      */
77                     ListenerTree.this.removeRegistration(node, this);
78                 }
79             };
80
81             addRegistration(node, reg);
82             return reg;
83         } finally {
84             // Always release the lock
85             releaseLock();
86         }
87     }
88
89     /**
90      * Obtain a tree walking context. This context ensures a consistent view of
91      * the listener registrations. The context should be closed as soon as it
92      * is not required, because each unclosed instance blocks modification of
93      * the listener tree.
94      *
95      * @return A walker instance.
96      *
97      * @deprecated Use {@link #takeSnapshot()} instead.
98      */
99     @Deprecated
100     public ListenerWalker getWalker() {
101         /*
102          * TODO: The only current user of this method is local to the datastore.
103          *       Since this class represents a read-lock, losing a reference to
104          *       it is a _major_ problem, as the registration process will get
105          *       wedged, eventually grinding the system to a halt. Should an
106          *       external user exist, make the Walker a phantom reference, which
107          *       will cleanup the lock if not told to do so.
108          */
109         return new ListenerWalker(takeSnapshot());
110     }
111 }