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) { |
| 58 | log.debug("GET NODES " + type); |
| 59 | |
| 60 | ObjectNode rootNode = mapper().createObjectNode(); |
| 61 | |
| 62 | try { |
| 63 | if (StringUtils.isBlank(type)) { |
| 64 | List<Object> nodes = get(XranStore.class).getNodes(); |
| 65 | |
| 66 | JsonNode jsonNode = mapper().readTree(nodes.get(0).toString()); |
| 67 | JsonNode jsonNode2 = mapper().readTree(nodes.get(1).toString()); |
| 68 | |
| 69 | ObjectNode arrayNode = rootNode.putObject("nodes"); |
| 70 | arrayNode.put("cells", jsonNode); |
| 71 | arrayNode.put("ues", jsonNode2); |
| 72 | } else if (type.equals("cell")) { |
| 73 | List<RnibCell> cellNodes = get(XranStore.class).getCellNodes(); |
| 74 | JsonNode jsonNode = mapper().readTree(cellNodes.toString()); |
| 75 | |
| 76 | ObjectNode arrayNode = rootNode.putObject("nodes"); |
| 77 | arrayNode.put("cells", jsonNode); |
| 78 | } else if (type.equals("ue")) { |
| 79 | List<RnibUe> ueNodes = get(XranStore.class).getUeNodes(); |
| 80 | JsonNode jsonNode = mapper().readTree(ueNodes.toString()); |
| 81 | |
| 82 | ObjectNode arrayNode = rootNode.putObject("nodes"); |
| 83 | arrayNode.put("ues", jsonNode); |
| 84 | } |
| 85 | } catch (IOException e) { |
| 86 | log.error(ExceptionUtils.getFullStackTrace(e)); |
| 87 | e.printStackTrace(); |
| 88 | } |
| 89 | |
| 90 | return ok(rootNode.toString()).build(); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * test. |
| 95 | * |
| 96 | * @param nodeid test |
| 97 | * @return test |
| 98 | */ |
| 99 | @GET |
| 100 | @Path("{nodeid}") |
| 101 | @Produces(MediaType.APPLICATION_JSON) |
| 102 | public Response getNodeid(@PathParam("nodeid") String nodeid) { |
| 103 | log.debug("GET NODEID {}", nodeid); |
| 104 | |
| 105 | Object node = get(XranStore.class).getByNodeId(nodeid); |
| 106 | |
| 107 | ObjectNode rootNode = mapper().createObjectNode(); |
| 108 | |
| 109 | if (node != null) { |
| 110 | try { |
| 111 | JsonNode jsonNode = mapper().readTree(node.toString()); |
| 112 | rootNode.put("node", jsonNode); |
| 113 | } catch (IOException e) { |
| 114 | log.error(ExceptionUtils.getFullStackTrace(e)); |
| 115 | e.printStackTrace(); |
| 116 | } |
| 117 | } else { |
| 118 | rootNode.put("error", "not found"); |
| 119 | } |
| 120 | |
| 121 | return ok(rootNode.toString()).build(); |
| 122 | } |
| 123 | |
| 124 | } |