15de81e290549080b0d991a093eef5732db4eed2
[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, persistenceId());
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 {}, isLeader: {}, hasLeader: {}", persistenceId(), logName(), message,
70                 isLeader, hasLeader);
71
72         ActorRef registrationActor = createActor(DataTreeNotificationListenerRegistrationActor.props());
73
74         if (hasLeader && message.isRegisterOnAllInstances() || isLeader) {
75             doRegistration(message, registrationActor);
76         } else {
77             log.debug("{}: Shard is not the leader - delaying registration", persistenceId());
78
79             DelayedListenerRegistration<L, M> delayedReg =
80                     new DelayedListenerRegistration<>(message, registrationActor);
81             Collection<DelayedListenerRegistration<L, M>> delayedRegList;
82             if (message.isRegisterOnAllInstances()) {
83                 delayedRegList = delayedListenerOnAllRegistrations;
84             } else {
85                 delayedRegList = delayedListenerRegistrations;
86             }
87
88             delayedRegList.add(delayedReg);
89             registrationActor.tell(new DataTreeNotificationListenerRegistrationActor.SetRegistration(
90                     delayedReg, () -> delayedRegList.remove(delayedReg)), ActorRef.noSender());
91         }
92
93         log.debug("{}: {} sending reply, listenerRegistrationPath = {} ", persistenceId(), logName(),
94                 registrationActor.path());
95
96         tellSender(newRegistrationReplyMessage(registrationActor));
97     }
98
99     protected ActorSelection processListenerRegistrationMessage(M message) {
100         final ActorSelection listenerActor = selectActor(message.getListenerActorPath());
101
102         // We have a leader so enable the listener.
103         listenerActor.tell(new EnableNotification(true, persistenceId()), getSelf());
104
105         if (!message.isRegisterOnAllInstances()) {
106             // This is a leader-only registration so store a reference to the listener actor so it can be notified
107             // at a later point if notifications should be enabled or disabled.
108             leaderOnlyListenerActors.add(listenerActor);
109         }
110
111         allListenerActors.add(listenerActor);
112
113         return listenerActor;
114     }
115
116     protected Logger log() {
117         return log;
118     }
119
120     protected void removeListenerActor(ActorSelection listenerActor) {
121         allListenerActors.remove(listenerActor);
122         leaderOnlyListenerActors.remove(listenerActor);
123     }
124
125     abstract void doRegistration(M message, ActorRef registrationActor);
126
127     protected abstract Object newRegistrationReplyMessage(ActorRef registrationActor);
128
129     protected abstract String logName();
130 }

©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.