Bug 8231: Fix testChangeListenerRegistration failure
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / AbstractDataListenerSupport.java
1 /*
2  * Copyright (c) 2015 Brocade Communications 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 java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.EventListener;
15 import java.util.concurrent.ConcurrentHashMap;
16 import org.opendaylight.controller.cluster.datastore.actors.DataTreeNotificationListenerRegistrationActor;
17 import org.opendaylight.controller.cluster.datastore.messages.EnableNotification;
18 import org.opendaylight.controller.cluster.datastore.messages.ListenerRegistrationMessage;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 abstract class AbstractDataListenerSupport<L extends EventListener, M extends ListenerRegistrationMessage,
23         D extends DelayedListenerRegistration<L, M>> extends LeaderLocalDelegateFactory<M> {
24     private final Logger log = LoggerFactory.getLogger(getClass());
25
26     private final Collection<D> delayedListenerRegistrations = ConcurrentHashMap.newKeySet();
27     private final Collection<D> delayedListenerOnAllRegistrations = ConcurrentHashMap.newKeySet();
28     private final Collection<ActorSelection> leaderOnlyListenerActors = ConcurrentHashMap.newKeySet();
29     private final Collection<ActorSelection> allListenerActors = ConcurrentHashMap.newKeySet();
30
31     protected AbstractDataListenerSupport(Shard shard) {
32         super(shard);
33     }
34
35     Collection<ActorSelection> getListenerActors() {
36         return new ArrayList<>(allListenerActors);
37     }
38
39     @Override
40     void onLeadershipChange(boolean isLeader, boolean hasLeader) {
41         log.debug("{}: onLeadershipChange, isLeader: {}, hasLeader : {}", persistenceId(), isLeader, hasLeader);
42
43         final EnableNotification msg = new EnableNotification(isLeader);
44         for (ActorSelection dataChangeListener : leaderOnlyListenerActors) {
45             dataChangeListener.tell(msg, getSelf());
46         }
47
48         if (hasLeader) {
49             for (D reg : delayedListenerOnAllRegistrations) {
50                 reg.createDelegate(this);
51             }
52
53             delayedListenerOnAllRegistrations.clear();
54         }
55
56         if (isLeader) {
57             for (D reg : delayedListenerRegistrations) {
58                 reg.createDelegate(this);
59             }
60
61             delayedListenerRegistrations.clear();
62         }
63     }
64
65     @Override
66     void onMessage(M message, boolean isLeader, boolean hasLeader) {
67         log.debug("{}: {} for {}, leader: {}", persistenceId(), logName(), message.getPath(), isLeader);
68
69         ActorRef registrationActor = createActor(DataTreeNotificationListenerRegistrationActor.props());
70
71         if (hasLeader && message.isRegisterOnAllInstances() || isLeader) {
72             doRegistration(message, registrationActor);
73         } else {
74             log.debug("{}: Shard is not the leader - delaying registration", persistenceId());
75
76             D delayedReg = newDelayedListenerRegistration(message, registrationActor);
77             Collection<D> delayedRegList;
78             if (message.isRegisterOnAllInstances()) {
79                 delayedRegList = delayedListenerOnAllRegistrations;
80             } else {
81                 delayedRegList = delayedListenerRegistrations;
82             }
83
84             delayedRegList.add(delayedReg);
85             registrationActor.tell(new DataTreeNotificationListenerRegistrationActor.SetRegistration(
86                     delayedReg, () -> delayedRegList.remove(delayedReg)), ActorRef.noSender());
87         }
88
89         log.debug("{}: {} sending reply, listenerRegistrationPath = {} ", persistenceId(), logName(),
90                 registrationActor.path());
91
92         tellSender(newRegistrationReplyMessage(registrationActor));
93     }
94
95     protected ActorSelection processListenerRegistrationMessage(M message) {
96         final ActorSelection listenerActor = selectActor(message.getListenerActorPath());
97
98         // We have a leader so enable the listener.
99         listenerActor.tell(new EnableNotification(true), getSelf());
100
101         if (!message.isRegisterOnAllInstances()) {
102             // This is a leader-only registration so store a reference to the listener actor so it can be notified
103             // at a later point if notifications should be enabled or disabled.
104             leaderOnlyListenerActors.add(listenerActor);
105         }
106
107         allListenerActors.add(listenerActor);
108
109         return listenerActor;
110     }
111
112     protected Logger log() {
113         return log;
114     }
115
116     protected void removeListenerActor(ActorSelection listenerActor) {
117         allListenerActors.remove(listenerActor);
118         leaderOnlyListenerActors.remove(listenerActor);
119     }
120
121     abstract void doRegistration(M message, ActorRef registrationActor);
122
123     protected abstract D newDelayedListenerRegistration(M message, ActorRef registrationActor);
124
125     protected abstract Object newRegistrationReplyMessage(ActorRef registrationActor);
126
127     protected abstract String logName();
128 }