Search in sources :

Example 6 with DOMSourceAnyxmlNode

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());
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DOMSourceAnyxmlNode(org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode) Diff(org.custommonkey.xmlunit.Diff) NetconfMessage(org.opendaylight.netconf.api.NetconfMessage) ChoiceNode(org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) Document(org.w3c.dom.Document) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) AbstractBaseSchemasTest(org.opendaylight.netconf.sal.connect.netconf.AbstractBaseSchemasTest) Test(org.junit.Test)

Example 7 with DOMSourceAnyxmlNode

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();
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DOMSourceAnyxmlNode(org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode) XmlElement(org.opendaylight.netconf.api.xml.XmlElement) Element(org.w3c.dom.Element) PathArgument(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument) Document(org.w3c.dom.Document)

Example 8 with DOMSourceAnyxmlNode

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());
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DOMSourceAnyxmlNode(org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode) ByteArrayInputStream(java.io.ByteArrayInputStream) Diff(org.custommonkey.xmlunit.Diff) Document(org.w3c.dom.Document) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) Test(org.junit.Test)

Example 9 with DOMSourceAnyxmlNode

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;
}
Also used : AugmentationIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier) SystemMapNode(org.opendaylight.yangtools.yang.data.api.schema.SystemMapNode) DOMSourceAnyxmlNode(org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode) HashMap(java.util.HashMap) QName(org.opendaylight.yangtools.yang.common.QName) ArrayList(java.util.ArrayList) UnkeyedListEntryNode(org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode) MapNode(org.opendaylight.yangtools.yang.data.api.schema.MapNode) SystemMapNode(org.opendaylight.yangtools.yang.data.api.schema.SystemMapNode) UserMapNode(org.opendaylight.yangtools.yang.data.api.schema.UserMapNode) NodeIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier) ChoiceNode(org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode) NodeWithValue(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) UserMapNode(org.opendaylight.yangtools.yang.data.api.schema.UserMapNode) AugmentationNode(org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode) UnkeyedListNode(org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode)

Example 10 with DOMSourceAnyxmlNode

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());
}
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) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) MapEntryNode(org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode) Node(org.w3c.dom.Node) SystemMapNode(org.opendaylight.yangtools.yang.data.api.schema.SystemMapNode) DOMSourceAnyxmlNode(org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode) 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) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) Test(org.junit.Test)

Aggregations

DOMSourceAnyxmlNode (org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode)30 Test (org.junit.Test)16 DOMSource (javax.xml.transform.dom.DOMSource)15 ContainerNode (org.opendaylight.yangtools.yang.data.api.schema.ContainerNode)15 Diff (org.custommonkey.xmlunit.Diff)10 NodeIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier)10 NormalizedNode (org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)10 Document (org.w3c.dom.Document)10 DataContainerChild (org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild)9 QName (org.opendaylight.yangtools.yang.common.QName)8 NetconfMessage (org.opendaylight.netconf.api.NetconfMessage)7 YangInstanceIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)7 XmlElement (org.opendaylight.netconf.api.xml.XmlElement)6 Element (org.w3c.dom.Element)6 MapEntryNode (org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode)5 IOException (java.io.IOException)4 XMLStreamException (javax.xml.stream.XMLStreamException)4 PathArgument (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument)4 NormalizedNodeStreamWriter (org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter)4 URISyntaxException (java.net.URISyntaxException)3