Search in sources :

Example 21 with DocumentedException

use of org.opendaylight.netconf.api.DocumentedException in project netconf by opendaylight.

the class SettableRpc method handle.

@Override
public Document handle(final Document requestMessage, final NetconfOperationChainedExecution subsequentOperation) throws DocumentedException {
    final XmlElement requestElement = XmlElement.fromDomDocument(requestMessage);
    final XmlElement rpcElement = requestElement.getOnlyChildElement();
    final String msgId = requestElement.getAttribute(XmlNetconfConstants.MESSAGE_ID);
    final Optional<Document> response = rpcHandler.getResponse(rpcElement);
    if (response.isPresent()) {
        final Document document = response.get();
        checkForError(document);
        document.getDocumentElement().setAttribute(XmlNetconfConstants.MESSAGE_ID, msgId);
        return document;
    } else if (subsequentOperation.isExecutionTermination()) {
        throw new DocumentedException("Mapping not found " + XmlUtil.toString(requestMessage), ErrorType.APPLICATION, ErrorTag.OPERATION_NOT_SUPPORTED, ErrorSeverity.ERROR);
    } else {
        return subsequentOperation.execute(requestMessage);
    }
}
Also used : DocumentedException(org.opendaylight.netconf.api.DocumentedException) XmlElement(org.opendaylight.netconf.api.xml.XmlElement) Document(org.w3c.dom.Document)

Example 22 with DocumentedException

use of org.opendaylight.netconf.api.DocumentedException in project netconf by opendaylight.

the class Get method handle.

@SuppressWarnings("checkstyle:IllegalCatch")
@Override
public Document handle(final Document requestMessage, final NetconfOperationChainedExecution subsequentOperation) throws DocumentedException {
    if (subsequentOperation.isExecutionTermination()) {
        throw new DocumentedException(String.format("Subsequent netconf operation expected by %s", this), ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED, ErrorSeverity.ERROR);
    }
    try {
        final Document innerResult = subsequentOperation.execute(requestMessage);
        final NetconfState netconfMonitoring = new NetconfState(netconfMonitor);
        Element monitoringXmlElement = new JaxBSerializer().toXml(netconfMonitoring);
        monitoringXmlElement = (Element) innerResult.importNode(monitoringXmlElement, true);
        final Element monitoringXmlElementPlaceholder = getPlaceholder(innerResult);
        monitoringXmlElementPlaceholder.appendChild(monitoringXmlElement);
        return innerResult;
    } catch (final RuntimeException e) {
        final String errorMessage = "Get operation for netconf-state subtree failed";
        LOG.warn(errorMessage, e);
        throw new DocumentedException(errorMessage, e, ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED, ErrorSeverity.ERROR, // FIXME: i.e. <error>e.getMessage()</error> ?
        Map.of(ErrorSeverity.ERROR.elementBody(), e.getMessage()));
    }
}
Also used : DocumentedException(org.opendaylight.netconf.api.DocumentedException) XmlElement(org.opendaylight.netconf.api.xml.XmlElement) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document)

Example 23 with DocumentedException

use of org.opendaylight.netconf.api.DocumentedException in project netconf by opendaylight.

the class XmlElementTest method testUnrecognisedElements.

@Test
public void testUnrecognisedElements() throws Exception {
    xmlElement.checkUnrecognisedElements(xmlElement.getOnlyChildElement("inner"), xmlElement.getOnlyChildElement("innerPrefixed"), xmlElement.getOnlyChildElement("innerNamespace"));
    final DocumentedException e = assertThrows(DocumentedException.class, () -> xmlElement.checkUnrecognisedElements(xmlElement.getOnlyChildElement("inner")));
    assertThat(e.getMessage(), // FIXME: this looks very suspect
    both(containsString("innerNamespace")).and(containsString("innerNamespace")));
}
Also used : DocumentedException(org.opendaylight.netconf.api.DocumentedException) Test(org.junit.Test)

Example 24 with DocumentedException

use of org.opendaylight.netconf.api.DocumentedException 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 25 with DocumentedException

use of org.opendaylight.netconf.api.DocumentedException in project netconf by opendaylight.

the class XmlElement method extractNamespaces.

private Map<String, String> extractNamespaces() throws DocumentedException {
    Map<String, String> namespaces = new HashMap<>();
    NamedNodeMap attributes = element.getAttributes();
    for (int i = 0; i < attributes.getLength(); i++) {
        Node attribute = attributes.item(i);
        String attribKey = attribute.getNodeName();
        if (attribKey.startsWith(XMLConstants.XMLNS_ATTRIBUTE)) {
            String prefix;
            if (attribKey.equals(XMLConstants.XMLNS_ATTRIBUTE)) {
                prefix = DEFAULT_NAMESPACE_PREFIX;
            } else {
                if (!attribKey.startsWith(XMLConstants.XMLNS_ATTRIBUTE + ":")) {
                    throw new DocumentedException("Attribute doesn't start with :", ErrorType.APPLICATION, ErrorTag.INVALID_VALUE, ErrorSeverity.ERROR);
                }
                prefix = attribKey.substring(XMLConstants.XMLNS_ATTRIBUTE.length() + 1);
            }
            namespaces.put(prefix, attribute.getNodeValue());
        }
    }
    // namespace does not have to be defined on this element but inherited
    if (!namespaces.containsKey(DEFAULT_NAMESPACE_PREFIX)) {
        Optional<String> namespaceOptionally = getNamespaceOptionally();
        if (namespaceOptionally.isPresent()) {
            namespaces.put(DEFAULT_NAMESPACE_PREFIX, namespaceOptionally.get());
        }
    }
    return namespaces;
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) HashMap(java.util.HashMap) DocumentedException(org.opendaylight.netconf.api.DocumentedException) Node(org.w3c.dom.Node)

Aggregations

DocumentedException (org.opendaylight.netconf.api.DocumentedException)54 Test (org.junit.Test)25 XmlElement (org.opendaylight.netconf.api.xml.XmlElement)14 Document (org.w3c.dom.Document)9 NetconfDocumentedException (org.opendaylight.netconf.api.NetconfDocumentedException)6 ExecutionException (java.util.concurrent.ExecutionException)5 NormalizedNode (org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)5 Element (org.w3c.dom.Element)5 ContainerNode (org.opendaylight.yangtools.yang.data.api.schema.ContainerNode)4 List (java.util.List)3 XMLNamespace (org.opendaylight.yangtools.yang.common.XMLNamespace)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 Reader (java.io.Reader)2 URI (java.net.URI)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 CompletableFuture (java.util.concurrent.CompletableFuture)2