blob: 10fcc5e7a215d3d8fdf85315ac621dd158b8f478 [file] [log] [blame]
slowr13fa5b02017-08-08 16:32:31 -07001/*
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -08002 * Copyright 2017-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 */
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -080016package org.onosproject.xran.impl.rest;
slowr13fa5b02017-08-08 16:32:31 -070017
18import com.fasterxml.jackson.databind.JsonNode;
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -080019import io.swagger.annotations.ApiResponse;
20import io.swagger.annotations.ApiResponses;
slowr13fa5b02017-08-08 16:32:31 -070021import org.apache.commons.lang.exception.ExceptionUtils;
22import org.onosproject.rest.AbstractWebResource;
23import org.onosproject.xran.XranStore;
slowr13fa5b02017-08-08 16:32:31 -070024import 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;
slowr13fa5b02017-08-08 16:32:31 -070035import java.io.InputStream;
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -080036import java.net.HttpURLConnection;
slowr13fa5b02017-08-08 16:32:31 -070037
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
slowr60d4d102017-08-16 18:33:58 -070047 public SliceWebResource() {
slowr60d4d102017-08-16 18:33:58 -070048 }
49
slowr13fa5b02017-08-08 16:32:31 -070050 /**
slowr577f3222017-08-28 10:49:08 -070051 * List the slice with the given slice ID.
slowr13fa5b02017-08-08 16:32:31 -070052 *
slowr577f3222017-08-28 10:49:08 -070053 * @param sliceid ID of the slice
54 * @return Response
slowr13fa5b02017-08-08 16:32:31 -070055 */
56 @GET
57 @Path("{sliceid}")
58 @Produces(MediaType.APPLICATION_JSON)
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -080059 @ApiResponses(value = {
60 @ApiResponse(code = 200, message = "HTTP_OK"),
61 @ApiResponse(code = 500, message = "HTTP_INTERNAL_ERROR"),
62 @ApiResponse(code = 404, message = "HTTP_NOT_FOUND")
63 })
slowr13fa5b02017-08-08 16:32:31 -070064 public Response getSlice(@PathParam("sliceid") long sliceid) {
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -080065 return get(XranStore.class).getSlice(sliceid).map(slice -> {
slowr13fa5b02017-08-08 16:32:31 -070066 try {
slowr60d4d102017-08-16 18:33:58 -070067 JsonNode jsonNode = mapper().valueToTree(slice);
68
69 return ResponseHelper.getResponse(
70 mapper(),
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -080071 HttpURLConnection.HTTP_OK,
slowr60d4d102017-08-16 18:33:58 -070072 jsonNode
73 );
74 } catch (Exception e) {
75 String fullStackTrace = ExceptionUtils.getFullStackTrace(e);
76 log.error(fullStackTrace);
slowr60d4d102017-08-16 18:33:58 -070077
78 return ResponseHelper.getResponse(
79 mapper(),
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -080080 HttpURLConnection.HTTP_INTERNAL_ERROR,
slowr60d4d102017-08-16 18:33:58 -070081 "Exception",
82 fullStackTrace
83 );
slowr13fa5b02017-08-08 16:32:31 -070084 }
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -080085 }).orElse(
86 ResponseHelper.getResponse(
87 mapper(),
88 HttpURLConnection.HTTP_NOT_FOUND,
89 "Not Found",
90 "Slice " + sliceid + " not found"
91 )
slowr60d4d102017-08-16 18:33:58 -070092 );
slowr13fa5b02017-08-08 16:32:31 -070093 }
94
95 /**
slowr577f3222017-08-28 10:49:08 -070096 * Create slice with the corresponding attributes.
slowr13fa5b02017-08-08 16:32:31 -070097 *
slowr577f3222017-08-28 10:49:08 -070098 * @param stream Attributes to create slice
99 * @return Response
slowr13fa5b02017-08-08 16:32:31 -0700100 */
101 @POST
102 @Consumes(MediaType.APPLICATION_JSON)
slowr60d4d102017-08-16 18:33:58 -0700103 @Produces(MediaType.APPLICATION_JSON)
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -0800104 @ApiResponses(value = {
105 @ApiResponse(code = 200, message = "HTTP_OK"),
106 @ApiResponse(code = 500, message = "HTTP_INTERNAL_ERROR"),
107 @ApiResponse(code = 501, message = "HTTP_NOT_IMPLEMENTED")
108 })
slowr13fa5b02017-08-08 16:32:31 -0700109 public Response postSlice(InputStream stream) {
slowr13fa5b02017-08-08 16:32:31 -0700110 try {
slowr60d4d102017-08-16 18:33:58 -0700111// ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
112// get(XranStore.class).createSlice(jsonTree);
slowr13fa5b02017-08-08 16:32:31 -0700113
slowr60d4d102017-08-16 18:33:58 -0700114 // FIXME: change when implemented
115 return ResponseHelper.getResponse(
116 mapper(),
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -0800117 HttpURLConnection.HTTP_NOT_IMPLEMENTED,
slowr60d4d102017-08-16 18:33:58 -0700118 "Not Implemented",
119 "POST Slice not implemented"
120 );
slowr13fa5b02017-08-08 16:32:31 -0700121 } catch (Exception e) {
slowr60d4d102017-08-16 18:33:58 -0700122 String fullStackTrace = ExceptionUtils.getFullStackTrace(e);
123 log.error(fullStackTrace);
slowr13fa5b02017-08-08 16:32:31 -0700124 e.printStackTrace();
slowr60d4d102017-08-16 18:33:58 -0700125
126 return ResponseHelper.getResponse(
127 mapper(),
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -0800128 HttpURLConnection.HTTP_INTERNAL_ERROR,
slowr60d4d102017-08-16 18:33:58 -0700129 "Exception",
130 fullStackTrace
131 );
slowr13fa5b02017-08-08 16:32:31 -0700132 }
slowr13fa5b02017-08-08 16:32:31 -0700133 }
134
135}