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, ListenerRegistration<DOMDataTreeChangeListener>> {
23 DataTreeChangeListenerSupport(final Shard shard) {
28 Entry<ListenerRegistration<DOMDataTreeChangeListener>, Optional<DataTreeCandidate>> createDelegate(
29 final RegisterDataTreeChangeListener message) {
30 ActorSelection dataChangeListenerPath = selectActor(message.getDataTreeChangeListenerPath());
32 // Notify the listener if notifications should be enabled or not
33 // If this shard is the leader then it will enable notifications else
35 dataChangeListenerPath.tell(new EnableNotification(true), getSelf());
37 // Now store a reference to the data change listener so it can be notified
38 // at a later point if notifications should be enabled or disabled
39 if(!message.isRegisterOnAllInstances()) {
40 addListenerActor(dataChangeListenerPath);
43 DOMDataTreeChangeListener listener = new ForwardingDataTreeChangeListener(dataChangeListenerPath);
45 log().debug("{}: Registering for path {}", persistenceId(), message.getPath());
47 Entry<ListenerRegistration<DOMDataTreeChangeListener>, Optional<DataTreeCandidate>> regEntry =
48 getShard().getDataStore().registerTreeChangeListener(message.getPath(), listener);
50 getShard().getDataStore().notifyOfInitialData(message.getPath(),
51 regEntry.getKey().getInstance(), regEntry.getValue());
57 protected DelayedDataTreeListenerRegistration newDelayedListenerRegistration(RegisterDataTreeChangeListener message) {
58 return new DelayedDataTreeListenerRegistration(message);
62 protected ActorRef newRegistrationActor(ListenerRegistration<DOMDataTreeChangeListener> registration) {
63 return createActor(DataTreeChangeListenerRegistrationActor.props(registration));
67 protected Object newRegistrationReplyMessage(ActorRef registrationActor) {
68 return new RegisterDataTreeChangeListenerReply(registrationActor);
72 protected String logName() {
73 return "registerTreeChangeListener";