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
*
* @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) {
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);
synchronized (this) {
if (!isClosed()) {
- this.listenerRegistrationActor = actor;
+ listenerRegistrationActor = actor;
return;
}
}
}
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