slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 1 | /* |
Dimitrios Mavrommatis | 96b255a | 2017-12-06 13:09:25 -0800 | [diff] [blame^] | 2 | * Copyright 2017-present Open Networking Foundation |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [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 | */ |
Dimitrios Mavrommatis | 96b255a | 2017-12-06 13:09:25 -0800 | [diff] [blame^] | 16 | package org.onosproject.xran.impl.rest; |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
Dimitrios Mavrommatis | 96b255a | 2017-12-06 13:09:25 -0800 | [diff] [blame^] | 19 | import io.swagger.annotations.ApiResponse; |
| 20 | import io.swagger.annotations.ApiResponses; |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 21 | import org.apache.commons.lang.exception.ExceptionUtils; |
| 22 | import org.onosproject.rest.AbstractWebResource; |
| 23 | import org.onosproject.xran.XranStore; |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 24 | import org.slf4j.Logger; |
| 25 | import org.slf4j.LoggerFactory; |
| 26 | |
| 27 | import javax.ws.rs.Consumes; |
| 28 | import javax.ws.rs.GET; |
| 29 | import javax.ws.rs.POST; |
| 30 | import javax.ws.rs.Path; |
| 31 | import javax.ws.rs.PathParam; |
| 32 | import javax.ws.rs.Produces; |
| 33 | import javax.ws.rs.core.MediaType; |
| 34 | import javax.ws.rs.core.Response; |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 35 | import java.io.InputStream; |
Dimitrios Mavrommatis | 96b255a | 2017-12-06 13:09:25 -0800 | [diff] [blame^] | 36 | import java.net.HttpURLConnection; |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * Slice web resource. |
| 40 | */ |
| 41 | @Path("slice") |
| 42 | public class SliceWebResource extends AbstractWebResource { |
| 43 | |
| 44 | private static final Logger log = |
| 45 | LoggerFactory.getLogger(SliceWebResource.class); |
| 46 | |
slowr | 60d4d10 | 2017-08-16 18:33:58 -0700 | [diff] [blame] | 47 | public SliceWebResource() { |
slowr | 60d4d10 | 2017-08-16 18:33:58 -0700 | [diff] [blame] | 48 | } |
| 49 | |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 50 | /** |
slowr | 577f322 | 2017-08-28 10:49:08 -0700 | [diff] [blame] | 51 | * List the slice with the given slice ID. |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 52 | * |
slowr | 577f322 | 2017-08-28 10:49:08 -0700 | [diff] [blame] | 53 | * @param sliceid ID of the slice |
| 54 | * @return Response |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 55 | */ |
| 56 | @GET |
| 57 | @Path("{sliceid}") |
| 58 | @Produces(MediaType.APPLICATION_JSON) |
Dimitrios Mavrommatis | 96b255a | 2017-12-06 13:09:25 -0800 | [diff] [blame^] | 59 | @ApiResponses(value = { |
| 60 | @ApiResponse(code = 200, message = "HTTP_OK"), |
| 61 | @ApiResponse(code = 500, message = "HTTP_INTERNAL_ERROR"), |
| 62 | @ApiResponse(code = 404, message = "HTTP_NOT_FOUND") |
| 63 | }) |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 64 | public Response getSlice(@PathParam("sliceid") long sliceid) { |
Dimitrios Mavrommatis | 96b255a | 2017-12-06 13:09:25 -0800 | [diff] [blame^] | 65 | return get(XranStore.class).getSlice(sliceid).map(slice -> { |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 66 | try { |
slowr | 60d4d10 | 2017-08-16 18:33:58 -0700 | [diff] [blame] | 67 | JsonNode jsonNode = mapper().valueToTree(slice); |
| 68 | |
| 69 | return ResponseHelper.getResponse( |
| 70 | mapper(), |
Dimitrios Mavrommatis | 96b255a | 2017-12-06 13:09:25 -0800 | [diff] [blame^] | 71 | HttpURLConnection.HTTP_OK, |
slowr | 60d4d10 | 2017-08-16 18:33:58 -0700 | [diff] [blame] | 72 | jsonNode |
| 73 | ); |
| 74 | } catch (Exception e) { |
| 75 | String fullStackTrace = ExceptionUtils.getFullStackTrace(e); |
| 76 | log.error(fullStackTrace); |
slowr | 60d4d10 | 2017-08-16 18:33:58 -0700 | [diff] [blame] | 77 | |
| 78 | return ResponseHelper.getResponse( |
| 79 | mapper(), |
Dimitrios Mavrommatis | 96b255a | 2017-12-06 13:09:25 -0800 | [diff] [blame^] | 80 | HttpURLConnection.HTTP_INTERNAL_ERROR, |
slowr | 60d4d10 | 2017-08-16 18:33:58 -0700 | [diff] [blame] | 81 | "Exception", |
| 82 | fullStackTrace |
| 83 | ); |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 84 | } |
Dimitrios Mavrommatis | 96b255a | 2017-12-06 13:09:25 -0800 | [diff] [blame^] | 85 | }).orElse( |
| 86 | ResponseHelper.getResponse( |
| 87 | mapper(), |
| 88 | HttpURLConnection.HTTP_NOT_FOUND, |
| 89 | "Not Found", |
| 90 | "Slice " + sliceid + " not found" |
| 91 | ) |
slowr | 60d4d10 | 2017-08-16 18:33:58 -0700 | [diff] [blame] | 92 | ); |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /** |
slowr | 577f322 | 2017-08-28 10:49:08 -0700 | [diff] [blame] | 96 | * Create slice with the corresponding attributes. |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 97 | * |
slowr | 577f322 | 2017-08-28 10:49:08 -0700 | [diff] [blame] | 98 | * @param stream Attributes to create slice |
| 99 | * @return Response |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 100 | */ |
| 101 | @POST |
| 102 | @Consumes(MediaType.APPLICATION_JSON) |
slowr | 60d4d10 | 2017-08-16 18:33:58 -0700 | [diff] [blame] | 103 | @Produces(MediaType.APPLICATION_JSON) |
Dimitrios Mavrommatis | 96b255a | 2017-12-06 13:09:25 -0800 | [diff] [blame^] | 104 | @ApiResponses(value = { |
| 105 | @ApiResponse(code = 200, message = "HTTP_OK"), |
| 106 | @ApiResponse(code = 500, message = "HTTP_INTERNAL_ERROR"), |
| 107 | @ApiResponse(code = 501, message = "HTTP_NOT_IMPLEMENTED") |
| 108 | }) |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 109 | public Response postSlice(InputStream stream) { |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 110 | try { |
slowr | 60d4d10 | 2017-08-16 18:33:58 -0700 | [diff] [blame] | 111 | // ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
| 112 | // get(XranStore.class).createSlice(jsonTree); |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 113 | |
slowr | 60d4d10 | 2017-08-16 18:33:58 -0700 | [diff] [blame] | 114 | // FIXME: change when implemented |
| 115 | return ResponseHelper.getResponse( |
| 116 | mapper(), |
Dimitrios Mavrommatis | 96b255a | 2017-12-06 13:09:25 -0800 | [diff] [blame^] | 117 | HttpURLConnection.HTTP_NOT_IMPLEMENTED, |
slowr | 60d4d10 | 2017-08-16 18:33:58 -0700 | [diff] [blame] | 118 | "Not Implemented", |
| 119 | "POST Slice not implemented" |
| 120 | ); |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 121 | } catch (Exception e) { |
slowr | 60d4d10 | 2017-08-16 18:33:58 -0700 | [diff] [blame] | 122 | String fullStackTrace = ExceptionUtils.getFullStackTrace(e); |
| 123 | log.error(fullStackTrace); |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 124 | e.printStackTrace(); |
slowr | 60d4d10 | 2017-08-16 18:33:58 -0700 | [diff] [blame] | 125 | |
| 126 | return ResponseHelper.getResponse( |
| 127 | mapper(), |
Dimitrios Mavrommatis | 96b255a | 2017-12-06 13:09:25 -0800 | [diff] [blame^] | 128 | HttpURLConnection.HTTP_INTERNAL_ERROR, |
slowr | 60d4d10 | 2017-08-16 18:33:58 -0700 | [diff] [blame] | 129 | "Exception", |
| 130 | fullStackTrace |
| 131 | ); |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 132 | } |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | } |