X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-distributed-datastore%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatastore%2FAbstractShardTest.java;h=382c17dd5a3b2253b2aeb0b34fefc58274e750a3;hp=34f0164504fe666e7cfb76090c57ed5b2362a268;hb=94603c85193862f85bf9d9aa51d5062d9f84e979;hpb=2a31c2cacb9ad8f015a49708261ea93d256f0f60 diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/AbstractShardTest.java b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/AbstractShardTest.java index 34f0164504..382c17dd5a 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/AbstractShardTest.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/AbstractShardTest.java @@ -21,7 +21,6 @@ import akka.japi.Creator; import akka.testkit.TestActorRef; import com.google.common.base.Function; import com.google.common.base.Optional; -import com.google.common.util.concurrent.CheckedFuture; import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.Uninterruptibles; import java.util.Collections; @@ -42,16 +41,16 @@ import org.opendaylight.controller.cluster.datastore.modification.WriteModificat import org.opendaylight.controller.cluster.raft.utils.InMemoryJournal; import org.opendaylight.controller.cluster.raft.utils.InMemorySnapshotStore; import org.opendaylight.controller.md.cluster.datastore.model.TestModel; -import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException; -import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore; -import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction; -import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort; -import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument; import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild; import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; +import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree; +import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate; +import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateTip; +import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification; +import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException; import org.opendaylight.yangtools.yang.model.api.SchemaContext; /** @@ -92,14 +91,15 @@ public abstract class AbstractShardTest extends AbstractActorTest{ newDatastoreContext(), SCHEMA_CONTEXT); } - protected void testRecovery(Set listEntryKeys) throws Exception { + protected void testRecovery(final Set listEntryKeys) throws Exception { // Create the actor and wait for recovery complete. - int nListEntries = listEntryKeys.size(); + final int nListEntries = listEntryKeys.size(); final CountDownLatch recoveryComplete = new CountDownLatch(1); @SuppressWarnings("serial") + final Creator creator = new Creator() { @Override public Shard create() throws Exception { @@ -117,25 +117,25 @@ public abstract class AbstractShardTest extends AbstractActorTest{ } }; - TestActorRef shard = TestActorRef.create(getSystem(), + final TestActorRef shard = TestActorRef.create(getSystem(), Props.create(new DelegatingShardCreator(creator)).withDispatcher(Dispatchers.DefaultDispatcherId()), "testRecovery"); assertEquals("Recovery complete", true, recoveryComplete.await(5, TimeUnit.SECONDS)); // Verify data in the data store. - NormalizedNode outerList = readStore(shard, TestModel.OUTER_LIST_PATH); + final NormalizedNode outerList = readStore(shard, TestModel.OUTER_LIST_PATH); assertNotNull(TestModel.OUTER_LIST_QNAME.getLocalName() + " not found", outerList); assertTrue(TestModel.OUTER_LIST_QNAME.getLocalName() + " value is not Iterable", outerList.getValue() instanceof Iterable); - for(Object entry: (Iterable) outerList.getValue()) { + for(final Object entry: (Iterable) outerList.getValue()) { assertTrue(TestModel.OUTER_LIST_QNAME.getLocalName() + " entry is not MapEntryNode", entry instanceof MapEntryNode); - MapEntryNode mapEntry = (MapEntryNode)entry; - Optional> idLeaf = + final MapEntryNode mapEntry = (MapEntryNode)entry; + final Optional> idLeaf = mapEntry.getChild(new YangInstanceIdentifier.NodeIdentifier(TestModel.ID_QNAME)); assertTrue("Missing leaf " + TestModel.ID_QNAME.getLocalName(), idLeaf.isPresent()); - Object value = idLeaf.get().getValue(); + final Object value = idLeaf.get().getValue(); assertTrue("Unexpected value for leaf "+ TestModel.ID_QNAME.getLocalName() + ": " + value, listEntryKeys.remove(value)); } @@ -155,7 +155,7 @@ public abstract class AbstractShardTest extends AbstractActorTest{ shard.tell(PoisonPill.getInstance(), ActorRef.noSender()); } - protected void verifyLastApplied(TestActorRef shard, long expectedValue) { + protected void verifyLastApplied(final TestActorRef shard, final long expectedValue) { long lastApplied = -1; for(int i = 0; i < 20 * 5; i++) { lastApplied = shard.underlyingActor().getShardMBean().getLastApplied(); @@ -168,49 +168,35 @@ public abstract class AbstractShardTest extends AbstractActorTest{ Assert.fail(String.format("Expected last applied: %d, Actual: %d", expectedValue, lastApplied)); } - protected NormalizedNode readStore(final InMemoryDOMDataStore store) throws ReadFailedException { - DOMStoreReadTransaction transaction = store.newReadOnlyTransaction(); - CheckedFuture>, ReadFailedException> read = - transaction.read(YangInstanceIdentifier.builder().build()); - - Optional> optional = read.checkedGet(); - - NormalizedNode normalizedNode = optional.get(); - - transaction.close(); - - return normalizedNode; - } - - protected DOMStoreThreePhaseCommitCohort setupMockWriteTransaction(final String cohortName, - final InMemoryDOMDataStore dataStore, final YangInstanceIdentifier path, final NormalizedNode data, + protected ShardDataTreeCohort setupMockWriteTransaction(final String cohortName, + final ShardDataTree dataStore, final YangInstanceIdentifier path, final NormalizedNode data, final MutableCompositeModification modification) { return setupMockWriteTransaction(cohortName, dataStore, path, data, modification, null); } - protected DOMStoreThreePhaseCommitCohort setupMockWriteTransaction(final String cohortName, - final InMemoryDOMDataStore dataStore, final YangInstanceIdentifier path, final NormalizedNode data, + protected ShardDataTreeCohort setupMockWriteTransaction(final String cohortName, + final ShardDataTree dataStore, final YangInstanceIdentifier path, final NormalizedNode data, final MutableCompositeModification modification, - final Function> preCommit) { + final Function> preCommit) { - DOMStoreWriteTransaction tx = dataStore.newWriteOnlyTransaction(); - tx.write(path, data); - DOMStoreThreePhaseCommitCohort cohort = createDelegatingMockCohort(cohortName, tx.ready(), preCommit); + final ReadWriteShardDataTreeTransaction tx = dataStore.newReadWriteTransaction("setup-mock-" + cohortName, null); + tx.getSnapshot().write(path, data); + final ShardDataTreeCohort cohort = createDelegatingMockCohort(cohortName, dataStore.finishTransaction(tx), preCommit); modification.addModification(new WriteModification(path, data)); return cohort; } - protected DOMStoreThreePhaseCommitCohort createDelegatingMockCohort(final String cohortName, - final DOMStoreThreePhaseCommitCohort actual) { + protected ShardDataTreeCohort createDelegatingMockCohort(final String cohortName, + final ShardDataTreeCohort actual) { return createDelegatingMockCohort(cohortName, actual, null); } - protected DOMStoreThreePhaseCommitCohort createDelegatingMockCohort(final String cohortName, - final DOMStoreThreePhaseCommitCohort actual, - final Function> preCommit) { - DOMStoreThreePhaseCommitCohort cohort = mock(DOMStoreThreePhaseCommitCohort.class, cohortName); + protected ShardDataTreeCohort createDelegatingMockCohort(final String cohortName, + final ShardDataTreeCohort actual, + final Function> preCommit) { + final ShardDataTreeCohort cohort = mock(ShardDataTreeCohort.class, cohortName); doAnswer(new Answer>() { @Override @@ -222,7 +208,11 @@ public abstract class AbstractShardTest extends AbstractActorTest{ doAnswer(new Answer>() { @Override public ListenableFuture answer(final InvocationOnMock invocation) throws Throwable { - return actual.preCommit(); + if(preCommit != null) { + return preCommit.apply(actual); + } else { + return actual.preCommit(); + } } }).when(cohort).preCommit(); @@ -240,43 +230,61 @@ public abstract class AbstractShardTest extends AbstractActorTest{ } }).when(cohort).abort(); + doAnswer(new Answer() { + @Override + public DataTreeCandidateTip answer(final InvocationOnMock invocation) { + return actual.getCandidate(); + } + }).when(cohort).getCandidate(); + return cohort; } - public static NormalizedNode readStore(final TestActorRef shard, final YangInstanceIdentifier id) + public static NormalizedNode readStore(final TestActorRef shard, final YangInstanceIdentifier id) throws ExecutionException, InterruptedException { - return readStore(shard.underlyingActor().getDataStore(), id); + return shard.underlyingActor().getDataStore().readNode(id).orNull(); } - public static NormalizedNode readStore(final InMemoryDOMDataStore store, final YangInstanceIdentifier id) - throws ExecutionException, InterruptedException { - DOMStoreReadTransaction transaction = store.newReadOnlyTransaction(); - - CheckedFuture>, ReadFailedException> future = - transaction.read(id); + public static NormalizedNode readStore(final DataTree store, final YangInstanceIdentifier id) { + return store.takeSnapshot().readNode(id).orNull(); + } - Optional> optional = future.get(); - NormalizedNode node = optional.isPresent()? optional.get() : null; + public static void writeToStore(final TestActorRef shard, final YangInstanceIdentifier id, + final NormalizedNode node) throws InterruptedException, ExecutionException { + writeToStore(shard.underlyingActor().getDataStore(), id, node); + } - transaction.close(); + public static void writeToStore(final ShardDataTree store, final YangInstanceIdentifier id, + final NormalizedNode node) throws InterruptedException, ExecutionException { + final ReadWriteShardDataTreeTransaction transaction = store.newReadWriteTransaction("writeToStore", null); - return node; + transaction.getSnapshot().write(id, node); + final ShardDataTreeCohort cohort = transaction.ready(); + cohort.canCommit().get(); + cohort.preCommit().get(); + cohort.commit(); } - public static void writeToStore(final TestActorRef shard, final YangInstanceIdentifier id, - final NormalizedNode node) throws ExecutionException, InterruptedException { - writeToStore(shard.underlyingActor().getDataStore(), id, node); + public static void mergeToStore(final ShardDataTree store, final YangInstanceIdentifier id, + final NormalizedNode node) throws InterruptedException, ExecutionException { + final ReadWriteShardDataTreeTransaction transaction = store.newReadWriteTransaction("writeToStore", null); + + transaction.getSnapshot().merge(id, node); + final ShardDataTreeCohort cohort = transaction.ready(); + cohort.canCommit().get(); + cohort.preCommit().get(); + cohort.commit(); } - public static void writeToStore(final InMemoryDOMDataStore store, final YangInstanceIdentifier id, - final NormalizedNode node) throws ExecutionException, InterruptedException { - DOMStoreWriteTransaction transaction = store.newWriteOnlyTransaction(); + public static void writeToStore(final DataTree store, final YangInstanceIdentifier id, + final NormalizedNode node) throws DataValidationFailedException { + final DataTreeModification transaction = store.takeSnapshot().newModification(); transaction.write(id, node); - - DOMStoreThreePhaseCommitCohort commitCohort = transaction.ready(); - commitCohort.preCommit().get(); - commitCohort.commit().get(); + transaction.ready(); + store.validate(transaction); + final DataTreeCandidate candidate = store.prepare(transaction); + store.commit(candidate); } @SuppressWarnings("serial")