Search in sources :

Example 21 with NormalizedNode

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

the class TransactionProxyTest method testWriteAfterAsyncRead.

@Test
@SuppressWarnings("checkstyle:IllegalCatch")
public void testWriteAfterAsyncRead() throws Exception {
    ActorRef actorRef = setupActorContextWithoutInitialCreateTransaction(getSystem(), DefaultShardStrategy.DEFAULT_SHARD);
    Promise<Object> createTxPromise = akka.dispatch.Futures.promise();
    doReturn(createTxPromise).when(mockActorContext).executeOperationAsync(eq(getSystem().actorSelection(actorRef.path())), eqCreateTransaction(memberName, READ_WRITE), any(Timeout.class));
    doReturn(readDataReply(null)).when(mockActorContext).executeOperationAsync(eq(actorSelection(actorRef)), eqReadData(), any(Timeout.class));
    expectBatchedModificationsReady(actorRef);
    final NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
    final TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
    final CountDownLatch readComplete = new CountDownLatch(1);
    final AtomicReference<Throwable> caughtEx = new AtomicReference<>();
    com.google.common.util.concurrent.Futures.addCallback(transactionProxy.read(TestModel.TEST_PATH), new FutureCallback<Optional<NormalizedNode<?, ?>>>() {

        @Override
        public void onSuccess(final Optional<NormalizedNode<?, ?>> result) {
            try {
                transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
            } catch (Exception e) {
                caughtEx.set(e);
            } finally {
                readComplete.countDown();
            }
        }

        @Override
        public void onFailure(final Throwable failure) {
            caughtEx.set(failure);
            readComplete.countDown();
        }
    }, MoreExecutors.directExecutor());
    createTxPromise.success(createTransactionReply(actorRef, DataStoreVersions.CURRENT_VERSION));
    Uninterruptibles.awaitUninterruptibly(readComplete, 5, TimeUnit.SECONDS);
    final Throwable t = caughtEx.get();
    if (t != null) {
        Throwables.propagateIfPossible(t, Exception.class);
        throw new RuntimeException(t);
    }
    // This sends the batched modification.
    transactionProxy.ready();
    verifyOneBatchedModification(actorRef, new WriteModification(TestModel.TEST_PATH, nodeToWrite), true);
}
Also used : WriteModification(org.opendaylight.controller.cluster.datastore.modification.WriteModification) Optional(com.google.common.base.Optional) ActorRef(akka.actor.ActorRef) Timeout(akka.util.Timeout) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ReadFailedException(org.opendaylight.mdsal.common.api.ReadFailedException) NotInitializedException(org.opendaylight.controller.cluster.datastore.exceptions.NotInitializedException) PrimaryNotFoundException(org.opendaylight.controller.cluster.datastore.exceptions.PrimaryNotFoundException) NoShardLeaderException(org.opendaylight.controller.cluster.datastore.exceptions.NoShardLeaderException) TimeoutException(org.opendaylight.controller.cluster.datastore.exceptions.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) NormalizedNodeAggregatorTest(org.opendaylight.controller.cluster.datastore.utils.NormalizedNodeAggregatorTest) Test(org.junit.Test)

Example 22 with NormalizedNode

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

the class ShardSnapshotActorTest method testSerializeSnapshot.

private static void testSerializeSnapshot(final String testName, final ShardDataTreeSnapshot snapshot, final boolean withInstallSnapshot) throws Exception {
    new TestKit(getSystem()) {

        {
            final ActorRef snapshotActor = getSystem().actorOf(ShardSnapshotActor.props(), testName);
            watch(snapshotActor);
            final NormalizedNode<?, ?> expectedRoot = snapshot.getRootNode().get();
            ByteArrayOutputStream installSnapshotStream = withInstallSnapshot ? new ByteArrayOutputStream() : null;
            ShardSnapshotActor.requestSnapshot(snapshotActor, snapshot, Optional.ofNullable(installSnapshotStream), getRef());
            final CaptureSnapshotReply reply = expectMsgClass(duration("3 seconds"), CaptureSnapshotReply.class);
            assertNotNull("getSnapshotState is null", reply.getSnapshotState());
            assertEquals("SnapshotState type", ShardSnapshotState.class, reply.getSnapshotState().getClass());
            assertEquals("Snapshot", snapshot, ((ShardSnapshotState) reply.getSnapshotState()).getSnapshot());
            if (installSnapshotStream != null) {
                final ShardDataTreeSnapshot deserialized;
                try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(installSnapshotStream.toByteArray()))) {
                    deserialized = ShardDataTreeSnapshot.deserialize(in);
                }
                assertEquals("Deserialized snapshot type", snapshot.getClass(), deserialized.getClass());
                final Optional<NormalizedNode<?, ?>> maybeNode = deserialized.getRootNode();
                assertEquals("isPresent", true, maybeNode.isPresent());
                assertEquals("Root node", expectedRoot, maybeNode.get());
            }
        }
    };
}
Also used : CaptureSnapshotReply(org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply) ShardDataTreeSnapshot(org.opendaylight.controller.cluster.datastore.persisted.ShardDataTreeSnapshot) MetadataShardDataTreeSnapshot(org.opendaylight.controller.cluster.datastore.persisted.MetadataShardDataTreeSnapshot) ByteArrayInputStream(java.io.ByteArrayInputStream) ActorRef(akka.actor.ActorRef) TestKit(akka.testkit.javadsl.TestKit) ByteArrayOutputStream(java.io.ByteArrayOutputStream) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) ObjectInputStream(java.io.ObjectInputStream)

