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