Search in sources :

Example 46 with XmlElement

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

the class NetconfMessageUtil method extractCapabilitiesFromHello.

public static Collection<String> extractCapabilitiesFromHello(final Document doc) {
    XmlElement responseElement = XmlElement.fromDomDocument(doc);
    // Extract child element <capabilities> from <hello> with or without(fallback) the same namespace
    Optional<XmlElement> capabilitiesElement = responseElement.getOnlyChildElementWithSameNamespaceOptionally(XmlNetconfConstants.CAPABILITIES);
    if (capabilitiesElement.isEmpty()) {
        capabilitiesElement = responseElement.getOnlyChildElementOptionally(XmlNetconfConstants.CAPABILITIES);
    }
    List<XmlElement> caps = capabilitiesElement.get().getChildElements(XmlNetconfConstants.CAPABILITY);
    return Collections2.transform(caps, input -> {
        // Trim possible leading/tailing whitespace
        try {
            return input.getTextContent().trim();
        } catch (DocumentedException e) {
            LOG.trace("Error fetching input text content", e);
            return null;
        }
    });
}
Also used : NetconfDocumentedException(org.opendaylight.netconf.api.NetconfDocumentedException) DocumentedException(org.opendaylight.netconf.api.DocumentedException) XmlElement(org.opendaylight.netconf.api.xml.XmlElement)

Example 47 with XmlElement

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

the class SubtreeFilter method applyRpcSubtreeFilter.

public static Document applyRpcSubtreeFilter(final Document requestDocument, final Document rpcReply) throws DocumentedException {
    OperationNameAndNamespace operationNameAndNamespace = new OperationNameAndNamespace(requestDocument);
    if (XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0.equals(operationNameAndNamespace.getNamespace()) && XmlNetconfConstants.GET.equals(operationNameAndNamespace.getOperationName()) || XmlNetconfConstants.GET_CONFIG.equals(operationNameAndNamespace.getOperationName())) {
        // process subtree filtering here, in case registered netconf operations do
        // not implement filtering.
        Optional<XmlElement> maybeFilter = operationNameAndNamespace.getOperationElement().getOnlyChildElementOptionally(XmlNetconfConstants.FILTER, XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
        if (maybeFilter.isEmpty()) {
            return rpcReply;
        }
        XmlElement filter = maybeFilter.get();
        if (isSupported(filter)) {
            // do
            return filtered(maybeFilter.get(), rpcReply);
        }
    }
    // return identical document
    return rpcReply;
}
Also used : XmlElement(org.opendaylight.netconf.api.xml.XmlElement) OperationNameAndNamespace(org.opendaylight.netconf.util.mapping.AbstractNetconfOperation.OperationNameAndNamespace)

Example 48 with XmlElement

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

the class SubtreeFilter method extractNotificationContent.

private static Document extractNotificationContent(final Document notification) throws DocumentedException {
    XmlElement root = XmlElement.fromDomElement(notification.getDocumentElement());
    XmlElement content = root.getOnlyChildElement();
    notification.removeChild(root.getDomElement());
    notification.appendChild(content.getDomElement());
    return notification;
}
Also used : XmlElement(org.opendaylight.netconf.api.xml.XmlElement)

Example 49 with XmlElement

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

the class SubtreeFilter method filteredNotification.

private static Document filteredNotification(final XmlElement filter, final Document originalNotification) throws DocumentedException {
    Document result = XmlUtil.newDocument();
    XmlElement dataSrc = XmlElement.fromDomDocument(originalNotification);
    Element dataDst = (Element) result.importNode(dataSrc.getDomElement(), false);
    for (XmlElement filterChild : filter.getChildElements()) {
        addSubtree2(filterChild, dataSrc.getOnlyChildElement(), XmlElement.fromDomElement(dataDst));
    }
    if (dataDst.getFirstChild() != null) {
        result.appendChild(dataDst.getFirstChild());
        return result;
    }
    return null;
}
Also used : XmlElement(org.opendaylight.netconf.api.xml.XmlElement) Element(org.w3c.dom.Element) XmlElement(org.opendaylight.netconf.api.xml.XmlElement) Document(org.w3c.dom.Document)

Example 50 with XmlElement

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

the class AbstractNetconfOperation method handle.

@Override
public Document handle(final Document requestMessage, final NetconfOperationChainedExecution subsequentOperation) throws DocumentedException {
    XmlElement requestElement = getRequestElementWithCheck(requestMessage);
    Document document = XmlUtil.newDocument();
    XmlElement operationElement = requestElement.getOnlyChildElement();
    Map<String, Attr> attributes = requestElement.getAttributes();
    Element response = handle(document, operationElement, subsequentOperation);
    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 {
        Element responseNS = XmlUtil.createElement(document, response.getNodeName(), Optional.of(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0));
        NodeList list = response.getChildNodes();
        while (list.getLength() != 0) {
            responseNS.appendChild(list.item(0));
        }
        rpcReply.appendChild(responseNS);
    }
    for (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)

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