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