Bump upstream SNAPSHOTS
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / DataTreeChangeListenerProxy.java
index 1a27f2e4fcdc19f2a3595764a80e00f963960ea4..37479871c72239e4cc84cd1a7fcab27981b241f2 100644 (file)
@@ -7,19 +7,21 @@
  */
 package org.opendaylight.controller.cluster.datastore;
 
+import static java.util.Objects.requireNonNull;
+
 import akka.actor.ActorRef;
 import akka.actor.ActorSelection;
 import akka.actor.PoisonPill;
 import akka.dispatch.OnComplete;
 import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.Preconditions;
-import javax.annotation.concurrent.GuardedBy;
+import org.checkerframework.checker.lock.qual.GuardedBy;
 import org.opendaylight.controller.cluster.datastore.exceptions.LocalShardNotFoundException;
-import org.opendaylight.controller.cluster.datastore.messages.CloseDataTreeChangeListenerRegistration;
+import org.opendaylight.controller.cluster.datastore.messages.CloseDataTreeNotificationListenerRegistration;
 import org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeChangeListener;
-import org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeChangeListenerReply;
-import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
-import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
+import org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeNotificationListenerReply;
+import org.opendaylight.controller.cluster.datastore.utils.ActorUtils;
+import org.opendaylight.mdsal.dom.api.ClusteredDOMDataTreeChangeListener;
+import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeListener;
 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.slf4j.Logger;
@@ -35,49 +37,58 @@ import scala.concurrent.Future;
 final class DataTreeChangeListenerProxy<T extends DOMDataTreeChangeListener> extends AbstractListenerRegistration<T> {
     private static final Logger LOG = LoggerFactory.getLogger(DataTreeChangeListenerProxy.class);
     private final ActorRef dataChangeListenerActor;
-    private final ActorContext actorContext;
+    private final ActorUtils actorUtils;
+    private final YangInstanceIdentifier registeredPath;
 
     @GuardedBy("this")
     private ActorSelection listenerRegistrationActor;
 
-    public DataTreeChangeListenerProxy(final ActorContext actorContext, final T listener) {
+    DataTreeChangeListenerProxy(final ActorUtils actorUtils, final T listener,
+            final YangInstanceIdentifier registeredPath) {
         super(listener);
-        this.actorContext = Preconditions.checkNotNull(actorContext);
-        this.dataChangeListenerActor = actorContext.getActorSystem().actorOf(
-            DataTreeChangeListenerActor.props(getInstance()).withDispatcher(actorContext.getNotificationDispatcherPath()));
+        this.actorUtils = requireNonNull(actorUtils);
+        this.registeredPath = requireNonNull(registeredPath);
+        this.dataChangeListenerActor = actorUtils.getActorSystem().actorOf(
+                DataTreeChangeListenerActor.props(getInstance(), registeredPath)
+                    .withDispatcher(actorUtils.getNotificationDispatcherPath()));
+
+        LOG.debug("{}: Created actor {} for DTCL {}", actorUtils.getDatastoreContext().getLogicalStoreType(),
+                dataChangeListenerActor, listener);
     }
 
     @Override
     protected synchronized void removeRegistration() {
         if (listenerRegistrationActor != null) {
-            listenerRegistrationActor.tell(CloseDataTreeChangeListenerRegistration.getInstance(), ActorRef.noSender());
+            listenerRegistrationActor.tell(CloseDataTreeNotificationListenerRegistration.getInstance(),
+                    ActorRef.noSender());
             listenerRegistrationActor = null;
         }
 
         dataChangeListenerActor.tell(PoisonPill.getInstance(), ActorRef.noSender());
     }
 
-    void init(final String shardName, final YangInstanceIdentifier treeId) {
-        Future<ActorRef> findFuture = actorContext.findLocalShardAsync(shardName);
+    void init(final String shardName) {
+        Future<ActorRef> findFuture = actorUtils.findLocalShardAsync(shardName);
         findFuture.onComplete(new OnComplete<ActorRef>() {
             @Override
             public void onComplete(final Throwable failure, final ActorRef shard) {
                 if (failure instanceof LocalShardNotFoundException) {
-                    LOG.debug("No local shard found for {} - DataTreeChangeListener {} at path {} " +
-                            "cannot be registered", shardName, getInstance(), treeId);
+                    LOG.debug("{}: No local shard found for {} - DataTreeChangeListener {} at path {} "
+                            + "cannot be registered", logContext(), shardName, getInstance(), registeredPath);
                 } else if (failure != null) {
-                    LOG.error("Failed to find local shard {} - DataTreeChangeListener {} at path {} " +
-                            "cannot be registered: {}", shardName, getInstance(), treeId, failure);
+                    LOG.error("{}: Failed to find local shard {} - DataTreeChangeListener {} at path {} "
+                            + "cannot be registered", logContext(), shardName, getInstance(), registeredPath,
+                            failure);
                 } else {
-                    doRegistration(shard, treeId);
+                    doRegistration(shard);
                 }
             }
-        }, actorContext.getClientDispatcher());
+        }, actorUtils.getClientDispatcher());
     }
 
     private void setListenerRegistrationActor(final ActorSelection actor) {
         if (actor == null) {
-            LOG.debug("Ignoring null actor on {}", this);
+            LOG.debug("{}: Ignoring null actor on {}", logContext(), this);
             return;
         }
 
@@ -89,32 +100,33 @@ final class DataTreeChangeListenerProxy<T extends DOMDataTreeChangeListener> ext
         }
 
         // This registration has already been closed, notify the actor
-        actor.tell(CloseDataTreeChangeListenerRegistration.getInstance(), null);
+        actor.tell(CloseDataTreeNotificationListenerRegistration.getInstance(), null);
     }
 
