blob: 2fe83852aecd2a7f419cc204c5310f1865ff92c7 [file] [log] [blame]
slowr13fa5b02017-08-08 16:32:31 -07001/*
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 */
16package org.onosproject.xran.rest;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import org.apache.commons.lang.exception.ExceptionUtils;
21import org.onosproject.rest.AbstractWebResource;
22import org.onosproject.xran.XranStore;
23import org.onosproject.xran.entities.RnibSlice;
24import org.slf4j.Logger;
25import org.slf4j.LoggerFactory;
26
27import javax.ws.rs.Consumes;
28import javax.ws.rs.GET;
29import javax.ws.rs.POST;
30import javax.ws.rs.Path;
31import javax.ws.rs.PathParam;
32import javax.ws.rs.Produces;
33import javax.ws.rs.core.MediaType;
34import javax.ws.rs.core.Response;
35import java.io.IOException;
36import java.io.InputStream;
37
38/**
39 * Slice web resource.
40 */
41@Path("slice")
42public 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}