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