blob: 8817be81dd21b6adb78cce5ece2acf37bdaf3be3 [file] [log] [blame]
slowr13fa5b02017-08-08 16:32:31 -07001/*
slowr577f3222017-08-28 10:49:08 -07002 * Copyright 2016-present Open Networking Foundation
slowr13fa5b02017-08-08 16:32:31 -07003 *
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;
slowr13fa5b02017-08-08 16:32:31 -070019import org.apache.commons.lang.exception.ExceptionUtils;
20import org.onosproject.rest.AbstractWebResource;
21import org.onosproject.xran.XranStore;
22import org.onosproject.xran.entities.RnibSlice;
23import org.slf4j.Logger;
24import org.slf4j.LoggerFactory;
25
26import javax.ws.rs.Consumes;
27import javax.ws.rs.GET;
28import javax.ws.rs.POST;
29import javax.ws.rs.Path;
30import javax.ws.rs.PathParam;
31import javax.ws.rs.Produces;
32import javax.ws.rs.core.MediaType;
33import javax.ws.rs.core.Response;
slowr13fa5b02017-08-08 16:32:31 -070034import java.io.InputStream;
35
36/**
37 * Slice web resource.
38 */
39@Path("slice")
40public class SliceWebResource extends AbstractWebResource {
41
42 private static final Logger log =
43 LoggerFactory.getLogger(SliceWebResource.class);
44
slowr60d4d102017-08-16 18:33:58 -070045 public SliceWebResource() {
46
47 }
48
slowr13fa5b02017-08-08 16:32:31 -070049 /**
slowr577f3222017-08-28 10:49:08 -070050 * List the slice with the given slice ID.
slowr13fa5b02017-08-08 16:32:31 -070051 *
slowr577f3222017-08-28 10:49:08 -070052 * @param sliceid ID of the slice
53 * @return Response
slowr13fa5b02017-08-08 16:32:31 -070054 */
55 @GET
56 @Path("{sliceid}")
57 @Produces(MediaType.APPLICATION_JSON)
58 public Response getSlice(@PathParam("sliceid") long sliceid) {
slowr13fa5b02017-08-08 16:32:31 -070059 RnibSlice slice = get(XranStore.class).getSlice(sliceid);
60
slowr13fa5b02017-08-08 16:32:31 -070061 if (slice != null) {
62 try {
slowr60d4d102017-08-16 18:33:58 -070063 JsonNode jsonNode = mapper().valueToTree(slice);
64
65 return ResponseHelper.getResponse(
66 mapper(),
slowr577f3222017-08-28 10:49:08 -070067 ResponseHelper.StatusCode.OK,
slowr60d4d102017-08-16 18:33:58 -070068 jsonNode
69 );
70 } catch (Exception e) {
71 String fullStackTrace = ExceptionUtils.getFullStackTrace(e);
72 log.error(fullStackTrace);
slowr13fa5b02017-08-08 16:32:31 -070073 e.printStackTrace();
slowr60d4d102017-08-16 18:33:58 -070074
75 return ResponseHelper.getResponse(
76 mapper(),
slowr577f3222017-08-28 10:49:08 -070077 ResponseHelper.StatusCode.INTERNAL_SERVER_ERROR,
slowr60d4d102017-08-16 18:33:58 -070078 "Exception",
79 fullStackTrace
80 );
slowr13fa5b02017-08-08 16:32:31 -070081 }
slowr13fa5b02017-08-08 16:32:31 -070082 }
83
slowr60d4d102017-08-16 18:33:58 -070084 return ResponseHelper.getResponse(
85 mapper(),
slowr577f3222017-08-28 10:49:08 -070086 ResponseHelper.StatusCode.NOT_FOUND,
slowr60d4d102017-08-16 18:33:58 -070087 "Not Found",
88 "Slice " + sliceid + " not found"
89 );
slowr13fa5b02017-08-08 16:32:31 -070090 }
91
92 /**
slowr577f3222017-08-28 10:49:08 -070093 * Create slice with the corresponding attributes.
slowr13fa5b02017-08-08 16:32:31 -070094 *
slowr577f3222017-08-28 10:49:08 -070095 * @param stream Attributes to create slice
96 * @return Response
slowr13fa5b02017-08-08 16:32:31 -070097 */
98 @POST
99 @Consumes(MediaType.APPLICATION_JSON)
slowr60d4d102017-08-16 18:33:58 -0700100 @Produces(MediaType.APPLICATION_JSON)
slowr13fa5b02017-08-08 16:32:31 -0700101 public Response postSlice(InputStream stream) {
slowr13fa5b02017-08-08 16:32:31 -0700102 try {
slowr60d4d102017-08-16 18:33:58 -0700103// ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
104// get(XranStore.class).createSlice(jsonTree);
slowr13fa5b02017-08-08 16:32:31 -0700105
slowr60d4d102017-08-16 18:33:58 -0700106 // FIXME: change when implemented
107 return ResponseHelper.getResponse(
108 mapper(),
slowr577f3222017-08-28 10:49:08 -0700109 ResponseHelper.StatusCode.NOT_IMPLEMENTED,
slowr60d4d102017-08-16 18:33:58 -0700110 "Not Implemented",
111 "POST Slice not implemented"
112 );
slowr13fa5b02017-08-08 16:32:31 -0700113 } catch (Exception e) {
slowr60d4d102017-08-16 18:33:58 -0700114 String fullStackTrace = ExceptionUtils.getFullStackTrace(e);
115 log.error(fullStackTrace);
slowr13fa5b02017-08-08 16:32:31 -0700116 e.printStackTrace();
slowr60d4d102017-08-16 18:33:58 -0700117
118 return ResponseHelper.getResponse(
119 mapper(),
slowr577f3222017-08-28 10:49:08 -0700120 ResponseHelper.StatusCode.INTERNAL_SERVER_ERROR,
slowr60d4d102017-08-16 18:33:58 -0700121 "Exception",
122 fullStackTrace
123 );
slowr13fa5b02017-08-08 16:32:31 -0700124 }
slowr13fa5b02017-08-08 16:32:31 -0700125 }
126
127}