Refactor Register*ListenerReply classes
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / AbstractDataListenerSupport.java
index ac7a933..5a09b87 100644 (file)
@@ -16,15 +16,18 @@ import java.util.concurrent.ConcurrentHashMap;
 import org.opendaylight.controller.cluster.datastore.actors.DataTreeNotificationListenerRegistrationActor;
 import org.opendaylight.controller.cluster.datastore.messages.EnableNotification;
 import org.opendaylight.controller.cluster.datastore.messages.ListenerRegistrationMessage;
+import org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeNotificationListenerReply;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-abstract class AbstractDataListenerSupport<L extends EventListener, M extends ListenerRegistrationMessage,
-        D extends DelayedListenerRegistration<L, M>> extends LeaderLocalDelegateFactory<M> {
+abstract class AbstractDataListenerSupport<L extends EventListener, M extends ListenerRegistrationMessage>
+        extends LeaderLocalDelegateFactory<M> {
     private final Logger log = LoggerFactory.getLogger(getClass());
 
-    private final Collection<D> delayedListenerRegistrations = ConcurrentHashMap.newKeySet();
-    private final Collection<D> delayedListenerOnAllRegistrations = ConcurrentHashMap.newKeySet();
+    private final Collection<DelayedListenerRegistration<L, M>> delayedListenerRegistrations =
+            ConcurrentHashMap.newKeySet();
+    private final Collection<DelayedListenerRegistration<L, M>> delayedListenerOnAllRegistrations =
+            ConcurrentHashMap.newKeySet();
     private final Collection<ActorSelection> leaderOnlyListenerActors = ConcurrentHashMap.newKeySet();
     private final Collection<ActorSelection> allListenerActors = ConcurrentHashMap.newKeySet();
 
@@ -40,22 +43,22 @@ abstract class AbstractDataListenerSupport<L extends EventListener, M extends Li
     void onLeadershipChange(boolean isLeader, boolean hasLeader) {
         log.debug("{}: onLeadershipChange, isLeader: {}, hasLeader : {}", persistenceId(), isLeader, hasLeader);
 
-        final EnableNotification msg = new EnableNotification(isLeader);
+        final EnableNotification msg = new EnableNotification(isLeader, persistenceId());
         for (ActorSelection dataChangeListener : leaderOnlyListenerActors) {
             dataChangeListener.tell(msg, getSelf());
         }
 
         if (hasLeader) {
-            for (D reg : delayedListenerOnAllRegistrations) {
-                reg.createDelegate(this);
+            for (DelayedListenerRegistration<L, M> reg : delayedListenerOnAllRegistrations) {
+                reg.doRegistration(this);
             }
 
             delayedListenerOnAllRegistrations.clear();
         }
 
         if (isLeader) {
-            for (D reg : delayedListenerRegistrations) {
-                reg.createDelegate(this);
+            for (DelayedListenerRegistration<L, M> reg : delayedListenerRegistrations) {
+                reg.doRegistration(this);
             }
 
             delayedListenerRegistrations.clear();
@@ -64,7 +67,8 @@ abstract class AbstractDataListenerSupport<L extends EventListener, M extends Li
 
     @Override
     void onMessage(M message, boolean isLeader, boolean hasLeader) {
-        log.debug("{}: {} for {}, leader: {}", persistenceId(), logName(), message.getPath(), isLeader);
+        log.debug("{}: {} for {}, isLeader: {}, hasLeader: {}", persistenceId(), logName(), message,
+                isLeader, hasLeader);
 
         ActorRef registrationActor = createActor(DataTreeNotificationListenerRegistrationActor.props());
 
@@ -73,8 +77,9 @@ abstract class AbstractDataListenerSupport<L extends EventListener, M extends Li
         } else {
             log.debug("{}: Shard is not the leader - delaying registration", persistenceId());
 
-            D delayedReg = newDelayedListenerRegistration(message, registrationActor);
-            Collection<D> delayedRegList;
+            DelayedListenerRegistration<L, M> delayedReg =
+                    new DelayedListenerRegistration<>(message, registrationActor);
+            Collection<DelayedListenerRegistration<L, M>> delayedRegList;
             if (message.isRegisterOnAllInstances()) {
                 delayedRegList = delayedListenerOnAllRegistrations;
             } else {
@@ -89,14 +94,14 @@ abstract class AbstractDataListenerSupport<L extends EventListener, M extends Li
         log.debug("{}: {} sending reply, listenerRegistrationPath = {} ", persistenceId(), logName(),
                 registrationActor.path());
 
-        tellSender(newRegistrationReplyMessage(registrationActor));
+        tellSender(new RegisterDataTreeNotificationListenerReply(registrationActor));
     }
 
     protected ActorSelection processListenerRegistrationMessage(M message) {
         final ActorSelection listenerActor = selectActor(message.getListenerActorPath());
 
         // We have a leader so enable the listener.
-        listenerActor.tell(new EnableNotification(true), getSelf());
+        listenerActor.tell(new EnableNotification(true, persistenceId()), getSelf());
 
         if (!message.isRegisterOnAllInstances()) {
             // This is a leader-only registration so store a reference to the listener actor so it can be notified
@@ -120,9 +125,5 @@ abstract class AbstractDataListenerSupport<L extends EventListener, M extends Li
 
     abstract void doRegistration(M message, ActorRef registrationActor);
 
-    protected abstract D newDelayedListenerRegistration(M message, ActorRef registrationActor);
-
-    protected abstract Object newRegistrationReplyMessage(ActorRef registrationActor);
-
     protected abstract String logName();
 }

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