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);
}
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);
}
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);
}
}
}
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();
}
Aggregations