Search in sources :

Example 26 with XmlElement

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

the class NetconfMessageTransformUtil method stripNotification.

public static Map.Entry<Instant, XmlElement> stripNotification(final NetconfMessage message) {
    final XmlElement xmlElement = XmlElement.fromDomDocument(message.getDocument());
    final List<XmlElement> childElements = xmlElement.getChildElements();
    Preconditions.checkArgument(childElements.size() == 2, "Unable to parse notification %s, unexpected format." + "\nExpected 2 childElements, actual childElements size is %s", message, childElements.size());
    final XmlElement eventTimeElement;
    final XmlElement notificationElement;
    if (childElements.get(0).getName().equals(EVENT_TIME)) {
        eventTimeElement = childElements.get(0);
        notificationElement = childElements.get(1);
    } else if (childElements.get(1).getName().equals(EVENT_TIME)) {
        eventTimeElement = childElements.get(1);
        notificationElement = childElements.get(0);
    } else {
        throw new IllegalArgumentException("Notification payload does not contain " + EVENT_TIME + " " + message);
    }
    try {
        return new SimpleEntry<>(NetconfNotification.RFC3339_DATE_PARSER.apply(eventTimeElement.getTextContent()).toInstant(), notificationElement);
    } catch (final DocumentedException e) {
        throw new IllegalArgumentException("Notification payload does not contain " + EVENT_TIME + " " + message, e);
    } catch (final DateTimeParseException e) {
        LOG.warn("Unable to parse event time from {}. Setting time to {}", eventTimeElement, NetconfNotification.UNKNOWN_EVENT_TIME, e);
        return new SimpleEntry<>(NetconfNotification.UNKNOWN_EVENT_TIME.toInstant(), notificationElement);
    }
}
Also used : DateTimeParseException(java.time.format.DateTimeParseException) SimpleEntry(java.util.AbstractMap.SimpleEntry) NetconfDocumentedException(org.opendaylight.netconf.api.NetconfDocumentedException) DocumentedException(org.opendaylight.netconf.api.DocumentedException) XmlElement(org.opendaylight.netconf.api.xml.XmlElement)

Example 27 with XmlElement

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

the class SchemalessRpcStructureTransformer method checkDataValidForPath.

private static void checkDataValidForPath(final YangInstanceIdentifier dataPath, final Element dataNode) {
    // if datapath is empty, consider dataNode to be a root node
    if (dataPath.isEmpty()) {
        return;
    }
    final XmlElement dataElement = XmlElement.fromDomElement(dataNode);
    final PathArgument lastPathArgument = dataPath.getLastPathArgument();
    final QName nodeType = lastPathArgument.getNodeType();
    if (!nodeType.getNamespace().toString().equals(dataNode.getNamespaceURI()) || !nodeType.getLocalName().equals(dataElement.getName())) {
        throw new IllegalStateException(String.format("Can't write data '%s' to path %s", dataNode.getTagName(), dataPath));
    }
    if (lastPathArgument instanceof NodeIdentifierWithPredicates) {
        checkKeyValuesValidForPath(dataElement, lastPathArgument);
    }
}
Also used : QName(org.opendaylight.yangtools.yang.common.QName) XmlElement(org.opendaylight.netconf.api.xml.XmlElement) PathArgument(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument) NodeIdentifierWithPredicates(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates)

Example 28 with XmlElement

use of org.opendaylight.netconf.api.xml.XmlElement in project lighty-netconf-simulator by PANTHEONtech.

the class RpcHandlerImpl method getResponse.

@Override
public Optional<Document> getResponse(final XmlElement rpcElement) {
    final Element element = rpcElement.getDomElement();
    final String formattedRequest = RPCUtil.formatXml(element);
    LOG.debug("Received get request with payload:\n{} ", formattedRequest);
    final Optional<RequestProcessor> processorForRequestOpt = getProcessorForRequest(element);
    if (processorForRequestOpt.isPresent()) {
        return Optional.ofNullable(processorForRequestOpt.get().processRequest(element));
    }
    return Optional.empty();
}
Also used : XmlElement(org.opendaylight.netconf.api.xml.XmlElement) Element(org.w3c.dom.Element)

Example 29 with XmlElement

use of org.opendaylight.netconf.api.xml.XmlElement 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 30 with XmlElement

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

the class SchemalessRpcStructureTransformer method checkKeyValuesValidForPath.

private static void checkKeyValuesValidForPath(final XmlElement dataElement, final PathArgument lastPathArgument) {
    final NodeIdentifierWithPredicates keyedId = (NodeIdentifierWithPredicates) lastPathArgument;
    for (Entry<QName, Object> entry : keyedId.entrySet()) {
        QName qualifiedName = entry.getKey();
        final List<XmlElement> key = dataElement.getChildElementsWithinNamespace(qualifiedName.getLocalName(), qualifiedName.getNamespace().toString());
        if (key.isEmpty()) {
            throw new IllegalStateException("No key present in xml");
        }
        if (key.size() > 1) {
            throw new IllegalStateException("Multiple values for same key present");
        }
        final String textContent;
        try {
            textContent = key.get(0).getTextContent();
        } catch (DocumentedException e) {
            throw new IllegalStateException("Key value not present in key element", e);
        }
        if (!entry.getValue().equals(textContent)) {
            throw new IllegalStateException("Key value in path not equal to key value in xml");
        }
    }
}
Also used : QName(org.opendaylight.yangtools.yang.common.QName) DocumentedException(org.opendaylight.netconf.api.DocumentedException) XmlElement(org.opendaylight.netconf.api.xml.XmlElement) NodeIdentifierWithPredicates(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates)

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