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 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import org.apache.commons.lang.StringUtils; |
| 21 | import org.apache.commons.lang.exception.ExceptionUtils; |
| 22 | import org.onosproject.rest.AbstractWebResource; |
| 23 | import org.onosproject.xran.XranStore; |
| 24 | import org.onosproject.xran.entities.RnibCell; |
| 25 | import org.onosproject.xran.entities.RnibUe; |
| 26 | import org.slf4j.Logger; |
| 27 | import org.slf4j.LoggerFactory; |
| 28 | |
| 29 | import javax.ws.rs.DefaultValue; |
| 30 | import javax.ws.rs.GET; |
| 31 | import javax.ws.rs.Path; |
| 32 | import javax.ws.rs.PathParam; |
| 33 | import javax.ws.rs.Produces; |
| 34 | import javax.ws.rs.QueryParam; |
| 35 | import javax.ws.rs.core.MediaType; |
| 36 | import javax.ws.rs.core.Response; |
| 37 | import java.io.IOException; |
| 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 | |
| 49 | /** |
| 50 | * test. |
| 51 | * |
| 52 | * @param type test |
| 53 | * @return test |
| 54 | */ |
| 55 | @GET |
| 56 | @Produces(MediaType.APPLICATION_JSON) |
| 57 | public Response getNodes(@DefaultValue("") @QueryParam("type") String type) { |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 58 | ObjectNode rootNode = mapper().createObjectNode(); |
| 59 | |
| 60 | try { |
| 61 | if (StringUtils.isBlank(type)) { |
| 62 | List<Object> nodes = get(XranStore.class).getNodes(); |
| 63 | |
| 64 | JsonNode jsonNode = mapper().readTree(nodes.get(0).toString()); |
| 65 | JsonNode jsonNode2 = mapper().readTree(nodes.get(1).toString()); |
| 66 | |
| 67 | ObjectNode arrayNode = rootNode.putObject("nodes"); |
| 68 | arrayNode.put("cells", jsonNode); |
| 69 | arrayNode.put("ues", jsonNode2); |
| 70 | } else if (type.equals("cell")) { |
| 71 | List<RnibCell> cellNodes = get(XranStore.class).getCellNodes(); |
| 72 | JsonNode jsonNode = mapper().readTree(cellNodes.toString()); |
| 73 | |
| 74 | ObjectNode arrayNode = rootNode.putObject("nodes"); |
| 75 | arrayNode.put("cells", jsonNode); |
| 76 | } else if (type.equals("ue")) { |
| 77 | List<RnibUe> ueNodes = get(XranStore.class).getUeNodes(); |
| 78 | JsonNode jsonNode = mapper().readTree(ueNodes.toString()); |
| 79 | |
| 80 | ObjectNode arrayNode = rootNode.putObject("nodes"); |
| 81 | arrayNode.put("ues", jsonNode); |
| 82 | } |
| 83 | } catch (IOException e) { |
| 84 | log.error(ExceptionUtils.getFullStackTrace(e)); |
| 85 | e.printStackTrace(); |
slowr | 8ddc2b1 | 2017-08-14 14:13:38 -0700 | [diff] [blame] | 86 | return Response.serverError() |
| 87 | .entity(ExceptionUtils.getFullStackTrace(e)) |
| 88 | .build(); |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | return ok(rootNode.toString()).build(); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * test. |
| 96 | * |
| 97 | * @param nodeid test |
| 98 | * @return test |
| 99 | */ |
| 100 | @GET |
| 101 | @Path("{nodeid}") |
| 102 | @Produces(MediaType.APPLICATION_JSON) |
| 103 | public Response getNodeid(@PathParam("nodeid") String nodeid) { |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 104 | Object node = get(XranStore.class).getByNodeId(nodeid); |
| 105 | |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 106 | if (node != null) { |
| 107 | try { |
slowr | 8ddc2b1 | 2017-08-14 14:13:38 -0700 | [diff] [blame] | 108 | ObjectNode rootNode = mapper().createObjectNode(); |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 109 | JsonNode jsonNode = mapper().readTree(node.toString()); |
| 110 | rootNode.put("node", jsonNode); |
slowr | 8ddc2b1 | 2017-08-14 14:13:38 -0700 | [diff] [blame] | 111 | return ok(rootNode.toString()).build(); |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 112 | } catch (IOException e) { |
| 113 | log.error(ExceptionUtils.getFullStackTrace(e)); |
| 114 | e.printStackTrace(); |
slowr | 8ddc2b1 | 2017-08-14 14:13:38 -0700 | [diff] [blame] | 115 | return Response.serverError() |
| 116 | .entity(ExceptionUtils.getFullStackTrace(e)) |
| 117 | .build(); |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 118 | } |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 119 | } |
| 120 | |
slowr | 8ddc2b1 | 2017-08-14 14:13:38 -0700 | [diff] [blame] | 121 | return Response.serverError().entity("node not found").build(); |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | } |