Search in sources :

Example 31 with NodeIdentifier

use of org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier in project controller by opendaylight.

the class DistributedShardedDOMDataTreeTest method testSingleNodeWritesAndRead.

@Test
public void testSingleNodeWritesAndRead() throws Exception {
    initEmptyDatastores();
    final DistributedShardRegistration shardRegistration = waitOnAsyncTask(leaderShardFactory.createDistributedShard(TEST_ID, Lists.newArrayList(AbstractTest.MEMBER_NAME)), DistributedShardedDOMDataTree.SHARD_FUTURE_TIMEOUT_DURATION);
    leaderTestKit.waitUntilLeader(leaderDistributedDataStore.getActorContext(), ClusterUtils.getCleanShardName(TEST_ID.getRootIdentifier()));
    final DOMDataTreeProducer producer = leaderShardFactory.createProducer(Collections.singleton(TEST_ID));
    final DOMDataTreeCursorAwareTransaction tx = producer.createTransaction(true);
    final DOMDataTreeWriteCursor cursor = tx.createCursor(TEST_ID);
    Assert.assertNotNull(cursor);
    final YangInstanceIdentifier nameId = YangInstanceIdentifier.builder(TestModel.TEST_PATH).node(TestModel.NAME_QNAME).build();
    final LeafNode<String> valueToCheck = ImmutableLeafNodeBuilder.<String>create().withNodeIdentifier(new NodeIdentifier(TestModel.NAME_QNAME)).withValue("Test Value").build();
    LOG.debug("Writing data {} at {}, cursor {}", nameId.getLastPathArgument(), valueToCheck, cursor);
    cursor.write(nameId.getLastPathArgument(), valueToCheck);
    cursor.close();
    LOG.debug("Got to pre submit");
    tx.submit().checkedGet();
    final DOMDataTreeListener mockedDataTreeListener = mock(DOMDataTreeListener.class);
    doNothing().when(mockedDataTreeListener).onDataTreeChanged(anyCollection(), anyMap());
    leaderShardFactory.registerListener(mockedDataTreeListener, Collections.singletonList(TEST_ID), true, Collections.emptyList());
    verify(mockedDataTreeListener, timeout(1000).times(1)).onDataTreeChanged(captorForChanges.capture(), captorForSubtrees.capture());
    final List<Collection<DataTreeCandidate>> capturedValue = captorForChanges.getAllValues();
    final java.util.Optional<NormalizedNode<?, ?>> dataAfter = capturedValue.get(0).iterator().next().getRootNode().getDataAfter();
    final NormalizedNode<?, ?> expected = ImmutableContainerNodeBuilder.create().withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME)).withChild(valueToCheck).build();
    assertEquals(expected, dataAfter.get());
    verifyNoMoreInteractions(mockedDataTreeListener);
    final String shardName = ClusterUtils.getCleanShardName(TEST_ID.getRootIdentifier());
    LOG.debug("Creating distributed datastore client for shard {}", shardName);
    final ActorContext actorContext = leaderDistributedDataStore.getActorContext();
    final Props distributedDataStoreClientProps = SimpleDataStoreClientActor.props(actorContext.getCurrentMemberName(), "Shard-" + shardName, actorContext, shardName);
    final ActorRef clientActor = leaderSystem.actorOf(distributedDataStoreClientProps);
    final DataStoreClient distributedDataStoreClient = SimpleDataStoreClientActor.getDistributedDataStoreClient(clientActor, 30, TimeUnit.SECONDS);
    final ClientLocalHistory localHistory = distributedDataStoreClient.createLocalHistory();
    final ClientTransaction tx2 = localHistory.createTransaction();
    final CheckedFuture<Optional<NormalizedNode<?, ?>>, org.opendaylight.mdsal.common.api.ReadFailedException> read = tx2.read(YangInstanceIdentifier.EMPTY);
    final Optional<NormalizedNode<?, ?>> optional = read.checkedGet();
    tx2.abort();
    localHistory.close();
    shardRegistration.close().toCompletableFuture().get();
}
Also used : DistributedShardRegistration(org.opendaylight.controller.cluster.sharding.DistributedShardFactory.DistributedShardRegistration) DOMDataTreeWriteCursor(org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor) ActorRef(akka.actor.ActorRef) AddressFromURIString(akka.actor.AddressFromURIString) Props(akka.actor.Props) DataStoreClient(org.opendaylight.controller.cluster.databroker.actors.dds.DataStoreClient) DOMDataTreeProducer(org.opendaylight.mdsal.dom.api.DOMDataTreeProducer) DOMDataTreeCursorAwareTransaction(org.opendaylight.mdsal.dom.api.DOMDataTreeCursorAwareTransaction) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) Optional(com.google.common.base.Optional) ClientTransaction(org.opendaylight.controller.cluster.databroker.actors.dds.ClientTransaction) DOMDataTreeListener(org.opendaylight.mdsal.dom.api.DOMDataTreeListener) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) ActorContext(org.opendaylight.controller.cluster.datastore.utils.ActorContext) ClientLocalHistory(org.opendaylight.controller.cluster.databroker.actors.dds.ClientLocalHistory) NodeIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier) Collection(java.util.Collection) Matchers.anyCollection(org.mockito.Matchers.anyCollection) Test(org.junit.Test) AbstractTest(org.opendaylight.controller.cluster.datastore.AbstractTest)

Example 32 with NodeIdentifier

use of org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier in project controller by opendaylight.

the class PrefixedShardConfigWriter method doWrite.

private DOMStoreThreePhaseCommitCohort doWrite(final YangInstanceIdentifier path, final Collection<MemberName> replicas) {
    final ListNodeBuilder<Object, LeafSetEntryNode<Object>> replicaListBuilder = ImmutableLeafSetNodeBuilder.create().withNodeIdentifier(new NodeIdentifier(ClusterUtils.SHARD_REPLICA_QNAME));
    replicas.forEach(name -> replicaListBuilder.withChild(ImmutableLeafSetEntryNodeBuilder.create().withNodeIdentifier(new NodeWithValue<>(ClusterUtils.SHARD_REPLICA_QNAME, name.getName())).withValue(name.getName()).build()));
    final MapEntryNode newEntry = ImmutableMapEntryNodeBuilder.create().withNodeIdentifier(new NodeIdentifierWithPredicates(ClusterUtils.SHARD_LIST_QNAME, ClusterUtils.SHARD_PREFIX_QNAME, path)).withChild(ImmutableLeafNodeBuilder.create().withNodeIdentifier(new NodeIdentifier(ClusterUtils.SHARD_PREFIX_QNAME)).withValue(path).build()).withChild(ImmutableContainerNodeBuilder.create().withNodeIdentifier(new NodeIdentifier(ClusterUtils.SHARD_REPLICAS_QNAME)).withChild(replicaListBuilder.build()).build()).build();
    final ClientTransaction tx = history.createTransaction();
    final DOMDataTreeWriteCursor cursor = tx.openCursor();
    ClusterUtils.SHARD_LIST_PATH.getPathArguments().forEach(cursor::enter);
    cursor.write(newEntry.getIdentifier(), newEntry);
    cursor.close();
    return tx.ready();
}
Also used : LeafSetEntryNode(org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode) DOMDataTreeWriteCursor(org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor) NodeIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier) ClientTransaction(org.opendaylight.controller.cluster.databroker.actors.dds.ClientTransaction) MapEntryNode(org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode) NodeIdentifierWithPredicates(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates)

Example 33 with NodeIdentifier

use of org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier in project controller by opendaylight.

the class AbstractEntityOwnershipTest method getMapEntryNodeChild.

protected MapEntryNode getMapEntryNodeChild(final DataContainerNode<? extends PathArgument> parent, final QName childMap, final QName child, final Object key, final boolean expectPresent) {
    Optional<DataContainerChild<? extends PathArgument, ?>> childNode = parent.getChild(new NodeIdentifier(childMap));
    assertEquals("Missing " + childMap.toString(), true, childNode.isPresent());
    MapNode entityTypeMapNode = (MapNode) childNode.get();
    Optional<MapEntryNode> entityTypeEntry = entityTypeMapNode.getChild(new NodeIdentifierWithPredicates(childMap, child, key));
    if (expectPresent && !entityTypeEntry.isPresent()) {
        fail("Missing " + childMap.toString() + " entry for " + key + ". Actual: " + entityTypeMapNode.getValue());
    } else if (!expectPresent && entityTypeEntry.isPresent()) {
        fail("Found unexpected " + childMap.toString() + " entry for " + key);
    }
    return entityTypeEntry.isPresent() ? entityTypeEntry.get() : null;
}
Also used : DataContainerChild(org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild) NodeIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier) PathArgument(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument) MapNode(org.opendaylight.yangtools.yang.data.api.schema.MapNode) MapEntryNode(org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode) NodeIdentifierWithPredicates(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates)

Example 34 with NodeIdentifier

use of org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier in project controller by opendaylight.

the class NormalizedNodeStreamReaderWriterTest method testNormalizedNodeStreaming.

