Search in sources :

Example 1 with RestconfException

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;
}
Also used : ResourceData(org.onosproject.yang.model.ResourceData) DefaultResourceData(org.onosproject.yang.model.DefaultResourceData) InputStream(java.io.InputStream) RestconfException(org.onosproject.restconf.api.RestconfException) UriBuilder(javax.ws.rs.core.UriBuilder) DefaultCompositeStream(org.onosproject.yang.runtime.DefaultCompositeStream) CompositeData(org.onosproject.yang.runtime.CompositeData) DefaultCompositeData(org.onosproject.yang.runtime.DefaultCompositeData) RuntimeContext(org.onosproject.yang.runtime.RuntimeContext) DefaultRuntimeContext(org.onosproject.yang.runtime.DefaultRuntimeContext) DefaultCompositeStream(org.onosproject.yang.runtime.DefaultCompositeStream) CompositeStream(org.onosproject.yang.runtime.CompositeStream) IOException(java.io.IOException) RestconfException(org.onosproject.restconf.api.RestconfException)

Example 2 with RestconfException

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;
}
Also used : InputStream(java.io.InputStream) RestconfException(org.onosproject.restconf.api.RestconfException) IOException(java.io.IOException) RestconfException(org.onosproject.restconf.api.RestconfException)

Example 3 with RestconfException

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;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Filter(org.onosproject.config.Filter) DataNode(org.onosproject.yang.model.DataNode) RestconfUtils.convertJsonToDataNode(org.onosproject.restconf.utils.RestconfUtils.convertJsonToDataNode) FailedException(org.onosproject.config.FailedException) RestconfException(org.onosproject.restconf.api.RestconfException)

Example 4 with RestconfException

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()));
        }
    }
}
Also used : ResourceData(org.onosproject.yang.model.ResourceData) DefaultResourceData(org.onosproject.yang.model.DefaultResourceData) ResourceId(org.onosproject.yang.model.ResourceId) DataNode(org.onosproject.yang.model.DataNode) RestconfUtils.convertJsonToDataNode(org.onosproject.restconf.utils.RestconfUtils.convertJsonToDataNode) RestconfException(org.onosproject.restconf.api.RestconfException) FailedException(org.onosproject.config.FailedException) RestconfException(org.onosproject.restconf.api.RestconfException)

Example 5 with RestconfException

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();
    }
}
Also used : RestconfException(org.onosproject.restconf.api.RestconfException) URI(java.net.URI) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) Produces(javax.ws.rs.Produces)

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