blob: 7b541773e08c8baec8f226b76991526f6833c0dd [file] [log] [blame]
slowr13fa5b02017-08-08 16:32:31 -07001/*
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 */
16package org.onosproject.xran.rest;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import org.apache.commons.lang.StringUtils;
21import org.apache.commons.lang.exception.ExceptionUtils;
22import org.onosproject.rest.AbstractWebResource;
23import org.onosproject.xran.XranStore;
24import org.onosproject.xran.entities.RnibCell;
25import org.onosproject.xran.entities.RnibUe;
26import org.slf4j.Logger;
27import org.slf4j.LoggerFactory;
28
29import javax.ws.rs.DefaultValue;
30import javax.ws.rs.GET;
31import javax.ws.rs.Path;
32import javax.ws.rs.PathParam;
33import javax.ws.rs.Produces;
34import javax.ws.rs.QueryParam;
35import javax.ws.rs.core.MediaType;
36import javax.ws.rs.core.Response;
37import java.io.IOException;
38import java.util.List;
39
40/**
41 * Node web resource.
42 */
43@Path("nodes")
44public 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) {
slowr13fa5b02017-08-08 16:32:31 -070058 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();
slowr8ddc2b12017-08-14 14:13:38 -070086 return Response.serverError()
87 .entity(ExceptionUtils.getFullStackTrace(e))
88 .build();
slowr13fa5b02017-08-08 16:32:31 -070089 }
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) {
slowr13fa5b02017-08-08 16:32:31 -0700104 Object node = get(XranStore.class).getByNodeId(nodeid);
105
slowr13fa5b02017-08-08 16:32:31 -0700106 if (node != null) {
107 try {
slowr8ddc2b12017-08-14 14:13:38 -0700108 ObjectNode rootNode = mapper().createObjectNode();
slowr13fa5b02017-08-08 16:32:31 -0700109 JsonNode jsonNode = mapper().readTree(node.toString());
110 rootNode.put("node", jsonNode);
slowr8ddc2b12017-08-14 14:13:38 -0700111 return ok(rootNode.toString()).build();
slowr13fa5b02017-08-08 16:32:31 -0700112 } catch (IOException e) {
113 log.error(ExceptionUtils.getFullStackTrace(e));
114 e.printStackTrace();
slowr8ddc2b12017-08-14 14:13:38 -0700115 return Response.serverError()
116 .entity(ExceptionUtils.getFullStackTrace(e))
117 .build();
slowr13fa5b02017-08-08 16:32:31 -0700118 }
slowr13fa5b02017-08-08 16:32:31 -0700119 }
120
slowr8ddc2b12017-08-14 14:13:38 -0700121 return Response.serverError().entity("node not found").build();
slowr13fa5b02017-08-08 16:32:31 -0700122 }
123
124}