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