@Test
public void testNormalizedNodeStreaming() throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos));
    NormalizedNode<?, ?> testContainer = createTestContainer();
    nnout.writeNormalizedNode(testContainer);
    QName toaster = QName.create("http://netconfcentral.org/ns/toaster", "2009-11-20", "toaster");
    QName darknessFactor = QName.create("http://netconfcentral.org/ns/toaster", "2009-11-20", "darknessFactor");
    QName description = QName.create("http://netconfcentral.org/ns/toaster", "2009-11-20", "description");
    ContainerNode toasterNode = Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(toaster)).withChild(ImmutableNodes.leafNode(darknessFactor, "1000")).withChild(ImmutableNodes.leafNode(description, largeString(20))).build();
    ContainerNode toasterContainer = Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(SchemaContext.NAME)).withChild(toasterNode).build();
    nnout.writeNormalizedNode(toasterContainer);
    NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bos.toByteArray()));
    NormalizedNode<?, ?> node = nnin.readNormalizedNode();
    Assert.assertEquals(testContainer, node);
    node = nnin.readNormalizedNode();
    Assert.assertEquals(toasterContainer, node);
}
Also used : QName(org.opendaylight.yangtools.yang.common.QName) NodeIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 35 with NodeIdentifier

use of org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier in project controller by opendaylight.

the class NormalizedNodeStreamReaderWriterTest method testAnyXmlStreaming.

@Test
public void testAnyXmlStreaming() throws Exception {
    String xml = "<foo xmlns=\"http://www.w3.org/TR/html4/\" x=\"123\"><bar>one</bar><bar>two</bar></foo>";
    final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    Node xmlNode = factory.newDocumentBuilder().parse(new InputSource(new StringReader(xml))).getDocumentElement();
    assertEquals("http://www.w3.org/TR/html4/", xmlNode.getNamespaceURI());
    NormalizedNode<?, ?> anyXmlContainer = ImmutableContainerNodeBuilder.create().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME)).withChild(Builders.anyXmlBuilder().withNodeIdentifier(new NodeIdentifier(TestModel.ANY_XML_QNAME)).withValue(new DOMSource(xmlNode)).build()).build();
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos));
    nnout.writeNormalizedNode(anyXmlContainer);
    NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bos.toByteArray()));
    ContainerNode deserialized = (ContainerNode) nnin.readNormalizedNode();
    Optional<DataContainerChild<? extends PathArgument, ?>> child = deserialized.getChild(new NodeIdentifier(TestModel.ANY_XML_QNAME));
    assertEquals("AnyXml child present", true, child.isPresent());
    StreamResult xmlOutput = new StreamResult(new StringWriter());
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.transform(((AnyXmlNode) child.get()).getValue(), xmlOutput);
    assertEquals("XML", xml, xmlOutput.getWriter().toString());
    assertEquals("http://www.w3.org/TR/html4/", ((AnyXmlNode) child.get()).getValue().getNode().getNamespaceURI());
}
Also used : InputSource(org.xml.sax.InputSource) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) LeafSetEntryNode(org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode) AnyXmlNode(org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode) Node(org.w3c.dom.Node) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) ByteArrayOutputStream(java.io.ByteArrayOutputStream) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) NodeIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier) StringWriter(java.io.StringWriter) DataContainerChild(org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild) NodeIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier) StringReader(java.io.StringReader) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) PathArgument(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument) Test(org.junit.Test)

Aggregations

NodeIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier)58 Test (org.junit.Test)34 ContainerNode (org.opendaylight.yangtools.yang.data.api.schema.ContainerNode)29 YangInstanceIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)12 ChoiceNode (org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode)11 MapEntryNode (org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode)11 NodeIdentifierWithPredicates (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates)10 DOMDataTreeWriteCursor (org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor)8 ByteBuf (io.netty.buffer.ByteBuf)7 MapNode (org.opendaylight.yangtools.yang.data.api.schema.MapNode)7 NormalizedNode (org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)7 UnkeyedListEntryNode (org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode)7 DOMDataTreeCursorAwareTransaction (org.opendaylight.mdsal.dom.api.DOMDataTreeCursorAwareTransaction)6 DOMDataTreeProducer (org.opendaylight.mdsal.dom.api.DOMDataTreeProducer)6 AbstractTest (org.opendaylight.controller.cluster.datastore.AbstractTest)5 NodeWithValue (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue)5 BigInteger (java.math.BigInteger)4 ClientTransaction (org.opendaylight.controller.cluster.databroker.actors.dds.ClientTransaction)4 DistributedShardRegistration (org.opendaylight.controller.cluster.sharding.DistributedShardFactory.DistributedShardRegistration)4 DOMDataWriteTransaction (org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction)4