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%2FShardRecoveryCoordinatorTest.java;h=31026fd79ff273626a6bf6dc0a6af6af49eef0e1;hp=f14bb3de765e73afb771755542031b3120190a71;hb=HEAD;hpb=2b0ddf57e4d3d2e78637a6762ae6b5a05128a3a1 diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/ShardRecoveryCoordinatorTest.java b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/ShardRecoveryCoordinatorTest.java index f14bb3de76..996aa86636 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/ShardRecoveryCoordinatorTest.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/ShardRecoveryCoordinatorTest.java @@ -5,167 +5,121 @@ * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ - package org.opendaylight.controller.cluster.datastore; -import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; -import com.google.common.base.Optional; + import java.io.IOException; +import java.util.Optional; import org.junit.Before; import org.junit.Test; -import org.opendaylight.controller.cluster.datastore.modification.ModificationPayload; -import org.opendaylight.controller.cluster.datastore.modification.MutableCompositeModification; -import org.opendaylight.controller.cluster.datastore.modification.WriteModification; -import org.opendaylight.controller.cluster.datastore.utils.SerializationUtils; -import org.opendaylight.controller.cluster.raft.protobuff.client.messages.CompositeModificationByteStringPayload; -import org.opendaylight.controller.cluster.raft.protobuff.client.messages.CompositeModificationPayload; +import org.mockito.Mockito; +import org.opendaylight.controller.cluster.datastore.persisted.CommitTransactionPayload; +import org.opendaylight.controller.cluster.datastore.persisted.MetadataShardDataTreeSnapshot; +import org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState; import org.opendaylight.controller.md.cluster.datastore.model.CarsModel; import org.opendaylight.controller.md.cluster.datastore.model.PeopleModel; import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper; 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.DataTreeCandidateTip; -import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification; -import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot; -import org.opendaylight.yangtools.yang.data.api.schema.tree.TipProducingDataTree; -import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory; -import org.opendaylight.yangtools.yang.data.impl.schema.tree.SchemaValidationFailedException; -import org.opendaylight.yangtools.yang.model.api.SchemaContext; +import org.opendaylight.yangtools.yang.data.tree.api.DataTree; +import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidate; +import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration; +import org.opendaylight.yangtools.yang.data.tree.api.DataTreeModification; +import org.opendaylight.yangtools.yang.data.tree.api.DataTreeSnapshot; +import org.opendaylight.yangtools.yang.data.tree.api.DataValidationFailedException; +import org.opendaylight.yangtools.yang.data.tree.api.SchemaValidationFailedException; +import org.opendaylight.yangtools.yang.data.tree.api.TreeType; +import org.opendaylight.yangtools.yang.data.tree.impl.di.InMemoryDataTreeFactory; +import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext; +import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class ShardRecoveryCoordinatorTest { +public class ShardRecoveryCoordinatorTest extends AbstractTest { + private static final Logger FOO_LOGGER = LoggerFactory.getLogger("foo"); private ShardDataTree peopleDataTree; - private SchemaContext peopleSchemaContext; - private SchemaContext carsSchemaContext; + private EffectiveModelContext peopleSchemaContext; + private EffectiveModelContext carsSchemaContext; + private ShardRecoveryCoordinator coordinator; @Before - public void setUp(){ + public void setUp() { peopleSchemaContext = SchemaContextHelper.select(SchemaContextHelper.PEOPLE_YANG); carsSchemaContext = SchemaContextHelper.select(SchemaContextHelper.CARS_YANG); - peopleDataTree = new ShardDataTree(peopleSchemaContext); - } + final Shard mockShard = Mockito.mock(Shard.class); - @Test - public void testAppendRecoveredLogEntryDataTreeCandidatePayload(){ - ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree, peopleSchemaContext, "foobar", LoggerFactory.getLogger("foo")); + peopleDataTree = new ShardDataTree(mockShard, peopleSchemaContext, TreeType.OPERATIONAL); + coordinator = ShardRecoveryCoordinator.create(peopleDataTree, "foobar", FOO_LOGGER); coordinator.startLogRecoveryBatch(10); - try { - coordinator.appendRecoveredLogEntry(DataTreeCandidatePayload.create(createCar())); - } catch(SchemaValidationFailedException e){ - fail("SchemaValidationFailedException should not happen if pruning is done"); - } - - coordinator.applyCurrentLogRecoveryBatch(); } @Test - public void testAppendRecoveredLogEntryModificationPayload() throws IOException { - ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree, peopleSchemaContext, "foobar", LoggerFactory.getLogger("foo")); - coordinator.startLogRecoveryBatch(10); + public void testAppendRecoveredLogEntryCommitTransactionPayload() throws IOException, + DataValidationFailedException { try { - MutableCompositeModification modification = new MutableCompositeModification((short) 1); - modification.addModification(new WriteModification(CarsModel.BASE_PATH, CarsModel.create())); - coordinator.appendRecoveredLogEntry(new ModificationPayload(modification)); - } catch(SchemaValidationFailedException e){ + coordinator.appendRecoveredLogEntry(CommitTransactionPayload.create(nextTransactionId(), createCar())); + } catch (final SchemaValidationFailedException e) { fail("SchemaValidationFailedException should not happen if pruning is done"); } - } - @Test - public void testAppendRecoveredLogEntryCompositeModificationPayload() throws IOException { - ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree, peopleSchemaContext, "foobar", LoggerFactory.getLogger("foo")); - coordinator.startLogRecoveryBatch(10); - try { - MutableCompositeModification modification = new MutableCompositeModification((short) 1); - modification.addModification(new WriteModification(CarsModel.BASE_PATH, CarsModel.create())); - coordinator.appendRecoveredLogEntry(new CompositeModificationPayload(modification.toSerializable())); - } catch(SchemaValidationFailedException e){ - fail("SchemaValidationFailedException should not happen if pruning is done"); - } - } - - @Test - public void testAppendRecoveredLogEntryCompositeModificationByteStringPayload() throws IOException { - ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree, peopleSchemaContext, "foobar", LoggerFactory.getLogger("foo")); - coordinator.startLogRecoveryBatch(10); - try { - MutableCompositeModification modification = new MutableCompositeModification((short) 1); - modification.addModification(new WriteModification(CarsModel.BASE_PATH, CarsModel.create())); - coordinator.appendRecoveredLogEntry(new CompositeModificationByteStringPayload(modification.toSerializable())); - } catch(SchemaValidationFailedException e){ - fail("SchemaValidationFailedException should not happen if pruning is done"); - } - - assertEquals(false, readCars(peopleDataTree).isPresent()); + coordinator.applyCurrentLogRecoveryBatch(); } @Test - public void testApplyRecoverySnapshot(){ - ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree , peopleSchemaContext, "foobar", LoggerFactory.getLogger("foo")); - coordinator.startLogRecoveryBatch(10); - + public void testApplyRecoverySnapshot() throws DataValidationFailedException { coordinator.applyRecoverySnapshot(createSnapshot()); - assertEquals(false, readCars(peopleDataTree).isPresent()); - assertEquals(true, readPeople(peopleDataTree).isPresent()); + assertFalse(readCars(peopleDataTree).isPresent()); + assertTrue(readPeople(peopleDataTree).isPresent()); } @Test - public void testApplyCurrentLogRecoveryBatch(){ - ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree, peopleSchemaContext, "foobar", LoggerFactory.getLogger("foo")); - coordinator.startLogRecoveryBatch(10); - + public void testApplyCurrentLogRecoveryBatch() { try { coordinator.applyCurrentLogRecoveryBatch(); - } catch(IllegalArgumentException e){ + } catch (final IllegalArgumentException e) { fail("IllegalArgumentException should not happen - if the pruning modification delegate is passed"); } } - private DataTreeCandidateTip createCar(){ - TipProducingDataTree dataTree = InMemoryDataTreeFactory.getInstance().create(); - dataTree.setSchemaContext(carsSchemaContext); + private DataTreeCandidate createCar() throws DataValidationFailedException { + final DataTree dataTree = new InMemoryDataTreeFactory().create( + DataTreeConfiguration.DEFAULT_OPERATIONAL, carsSchemaContext); - DataTreeSnapshot snapshot = dataTree.takeSnapshot(); + final DataTreeSnapshot snapshot = dataTree.takeSnapshot(); - DataTreeModification modification = snapshot.newModification(); + final DataTreeModification modification = snapshot.newModification(); modification.merge(CarsModel.BASE_PATH, CarsModel.create()); - + modification.ready(); return dataTree.prepare(modification); } - private Optional> readCars(ShardDataTree shardDataTree){ - TipProducingDataTree dataTree = shardDataTree.getDataTree(); - dataTree.setSchemaContext(peopleSchemaContext); + private Optional readCars(final ShardDataTree shardDataTree) { + final DataTree dataTree = shardDataTree.getDataTree(); + // FIXME: this should not be called here + dataTree.setEffectiveModelContext(peopleSchemaContext); - DataTreeSnapshot snapshot = dataTree.takeSnapshot(); - - DataTreeModification modification = snapshot.newModification(); - - return modification.readNode(CarsModel.BASE_PATH); + return shardDataTree.readNode(CarsModel.BASE_PATH); } - private Optional> readPeople(ShardDataTree shardDataTree){ - TipProducingDataTree dataTree = shardDataTree.getDataTree(); - dataTree.setSchemaContext(peopleSchemaContext); - - DataTreeSnapshot snapshot = dataTree.takeSnapshot(); + private Optional readPeople(final ShardDataTree shardDataTree) { + final DataTree dataTree = shardDataTree.getDataTree(); + // FIXME: this should not be called here + dataTree.setEffectiveModelContext(peopleSchemaContext); - DataTreeModification modification = snapshot.newModification(); - - return modification.readNode(PeopleModel.BASE_PATH); + return shardDataTree.readNode(PeopleModel.BASE_PATH); } - - - private byte[] createSnapshot(){ - TipProducingDataTree dataTree = InMemoryDataTreeFactory.getInstance().create(); - dataTree.setSchemaContext(SchemaContextHelper.select(SchemaContextHelper.CARS_YANG, SchemaContextHelper.PEOPLE_YANG)); + private static ShardSnapshotState createSnapshot() throws DataValidationFailedException { + final DataTree dataTree = new InMemoryDataTreeFactory().create( + DataTreeConfiguration.DEFAULT_OPERATIONAL, SchemaContextHelper.select(SchemaContextHelper.CARS_YANG, + SchemaContextHelper.PEOPLE_YANG)); DataTreeSnapshot snapshot = dataTree.takeSnapshot(); @@ -173,21 +127,10 @@ public class ShardRecoveryCoordinatorTest { modification.merge(CarsModel.BASE_PATH, CarsModel.create()); modification.merge(PeopleModel.BASE_PATH, PeopleModel.create()); + modification.ready(); + dataTree.commit(dataTree.prepare(modification)); - DataTreeCandidateTip prepare = dataTree.prepare(modification); - - dataTree.commit(prepare); - - snapshot = dataTree.takeSnapshot(); - - modification = snapshot.newModification(); - - Optional> optional = modification.readNode(YangInstanceIdentifier.EMPTY); - - byte[] bytes = SerializationUtils.serializeNormalizedNode(optional.get()); - - return bytes; - - + return new ShardSnapshotState(new MetadataShardDataTreeSnapshot(dataTree.takeSnapshot().readNode( + YangInstanceIdentifier.of()).orElseThrow())); } -} \ No newline at end of file +}