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 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.store.impl.DataChangeListenerRegistration;
13 import org.opendaylight.mdsal.dom.spi.AbstractRegistrationTree;
14 import org.opendaylight.mdsal.dom.spi.RegistrationTreeNode;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
16 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
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.
22 * @author Robert Varga
24 public final class ListenerTree extends AbstractRegistrationTree<DataChangeListenerRegistration<?>> {
25 private ListenerTree() {
26 // Private to disallow direct instantiation
30 * Create a new empty instance of the listener tree.
32 * @return An empty instance.
34 public static ListenerTree create() {
35 return new ListenerTree();
39 * Registers listener on this node.
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
46 public <L extends AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>
47 DataChangeListenerRegistration<L> registerDataChangeListener(final YangInstanceIdentifier path,
48 final L listener, final DataChangeScope scope) {
50 // Take the write lock
53 final RegistrationTreeNode<DataChangeListenerRegistration<?>> node = findNodeFor(path.getPathArguments());
54 DataChangeListenerRegistration<L> reg = new DataChangeListenerRegistrationImpl<L>(listener) {
56 public DataChangeScope getScope() {
61 public YangInstanceIdentifier getPath() {
66 protected void removeRegistration() {
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.
74 * While this does not directly violate the ListenerRegistration
75 * contract, it is probably not going to be liked by the users.
77 ListenerTree.this.removeRegistration(node, this);
81 addRegistration(node, reg);
84 // Always release the lock