Bump upstreams
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / DataTreeChangeListenerProxy.java
index 37479871c72239e4cc84cd1a7fcab27981b241f2..be849284e7f1fabdd04f9e3255e99a5912e53f48 100644 (file)
@@ -14,19 +14,20 @@ import akka.actor.ActorSelection;
 import akka.actor.PoisonPill;
 import akka.dispatch.OnComplete;
 import com.google.common.annotations.VisibleForTesting;
+import com.google.common.util.concurrent.MoreExecutors;
+import java.util.concurrent.Executor;
 import org.checkerframework.checker.lock.qual.GuardedBy;
+import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.controller.cluster.datastore.exceptions.LocalShardNotFoundException;
 import org.opendaylight.controller.cluster.datastore.messages.CloseDataTreeNotificationListenerRegistration;
 import org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeChangeListener;
 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.concepts.AbstractObjectRegistration;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import scala.concurrent.Future;
 
 /**
  * Proxy class for holding required state to lazily instantiate a listener registration with an
@@ -34,28 +35,61 @@ import scala.concurrent.Future;
  *
  * @param <T> listener type
  */
-final class DataTreeChangeListenerProxy<T extends DOMDataTreeChangeListener> extends AbstractListenerRegistration<T> {
+final class DataTreeChangeListenerProxy extends AbstractObjectRegistration<DOMDataTreeChangeListener> {
     private static final Logger LOG = LoggerFactory.getLogger(DataTreeChangeListenerProxy.class);
     private final ActorRef dataChangeListenerActor;
     private final ActorUtils actorUtils;
     private final YangInstanceIdentifier registeredPath;
+    private final boolean clustered;
 
     @GuardedBy("this")
     private ActorSelection listenerRegistrationActor;
 
-    DataTreeChangeListenerProxy(final ActorUtils actorUtils, final T listener,
-            final YangInstanceIdentifier registeredPath) {
+    @VisibleForTesting
+    private DataTreeChangeListenerProxy(final ActorUtils actorUtils, final DOMDataTreeChangeListener listener,
+            final YangInstanceIdentifier registeredPath, final boolean clustered, final String shardName) {
         super(listener);
         this.actorUtils = requireNonNull(actorUtils);
         this.registeredPath = requireNonNull(registeredPath);
-        this.dataChangeListenerActor = actorUtils.getActorSystem().actorOf(
+        this.clustered = clustered;
+        dataChangeListenerActor = actorUtils.getActorSystem().actorOf(
                 DataTreeChangeListenerActor.props(getInstance(), registeredPath)
                     .withDispatcher(actorUtils.getNotificationDispatcherPath()));
-
         LOG.debug("{}: Created actor {} for DTCL {}", actorUtils.getDatastoreContext().getLogicalStoreType(),
                 dataChangeListenerActor, listener);
     }
 
+    static @NonNull DataTreeChangeListenerProxy of(final ActorUtils actorUtils,
+            final DOMDataTreeChangeListener listener, final YangInstanceIdentifier registeredPath,
+            final boolean clustered, final String shardName) {
+        return ofTesting(actorUtils, listener, registeredPath, clustered, shardName, MoreExecutors.directExecutor());
+    }
+
+    @VisibleForTesting
+    static @NonNull DataTreeChangeListenerProxy ofTesting(final ActorUtils actorUtils,
+            final DOMDataTreeChangeListener listener, final YangInstanceIdentifier registeredPath,
+            final boolean clustered, final String shardName, final Executor executor) {
+        final var ret = new DataTreeChangeListenerProxy(actorUtils, listener, registeredPath, clustered, shardName);
+        executor.execute(() -> {
+            LOG.debug("{}: Starting discovery of shard {}", ret.logContext(), shardName);
+            actorUtils.findLocalShardAsync(shardName).onComplete(new OnComplete<>() {
+                @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", ret.logContext(), shardName, listener, registeredPath);
+                    } else if (failure != null) {
+                        LOG.error("{}: Failed to find local shard {} - DataTreeChangeListener {} at path {} cannot be "
+                            + "registered", ret.logContext(), shardName, listener, registeredPath, failure);
+                    } else {
+                        ret.doRegistration(shard);
+                    }
+                }
+            }, actorUtils.getClientDispatcher());
+        });
+        return ret;
+    }
+
     @Override
     protected synchronized void removeRegistration() {
         if (listenerRegistrationActor != null) {
@@ -67,25 +101,6 @@ final class DataTreeChangeListenerProxy<T extends DOMDataTreeChangeListener> ext
         dataChangeListenerActor.tell(PoisonPill.getInstance(), ActorRef.noSender());
     }
 
-    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", logContext(), shardName, getInstance(), registeredPath);
-                } else if (failure != null) {
-                    LOG.error("{}: Failed to find local shard {} - DataTreeChangeListener {} at path {} "
-                            + "cannot be registered", logContext(), shardName, getInstance(), registeredPath,
-                            failure);
-                } else {
-                    doRegistration(shard);
-                }
-            }
-        }, actorUtils.getClientDispatcher());
-    }
-
     private void setListenerRegistrationActor(final ActorSelection actor) {
         if (actor == null) {
             LOG.debug("{}: Ignoring null actor on {}", logContext(), this);
@@ -94,7 +109,7 @@ final class DataTreeChangeListenerProxy<T extends DOMDataTreeChangeListener> ext
 
         synchronized (this) {
             if (!isClosed()) {
-                this.listenerRegistrationActor = actor;
+                listenerRegistrationActor = actor;
                 return;
             }
         }
@@ -104,25 +119,20 @@ final class DataTreeChangeListenerProxy<T extends DOMDataTreeChangeListener> ext
     }
 
     private void doRegistration(final ActorRef shard) {
-
-        Future<Object> future = actorUtils.executeOperationAsync(shard,
-                new RegisterDataTreeChangeListener(registeredPath, dataChangeListenerActor,
-                        getInstance() instanceof ClusteredDOMDataTreeChangeListener),
-                actorUtils.getDatastoreContext().getShardInitializationTimeout());
-
-        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 {}", logContext(),
+        actorUtils.executeOperationAsync(shard,
+            new RegisterDataTreeChangeListener(registeredPath, dataChangeListenerActor, clustered),
+            actorUtils.getDatastoreContext().getShardInitializationTimeout()).onComplete(new OnComplete<>() {
+                @Override
+                public void onComplete(final Throwable failure, final Object result) {
+                    if (failure != null) {
+                        LOG.error("{}: Failed to register DataTreeChangeListener {} at path {}", logContext(),
                             getInstance(), registeredPath, failure);
-                } else {
-                    RegisterDataTreeNotificationListenerReply reply = (RegisterDataTreeNotificationListenerReply)result;
-                    setListenerRegistrationActor(actorUtils.actorSelection(
-                            reply.getListenerRegistrationPath()));
+                    } else {
+                        setListenerRegistrationActor(actorUtils.actorSelection(
+                            ((RegisterDataTreeNotificationListenerReply) result).getListenerRegistrationPath()));
+                    }
                 }
-            }
-        }, actorUtils.getClientDispatcher());
+            }, actorUtils.getClientDispatcher());
     }
 
     @VisibleForTesting