use of org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode in project netconf by opendaylight.
the class BaseRpcSchemalessTransformerTest method toRpcRequest.
@Test
public void toRpcRequest() throws Exception {
final Document doc = XmlUtil.readXmlToDocument(getClass().getResourceAsStream("/schemaless/edit-config/container.xml"));
final DOMSourceAnyxmlNode xml = Builders.anyXmlBuilder().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(NetconfMessageTransformUtil.NETCONF_CONFIG_QNAME)).withValue(new DOMSource(doc.getDocumentElement())).build();
final ChoiceNode editContent = Builders.choiceBuilder().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(EditContent.QNAME)).withChild(xml).build();
final ChoiceNode candidate = Builders.choiceBuilder().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(ConfigTarget.QNAME)).withChild(Builders.leafBuilder().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(Candidate.QNAME)).withValue(Empty.value()).build()).build();
final ContainerNode target = Builders.containerBuilder().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(NetconfMessageTransformUtil.NETCONF_TARGET_QNAME)).withChild(candidate).build();
final ContainerNode editConfig = Builders.containerBuilder().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME)).withChild(editContent).withChild(target).build();
final NetconfMessage msg = transformer.toRpcRequest(NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME, editConfig);
final Diff diff = XMLUnit.compareXML(EXP_RPC, XmlUtil.toString(msg.getDocument()));
assertTrue(diff.toString(), diff.similar());
}
use of org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode in project netconf by opendaylight.
the class SchemalessRpcStructureTransformer method createEditConfigStructure.
/**
* This class in not context aware. All elements are present in resulting structure, which are present in data path.
* @see RpcStructureTransformer#createEditConfigStructure(Optional, YangInstanceIdentifier, Optional)
* @param data data
* @param dataPath path, where data will be written
* @param operation operation
* @return config structure
*/
@Override
public DOMSourceAnyxmlNode createEditConfigStructure(final Optional<NormalizedNode> data, final YangInstanceIdentifier dataPath, final Optional<ModifyAction> operation) {
Preconditions.checkArgument(data.isPresent());
Preconditions.checkArgument(data.get() instanceof DOMSourceAnyxmlNode);
final DOMSourceAnyxmlNode anxmlData = (DOMSourceAnyxmlNode) data.get();
final Document document = XmlUtil.newDocument();
final Element dataNode = (Element) document.importNode(getSourceElement(anxmlData.body()), true);
checkDataValidForPath(dataPath, dataNode);
final Element configElement = document.createElementNS(NETCONF_CONFIG_QNAME.getNamespace().toString(), NETCONF_CONFIG_QNAME.getLocalName());
document.appendChild(configElement);
final Element parentXmlStructure;
if (dataPath.isEmpty()) {
parentXmlStructure = dataNode;
configElement.appendChild(parentXmlStructure);
} else {
final List<PathArgument> pathArguments = dataPath.getPathArguments();
// last will be appended later
final List<PathArgument> pathWithoutLast = pathArguments.subList(0, pathArguments.size() - 1);
parentXmlStructure = instanceIdToXmlStructure(pathWithoutLast, configElement);
}
operation.ifPresent(modifyAction -> setOperationAttribute(modifyAction, document, dataNode));
// append data
parentXmlStructure.appendChild(document.importNode(dataNode, true));
return Builders.anyXmlBuilder().withNodeIdentifier(NETCONF_CONFIG_NODEID).withValue(new DOMSource(document.getDocumentElement())).build();
}
use of org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode in project controller by opendaylight.
the class SerializationUtilsTest method testSerializeDeserializeAnyXmlNode.
@Test
public void testSerializeDeserializeAnyXmlNode() throws Exception {
final ByteArrayInputStream is = new ByteArrayInputStream("<xml><data/></xml>".getBytes(Charset.defaultCharset()));
final Document parse = UntrustedXML.newDocumentBuilder().parse(is);
final DOMSourceAnyxmlNode anyXmlNode = Builders.anyXmlBuilder().withNodeIdentifier(id("anyXmlNode")).withValue(new DOMSource(parse)).build();
final byte[] bytes = serializeNormalizedNode(anyXmlNode);
assertEquals(113, bytes.length);
final NormalizedNode deserialized = deserializeNormalizedNode(bytes);
final DOMSource value = (DOMSource) deserialized.body();
final Diff diff = XMLUnit.compareXML((Document) anyXmlNode.body().getNode(), value.getNode().getOwnerDocument());
assertTrue(diff.toString(), diff.similar());
}
use of org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode in project yangtools by opendaylight.
the class ImmutableNormalizedNodeStreamWriterTest method buildOuterContainerNode.
private NormalizedNode buildOuterContainerNode() {
// my-container-1
SystemMapNode myKeyedListNode = Builders.mapBuilder().withNodeIdentifier(new NodeIdentifier(myKeyedList)).withChild(Builders.mapEntryBuilder().withNodeIdentifier(NodeIdentifierWithPredicates.of(myKeyedList, myKeyLeaf, "listkeyvalue1")).withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(myLeafInList1)).withValue("listleafvalue1").build()).withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(myLeafInList2)).withValue("listleafvalue2").build()).build()).withChild(Builders.mapEntryBuilder().withNodeIdentifier(NodeIdentifierWithPredicates.of(myKeyedList, myKeyLeaf, "listkeyvalue2")).withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(myLeafInList1)).withValue("listleafvalue12").build()).withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(myLeafInList2)).withValue("listleafvalue22").build()).build()).build();
UserMapNode myOrderedListNode = Builders.orderedMapBuilder().withNodeIdentifier(new NodeIdentifier(myOrderedList)).withChild(Builders.mapEntryBuilder().withNodeIdentifier(NodeIdentifierWithPredicates.of(myOrderedList, myKeyLeafInOrderedList, "olistkeyvalue1")).withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(myLeafInOrderedList1)).withValue("olistleafvalue1").build()).withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(myLeafInOrderedList2)).withValue("olistleafvalue2").build()).build()).withChild(Builders.mapEntryBuilder().withNodeIdentifier(NodeIdentifierWithPredicates.of(myOrderedList, myKeyLeafInOrderedList, "olistkeyvalue2")).withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(myLeafInOrderedList1)).withValue("olistleafvalue12").build()).withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(myLeafInOrderedList2)).withValue("olistleafvalue22").build()).build()).build();
final UnkeyedListEntryNode unkeyedListEntry = Builders.unkeyedListEntryBuilder().withNodeIdentifier(new NodeIdentifier(myLeafInUnkeyedList)).withChild(ImmutableNodes.leafNode(myLeafInUnkeyedList, "foo")).build();
final List<UnkeyedListEntryNode> unkeyedListEntries = new ArrayList<>();
unkeyedListEntries.add(unkeyedListEntry);
UnkeyedListNode myUnkeyedListNode = Builders.unkeyedListBuilder().withNodeIdentifier(new NodeIdentifier(myUnkeyedList)).withValue(unkeyedListEntries).build();
LeafNode<?> myLeaf1Node = Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(myLeaf1)).withValue("value1").build();
LeafSetNode<?> myLeafListNode = Builders.leafSetBuilder().withNodeIdentifier(new NodeIdentifier(myLeafList)).withChild(Builders.leafSetEntryBuilder().withNodeIdentifier(new NodeWithValue<>(myLeafList, "lflvalue1")).withValue("lflvalue1").build()).withChild(Builders.leafSetEntryBuilder().withNodeIdentifier(new NodeWithValue<>(myLeafList, "lflvalue2")).withValue("lflvalue2").build()).build();
LeafSetNode<?> myOrderedLeafListNode = Builders.orderedLeafSetBuilder().withNodeIdentifier(new NodeIdentifier(myOrderedLeafList)).withChild(Builders.leafSetEntryBuilder().withNodeIdentifier(new NodeWithValue<>(myOrderedLeafList, "olflvalue1")).withValue("olflvalue1").build()).withChild(Builders.leafSetEntryBuilder().withNodeIdentifier(new NodeWithValue<>(myOrderedLeafList, "olflvalue2")).withValue("olflvalue2").build()).build();
ContainerNode myContainer1Node = Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(myContainer1)).withChild(myKeyedListNode).withChild(myOrderedListNode).withChild(myUnkeyedListNode).withChild(myLeaf1Node).withChild(myLeafListNode).withChild(myOrderedLeafListNode).build();
// my-container-2
ContainerNode innerContainerNode = Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(innerContainer)).withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(myLeaf2)).withValue("value2").build()).build();
LeafNode<?> myLeaf3Node = Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(myLeaf3)).withValue("value3").build();
ChoiceNode myChoiceNode = Builders.choiceBuilder().withNodeIdentifier(new NodeIdentifier(myChoice)).withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(myLeafInCase2)).withValue("case2value").build()).build();
DOMSourceAnyxmlNode myAnyxmlNode = Builders.anyXmlBuilder().withNodeIdentifier(new NodeIdentifier(myAnyxml)).withValue(anyxmlDomSource).build();
ContainerNode myContainer2Node = Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(myContainer2)).withChild(innerContainerNode).withChild(myLeaf3Node).withChild(myChoiceNode).withChild(myAnyxmlNode).build();
// my-container-3
Map<QName, Object> keys = new HashMap<>();
keys.put(myFirstKeyLeaf, "listkeyvalue1");
keys.put(mySecondKeyLeaf, "listkeyvalue2");
MapNode myDoublyKeyedListNode = Builders.mapBuilder().withNodeIdentifier(new NodeIdentifier(myDoublyKeyedList)).withChild(Builders.mapEntryBuilder().withNodeIdentifier(NodeIdentifierWithPredicates.of(myDoublyKeyedList, keys)).withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(myLeafInList3)).withValue("listleafvalue1").build()).build()).build();
AugmentationNode myDoublyKeyedListAugNode = Builders.augmentationBuilder().withNodeIdentifier(new AugmentationIdentifier(ImmutableSet.of(myDoublyKeyedList))).withChild(myDoublyKeyedListNode).build();
ContainerNode myContainer3Node = Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(myContainer3)).withChild(myDoublyKeyedListAugNode).build();
AugmentationNode myContainer3AugNode = Builders.augmentationBuilder().withNodeIdentifier(new AugmentationIdentifier(ImmutableSet.of(myContainer3))).withChild(myContainer3Node).build();
ContainerNode outerContainerNode = Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(outerContainer)).withChild(myContainer1Node).withChild(myContainer2Node).withChild(myContainer3AugNode).build();
return outerContainerNode;
}
use of org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode in project yangtools 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 = version.newDataOutput(ByteStreams.newDataOutput(bos));
nnout.writeNormalizedNode(anyXmlContainer);
final byte[] bytes = bos.toByteArray();
assertEquals(anyxmlStreamingSize, bytes.length);
NormalizedNodeDataInput nnin = NormalizedNodeDataInput.newDataInput(ByteStreams.newDataInput(bytes));
ContainerNode deserialized = (ContainerNode) nnin.readNormalizedNode();
Optional<DataContainerChild> child = deserialized.findChildByArg(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(((DOMSourceAnyxmlNode) child.get()).body(), xmlOutput);
assertEquals("XML", xml, xmlOutput.getWriter().toString());
assertEquals("http://www.w3.org/TR/html4/", ((DOMSourceAnyxmlNode) child.get()).body().getNode().getNamespaceURI());
}
Aggregations