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