Make LeaderLocalDelegateFactory a bit more useful 27/17227/9
authorRobert Varga <rovarga@cisco.com>
Fri, 27 Mar 2015 11:35:37 +0000 (12:35 +0100)
committerTom Pantelis <tpanteli@brocade.com>
Sun, 29 Mar 2015 21:39:06 +0000 (17:39 -0400)
It seems that the two isntantiations use functionality which can be
easily abstracted out, simplifying code.

Change-Id: Id5e7cb71055ef14b2139be2ba74e29f9cd10da60
Signed-off-by: Robert Varga <rovarga@cisco.com>
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/DataChangeListenerSupport.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/DataTreeChangeListenerSupport.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/LeaderLocalDelegateFactory.java

index 7a033cf..939ddf8 100644 (file)
@@ -7,7 +7,6 @@
  */
 package org.opendaylight.controller.cluster.datastore;
 
-import com.google.common.base.Preconditions;
 import java.util.ArrayList;
 import java.util.List;
 import akka.actor.ActorRef;
@@ -26,16 +25,15 @@ final class DataChangeListenerSupport extends LeaderLocalDelegateFactory<Registe
     private static final Logger LOG = LoggerFactory.getLogger(DataChangeListenerSupport.class);
     private final List<DelayedListenerRegistration> delayedListenerRegistrations = new ArrayList<>();
     private final List<ActorSelection> dataChangeListeners =  new ArrayList<>();
-    private final Shard shard;
 
     DataChangeListenerSupport(final Shard shard) {
-        this.shard = Preconditions.checkNotNull(shard);
+        super(shard);
     }
 
     @Override
     void onLeadershipChange(final boolean isLeader) {
         for (ActorSelection dataChangeListener : dataChangeListeners) {
-            dataChangeListener.tell(new EnableNotification(isLeader), shard.getSelf());
+            dataChangeListener.tell(new EnableNotification(isLeader), getSelf());
         }
 
         if (isLeader) {
@@ -52,39 +50,37 @@ final class DataChangeListenerSupport extends LeaderLocalDelegateFactory<Registe
     @Override
     void onMessage(final RegisterChangeListener message, final boolean isLeader) {
 
-        LOG.debug("{}: registerDataChangeListener for {}, leader: {}", shard.persistenceId(), message.getPath(), isLeader);
+        LOG.debug("{}: registerDataChangeListener for {}, leader: {}", persistenceId(), message.getPath(), isLeader);
 
         ListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier,
                                                      NormalizedNode<?, ?>>> registration;
         if (isLeader) {
             registration = createDelegate(message);
         } else {
-            LOG.debug("{}: Shard is not the leader - delaying registration", shard.persistenceId());
+            LOG.debug("{}: Shard is not the leader - delaying registration", persistenceId());
 
             DelayedListenerRegistration delayedReg = new DelayedListenerRegistration(message);
             delayedListenerRegistrations.add(delayedReg);
             registration = delayedReg;
         }
 
-        ActorRef listenerRegistration = shard.getContext().actorOf(
-                DataChangeListenerRegistration.props(registration));
+        ActorRef listenerRegistration = createActor(DataChangeListenerRegistration.props(registration));
 
         LOG.debug("{}: registerDataChangeListener sending reply, listenerRegistrationPath = {} ",
-                shard.persistenceId(), listenerRegistration.path());
+                persistenceId(), listenerRegistration.path());
 
-        shard.getSender().tell(new RegisterChangeListenerReply(listenerRegistration), shard.getSelf());
+        tellSender(new RegisterChangeListenerReply(listenerRegistration));
     }
 
     @Override
     ListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>> createDelegate(
             final RegisterChangeListener message) {
-        ActorSelection dataChangeListenerPath = shard.getContext().system().actorSelection(
-                message.getDataChangeListenerPath());
+        ActorSelection dataChangeListenerPath = selectActor(message.getDataChangeListenerPath());
 
         // Notify the listener if notifications should be enabled or not
         // If this shard is the leader then it will enable notifications else
         // it will not
-        dataChangeListenerPath.tell(new EnableNotification(true), shard.getSelf());
+        dataChangeListenerPath.tell(new EnableNotification(true), getSelf());
 
         // Now store a reference to the data change listener so it can be notified
         // at a later point if notifications should be enabled or disabled
@@ -93,9 +89,9 @@ final class DataChangeListenerSupport extends LeaderLocalDelegateFactory<Registe
         AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> listener =
                 new DataChangeListenerProxy(dataChangeListenerPath);
 
-        LOG.debug("{}: Registering for path {}", shard.persistenceId(), message.getPath());
+        LOG.debug("{}: Registering for path {}", persistenceId(), message.getPath());
 
-        return shard.getDataStore().registerChangeListener(message.getPath(), listener,
+        return getShard().getDataStore().registerChangeListener(message.getPath(), listener,
                 message.getScope());
     }
 }
index afce4df..3987c9a 100644 (file)
@@ -9,7 +9,6 @@ package org.opendaylight.controller.cluster.datastore;
 
 import akka.actor.ActorRef;
 import akka.actor.ActorSelection;
-import com.google.common.base.Preconditions;
 import java.util.ArrayList;
 import java.util.Collection;
 import org.opendaylight.controller.cluster.datastore.messages.EnableNotification;
@@ -24,10 +23,9 @@ final class DataTreeChangeListenerSupport extends LeaderLocalDelegateFactory<Reg
     private static final Logger LOG = LoggerFactory.getLogger(DataTreeChangeListenerSupport.class);
     private final ArrayList<DelayedDataTreeListenerRegistration> delayedRegistrations = new ArrayList<>();
     private final Collection<ActorSelection> actors = new ArrayList<>();
-    private final Shard shard;
 
     DataTreeChangeListenerSupport(final Shard shard) {
-        this.shard = Preconditions.checkNotNull(shard);
+        super(shard);
     }
 
     @Override
@@ -42,17 +40,17 @@ final class DataTreeChangeListenerSupport extends LeaderLocalDelegateFactory<Reg
 
         final EnableNotification msg = new EnableNotification(isLeader);
         for (ActorSelection dataChangeListener : actors) {
-            dataChangeListener.tell(msg, shard.getSelf());
+            dataChangeListener.tell(msg, getSelf());
         }
     }
 
     @Override
     void onMessage(final RegisterDataTreeChangeListener registerTreeChangeListener, final boolean isLeader) {
-        LOG.debug("{}: registerTreeChangeListener for {}, leader: {}", shard.persistenceId(), registerTreeChangeListener.getPath(), isLeader);
+        LOG.debug("{}: registerTreeChangeListener for {}, leader: {}", persistenceId(), registerTreeChangeListener.getPath(), isLeader);
 
         final ListenerRegistration<DOMDataTreeChangeListener> registration;
         if (!isLeader) {
-            LOG.debug("{}: Shard is not the leader - delaying registration", shard.persistenceId());
+            LOG.debug("{}: Shard is not the leader - delaying registration", persistenceId());
 
             DelayedDataTreeListenerRegistration delayedReg =
                     new DelayedDataTreeListenerRegistration(registerTreeChangeListener);
@@ -62,24 +60,22 @@ final class DataTreeChangeListenerSupport extends LeaderLocalDelegateFactory<Reg
             registration = createDelegate(registerTreeChangeListener);
         }
 
-        ActorRef listenerRegistration = shard.getContext().actorOf(
-                DataTreeChangeListenerRegistrationActor.props(registration));
+        ActorRef listenerRegistration = createActor(DataTreeChangeListenerRegistrationActor.props(registration));
 
         LOG.debug("{}: registerDataChangeListener sending reply, listenerRegistrationPath = {} ",
-            shard.persistenceId(), listenerRegistration.path());
+            persistenceId(), listenerRegistration.path());
 
