Search in sources :

Example 21 with DataTree

use of org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree in project controller by opendaylight.

the class ReadyLocalTransactionSerializerTest method testToAndFromBinary.

@Test
public void testToAndFromBinary() throws NotSerializableException {
    DataTree dataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL, TestModel.createTestContext());
    DataTreeModification modification = dataTree.takeSnapshot().newModification();
    ContainerNode writeData = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
    new WriteModification(TestModel.TEST_PATH, writeData).apply(modification);
    MapNode mergeData = ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build();
    new MergeModification(TestModel.OUTER_LIST_PATH, mergeData).apply(modification);
    TransactionIdentifier txId = nextTransactionId();
    ReadyLocalTransaction readyMessage = new ReadyLocalTransaction(txId, modification, true);
    final ExtendedActorSystem system = (ExtendedActorSystem) ExtendedActorSystem.create("test");
    final Object deserialized;
    try {
        final ReadyLocalTransactionSerializer serializer = new ReadyLocalTransactionSerializer(system);
        final byte[] bytes = serializer.toBinary(readyMessage);
        deserialized = serializer.fromBinary(bytes, ReadyLocalTransaction.class);
    } finally {
        JavaTestKit.shutdownActorSystem(system);
    }
    assertNotNull("fromBinary returned null", deserialized);
    assertEquals("fromBinary return type", BatchedModifications.class, deserialized.getClass());
    BatchedModifications batched = (BatchedModifications) deserialized;
    assertEquals("getTransactionID", txId, batched.getTransactionId());
    assertEquals("getVersion", DataStoreVersions.CURRENT_VERSION, batched.getVersion());
    List<Modification> batchedMods = batched.getModifications();
    assertEquals("getModifications size", 2, batchedMods.size());
    Modification mod = batchedMods.get(0);
    assertEquals("Modification type", WriteModification.class, mod.getClass());
    assertEquals("Modification getPath", TestModel.TEST_PATH, ((WriteModification) mod).getPath());
    assertEquals("Modification getData", writeData, ((WriteModification) mod).getData());
    mod = batchedMods.get(1);
    assertEquals("Modification type", MergeModification.class, mod.getClass());
    assertEquals("Modification getPath", TestModel.OUTER_LIST_PATH, ((MergeModification) mod).getPath());
    assertEquals("Modification getData", mergeData, ((MergeModification) mod).getData());
}
Also used : DataTreeModification(org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification) WriteModification(org.opendaylight.controller.cluster.datastore.modification.WriteModification) DataTreeModification(org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification) WriteModification(org.opendaylight.controller.cluster.datastore.modification.WriteModification) Modification(org.opendaylight.controller.cluster.datastore.modification.Modification) MergeModification(org.opendaylight.controller.cluster.datastore.modification.MergeModification) MapNode(org.opendaylight.yangtools.yang.data.api.schema.MapNode) InMemoryDataTreeFactory(org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory) MergeModification(org.opendaylight.controller.cluster.datastore.modification.MergeModification) ExtendedActorSystem(akka.actor.ExtendedActorSystem) DataTree(org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree) TransactionIdentifier(org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) Test(org.junit.Test) AbstractTest(org.opendaylight.controller.cluster.datastore.AbstractTest)

Example 22 with DataTree

use of org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree in project controller by opendaylight.

the class AbstractDataStoreClientBehaviorTest method testGetConnection.

