blob: 6c74d953be6857cd2c812079e06a52431a1b9137 [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.StringUtils;
22import org.apache.commons.lang.exception.ExceptionUtils;
23import org.onosproject.rest.AbstractWebResource;
24import org.onosproject.xran.XranStore;
slowr13fa5b02017-08-08 16:32:31 -070025import org.slf4j.Logger;
26import org.slf4j.LoggerFactory;
27
28import javax.ws.rs.DefaultValue;
29import javax.ws.rs.GET;
30import javax.ws.rs.Path;
31import javax.ws.rs.PathParam;
32import javax.ws.rs.Produces;
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -080033import javax.ws.rs.QueryParam;
slowr13fa5b02017-08-08 16:32:31 -070034import javax.ws.rs.core.MediaType;
35import javax.ws.rs.core.Response;
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -080036import java.net.HttpURLConnection;
slowr13fa5b02017-08-08 16:32:31 -070037import java.util.List;
38
39/**
40 * Node web resource.
41 */
42@Path("nodes")
43public class NodeWebResource extends AbstractWebResource {
44
45 private static final Logger log =
46 LoggerFactory.getLogger(NodeWebResource.class);
47
slowr60d4d102017-08-16 18:33:58 -070048 public NodeWebResource() {
slowr60d4d102017-08-16 18:33:58 -070049 }
50
slowr13fa5b02017-08-08 16:32:31 -070051 /**
slowr577f3222017-08-28 10:49:08 -070052 * List all the nodes in the R-NIB.
slowr13fa5b02017-08-08 16:32:31 -070053 *
slowr577f3222017-08-28 10:49:08 -070054 * @param type Type of node (cell/ue)
55 * @return Response
slowr13fa5b02017-08-08 16:32:31 -070056 */
57 @GET
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 getNodes(@DefaultValue("") @QueryParam("type") String type) {
slowr60d4d102017-08-16 18:33:58 -070065 JsonNode jsonNode;
slowr13fa5b02017-08-08 16:32:31 -070066 try {
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -080067 List<? extends Object> nodes;
slowr577f3222017-08-28 10:49:08 -070068 // List cell type of nodes or UE type of nodes.
slowr13fa5b02017-08-08 16:32:31 -070069 if (StringUtils.isBlank(type)) {
slowr60d4d102017-08-16 18:33:58 -070070 nodes = get(XranStore.class).getNodes();
slowr13fa5b02017-08-08 16:32:31 -070071 } else if (type.equals("cell")) {
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -080072 nodes = get(XranStore.class).getCellNodes();
slowr13fa5b02017-08-08 16:32:31 -070073 } else if (type.equals("ue")) {
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -080074 nodes = get(XranStore.class).getUeNodes();
slowr60d4d102017-08-16 18:33:58 -070075 } else {
76 return ResponseHelper.getResponse(
77 mapper(),
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -080078 HttpURLConnection.HTTP_NOT_FOUND,
slowr60d4d102017-08-16 18:33:58 -070079 "Not Found",
80 "Type of node was not found"
81 );
slowr13fa5b02017-08-08 16:32:31 -070082 }
slowr60d4d102017-08-16 18:33:58 -070083
84 if (nodes.size() == 0) {
85 return ResponseHelper.getResponse(
86 mapper(),
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -080087 HttpURLConnection.HTTP_NOT_FOUND,
slowr60d4d102017-08-16 18:33:58 -070088 "Not Found",
89 "No nodes found"
90 );
91 }
92
93 jsonNode = mapper().valueToTree(nodes);
94 } catch (Exception e) {
95 String fullStackTrace = ExceptionUtils.getFullStackTrace(e);
96 log.error(fullStackTrace);
slowr60d4d102017-08-16 18:33:58 -070097
98 return ResponseHelper.getResponse(
99 mapper(),
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -0800100 HttpURLConnection.HTTP_INTERNAL_ERROR,
slowr60d4d102017-08-16 18:33:58 -0700101 "Exception",
102 fullStackTrace
103 );
slowr13fa5b02017-08-08 16:32:31 -0700104 }
105
slowr60d4d102017-08-16 18:33:58 -0700106 return ResponseHelper.getResponse(
107 mapper(),
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -0800108 HttpURLConnection.HTTP_OK,
slowr60d4d102017-08-16 18:33:58 -0700109 jsonNode
110 );
slowr13fa5b02017-08-08 16:32:31 -0700111 }
112
113 /**
slowr577f3222017-08-28 10:49:08 -0700114 * List the node with a specific node id.
slowr13fa5b02017-08-08 16:32:31 -0700115 *
slowr577f3222017-08-28 10:49:08 -0700116 * @param nodeid ID of the node
117 * @return Response
slowr13fa5b02017-08-08 16:32:31 -0700118 */
119 @GET
120 @Path("{nodeid}")
121 @Produces(MediaType.APPLICATION_JSON)
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -0800122 @ApiResponses(value = {
123 @ApiResponse(code = 200, message = "HTTP_OK"),
124 @ApiResponse(code = 500, message = "HTTP_INTERNAL_ERROR"),
125 @ApiResponse(code = 404, message = "HTTP_NOT_FOUND")
126 })
slowr13fa5b02017-08-08 16:32:31 -0700127 public Response getNodeid(@PathParam("nodeid") String nodeid) {
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -0800128 Object node = get(XranStore.class).getNode(nodeid);
slowr13fa5b02017-08-08 16:32:31 -0700129
slowr13fa5b02017-08-08 16:32:31 -0700130 if (node != null) {
131 try {
slowr60d4d102017-08-16 18:33:58 -0700132 JsonNode jsonNode = mapper().valueToTree(node);
133
134 return ResponseHelper.getResponse(
135 mapper(),
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -0800136 HttpURLConnection.HTTP_OK,
slowr60d4d102017-08-16 18:33:58 -0700137 jsonNode
138 );
139 } catch (Exception e) {
140 String fullStackTrace = ExceptionUtils.getFullStackTrace(e);
141 log.error(fullStackTrace);
slowr13fa5b02017-08-08 16:32:31 -0700142 e.printStackTrace();
slowr60d4d102017-08-16 18:33:58 -0700143
144 return ResponseHelper.getResponse(
145 mapper(),
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -0800146 HttpURLConnection.HTTP_INTERNAL_ERROR,
slowr60d4d102017-08-16 18:33:58 -0700147 "Exception",
148 fullStackTrace
149 );
slowr13fa5b02017-08-08 16:32:31 -0700150 }
slowr13fa5b02017-08-08 16:32:31 -0700151 }
152
slowr60d4d102017-08-16 18:33:58 -0700153 return ResponseHelper.getResponse(
154 mapper(),
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -0800155 HttpURLConnection.HTTP_NOT_FOUND,
slowr60d4d102017-08-16 18:33:58 -0700156 "Not Found",
157 "Node " + nodeid + " was not found"
158 );
slowr13fa5b02017-08-08 16:32:31 -0700159 }
160
161}