blob: 961ed1940b5c30d1c267efd04e5636c97c8c0e2a [file] [log] [blame]
slowr13fa5b02017-08-08 16:32:31 -07001/*
slowr577f3222017-08-28 10:49:08 -07002 * Copyright 2016-present Open Networking Foundation
slowr13fa5b02017-08-08 16:32:31 -07003 *
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;
slowr13fa5b02017-08-08 16:32:31 -070019import org.apache.commons.lang.StringUtils;
20import org.apache.commons.lang.exception.ExceptionUtils;
21import org.onosproject.rest.AbstractWebResource;
22import org.onosproject.xran.XranStore;
slowr13fa5b02017-08-08 16:32:31 -070023import org.slf4j.Logger;
24import org.slf4j.LoggerFactory;
25
26import javax.ws.rs.DefaultValue;
27import javax.ws.rs.GET;
28import javax.ws.rs.Path;
slowr577f3222017-08-28 10:49:08 -070029import javax.ws.rs.QueryParam;
slowr13fa5b02017-08-08 16:32:31 -070030import javax.ws.rs.PathParam;
31import javax.ws.rs.Produces;
slowr13fa5b02017-08-08 16:32:31 -070032import javax.ws.rs.core.MediaType;
33import javax.ws.rs.core.Response;
slowr13fa5b02017-08-08 16:32:31 -070034import java.util.List;
35
36/**
37 * Node web resource.
38 */
39@Path("nodes")
40public class NodeWebResource extends AbstractWebResource {
41
42 private static final Logger log =
43 LoggerFactory.getLogger(NodeWebResource.class);
44
slowr60d4d102017-08-16 18:33:58 -070045 public NodeWebResource() {
46
47 }
48
slowr13fa5b02017-08-08 16:32:31 -070049 /**
slowr577f3222017-08-28 10:49:08 -070050 * List all the nodes in the R-NIB.
slowr13fa5b02017-08-08 16:32:31 -070051 *
slowr577f3222017-08-28 10:49:08 -070052 * @param type Type of node (cell/ue)
53 * @return Response
slowr13fa5b02017-08-08 16:32:31 -070054 */
55 @GET
56 @Produces(MediaType.APPLICATION_JSON)
57 public Response getNodes(@DefaultValue("") @QueryParam("type") String type) {
slowr60d4d102017-08-16 18:33:58 -070058 JsonNode jsonNode;
slowr13fa5b02017-08-08 16:32:31 -070059 try {
slowr60d4d102017-08-16 18:33:58 -070060 List<Object> nodes;
slowr577f3222017-08-28 10:49:08 -070061 // List cell type of nodes or UE type of nodes.
slowr13fa5b02017-08-08 16:32:31 -070062 if (StringUtils.isBlank(type)) {
slowr60d4d102017-08-16 18:33:58 -070063 nodes = get(XranStore.class).getNodes();
slowr13fa5b02017-08-08 16:32:31 -070064 } else if (type.equals("cell")) {
slowr577f3222017-08-28 10:49:08 -070065 nodes = get(XranStore.class).getcellnodes();
slowr13fa5b02017-08-08 16:32:31 -070066 } else if (type.equals("ue")) {
slowr577f3222017-08-28 10:49:08 -070067 nodes = get(XranStore.class).getuenodes();
slowr60d4d102017-08-16 18:33:58 -070068 } else {
69 return ResponseHelper.getResponse(
70 mapper(),
slowr577f3222017-08-28 10:49:08 -070071 ResponseHelper.StatusCode.NOT_FOUND,
slowr60d4d102017-08-16 18:33:58 -070072 "Not Found",
73 "Type of node was not found"
74 );
slowr13fa5b02017-08-08 16:32:31 -070075 }
slowr60d4d102017-08-16 18:33:58 -070076
77 if (nodes.size() == 0) {
78 return ResponseHelper.getResponse(
79 mapper(),
slowr577f3222017-08-28 10:49:08 -070080 ResponseHelper.StatusCode.NOT_FOUND,
slowr60d4d102017-08-16 18:33:58 -070081 "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);
slowr13fa5b02017-08-08 16:32:31 -070090 e.printStackTrace();
slowr60d4d102017-08-16 18:33:58 -070091
92 return ResponseHelper.getResponse(
93 mapper(),
slowr577f3222017-08-28 10:49:08 -070094 ResponseHelper.StatusCode.INTERNAL_SERVER_ERROR,
slowr60d4d102017-08-16 18:33:58 -070095 "Exception",
96 fullStackTrace
97 );
slowr13fa5b02017-08-08 16:32:31 -070098 }
99
slowr60d4d102017-08-16 18:33:58 -0700100 return ResponseHelper.getResponse(
101 mapper(),
slowr577f3222017-08-28 10:49:08 -0700102 ResponseHelper.StatusCode.OK,
slowr60d4d102017-08-16 18:33:58 -0700103 jsonNode
104 );
slowr13fa5b02017-08-08 16:32:31 -0700105 }
106
107 /**
slowr577f3222017-08-28 10:49:08 -0700108 * List the node with a specific node id.
slowr13fa5b02017-08-08 16:32:31 -0700109 *
slowr577f3222017-08-28 10:49:08 -0700110 * @param nodeid ID of the node
111 * @return Response
slowr13fa5b02017-08-08 16:32:31 -0700112 */
113 @GET
114 @Path("{nodeid}")
115 @Produces(MediaType.APPLICATION_JSON)
116 public Response getNodeid(@PathParam("nodeid") String nodeid) {
slowr577f3222017-08-28 10:49:08 -0700117 Object node = get(XranStore.class).getbynodeid(nodeid);
slowr13fa5b02017-08-08 16:32:31 -0700118
slowr13fa5b02017-08-08 16:32:31 -0700119 if (node != null) {
120 try {
slowr60d4d102017-08-16 18:33:58 -0700121 JsonNode jsonNode = mapper().valueToTree(node);
122
123 return ResponseHelper.getResponse(
124 mapper(),
slowr577f3222017-08-28 10:49:08 -0700125 ResponseHelper.StatusCode.OK,
slowr60d4d102017-08-16 18:33:58 -0700126 jsonNode
127 );
128 } catch (Exception e) {
129 String fullStackTrace = ExceptionUtils.getFullStackTrace(e);
130 log.error(fullStackTrace);
slowr13fa5b02017-08-08 16:32:31 -0700131 e.printStackTrace();
slowr60d4d102017-08-16 18:33:58 -0700132
133 return ResponseHelper.getResponse(
134 mapper(),
slowr577f3222017-08-28 10:49:08 -0700135 ResponseHelper.StatusCode.INTERNAL_SERVER_ERROR,
slowr60d4d102017-08-16 18:33:58 -0700136 "Exception",
137 fullStackTrace
138 );
slowr13fa5b02017-08-08 16:32:31 -0700139 }
slowr13fa5b02017-08-08 16:32:31 -0700140 }
141
slowr60d4d102017-08-16 18:33:58 -0700142 return ResponseHelper.getResponse(
143 mapper(),
slowr577f3222017-08-28 10:49:08 -0700144 ResponseHelper.StatusCode.NOT_FOUND,
slowr60d4d102017-08-16 18:33:58 -0700145 "Not Found",
146 "Node " + nodeid + " was not found"
147 );
slowr13fa5b02017-08-08 16:32:31 -0700148 }
149
150}