blob: f6ae8253cc361a0d82abb7aa0d3158918127536c [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 */
Jonathan Hart9bb32ab2015-05-05 18:17:31 -070016package org.onosproject.rest.resources;
Ray Milkey82e50312015-01-22 17:01:42 -080017
Thomas Vachuskaf8cac482015-04-08 19:40:12 -070018import com.fasterxml.jackson.databind.node.ArrayNode;
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import com.google.common.collect.Lists;
Ray Milkey82e50312015-01-22 17:01:42 -080021import org.onosproject.net.ConnectPoint;
22import org.onosproject.net.DeviceId;
23import org.onosproject.net.Link;
24import org.onosproject.net.PortNumber;
25import org.onosproject.net.topology.ClusterId;
26import org.onosproject.net.topology.Topology;
27import org.onosproject.net.topology.TopologyCluster;
28import org.onosproject.net.topology.TopologyService;
Jonathan Hart9bb32ab2015-05-05 18:17:31 -070029import org.onosproject.rest.AbstractWebResource;
Ray Milkey82e50312015-01-22 17:01:42 -080030
Thomas Vachuskaf8cac482015-04-08 19:40:12 -070031import javax.ws.rs.GET;
32import javax.ws.rs.Path;
33import javax.ws.rs.PathParam;
34import javax.ws.rs.Produces;
35import javax.ws.rs.core.MediaType;
36import javax.ws.rs.core.Response;
37import java.util.List;
38
39import static org.onlab.util.Tools.nullIsNotFound;
Ray Milkey82e50312015-01-22 17:01:42 -080040
41/**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070042 * Query network topology graph and its components.
Ray Milkey82e50312015-01-22 17:01:42 -080043 */
Ray Milkey82e50312015-01-22 17:01:42 -080044@Path("topology")
45public class TopologyWebResource extends AbstractWebResource {
46
47 public static final String CLUSTER_NOT_FOUND = "Cluster is not found";
48
49 /**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070050 * Get overview of current topology.
Ray Milkey82e50312015-01-22 17:01:42 -080051 *
52 * @return topology overview
53 */
54 @GET
55 @Produces(MediaType.APPLICATION_JSON)
56 public Response getTopology() {
57 Topology topology = get(TopologyService.class).currentTopology();
Thomas Vachuskaf8cac482015-04-08 19:40:12 -070058 ObjectNode root = codec(Topology.class).encode(topology, this);
Ray Milkey3f025692015-01-26 11:15:41 -080059 return ok(root).build();
Ray Milkey82e50312015-01-22 17:01:42 -080060 }
61
62 /**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070063 * Get overview of topology SCCs.
Ray Milkey82e50312015-01-22 17:01:42 -080064 *
65 * @return topology clusters overview
66 */
67 @GET
68 @Produces(MediaType.APPLICATION_JSON)
69 @Path("clusters")
70 public Response getClusters() {
Thomas Vachuskaf8cac482015-04-08 19:40:12 -070071 TopologyService service = get(TopologyService.class);
72 Topology topology = service.currentTopology();
73 Iterable<TopologyCluster> clusters = service.getClusters(topology);
74 ObjectNode root = encodeArray(TopologyCluster.class, "clusters", clusters);
Ray Milkey3f025692015-01-26 11:15:41 -080075 return ok(root).build();
Ray Milkey82e50312015-01-22 17:01:42 -080076 }
77
78 /**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070079 * Get details of a specific SCC.
Ray Milkey82e50312015-01-22 17:01:42 -080080 *
Ray Milkeyf25d1352015-01-23 15:14:08 -080081 * @param clusterId id of the cluster to query
Ray Milkey82e50312015-01-22 17:01:42 -080082 * @return topology cluster details
83 */
84 @GET
85 @Produces(MediaType.APPLICATION_JSON)
86 @Path("clusters/{id}")
87 public Response getCluster(@PathParam("id") int clusterId) {
88 Topology topology = get(TopologyService.class).currentTopology();
Thomas Vachuskaf8cac482015-04-08 19:40:12 -070089 TopologyCluster cluster = getTopologyCluster(clusterId, topology);
90 ObjectNode root = codec(TopologyCluster.class).encode(cluster, this);
Ray Milkey3f025692015-01-26 11:15:41 -080091 return ok(root).build();
Ray Milkey82e50312015-01-22 17:01:42 -080092 }
93
Thomas Vachuskaf8cac482015-04-08 19:40:12 -070094 private TopologyCluster getTopologyCluster(int clusterId, Topology topology) {
95 return nullIsNotFound(
96 get(TopologyService.class)
97 .getCluster(topology, ClusterId.clusterId(clusterId)),
98 CLUSTER_NOT_FOUND);
99 }
100
Ray Milkey82e50312015-01-22 17:01:42 -0800101 /**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700102 * Get devices in a specific SCC.
Ray Milkey82e50312015-01-22 17:01:42 -0800103 *
Ray Milkeyf25d1352015-01-23 15:14:08 -0800104 * @param clusterId id of the cluster to query
Ray Milkey82e50312015-01-22 17:01:42 -0800105 * @return topology cluster devices
106 */
107 @GET
108 @Produces(MediaType.APPLICATION_JSON)
109 @Path("clusters/{id}/devices")
110 public Response getClusterDevices(@PathParam("id") int clusterId) {
Thomas Vachuskaf8cac482015-04-08 19:40:12 -0700111 TopologyService service = get(TopologyService.class);
112 Topology topology = service.currentTopology();
113 TopologyCluster cluster = getTopologyCluster(clusterId, topology);
Ray Milkey82e50312015-01-22 17:01:42 -0800114
115 List<DeviceId> deviceIds =
Thomas Vachuskaf8cac482015-04-08 19:40:12 -0700116 Lists.newArrayList(service.getClusterDevices(topology, cluster));
Ray Milkey82e50312015-01-22 17:01:42 -0800117
118 ObjectNode root = mapper().createObjectNode();
119 ArrayNode devicesNode = root.putArray("devices");
Thomas Vachuskaf8cac482015-04-08 19:40:12 -0700120 deviceIds.forEach(id -> devicesNode.add(id.toString()));
Ray Milkey3f025692015-01-26 11:15:41 -0800121 return ok(root).build();
Ray Milkey82e50312015-01-22 17:01:42 -0800122 }
123
124 /**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700125 * Get links in specific SCC.
Ray Milkey82e50312015-01-22 17:01:42 -0800126 *
Ray Milkeyf25d1352015-01-23 15:14:08 -0800127 * @param clusterId id of the cluster to query
Ray Milkey82e50312015-01-22 17:01:42 -0800128 * @return topology cluster links
129 */
130 @GET
131 @Produces(MediaType.APPLICATION_JSON)
132 @Path("clusters/{id}/links")
133 public Response getClusterLinks(@PathParam("id") int clusterId) {
134 Topology topology = get(TopologyService.class).currentTopology();
Thomas Vachuskaf8cac482015-04-08 19:40:12 -0700135 TopologyCluster cluster = getTopologyCluster(clusterId, topology);
Ray Milkey82e50312015-01-22 17:01:42 -0800136
137 List<Link> links =
138 Lists.newArrayList(get(TopologyService.class)
139 .getClusterLinks(topology, cluster));
140
Ray Milkey3f025692015-01-26 11:15:41 -0800141 return ok(encodeArray(Link.class, "links", links)).build();
Ray Milkey82e50312015-01-22 17:01:42 -0800142 }
143
144 /**
145 * Extracts the port number portion of the ConnectPoint.
146 *
147 * @param deviceString string representing the device/port
148 * @return port number as a string, empty string if the port is not found
149 */
150 private static String getPortNumber(String deviceString) {
151 int separator = deviceString.lastIndexOf(':');
152 if (separator <= 0) {
153 return "";
154 }
155 return deviceString.substring(separator + 1, deviceString.length());
156 }
157
158 /**
159 * Extracts the device ID portion of the ConnectPoint.
160 *
161 * @param deviceString string representing the device/port
162 * @return device ID string
163 */
164 private static String getDeviceId(String deviceString) {
165 int separator = deviceString.lastIndexOf(':');
166 if (separator <= 0) {
167 return "";
168 }
169 return deviceString.substring(0, separator);
170 }
171
172 /**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700173 * Test if a connect point is in broadcast set.
Ray Milkey82e50312015-01-22 17:01:42 -0800174 *
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700175 * @param connectPointString deviceid:portnumber
Ray Milkey82e50312015-01-22 17:01:42 -0800176 * @return JSON representation of true if the connect point is broadcast,
177 * false otherwise
178 */
179 @GET
180 @Produces(MediaType.APPLICATION_JSON)
181 @Path("broadcast/{connectPoint}")
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700182 public Response getConnectPointBroadcast(@PathParam("connectPoint") String connectPointString) {
Ray Milkey82e50312015-01-22 17:01:42 -0800183 Topology topology = get(TopologyService.class).currentTopology();
184
185 DeviceId deviceId = DeviceId.deviceId(getDeviceId(connectPointString));
186 PortNumber portNumber = PortNumber.portNumber(getPortNumber(connectPointString));
187 ConnectPoint connectPoint = new ConnectPoint(deviceId, portNumber);
188 boolean isBroadcast = get(TopologyService.class).isBroadcastPoint(topology, connectPoint);
189
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800190 return ok(mapper()
191 .createObjectNode()
Ray Milkey3f025692015-01-26 11:15:41 -0800192 .put("broadcast", isBroadcast))
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800193 .build();
Ray Milkey82e50312015-01-22 17:01:42 -0800194 }
195
196 /**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700197 * Test if a connect point is infrastructure or edge.
Ray Milkey82e50312015-01-22 17:01:42 -0800198 *
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700199 * @param connectPointString deviceid:portnumber
Ray Milkey82e50312015-01-22 17:01:42 -0800200 * @return JSON representation of true if the connect point is broadcast,
201 * false otherwise
202 */
203 @GET
204 @Produces(MediaType.APPLICATION_JSON)
205 @Path("infrastructure/{connectPoint}")
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700206 public Response getConnectPointInfrastructure(@PathParam("connectPoint") String connectPointString) {
Ray Milkey82e50312015-01-22 17:01:42 -0800207 Topology topology = get(TopologyService.class).currentTopology();
208
209 DeviceId deviceId = DeviceId.deviceId(getDeviceId(connectPointString));
210 PortNumber portNumber = PortNumber.portNumber(getPortNumber(connectPointString));
211 ConnectPoint connectPoint = new ConnectPoint(deviceId, portNumber);
212 boolean isInfrastructure = get(TopologyService.class).isInfrastructure(topology, connectPoint);
213
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800214 return ok(mapper()
215 .createObjectNode()
Ray Milkey3f025692015-01-26 11:15:41 -0800216 .put("infrastructure", isInfrastructure))
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800217 .build();
Ray Milkey82e50312015-01-22 17:01:42 -0800218 }
219
220}