Search in sources :

Example 26 with DocumentedException

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

Example 27 with DocumentedException

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

the class DefaultCloseSessionTest method testDefaultCloseSession2.

@Test
public void testDefaultCloseSession2() throws Exception {
    final NetconfDocumentedException expectedCause = new NetconfDocumentedException("testMessage");
    final AutoCloseable res = mock(AutoCloseable.class);
    doThrow(expectedCause).when(res).close();
    final DefaultCloseSession session = new DefaultCloseSession("testSession", res);
    XmlElement elem = XmlElement.fromDomElement(XmlUtil.readXmlToElement("<elem/>"));
    final DocumentedException ex = assertThrows(DocumentedException.class, () -> session.handleWithNoSubsequentOperations(XmlUtil.newDocument(), elem));
    assertEquals("Unable to properly close session testSession", ex.getMessage());
    assertSame(expectedCause, ex.getCause());
}
Also used : NetconfDocumentedException(org.opendaylight.netconf.api.NetconfDocumentedException) DocumentedException(org.opendaylight.netconf.api.DocumentedException) NetconfDocumentedException(org.opendaylight.netconf.api.NetconfDocumentedException) XmlElement(org.opendaylight.netconf.api.xml.XmlElement) Test(org.junit.Test)

Example 28 with DocumentedException

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

the class NetconfOperationRouterImplTest method testOnNetconfMessageFail.

@Test
public void testOnNetconfMessageFail() throws Exception {
    final DocumentedException ex = assertThrows(DocumentedException.class, () -> emptyOperationRouter.onNetconfMessage(TEST_RPC_DOC, null));
    assertEquals(ErrorTag.OPERATION_NOT_SUPPORTED, ex.getErrorTag());
}
Also used : DocumentedException(org.opendaylight.netconf.api.DocumentedException) Test(org.junit.Test)

Example 29 with DocumentedException

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

the class NetconfOperationRouterImpl method onNetconfMessage.

@SuppressWarnings("checkstyle:IllegalCatch")
@Override
public Document onNetconfMessage(final Document message, final NetconfServerSession session) throws DocumentedException {
    requireNonNull(allNetconfOperations, "Operation router was not initialized properly");
    final NetconfOperationExecution netconfOperationExecution;
    try {
        netconfOperationExecution = getNetconfOperationWithHighestPriority(message, session);
    } catch (IllegalArgumentException | IllegalStateException e) {
        final String messageAsString = XmlUtil.toString(message);
        LOG.warn("Unable to handle rpc {} on session {}", messageAsString, session, e);
        final ErrorTag tag = e instanceof IllegalArgumentException ? ErrorTag.OPERATION_NOT_SUPPORTED : ErrorTag.OPERATION_FAILED;
        throw new DocumentedException(String.format("Unable to handle rpc %s on session %s", messageAsString, session), e, ErrorType.APPLICATION, tag, ErrorSeverity.ERROR, // </java-throwable>
        Map.of(tag.elementBody(), e.getMessage()));
    } catch (final RuntimeException e) {
        throw handleUnexpectedEx("sort", e);
    }
    try {
        return executeOperationWithHighestPriority(message, netconfOperationExecution);
    } catch (final RuntimeException e) {
        throw handleUnexpectedEx("execution", e);
    }
}
Also used : ErrorTag(org.opendaylight.yangtools.yang.common.ErrorTag) DocumentedException(org.opendaylight.netconf.api.DocumentedException)

Example 30 with DocumentedException

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

the class DeserializerExceptionHandler method handleDeserializerException.

private static void handleDeserializerException(final ChannelHandlerContext ctx, final Throwable cause) {
    final String message = cause.getMessage();
    SendErrorExceptionUtil.sendErrorMessage(ctx.channel(), new DocumentedException(message, ErrorType.RPC, ErrorTag.MALFORMED_MESSAGE, ErrorSeverity.ERROR, message != null ? Map.of("cause", message) : Map.of()));
}
Also used : DocumentedException(org.opendaylight.netconf.api.DocumentedException)

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