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 java.util.Map.Entry;
14 import org.opendaylight.controller.cluster.datastore.messages.EnableNotification;
15 import org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeChangeListener;
16 import org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeChangeListenerReply;
17 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
18 import org.opendaylight.yangtools.concepts.ListenerRegistration;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
21 final class DataTreeChangeListenerSupport extends AbstractDataListenerSupport<DOMDataTreeChangeListener,
22 RegisterDataTreeChangeListener, DelayedDataTreeListenerRegistration,
23 ListenerRegistration<DOMDataTreeChangeListener>> {
24 DataTreeChangeListenerSupport(final Shard shard) {
29 Entry<ListenerRegistration<DOMDataTreeChangeListener>, Optional<DataTreeCandidate>> createDelegate(
30 final RegisterDataTreeChangeListener message) {
31 ActorSelection dataChangeListenerPath = selectActor(message.getDataTreeChangeListenerPath());
33 // Notify the listener if notifications should be enabled or not
34 // If this shard is the leader then it will enable notifications else
36 dataChangeListenerPath.tell(new EnableNotification(true), getSelf());
38 // Now store a reference to the data change listener so it can be notified
39 // at a later point if notifications should be enabled or disabled
40 if (!message.isRegisterOnAllInstances()) {
41 addListenerActor(dataChangeListenerPath);
44 DOMDataTreeChangeListener listener = new ForwardingDataTreeChangeListener(dataChangeListenerPath);
46 log().debug("{}: Registering for path {}", persistenceId(), message.getPath());
48 Entry<ListenerRegistration<DOMDataTreeChangeListener>, Optional<DataTreeCandidate>> regEntry =
49 getShard().getDataStore().registerTreeChangeListener(message.getPath(), listener);
51 getShard().getDataStore().notifyOfInitialData(message.getPath(),
52 regEntry.getKey().getInstance(), regEntry.getValue());
58 protected DelayedDataTreeListenerRegistration newDelayedListenerRegistration(
59 RegisterDataTreeChangeListener message) {
60 return new DelayedDataTreeListenerRegistration(message);
64 protected ActorRef newRegistrationActor(ListenerRegistration<DOMDataTreeChangeListener> registration) {
65 return createActor(DataTreeChangeListenerRegistrationActor.props(registration));
69 protected Object newRegistrationReplyMessage(ActorRef registrationActor) {
70 return new RegisterDataTreeChangeListenerReply(registrationActor);
74 protected String logName() {
75 return "registerTreeChangeListener";