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 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 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 | |
| 47 | /** |
| 48 | * test. |
| 49 | * |
| 50 | * @param sliceid test |
| 51 | * @return test |
| 52 | */ |
| 53 | @GET |
| 54 | @Path("{sliceid}") |
| 55 | @Produces(MediaType.APPLICATION_JSON) |
| 56 | public Response getSlice(@PathParam("sliceid") long sliceid) { |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 57 | RnibSlice slice = get(XranStore.class).getSlice(sliceid); |
| 58 | |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 59 | if (slice != null) { |
| 60 | try { |
slowr | 8ddc2b1 | 2017-08-14 14:13:38 -0700 | [diff] [blame] | 61 | ObjectNode rootNode = mapper().createObjectNode(); |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 62 | JsonNode jsonNode = mapper().readTree(slice.toString()); |
| 63 | rootNode.put("slice", jsonNode); |
slowr | 8ddc2b1 | 2017-08-14 14:13:38 -0700 | [diff] [blame] | 64 | return ok(rootNode.toString()).build(); |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 65 | } catch (IOException e) { |
| 66 | log.error(ExceptionUtils.getFullStackTrace(e)); |
| 67 | e.printStackTrace(); |
slowr | 8ddc2b1 | 2017-08-14 14:13:38 -0700 | [diff] [blame] | 68 | return Response.serverError() |
| 69 | .entity(ExceptionUtils.getFullStackTrace(e)) |
| 70 | .build(); |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 71 | } |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 72 | } |
| 73 | |
slowr | 8ddc2b1 | 2017-08-14 14:13:38 -0700 | [diff] [blame] | 74 | return Response.serverError().entity("slice not found").build(); |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | /** |
| 78 | * test. |
| 79 | * |
| 80 | * @param stream test |
| 81 | * @return test |
| 82 | */ |
| 83 | @POST |
| 84 | @Consumes(MediaType.APPLICATION_JSON) |
| 85 | public Response postSlice(InputStream stream) { |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 86 | try { |
slowr | 8ddc2b1 | 2017-08-14 14:13:38 -0700 | [diff] [blame] | 87 | boolean b; |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 88 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
| 89 | |
| 90 | b = get(XranStore.class).createSlice(jsonTree); |
slowr | 8ddc2b1 | 2017-08-14 14:13:38 -0700 | [diff] [blame] | 91 | return ok(b).build(); |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 92 | } catch (Exception e) { |
| 93 | log.error(ExceptionUtils.getFullStackTrace(e)); |
| 94 | e.printStackTrace(); |
slowr | 8ddc2b1 | 2017-08-14 14:13:38 -0700 | [diff] [blame] | 95 | return Response.serverError() |
| 96 | .entity(ExceptionUtils.getFullStackTrace(e)) |
| 97 | .build(); |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 98 | } |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | } |