Search in sources :

Example 11 with XmlElement

use of org.opendaylight.netconf.api.xml.XmlElement in project netconf by opendaylight.

the class NetconfMDSalMappingTest method testEditConfigGetElementByTagName.

@Test
public void testEditConfigGetElementByTagName() throws Exception {
    String stringWithoutPrefix = "<rpc xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"0\">\n" + "  <edit-config>\n" + "    <target>\n" + "      <candidate/>\n" + "    </target>\n" + "  </edit-config>\n" + "</rpc>";
    XmlElement xe = getXmlElement(stringWithoutPrefix);
    NodeList nodeList = EditConfig.getElementsByTagName(xe, TARGET_KEY);
    assertEquals(1, nodeList.getLength());
    String stringWithPrefix = "<nc:rpc xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"0\">\n" + "  <nc:edit-config>\n" + "    <nc:target>\n" + "      <nc:candidate/>\n" + "    </nc:target>\n" + "  </nc:edit-config>\n" + "</nc:rpc>";
    xe = getXmlElement(stringWithPrefix);
    nodeList = EditConfig.getElementsByTagName(xe, TARGET_KEY);
    assertEquals(1, nodeList.getLength());
    String stringWithoutTarget = "<nc:rpc xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"0\">\n" + "  <nc:edit-config>\n" + "    <nc:target>\n" + "    </nc:target>\n" + "  </nc:edit-config>\n" + "</nc:rpc>";
    xe = getXmlElement(stringWithoutTarget);
    final NodeList targetKey = EditConfig.getElementsByTagName(xe, TARGET_KEY);
    assertThrows(DocumentedException.class, () -> XmlElement.fromDomElement((Element) targetKey.item(0)).getOnlyChildElement());
}
Also used : NodeList(org.w3c.dom.NodeList) XmlElement(org.opendaylight.netconf.api.xml.XmlElement) Test(org.junit.Test)

Example 12 with XmlElement

use of org.opendaylight.netconf.api.xml.XmlElement in project netconf by opendaylight.

the class NetconfMDSalMappingTest method getXmlElement.

private static XmlElement getXmlElement(final String elementAsString) throws Exception {
    Document document = XmlUtil.readXmlToDocument(elementAsString);
    Element element = document.getDocumentElement();
    return XmlElement.fromDomElement(element);
}
Also used : XmlElement(org.opendaylight.netconf.api.xml.XmlElement) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document)

Example 13 with XmlElement

use of org.opendaylight.netconf.api.xml.XmlElement in project netconf by opendaylight.

the class Netconf506Test method testValidateTypes.

@Test
public void testValidateTypes() throws Exception {
    final SchemaContext context = YangParserTestUtils.parseYangResources(Bug8084.class, "/yang/filter-validator-test-mod-0.yang", "/yang/mdsal-netconf-mapping-test.yang");
    final CurrentSchemaContext currentContext = mock(CurrentSchemaContext.class);
    doReturn(context).when(currentContext).getCurrentContext();
    final FilterContentValidator validator = new FilterContentValidator(currentContext);
    final Document document = XmlUtil.readXmlToDocument(FilterContentValidatorTest.class.getResourceAsStream("/filter/netconf506.xml"));
    final XmlElement xmlElement = XmlElement.fromDomDocument(document);
    final YangInstanceIdentifier actual = validator.validate(xmlElement);
    final Map<QName, Object> inputs = new HashMap<>();
    inputs.put(QName.create(BASE, "name"), "foo");
    final YangInstanceIdentifier expected = YangInstanceIdentifier.builder().node(BASE).node(QName.create(BASE, "leafref-key-list")).nodeWithKey(QName.create(BASE, "leafref-key-list"), inputs).build();
    assertEquals(expected, actual);
}
Also used : HashMap(java.util.HashMap) QName(org.opendaylight.yangtools.yang.common.QName) XmlElement(org.opendaylight.netconf.api.xml.XmlElement) SchemaContext(org.opendaylight.yangtools.yang.model.api.SchemaContext) CurrentSchemaContext(org.opendaylight.netconf.mdsal.connector.CurrentSchemaContext) CurrentSchemaContext(org.opendaylight.netconf.mdsal.connector.CurrentSchemaContext) Document(org.w3c.dom.Document) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) Test(org.junit.Test)

Example 14 with XmlElement

use of org.opendaylight.netconf.api.xml.XmlElement in project netconf by opendaylight.

the class RuntimeRpc method handle.

@Override
public Document handle(final Document requestMessage, final NetconfOperationChainedExecution subsequentOperation) throws DocumentedException {
    final XmlElement requestElement = getRequestElementWithCheck(requestMessage);
    final Document document = XmlUtil.newDocument();
    final XmlElement operationElement = requestElement.getOnlyChildElement();
    final Map<String, Attr> attributes = requestElement.getAttributes();
    final Element response = handle(document, operationElement, subsequentOperation);
    final Element rpcReply = XmlUtil.createElement(document, XmlNetconfConstants.RPC_REPLY_KEY, Optional.of(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0));
    if (XmlElement.fromDomElement(response).hasNamespace()) {
        rpcReply.appendChild(response);
    } else {
        final NodeList list = response.getChildNodes();
        if (list.getLength() == 0) {
            rpcReply.appendChild(response);
        } else {
            while (list.getLength() != 0) {
                rpcReply.appendChild(list.item(0));
            }
        }
    }
    for (final Attr attribute : attributes.values()) {
        rpcReply.setAttributeNode((Attr) document.importNode(attribute, true));
    }
    document.appendChild(rpcReply);
    return document;
}
Also used : XmlElement(org.opendaylight.netconf.api.xml.XmlElement) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) XmlElement(org.opendaylight.netconf.api.xml.XmlElement) Document(org.w3c.dom.Document) Attr(org.w3c.dom.Attr)

Example 15 with XmlElement

use of org.opendaylight.netconf.api.xml.XmlElement in project netconf by opendaylight.

the class FilterContentValidator method getKeyValues.

private Map<QName, Object> getKeyValues(final List<String> path, final XmlElement filterContent, final DataSchemaNode parentSchemaNode, final ListSchemaNode listSchemaNode) {
    XmlElement current = filterContent;
    // find list element
    for (final String pathElement : path) {
        final List<XmlElement> childElements = current.getChildElements(pathElement);
        // if there are multiple list entries present in the filter, we can't use any keys and must read whole list
        if (childElements.size() != 1) {
            return Map.of();
        }
        current = childElements.get(0);
    }
    final Map<QName, Object> keys = new HashMap<>();
    final List<QName> keyDefinition = listSchemaNode.getKeyDefinition();
    for (final QName qualifiedName : keyDefinition) {
        final Optional<XmlElement> childElements = current.getOnlyChildElementOptionally(qualifiedName.getLocalName());
        if (childElements.isEmpty()) {
            return Map.of();
        }
        final Optional<String> keyValue = childElements.get().getOnlyTextContentOptionally();
        if (keyValue.isPresent()) {
            final LeafSchemaNode listKey = (LeafSchemaNode) listSchemaNode.getDataChildByName(qualifiedName);
            if (listKey instanceof IdentityrefTypeDefinition) {
                keys.put(qualifiedName, keyValue.get());
            } else {
                final TypeDefinition<? extends TypeDefinition<?>> keyType = listKey.getType();
                if (keyType instanceof IdentityrefTypeDefinition || keyType instanceof LeafrefTypeDefinition) {
                    final Document document = filterContent.getDomElement().getOwnerDocument();
                    final NamespaceContext nsContext = new UniversalNamespaceContextImpl(document, false);
                    final EffectiveModelContext modelContext = schemaContext.getCurrentContext();
                    final XmlCodecFactory xmlCodecFactory = XmlCodecFactory.create(modelContext);
                    final SchemaInferenceStack resolver = SchemaInferenceStack.of(modelContext, Absolute.of(parentSchemaNode.getQName(), listSchemaNode.getQName(), listKey.getQName()));
                    final TypeAwareCodec<?, NamespaceContext, XMLStreamWriter> typeCodec = xmlCodecFactory.codecFor(listKey, resolver);
                    final Object deserializedKeyValue = typeCodec.parseValue(nsContext, keyValue.get());
                    keys.put(qualifiedName, deserializedKeyValue);
                } else {
                    final Object deserializedKey = TypeDefinitionAwareCodec.from(keyType).deserialize(keyValue.get());
                    keys.put(qualifiedName, deserializedKey);
                }
            }
        }
    }
    return keys;
}
Also used : SchemaInferenceStack(org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack) IdentityrefTypeDefinition(org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition) HashMap(java.util.HashMap) QName(org.opendaylight.yangtools.yang.common.QName) LeafSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafSchemaNode) Document(org.w3c.dom.Document) LeafrefTypeDefinition(org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition) NamespaceContext(javax.xml.namespace.NamespaceContext) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) XmlElement(org.opendaylight.netconf.api.xml.XmlElement) XmlCodecFactory(org.opendaylight.yangtools.yang.data.codec.xml.XmlCodecFactory) EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext)

Aggregations

XmlElement (org.opendaylight.netconf.api.xml.XmlElement)50 Element (org.w3c.dom.Element)21 Document (org.w3c.dom.Document)16 DocumentedException (org.opendaylight.netconf.api.DocumentedException)13 Test (org.junit.Test)8 QName (org.opendaylight.yangtools.yang.common.QName)7 NodeList (org.w3c.dom.NodeList)6 DOMSource (javax.xml.transform.dom.DOMSource)5 ContainerNode (org.opendaylight.yangtools.yang.data.api.schema.ContainerNode)5 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 NormalizedNode (org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)4 URISyntaxException (java.net.URISyntaxException)3 DOMSourceAnyxmlNode (org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode)3 NormalizedNodeResult (org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult)3 SAXException (org.xml.sax.SAXException)3 XmlNodeConverter (io.lighty.codecs.util.XmlNodeConverter)2 DeserializationException (io.lighty.codecs.util.exception.DeserializationException)2 Response (io.lighty.netconf.device.response.Response)2 ResponseData (io.lighty.netconf.device.response.ResponseData)2