Search in sources :

Example 11 with DOMSourceAnyxmlNode

use of org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode in project yangtools by opendaylight.

the class SchemalessXMLStreamNormalizedNodeStreamWriterTest method buildOuterContainerNode.

private NormalizedNode buildOuterContainerNode() {
    // my-container-1
    MapNode 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(myUnkeyedList)).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 = ImmutableNodes.leafNode(myLeaf1, "value1");
    SystemLeafSetNode<?> 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();
    UserLeafSetNode<?> 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).build();
    // .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) 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) 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 12 with DOMSourceAnyxmlNode

use of org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode in project yangtools by opendaylight.

the class AnyXmlWithParamsParsingTest method testAnyXmlWithParams.

@Test
public void testAnyXmlWithParams() throws Exception {
    final EffectiveModelContext context = YangParserTestUtils.parseYangResourceDirectory("/anyxml-support/params/");
    final Document doc = UntrustedXML.newDocumentBuilder().parse(EDIT_CONFIG);
    final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
    final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder);
    final XmlParserStream xmlParser = XmlParserStream.create(writer, SchemaInferenceStack.of(context, Absolute.of(QName.create(IETF_NETCONF, "edit-config"), YangConstants.operationInputQName(IETF_NETCONF))).toInference());
    xmlParser.traverse(new DOMSource(doc.getDocumentElement()));
    final NormalizedNode parsed = resultHolder.getResult();
    final DataContainerChild editCfg = ((ContainerNode) parsed).childByArg(getNodeId(parsed, "edit-content"));
    final DOMSource anyXmlParsedDom = ((DOMSourceAnyxmlNode) ((ChoiceNode) editCfg).childByArg(getNodeId(parsed, "config"))).body();
    assertNotNull(anyXmlParsedDom);
    final String anyXmlParsedDomString = toStringDom(anyXmlParsedDom);
    assertThat(anyXmlParsedDomString, containsString("active xmpref:prefixed2=\"attribute2\""));
    assertThat(anyXmlParsedDomString, containsString("interface-name xmpref:prefixed3=\"attribute3\""));
    assertThat(anyXmlParsedDomString, containsString("interface-configuration xmlns:xmpref=\"xml:namespace:prefix\" simple=\"attribute\""));
    assertThat(anyXmlParsedDomString, containsString("interface-configurations xmlns=\"http://cisco.com/ns/yang/Cisco-IOS-XR-ifmgr-cfg\""));
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DOMSourceAnyxmlNode(org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode) DataContainerChild(org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild) NormalizedNodeResult(org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult) ImmutableNormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter) NormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Document(org.w3c.dom.Document) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext) Test(org.junit.Test)

Example 13 with DOMSourceAnyxmlNode

use of org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode in project yangtools by opendaylight.

the class NormalizedNodeWriterTest method testNormalizedNodeWriter.

