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.Collection;
15 import java.util.Collections;
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, ListenerRegistration<DOMDataTreeChangeListener>> {
28 private final Set<ActorSelection> listenerActors = Sets.newConcurrentHashSet();
30 DataTreeChangeListenerSupport(final Shard shard) {
34 Collection<ActorSelection> getListenerActors() {
35 return Collections.unmodifiableCollection(listenerActors);
39 ListenerRegistration<DOMDataTreeChangeListener> createDelegate(
40 final RegisterDataTreeChangeListener message) {
41 final ActorSelection dataChangeListenerPath = selectActor(message.getDataTreeChangeListenerPath());
43 // Notify the listener if notifications should be enabled or not
44 // If this shard is the leader then it will enable notifications else
46 dataChangeListenerPath.tell(new EnableNotification(true), getSelf());
48 // Now store a reference to the data change listener so it can be notified
49 // at a later point if notifications should be enabled or disabled
50 if(!message.isRegisterOnAllInstances()) {
51 addListenerActor(dataChangeListenerPath);
54 DOMDataTreeChangeListener listener = new ForwardingDataTreeChangeListener(dataChangeListenerPath);
56 log().debug("{}: Registering for path {}", persistenceId(), message.getPath());
58 Entry<ListenerRegistration<DOMDataTreeChangeListener>, Optional<DataTreeCandidate>> regEntry =
59 getShard().getDataStore().registerTreeChangeListener(message.getPath(), listener);
61 getShard().getDataStore().notifyOfInitialData(message.getPath(),
62 regEntry.getKey().getInstance(), regEntry.getValue());
64 listenerActors.add(dataChangeListenerPath);
65 final ListenerRegistration<DOMDataTreeChangeListener> delegate = regEntry.getKey();
66 return new ListenerRegistration<DOMDataTreeChangeListener>() {
68 public DOMDataTreeChangeListener getInstance() {
69 return delegate.getInstance();
74 listenerActors.remove(dataChangeListenerPath);
81 protected DelayedDataTreeListenerRegistration newDelayedListenerRegistration(RegisterDataTreeChangeListener message) {
82 return new DelayedDataTreeListenerRegistration(message);
86 protected ActorRef newRegistrationActor(ListenerRegistration<DOMDataTreeChangeListener> registration) {
87 return createActor(DataTreeChangeListenerRegistrationActor.props(registration));
91 protected Object newRegistrationReplyMessage(ActorRef registrationActor) {
92 return new RegisterDataTreeChangeListenerReply(registrationActor);
96 protected String logName() {
97 return "registerTreeChangeListener";