2 * Copyright (c) 2015 Brocade Communications Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.datastore;
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;
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());
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();
31 protected AbstractDataListenerSupport(Shard shard) {
35 Collection<ActorSelection> getListenerActors() {
36 return new ArrayList<>(allListenerActors);
40 void onLeadershipChange(boolean isLeader, boolean hasLeader) {
41 log.debug("{}: onLeadershipChange, isLeader: {}, hasLeader : {}", persistenceId(), isLeader, hasLeader);
43 final EnableNotification msg = new EnableNotification(isLeader);
44 for (ActorSelection dataChangeListener : leaderOnlyListenerActors) {
45 dataChangeListener.tell(msg, getSelf());
49 for (D reg : delayedListenerOnAllRegistrations) {
50 reg.createDelegate(this);
53 delayedListenerOnAllRegistrations.clear();
57 for (D reg : delayedListenerRegistrations) {
58 reg.createDelegate(this);
61 delayedListenerRegistrations.clear();
66 void onMessage(M message, boolean isLeader, boolean hasLeader) {
67 log.debug("{}: {} for {}, leader: {}", persistenceId(), logName(), message.getPath(), isLeader);
69 ActorRef registrationActor = createActor(DataTreeNotificationListenerRegistrationActor.props());
71 if (hasLeader && message.isRegisterOnAllInstances() || isLeader) {
72 doRegistration(message, registrationActor);
74 log.debug("{}: Shard is not the leader - delaying registration", persistenceId());
76 D delayedReg = newDelayedListenerRegistration(message, registrationActor);
77 Collection<D> delayedRegList;
78 if (message.isRegisterOnAllInstances()) {
79 delayedRegList = delayedListenerOnAllRegistrations;
81 delayedRegList = delayedListenerRegistrations;
84 delayedRegList.add(delayedReg);
85 registrationActor.tell(new DataTreeNotificationListenerRegistrationActor.SetRegistration(
86 delayedReg, () -> delayedRegList.remove(delayedReg)), ActorRef.noSender());
89 log.debug("{}: {} sending reply, listenerRegistrationPath = {} ", persistenceId(), logName(),
90 registrationActor.path());
92 tellSender(newRegistrationReplyMessage(registrationActor));
95 protected ActorSelection processListenerRegistrationMessage(M message) {
96 final ActorSelection listenerActor = selectActor(message.getListenerActorPath());
98 // We have a leader so enable the listener.
99 listenerActor.tell(new EnableNotification(true), getSelf());
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);
107 allListenerActors.add(listenerActor);
109 return listenerActor;
112 protected Logger log() {
116 protected void removeListenerActor(ActorSelection listenerActor) {
117 allListenerActors.remove(listenerActor);
118 leaderOnlyListenerActors.remove(listenerActor);
121 abstract void doRegistration(M message, ActorRef registrationActor);
123 protected abstract D newDelayedListenerRegistration(M message, ActorRef registrationActor);
125 protected abstract Object newRegistrationReplyMessage(ActorRef registrationActor);
127 protected abstract String logName();