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.sal.core.compat;
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.mdsal.dom.spi.AbstractRegistrationTree;
13 import org.opendaylight.mdsal.dom.spi.RegistrationTreeNode;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
15 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
18 * A set of listeners organized as a tree by node to which they listen. This class
19 * allows for efficient lookup of listeners when we walk the DataTreeCandidate.
21 * @author Robert Varga
23 public final class ListenerTree extends AbstractRegistrationTree<DataChangeListenerRegistration<?>> {
24 private ListenerTree() {
25 // Private to disallow direct instantiation
29 * Create a new empty instance of the listener tree.
31 * @return An empty instance.
33 public static ListenerTree create() {
34 return new ListenerTree();
38 * Registers listener on this node.
40 * @param path Full path on which listener is registered.
41 * @param listener Listener
42 * @param scope Scope of triggering event.
43 * @return Listener registration
45 public <L extends AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>
46 DataChangeListenerRegistration<L> registerDataChangeListener(final YangInstanceIdentifier path,
47 final L listener, final DataChangeScope scope) {
49 // Take the write lock
52 final RegistrationTreeNode<DataChangeListenerRegistration<?>> node = findNodeFor(path.getPathArguments());
53 DataChangeListenerRegistration<L> reg = new DataChangeListenerRegistrationImpl<L>(listener) {
55 public DataChangeScope getScope() {
60 public YangInstanceIdentifier getPath() {
65 protected void removeRegistration() {
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.
73 * While this does not directly violate the ListenerRegistration
74 * contract, it is probably not going to be liked by the users.
76 ListenerTree.this.removeRegistration(node, this);
80 addRegistration(node, reg);
83 // Always release the lock