2 * Copyright (c) 2015 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.cluster.datastore;
10 import akka.actor.ActorRef;
11 import akka.actor.ActorSelection;
12 import com.google.common.base.Optional;
13 import com.google.common.collect.Sets;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Map.Entry;
18 import org.opendaylight.controller.cluster.datastore.messages.EnableNotification;
19 import org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeChangeListener;
20 import org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeChangeListenerReply;
21 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
22 import org.opendaylight.yangtools.concepts.ListenerRegistration;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
25 final class DataTreeChangeListenerSupport extends AbstractDataListenerSupport<DOMDataTreeChangeListener,
26 RegisterDataTreeChangeListener, DelayedDataTreeListenerRegistration,
27 ListenerRegistration<DOMDataTreeChangeListener>> {
29 private final Set<ActorSelection> listenerActors = Sets.newConcurrentHashSet();
31 DataTreeChangeListenerSupport(final Shard shard) {
35 Collection<ActorSelection> getListenerActors() {
36 return new ArrayList<>(listenerActors);
40 ListenerRegistration<DOMDataTreeChangeListener> createDelegate(
41 final RegisterDataTreeChangeListener message) {
42 final ActorSelection dataChangeListenerPath = selectActor(message.getDataTreeChangeListenerPath());
44 // Notify the listener if notifications should be enabled or not
45 // If this shard is the leader then it will enable notifications else
47 dataChangeListenerPath.tell(new EnableNotification(true), getSelf());
49 // Now store a reference to the data change listener so it can be notified
50 // at a later point if notifications should be enabled or disabled
51 if (!message.isRegisterOnAllInstances()) {
52 addListenerActor(dataChangeListenerPath);
55 DOMDataTreeChangeListener listener = new ForwardingDataTreeChangeListener(dataChangeListenerPath);
57 log().debug("{}: Registering for path {}", persistenceId(), message.getPath());
59 Entry<ListenerRegistration<DOMDataTreeChangeListener>, Optional<DataTreeCandidate>> regEntry =
60 getShard().getDataStore().registerTreeChangeListener(message.getPath(), listener);
62 getShard().getDataStore().notifyOfInitialData(message.getPath(),
63 regEntry.getKey().getInstance(), regEntry.getValue());
65 listenerActors.add(dataChangeListenerPath);
66 final ListenerRegistration<DOMDataTreeChangeListener> delegate = regEntry.getKey();
67 return new ListenerRegistration<DOMDataTreeChangeListener>() {
69 public DOMDataTreeChangeListener getInstance() {
70 return delegate.getInstance();
75 listenerActors.remove(dataChangeListenerPath);
82 protected DelayedDataTreeListenerRegistration newDelayedListenerRegistration(
83 RegisterDataTreeChangeListener message) {
84 return new DelayedDataTreeListenerRegistration(message);
88 protected ActorRef newRegistrationActor(ListenerRegistration<DOMDataTreeChangeListener> registration) {
89 return createActor(DataTreeChangeListenerRegistrationActor.props(registration));
93 protected Object newRegistrationReplyMessage(ActorRef registrationActor) {
94 return new RegisterDataTreeChangeListenerReply(registrationActor);
98 protected String logName() {
99 return "registerTreeChangeListener";