Search in sources :

Example 1 with SchemaOrderedNormalizedNodeWriter

use of org.opendaylight.yangtools.yang.data.impl.schema.SchemaOrderedNormalizedNodeWriter in project yangtools by opendaylight.

the class SchemaOrderedNormalizedNodeWriterTest method testWrite.

@Test
public void testWrite() throws XMLStreamException, IOException, SAXException {
    final StringWriter stringWriter = new StringWriter();
    final XMLStreamWriter xmlStreamWriter = factory.createXMLStreamWriter(stringWriter);
    EffectiveModelContext schemaContext = getSchemaContext("/bug1848/foo.yang");
    NormalizedNodeStreamWriter writer = XMLStreamNormalizedNodeStreamWriter.create(xmlStreamWriter, schemaContext);
    try (SchemaOrderedNormalizedNodeWriter nnw = new SchemaOrderedNormalizedNodeWriter(writer, schemaContext)) {
        List<MapEntryNode> rule1Names = new ArrayList<>();
        rule1Names.add(ImmutableNodes.mapEntry(createQName(FOO_NAMESPACE, RULE_NODE), createQName(FOO_NAMESPACE, NAME_NODE), "rule1"));
        rule1Names.add(ImmutableNodes.mapEntry(createQName(FOO_NAMESPACE, RULE_NODE), createQName(FOO_NAMESPACE, NAME_NODE), "rule2"));
        List<MapEntryNode> rule2Names = new ArrayList<>();
        rule1Names.add(ImmutableNodes.mapEntry(createQName(FOO_NAMESPACE, RULE_NODE), createQName(FOO_NAMESPACE, NAME_NODE), "rule3"));
        rule1Names.add(ImmutableNodes.mapEntry(createQName(FOO_NAMESPACE, RULE_NODE), createQName(FOO_NAMESPACE, NAME_NODE), "rule4"));
        UserMapNode rules1 = Builders.orderedMapBuilder().withNodeIdentifier(getNodeIdentifier(FOO_NAMESPACE, RULE_NODE)).withValue(rule1Names).build();
        UserMapNode rules2 = Builders.orderedMapBuilder().withNodeIdentifier(getNodeIdentifier(FOO_NAMESPACE, RULE_NODE)).withValue(rule2Names).build();
        List<MapEntryNode> policyNodes = new ArrayList<>();
        final MapEntryNode pn1 = ImmutableNodes.mapEntryBuilder(createQName(FOO_NAMESPACE, POLICY_NODE), createQName(FOO_NAMESPACE, NAME_NODE), "policy1").withChild(rules1).build();
        final MapEntryNode pn2 = ImmutableNodes.mapEntryBuilder(createQName(FOO_NAMESPACE, POLICY_NODE), createQName(FOO_NAMESPACE, NAME_NODE), "policy2").withChild(rules2).build();
        policyNodes.add(pn1);
        policyNodes.add(pn2);
        UserMapNode policy = Builders.orderedMapBuilder().withNodeIdentifier(getNodeIdentifier(FOO_NAMESPACE, POLICY_NODE)).withValue(policyNodes).build();
        ContainerNode root = Builders.containerBuilder().withNodeIdentifier(getNodeIdentifier(FOO_NAMESPACE, "root")).withChild(policy).build();
        nnw.write(root);
    }
    XMLAssert.assertXMLIdentical(new Diff(EXPECTED_1, stringWriter.toString()), true);
}
Also used : StringWriter(java.io.StringWriter) SchemaOrderedNormalizedNodeWriter(org.opendaylight.yangtools.yang.data.impl.schema.SchemaOrderedNormalizedNodeWriter) Diff(org.custommonkey.xmlunit.Diff) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) ArrayList(java.util.ArrayList) NormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) UserMapNode(org.opendaylight.yangtools.yang.data.api.schema.UserMapNode) MapEntryNode(org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode) EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext) Test(org.junit.Test)

Example 2 with SchemaOrderedNormalizedNodeWriter

use of org.opendaylight.yangtools.yang.data.impl.schema.SchemaOrderedNormalizedNodeWriter in project yangtools by opendaylight.

the class SchemaOrderedNormalizedNodeWriterTest method testWriteOrder.

@Test
public void testWriteOrder() throws XMLStreamException, IOException, SAXException {
    final StringWriter stringWriter = new StringWriter();
    final XMLStreamWriter xmlStreamWriter = factory.createXMLStreamWriter(stringWriter);
    EffectiveModelContext schemaContext = getSchemaContext("/bug1848/order.yang");
    NormalizedNodeStreamWriter writer = XMLStreamNormalizedNodeStreamWriter.create(xmlStreamWriter, schemaContext);
    try (NormalizedNodeWriter nnw = new SchemaOrderedNormalizedNodeWriter(writer, schemaContext)) {
        ContainerNode cont = Builders.containerBuilder().withNodeIdentifier(getNodeIdentifier(ORDER_NAMESPACE, "cont")).withChild(ImmutableNodes.leafNode(createQName(ORDER_NAMESPACE, "content"), "content1")).build();
        ContainerNode root = Builders.containerBuilder().withNodeIdentifier(getNodeIdentifier(ORDER_NAMESPACE, "root")).withChild(cont).withChild(ImmutableNodes.leafNode(createQName(ORDER_NAMESPACE, "id"), "id1")).build();
        nnw.write(root);
    }
    XMLAssert.assertXMLIdentical(new Diff(EXPECTED_2, stringWriter.toString()), true);
}
Also used : StringWriter(java.io.StringWriter) SchemaOrderedNormalizedNodeWriter(org.opendaylight.yangtools.yang.data.impl.schema.SchemaOrderedNormalizedNodeWriter) NormalizedNodeWriter(org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter) SchemaOrderedNormalizedNodeWriter(org.opendaylight.yangtools.yang.data.impl.schema.SchemaOrderedNormalizedNodeWriter) Diff(org.custommonkey.xmlunit.Diff) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) NormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext) Test(org.junit.Test)

Example 3 with SchemaOrderedNormalizedNodeWriter

use of org.opendaylight.yangtools.yang.data.impl.schema.SchemaOrderedNormalizedNodeWriter in project netconf by opendaylight.

the class NetconfMessageTransformUtil method writeNormalizedOperationInput.

@SuppressWarnings("checkstyle:IllegalCatch")
public static void writeNormalizedOperationInput(final ContainerNode normalized, final DOMResult result, final Absolute operationPath, final EffectiveModelContext baseNetconfCtx) throws IOException, XMLStreamException {
    final QName inputQName = YangConstants.operationInputQName(operationPath.lastNodeIdentifier().getModule());
    // FIXME: eliminate this conversion
    final SchemaPath inputPath = operationPath.asSchemaPath().createChild(inputQName);
    final XMLStreamWriter writer = NetconfUtil.XML_FACTORY.createXMLStreamWriter(result);
    try {
        try (NormalizedNodeStreamWriter normalizedNodeStreamWriter = XMLStreamNormalizedNodeStreamWriter.create(writer, baseNetconfCtx, inputPath)) {
            try (SchemaOrderedNormalizedNodeWriter normalizedNodeWriter = new SchemaOrderedNormalizedNodeWriter(normalizedNodeStreamWriter, baseNetconfCtx, inputPath)) {
                final Collection<DataContainerChild> value = normalized.body();
                normalizedNodeWriter.write(value);
                normalizedNodeWriter.flush();
            }
        }
    } finally {
        try {
            writer.close();
        } catch (final Exception e) {
            LOG.warn("Unable to close resource properly", e);
        }
    }
}
Also used : SchemaOrderedNormalizedNodeWriter(org.opendaylight.yangtools.yang.data.impl.schema.SchemaOrderedNormalizedNodeWriter) SchemaPath(org.opendaylight.yangtools.yang.model.api.SchemaPath) DataContainerChild(org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild) QName(org.opendaylight.yangtools.yang.common.QName) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) XMLStreamNormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.codec.xml.XMLStreamNormalizedNodeStreamWriter) NormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter) XMLStreamException(javax.xml.stream.XMLStreamException) DateTimeParseException(java.time.format.DateTimeParseException) NetconfDocumentedException(org.opendaylight.netconf.api.NetconfDocumentedException) DocumentedException(org.opendaylight.netconf.api.DocumentedException) IOException(java.io.IOException)

Example 4 with SchemaOrderedNormalizedNodeWriter

use of org.opendaylight.yangtools.yang.data.impl.schema.SchemaOrderedNormalizedNodeWriter in project netconf by opendaylight.

the class RuntimeRpc method transformNormalizedNode.

private Element transformNormalizedNode(final Document document, final NormalizedNode data, final Absolute rpcOutputPath) {
    final DOMResult result = new DOMResult(document.createElement(XmlNetconfConstants.RPC_REPLY_KEY));
    final XMLStreamWriter xmlWriter = getXmlStreamWriter(result);
    final NormalizedNodeStreamWriter nnStreamWriter = XMLStreamNormalizedNodeStreamWriter.create(xmlWriter, schemaContext.getCurrentContext(), rpcOutputPath);
    final SchemaOrderedNormalizedNodeWriter nnWriter = new SchemaOrderedNormalizedNodeWriter(nnStreamWriter, schemaContext.getCurrentContext(), rpcOutputPath.asSchemaPath());
    writeRootElement(xmlWriter, nnWriter, (ContainerNode) data);
    try {
        nnStreamWriter.close();
        xmlWriter.close();
    } catch (IOException | XMLStreamException e) {
        LOG.warn("Error while closing streams", e);
    }
    return (Element) result.getNode();
}
Also used : DOMResult(javax.xml.transform.dom.DOMResult) SchemaOrderedNormalizedNodeWriter(org.opendaylight.yangtools.yang.data.impl.schema.SchemaOrderedNormalizedNodeWriter) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) XmlElement(org.opendaylight.netconf.api.xml.XmlElement) Element(org.w3c.dom.Element) ImmutableNormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter) XMLStreamNormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.codec.xml.XMLStreamNormalizedNodeStreamWriter) NormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter) IOException(java.io.IOException)

Aggregations

XMLStreamWriter (javax.xml.stream.XMLStreamWriter)4 NormalizedNodeStreamWriter (org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter)4 SchemaOrderedNormalizedNodeWriter (org.opendaylight.yangtools.yang.data.impl.schema.SchemaOrderedNormalizedNodeWriter)4 IOException (java.io.IOException)2 StringWriter (java.io.StringWriter)2 XMLStreamException (javax.xml.stream.XMLStreamException)2 Diff (org.custommonkey.xmlunit.Diff)2 Test (org.junit.Test)2 ContainerNode (org.opendaylight.yangtools.yang.data.api.schema.ContainerNode)2 XMLStreamNormalizedNodeStreamWriter (org.opendaylight.yangtools.yang.data.codec.xml.XMLStreamNormalizedNodeStreamWriter)2 EffectiveModelContext (org.opendaylight.yangtools.yang.model.api.EffectiveModelContext)2 DateTimeParseException (java.time.format.DateTimeParseException)1 ArrayList (java.util.ArrayList)1 DOMResult (javax.xml.transform.dom.DOMResult)1 DocumentedException (org.opendaylight.netconf.api.DocumentedException)1 NetconfDocumentedException (org.opendaylight.netconf.api.NetconfDocumentedException)1 XmlElement (org.opendaylight.netconf.api.xml.XmlElement)1 QName (org.opendaylight.yangtools.yang.common.QName)1 DataContainerChild (org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild)1 MapEntryNode (org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode)1