*/
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 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.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.ClusteredDOMDataTreeChangeListener;
-import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
-import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
+import org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeNotificationListenerReply;
+import org.opendaylight.controller.cluster.datastore.utils.ActorUtils;
+import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeListener;
+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 ActorContext actorContext;
+ private final ActorUtils actorUtils;
+ private final YangInstanceIdentifier registeredPath;
+ private final boolean clustered;
@GuardedBy("this")
private ActorSelection listenerRegistrationActor;
- public DataTreeChangeListenerProxy(final ActorContext actorContext, final T listener) {
+ @VisibleForTesting
+ private DataTreeChangeListenerProxy(final ActorUtils actorUtils, final DOMDataTreeChangeListener listener,
+ final YangInstanceIdentifier registeredPath, final boolean clustered, final String shardName) {
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.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) {
- 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);
- 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);
- } else if (failure != null) {
- LOG.error("Failed to find local shard {} - DataTreeChangeListener {} at path {} " +
- "cannot be registered: {}", shardName, getInstance(), treeId, failure);
- } else {
- doRegistration(shard, treeId);
- }
- }
- }, actorContext.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;
}
synchronized (this) {
if (!isClosed()) {
- this.listenerRegistrationActor = actor;
+ listenerRegistrationActor = actor;
return;
}
}
// 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) {
-
- Future<Object> future = actorContext.executeOperationAsync(shard,
- new RegisterDataTreeChangeListener(path, dataChangeListenerActor,
- getInstance() instanceof ClusteredDOMDataTreeChangeListener),
- actorContext.getDatastoreContext().getShardInitializationTimeout());
-
- future.onComplete(new OnComplete<Object>(){
- @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);
- } else {
- RegisterDataTreeChangeListenerReply reply = (RegisterDataTreeChangeListenerReply) result;
- setListenerRegistrationActor(actorContext.actorSelection(
- reply.getListenerRegistrationPath()));
+ private void doRegistration(final ActorRef shard) {
+ 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 {
+ setListenerRegistrationActor(actorUtils.actorSelection(
+ ((RegisterDataTreeNotificationListenerReply) result).getListenerRegistrationPath()));
+ }
}
- }
- }, actorContext.getClientDispatcher());
+ }, actorUtils.getClientDispatcher());
}
@VisibleForTesting
- ActorSelection getListenerRegistrationActor() {
+ synchronized ActorSelection getListenerRegistrationActor() {
return listenerRegistrationActor;
}
ActorRef getDataChangeListenerActor() {
return dataChangeListenerActor;
}
+
+ private String logContext() {
+ return actorUtils.getDatastoreContext().getLogicalStoreType().toString();
+ }
}