use of org.onosproject.restconf.api.RestconfException in project onos by opennetworkinglab.
the class RestconfWebResource method handlePostRequest.
/**
* Handles a RESTCONF POST operation against a data resource. If the
* operation is successful, HTTP status code "201 Created" is returned
* and there is no response message-body. If the data resource already
* exists, then the HTTP status code "409 Conflict" is returned.
*
* @param uriString URI of the parent data resource
* @param stream Input JSON object
* @return HTTP response
*/
@POST
@Consumes({ MediaTypeRestconf.APPLICATION_YANG_DATA_JSON, MediaType.APPLICATION_JSON })
@Produces(MediaTypeRestconf.APPLICATION_YANG_DATA_JSON)
@Path("data/{identifier : .+}")
public Response handlePostRequest(@PathParam("identifier") String uriString, InputStream stream) {
log.debug("handlePostRequest: {}", uriString);
URI uri = uriInfo.getRequestUri();
try {
ObjectNode rootNode = readTreeFromStream(mapper(), stream);
service.runPostOperationOnDataResource(uri, rootNode);
return Response.created(uriInfo.getRequestUri()).build();
} catch (JsonProcessingException e) {
log.error("ERROR: handlePostRequest ", e);
RestconfError error = RestconfError.builder(RestconfError.ErrorType.APPLICATION, RestconfError.ErrorTag.MALFORMED_MESSAGE).errorMessage(e.getMessage()).errorAppTag("handlePostRequest").build();
return Response.status(BAD_REQUEST).entity(RestconfError.wrapErrorAsJson(Arrays.asList(error))).build();
} catch (RestconfException e) {
log.error("ERROR: handlePostRequest: {}", e.getMessage());
log.debug("Exception in handlePostRequest:", e);
return Response.status(e.getResponse().getStatus()).entity(e.toRestconfErrorJson()).build();
} catch (IOException ex) {
log.error("ERROR: handlePostRequest ", ex);
RestconfError error = RestconfError.builder(RestconfError.ErrorType.APPLICATION, RestconfError.ErrorTag.OPERATION_FAILED).errorMessage(ex.getMessage()).errorAppTag("handlePostRequest").build();
return Response.status(INTERNAL_SERVER_ERROR).entity(RestconfError.wrapErrorAsJson(Arrays.asList(error))).build();
}
}
use of org.onosproject.restconf.api.RestconfException in project onos by opennetworkinglab.
the class RestconfWebResource method handleRpcRequest.
/**
* Handles a RESTCONF RPC request. This function executes the RPC in
* the target application's context and returns the results as a Future.
*
* @param rpcName Name of the RPC
* @param rpcInput Input parameters
* @param request RESTCONF client information from which the client IP
* address is retrieved
* @return RPC output
*/
@POST
@Consumes({ MediaTypeRestconf.APPLICATION_YANG_DATA_JSON, MediaType.APPLICATION_JSON })
@Produces(MediaTypeRestconf.APPLICATION_YANG_DATA_JSON)
@Path("operations/{rpc : .+}")
public Response handleRpcRequest(@PathParam("rpc") String rpcName, InputStream rpcInput, @Context HttpServletRequest request) {
URI uri = uriInfo.getRequestUri();
try {
ObjectNode inputNode = readTreeFromStream(mapper(), rpcInput);
CompletableFuture<RestconfRpcOutput> rpcFuture = service.runRpc(uri, inputNode, request.getRemoteAddr());
RestconfRpcOutput restconfRpcOutput;
restconfRpcOutput = rpcFuture.get();
if (restconfRpcOutput.status() != OK) {
return Response.status(restconfRpcOutput.status()).entity(restconfRpcOutput.reason()).build();
}
ObjectNode node = restconfRpcOutput.output();
return ok(node).build();
} catch (JsonProcessingException e) {
log.error("ERROR: handleRpcRequest", e);
RestconfError error = RestconfError.builder(RestconfError.ErrorType.APPLICATION, RestconfError.ErrorTag.MALFORMED_MESSAGE).errorMessage(e.getMessage()).errorAppTag("handleRpcRequest").build();
return Response.status(BAD_REQUEST).entity(RestconfError.wrapErrorAsJson(Arrays.asList(error))).build();
} catch (RestconfException e) {
log.error("ERROR: handleRpcRequest: {}", e.getMessage());
log.debug("Exception in handleRpcRequest:", e);
return Response.status(e.getResponse().getStatus()).entity(e.toRestconfErrorJson()).build();
} catch (Exception e) {
log.error("ERROR: handleRpcRequest ", e);
RestconfError error = RestconfError.builder(RestconfError.ErrorType.APPLICATION, RestconfError.ErrorTag.OPERATION_FAILED).errorMessage(e.getMessage()).errorAppTag("handleRpcRequest").build();
return Response.status(INTERNAL_SERVER_ERROR).entity(RestconfError.wrapErrorAsJson(Arrays.asList(error))).build();
}
}
use of org.onosproject.restconf.api.RestconfException in project onos by opennetworkinglab.
the class RestconfWebResource method handleGetRequest.
/**
* Handles a RESTCONF GET operation against a target data resource. If the
* operation is successful, the JSON presentation of the resource plus HTTP
* status code "200 OK" is returned. If it is not found then "404 Not Found"
* is returned. On internal error "500 Internal Server Error" is returned.
*
* @param uriString URI of the data resource.
* @return HTTP response - 200, 404 or 500
*/
@GET
@Produces(MediaTypeRestconf.APPLICATION_YANG_DATA_JSON)
@Path("data/{identifier : .+}")
public Response handleGetRequest(@PathParam("identifier") String uriString) {
log.debug("handleGetRequest: {}", uriString);
URI uri = uriInfo.getRequestUri();
try {
ObjectNode node = service.runGetOperationOnDataResource(uri);
if (node == null) {
RestconfError error = RestconfError.builder(RestconfError.ErrorType.PROTOCOL, RestconfError.ErrorTag.INVALID_VALUE).errorMessage("Resource not found").errorPath(uriString).errorAppTag("handleGetRequest").build();
return Response.status(NOT_FOUND).entity(RestconfError.wrapErrorAsJson(Arrays.asList(error))).build();
}
return Response.ok(node).build();
} catch (RestconfException e) {
log.error("ERROR: handleGetRequest: {}", e.getMessage());
log.debug("Exception in handleGetRequest:", e);
return Response.status(e.getResponse().getStatus()).entity(e.toRestconfErrorJson()).build();
} catch (Exception e) {
RestconfError error = RestconfError.builder(RestconfError.ErrorType.APPLICATION, RestconfError.ErrorTag.OPERATION_FAILED).errorMessage(e.getMessage()).errorAppTag("handlePostRequest").build();
return Response.status(INTERNAL_SERVER_ERROR).entity(RestconfError.wrapErrorAsJson(Arrays.asList(error))).build();
}
}
use of org.onosproject.restconf.api.RestconfException in project onos by opennetworkinglab.
the class RestconfWebResourceTest method testHandleGetRequestRestconfException.
/**
* Test handleGetRequest when an RestconfException is thrown.
*/
@Test
public void testHandleGetRequestRestconfException() {
expect(restconfService.runGetOperationOnDataResource(URI.create(getBaseUri() + DATA_IETF_SYSTEM_SYSTEM))).andThrow(new RestconfException("Suitable error message", RestconfError.ErrorTag.OPERATION_FAILED, INTERNAL_SERVER_ERROR, Optional.of("/" + DATA_IETF_SYSTEM_SYSTEM), Optional.of("More info about the error"))).anyTimes();
replay(restconfService);
WebTarget wt = target();
try {
String response = wt.path("/" + DATA_IETF_SYSTEM_SYSTEM).request().get(String.class);
fail("Expecting fail as response is RestconfException");
} catch (InternalServerErrorException e) {
assertNotNull(e.getResponse());
assertRestconfErrorJson(e.getResponse());
}
}
use of org.onosproject.restconf.api.RestconfException in project onos by opennetworkinglab.
the class RestconfWebResourceTest method testHandlePostRequestAlreadyExists.
/**
* Test handlePostRequest with 'already exists' exception.
*/
@Test
public void testHandlePostRequestAlreadyExists() {
ObjectMapper mapper = new ObjectMapper();
ObjectNode ietfSystemSubNode = mapper.createObjectNode();
ietfSystemSubNode.put("contact", "Open Networking Foundation");
ietfSystemSubNode.put("hostname", "host1");
ietfSystemSubNode.put("location", "The moon");
ObjectNode ietfSystemNode = mapper.createObjectNode();
ietfSystemNode.put("ietf-system:system", ietfSystemSubNode);
restconfService.runPostOperationOnDataResource(EasyMock.<URI>anyObject(), EasyMock.<ObjectNode>anyObject());
expectLastCall().andThrow(new RestconfException("Requested node already present", null, RestconfError.ErrorTag.DATA_EXISTS, CONFLICT, Optional.of("/" + DATA_IETF_SYSTEM_SYSTEM)));
replay(restconfService);
WebTarget wt = target();
Response response = wt.path("/" + DATA_IETF_SYSTEM_SYSTEM).request().post(Entity.json(ietfSystemNode.toString()));
assertEquals(409, response.getStatus());
}
Aggregations