@Test
public void testNormalizedNodeWriter() throws IOException {
    final NormalizedNodeStreamWriter loggingNormalizedNodeStreamWriter = new LoggingNormalizedNodeStreamWriter();
    final NormalizedNodeWriter orderedNormalizedNodeWriter = NormalizedNodeWriter.forStreamWriter(loggingNormalizedNodeStreamWriter);
    assertEquals(loggingNormalizedNodeStreamWriter, orderedNormalizedNodeWriter.getWriter());
    final IllegalStateException ex = assertThrows(IllegalStateException.class, () -> orderedNormalizedNodeWriter.write(mock(NormalizedNode.class)));
    assertTrue(ex.getMessage().startsWith("It wasn't possible to serialize node"));
    final LeafSetEntryNode<?> mockedLeafSetEntryNode = mock(LeafSetEntryNode.class);
    doReturn(new NodeWithValue<>(myLeafList, "leaflist-value-1")).when(mockedLeafSetEntryNode).getIdentifier();
    doReturn("leaflist-value-1").when(mockedLeafSetEntryNode).body();
    assertNotNull(orderedNormalizedNodeWriter.write(mockedLeafSetEntryNode));
    final LeafNode<?> mockedLeafNode = mock(LeafNode.class);
    doReturn("leaf-value-1").when(mockedLeafNode).body();
    assertNotNull(orderedNormalizedNodeWriter.write(mockedLeafNode));
    final DOMSourceAnyxmlNode mockedAnyXmlNode = mock(DOMSourceAnyxmlNode.class);
    doCallRealMethod().when(mockedAnyXmlNode).bodyObjectModel();
    doReturn(new DOMSource()).when(mockedAnyXmlNode).body();
    assertNotNull(orderedNormalizedNodeWriter.write(mockedAnyXmlNode));
    final NormalizedNode mockedContainerNode = mock(ContainerNode.class);
    assertNotNull(orderedNormalizedNodeWriter.write(mockedContainerNode));
    final MapEntryNode mockedMapEntryNode = mock(MapEntryNode.class);
    doReturn(NodeIdentifierWithPredicates.of(myKeyedList, myKeyLeaf, "list-key-value-1")).when(mockedMapEntryNode).getIdentifier();
    doReturn(null).when(mockedMapEntryNode).childByArg(any(NodeIdentifier.class));
    assertNotNull(orderedNormalizedNodeWriter.write(mockedMapEntryNode));
    final UnkeyedListEntryNode mockedUnkeyedListEntryNode = mock(UnkeyedListEntryNode.class);
    assertNotNull(orderedNormalizedNodeWriter.write(mockedUnkeyedListEntryNode));
    assertNotNull(orderedNormalizedNodeWriter.write(mock(ChoiceNode.class)));
    assertNotNull(orderedNormalizedNodeWriter.write(mock(AugmentationNode.class)));
    final UnkeyedListNode mockedUnkeyedListNode = mock(UnkeyedListNode.class);
    final Set<?> value = Set.of(mockedUnkeyedListEntryNode);
    doReturn(value).when(mockedUnkeyedListNode).body();
    assertNotNull(orderedNormalizedNodeWriter.write(mockedUnkeyedListNode));
    assertNotNull(orderedNormalizedNodeWriter.write(mock(UserMapNode.class)));
    assertNotNull(orderedNormalizedNodeWriter.write(mock(SystemMapNode.class)));
    assertNotNull(orderedNormalizedNodeWriter.write(mock(UserLeafSetNode.class)));
    assertNotNull(orderedNormalizedNodeWriter.write(mock(SystemLeafSetNode.class)));
    orderedNormalizedNodeWriter.flush();
    orderedNormalizedNodeWriter.close();
    final NormalizedNodeWriter normalizedNodeWriter = NormalizedNodeWriter.forStreamWriter(loggingNormalizedNodeStreamWriter, false);
    assertNotNull(normalizedNodeWriter.write(mockedMapEntryNode));
    normalizedNodeWriter.flush();
    normalizedNodeWriter.close();
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DOMSourceAnyxmlNode(org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode) UnkeyedListEntryNode(org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode) MapEntryNode(org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode) NodeIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) UnkeyedListNode(org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode) Test(org.junit.Test)

Example 14 with DOMSourceAnyxmlNode

use of org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode in project netconf by opendaylight.

the class SchemalessMessageTransformer method toNotification.

@Override
public DOMNotification toNotification(final NetconfMessage message) {
    final Map.Entry<Instant, XmlElement> stripped = NetconfMessageTransformUtil.stripNotification(message);
    final QName notificationNoRev;
    try {
        notificationNoRev = QName.create(stripped.getValue().getNamespace(), stripped.getValue().getName()).withoutRevision();
    } catch (final MissingNameSpaceException e) {
        throw new IllegalArgumentException("Unable to parse notification " + message + ", cannot find namespace", e);
    }
    final DOMSourceAnyxmlNode notificationPayload = Builders.anyXmlBuilder().withNodeIdentifier(new NodeIdentifier(notificationNoRev)).withValue(new DOMSource(stripped.getValue().getDomElement())).build();
    final ContainerNode notificationBody = Builders.containerBuilder().withNodeIdentifier(SCHEMALESS_NOTIFICATION_PAYLOAD).withChild(notificationPayload).build();
    return new NetconfMessageTransformer.NetconfDeviceNotification(notificationBody, stripped.getKey());
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DOMSourceAnyxmlNode(org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode) QName(org.opendaylight.yangtools.yang.common.QName) Instant(java.time.Instant) MissingNameSpaceException(org.opendaylight.netconf.api.xml.MissingNameSpaceException) NodeIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier) XmlElement(org.opendaylight.netconf.api.xml.XmlElement) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) Map(java.util.Map)

Example 15 with DOMSourceAnyxmlNode

use of org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode in project netconf by opendaylight.

the class SchemalessMessageTransformer method toRpcResult.

/**
 * Transforms reply message to anyXml node.
 * In case, that rpc-reply doesn't contain data and contains only &lt;ok/&gt; element, returns null.
 * @param rpcReply reply message
 * @return anyxml
 */
@Override
public DOMRpcResult toRpcResult(final NetconfMessage rpcReply, final QName rpc) {
    final Document document = rpcReply.getDocument();
    final DOMSourceAnyxmlNode result;
    if (BaseRpcSchemalessTransformer.isOkPresent(document)) {
        result = null;
    } else {
        result = Builders.anyXmlBuilder().withNodeIdentifier(NetconfMessageTransformUtil.NETCONF_RPC_REPLY_NODEID).withValue(new DOMSource(rpcReply.getDocument())).build();
    }
    return new DefaultDOMRpcResult(result);
}
Also used : DefaultDOMRpcResult(org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult) DOMSource(javax.xml.transform.dom.DOMSource) DOMSourceAnyxmlNode(org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode) Document(org.w3c.dom.Document)

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