-    private void doRegistration(final ActorRef shard, final YangInstanceIdentifier path) {
+    private void doRegistration(final ActorRef shard) {
 
-        Future<Object> future = actorContext.executeOperationAsync(shard,
-                new RegisterDataTreeChangeListener(path, dataChangeListenerActor),
-                actorContext.getDatastoreContext().getShardInitializationTimeout());
+        Future<Object> future = actorUtils.executeOperationAsync(shard,
+                new RegisterDataTreeChangeListener(registeredPath, dataChangeListenerActor,
+                        getInstance() instanceof ClusteredDOMDataTreeChangeListener),
+                actorUtils.getDatastoreContext().getShardInitializationTimeout());
 
-        future.onComplete(new OnComplete<Object>(){
+        future.onComplete(new OnComplete<>() {
             @Override
             public void onComplete(final Throwable failure, final Object result) {
                 if (failure != null) {
-                    LOG.error("Failed to register DataTreeChangeListener {} at path {}",
-                            getInstance(), path.toString(), failure);
+                    LOG.error("{}: Failed to register DataTreeChangeListener {} at path {}", logContext(),
+                            getInstance(), registeredPath, failure);
                 } else {
-                    RegisterDataTreeChangeListenerReply reply = (RegisterDataTreeChangeListenerReply) result;
-                    setListenerRegistrationActor(actorContext.actorSelection(
+                    RegisterDataTreeNotificationListenerReply reply = (RegisterDataTreeNotificationListenerReply)result;
+                    setListenerRegistrationActor(actorUtils.actorSelection(
                             reply.getListenerRegistrationPath()));
                 }
             }
-        }, actorContext.getClientDispatcher());
+        }, actorUtils.getClientDispatcher());
     }
 
     @VisibleForTesting
-    ActorSelection getListenerRegistrationActor() {
+    synchronized ActorSelection getListenerRegistrationActor() {
         return listenerRegistrationActor;
     }
 
@@ -122,4 +134,8 @@ final class DataTreeChangeListenerProxy<T extends DOMDataTreeChangeListener> ext
     ActorRef getDataChangeListenerActor() {
         return dataChangeListenerActor;
     }
+
+    private String logContext() {
+        return actorUtils.getDatastoreContext().getLogicalStoreType().toString();
+    }
 }