package org.opendaylight.controller.clustering.it.provider;
-import com.google.common.base.Preconditions;
+import static akka.actor.ActorRef.noSender;
+
+import akka.actor.ActorRef;
+import akka.actor.ActorSystem;
+import akka.actor.PoisonPill;
+import akka.actor.Props;
+import com.google.common.base.Optional;
+import com.google.common.util.concurrent.CheckedFuture;
import com.google.common.util.concurrent.Futures;
-import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.SettableFuture;
import java.io.PrintWriter;
import java.io.StringWriter;
-import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
-import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
-import javax.annotation.Nonnull;
+import java.util.concurrent.TimeUnit;
+import org.opendaylight.controller.cluster.ActorSystemProvider;
+import org.opendaylight.controller.cluster.databroker.actors.dds.ClientLocalHistory;
+import org.opendaylight.controller.cluster.databroker.actors.dds.ClientTransaction;
+import org.opendaylight.controller.cluster.databroker.actors.dds.DataStoreClient;
+import org.opendaylight.controller.cluster.databroker.actors.dds.SimpleDataStoreClientActor;
+import org.opendaylight.controller.cluster.datastore.DistributedDataStoreInterface;
+import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
+import org.opendaylight.controller.cluster.datastore.utils.ClusterUtils;
import org.opendaylight.controller.cluster.sharding.DistributedShardFactory;
import org.opendaylight.controller.clustering.it.provider.impl.FlappingSingletonService;
import org.opendaylight.controller.clustering.it.provider.impl.GetConstantService;
import org.opendaylight.controller.clustering.it.provider.impl.IdIntsDOMDataTreeLIstener;
+import org.opendaylight.controller.clustering.it.provider.impl.IdIntsListener;
+import org.opendaylight.controller.clustering.it.provider.impl.PrefixLeaderHandler;
import org.opendaylight.controller.clustering.it.provider.impl.PrefixShardHandler;
import org.opendaylight.controller.clustering.it.provider.impl.ProduceTransactionsHandler;
import org.opendaylight.controller.clustering.it.provider.impl.PublishNotificationsTask;
import org.opendaylight.controller.clustering.it.provider.impl.YnlListener;
import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
import org.opendaylight.controller.md.sal.binding.api.NotificationService;
+import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
+import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
+import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
+import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeService;
import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementationRegistration;
import org.opendaylight.controller.md.sal.dom.api.DOMRpcProviderService;
import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
-import org.opendaylight.mdsal.dom.api.DOMDataTreeListener;
-import org.opendaylight.mdsal.dom.api.DOMDataTreeListeningException;
import org.opendaylight.mdsal.dom.api.DOMDataTreeLoopException;
import org.opendaylight.mdsal.dom.api.DOMDataTreeService;
import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.UnsubscribeDdtlOutput;
import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.UnsubscribeDdtlOutputBuilder;
import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.UnsubscribeDtclOutput;
+import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.UnsubscribeDtclOutputBuilder;
import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.UnsubscribeYnlInput;
import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.UnsubscribeYnlOutput;
import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.WriteTransactionsInput;
import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
import org.opendaylight.yangtools.yang.common.RpcResult;
import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
-import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MdsalLowLevelTestProvider implements OdlMdsalLowlevelControlService {
private static final Logger LOG = LoggerFactory.getLogger(MdsalLowLevelTestProvider.class);
+ private static final org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType CONTROLLER_CONFIG =
+ org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.CONFIGURATION;
private final RpcProviderRegistry rpcRegistry;
private final BindingAwareBroker.RpcRegistration<OdlMdsalLowlevelControlService> registration;
private final DistributedShardFactory distributedShardFactory;
+ private final DistributedDataStoreInterface configDataStore;
private final DOMDataTreeService domDataTreeService;
private final BindingNormalizedNodeSerializer bindingNormalizedNodeSerializer;
private final DOMDataBroker domDataBroker;
private final SchemaService schemaService;
private final ClusterSingletonServiceProvider singletonService;
private final DOMRpcProviderService domRpcService;
+ private final PrefixLeaderHandler prefixLeaderHandler;
private final PrefixShardHandler prefixShardHandler;
+ private final DOMDataTreeChangeService domDataTreeChangeService;
+ private final ActorSystem actorSystem;
private Map<InstanceIdentifier<?>, DOMRpcImplementationRegistration<RoutedGetConstantService>> routedRegistrations =
new HashMap<>();
private DOMRpcImplementationRegistration<GetConstantService> globalGetConstantRegistration = null;
private ClusterSingletonServiceRegistration getSingletonConstantRegistration;
private FlappingSingletonService flappingSingletonService;
+ private ListenerRegistration<DOMDataTreeChangeListener> dtclReg;
+ private IdIntsListener idIntsListener;
private Map<String, PublishNotificationsTask> publishNotificationsTasks = new HashMap<>();
private ListenerRegistration<IdIntsDOMDataTreeLIstener> ddtlReg;
private IdIntsDOMDataTreeLIstener idIntsDdtl;
+
+
public MdsalLowLevelTestProvider(final RpcProviderRegistry rpcRegistry,
final DOMRpcProviderService domRpcService,
final ClusterSingletonServiceProvider singletonService,
final NotificationService notificationService,
final DOMDataBroker domDataBroker,
final DOMDataTreeService domDataTreeService,
- final DistributedShardFactory distributedShardFactory) {
+ final DistributedShardFactory distributedShardFactory,
+ final DistributedDataStoreInterface configDataStore,
+ final ActorSystemProvider actorSystemProvider) {
this.rpcRegistry = rpcRegistry;
this.domRpcService = domRpcService;
this.singletonService = singletonService;
this.domDataBroker = domDataBroker;
this.domDataTreeService = domDataTreeService;
this.distributedShardFactory = distributedShardFactory;
+ this.configDataStore = configDataStore;
+ this.actorSystem = actorSystemProvider.getActorSystem();
+
+ this.prefixLeaderHandler = new PrefixLeaderHandler(domDataTreeService, bindingNormalizedNodeSerializer);
+
+ domDataTreeChangeService =
+ (DOMDataTreeChangeService) domDataBroker.getSupportedExtensions().get(DOMDataTreeChangeService.class);
registration = rpcRegistry.addRpcImplementation(OdlMdsalLowlevelControlService.class, this);
- prefixShardHandler = new PrefixShardHandler(distributedShardFactory, bindingNormalizedNodeSerializer);
+ prefixShardHandler = new PrefixShardHandler(distributedShardFactory, domDataTreeService,
+ bindingNormalizedNodeSerializer);
}
@Override
@Override
public Future<RpcResult<Void>> subscribeDtcl() {
- return null;
+
+ if (dtclReg != null) {
+ final RpcError error = RpcResultBuilder.newError(ErrorType.RPC, "Registration present.",
+ "There is already dataTreeChangeListener registered on id-ints list.");
+ return Futures.immediateFuture(RpcResultBuilder.<Void>failed().withRpcError(error).build());
+ }
+
+ idIntsListener = new IdIntsListener();
+
+ dtclReg = domDataTreeChangeService
+ .registerDataTreeChangeListener(
+ new org.opendaylight.controller.md.sal.dom.api.DOMDataTreeIdentifier(
+ CONTROLLER_CONFIG, WriteTransactionsHandler.ID_INT_YID),
+ idIntsListener);
+
+ return Futures.immediateFuture(RpcResultBuilder.<Void>success().build());
}
@Override
@Override
public Future<RpcResult<Void>> becomePrefixLeader(final BecomePrefixLeaderInput input) {
- return null;
+ LOG.debug("become-prefix-leader, input: {}", input);
+
+ return prefixLeaderHandler.makeLeaderLocal(input);
}
@Override
ddtlReg =
domDataTreeService.registerListener(idIntsDdtl,
Collections.singleton(new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION,
- ProduceTransactionsHandler.ID_INTS_YID))
+ ProduceTransactionsHandler.ID_INT_YID))
, true, Collections.emptyList());
} catch (DOMDataTreeLoopException e) {
LOG.error("Failed to register DOMDataTreeListener.", e);
@Override
public Future<RpcResult<UnsubscribeDtclOutput>> unsubscribeDtcl() {
- return null;
+ LOG.debug("Received unsubscribe-dtcl");
+
+ if (idIntsListener == null || dtclReg == null) {
+ final RpcError error = RpcResultBuilder.newError(
+ ErrorType.RPC, "Dtcl missing.", "No DataTreeChangeListener registered.");
+ return Futures.immediateFuture(RpcResultBuilder.<UnsubscribeDtclOutput>failed().withRpcError(error).build());
+ }
+
+ final DOMDataReadOnlyTransaction rTx = domDataBroker.newReadOnlyTransaction();
+ try {
+ if (dtclReg != null) {
+ dtclReg.close();
+ dtclReg = null;
+ }
+
+
+
+ final Optional<NormalizedNode<?, ?>> readResult =
+ rTx.read(CONTROLLER_CONFIG, WriteTransactionsHandler.ID_INT_YID).checkedGet();
+
+ if (!readResult.isPresent()) {
+ final RpcError error = RpcResultBuilder.newError(
+ ErrorType.APPLICATION, "Final read empty.", "No data read from id-ints list.");
+ return Futures.immediateFuture(RpcResultBuilder.<UnsubscribeDtclOutput>failed()
+ .withRpcError(error).build());
+ }
+
+ return Futures.immediateFuture(
+ RpcResultBuilder.success(new UnsubscribeDtclOutputBuilder()
+ .setCopyMatches(idIntsListener.checkEqual(readResult.get()))).build());
+
+ } catch (final ReadFailedException e) {
+ final RpcError error = RpcResultBuilder.newError(
+ ErrorType.APPLICATION, "Read failed.", "Final read from id-ints failed.");
+ return Futures.immediateFuture(RpcResultBuilder.<UnsubscribeDtclOutput>failed()
+ .withRpcError(error).build());
+
+ }
}
@Override
ddtlReg.close();
ddtlReg = null;
- final ReadListener readListener = new ReadListener();
+ final String shardName = ClusterUtils.getCleanShardName(ProduceTransactionsHandler.ID_INTS_YID);
+ LOG.debug("Creating distributed datastore client for shard {}", shardName);
+
+ final ActorContext actorContext = configDataStore.getActorContext();
+ final Props distributedDataStoreClientProps =
+ SimpleDataStoreClientActor.props(actorContext.getCurrentMemberName(),
+ "Shard-" + shardName, actorContext, shardName);
+
+ final ActorRef clientActor = actorSystem.actorOf(distributedDataStoreClientProps);
+ final DataStoreClient distributedDataStoreClient;
try {
- final ListenerRegistration<ReadListener> registration = domDataTreeService.registerListener(readListener,
- Collections.singleton(new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION,
- ProduceTransactionsHandler.ID_INTS_YID))
- , true, Collections.emptyList());
+ distributedDataStoreClient = SimpleDataStoreClientActor
+ .getDistributedDataStoreClient(clientActor, 30, TimeUnit.SECONDS);
+ } catch (final Exception e) {
+ LOG.error("Failed to get actor for {}", distributedDataStoreClientProps, e);
+ clientActor.tell(PoisonPill.getInstance(), noSender());
+ final RpcError error = RpcResultBuilder.newError(
+ ErrorType.APPLICATION, "Unable to create ds client for read.",
+ "Unable to create ds client for read.");
+ return Futures.immediateFuture(RpcResultBuilder.<UnsubscribeDdtlOutput>failed()
+ .withRpcError(error).build());
+ }
- final DataTreeCandidate dataTreeCandidate = readListener.getFirstNotif().get();
- registration.close();
+ final ClientLocalHistory localHistory = distributedDataStoreClient.createLocalHistory();
+ final ClientTransaction tx = localHistory.createTransaction();
+ final CheckedFuture<Optional<NormalizedNode<?, ?>>,
+ org.opendaylight.mdsal.common.api.ReadFailedException> read =
+ tx.read(YangInstanceIdentifier.of(ProduceTransactionsHandler.ID_INT));
- if (!dataTreeCandidate.getRootNode().getDataAfter().isPresent()) {
+ tx.abort();
+ localHistory.close();
+ try {
+ final Optional<NormalizedNode<?, ?>> optional = read.checkedGet();
+ if (!optional.isPresent()) {
+ LOG.warn("Final read from client is empty.");
final RpcError error = RpcResultBuilder.newError(
- ErrorType.APPLICATION, "Final read empty.", "No data read from id-ints list.");
+ ErrorType.APPLICATION, "Read failed.", "Final read from id-ints is empty.");
return Futures.immediateFuture(RpcResultBuilder.<UnsubscribeDdtlOutput>failed()
.withRpcError(error).build());
}
- final NormalizedNode<?, ?> lastRead = dataTreeCandidate.getRootNode().getDataAfter().get();
-
return Futures.immediateFuture(
RpcResultBuilder.success(new UnsubscribeDdtlOutputBuilder()
- .setCopyMatches(idIntsDdtl.checkEqual(lastRead))).build());
-
+ .setCopyMatches(idIntsDdtl.checkEqual(optional.get()))).build());
- } catch (final DOMDataTreeLoopException | InterruptedException | ExecutionException e) {
+ } catch (org.opendaylight.mdsal.common.api.ReadFailedException e) {
LOG.error("Unable to read data to verify ddtl data.", e);
final RpcError error = RpcResultBuilder.newError(
ErrorType.APPLICATION, "Read failed.", "Final read from id-ints failed.");
return Futures.immediateFuture(RpcResultBuilder.<UnsubscribeDdtlOutput>failed()
.withRpcError(error).build());
- }
- }
-
- private static class ReadListener implements DOMDataTreeListener {
-
- private Collection<DataTreeCandidate> changes = null;
- private SettableFuture<DataTreeCandidate> readFuture;
-
- @Override
- public synchronized void onDataTreeChanged(@Nonnull final Collection<DataTreeCandidate> changes,
- @Nonnull final Map<DOMDataTreeIdentifier, NormalizedNode<?, ?>> subtrees) {
- Preconditions.checkArgument(changes.size() == 1);
-
- if (this.changes == null) {
- this.changes = changes;
-
- readFuture.set(changes.iterator().next());
- }
- }
-
- @Override
- public void onDataTreeFailed(@Nonnull final Collection<DOMDataTreeListeningException> causes) {
- LOG.error("Read Listener failed. {}", causes);
- }
-
- public synchronized ListenableFuture<DataTreeCandidate> getFirstNotif() {
- if (changes != null) {
- return Futures.immediateFuture(changes.iterator().next());
- }
-
- readFuture = SettableFuture.create();
- return readFuture;
+ } finally {
+ distributedDataStoreClient.close();
+ clientActor.tell(PoisonPill.getInstance(), noSender());
}
}
}