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