X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsamples%2Fclustering-test-app%2Fprovider%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fclustering%2Fit%2Fprovider%2FMdsalLowLevelTestProvider.java;h=bea3bcd11f0c6f68c02c69cccc79c250bc2fc54c;hp=98ae842a505863fec0e7199a56ab18d1ebd75f05;hb=649930cb08759c14c69d5442cbae047028d6059d;hpb=841d4041df1926cd2cab034533ad081cd4351ac7 diff --git a/opendaylight/md-sal/samples/clustering-test-app/provider/src/main/java/org/opendaylight/controller/clustering/it/provider/MdsalLowLevelTestProvider.java b/opendaylight/md-sal/samples/clustering-test-app/provider/src/main/java/org/opendaylight/controller/clustering/it/provider/MdsalLowLevelTestProvider.java index 98ae842a50..bea3bcd11f 100644 --- a/opendaylight/md-sal/samples/clustering-test-app/provider/src/main/java/org/opendaylight/controller/clustering/it/provider/MdsalLowLevelTestProvider.java +++ b/opendaylight/md-sal/samples/clustering-test-app/provider/src/main/java/org/opendaylight/controller/clustering/it/provider/MdsalLowLevelTestProvider.java @@ -8,20 +8,31 @@ package org.opendaylight.controller.clustering.it.provider; +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.base.Preconditions; +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; @@ -50,8 +61,6 @@ import org.opendaylight.controller.sal.core.api.model.SchemaService; 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; @@ -91,8 +100,8 @@ import org.opendaylight.yangtools.yang.common.RpcError; 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; @@ -105,6 +114,7 @@ public class MdsalLowLevelTestProvider implements OdlMdsalLowlevelControlService private final RpcProviderRegistry rpcRegistry; private final BindingAwareBroker.RpcRegistration registration; private final DistributedShardFactory distributedShardFactory; + private final DistributedDataStoreInterface configDataStore; private final DOMDataTreeService domDataTreeService; private final BindingNormalizedNodeSerializer bindingNormalizedNodeSerializer; private final DOMDataBroker domDataBroker; @@ -116,6 +126,7 @@ public class MdsalLowLevelTestProvider implements OdlMdsalLowlevelControlService private final PrefixLeaderHandler prefixLeaderHandler; private final PrefixShardHandler prefixShardHandler; private final DOMDataTreeChangeService domDataTreeChangeService; + private final ActorSystem actorSystem; private Map, DOMRpcImplementationRegistration> routedRegistrations = new HashMap<>(); @@ -142,7 +153,9 @@ public class MdsalLowLevelTestProvider implements OdlMdsalLowlevelControlService 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; @@ -153,6 +166,9 @@ public class MdsalLowLevelTestProvider implements OdlMdsalLowlevelControlService this.domDataBroker = domDataBroker; this.domDataTreeService = domDataTreeService; this.distributedShardFactory = distributedShardFactory; + this.configDataStore = configDataStore; + this.actorSystem = actorSystemProvider.getActorSystem(); + this.prefixLeaderHandler = new PrefixLeaderHandler(domDataTreeService, bindingNormalizedNodeSerializer); domDataTreeChangeService = @@ -160,7 +176,8 @@ public class MdsalLowLevelTestProvider implements OdlMdsalLowlevelControlService registration = rpcRegistry.addRpcImplementation(OdlMdsalLowlevelControlService.class, this); - prefixShardHandler = new PrefixShardHandler(distributedShardFactory, bindingNormalizedNodeSerializer); + prefixShardHandler = new PrefixShardHandler(distributedShardFactory, domDataTreeService, + bindingNormalizedNodeSerializer); } @Override @@ -217,7 +234,7 @@ public class MdsalLowLevelTestProvider implements OdlMdsalLowlevelControlService dtclReg = domDataTreeChangeService .registerDataTreeChangeListener( new org.opendaylight.controller.md.sal.dom.api.DOMDataTreeIdentifier( - CONTROLLER_CONFIG, WriteTransactionsHandler.ID_INTS_YID), + CONTROLLER_CONFIG, WriteTransactionsHandler.ID_INT_YID), idIntsListener); return Futures.immediateFuture(RpcResultBuilder.success().build()); @@ -374,7 +391,7 @@ public class MdsalLowLevelTestProvider implements OdlMdsalLowlevelControlService 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); @@ -446,8 +463,10 @@ public class MdsalLowLevelTestProvider implements OdlMdsalLowlevelControlService dtclReg = null; } + + final Optional> readResult = - rTx.read(CONTROLLER_CONFIG, WriteTransactionsHandler.ID_INTS_YID).checkedGet(); + rTx.read(CONTROLLER_CONFIG, WriteTransactionsHandler.ID_INT_YID).checkedGet(); if (!readResult.isPresent()) { final RpcError error = RpcResultBuilder.newError( @@ -580,68 +599,60 @@ public class MdsalLowLevelTestProvider implements OdlMdsalLowlevelControlService 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 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.failed() + .withRpcError(error).build()); + } - final DataTreeCandidate dataTreeCandidate = readListener.getFirstNotif().get(); - registration.close(); + final ClientLocalHistory localHistory = distributedDataStoreClient.createLocalHistory(); + final ClientTransaction tx = localHistory.createTransaction(); + final CheckedFuture>, + 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> 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.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.failed() .withRpcError(error).build()); - } - } - - private static class ReadListener implements DOMDataTreeListener { - - private Collection changes = null; - private SettableFuture readFuture; - - @Override - public synchronized void onDataTreeChanged(@Nonnull final Collection changes, - @Nonnull final Map> 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 causes) { - LOG.error("Read Listener failed. {}", causes); - } - - public synchronized ListenableFuture getFirstNotif() { - if (changes != null) { - return Futures.immediateFuture(changes.iterator().next()); - } - - readFuture = SettableFuture.create(); - return readFuture; + } finally { + distributedDataStoreClient.close(); + clientActor.tell(PoisonPill.getInstance(), noSender()); } } }