use of org.onosproject.restconf.api.RestconfException 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.RestconfException 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();
}
}
use of org.onosproject.restconf.api.RestconfException in project onos by opennetworkinglab.
the class RestconfUtils method convertDataNodeToJson.
/**
* Convert Resource Id and Data Node to Json ObjectNode.
*
* @param rid resource identifier
* @param dataNode represents type of node in data store
* @return JSON representation of the data resource
*/
public static ObjectNode convertDataNodeToJson(ResourceId rid, DataNode dataNode) {
RuntimeContext.Builder runtimeContextBuilder = DefaultRuntimeContext.builder();
runtimeContextBuilder.setDataFormat(JSON_FORMAT);
RuntimeContext context = runtimeContextBuilder.build();
DefaultResourceData.Builder resourceDataBuilder = DefaultResourceData.builder();
resourceDataBuilder.addDataNode(dataNode);
resourceDataBuilder.resourceId(rid);
ResourceData resourceData = resourceDataBuilder.build();
DefaultCompositeData.Builder compositeDataBuilder = DefaultCompositeData.builder();
compositeDataBuilder.resourceData(resourceData);
CompositeData compositeData = compositeDataBuilder.build();
ObjectNode rootNode = null;
try {
// CompositeData --- YangRuntimeService ---> CompositeStream.
CompositeStream compositeStream = YANG_RUNTIME.encode(compositeData, context);
InputStream inputStream = compositeStream.resourceData();
rootNode = convertInputStreamToObjectNode(inputStream);
} catch (Exception ex) {
log.error("convertInputStreamToObjectNode failure: {}", ex.getMessage());
log.debug("convertInputStreamToObjectNode failure", ex);
}
if (rootNode == null) {
throw new RestconfException("ERROR: InputStream can not be convert to ObjectNode", null, RestconfError.ErrorTag.DATA_MISSING, CONFLICT, Optional.empty());
}
return rootNode;
}
use of org.onosproject.restconf.api.RestconfException in project onos by opennetworkinglab.
the class RestconfUtils method convertInputStreamToObjectNode.
/**
* Converts an input stream to JSON objectNode.
*
* @param inputStream the InputStream from Resource Data
* @return JSON representation of the data resource
*/
public static ObjectNode convertInputStreamToObjectNode(InputStream inputStream) {
ObjectNode rootNode;
ObjectMapper mapper = new ObjectMapper();
try {
rootNode = readTreeFromStream(mapper, inputStream);
} catch (IOException e) {
throw new RestconfException("ERROR: InputStream failed to parse", e, RestconfError.ErrorTag.OPERATION_FAILED, INTERNAL_SERVER_ERROR, Optional.empty());
}
return rootNode;
}
use of org.onosproject.restconf.api.RestconfException in project onos by opennetworkinglab.
the class RestconfManager method runPutOperationOnDataResource.
@Override
public void runPutOperationOnDataResource(URI uri, ObjectNode rootNode) throws RestconfException {
DataResourceLocator rl = DataResourceLocator.newInstance(uri);
ResourceData receivedData = convertJsonToDataNode(rmLastPathSegment(rl.uriForYangRuntime()), rootNode);
List<DataNode> dataNodeList = receivedData.dataNodes();
if (dataNodeList == null || dataNodeList.isEmpty()) {
log.warn("There is no one Data Node can be proceed.");
return;
}
if (dataNodeList.size() > 1) {
log.warn("There are more than one Data Node can be proceed: {}", dataNodeList.size());
}
DataNode dataNode = dataNodeList.get(0);
try {
/*
* If the data node already exists, then replace it.
* Otherwise, create it.
*/
if (dynamicConfigService.nodeExist(rl.ridForDynConfig())) {
dynamicConfigService.replaceNode(parentOf(rl.ridForDynConfig()), dataNode);
} else {
dynamicConfigService.createNode(parentOf(rl.ridForDynConfig()), dataNode);
}
} catch (FailedException e) {
log.error("ERROR: DynamicConfigService: ", e);
throw new RestconfException("ERROR: DynamicConfigService", e, RestconfError.ErrorTag.OPERATION_FAILED, INTERNAL_SERVER_ERROR, Optional.of(uri.getPath()));
}
}
Aggregations