Search in sources :

Example 1 with ErrorTag

use of org.opendaylight.yangtools.yang.common.ErrorTag in project netconf by opendaylight.

the class RestconfErrorTest method testRestConfErrorWithRpcError.

@Test
public void testRestConfErrorWithRpcError() {
    // All fields set
    RpcError rpcError = RpcResultBuilder.newError(RpcError.ErrorType.PROTOCOL, ErrorTag.BAD_ATTRIBUTE.elementBody(), "mock error-message", "mock app-tag", "mock error-info", new Exception("mock cause"));
    validateRestConfError("mock error-message", ErrorType.PROTOCOL, ErrorTag.BAD_ATTRIBUTE, "mock app-tag", "mock error-info", new RestconfError(rpcError));
    // All fields set except 'info' - expect error-info set to 'cause'
    rpcError = RpcResultBuilder.newError(RpcError.ErrorType.PROTOCOL, ErrorTag.BAD_ATTRIBUTE.elementBody(), "mock error-message", "mock app-tag", null, new Exception("mock cause"));
    validateRestConfError("mock error-message", ErrorType.PROTOCOL, ErrorTag.BAD_ATTRIBUTE, "mock app-tag", new Contains("mock cause"), new RestconfError(rpcError));
    // Some fields set - expect error-info set to ErrorSeverity
    rpcError = RpcResultBuilder.newError(RpcError.ErrorType.RPC, ErrorTag.ACCESS_DENIED.elementBody(), null, null, null, null);
    validateRestConfError(null, ErrorType.RPC, ErrorTag.ACCESS_DENIED, null, "<severity>error</severity>", new RestconfError(rpcError));
    // 'tag' field not mapped to ErrorTag - expect error-tag set to OPERATION_FAILED
    rpcError = RpcResultBuilder.newWarning(RpcError.ErrorType.TRANSPORT, "not mapped", null, null, null, null);
    validateRestConfError(null, ErrorType.TRANSPORT, new ErrorTag("not mapped"), null, "<severity>warning</severity>", new RestconfError(rpcError));
    // No fields set - edge case
    rpcError = RpcResultBuilder.newError(RpcError.ErrorType.APPLICATION, null, null, null, null, null);
    validateRestConfError(null, ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED, null, "<severity>error</severity>", new RestconfError(rpcError));
}
Also used : ErrorTag(org.opendaylight.yangtools.yang.common.ErrorTag) RpcError(org.opendaylight.yangtools.yang.common.RpcError) RestconfError(org.opendaylight.restconf.common.errors.RestconfError) Test(org.junit.Test)

Example 2 with ErrorTag

use of org.opendaylight.yangtools.yang.common.ErrorTag in project netconf by opendaylight.

the class InvokeRpcMethodTest method testInvokeRpcWithNoPayloadRpc_FailWithRpcError.

@Test
public void testInvokeRpcWithNoPayloadRpc_FailWithRpcError() {
    final List<RpcError> rpcErrors = Arrays.asList(RpcResultBuilder.newError(RpcError.ErrorType.TRANSPORT, "bogusTag", "foo"), RpcResultBuilder.newWarning(RpcError.ErrorType.RPC, "in-use", "bar", "app-tag", null, null));
    final DOMRpcResult result = new DefaultDOMRpcResult(rpcErrors);
    final QName path = QName.create("(http://netconfcentral.org/ns/toaster?revision=2009-11-20)cancel-toast");
    doReturn(immediateFluentFuture(result)).when(brokerFacade).invokeRpc(eq(path), any());
    final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class, () -> this.restconfImpl.invokeRpc("toaster:cancel-toast", null, uriInfo));
    // We are performing pass-through here of error-tag, hence the tag remains as specified, but we want to make
    // sure the HTTP status remains the same as
    final ErrorTag bogus = new ErrorTag("bogusTag");
    verifyRestconfDocumentedException(ex, 0, ErrorType.TRANSPORT, bogus, Optional.of("foo"), Optional.empty());
    assertEquals(ErrorTags.statusOf(ErrorTag.OPERATION_FAILED), ErrorTags.statusOf(bogus));
    verifyRestconfDocumentedException(ex, 1, ErrorType.RPC, ErrorTag.IN_USE, Optional.of("bar"), Optional.of("app-tag"));
}
Also used : DefaultDOMRpcResult(org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult) RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) ErrorTag(org.opendaylight.yangtools.yang.common.ErrorTag) DefaultDOMRpcResult(org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult) DOMRpcResult(org.opendaylight.mdsal.dom.api.DOMRpcResult) QName(org.opendaylight.yangtools.yang.common.QName) RpcError(org.opendaylight.yangtools.yang.common.RpcError) Test(org.junit.Test)

Example 3 with ErrorTag

use of org.opendaylight.yangtools.yang.common.ErrorTag in project netconf by opendaylight.

the class DocumentedException method fromXMLDocument.

public static DocumentedException fromXMLDocument(final Document fromDoc) {
    ErrorType errorType = ErrorType.APPLICATION;
    ErrorTag errorTag = ErrorTag.OPERATION_FAILED;
    ErrorSeverity errorSeverity = ErrorSeverity.ERROR;
    Map<String, String> errorInfo = null;
    String errorMessage = "";
    String allErrorMessages = "";
    Node rpcReply = fromDoc.getDocumentElement();
    // FIXME: we only handle one rpc-error. For now, shove extra errorMessages found in multiple rpc-error in the
    // errorInfo Map to at least let them propagate back to caller.
    // this will be solved through migration to YangNetconfErrorAware, as that allows reporting multipl
    // error events
    int rpcErrorCount = 0;
    NodeList replyChildren = rpcReply.getChildNodes();
    for (int i = 0; i < replyChildren.getLength(); i++) {
        Node replyChild = replyChildren.item(i);
        if (RPC_ERROR.equals(replyChild.getLocalName())) {
            rpcErrorCount++;
            NodeList rpcErrorChildren = replyChild.getChildNodes();
            for (int j = 0; j < rpcErrorChildren.getLength(); j++) {
                Node rpcErrorChild = rpcErrorChildren.item(j);
                // FIXME: use a switch expression here
                if (ERROR_TYPE.equals(rpcErrorChild.getLocalName())) {
                    final ErrorType type = ErrorType.forElementBody(rpcErrorChild.getTextContent());
                    // FIXME: this should be a hard error
                    errorType = type != null ? type : ErrorType.APPLICATION;
                } else if (ERROR_TAG.equals(rpcErrorChild.getLocalName())) {
                    errorTag = new ErrorTag(rpcErrorChild.getTextContent());
                } else if (ERROR_SEVERITY.equals(rpcErrorChild.getLocalName())) {
                    final ErrorSeverity sev = ErrorSeverity.forElementBody(rpcErrorChild.getTextContent());
                    // FIXME: this should be a hard error
                    errorSeverity = sev != null ? sev : ErrorSeverity.ERROR;
                } else if (ERROR_MESSAGE.equals(rpcErrorChild.getLocalName())) {
                    errorMessage = rpcErrorChild.getTextContent();
                    allErrorMessages = allErrorMessages + errorMessage;
                } else if (ERROR_INFO.equals(rpcErrorChild.getLocalName())) {
                    errorInfo = parseErrorInfo(rpcErrorChild);
                }
            }
        }
    }
    if (rpcErrorCount > 1) {
        if (errorInfo == null) {
            errorInfo = new HashMap<>();
        }
        errorInfo.put("Multiple Errors Found", allErrorMessages);
    }
    return new DocumentedException(errorMessage, errorType, errorTag, errorSeverity, errorInfo);
}
Also used : ErrorTag(org.opendaylight.yangtools.yang.common.ErrorTag) ErrorType(org.opendaylight.yangtools.yang.common.ErrorType) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) ErrorSeverity(org.opendaylight.yangtools.yang.common.ErrorSeverity)

Example 4 with ErrorTag

use of org.opendaylight.yangtools.yang.common.ErrorTag 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 5 with ErrorTag

use of org.opendaylight.yangtools.yang.common.ErrorTag in project netconf by opendaylight.

the class AbstractWriteTx method extractResult.

@SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD", justification = "https://github.com/spotbugs/spotbugs/issues/811")
private void extractResult(final List<DOMRpcResult> domRpcResults, final SettableFuture<RpcResult<Void>> transformed) {
    ErrorType errType = ErrorType.APPLICATION;
    ErrorSeverity errSeverity = ErrorSeverity.ERROR;
    StringBuilder msgBuilder = new StringBuilder();
    boolean errorsEncouneterd = false;
    String errorTag = "operation-failed";
    for (final DOMRpcResult domRpcResult : domRpcResults) {
        if (!domRpcResult.getErrors().isEmpty()) {
            errorsEncouneterd = true;
            final RpcError error = domRpcResult.getErrors().iterator().next();
            errType = error.getErrorType().toNetconf();
            errSeverity = error.getSeverity().toNetconf();
            msgBuilder.append(error.getMessage());
            msgBuilder.append(error.getInfo());
            errorTag = error.getTag();
        }
    }
    if (errorsEncouneterd) {
        final NetconfDocumentedException exception = new NetconfDocumentedException(id + ":RPC during tx failed. " + msgBuilder, errType, new ErrorTag(errorTag), errSeverity);
        transformed.setException(exception);
        return;
    }
    transformed.set(RpcResultBuilder.<Void>success().build());
}
Also used : ErrorTag(org.opendaylight.yangtools.yang.common.ErrorTag) ErrorType(org.opendaylight.yangtools.yang.common.ErrorType) DOMRpcResult(org.opendaylight.mdsal.dom.api.DOMRpcResult) RpcError(org.opendaylight.yangtools.yang.common.RpcError) NetconfDocumentedException(org.opendaylight.netconf.api.NetconfDocumentedException) ErrorSeverity(org.opendaylight.yangtools.yang.common.ErrorSeverity) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Aggregations

ErrorTag (org.opendaylight.yangtools.yang.common.ErrorTag)10 ErrorType (org.opendaylight.yangtools.yang.common.ErrorType)6 Test (org.junit.Test)5 RestconfError (org.opendaylight.restconf.common.errors.RestconfError)5 RpcError (org.opendaylight.yangtools.yang.common.RpcError)4 NetconfDocumentedException (org.opendaylight.netconf.api.NetconfDocumentedException)3 ErrorSeverity (org.opendaylight.yangtools.yang.common.ErrorSeverity)3 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)2 TransactionCommitFailedException (org.opendaylight.mdsal.common.api.TransactionCommitFailedException)2 DOMRpcResult (org.opendaylight.mdsal.dom.api.DOMRpcResult)2 DocumentedException (org.opendaylight.netconf.api.DocumentedException)2 RestconfDocumentedException (org.opendaylight.restconf.common.errors.RestconfDocumentedException)2 List (java.util.List)1 StringJoiner (java.util.StringJoiner)1 ExecutionException (java.util.concurrent.ExecutionException)1 DefaultDOMRpcResult (org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult)1 QName (org.opendaylight.yangtools.yang.common.QName)1 Node (org.w3c.dom.Node)1 NodeList (org.w3c.dom.NodeList)1