@Test
public void testGetConnection() throws Exception {
    // set up data tree mock
    final CursorAwareDataTreeModification modification = mock(CursorAwareDataTreeModification.class);
    when(modification.readNode(YangInstanceIdentifier.EMPTY)).thenReturn(Optional.empty());
    final DataTreeSnapshot snapshot = mock(DataTreeSnapshot.class);
    when(snapshot.newModification()).thenReturn(modification);
    final DataTree dataTree = mock(DataTree.class);
    when(dataTree.takeSnapshot()).thenReturn(snapshot);
    final TestProbe backendProbe = new TestProbe(system, "backend");
    final long shard = 0L;
    behavior.createTransaction().read(YangInstanceIdentifier.EMPTY);
    final AbstractClientConnection<ShardBackendInfo> connection = behavior.getConnection(shard);
    // check cached connection for same shard
    Assert.assertSame(connection, behavior.getConnection(shard));
    final ConnectClientRequest connectClientRequest = actorContextProbe.expectMsgClass(ConnectClientRequest.class);
    Assert.assertEquals(CLIENT_ID, connectClientRequest.getTarget());
    final long sequence = 0L;
    Assert.assertEquals(sequence, connectClientRequest.getSequence());
    actorContextProbe.reply(new ConnectClientSuccess(CLIENT_ID, sequence, backendProbe.ref(), Collections.emptyList(), dataTree, 3));
    Assert.assertEquals(clientActorProbe.ref(), connection.localActor());
    // capture and execute command passed to client context
    final InternalCommand<ShardBackendInfo> command = clientActorProbe.expectMsgClass(InternalCommand.class);
    command.execute(behavior);
    // check, whether command was reaplayed
    verify(modification).readNode(YangInstanceIdentifier.EMPTY);
}
Also used : ConnectClientSuccess(org.opendaylight.controller.cluster.access.commands.ConnectClientSuccess) ConnectClientRequest(org.opendaylight.controller.cluster.access.commands.ConnectClientRequest) DataTree(org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree) TestProbe(akka.testkit.TestProbe) DataTreeSnapshot(org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot) CursorAwareDataTreeModification(org.opendaylight.yangtools.yang.data.api.schema.tree.CursorAwareDataTreeModification) Test(org.junit.Test)

Example 23 with DataTree

use of org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree in project controller by opendaylight.

the class DistributedEntityOwnershipService method getOwnershipState.

@Override
public Optional<EntityOwnershipState> getOwnershipState(final DOMEntity forEntity) {
    Preconditions.checkNotNull(forEntity, "forEntity cannot be null");
    DataTree dataTree = getLocalEntityOwnershipShardDataTree();
    if (dataTree == null) {
        return Optional.absent();
    }
    java.util.Optional<NormalizedNode<?, ?>> entityNode = dataTree.takeSnapshot().readNode(entityPath(forEntity.getType(), forEntity.getIdentifier()));
    if (!entityNode.isPresent()) {
        return Optional.absent();
    }
    // Check if there are any candidates, if there are none we do not really have ownership state
    final MapEntryNode entity = (MapEntryNode) entityNode.get();
    final java.util.Optional<DataContainerChild<? extends PathArgument, ?>> optionalCandidates = entity.getChild(CANDIDATE_NODE_ID);
    final boolean hasCandidates = optionalCandidates.isPresent() && ((MapNode) optionalCandidates.get()).getValue().size() > 0;
    if (!hasCandidates) {
        return Optional.absent();
    }
    MemberName localMemberName = context.getCurrentMemberName();
    java.util.Optional<DataContainerChild<? extends PathArgument, ?>> ownerLeaf = entity.getChild(ENTITY_OWNER_NODE_ID);
    String owner = ownerLeaf.isPresent() ? ownerLeaf.get().getValue().toString() : null;
    boolean hasOwner = !Strings.isNullOrEmpty(owner);
    boolean isOwner = hasOwner && localMemberName.getName().equals(owner);
    return Optional.of(EntityOwnershipState.from(isOwner, hasOwner));
}
Also used : DataTree(org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree) GetShardDataTree(org.opendaylight.controller.cluster.datastore.messages.GetShardDataTree) DataContainerChild(org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild) PathArgument(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument) MemberName(org.opendaylight.controller.cluster.access.concepts.MemberName) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) MapEntryNode(org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode)

Example 24 with DataTree

use of org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree in project controller by opendaylight.

the class ShardRecoveryCoordinatorTest method createCar.

private DataTreeCandidate createCar() {
    final DataTree dataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL, carsSchemaContext);
    final DataTreeSnapshot snapshot = dataTree.takeSnapshot();
    final DataTreeModification modification = snapshot.newModification();
    modification.merge(CarsModel.BASE_PATH, CarsModel.create());
    modification.ready();
    return dataTree.prepare(modification);
}
Also used : DataTreeModification(org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification) DataTree(org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree) DataTreeSnapshot(org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot) MetadataShardDataTreeSnapshot(org.opendaylight.controller.cluster.datastore.persisted.MetadataShardDataTreeSnapshot) InMemoryDataTreeFactory(org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory)

Example 25 with DataTree

use of org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree in project controller by opendaylight.

the class ShardTest method testCommitWhenTransactionHasModifications.

