Search in sources :

Example 16 with RestconfError

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

the class RestconfDocumentedExceptionMapper method toResponse.

@Override
public Response toResponse(final RestconfDocumentedException exception) {
    LOG.debug("In toResponse: {}", exception.getMessage());
    final List<MediaType> mediaTypeList = new ArrayList<>();
    if (headers.getMediaType() != null) {
        mediaTypeList.add(headers.getMediaType());
    }
    mediaTypeList.addAll(headers.getAcceptableMediaTypes());
    final MediaType mediaType = mediaTypeList.stream().filter(type -> !type.equals(MediaType.WILDCARD_TYPE)).findFirst().orElse(MediaType.APPLICATION_JSON_TYPE);
    LOG.debug("Using MediaType: {}", mediaType);
    final List<RestconfError> errors = exception.getErrors();
    if (errors.isEmpty()) {
        return Response.status(exception.getStatus()).type(MediaType.TEXT_PLAIN_TYPE).entity(" ").build();
    }
    final Status status = ErrorTags.statusOf(errors.iterator().next().getErrorTag());
    final DataNodeContainer errorsSchemaNode = (DataNodeContainer) controllerContext.getRestconfModuleErrorsSchemaNode();
    if (errorsSchemaNode == null) {
        return Response.status(status).type(MediaType.TEXT_PLAIN_TYPE).entity(exception.getMessage()).build();
    }
    checkState(errorsSchemaNode instanceof ContainerSchemaNode, "Found Errors SchemaNode isn't ContainerNode");
    final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> errContBuild = SchemaAwareBuilders.containerBuilder((ContainerSchemaNode) errorsSchemaNode);
    final List<DataSchemaNode> schemaList = ControllerContext.findInstanceDataChildrenByName(errorsSchemaNode, RestConfModule.ERROR_LIST_SCHEMA_NODE);
    final DataSchemaNode errListSchemaNode = Iterables.getFirst(schemaList, null);
    checkState(errListSchemaNode instanceof ListSchemaNode, "Found Error SchemaNode isn't ListSchemaNode");
    final CollectionNodeBuilder<MapEntryNode, SystemMapNode> listErorsBuilder = SchemaAwareBuilders.mapBuilder((ListSchemaNode) errListSchemaNode);
    for (final RestconfError error : errors) {
        listErorsBuilder.withChild(toErrorEntryNode(error, errListSchemaNode));
    }
    errContBuild.withChild(listErorsBuilder.build());
    final NormalizedNodeContext errContext = new NormalizedNodeContext(new InstanceIdentifierContext<>(ERRORS, (DataSchemaNode) errorsSchemaNode, null, controllerContext.getGlobalSchema()), errContBuild.build());
    Object responseBody;
    if (mediaType.getSubtype().endsWith("json")) {
        responseBody = toJsonResponseBody(errContext, errorsSchemaNode);
    } else {
        responseBody = toXMLResponseBody(errContext, errorsSchemaNode);
    }
    return Response.status(status).type(mediaType).entity(responseBody).build();
}
Also used : Status(javax.ws.rs.core.Response.Status) SystemMapNode(org.opendaylight.yangtools.yang.data.api.schema.SystemMapNode) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) ArrayList(java.util.ArrayList) MapEntryNode(org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode) ContainerSchemaNode(org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode) NodeIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier) ListSchemaNode(org.opendaylight.yangtools.yang.model.api.ListSchemaNode) RestconfError(org.opendaylight.restconf.common.errors.RestconfError) MediaType(javax.ws.rs.core.MediaType) DataNodeContainer(org.opendaylight.yangtools.yang.model.api.DataNodeContainer) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode)

Example 17 with RestconfError

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

the class QueryParametersParser method parseWriterParameters.

