f039ab707ba76fa4749c7895469d2c5af40b12ba
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / DataTreeChangeListenerSupport.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.datastore;
9
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;
20
21 final class DataTreeChangeListenerSupport extends AbstractDataListenerSupport<DOMDataTreeChangeListener,
22         RegisterDataTreeChangeListener, DelayedDataTreeListenerRegistration,
23         ListenerRegistration<DOMDataTreeChangeListener>> {
24     DataTreeChangeListenerSupport(final Shard shard) {
25         super(shard);
26     }
27
28     @Override
29     Entry<ListenerRegistration<DOMDataTreeChangeListener>, Optional<DataTreeCandidate>> createDelegate(
30             final RegisterDataTreeChangeListener message) {
31         ActorSelection dataChangeListenerPath = selectActor(message.getDataTreeChangeListenerPath());
32
33         // Notify the listener if notifications should be enabled or not
34         // If this shard is the leader then it will enable notifications else
35         // it will not
36         dataChangeListenerPath.tell(new EnableNotification(true), getSelf());
37
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);
42         }
43
44         DOMDataTreeChangeListener listener = new ForwardingDataTreeChangeListener(dataChangeListenerPath);
45
46         log().debug("{}: Registering for path {}", persistenceId(), message.getPath());
47
48         Entry<ListenerRegistration<DOMDataTreeChangeListener>, Optional<DataTreeCandidate>> regEntry =
49                 getShard().getDataStore().registerTreeChangeListener(message.getPath(), listener);
50
51         getShard().getDataStore().notifyOfInitialData(message.getPath(),
52                 regEntry.getKey().getInstance(), regEntry.getValue());
53
54         return regEntry;
55     }
56
57     @Override
58     protected DelayedDataTreeListenerRegistration newDelayedListenerRegistration(
59             RegisterDataTreeChangeListener message) {
60         return new DelayedDataTreeListenerRegistration(message);
61     }
62
63     @Override
64     protected ActorRef newRegistrationActor(ListenerRegistration<DOMDataTreeChangeListener> registration) {
65         return createActor(DataTreeChangeListenerRegistrationActor.props(registration));
66     }
67
68     @Override
69     protected Object newRegistrationReplyMessage(ActorRef registrationActor) {
70         return new RegisterDataTreeChangeListenerReply(registrationActor);
71     }
72
73     @Override
74     protected String logName() {
75         return "registerTreeChangeListener";
76     }
77 }