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

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.