Search in sources :

Example 6 with RestconfException

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();
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) RestconfException(org.onosproject.restconf.api.RestconfException) RestconfError(org.onosproject.restconf.api.RestconfError) IOException(java.io.IOException) URI(java.net.URI) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 7 with RestconfException

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();
    }
}
Also used : RestconfRpcOutput(org.onosproject.restconf.api.RestconfRpcOutput) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) RestconfException(org.onosproject.restconf.api.RestconfException) RestconfError(org.onosproject.restconf.api.RestconfError) URI(java.net.URI) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) RestconfException(org.onosproject.restconf.api.RestconfException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 8 with RestconfException

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();
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) RestconfException(org.onosproject.restconf.api.RestconfException) RestconfError(org.onosproject.restconf.api.RestconfError) URI(java.net.URI) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) RestconfException(org.onosproject.restconf.api.RestconfException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 9 with RestconfException

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());
    }
}
Also used : RestconfException(org.onosproject.restconf.api.RestconfException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) WebTarget(javax.ws.rs.client.WebTarget) ResourceTest(org.onosproject.rest.resources.ResourceTest) Test(org.junit.Test)

Example 10 with RestconfException

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());
}
Also used : Response(javax.ws.rs.core.Response) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) RestconfException(org.onosproject.restconf.api.RestconfException) WebTarget(javax.ws.rs.client.WebTarget) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ResourceTest(org.onosproject.rest.resources.ResourceTest) Test(org.junit.Test)

Aggregations

RestconfException (org.onosproject.restconf.api.RestconfException)16 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)9 IOException (java.io.IOException)9 URI (java.net.URI)6 Path (javax.ws.rs.Path)6 Produces (javax.ws.rs.Produces)6 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)5 RestconfError (org.onosproject.restconf.api.RestconfError)5 DefaultResourceData (org.onosproject.yang.model.DefaultResourceData)5 ResourceData (org.onosproject.yang.model.ResourceData)5 Consumes (javax.ws.rs.Consumes)4 FailedException (org.onosproject.config.FailedException)4 RestconfUtils.convertJsonToDataNode (org.onosproject.restconf.utils.RestconfUtils.convertJsonToDataNode)4 DataNode (org.onosproject.yang.model.DataNode)4 InputStream (java.io.InputStream)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 POST (javax.ws.rs.POST)2 WebTarget (javax.ws.rs.client.WebTarget)2 Test (org.junit.Test)2 ResourceTest (org.onosproject.rest.resources.ResourceTest)2