use of org.onosproject.restconf.api.RestconfError 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.RestconfError 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.RestconfError 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.RestconfError in project onos by opennetworkinglab.
the class RestconfWebResource method handlePatchRequest.
/**
* Handles a RESTCONF PATCH operation against a data resource.
* If the PATCH request succeeds, a "200 OK" status-line is returned if
* there is a message-body, and "204 No Content" is returned if no
* response message-body is sent.
*
* @param uriString URI of the parent data resource
* @param stream Input JSON object
* @return HTTP response
*/
@PATCH
@Consumes({ MediaTypeRestconf.APPLICATION_YANG_DATA_JSON, MediaType.APPLICATION_JSON })
@Produces(MediaTypeRestconf.APPLICATION_YANG_DATA_JSON)
@Path("data/{identifier : .+}")
public Response handlePatchRequest(@PathParam("identifier") String uriString, InputStream stream) {
log.debug("handlePatchRequest: {}", uriString);
URI uri = uriInfo.getRequestUri();
try {
ObjectNode rootNode = readTreeFromStream(mapper(), stream);
service.runPatchOperationOnDataResource(uri, rootNode);
return Response.ok().build();
} catch (JsonProcessingException e) {
log.error("ERROR: handlePatchRequest ", e);
RestconfError error = RestconfError.builder(RestconfError.ErrorType.APPLICATION, RestconfError.ErrorTag.MALFORMED_MESSAGE).errorMessage(e.getMessage()).errorAppTag("handlePatchRequest").build();
return Response.status(BAD_REQUEST).entity(RestconfError.wrapErrorAsJson(Arrays.asList(error))).build();
} catch (RestconfException e) {
log.error("ERROR: handlePatchRequest: {}", e.getMessage());
log.debug("Exception in handlePatchRequest:", e);
return Response.status(e.getResponse().getStatus()).entity(e.toRestconfErrorJson()).build();
} catch (IOException ex) {
log.error("ERROR: handlePatchRequest ", ex);
RestconfError error = RestconfError.builder(RestconfError.ErrorType.APPLICATION, RestconfError.ErrorTag.OPERATION_FAILED).errorMessage(ex.getMessage()).errorAppTag("handlePatchRequest").build();
return Response.status(INTERNAL_SERVER_ERROR).entity(RestconfError.wrapErrorAsJson(Arrays.asList(error))).build();
}
}
use of org.onosproject.restconf.api.RestconfError in project onos by opennetworkinglab.
the class RestconfWebResource method handlePutRequest.
/**
* Handles a RESTCONF PUT operation against a data resource. If a new
* resource is successfully created, then the HTTP status code "201 Created"
* is returned. If an existing resource is modified, then the HTTP
* status code "204 No Content" is returned. If the input JSON payload
* contains errors, then "400 Bad Request" is returned. If an exception
* occurs during the operation, the status code enclosed in
* the RestconfException object, such as "500 Internal Server Error",
* is returned.
*
* @param uriString URI of the data resource.
* @param stream Input JSON object
* @return HTTP response
*/
@PUT
@Consumes({ MediaTypeRestconf.APPLICATION_YANG_DATA_JSON, MediaType.APPLICATION_JSON })
@Produces(MediaTypeRestconf.APPLICATION_YANG_DATA_JSON)
@Path("data/{identifier : .+}")
public Response handlePutRequest(@PathParam("identifier") String uriString, InputStream stream) {
log.debug("handlePutRequest: {}", uriString);
URI uri = uriInfo.getRequestUri();
try {
ObjectNode rootNode = readTreeFromStream(mapper(), stream);
service.runPutOperationOnDataResource(uri, rootNode);
return Response.created(uriInfo.getRequestUri()).build();
} catch (JsonProcessingException e) {
log.error("ERROR: handlePutRequest ", e);
RestconfError error = RestconfError.builder(RestconfError.ErrorType.APPLICATION, RestconfError.ErrorTag.MALFORMED_MESSAGE).errorMessage(e.getMessage()).errorAppTag("handlePutRequest").build();
return Response.status(BAD_REQUEST).entity(RestconfError.wrapErrorAsJson(Arrays.asList(error))).build();
} catch (RestconfException e) {
log.error("ERROR: handlePutRequest: {}", e.getMessage());
log.debug("Exception in handlePutRequest:", e);
return Response.status(e.getResponse().getStatus()).entity(e.toRestconfErrorJson()).build();
} catch (IOException ex) {
log.error("ERROR: handlePutRequest ", ex);
RestconfError error = RestconfError.builder(RestconfError.ErrorType.APPLICATION, RestconfError.ErrorTag.OPERATION_FAILED).errorMessage(ex.getMessage()).errorAppTag("handlePutRequest").build();
return Response.status(INTERNAL_SERVER_ERROR).entity(RestconfError.wrapErrorAsJson(Arrays.asList(error))).build();
}
}
Aggregations