Search in sources :

Example 6 with XmlElement

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

the class SubtreeFilter method filtered.

private static Document filtered(final XmlElement filter, final Document originalReplyDocument) throws DocumentedException {
    Document result = XmlUtil.newDocument();
    // even if filter is empty, copy /rpc/data
    Element rpcReply = originalReplyDocument.getDocumentElement();
    Node rpcReplyDst = result.importNode(rpcReply, false);
    result.appendChild(rpcReplyDst);
    XmlElement dataSrc = XmlElement.fromDomElement(rpcReply).getOnlyChildElement("data", XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
    Element dataDst = (Element) result.importNode(dataSrc.getDomElement(), false);
    rpcReplyDst.appendChild(dataDst);
    addSubtree(filter, dataSrc, XmlElement.fromDomElement(dataDst));
    return result;
}
Also used : XmlElement(org.opendaylight.netconf.api.xml.XmlElement) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) XmlElement(org.opendaylight.netconf.api.xml.XmlElement) Document(org.w3c.dom.Document)

Example 7 with XmlElement

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

the class SubtreeFilter method addSubtree2.

private static MatchingResult addSubtree2(final XmlElement filter, final XmlElement src, final XmlElement dstParent) throws DocumentedException {
    Document document = dstParent.getDomElement().getOwnerDocument();
    MatchingResult matches = matches(src, filter);
    if (matches != MatchingResult.NO_MATCH && matches != MatchingResult.CONTENT_MISMATCH) {
        // copy srcChild to dst
        boolean filterHasChildren = !filter.getChildElements().isEmpty();
        // copy to depth if this is leaf of filter tree
        Element copied = (Element) document.importNode(src.getDomElement(), !filterHasChildren);
        boolean shouldAppend = !filterHasChildren;
        if (filterHasChildren) {
            // this implies TAG_MATCH
            // do the same recursively
            int numberOfTextMatchingChildren = 0;
            for (XmlElement srcChild : src.getChildElements()) {
                for (XmlElement filterChild : filter.getChildElements()) {
                    MatchingResult childMatch = addSubtree2(filterChild, srcChild, XmlElement.fromDomElement(copied));
                    if (childMatch == MatchingResult.CONTENT_MISMATCH) {
                        return MatchingResult.NO_MATCH;
                    }
                    if (childMatch == MatchingResult.CONTENT_MATCH) {
                        numberOfTextMatchingChildren++;
                    }
                    shouldAppend |= childMatch != MatchingResult.NO_MATCH;
                }
            }
            // if only text matching child filters are specified..
            if (numberOfTextMatchingChildren == filter.getChildElements().size()) {
                // force all children to be added (to depth). This is done by copying parent node to depth.
                // implies shouldAppend == true
                copied = (Element) document.importNode(src.getDomElement(), true);
            }
        }
        if (shouldAppend) {
            dstParent.getDomElement().appendChild(copied);
        }
    }
    return matches;
}
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 8 with XmlElement

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

the class SubtreeFilterNotificationTest method testFilterNotification.

@Test
public void testFilterNotification() throws Exception {
    XmlElement filter = XmlElement.fromDomDocument(getDocument("filter.xml"));
    Document preFilterDocument = getDocument("pre-filter.xml");
    Document postFilterDocument = getDocument("post-filter.xml");
    Optional<Document> actualPostFilterDocumentOpt = SubtreeFilter.applySubtreeNotificationFilter(filter, preFilterDocument);
    if (actualPostFilterDocumentOpt.isPresent()) {
        Document actualPostFilterDocument = actualPostFilterDocumentOpt.get();
        LOG.info("Actual document: {}", XmlUtil.toString(actualPostFilterDocument));
        Diff diff = XMLUnit.compareXML(postFilterDocument, actualPostFilterDocument);
        assertTrue(diff.toString(), diff.similar());
    } else {
        assertEquals("empty", XmlElement.fromDomDocument(postFilterDocument).getName());
    }
}
Also used : Diff(org.custommonkey.xmlunit.Diff) XmlElement(org.opendaylight.netconf.api.xml.XmlElement) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 9 with XmlElement

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

the class NetconfUtil method checkIsMessageOk.

public static Document checkIsMessageOk(final Document response) throws DocumentedException {
    final XmlElement docElement = XmlElement.fromDomDocument(response);
    // FIXME: we should throw DocumentedException here
    checkState(XmlNetconfConstants.RPC_REPLY_KEY.equals(docElement.getName()));
    final XmlElement element = docElement.getOnlyChildElement();
    if (XmlNetconfConstants.OK.equals(element.getName())) {
        return response;
    }
    LOG.warn("Can not load last configuration. Operation failed.");
    // FIXME: we should be throwing a DocumentedException here
    throw new IllegalStateException("Can not load last configuration. Operation failed: " + XmlUtil.toString(response));
}
Also used : XmlElement(org.opendaylight.netconf.api.xml.XmlElement)

Example 10 with XmlElement

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

the class CreateSubscription method handleWithNoSubsequentOperations.

@Override
protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement operationElement) throws DocumentedException {
    operationElement.checkName(CREATE_SUBSCRIPTION);
    operationElement.checkNamespace(CreateSubscriptionInput.QNAME.getNamespace().toString());
    // FIXME reimplement using CODEC_REGISTRY and parse everything into generated class instance
    // Binding doesn't support anyxml nodes yet, so filter could not be retrieved
    // xml -> normalized node -> CreateSubscriptionInput conversion could be slower than current approach
    final Optional<XmlElement> filter = operationElement.getOnlyChildElementWithSameNamespaceOptionally("filter");
    // Replay not supported
    final Optional<XmlElement> startTime = operationElement.getOnlyChildElementWithSameNamespaceOptionally("startTime");
    checkArgument(startTime.isEmpty(), "StartTime element not yet supported");
    // Stop time not supported
    final Optional<XmlElement> stopTime = operationElement.getOnlyChildElementWithSameNamespaceOptionally("stopTime");
    checkArgument(stopTime.isEmpty(), "StopTime element not yet supported");
    final StreamNameType streamNameType = parseStreamIfPresent(operationElement);
    requireNonNull(netconfSession);
    // Premature streams are allowed (meaning listener can register even if no provider is available yet)
    if (!notifications.isStreamAvailable(streamNameType)) {
        LOG.warn("Registering premature stream {}. No publisher available yet for session {}", streamNameType, getNetconfSessionIdForReporting());
    }
    final NotificationListenerRegistration notificationListenerRegistration = notifications.registerNotificationListener(streamNameType, new NotificationSubscription(netconfSession, filter));
    subscriptions.add(notificationListenerRegistration);
    return document.createElement(XmlNetconfConstants.OK);
}
Also used : StreamNameType(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.StreamNameType) XmlElement(org.opendaylight.netconf.api.xml.XmlElement) NotificationListenerRegistration(org.opendaylight.netconf.notifications.NotificationListenerRegistration)

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