private void testCommitWhenTransactionHasModifications(final boolean readWrite) throws Exception {
    new ShardTestKit(getSystem()) {

        {
            final DataTree dataTree = createDelegatingMockDataTree();
            final TestActorRef<Shard> shard = actorFactory.createTestActor(newShardBuilder().dataTree(dataTree).props().withDispatcher(Dispatchers.DefaultDispatcherId()), "testCommitWhenTransactionHasModifications-" + readWrite);
            waitUntilLeader(shard);
            final FiniteDuration duration = duration("5 seconds");
            final TransactionIdentifier transactionID = nextTransactionId();
            if (readWrite) {
                shard.tell(prepareForwardedReadyTransaction(shard, transactionID, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME), false), getRef());
            } else {
                shard.tell(prepareBatchedModifications(transactionID, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME), false), getRef());
            }
            expectMsgClass(duration, ReadyTransactionReply.class);
            // Send the CanCommitTransaction message.
            shard.tell(new CanCommitTransaction(transactionID, CURRENT_VERSION).toSerializable(), getRef());
            final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply.fromSerializable(expectMsgClass(duration, CanCommitTransactionReply.class));
            assertEquals("Can commit", true, canCommitReply.getCanCommit());
            shard.tell(new CommitTransaction(transactionID, CURRENT_VERSION).toSerializable(), getRef());
            expectMsgClass(duration, CommitTransactionReply.class);
            final InOrder inOrder = inOrder(dataTree);
            inOrder.verify(dataTree).validate(any(DataTreeModification.class));
            inOrder.verify(dataTree).prepare(any(DataTreeModification.class));
            inOrder.verify(dataTree).commit(any(DataTreeCandidate.class));
            // Purge request is scheduled as asynchronous, wait for two heartbeats to let it propagate into
            // the journal
            Thread.sleep(HEARTBEAT_MILLIS * 2);
            shard.tell(Shard.GET_SHARD_MBEAN_MESSAGE, getRef());
            final ShardStats shardStats = expectMsgClass(duration, ShardStats.class);
            // Use MBean for verification
            // Committed transaction count should increase as usual
            assertEquals(1, shardStats.getCommittedTransactionsCount());
            // Commit index should advance as we do not have an empty
            // modification
            assertEquals(1, shardStats.getCommitIndex());
        }
    };
}
Also used : DataTreeModification(org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification) ShardStats(org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardStats) InOrder(org.mockito.InOrder) FiniteDuration(scala.concurrent.duration.FiniteDuration) CanCommitTransactionReply(org.opendaylight.controller.cluster.datastore.messages.CanCommitTransactionReply) DataTreeCandidate(org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate) CanCommitTransaction(org.opendaylight.controller.cluster.datastore.messages.CanCommitTransaction) CommitTransaction(org.opendaylight.controller.cluster.datastore.messages.CommitTransaction) DataTree(org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree) TransactionIdentifier(org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier) CanCommitTransaction(org.opendaylight.controller.cluster.datastore.messages.CanCommitTransaction)

Aggregations

DataTree (org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree)33 Test (org.junit.Test)21 DataTreeModification (org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification)19 InMemoryDataTreeFactory (org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory)14 ActorRef (akka.actor.ActorRef)7 TransactionIdentifier (org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier)7 MetadataShardDataTreeSnapshot (org.opendaylight.controller.cluster.datastore.persisted.MetadataShardDataTreeSnapshot)7 ShardSnapshotState (org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState)6 AddressFromURIString (akka.actor.AddressFromURIString)5 DataTreeSnapshot (org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot)5 FiniteDuration (scala.concurrent.duration.FiniteDuration)5 TestActorRef (akka.testkit.TestActorRef)4 CanCommitTransaction (org.opendaylight.controller.cluster.datastore.messages.CanCommitTransaction)4 CanCommitTransactionReply (org.opendaylight.controller.cluster.datastore.messages.CanCommitTransactionReply)4 GetShardDataTree (org.opendaylight.controller.cluster.datastore.messages.GetShardDataTree)4 LocalPrimaryShardFound (org.opendaylight.controller.cluster.datastore.messages.LocalPrimaryShardFound)4 ContainerNode (org.opendaylight.yangtools.yang.data.api.schema.ContainerNode)4 TestKit (akka.testkit.javadsl.TestKit)3 Timeout (akka.util.Timeout)3 InOrder (org.mockito.InOrder)3