-        shard.getSender().tell(new RegisterDataTreeChangeListenerReply(listenerRegistration), shard.getSelf());
+        tellSender(new RegisterDataTreeChangeListenerReply(listenerRegistration));
     }
 
     @Override
     ListenerRegistration<DOMDataTreeChangeListener> createDelegate(final RegisterDataTreeChangeListener message) {
-        ActorSelection dataChangeListenerPath = shard.getContext().system().actorSelection(
-            message.getDataTreeChangeListenerPath().path());
+        ActorSelection dataChangeListenerPath = selectActor(message.getDataTreeChangeListenerPath());
 
         // Notify the listener if notifications should be enabled or not
         // If this shard is the leader then it will enable notifications else
         // it will not
-        dataChangeListenerPath.tell(new EnableNotification(true), shard.getSelf());
+        dataChangeListenerPath.tell(new EnableNotification(true), getSelf());
 
         // Now store a reference to the data change listener so it can be notified
         // at a later point if notifications should be enabled or disabled
@@ -87,8 +83,8 @@ final class DataTreeChangeListenerSupport extends LeaderLocalDelegateFactory<Reg
 
         DOMDataTreeChangeListener listener = new ForwardingDataTreeChangeListener(dataChangeListenerPath);
 
-        LOG.debug("{}: Registering for path {}", shard.persistenceId(), message.getPath());
+        LOG.debug("{}: Registering for path {}", persistenceId(), message.getPath());
 
-        return shard.getDataStore().registerTreeChangeListener(message.getPath(), listener);
+        return getShard().getDataStore().registerTreeChangeListener(message.getPath(), listener);
     }
 }
index 891c0bf..d33cebb 100644 (file)
@@ -7,6 +7,12 @@
  */
 package org.opendaylight.controller.cluster.datastore;
 
+import akka.actor.ActorPath;
+import akka.actor.ActorRef;
+import akka.actor.ActorSelection;
+import akka.actor.Props;
+import com.google.common.base.Preconditions;
+
 /**
  * Base class for factories instantiating delegates which are local to the
  * shard leader.
@@ -15,6 +21,40 @@ package org.opendaylight.controller.cluster.datastore;
  * <M> message type
  */
 abstract class LeaderLocalDelegateFactory<M, D> extends DelegateFactory<M, D> {
+    private final Shard shard;
+
+    protected LeaderLocalDelegateFactory(final Shard shard) {
+        this.shard = Preconditions.checkNotNull(shard);
+    }
+
+    protected final ActorRef getSelf() {
+        return shard.getSelf();
+    }
+
+    protected final Shard getShard() {
+        return shard;
+    }
+
+    protected final String persistenceId() {
+        return shard.persistenceId();
+    }
+
+    protected final void tellSender(final Object message) {
+        shard.getSender().tell(message, getSelf());
+    }
+
+    protected final ActorRef createActor(final Props props) {
+        return shard.getContext().actorOf(props);
+    }
+
+    protected final ActorSelection selectActor(ActorRef ref) {
+        return shard.getContext().system().actorSelection(ref.path());
+    }
+
+    protected final ActorSelection selectActor(ActorPath path) {
+        return shard.getContext().system().actorSelection(path);
+    }
+
     /**
      * Invoked whenever the local shard's leadership role changes.
      *

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