Search in sources :

Example 1 with RestconfRpcOutput

use of org.onosproject.restconf.api.RestconfRpcOutput in project onos by opennetworkinglab.

the class RestconfUtils method convertRpcOutput.

/**
 * Creates a RESTCONF RPC output object from a given YANG RPC output object.
 *
 * @param cmdId     resource ID of the RPC
 * @param rpcOutput given RPC output in YANG format
 * @return RPC output in RESTCONF format
 */
public static RestconfRpcOutput convertRpcOutput(ResourceId cmdId, RpcOutput rpcOutput) {
    RestconfRpcOutput restconfRpcOutput = new RestconfRpcOutput();
    restconfRpcOutput.status(convertResponseStatus(rpcOutput.status()));
    if (rpcOutput.data() != null) {
        restconfRpcOutput.output(convertDataNodeToJson(cmdId, rpcOutput.data()));
    }
    return restconfRpcOutput;
}
Also used : RestconfRpcOutput(org.onosproject.restconf.api.RestconfRpcOutput)

Example 2 with RestconfRpcOutput

use of org.onosproject.restconf.api.RestconfRpcOutput 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 3 with RestconfRpcOutput

use of org.onosproject.restconf.api.RestconfRpcOutput in project onos by opennetworkinglab.

the class RestconfManager method executeRpc.

private RestconfRpcOutput executeRpc(URI uri, ObjectNode input, String clientIpAddress) {
    ResourceData rpcInputNode = convertJsonToDataNode(uri, input);
    ResourceId resourceId = rpcInputNode.resourceId();
    List<DataNode> inputDataNodeList = rpcInputNode.dataNodes();
    DataNode inputDataNode = inputDataNodeList.get(0);
    RpcInput rpcInput = new RpcInput(resourceId, inputDataNode);
    RestconfRpcOutput restconfOutput = null;
    try {
        CompletableFuture<RpcOutput> rpcFuture = dynamicConfigService.invokeRpc(rpcInput);
        RpcOutput rpcOutput = rpcFuture.get();
        restconfOutput = RestconfUtils.convertRpcOutput(resourceId, rpcOutput);
    } catch (InterruptedException e) {
        log.error("ERROR: computeResultQ.take() has been interrupted.");
        log.debug("executeRpc Exception:", e);
        RestconfError error = RestconfError.builder(RestconfError.ErrorType.RPC, RestconfError.ErrorTag.OPERATION_FAILED).errorMessage("RPC execution has been interrupted").errorPath(uri.getPath()).build();
        restconfOutput = new RestconfRpcOutput(INTERNAL_SERVER_ERROR, RestconfError.wrapErrorAsJson(Arrays.asList(error)));
        restconfOutput.reason("RPC execution has been interrupted");
    } catch (Exception e) {
        log.error("ERROR: executeRpc: {}", e.getMessage());
        log.debug("executeRpc Exception:", e);
        RestconfError error = RestconfError.builder(RestconfError.ErrorType.RPC, RestconfError.ErrorTag.OPERATION_FAILED).errorMessage(e.getMessage()).errorPath(uri.getPath()).build();
        restconfOutput = new RestconfRpcOutput(INTERNAL_SERVER_ERROR, RestconfError.wrapErrorAsJson(Arrays.asList(error)));
        restconfOutput.reason(e.getMessage());
    }
    return restconfOutput;
}
Also used : ResourceData(org.onosproject.yang.model.ResourceData) DefaultResourceData(org.onosproject.yang.model.DefaultResourceData) RestconfRpcOutput(org.onosproject.restconf.api.RestconfRpcOutput) ResourceId(org.onosproject.yang.model.ResourceId) DataNode(org.onosproject.yang.model.DataNode) RestconfUtils.convertJsonToDataNode(org.onosproject.restconf.utils.RestconfUtils.convertJsonToDataNode) RpcInput(org.onosproject.yang.model.RpcInput) RestconfRpcOutput(org.onosproject.restconf.api.RestconfRpcOutput) RpcOutput(org.onosproject.yang.model.RpcOutput) RestconfError(org.onosproject.restconf.api.RestconfError) FailedException(org.onosproject.config.FailedException) RestconfException(org.onosproject.restconf.api.RestconfException)

Aggregations

RestconfRpcOutput (org.onosproject.restconf.api.RestconfRpcOutput)3 RestconfError (org.onosproject.restconf.api.RestconfError)2 RestconfException (org.onosproject.restconf.api.RestconfException)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 IOException (java.io.IOException)1 URI (java.net.URI)1 Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 FailedException (org.onosproject.config.FailedException)1 RestconfUtils.convertJsonToDataNode (org.onosproject.restconf.utils.RestconfUtils.convertJsonToDataNode)1 DataNode (org.onosproject.yang.model.DataNode)1 DefaultResourceData (org.onosproject.yang.model.DefaultResourceData)1 ResourceData (org.onosproject.yang.model.ResourceData)1 ResourceId (org.onosproject.yang.model.ResourceId)1 RpcInput (org.onosproject.yang.model.RpcInput)1 RpcOutput (org.onosproject.yang.model.RpcOutput)1