slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 1 | /* |
Dimitrios Mavrommatis | 96b255a | 2017-12-06 13:09:25 -0800 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 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 | */ |
Dimitrios Mavrommatis | 96b255a | 2017-12-06 13:09:25 -0800 | [diff] [blame] | 16 | package org.onosproject.xran.impl.rest; |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
Dimitrios Mavrommatis | 96b255a | 2017-12-06 13:09:25 -0800 | [diff] [blame] | 19 | import io.swagger.annotations.ApiResponse; |
| 20 | import io.swagger.annotations.ApiResponses; |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 21 | import org.apache.commons.lang.StringUtils; |
| 22 | import org.apache.commons.lang.exception.ExceptionUtils; |
| 23 | import org.onosproject.rest.AbstractWebResource; |
| 24 | import org.onosproject.xran.XranStore; |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 25 | import org.slf4j.Logger; |
| 26 | import org.slf4j.LoggerFactory; |
| 27 | |
| 28 | import javax.ws.rs.DefaultValue; |
| 29 | import javax.ws.rs.GET; |
| 30 | import javax.ws.rs.Path; |
| 31 | import javax.ws.rs.PathParam; |
| 32 | import javax.ws.rs.Produces; |
Dimitrios Mavrommatis | 96b255a | 2017-12-06 13:09:25 -0800 | [diff] [blame] | 33 | import javax.ws.rs.QueryParam; |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 34 | import javax.ws.rs.core.MediaType; |
| 35 | import javax.ws.rs.core.Response; |
Dimitrios Mavrommatis | 96b255a | 2017-12-06 13:09:25 -0800 | [diff] [blame] | 36 | import java.net.HttpURLConnection; |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 37 | import java.util.List; |
| 38 | |
| 39 | /** |
| 40 | * Node web resource. |
| 41 | */ |
| 42 | @Path("nodes") |
| 43 | public class NodeWebResource extends AbstractWebResource { |
| 44 | |
| 45 | private static final Logger log = |
| 46 | LoggerFactory.getLogger(NodeWebResource.class); |
| 47 | |
slowr | 60d4d10 | 2017-08-16 18:33:58 -0700 | [diff] [blame] | 48 | public NodeWebResource() { |
slowr | 60d4d10 | 2017-08-16 18:33:58 -0700 | [diff] [blame] | 49 | } |
| 50 | |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 51 | /** |
slowr | 577f322 | 2017-08-28 10:49:08 -0700 | [diff] [blame] | 52 | * List all the nodes in the R-NIB. |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 53 | * |
slowr | 577f322 | 2017-08-28 10:49:08 -0700 | [diff] [blame] | 54 | * @param type Type of node (cell/ue) |
| 55 | * @return Response |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 56 | */ |
| 57 | @GET |
| 58 | @Produces(MediaType.APPLICATION_JSON) |
Dimitrios Mavrommatis | 96b255a | 2017-12-06 13:09:25 -0800 | [diff] [blame] | 59 | @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 | }) |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 64 | public Response getNodes(@DefaultValue("") @QueryParam("type") String type) { |
slowr | 60d4d10 | 2017-08-16 18:33:58 -0700 | [diff] [blame] | 65 | JsonNode jsonNode; |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 66 | try { |
Dimitrios Mavrommatis | 96b255a | 2017-12-06 13:09:25 -0800 | [diff] [blame] | 67 | List<? extends Object> nodes; |
slowr | 577f322 | 2017-08-28 10:49:08 -0700 | [diff] [blame] | 68 | // List cell type of nodes or UE type of nodes. |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 69 | if (StringUtils.isBlank(type)) { |
slowr | 60d4d10 | 2017-08-16 18:33:58 -0700 | [diff] [blame] | 70 | nodes = get(XranStore.class).getNodes(); |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 71 | } else if (type.equals("cell")) { |
Dimitrios Mavrommatis | 96b255a | 2017-12-06 13:09:25 -0800 | [diff] [blame] | 72 | nodes = get(XranStore.class).getCellNodes(); |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 73 | } else if (type.equals("ue")) { |
Dimitrios Mavrommatis | 96b255a | 2017-12-06 13:09:25 -0800 | [diff] [blame] | 74 | nodes = get(XranStore.class).getUeNodes(); |
slowr | 60d4d10 | 2017-08-16 18:33:58 -0700 | [diff] [blame] | 75 | } else { |
| 76 | return ResponseHelper.getResponse( |
| 77 | mapper(), |
Dimitrios Mavrommatis | 96b255a | 2017-12-06 13:09:25 -0800 | [diff] [blame] | 78 | HttpURLConnection.HTTP_NOT_FOUND, |
slowr | 60d4d10 | 2017-08-16 18:33:58 -0700 | [diff] [blame] | 79 | "Not Found", |
| 80 | "Type of node was not found" |
| 81 | ); |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 82 | } |
slowr | 60d4d10 | 2017-08-16 18:33:58 -0700 | [diff] [blame] | 83 | |
| 84 | if (nodes.size() == 0) { |
| 85 | return ResponseHelper.getResponse( |
| 86 | mapper(), |
Dimitrios Mavrommatis | 96b255a | 2017-12-06 13:09:25 -0800 | [diff] [blame] | 87 | HttpURLConnection.HTTP_NOT_FOUND, |
slowr | 60d4d10 | 2017-08-16 18:33:58 -0700 | [diff] [blame] | 88 | "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); |
slowr | 60d4d10 | 2017-08-16 18:33:58 -0700 | [diff] [blame] | 97 | |
| 98 | return ResponseHelper.getResponse( |
| 99 | mapper(), |
Dimitrios Mavrommatis | 96b255a | 2017-12-06 13:09:25 -0800 | [diff] [blame] | 100 | HttpURLConnection.HTTP_INTERNAL_ERROR, |
slowr | 60d4d10 | 2017-08-16 18:33:58 -0700 | [diff] [blame] | 101 | "Exception", |
| 102 | fullStackTrace |
| 103 | ); |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 104 | } |
| 105 | |
slowr | 60d4d10 | 2017-08-16 18:33:58 -0700 | [diff] [blame] | 106 | return ResponseHelper.getResponse( |
| 107 | mapper(), |
Dimitrios Mavrommatis | 96b255a | 2017-12-06 13:09:25 -0800 | [diff] [blame] | 108 | HttpURLConnection.HTTP_OK, |
slowr | 60d4d10 | 2017-08-16 18:33:58 -0700 | [diff] [blame] | 109 | jsonNode |
| 110 | ); |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | /** |
slowr | 577f322 | 2017-08-28 10:49:08 -0700 | [diff] [blame] | 114 | * List the node with a specific node id. |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 115 | * |
slowr | 577f322 | 2017-08-28 10:49:08 -0700 | [diff] [blame] | 116 | * @param nodeid ID of the node |
| 117 | * @return Response |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 118 | */ |
| 119 | @GET |
| 120 | @Path("{nodeid}") |
| 121 | @Produces(MediaType.APPLICATION_JSON) |
Dimitrios Mavrommatis | 96b255a | 2017-12-06 13:09:25 -0800 | [diff] [blame] | 122 | @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 | }) |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 127 | public Response getNodeid(@PathParam("nodeid") String nodeid) { |
Dimitrios Mavrommatis | 96b255a | 2017-12-06 13:09:25 -0800 | [diff] [blame] | 128 | Object node = get(XranStore.class).getNode(nodeid); |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 129 | |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 130 | if (node != null) { |
| 131 | try { |
slowr | 60d4d10 | 2017-08-16 18:33:58 -0700 | [diff] [blame] | 132 | JsonNode jsonNode = mapper().valueToTree(node); |
| 133 | |
| 134 | return ResponseHelper.getResponse( |
| 135 | mapper(), |
Dimitrios Mavrommatis | 96b255a | 2017-12-06 13:09:25 -0800 | [diff] [blame] | 136 | HttpURLConnection.HTTP_OK, |
slowr | 60d4d10 | 2017-08-16 18:33:58 -0700 | [diff] [blame] | 137 | jsonNode |
| 138 | ); |
| 139 | } catch (Exception e) { |
| 140 | String fullStackTrace = ExceptionUtils.getFullStackTrace(e); |
| 141 | log.error(fullStackTrace); |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 142 | e.printStackTrace(); |
slowr | 60d4d10 | 2017-08-16 18:33:58 -0700 | [diff] [blame] | 143 | |
| 144 | return ResponseHelper.getResponse( |
| 145 | mapper(), |
Dimitrios Mavrommatis | 96b255a | 2017-12-06 13:09:25 -0800 | [diff] [blame] | 146 | HttpURLConnection.HTTP_INTERNAL_ERROR, |
slowr | 60d4d10 | 2017-08-16 18:33:58 -0700 | [diff] [blame] | 147 | "Exception", |
| 148 | fullStackTrace |
| 149 | ); |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 150 | } |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 151 | } |
| 152 | |
slowr | 60d4d10 | 2017-08-16 18:33:58 -0700 | [diff] [blame] | 153 | return ResponseHelper.getResponse( |
| 154 | mapper(), |
Dimitrios Mavrommatis | 96b255a | 2017-12-06 13:09:25 -0800 | [diff] [blame] | 155 | HttpURLConnection.HTTP_NOT_FOUND, |
slowr | 60d4d10 | 2017-08-16 18:33:58 -0700 | [diff] [blame] | 156 | "Not Found", |
| 157 | "Node " + nodeid + " was not found" |
| 158 | ); |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | } |