Search in sources :

Example 26 with RestconfError

use of org.opendaylight.restconf.common.errors.RestconfError in project netconf by opendaylight.

the class QueryParams method newReadDataParams.

/**
 * Parse parameters from URI request and check their types and values.
 *
 * @param uriInfo    URI info
 * @return {@link ReadDataParams}
 */
@NonNull
public static ReadDataParams newReadDataParams(final UriInfo uriInfo) {
    ContentParam content = ContentParam.ALL;
    DepthParam depth = null;
    FieldsParam fields = null;
    WithDefaultsParam withDefaults = null;
    PrettyPrintParam prettyPrint = null;
    boolean tagged = false;
    for (Entry<String, List<String>> entry : uriInfo.getQueryParameters().entrySet()) {
        final String paramName = entry.getKey();
        final List<String> paramValues = entry.getValue();
        try {
            switch(paramName) {
                case ContentParam.uriName:
                    content = optionalParam(ContentParam::forUriValue, paramName, paramValues);
                    break;
                case DepthParam.uriName:
                    final String depthStr = optionalParam(paramName, paramValues);
                    try {
                        depth = DepthParam.forUriValue(depthStr);
                    } catch (IllegalArgumentException e) {
                        throw new RestconfDocumentedException(e, new RestconfError(ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE, "Invalid depth parameter: " + depthStr, null, "The depth parameter must be an integer between 1 and 65535 or \"unbounded\""));
                    }
                    break;
                case FieldsParam.uriName:
                    fields = optionalParam(FieldsParam::forUriValue, paramName, paramValues);
                    break;
                case WithDefaultsParam.uriName:
                    final var defaultsVal = optionalParam(WithDefaultsParam::forUriValue, paramName, paramValues);
                    if (defaultsVal != null) {
                        switch(defaultsVal) {
                            case REPORT_ALL:
                                withDefaults = null;
                                tagged = false;
                                break;
                            case REPORT_ALL_TAGGED:
                                withDefaults = null;
                                tagged = true;
                                break;
                            default:
                                withDefaults = defaultsVal;
                                tagged = false;
                        }
                    }
                    break;
                case PrettyPrintParam.uriName:
                    prettyPrint = optionalParam(PrettyPrintParam::forUriValue, paramName, paramValues);
                    break;
                default:
                    throw unhandledParam("read", paramName);
            }
        } catch (IllegalArgumentException e) {
            throw new RestconfDocumentedException("Invalid " + paramName + " value: " + e.getMessage(), ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE, e);
        }
    }
    return ReadDataParams.of(content, depth, fields, withDefaults, tagged, prettyPrint);
}
Also used : RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) WithDefaultsParam(org.opendaylight.restconf.nb.rfc8040.WithDefaultsParam) PrettyPrintParam(org.opendaylight.restconf.nb.rfc8040.PrettyPrintParam) DepthParam(org.opendaylight.restconf.nb.rfc8040.DepthParam) ContentParam(org.opendaylight.restconf.nb.rfc8040.ContentParam) RestconfError(org.opendaylight.restconf.common.errors.RestconfError) List(java.util.List) FieldsParam(org.opendaylight.restconf.nb.rfc8040.FieldsParam) Objects.requireNonNull(java.util.Objects.requireNonNull) NonNull(org.eclipse.jdt.annotation.NonNull)

Example 27 with RestconfError

use of org.opendaylight.restconf.common.errors.RestconfError in project netconf by opendaylight.

the class FutureCallbackTx method addCallback.

/**
 * Add callback to the future object and close transaction chain.
 *
 * @param listenableFuture
 *             future object
 * @param txType
 *             type of operation (READ, POST, PUT, DELETE)
 * @param dataFactory
 *             factory setting result
 * @param path unique identifier of a particular node instance in the data tree
 * @throws RestconfDocumentedException
 *             if the Future throws an exception
 */
// FIXME: this is a *synchronous operation* and has to die
static <T> void addCallback(final ListenableFuture<T> listenableFuture, final String txType, final FutureDataFactory<? super T> dataFactory, final YangInstanceIdentifier path) throws RestconfDocumentedException {
    try {
        final T result = listenableFuture.get();
        dataFactory.setResult(result);
        LOG.trace("Transaction({}) SUCCESSFUL", txType);
    } catch (InterruptedException e) {
        dataFactory.setFailureStatus();
        LOG.warn("Transaction({}) FAILED!", txType, e);
        throw new RestconfDocumentedException("Transaction failed", e);
    } catch (ExecutionException e) {
        dataFactory.setFailureStatus();
        LOG.warn("Transaction({}) FAILED!", txType, e);
        final Throwable cause = e.getCause();
        if (cause instanceof TransactionCommitFailedException) {
            /* If device send some error message we want this message to get to client
                   and not just to throw it away or override it with new generic message.
                   We search for NetconfDocumentedException that was send from netconfSB
                   and we create RestconfDocumentedException accordingly.
                */
            final List<Throwable> causalChain = Throwables.getCausalChain(cause);
            for (Throwable error : causalChain) {
                if (error instanceof DocumentedException) {
                    final ErrorTag errorTag = ((DocumentedException) error).getErrorTag();
                    if (errorTag.equals(ErrorTag.DATA_EXISTS)) {
                        LOG.trace("Operation via Restconf was not executed because data at {} already exists", path);
                        throw new RestconfDocumentedException(e, new RestconfError(ErrorType.PROTOCOL, ErrorTag.DATA_EXISTS, "Data already exists", path));
                    } else if (errorTag.equals(ErrorTag.DATA_MISSING)) {
                        LOG.trace("Operation via Restconf was not executed because data at {} does not exist", path);
                        throw new RestconfDocumentedException(e, new RestconfError(ErrorType.PROTOCOL, ErrorTag.DATA_MISSING, "Data does not exist", path));
                    }
                }
                if (error instanceof NetconfDocumentedException) {
                    throw new RestconfDocumentedException(error.getMessage(), ((NetconfDocumentedException) error).getErrorType(), ((NetconfDocumentedException) error).getErrorTag(), e);
                }
            }
            throw new RestconfDocumentedException("Transaction(" + txType + ") not committed correctly", e);
        } else {
            throw new RestconfDocumentedException("Transaction failed", e);
        }
    }
}
Also used : TransactionCommitFailedException(org.opendaylight.mdsal.common.api.TransactionCommitFailedException) RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) ErrorTag(org.opendaylight.yangtools.yang.common.ErrorTag) NetconfDocumentedException(org.opendaylight.netconf.api.NetconfDocumentedException) DocumentedException(org.opendaylight.netconf.api.DocumentedException) RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) RestconfError(org.opendaylight.restconf.common.errors.RestconfError) NetconfDocumentedException(org.opendaylight.netconf.api.NetconfDocumentedException) List(java.util.List) ExecutionException(java.util.concurrent.ExecutionException)

Example 28 with RestconfError

use of org.opendaylight.restconf.common.errors.RestconfError in project netconf by opendaylight.

the class TestXmlBodyReader method wrongRootElementTest.

/**
 * Test PUT operation when message root element is not the same as the last element in request URI.
 * PUT operation message should always start with schema node from URI otherwise exception should be
 * thrown.
 */
@Test
public void wrongRootElementTest() throws Exception {
    mockBodyReader("instance-identifier-module:cont", this.xmlBodyReader, false);
    final InputStream inputStream = TestXmlBodyReader.class.getResourceAsStream("/instanceidentifier/xml/bug7933.xml");
    try {
        this.xmlBodyReader.readFrom(null, null, null, this.mediaType, null, inputStream);
        Assert.fail("Test should fail due to malformed PUT operation message");
    } catch (final RestconfDocumentedException exception) {
        final RestconfError restconfError = exception.getErrors().get(0);
        assertEquals(ErrorType.PROTOCOL, restconfError.getErrorType());
        assertEquals(ErrorTag.MALFORMED_MESSAGE, restconfError.getErrorTag());
    }
}
Also used : RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) InputStream(java.io.InputStream) RestconfError(org.opendaylight.restconf.common.errors.RestconfError) Test(org.junit.Test)

Example 29 with RestconfError

use of org.opendaylight.restconf.common.errors.RestconfError in project netconf by opendaylight.

the class TestXmlBodyReaderMountPoint method wrongRootElementTest.

/**
 * Test PUT operation when message root element is not the same as the last element in request URI.
 * PUT operation message should always start with schema node from URI otherwise exception should be
 * thrown.
 */
@Test
public void wrongRootElementTest() throws Exception {
    mockBodyReader("instance-identifier-module:cont/yang-ext:mount", this.xmlBodyReader, false);
    final InputStream inputStream = TestXmlBodyReader.class.getResourceAsStream("/instanceidentifier/xml/bug7933.xml");
    try {
        this.xmlBodyReader.readFrom(null, null, null, this.mediaType, null, inputStream);
        Assert.fail("Test should fail due to malformed PUT operation message");
    } catch (final RestconfDocumentedException exception) {
        final RestconfError restconfError = exception.getErrors().get(0);
        assertEquals(ErrorType.PROTOCOL, restconfError.getErrorType());
        assertEquals(ErrorTag.MALFORMED_MESSAGE, restconfError.getErrorTag());
    }
}
Also used : RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) InputStream(java.io.InputStream) RestconfError(org.opendaylight.restconf.common.errors.RestconfError) Test(org.junit.Test)

Example 30 with RestconfError

use of org.opendaylight.restconf.common.errors.RestconfError in project netconf by opendaylight.

the class JSONRestconfServiceImpl method toRpcErrors.

private static RpcError[] toRpcErrors(final List<RestconfError> from) {
    final RpcError[] to = new RpcError[from.size()];
    int index = 0;
    for (final RestconfError e : from) {
        to[index++] = RpcResultBuilder.newError(e.getErrorType().toLegacy(), e.getErrorTag().elementBody(), e.getErrorMessage());
    }
    return to;
}
Also used : RpcError(org.opendaylight.yangtools.yang.common.RpcError) RestconfError(org.opendaylight.restconf.common.errors.RestconfError)

Aggregations

RestconfError (org.opendaylight.restconf.common.errors.RestconfError)30 RestconfDocumentedException (org.opendaylight.restconf.common.errors.RestconfDocumentedException)19 Test (org.junit.Test)16 InputStream (java.io.InputStream)6 JerseyTest (org.glassfish.jersey.test.JerseyTest)4 ErrorTag (org.opendaylight.yangtools.yang.common.ErrorTag)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Response (javax.ws.rs.core.Response)3 ErrorType (org.opendaylight.yangtools.yang.common.ErrorType)3 MediaType (javax.ws.rs.core.MediaType)2 Status (javax.ws.rs.core.Response.Status)2 Ignore (org.junit.Ignore)2 PatchEntity (org.opendaylight.restconf.common.patch.PatchEntity)2 PatchStatusContext (org.opendaylight.restconf.common.patch.PatchStatusContext)2 PatchStatusEntity (org.opendaylight.restconf.common.patch.PatchStatusEntity)2 RpcError (org.opendaylight.yangtools.yang.common.RpcError)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1