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