BUG 5656 : Entity ownership candidates not removed consistently on leadership change
[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, ListenerRegistration<DOMDataTreeChangeListener>> {
23     DataTreeChangeListenerSupport(final Shard shard) {
24         super(shard);
25     }
26
27     @Override
28     Entry<ListenerRegistration<DOMDataTreeChangeListener>, Optional<DataTreeCandidate>> createDelegate(
29             final RegisterDataTreeChangeListener message) {
30         ActorSelection dataChangeListenerPath = selectActor(message.getDataTreeChangeListenerPath());
31
32         // Notify the listener if notifications should be enabled or not
33         // If this shard is the leader then it will enable notifications else
34         // it will not
35         dataChangeListenerPath.tell(new EnableNotification(true), getSelf());
36
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);
41         }
42
43         DOMDataTreeChangeListener listener = new ForwardingDataTreeChangeListener(dataChangeListenerPath);
44
45         log().debug("{}: Registering for path {}", persistenceId(), message.getPath());
46
47         Entry<ListenerRegistration<DOMDataTreeChangeListener>, Optional<DataTreeCandidate>> regEntry =
48                 getShard().getDataStore().registerTreeChangeListener(message.getPath(), listener);
49
50         getShard().getDataStore().notifyOfInitialData(message.getPath(),
51                 regEntry.getKey().getInstance(), regEntry.getValue());
52
53         return regEntry;
54     }
55
56     @Override
57     protected DelayedDataTreeListenerRegistration newDelayedListenerRegistration(RegisterDataTreeChangeListener message) {
58         return new DelayedDataTreeListenerRegistration(message);
59     }
60
61     @Override
62     protected ActorRef newRegistrationActor(ListenerRegistration<DOMDataTreeChangeListener> registration) {
63         return createActor(DataTreeChangeListenerRegistrationActor.props(registration));
64     }
65
66     @Override
67     protected Object newRegistrationReplyMessage(ActorRef registrationActor) {
68         return new RegisterDataTreeChangeListenerReply(registrationActor);
69     }
70
71     @Override
72     protected String logName() {
73         return "registerTreeChangeListener";
74     }
75 }