blob: a10232cba8e67285e59a8026dac97f7c238d4e94 [file] [log] [blame]
Ray Milkey82e50312015-01-22 17:01:42 -08001/*
2 * Copyright 2015 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.rest;
17
18import java.util.List;
19
20import javax.ws.rs.GET;
21import javax.ws.rs.Path;
22import javax.ws.rs.PathParam;
23import javax.ws.rs.Produces;
24import javax.ws.rs.core.MediaType;
25import javax.ws.rs.core.Response;
26
27import org.onosproject.net.ConnectPoint;
28import org.onosproject.net.DeviceId;
29import org.onosproject.net.Link;
30import org.onosproject.net.PortNumber;
31import org.onosproject.net.topology.ClusterId;
32import org.onosproject.net.topology.Topology;
33import org.onosproject.net.topology.TopologyCluster;
34import org.onosproject.net.topology.TopologyService;
35
36import com.fasterxml.jackson.databind.node.ArrayNode;
37import com.fasterxml.jackson.databind.node.ObjectNode;
38import com.google.common.collect.Lists;
39
40/**
41 * REST resource for interacting with the inventory of clusters.
42 */
43
44@Path("topology")
45public class TopologyWebResource extends AbstractWebResource {
46
47 public static final String CLUSTER_NOT_FOUND = "Cluster is not found";
48
49 /**
50 * Gets the topology overview for a REST GET operation.
51 *
52 * @return topology overview
53 */
54 @GET
55 @Produces(MediaType.APPLICATION_JSON)
56 public Response getTopology() {
57 Topology topology = get(TopologyService.class).currentTopology();
58 ObjectNode root =
59 codec(Topology.class).encode(topology, this);
Ray Milkey3f025692015-01-26 11:15:41 -080060 return ok(root).build();
Ray Milkey82e50312015-01-22 17:01:42 -080061 }
62
63 /**
64 * Gets the topology clusters overview for a REST GET operation.
65 *
66 * @return topology clusters overview
67 */
68 @GET
69 @Produces(MediaType.APPLICATION_JSON)
70 @Path("clusters")
71 public Response getClusters() {
72 Topology topology = get(TopologyService.class).currentTopology();
73 Iterable<TopologyCluster> clusters =
74 get(TopologyService.class).getClusters(topology);
75 ObjectNode root =
76 encodeArray(TopologyCluster.class, "clusters", clusters);
Ray Milkey3f025692015-01-26 11:15:41 -080077 return ok(root).build();
Ray Milkey82e50312015-01-22 17:01:42 -080078 }
79
80 /**
81 * Gets details for a topology cluster for a REST GET operation.
82 *
Ray Milkeyf25d1352015-01-23 15:14:08 -080083 * @param clusterId id of the cluster to query
Ray Milkey82e50312015-01-22 17:01:42 -080084 * @return topology cluster details
85 */
86 @GET
87 @Produces(MediaType.APPLICATION_JSON)
88 @Path("clusters/{id}")
89 public Response getCluster(@PathParam("id") int clusterId) {
90 Topology topology = get(TopologyService.class).currentTopology();
91 TopologyCluster cluster =
92 nullIsNotFound(
93 get(TopologyService.class)
94 .getCluster(topology,
95 ClusterId.clusterId(clusterId)),
96 CLUSTER_NOT_FOUND);
97 ObjectNode root =
98 codec(TopologyCluster.class).encode(cluster, this);
Ray Milkey3f025692015-01-26 11:15:41 -080099 return ok(root).build();
Ray Milkey82e50312015-01-22 17:01:42 -0800100 }
101
102 /**
103 * Gets devices for a topology cluster for a REST GET operation.
104 *
Ray Milkeyf25d1352015-01-23 15:14:08 -0800105 * @param clusterId id of the cluster to query
Ray Milkey82e50312015-01-22 17:01:42 -0800106 * @return topology cluster devices
107 */
108 @GET
109 @Produces(MediaType.APPLICATION_JSON)
110 @Path("clusters/{id}/devices")
111 public Response getClusterDevices(@PathParam("id") int clusterId) {
112 Topology topology = get(TopologyService.class).currentTopology();
113 TopologyCluster cluster =
114 nullIsNotFound(
115 get(TopologyService.class)
116 .getCluster(topology,
117 ClusterId.clusterId(clusterId)),
118 CLUSTER_NOT_FOUND);
119
120 List<DeviceId> deviceIds =
121 Lists.newArrayList(get(TopologyService.class)
122 .getClusterDevices(topology, cluster));
123
124 ObjectNode root = mapper().createObjectNode();
125 ArrayNode devicesNode = root.putArray("devices");
126
127 for (DeviceId deviceId : deviceIds) {
128 devicesNode.add(deviceId.toString());
129 }
Ray Milkey3f025692015-01-26 11:15:41 -0800130 return ok(root).build();
Ray Milkey82e50312015-01-22 17:01:42 -0800131 }
132
133 /**
134 * Gets links for a topology cluster for a REST GET operation.
135 *
Ray Milkeyf25d1352015-01-23 15:14:08 -0800136 * @param clusterId id of the cluster to query
Ray Milkey82e50312015-01-22 17:01:42 -0800137 * @return topology cluster links
138 */
139 @GET
140 @Produces(MediaType.APPLICATION_JSON)
141 @Path("clusters/{id}/links")
142 public Response getClusterLinks(@PathParam("id") int clusterId) {
143 Topology topology = get(TopologyService.class).currentTopology();
144 TopologyCluster cluster =
145 nullIsNotFound(get(TopologyService.class).getCluster(topology,
146 ClusterId.clusterId(clusterId)),
147 CLUSTER_NOT_FOUND);
148
149 List<Link> links =
150 Lists.newArrayList(get(TopologyService.class)
151 .getClusterLinks(topology, cluster));
152
Ray Milkey3f025692015-01-26 11:15:41 -0800153 return ok(encodeArray(Link.class, "links", links)).build();
Ray Milkey82e50312015-01-22 17:01:42 -0800154 }
155
156 /**
157 * Extracts the port number portion of the ConnectPoint.
158 *
159 * @param deviceString string representing the device/port
160 * @return port number as a string, empty string if the port is not found
161 */
162 private static String getPortNumber(String deviceString) {
163 int separator = deviceString.lastIndexOf(':');
164 if (separator <= 0) {
165 return "";
166 }
167 return deviceString.substring(separator + 1, deviceString.length());
168 }
169
170 /**
171 * Extracts the device ID portion of the ConnectPoint.
172 *
173 * @param deviceString string representing the device/port
174 * @return device ID string
175 */
176 private static String getDeviceId(String deviceString) {
177 int separator = deviceString.lastIndexOf(':');
178 if (separator <= 0) {
179 return "";
180 }
181 return deviceString.substring(0, separator);
182 }
183
184 /**
185 * Gets the broadcast flag of a connect point for a REST GET operation.
186 *
Ray Milkeyf25d1352015-01-23 15:14:08 -0800187 * @param connectPointString string representation of the connect point to query.
188 * Format is deviceid:portnumber
Ray Milkey82e50312015-01-22 17:01:42 -0800189 * @return JSON representation of true if the connect point is broadcast,
190 * false otherwise
191 */
192 @GET
193 @Produces(MediaType.APPLICATION_JSON)
194 @Path("broadcast/{connectPoint}")
195 public Response getConnectPointBroadcast(
196 @PathParam("connectPoint") String connectPointString) {
197 Topology topology = get(TopologyService.class).currentTopology();
198
199 DeviceId deviceId = DeviceId.deviceId(getDeviceId(connectPointString));
200 PortNumber portNumber = PortNumber.portNumber(getPortNumber(connectPointString));
201 ConnectPoint connectPoint = new ConnectPoint(deviceId, portNumber);
202 boolean isBroadcast = get(TopologyService.class).isBroadcastPoint(topology, connectPoint);
203
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800204 return ok(mapper()
205 .createObjectNode()
Ray Milkey3f025692015-01-26 11:15:41 -0800206 .put("broadcast", isBroadcast))
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800207 .build();
Ray Milkey82e50312015-01-22 17:01:42 -0800208 }
209
210 /**
211 * Gets the infrastructure flag of a connect point for a REST GET operation.
212 *
Ray Milkeyf25d1352015-01-23 15:14:08 -0800213 * @param connectPointString string representation of the connect point to query.
214 * Format is deviceid:portnumber
Ray Milkey82e50312015-01-22 17:01:42 -0800215 * @return JSON representation of true if the connect point is broadcast,
216 * false otherwise
217 */
218 @GET
219 @Produces(MediaType.APPLICATION_JSON)
220 @Path("infrastructure/{connectPoint}")
221 public Response getConnectPointInfrastructure(
222 @PathParam("connectPoint") String connectPointString) {
223 Topology topology = get(TopologyService.class).currentTopology();
224
225 DeviceId deviceId = DeviceId.deviceId(getDeviceId(connectPointString));
226 PortNumber portNumber = PortNumber.portNumber(getPortNumber(connectPointString));
227 ConnectPoint connectPoint = new ConnectPoint(deviceId, portNumber);
228 boolean isInfrastructure = get(TopologyService.class).isInfrastructure(topology, connectPoint);
229
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800230 return ok(mapper()
231 .createObjectNode()
Ray Milkey3f025692015-01-26 11:15:41 -0800232 .put("infrastructure", isInfrastructure))
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800233 .build();
Ray Milkey82e50312015-01-22 17:01:42 -0800234 }
235
236}