Search in sources :

Example 11 with RestconfException

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();
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) RestconfException(org.onosproject.restconf.api.RestconfException) RestconfError(org.onosproject.restconf.api.RestconfError) IOException(java.io.IOException) URI(java.net.URI) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) PATCH(javax.ws.rs.PATCH)

Example 12 with RestconfException

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();
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) RestconfException(org.onosproject.restconf.api.RestconfException) RestconfError(org.onosproject.restconf.api.RestconfError) IOException(java.io.IOException) URI(java.net.URI) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) PUT(javax.ws.rs.PUT)

Example 13 with RestconfException

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;
}
Also used : ResourceData(org.onosproject.yang.model.ResourceData) DefaultResourceData(org.onosproject.yang.model.DefaultResourceData) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) InputStream(java.io.InputStream) CompositeData(org.onosproject.yang.runtime.CompositeData) DefaultCompositeData(org.onosproject.yang.runtime.DefaultCompositeData) DefaultCompositeStream(org.onosproject.yang.runtime.DefaultCompositeStream) CompositeStream(org.onosproject.yang.runtime.CompositeStream) IOException(java.io.IOException) RestconfException(org.onosproject.restconf.api.RestconfException) DefaultResourceData(org.onosproject.yang.model.DefaultResourceData) DefaultCompositeData(org.onosproject.yang.runtime.DefaultCompositeData) RestconfException(org.onosproject.restconf.api.RestconfException) RuntimeContext(org.onosproject.yang.runtime.RuntimeContext) DefaultRuntimeContext(org.onosproject.yang.runtime.DefaultRuntimeContext)

Example 14 with RestconfException

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;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) RestconfException(org.onosproject.restconf.api.RestconfException) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 15 with RestconfException

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()));
    }
}
Also used : ResourceData(org.onosproject.yang.model.ResourceData) DefaultResourceData(org.onosproject.yang.model.DefaultResourceData) 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)

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