blob: 2b9124ce8843d8ebbc77c2bcb9a9b4662f337fcc [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
slowr60d4d102017-08-16 18:33:58 -070018import com.fasterxml.jackson.annotation.JsonInclude;
slowr13fa5b02017-08-08 16:32:31 -070019import com.fasterxml.jackson.databind.JsonNode;
slowr60d4d102017-08-16 18:33:58 -070020import com.google.common.collect.Lists;
slowr13fa5b02017-08-08 16:32:31 -070021import org.apache.commons.lang.StringUtils;
22import org.apache.commons.lang.exception.ExceptionUtils;
23import org.onosproject.rest.AbstractWebResource;
24import org.onosproject.xran.XranStore;
25import org.onosproject.xran.entities.RnibCell;
26import org.onosproject.xran.entities.RnibUe;
27import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
29
30import javax.ws.rs.DefaultValue;
31import javax.ws.rs.GET;
32import javax.ws.rs.Path;
33import javax.ws.rs.PathParam;
34import javax.ws.rs.Produces;
35import javax.ws.rs.QueryParam;
36import javax.ws.rs.core.MediaType;
37import javax.ws.rs.core.Response;
slowr13fa5b02017-08-08 16:32:31 -070038import 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
slowr60d4d102017-08-16 18:33:58 -070049 public NodeWebResource() {
50
51 }
52
slowr13fa5b02017-08-08 16:32:31 -070053 /**
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) {
slowr60d4d102017-08-16 18:33:58 -070062 JsonNode jsonNode;
slowr13fa5b02017-08-08 16:32:31 -070063 try {
slowr60d4d102017-08-16 18:33:58 -070064 List<Object> nodes;
slowr13fa5b02017-08-08 16:32:31 -070065 if (StringUtils.isBlank(type)) {
slowr60d4d102017-08-16 18:33:58 -070066 nodes = get(XranStore.class).getNodes();
slowr13fa5b02017-08-08 16:32:31 -070067 } else if (type.equals("cell")) {
slowr60d4d102017-08-16 18:33:58 -070068 nodes = get(XranStore.class).getCellNodes();
slowr13fa5b02017-08-08 16:32:31 -070069 } else if (type.equals("ue")) {
slowr60d4d102017-08-16 18:33:58 -070070 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 );
slowr13fa5b02017-08-08 16:32:31 -070078 }
slowr60d4d102017-08-16 18:33:58 -070079
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);
slowr13fa5b02017-08-08 16:32:31 -070093 e.printStackTrace();
slowr60d4d102017-08-16 18:33:58 -070094
95 return ResponseHelper.getResponse(
96 mapper(),
97 ResponseHelper.statusCode.INTERNAL_SERVER_ERROR,
98 "Exception",
99 fullStackTrace
100 );
slowr13fa5b02017-08-08 16:32:31 -0700101 }
102
slowr60d4d102017-08-16 18:33:58 -0700103 return ResponseHelper.getResponse(
104 mapper(),
105 ResponseHelper.statusCode.OK,
106 jsonNode
107 );
slowr13fa5b02017-08-08 16:32:31 -0700108 }
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) {
slowr13fa5b02017-08-08 16:32:31 -0700120 Object node = get(XranStore.class).getByNodeId(nodeid);
121
slowr13fa5b02017-08-08 16:32:31 -0700122 if (node != null) {
123 try {
slowr60d4d102017-08-16 18:33:58 -0700124 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);
slowr13fa5b02017-08-08 16:32:31 -0700134 e.printStackTrace();
slowr60d4d102017-08-16 18:33:58 -0700135
136 return ResponseHelper.getResponse(
137 mapper(),
138 ResponseHelper.statusCode.INTERNAL_SERVER_ERROR,
139 "Exception",
140 fullStackTrace
141 );
slowr13fa5b02017-08-08 16:32:31 -0700142 }
slowr13fa5b02017-08-08 16:32:31 -0700143 }
144
slowr60d4d102017-08-16 18:33:58 -0700145 return ResponseHelper.getResponse(
146 mapper(),
147 ResponseHelper.statusCode.NOT_FOUND,
148 "Not Found",
149 "Node " + nodeid + " was not found"
150 );
slowr13fa5b02017-08-08 16:32:31 -0700151 }
152
153}