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) { |
| 57 | log.debug("GET SLICE {}", sliceid); |
| 58 | |
| 59 | RnibSlice slice = get(XranStore.class).getSlice(sliceid); |
| 60 | |
| 61 | ObjectNode rootNode = mapper().createObjectNode(); |
| 62 | |
| 63 | if (slice != null) { |
| 64 | try { |
| 65 | JsonNode jsonNode = mapper().readTree(slice.toString()); |
| 66 | rootNode.put("slice", jsonNode); |
| 67 | } catch (IOException e) { |
| 68 | log.error(ExceptionUtils.getFullStackTrace(e)); |
| 69 | e.printStackTrace(); |
| 70 | } |
| 71 | } else { |
| 72 | rootNode.put("error", "not found"); |
| 73 | } |
| 74 | |
| 75 | return ok(rootNode.toString()).build(); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * test. |
| 80 | * |
| 81 | * @param stream test |
| 82 | * @return test |
| 83 | */ |
| 84 | @POST |
| 85 | @Consumes(MediaType.APPLICATION_JSON) |
| 86 | public Response postSlice(InputStream stream) { |
| 87 | log.debug("POST SLICE"); |
| 88 | |
| 89 | boolean b = false; |
| 90 | try { |
| 91 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
| 92 | |
| 93 | b = get(XranStore.class).createSlice(jsonTree); |
| 94 | } catch (Exception e) { |
| 95 | log.error(ExceptionUtils.getFullStackTrace(e)); |
| 96 | e.printStackTrace(); |
| 97 | } |
| 98 | |
| 99 | return ok(b).build(); |
| 100 | } |
| 101 | |
| 102 | } |