slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 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 | |
slowr | 60d4d10 | 2017-08-16 18:33:58 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.annotation.JsonInclude; |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.JsonNode; |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 20 | import org.apache.commons.lang.exception.ExceptionUtils; |
| 21 | import org.onosproject.rest.AbstractWebResource; |
| 22 | import org.onosproject.xran.XranStore; |
| 23 | import org.onosproject.xran.entities.RnibSlice; |
| 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; |
| 35 | import java.io.IOException; |
| 36 | import java.io.InputStream; |
| 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() { |
| 48 | |
| 49 | } |
| 50 | |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 51 | /** |
| 52 | * test. |
| 53 | * |
| 54 | * @param sliceid test |
| 55 | * @return test |
| 56 | */ |
| 57 | @GET |
| 58 | @Path("{sliceid}") |
| 59 | @Produces(MediaType.APPLICATION_JSON) |
| 60 | public Response getSlice(@PathParam("sliceid") long sliceid) { |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 61 | RnibSlice slice = get(XranStore.class).getSlice(sliceid); |
| 62 | |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 63 | if (slice != null) { |
| 64 | try { |
slowr | 60d4d10 | 2017-08-16 18:33:58 -0700 | [diff] [blame] | 65 | JsonNode jsonNode = mapper().valueToTree(slice); |
| 66 | |
| 67 | return ResponseHelper.getResponse( |
| 68 | mapper(), |
| 69 | ResponseHelper.statusCode.OK, |
| 70 | jsonNode |
| 71 | ); |
| 72 | } catch (Exception e) { |
| 73 | String fullStackTrace = ExceptionUtils.getFullStackTrace(e); |
| 74 | log.error(fullStackTrace); |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 75 | e.printStackTrace(); |
slowr | 60d4d10 | 2017-08-16 18:33:58 -0700 | [diff] [blame] | 76 | |
| 77 | return ResponseHelper.getResponse( |
| 78 | mapper(), |
| 79 | ResponseHelper.statusCode.INTERNAL_SERVER_ERROR, |
| 80 | "Exception", |
| 81 | fullStackTrace |
| 82 | ); |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 83 | } |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 84 | } |
| 85 | |
slowr | 60d4d10 | 2017-08-16 18:33:58 -0700 | [diff] [blame] | 86 | return ResponseHelper.getResponse( |
| 87 | mapper(), |
| 88 | ResponseHelper.statusCode.NOT_FOUND, |
| 89 | "Not Found", |
| 90 | "Slice " + sliceid + " not found" |
| 91 | ); |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /** |
| 95 | * test. |
| 96 | * |
| 97 | * @param stream test |
| 98 | * @return test |
| 99 | */ |
| 100 | @POST |
| 101 | @Consumes(MediaType.APPLICATION_JSON) |
slowr | 60d4d10 | 2017-08-16 18:33:58 -0700 | [diff] [blame] | 102 | @Produces(MediaType.APPLICATION_JSON) |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 103 | public Response postSlice(InputStream stream) { |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 104 | try { |
slowr | 60d4d10 | 2017-08-16 18:33:58 -0700 | [diff] [blame] | 105 | // ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
| 106 | // get(XranStore.class).createSlice(jsonTree); |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 107 | |
slowr | 60d4d10 | 2017-08-16 18:33:58 -0700 | [diff] [blame] | 108 | // FIXME: change when implemented |
| 109 | return ResponseHelper.getResponse( |
| 110 | mapper(), |
| 111 | ResponseHelper.statusCode.NOT_IMPLEMENTED, |
| 112 | "Not Implemented", |
| 113 | "POST Slice not implemented" |
| 114 | ); |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 115 | } catch (Exception e) { |
slowr | 60d4d10 | 2017-08-16 18:33:58 -0700 | [diff] [blame] | 116 | String fullStackTrace = ExceptionUtils.getFullStackTrace(e); |
| 117 | log.error(fullStackTrace); |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 118 | e.printStackTrace(); |
slowr | 60d4d10 | 2017-08-16 18:33:58 -0700 | [diff] [blame] | 119 | |
| 120 | return ResponseHelper.getResponse( |
| 121 | mapper(), |
| 122 | ResponseHelper.statusCode.INTERNAL_SERVER_ERROR, |
| 123 | "Exception", |
| 124 | fullStackTrace |
| 125 | ); |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 126 | } |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | } |