blob: e895d55c827a7570bb6a900c3f832a4fc0c01355 [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) {
slowr13fa5b02017-08-08 16:32:31 -070057 RnibSlice slice = get(XranStore.class).getSlice(sliceid);
58
slowr13fa5b02017-08-08 16:32:31 -070059 if (slice != null) {
60 try {
slowr8ddc2b12017-08-14 14:13:38 -070061 ObjectNode rootNode = mapper().createObjectNode();
slowr13fa5b02017-08-08 16:32:31 -070062 JsonNode jsonNode = mapper().readTree(slice.toString());
63 rootNode.put("slice", jsonNode);
slowr8ddc2b12017-08-14 14:13:38 -070064 return ok(rootNode.toString()).build();
slowr13fa5b02017-08-08 16:32:31 -070065 } catch (IOException e) {
66 log.error(ExceptionUtils.getFullStackTrace(e));
67 e.printStackTrace();
slowr8ddc2b12017-08-14 14:13:38 -070068 return Response.serverError()
69 .entity(ExceptionUtils.getFullStackTrace(e))
70 .build();
slowr13fa5b02017-08-08 16:32:31 -070071 }
slowr13fa5b02017-08-08 16:32:31 -070072 }
73
slowr8ddc2b12017-08-14 14:13:38 -070074 return Response.serverError().entity("slice not found").build();
slowr13fa5b02017-08-08 16:32:31 -070075 }
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) {
slowr13fa5b02017-08-08 16:32:31 -070086 try {
slowr8ddc2b12017-08-14 14:13:38 -070087 boolean b;
slowr13fa5b02017-08-08 16:32:31 -070088 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
89
90 b = get(XranStore.class).createSlice(jsonTree);
slowr8ddc2b12017-08-14 14:13:38 -070091 return ok(b).build();
slowr13fa5b02017-08-08 16:32:31 -070092 } catch (Exception e) {
93 log.error(ExceptionUtils.getFullStackTrace(e));
94 e.printStackTrace();
slowr8ddc2b12017-08-14 14:13:38 -070095 return Response.serverError()
96 .entity(ExceptionUtils.getFullStackTrace(e))
97 .build();
slowr13fa5b02017-08-08 16:32:31 -070098 }
slowr13fa5b02017-08-08 16:32:31 -070099 }
100
101}