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();
}
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();
}
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);
}
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);
}
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);
}
Aggregations