Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.protocol.restconf.server.rpp; |
| 18 | |
| 19 | import com.fasterxml.jackson.core.JsonProcessingException; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import org.glassfish.jersey.server.ChunkedOutput; |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 22 | import org.onosproject.rest.AbstractWebResource; |
Sean Condon | 13b1681 | 2018-01-25 10:31:49 +0000 | [diff] [blame] | 23 | import org.onosproject.restconf.api.RestconfError; |
Henry Yu | 3a91b13 | 2017-04-26 16:00:55 -0400 | [diff] [blame] | 24 | import org.onosproject.restconf.api.RestconfException; |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 25 | import org.onosproject.restconf.api.RestconfRpcOutput; |
Henry Yu | 3a91b13 | 2017-04-26 16:00:55 -0400 | [diff] [blame] | 26 | import org.onosproject.restconf.api.RestconfService; |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 27 | import org.slf4j.Logger; |
| 28 | |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 29 | import javax.servlet.http.HttpServletRequest; |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 30 | import javax.ws.rs.Consumes; |
| 31 | import javax.ws.rs.DELETE; |
| 32 | import javax.ws.rs.GET; |
Dimitrios Mavrommatis | 89e60d7 | 2017-12-02 18:45:35 -0800 | [diff] [blame] | 33 | import javax.ws.rs.PATCH; |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 34 | import javax.ws.rs.POST; |
| 35 | import javax.ws.rs.PUT; |
| 36 | import javax.ws.rs.Path; |
| 37 | import javax.ws.rs.PathParam; |
| 38 | import javax.ws.rs.Produces; |
| 39 | import javax.ws.rs.core.Context; |
| 40 | import javax.ws.rs.core.MediaType; |
| 41 | import javax.ws.rs.core.Response; |
| 42 | import javax.ws.rs.core.UriInfo; |
| 43 | import java.io.IOException; |
| 44 | import java.io.InputStream; |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 45 | import java.net.URI; |
Sean Condon | 13b1681 | 2018-01-25 10:31:49 +0000 | [diff] [blame] | 46 | import java.util.Arrays; |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 47 | import java.util.concurrent.CompletableFuture; |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 48 | |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 49 | import static javax.ws.rs.core.Response.Status.BAD_REQUEST; |
| 50 | import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR; |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 51 | import static javax.ws.rs.core.Response.Status.NOT_FOUND; |
| 52 | import static javax.ws.rs.core.Response.Status.OK; |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 53 | import static org.slf4j.LoggerFactory.getLogger; |
| 54 | |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 55 | |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 56 | /* |
| 57 | * This class is the main implementation of the RESTCONF Protocol |
| 58 | * Proxy module. Currently it only handles some basic operations |
| 59 | * on data resource nodes. However, the design intention is to |
| 60 | * create a code structure that allows new methods/functionality |
| 61 | * to be easily added in future releases. |
| 62 | */ |
| 63 | |
| 64 | /** |
| 65 | * Implementation of the RESTCONF Protocol Proxy module. |
| 66 | */ |
| 67 | @Path("/") |
| 68 | public class RestconfWebResource extends AbstractWebResource { |
| 69 | |
| 70 | @Context |
| 71 | UriInfo uriInfo; |
| 72 | |
Henry Yu | 3a91b13 | 2017-04-26 16:00:55 -0400 | [diff] [blame] | 73 | private final RestconfService service = get(RestconfService.class); |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 74 | private final Logger log = getLogger(getClass()); |
| 75 | |
| 76 | /** |
| 77 | * Handles a RESTCONF GET operation against a target data resource. If the |
| 78 | * operation is successful, the JSON presentation of the resource plus HTTP |
Sean Condon | 13b1681 | 2018-01-25 10:31:49 +0000 | [diff] [blame] | 79 | * status code "200 OK" is returned. If it is not found then "404 Not Found" |
| 80 | * is returned. On internal error "500 Internal Server Error" is returned. |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 81 | * |
| 82 | * @param uriString URI of the data resource. |
Sean Condon | 13b1681 | 2018-01-25 10:31:49 +0000 | [diff] [blame] | 83 | * @return HTTP response - 200, 404 or 500 |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 84 | */ |
| 85 | @GET |
| 86 | @Produces(MediaType.APPLICATION_JSON) |
| 87 | @Path("data/{identifier : .+}") |
| 88 | public Response handleGetRequest(@PathParam("identifier") String uriString) { |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 89 | log.debug("handleGetRequest: {}", uriString); |
| 90 | |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 91 | URI uri = uriInfo.getRequestUri(); |
| 92 | |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 93 | try { |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 94 | ObjectNode node = service.runGetOperationOnDataResource(uri); |
| 95 | if (node == null) { |
Sean Condon | 13b1681 | 2018-01-25 10:31:49 +0000 | [diff] [blame] | 96 | RestconfError error = |
| 97 | RestconfError.builder(RestconfError.ErrorType.PROTOCOL, |
| 98 | RestconfError.ErrorTag.INVALID_VALUE) |
| 99 | .errorMessage("Resource not found") |
| 100 | .errorPath(uriString) |
| 101 | .errorAppTag("handleGetRequest") |
| 102 | .build(); |
| 103 | return Response.status(NOT_FOUND) |
| 104 | .entity(RestconfError.wrapErrorAsJson(Arrays.asList(error))).build(); |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 105 | } |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 106 | return ok(node).build(); |
| 107 | } catch (RestconfException e) { |
| 108 | log.error("ERROR: handleGetRequest: {}", e.getMessage()); |
| 109 | log.debug("Exception in handleGetRequest:", e); |
Sean Condon | 13b1681 | 2018-01-25 10:31:49 +0000 | [diff] [blame] | 110 | return Response.status(e.getResponse().getStatus()).entity(e.toRestconfErrorJson()).build(); |
| 111 | } catch (Exception e) { |
| 112 | RestconfError error = RestconfError |
| 113 | .builder(RestconfError.ErrorType.APPLICATION, RestconfError.ErrorTag.OPERATION_FAILED) |
| 114 | .errorMessage(e.getMessage()).errorAppTag("handlePostRequest").build(); |
| 115 | return Response.status(INTERNAL_SERVER_ERROR) |
| 116 | .entity(RestconfError.wrapErrorAsJson(Arrays.asList(error))).build(); |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Handles the RESTCONF Event Notification Subscription request. If the |
| 122 | * subscription is successful, a ChunkedOutput stream is created and returned |
| 123 | * to the caller. |
Yuta HIGUCHI | d1ce4bc | 2017-06-03 01:05:33 -0700 | [diff] [blame] | 124 | * <p> |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 125 | * This function is not blocked on streaming the data (so that it can handle |
| 126 | * other incoming requests). Instead, a worker thread running in the background |
| 127 | * does the data streaming. If errors occur during streaming, the worker thread |
| 128 | * calls ChunkedOutput.close() to disconnect the session and terminates itself. |
| 129 | * |
| 130 | * @param streamId Event stream ID |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 131 | * @param request RESTCONF client information from which the client IP |
| 132 | * address is retrieved |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 133 | * @return A string data stream over HTTP keep-alive session |
| 134 | */ |
| 135 | @GET |
| 136 | @Produces(MediaType.APPLICATION_JSON) |
| 137 | @Path("streams/{streamId}") |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 138 | public ChunkedOutput<String> handleNotificationRegistration(@PathParam("streamId") String streamId, |
| 139 | @Context HttpServletRequest request) { |
Yuta HIGUCHI | d1ce4bc | 2017-06-03 01:05:33 -0700 | [diff] [blame] | 140 | final ChunkedOutput<String> output = new ChunkedOutput<>(String.class); |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 141 | try { |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 142 | service.subscribeEventStream(streamId, request.getRemoteAddr(), output); |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 143 | } catch (RestconfException e) { |
| 144 | log.error("ERROR: handleNotificationRegistration: {}", e.getMessage()); |
| 145 | log.debug("Exception in handleNotificationRegistration:", e); |
| 146 | try { |
| 147 | output.close(); |
| 148 | } catch (IOException ex) { |
| 149 | log.error("ERROR: handleNotificationRegistration:", ex); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | return output; |
| 154 | } |
| 155 | |
| 156 | /** |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 157 | * Handles a RESTCONF POST operation against the entire data store. If the |
| 158 | * operation is successful, HTTP status code "201 Created" is returned |
| 159 | * and there is no response message-body. If the data resource already |
| 160 | * exists, then the HTTP status code "409 Conflict" is returned. |
| 161 | * |
| 162 | * @param stream Input JSON object |
| 163 | * @return HTTP response |
| 164 | */ |
| 165 | @POST |
| 166 | @Consumes(MediaType.APPLICATION_JSON) |
| 167 | @Produces(MediaType.APPLICATION_JSON) |
| 168 | @Path("data") |
| 169 | public Response handlePostDatastore(InputStream stream) { |
| 170 | |
| 171 | log.debug("handlePostDatastore"); |
| 172 | return handlePostRequest(null, stream); |
| 173 | } |
| 174 | |
| 175 | /** |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 176 | * Handles a RESTCONF POST operation against a data resource. If the |
| 177 | * operation is successful, HTTP status code "201 Created" is returned |
| 178 | * and there is no response message-body. If the data resource already |
| 179 | * exists, then the HTTP status code "409 Conflict" is returned. |
| 180 | * |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 181 | * @param uriString URI of the parent data resource |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 182 | * @param stream Input JSON object |
| 183 | * @return HTTP response |
| 184 | */ |
| 185 | @POST |
| 186 | @Consumes(MediaType.APPLICATION_JSON) |
| 187 | @Produces(MediaType.APPLICATION_JSON) |
| 188 | @Path("data/{identifier : .+}") |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 189 | public Response handlePostRequest(@PathParam("identifier") String uriString, |
| 190 | InputStream stream) { |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 191 | log.debug("handlePostRequest: {}", uriString); |
| 192 | |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 193 | URI uri = uriInfo.getRequestUri(); |
| 194 | |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 195 | try { |
| 196 | ObjectNode rootNode = (ObjectNode) mapper().readTree(stream); |
| 197 | |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 198 | service.runPostOperationOnDataResource(uri, rootNode); |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 199 | return Response.created(uriInfo.getRequestUri()).build(); |
| 200 | } catch (JsonProcessingException e) { |
| 201 | log.error("ERROR: handlePostRequest ", e); |
Sean Condon | 13b1681 | 2018-01-25 10:31:49 +0000 | [diff] [blame] | 202 | RestconfError error = RestconfError |
| 203 | .builder(RestconfError.ErrorType.APPLICATION, RestconfError.ErrorTag.MALFORMED_MESSAGE) |
| 204 | .errorMessage(e.getMessage()).errorAppTag("handlePostRequest").build(); |
| 205 | return Response.status(BAD_REQUEST) |
| 206 | .entity(RestconfError.wrapErrorAsJson(Arrays.asList(error))).build(); |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 207 | } catch (RestconfException e) { |
| 208 | log.error("ERROR: handlePostRequest: {}", e.getMessage()); |
| 209 | log.debug("Exception in handlePostRequest:", e); |
Sean Condon | 13b1681 | 2018-01-25 10:31:49 +0000 | [diff] [blame] | 210 | return Response.status(e.getResponse().getStatus()) |
| 211 | .entity(e.toRestconfErrorJson()).build(); |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 212 | } catch (IOException ex) { |
| 213 | log.error("ERROR: handlePostRequest ", ex); |
Sean Condon | 13b1681 | 2018-01-25 10:31:49 +0000 | [diff] [blame] | 214 | RestconfError error = RestconfError |
| 215 | .builder(RestconfError.ErrorType.APPLICATION, RestconfError.ErrorTag.OPERATION_FAILED) |
| 216 | .errorMessage(ex.getMessage()).errorAppTag("handlePostRequest").build(); |
| 217 | return Response.status(INTERNAL_SERVER_ERROR) |
| 218 | .entity(RestconfError.wrapErrorAsJson(Arrays.asList(error))).build(); |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
| 222 | /** |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 223 | * Handles a RESTCONF RPC request. This function executes the RPC in |
| 224 | * the target application's context and returns the results as a Future. |
| 225 | * |
| 226 | * @param rpcName Name of the RPC |
| 227 | * @param rpcInput Input parameters |
| 228 | * @param request RESTCONF client information from which the client IP |
| 229 | * address is retrieved |
| 230 | * @return RPC output |
| 231 | */ |
| 232 | @POST |
| 233 | @Consumes(MediaType.APPLICATION_JSON) |
| 234 | @Produces(MediaType.APPLICATION_JSON) |
| 235 | @Path("operations/{rpc : .+}") |
| 236 | public Response handleRpcRequest(@PathParam("rpc") String rpcName, |
| 237 | InputStream rpcInput, |
| 238 | @Context HttpServletRequest request) { |
| 239 | URI uri = uriInfo.getRequestUri(); |
| 240 | try { |
| 241 | ObjectNode inputNode = (ObjectNode) mapper().readTree(rpcInput); |
| 242 | CompletableFuture<RestconfRpcOutput> rpcFuture = service.runRpc(uri, |
| 243 | inputNode, |
| 244 | request.getRemoteAddr()); |
| 245 | RestconfRpcOutput restconfRpcOutput; |
| 246 | restconfRpcOutput = rpcFuture.get(); |
| 247 | if (restconfRpcOutput.status() != OK) { |
| 248 | return Response.status(restconfRpcOutput.status()) |
| 249 | .entity(restconfRpcOutput.reason()).build(); |
| 250 | } |
| 251 | ObjectNode node = restconfRpcOutput.output(); |
| 252 | return ok(node).build(); |
| 253 | } catch (JsonProcessingException e) { |
| 254 | log.error("ERROR: handleRpcRequest", e); |
Sean Condon | 13b1681 | 2018-01-25 10:31:49 +0000 | [diff] [blame] | 255 | RestconfError error = RestconfError |
| 256 | .builder(RestconfError.ErrorType.APPLICATION, RestconfError.ErrorTag.MALFORMED_MESSAGE) |
| 257 | .errorMessage(e.getMessage()).errorAppTag("handleRpcRequest").build(); |
| 258 | return Response.status(BAD_REQUEST) |
| 259 | .entity(RestconfError.wrapErrorAsJson(Arrays.asList(error))).build(); |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 260 | } catch (RestconfException e) { |
| 261 | log.error("ERROR: handleRpcRequest: {}", e.getMessage()); |
| 262 | log.debug("Exception in handleRpcRequest:", e); |
Sean Condon | 13b1681 | 2018-01-25 10:31:49 +0000 | [diff] [blame] | 263 | return Response.status(e.getResponse().getStatus()) |
| 264 | .entity(e.toRestconfErrorJson()).build(); |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 265 | } catch (Exception e) { |
| 266 | log.error("ERROR: handleRpcRequest ", e); |
Sean Condon | 13b1681 | 2018-01-25 10:31:49 +0000 | [diff] [blame] | 267 | RestconfError error = RestconfError |
| 268 | .builder(RestconfError.ErrorType.APPLICATION, RestconfError.ErrorTag.OPERATION_FAILED) |
| 269 | .errorMessage(e.getMessage()).errorAppTag("handleRpcRequest").build(); |
| 270 | return Response.status(INTERNAL_SERVER_ERROR) |
| 271 | .entity(RestconfError.wrapErrorAsJson(Arrays.asList(error))).build(); |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 272 | } |
| 273 | } |
| 274 | |
| 275 | /** |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 276 | * Handles a RESTCONF PUT operation against a data resource. If a new |
| 277 | * resource is successfully created, then the HTTP status code "201 Created" |
| 278 | * is returned. If an existing resource is modified, then the HTTP |
| 279 | * status code "204 No Content" is returned. If the input JSON payload |
| 280 | * contains errors, then "400 Bad Request" is returned. If an exception |
| 281 | * occurs during the operation, the status code enclosed in |
| 282 | * the RestconfException object, such as "500 Internal Server Error", |
| 283 | * is returned. |
| 284 | * |
| 285 | * @param uriString URI of the data resource. |
| 286 | * @param stream Input JSON object |
| 287 | * @return HTTP response |
| 288 | */ |
| 289 | @PUT |
| 290 | @Consumes(MediaType.APPLICATION_JSON) |
| 291 | @Produces(MediaType.APPLICATION_JSON) |
| 292 | @Path("data/{identifier : .+}") |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 293 | public Response handlePutRequest(@PathParam("identifier") String uriString, |
| 294 | InputStream stream) { |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 295 | log.debug("handlePutRequest: {}", uriString); |
| 296 | |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 297 | URI uri = uriInfo.getRequestUri(); |
| 298 | |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 299 | try { |
| 300 | ObjectNode rootNode = (ObjectNode) mapper().readTree(stream); |
| 301 | |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 302 | service.runPutOperationOnDataResource(uri, rootNode); |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 303 | return Response.created(uriInfo.getRequestUri()).build(); |
| 304 | } catch (JsonProcessingException e) { |
| 305 | log.error("ERROR: handlePutRequest ", e); |
Sean Condon | 13b1681 | 2018-01-25 10:31:49 +0000 | [diff] [blame] | 306 | RestconfError error = RestconfError |
| 307 | .builder(RestconfError.ErrorType.APPLICATION, RestconfError.ErrorTag.MALFORMED_MESSAGE) |
| 308 | .errorMessage(e.getMessage()).errorAppTag("handlePutRequest").build(); |
| 309 | return Response.status(BAD_REQUEST) |
| 310 | .entity(RestconfError.wrapErrorAsJson(Arrays.asList(error))).build(); |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 311 | } catch (RestconfException e) { |
| 312 | log.error("ERROR: handlePutRequest: {}", e.getMessage()); |
| 313 | log.debug("Exception in handlePutRequest:", e); |
Sean Condon | 13b1681 | 2018-01-25 10:31:49 +0000 | [diff] [blame] | 314 | return Response.status(e.getResponse().getStatus()) |
| 315 | .entity(e.toRestconfErrorJson()).build(); |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 316 | } catch (IOException ex) { |
| 317 | log.error("ERROR: handlePutRequest ", ex); |
Sean Condon | 13b1681 | 2018-01-25 10:31:49 +0000 | [diff] [blame] | 318 | RestconfError error = RestconfError |
| 319 | .builder(RestconfError.ErrorType.APPLICATION, RestconfError.ErrorTag.OPERATION_FAILED) |
| 320 | .errorMessage(ex.getMessage()).errorAppTag("handlePutRequest").build(); |
| 321 | return Response.status(INTERNAL_SERVER_ERROR) |
| 322 | .entity(RestconfError.wrapErrorAsJson(Arrays.asList(error))).build(); |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 323 | } |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * Handles the RESTCONF DELETION Operation against a data resource. If the |
| 328 | * resource is successfully deleted, the HTTP status code "204 No Content" |
| 329 | * is returned in the response. If an exception occurs, then the |
| 330 | * HTTP status code enclosed in the RestconfException object is |
| 331 | * returned. |
| 332 | * |
| 333 | * @param uriString URI of the data resource to be deleted. |
| 334 | * @return HTTP response |
| 335 | */ |
| 336 | @DELETE |
| 337 | @Produces(MediaType.APPLICATION_JSON) |
| 338 | @Path("data/{identifier : .+}") |
| 339 | public Response handleDeleteRequest(@PathParam("identifier") String uriString) { |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 340 | log.debug("handleDeleteRequest: {}", uriString); |
| 341 | |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 342 | URI uri = uriInfo.getRequestUri(); |
| 343 | |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 344 | try { |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 345 | service.runDeleteOperationOnDataResource(uri); |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 346 | return Response.ok().build(); |
| 347 | } catch (RestconfException e) { |
| 348 | log.error("ERROR: handleDeleteRequest: {}", e.getMessage()); |
| 349 | log.debug("Exception in handleDeleteRequest:", e); |
Sean Condon | 13b1681 | 2018-01-25 10:31:49 +0000 | [diff] [blame] | 350 | return Response.status(e.getResponse().getStatus()) |
| 351 | .entity(e.toRestconfErrorJson()).build(); |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 352 | } |
| 353 | } |
| 354 | |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 355 | /** |
| 356 | * Handles a RESTCONF PATCH operation against a data resource. |
| 357 | * If the PATCH request succeeds, a "200 OK" status-line is returned if |
| 358 | * there is a message-body, and "204 No Content" is returned if no |
| 359 | * response message-body is sent. |
| 360 | * |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 361 | * @param uriString URI of the parent data resource |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 362 | * @param stream Input JSON object |
| 363 | * @return HTTP response |
| 364 | */ |
Dimitrios Mavrommatis | 89e60d7 | 2017-12-02 18:45:35 -0800 | [diff] [blame] | 365 | @PATCH |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 366 | @Consumes(MediaType.APPLICATION_JSON) |
| 367 | @Produces(MediaType.APPLICATION_JSON) |
| 368 | @Path("data/{identifier : .+}") |
| 369 | public Response handlePatchRequest(@PathParam("identifier") String uriString, |
| 370 | InputStream stream) { |
| 371 | |
| 372 | log.debug("handlePatchRequest: {}", uriString); |
| 373 | |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 374 | URI uri = uriInfo.getRequestUri(); |
| 375 | |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 376 | try { |
| 377 | ObjectNode rootNode = (ObjectNode) mapper().readTree(stream); |
| 378 | |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 379 | service.runPatchOperationOnDataResource(uri, rootNode); |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 380 | return Response.ok().build(); |
| 381 | } catch (JsonProcessingException e) { |
| 382 | log.error("ERROR: handlePatchRequest ", e); |
Sean Condon | 13b1681 | 2018-01-25 10:31:49 +0000 | [diff] [blame] | 383 | RestconfError error = RestconfError |
| 384 | .builder(RestconfError.ErrorType.APPLICATION, RestconfError.ErrorTag.MALFORMED_MESSAGE) |
| 385 | .errorMessage(e.getMessage()).errorAppTag("handlePatchRequest").build(); |
| 386 | return Response.status(BAD_REQUEST) |
| 387 | .entity(RestconfError.wrapErrorAsJson(Arrays.asList(error))).build(); |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 388 | } catch (RestconfException e) { |
| 389 | log.error("ERROR: handlePatchRequest: {}", e.getMessage()); |
| 390 | log.debug("Exception in handlePatchRequest:", e); |
Sean Condon | 13b1681 | 2018-01-25 10:31:49 +0000 | [diff] [blame] | 391 | return Response.status(e.getResponse().getStatus()) |
| 392 | .entity(e.toRestconfErrorJson()).build(); |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 393 | } catch (IOException ex) { |
| 394 | log.error("ERROR: handlePatchRequest ", ex); |
Sean Condon | 13b1681 | 2018-01-25 10:31:49 +0000 | [diff] [blame] | 395 | RestconfError error = RestconfError |
| 396 | .builder(RestconfError.ErrorType.APPLICATION, RestconfError.ErrorTag.OPERATION_FAILED) |
| 397 | .errorMessage(ex.getMessage()).errorAppTag("handlePatchRequest").build(); |
| 398 | return Response.status(INTERNAL_SERVER_ERROR) |
| 399 | .entity(RestconfError.wrapErrorAsJson(Arrays.asList(error))).build(); |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 400 | } |
| 401 | } |
| 402 | |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 403 | |
| 404 | /** |
| 405 | * Handles a RESTCONF PATCH operation against the entire data store. |
| 406 | * If the PATCH request succeeds, a "200 OK" status-line is returned if |
| 407 | * there is a message-body, and "204 No Content" is returned if no |
| 408 | * response message-body is sent. |
| 409 | * |
| 410 | * @param stream Input JSON object |
| 411 | * @return HTTP response |
| 412 | */ |
Dimitrios Mavrommatis | 89e60d7 | 2017-12-02 18:45:35 -0800 | [diff] [blame] | 413 | @PATCH |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 414 | @Consumes(MediaType.APPLICATION_JSON) |
| 415 | @Produces(MediaType.APPLICATION_JSON) |
| 416 | @Path("data") |
| 417 | public Response handlePatchDatastore(InputStream stream) { |
| 418 | log.debug("handlePatchDatastore"); |
| 419 | return handlePatchRequest(null, stream); |
| 420 | } |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 421 | } |