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.RegisterChangeListener;
16 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListenerReply;
17 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
18 import org.opendaylight.controller.md.sal.dom.store.impl.DataChangeListenerRegistration;
19 import org.opendaylight.yangtools.concepts.ListenerRegistration;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
24 final class DataChangeListenerSupport extends AbstractDataListenerSupport<
25 AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>, RegisterChangeListener,
26 DelayedDataChangeListenerRegistration, DataChangeListenerRegistration<
27 AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>> {
29 DataChangeListenerSupport(final Shard shard) {
34 Entry<DataChangeListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>,
35 Optional<DataTreeCandidate>> createDelegate(final RegisterChangeListener message) {
36 ActorSelection dataChangeListenerPath = selectActor(message.getDataChangeListenerPath());
38 // Notify the listener if notifications should be enabled or not
39 // If this shard is the leader then it will enable notifications else
41 dataChangeListenerPath.tell(new EnableNotification(true), getSelf());
43 // Now store a reference to the data change listener so it can be notified
44 // at a later point if notifications should be enabled or disabled
45 if(!message.isRegisterOnAllInstances()) {
46 addListenerActor(dataChangeListenerPath);
49 AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> listener =
50 new DataChangeListenerProxy(dataChangeListenerPath);
52 log().debug("{}: Registering for path {}", persistenceId(), message.getPath());
54 Entry<DataChangeListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>,
55 Optional<DataTreeCandidate>> regEntry = getShard().getDataStore().registerChangeListener(
56 message.getPath(), listener, message.getScope());
58 getShard().getDataStore().notifyOfInitialData(regEntry.getKey(), regEntry.getValue());
64 protected DelayedDataChangeListenerRegistration newDelayedListenerRegistration(RegisterChangeListener message) {
65 return new DelayedDataChangeListenerRegistration(message);
69 protected ActorRef newRegistrationActor(
70 ListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>> registration) {
71 return createActor(DataChangeListenerRegistrationActor.props(registration));
75 protected Object newRegistrationReplyMessage(ActorRef registrationActor) {
76 return new RegisterChangeListenerReply(registrationActor);
80 protected String logName() {
81 return "registerDataChangeListener";