use of org.onosproject.restconf.api.RestconfException in project onos by opennetworkinglab.
the class RestconfUtils method convertJsonToDataNode.
/**
* Convert URI and ObjectNode to ResourceData.
*
* @param uri URI of the data resource
* @param rootNode JSON representation of the data resource
* @return represents type of node in data store
*/
public static ResourceData convertJsonToDataNode(URI uri, ObjectNode rootNode) {
RuntimeContext.Builder runtimeContextBuilder = new DefaultRuntimeContext.Builder();
runtimeContextBuilder.setDataFormat(JSON_FORMAT);
RuntimeContext context = runtimeContextBuilder.build();
ResourceData resourceData = null;
InputStream jsonData = null;
try {
if (rootNode != null) {
jsonData = convertObjectNodeToInputStream(rootNode);
}
String uriString = getRawUriPath(uri);
CompositeStream compositeStream = new DefaultCompositeStream(uriString, jsonData);
// CompositeStream --- YangRuntimeService ---> CompositeData.
CompositeData compositeData = YANG_RUNTIME.decode(compositeStream, context);
resourceData = compositeData.resourceData();
} catch (RestconfException ex) {
throw ex;
} catch (Exception ex) {
log.error("convertJsonToDataNode failure: {}", ex.getMessage(), ex);
log.info("Failed JSON: \n{}", rootNode);
log.debug("convertJsonToDataNode failure", ex);
throw new RestconfException("ERROR: JSON cannot be converted to DataNode", ex, RestconfError.ErrorTag.OPERATION_FAILED, INTERNAL_SERVER_ERROR, Optional.of(uri.getPath()));
}
if (resourceData == null) {
throw new RestconfException("ERROR: JSON cannot be converted to DataNode", RestconfError.ErrorTag.DATA_MISSING, CONFLICT, Optional.of(uri.getPath()), Optional.empty());
}
return resourceData;
}
use of org.onosproject.restconf.api.RestconfException in project onos by opennetworkinglab.
the class RestconfUtils method convertObjectNodeToInputStream.
/**
* Convert ObjectNode to InputStream.
*
* @param rootNode JSON representation of the data resource
* @return the InputStream from Resource Data
*/
public static InputStream convertObjectNodeToInputStream(ObjectNode rootNode) {
String json = rootNode.toString();
InputStream inputStream;
try {
inputStream = IOUtils.toInputStream(json);
} catch (Exception e) {
throw new RestconfException("ERROR: Json Node failed to parse", e, RestconfError.ErrorTag.MALFORMED_MESSAGE, BAD_REQUEST, Optional.empty());
}
return inputStream;
}
use of org.onosproject.restconf.api.RestconfException in project onos by opennetworkinglab.
the class RestconfManager method runGetOperationOnDataResource.
@Override
public ObjectNode runGetOperationOnDataResource(URI uri) throws RestconfException {
DataResourceLocator rl = DataResourceLocator.newInstance(uri);
// TODO: define Filter (if there is any requirement).
Filter filter = Filter.builder().build();
DataNode dataNode;
try {
if (!dynamicConfigService.nodeExist(rl.ridForDynConfig())) {
return null;
}
dataNode = dynamicConfigService.readNode(rl.ridForDynConfig(), filter);
} 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()));
}
ObjectNode rootNode = convertDataNodeToJson(rl.ridForYangRuntime(), dataNode);
return rootNode;
}
use of org.onosproject.restconf.api.RestconfException in project onos by opennetworkinglab.
the class RestconfManager method runPostOperationOnDataResource.
@Override
public void runPostOperationOnDataResource(URI uri, ObjectNode rootNode) throws RestconfException {
DataResourceLocator rl = DataResourceLocator.newInstance(uri);
ResourceData receivedData = convertJsonToDataNode(rl.uriForYangRuntime(), rootNode);
ResourceId rid = receivedData.resourceId();
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);
if (rid == null) {
rid = ResourceId.builder().addBranchPointSchema("/", null).build();
dataNode = removeTopNode(dataNode);
}
try {
dynamicConfigService.createNode(rl.ridForDynConfig(), dataNode);
} catch (Exception e) {
if (e.getMessage().startsWith("Requested node already present")) {
throw new RestconfException("Already exists", e, RestconfError.ErrorTag.DATA_EXISTS, CONFLICT, Optional.of(uri.getPath()));
} else {
log.error("ERROR: DynamicConfigService: creating {} with {}", ResourceIds.toInstanceIdentifier(rl.ridForDynConfig()), dataNode, e);
throw new RestconfException("ERROR: DynamicConfigService", e, RestconfError.ErrorTag.OPERATION_FAILED, INTERNAL_SERVER_ERROR, Optional.of(uri.getPath()));
}
}
}
use of org.onosproject.restconf.api.RestconfException in project onos by opennetworkinglab.
the class RestconfWebResource method handleDeleteRequest.
/**
* Handles the RESTCONF DELETION Operation against a data resource. If the
* resource is successfully deleted, the HTTP status code "204 No Content"
* is returned in the response. If an exception occurs, then the
* HTTP status code enclosed in the RestconfException object is
* returned.
*
* @param uriString URI of the data resource to be deleted.
* @return HTTP response
*/
@DELETE
@Produces(MediaTypeRestconf.APPLICATION_YANG_DATA_JSON)
@Path("data/{identifier : .+}")
public Response handleDeleteRequest(@PathParam("identifier") String uriString) {
log.debug("handleDeleteRequest: {}", uriString);
URI uri = uriInfo.getRequestUri();
try {
service.runDeleteOperationOnDataResource(uri);
return Response.ok().build();
} catch (RestconfException e) {
log.error("ERROR: handleDeleteRequest: {}", e.getMessage());
log.debug("Exception in handleDeleteRequest:", e);
return Response.status(e.getResponse().getStatus()).entity(e.toRestconfErrorJson()).build();
}
}
Aggregations