Example 23 with NormalizedNode

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

the class LocalTransactionContextTest method testRead.

@Test
public void testRead() {
    YangInstanceIdentifier yangInstanceIdentifier = YangInstanceIdentifier.EMPTY;
    NormalizedNode<?, ?> normalizedNode = mock(NormalizedNode.class);
    doReturn(Futures.immediateCheckedFuture(Optional.of(normalizedNode))).when(readWriteTransaction).read(yangInstanceIdentifier);
    localTransactionContext.executeRead(new ReadData(yangInstanceIdentifier, DataStoreVersions.CURRENT_VERSION), SettableFuture.<Optional<NormalizedNode<?, ?>>>create());
    verify(readWriteTransaction).read(yangInstanceIdentifier);
}
Also used : NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) ReadData(org.opendaylight.controller.cluster.datastore.messages.ReadData) Test(org.junit.Test)

Example 24 with NormalizedNode

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

the class NormalizedNodePrunerTest method countNodes.

private static int countNodes(final NormalizedNode<?, ?> normalizedNode, final String namespaceFilter) {
    if (normalizedNode == null) {
        return 0;
    }
    final AtomicInteger count = new AtomicInteger();
    new NormalizedNodeNavigator((level, parentPath, normalizedNode1) -> {
        if (!(normalizedNode1.getIdentifier() instanceof AugmentationIdentifier)) {
            if (normalizedNode1.getIdentifier().getNodeType().getNamespace().toString().contains(namespaceFilter)) {
                count.incrementAndGet();
            }
        }
    }).navigate(YangInstanceIdentifier.EMPTY.toString(), normalizedNode);
    return count.get();
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) NodeIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier) LeafSetEntryNode(org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode) SchemaContext(org.opendaylight.yangtools.yang.model.api.SchemaContext) AnyXmlNode(org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode) MockitoAnnotations(org.mockito.MockitoAnnotations) TestModel(org.opendaylight.controller.cluster.datastore.util.TestModel) NormalizedNodeWriter(org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MapNode(org.opendaylight.yangtools.yang.data.api.schema.MapNode) ImmutableLeafSetNodeBuilder(org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder) ImmutableNodes.mapEntry(org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapEntry) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) ImmutableLeafSetEntryNodeBuilder(org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetEntryNodeBuilder) NormalizedNodeNavigator(org.opendaylight.controller.cluster.datastore.node.utils.NormalizedNodeNavigator) Builders(org.opendaylight.yangtools.yang.data.impl.schema.Builders) Before(org.junit.Before) LeafNode(org.opendaylight.yangtools.yang.data.api.schema.LeafNode) ImmutableNodes.mapNodeBuilder(org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapNodeBuilder) Assert.assertTrue(org.junit.Assert.assertTrue) NodeWithValue(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue) IOException(java.io.IOException) Test(org.junit.Test) Sets(com.google.common.collect.Sets) ImmutableNodes.mapEntryBuilder(org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapEntryBuilder) Assert.assertNull(org.junit.Assert.assertNull) ImmutableNodes(org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes) Assert(org.junit.Assert) Assert.assertEquals(org.junit.Assert.assertEquals) AugmentationIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) Mockito.mock(org.mockito.Mockito.mock) NormalizedNodeNavigator(org.opendaylight.controller.cluster.datastore.node.utils.NormalizedNodeNavigator) AugmentationIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 25 with NormalizedNode

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

the class InMemoryDOMStoreTreeChangePublisher method registerTreeChangeListener.

<L extends DOMDataTreeChangeListener> ListenerRegistration<L> registerTreeChangeListener(final YangInstanceIdentifier treeId, final L listener, final DataTreeSnapshot snapshot) {
    final AbstractDOMDataTreeChangeListenerRegistration<L> reg = registerTreeChangeListener(treeId, listener);
    final Optional<NormalizedNode<?, ?>> node = snapshot.readNode(YangInstanceIdentifier.EMPTY);
    if (node.isPresent()) {
        final DataTreeCandidate candidate = DataTreeCandidates.fromNormalizedNode(YangInstanceIdentifier.EMPTY, node.get());
        InMemoryDOMStoreTreeChangePublisher publisher = new InMemoryDOMStoreTreeChangePublisher(notificationManager);
        publisher.registerTreeChangeListener(treeId, listener);
        publisher.publishChange(candidate);
    }
    return reg;
}
Also used : DataTreeCandidate(org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)

Aggregations

NormalizedNode (org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)94 Test (org.junit.Test)55 YangInstanceIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)39 ContainerNode (org.opendaylight.yangtools.yang.data.api.schema.ContainerNode)18 Optional (com.google.common.base.Optional)15 MapEntryNode (org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode)11 DOMStoreReadTransaction (org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction)10 ReadFailedException (org.opendaylight.controller.md.sal.common.api.data.ReadFailedException)9 DOMStoreWriteTransaction (org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction)9 NodeIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier)9 ActorRef (akka.actor.ActorRef)8 InOrder (org.mockito.InOrder)8 DOMStoreReadWriteTransaction (org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction)8 DOMStoreThreePhaseCommitCohort (org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort)8 ExecutionException (java.util.concurrent.ExecutionException)7 ReadFailedException (org.opendaylight.mdsal.common.api.ReadFailedException)7 DataTreeCandidate (org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate)7 Map (java.util.Map)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)6