public static WriterParameters parseWriterParameters(final UriInfo info) {
    final WriterParameters.WriterParametersBuilder wpBuilder = new WriterParameters.WriterParametersBuilder();
    if (info == null) {
        return wpBuilder.build();
    }
    String param = info.getQueryParameters(false).getFirst(UriParameters.DEPTH.toString());
    if (!Strings.isNullOrEmpty(param) && !"unbounded".equals(param)) {
        try {
            final int depth = Integer.parseInt(param);
            if (depth < 1) {
                throw new RestconfDocumentedException(new RestconfError(ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE, "Invalid depth parameter: " + depth, null, "The depth parameter must be an integer > 1 or \"unbounded\""));
            }
            wpBuilder.setDepth(depth);
        } catch (final NumberFormatException e) {
            throw new RestconfDocumentedException(e, new RestconfError(ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE, "Invalid depth parameter: " + e.getMessage(), null, "The depth parameter must be an integer > 1 or \"unbounded\""));
        }
    }
    param = info.getQueryParameters(false).getFirst(UriParameters.PRETTY_PRINT.toString());
    wpBuilder.setPrettyPrint("true".equals(param));
    return wpBuilder.build();
}
Also used : RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) WriterParameters(org.opendaylight.netconf.sal.rest.impl.WriterParameters) RestconfError(org.opendaylight.restconf.common.errors.RestconfError)

Example 18 with RestconfError

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

the class RestconfDocumentedExceptionMapperTest method testToJsonResponseWithMultipleErrors.

@Test
// FIXME : find why it return "error-type" RPC no expected APPLICATION
@Ignore
public void testToJsonResponseWithMultipleErrors() throws Exception {
    final List<RestconfError> errorList = Arrays.asList(new RestconfError(ErrorType.APPLICATION, ErrorTag.LOCK_DENIED, "mock error1"), new RestconfError(ErrorType.RPC, ErrorTag.ROLLBACK_FAILED, "mock error2"));
    stageMockEx(new RestconfDocumentedException("mock", null, errorList));
    final Response resp = target("/operational/foo").request(MediaType.APPLICATION_JSON).get();
    final InputStream stream = verifyResponse(resp, MediaType.APPLICATION_JSON, Status.CONFLICT);
    final JsonArray arrayElement = parseJsonErrorArrayElement(stream);
    assertEquals("\"error\" Json array element length", 2, arrayElement.size());
    verifyJsonErrorNode(arrayElement.get(0), ErrorType.APPLICATION, ErrorTag.LOCK_DENIED, "mock error1", null, null);
    verifyJsonErrorNode(arrayElement.get(1), ErrorType.RPC, ErrorTag.ROLLBACK_FAILED, "mock error2", null, null);
}
Also used : Response(javax.ws.rs.core.Response) JsonArray(com.google.gson.JsonArray) RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) RestconfError(org.opendaylight.restconf.common.errors.RestconfError) Ignore(org.junit.Ignore) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 19 with RestconfError

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

the class RestconfErrorTest method testRestConfDocumentedException_WithAppTagErrorInfo.

@Test
public void testRestConfDocumentedException_WithAppTagErrorInfo() {
    String expectedMessage = "Message";
    ErrorType expectedErrorType = ErrorType.RPC;
    ErrorTag expectedErrorTag = ErrorTag.IN_USE;
    String expectedErrorAppTag = "application.tag";
    String errorInfo = "<extra><sessionid>session.id</sessionid></extra>";
    RestconfError error = new RestconfError(expectedErrorType, expectedErrorTag, expectedMessage, expectedErrorAppTag, errorInfo);
    validateRestConfError(expectedMessage, expectedErrorType, expectedErrorTag, expectedErrorAppTag, errorInfo, error);
}
Also used : ErrorTag(org.opendaylight.yangtools.yang.common.ErrorTag) ErrorType(org.opendaylight.yangtools.yang.common.ErrorType) RestconfError(org.opendaylight.restconf.common.errors.RestconfError) Test(org.junit.Test)

Example 20 with RestconfError

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

the class RestconfErrorTest method testRestConfDocumentedException_NoCause.

@Test
public void testRestConfDocumentedException_NoCause() {
    String expectedMessage = "Message";
    ErrorType expectedErrorType = ErrorType.RPC;
    ErrorTag expectedErrorTag = ErrorTag.IN_USE;
    RestconfError error = new RestconfError(expectedErrorType, expectedErrorTag, expectedMessage);
    validateRestConfError(expectedMessage, expectedErrorType, expectedErrorTag, null, (String) null, error);
}
Also used : ErrorTag(org.opendaylight.yangtools.yang.common.ErrorTag) ErrorType(org.opendaylight.yangtools.yang.common.ErrorType) RestconfError(org.opendaylight.restconf.common.errors.RestconfError) Test(org.junit.Test)

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