blob: 683f7afefeb587e96f5adc8b9b974adcab1074b3 [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
Andrea Campanella10c4adc2015-12-03 15:27:54 -080053 * @onos.rsModel Topology
Ray Milkey82e50312015-01-22 17:01:42 -080054 */
55 @GET
56 @Produces(MediaType.APPLICATION_JSON)
57 public Response getTopology() {
58 Topology topology = get(TopologyService.class).currentTopology();
Thomas Vachuskaf8cac482015-04-08 19:40:12 -070059 ObjectNode root = 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 /**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070064 * Get overview of topology SCCs.
Ray Milkey82e50312015-01-22 17:01:42 -080065 *
66 * @return topology clusters overview
Andrea Campanella10c4adc2015-12-03 15:27:54 -080067 * @onos.rsModel TopologyClusters
Ray Milkey82e50312015-01-22 17:01:42 -080068 */
69 @GET
70 @Produces(MediaType.APPLICATION_JSON)
71 @Path("clusters")
72 public Response getClusters() {
Thomas Vachuskaf8cac482015-04-08 19:40:12 -070073 TopologyService service = get(TopologyService.class);
74 Topology topology = service.currentTopology();
75 Iterable<TopologyCluster> clusters = service.getClusters(topology);
76 ObjectNode root = 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 /**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070081 * Get details of a specific SCC.
Ray Milkey82e50312015-01-22 17:01:42 -080082 *
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
Andrea Campanella10c4adc2015-12-03 15:27:54 -080085 * @onos.rsModel TopologyCluster
Ray Milkey82e50312015-01-22 17:01:42 -080086 */
87 @GET
88 @Produces(MediaType.APPLICATION_JSON)
89 @Path("clusters/{id}")
90 public Response getCluster(@PathParam("id") int clusterId) {
91 Topology topology = get(TopologyService.class).currentTopology();
Thomas Vachuskaf8cac482015-04-08 19:40:12 -070092 TopologyCluster cluster = getTopologyCluster(clusterId, topology);
93 ObjectNode root = codec(TopologyCluster.class).encode(cluster, this);
Ray Milkey3f025692015-01-26 11:15:41 -080094 return ok(root).build();
Ray Milkey82e50312015-01-22 17:01:42 -080095 }
96
Thomas Vachuskaf8cac482015-04-08 19:40:12 -070097 private TopologyCluster getTopologyCluster(int clusterId, Topology topology) {
98 return nullIsNotFound(
99 get(TopologyService.class)
100 .getCluster(topology, ClusterId.clusterId(clusterId)),
101 CLUSTER_NOT_FOUND);
102 }
103
Ray Milkey82e50312015-01-22 17:01:42 -0800104 /**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700105 * Get devices in a specific SCC.
Ray Milkey82e50312015-01-22 17:01:42 -0800106 *
Ray Milkeyf25d1352015-01-23 15:14:08 -0800107 * @param clusterId id of the cluster to query
Ray Milkey82e50312015-01-22 17:01:42 -0800108 * @return topology cluster devices
Andrea Campanella10c4adc2015-12-03 15:27:54 -0800109 * @onos.rsModel TopologyClustersDevices
Ray Milkey82e50312015-01-22 17:01:42 -0800110 */
111 @GET
112 @Produces(MediaType.APPLICATION_JSON)
113 @Path("clusters/{id}/devices")
114 public Response getClusterDevices(@PathParam("id") int clusterId) {
Thomas Vachuskaf8cac482015-04-08 19:40:12 -0700115 TopologyService service = get(TopologyService.class);
116 Topology topology = service.currentTopology();
117 TopologyCluster cluster = getTopologyCluster(clusterId, topology);
Ray Milkey82e50312015-01-22 17:01:42 -0800118
119 List<DeviceId> deviceIds =
Thomas Vachuskaf8cac482015-04-08 19:40:12 -0700120 Lists.newArrayList(service.getClusterDevices(topology, cluster));
Ray Milkey82e50312015-01-22 17:01:42 -0800121
122 ObjectNode root = mapper().createObjectNode();
123 ArrayNode devicesNode = root.putArray("devices");
Thomas Vachuskaf8cac482015-04-08 19:40:12 -0700124 deviceIds.forEach(id -> devicesNode.add(id.toString()));
Ray Milkey3f025692015-01-26 11:15:41 -0800125 return ok(root).build();
Ray Milkey82e50312015-01-22 17:01:42 -0800126 }
127
128 /**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700129 * Get links in specific SCC.
Ray Milkey82e50312015-01-22 17:01:42 -0800130 *
Ray Milkeyf25d1352015-01-23 15:14:08 -0800131 * @param clusterId id of the cluster to query
Ray Milkey82e50312015-01-22 17:01:42 -0800132 * @return topology cluster links
Andrea Campanella10c4adc2015-12-03 15:27:54 -0800133 * @onos.rsModel LinksGet
Ray Milkey82e50312015-01-22 17:01:42 -0800134 */
135 @GET
136 @Produces(MediaType.APPLICATION_JSON)
137 @Path("clusters/{id}/links")
138 public Response getClusterLinks(@PathParam("id") int clusterId) {
139 Topology topology = get(TopologyService.class).currentTopology();
Thomas Vachuskaf8cac482015-04-08 19:40:12 -0700140 TopologyCluster cluster = getTopologyCluster(clusterId, topology);
Ray Milkey82e50312015-01-22 17:01:42 -0800141
142 List<Link> links =
143 Lists.newArrayList(get(TopologyService.class)
andrea1ce2bc82015-11-18 16:58:10 -0800144 .getClusterLinks(topology, cluster));
Ray Milkey82e50312015-01-22 17:01:42 -0800145
Ray Milkey3f025692015-01-26 11:15:41 -0800146 return ok(encodeArray(Link.class, "links", links)).build();
Ray Milkey82e50312015-01-22 17:01:42 -0800147 }
148
149 /**
150 * Extracts the port number portion of the ConnectPoint.
151 *
152 * @param deviceString string representing the device/port
153 * @return port number as a string, empty string if the port is not found
154 */
155 private static String getPortNumber(String deviceString) {
156 int separator = deviceString.lastIndexOf(':');
157 if (separator <= 0) {
158 return "";
159 }
160 return deviceString.substring(separator + 1, deviceString.length());
161 }
162
163 /**
164 * Extracts the device ID portion of the ConnectPoint.
165 *
166 * @param deviceString string representing the device/port
167 * @return device ID string
168 */
169 private static String getDeviceId(String deviceString) {
170 int separator = deviceString.lastIndexOf(':');
171 if (separator <= 0) {
172 return "";
173 }
174 return deviceString.substring(0, separator);
175 }
176
177 /**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700178 * Test if a connect point is in broadcast set.
Ray Milkey82e50312015-01-22 17:01:42 -0800179 *
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700180 * @param connectPointString deviceid:portnumber
Ray Milkey82e50312015-01-22 17:01:42 -0800181 * @return JSON representation of true if the connect point is broadcast,
andrea1ce2bc82015-11-18 16:58:10 -0800182 * false otherwise
Andrea Campanella10c4adc2015-12-03 15:27:54 -0800183 * @onos.rsModel TopologyBroadcast
Ray Milkey82e50312015-01-22 17:01:42 -0800184 */
185 @GET
186 @Produces(MediaType.APPLICATION_JSON)
187 @Path("broadcast/{connectPoint}")
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700188 public Response getConnectPointBroadcast(@PathParam("connectPoint") String connectPointString) {
Ray Milkey82e50312015-01-22 17:01:42 -0800189 Topology topology = get(TopologyService.class).currentTopology();
190
191 DeviceId deviceId = DeviceId.deviceId(getDeviceId(connectPointString));
192 PortNumber portNumber = PortNumber.portNumber(getPortNumber(connectPointString));
193 ConnectPoint connectPoint = new ConnectPoint(deviceId, portNumber);
194 boolean isBroadcast = get(TopologyService.class).isBroadcastPoint(topology, connectPoint);
195
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800196 return ok(mapper()
andrea1ce2bc82015-11-18 16:58:10 -0800197 .createObjectNode()
198 .put("broadcast", isBroadcast))
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800199 .build();
Ray Milkey82e50312015-01-22 17:01:42 -0800200 }
201
202 /**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700203 * Test if a connect point is infrastructure or edge.
Ray Milkey82e50312015-01-22 17:01:42 -0800204 *
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700205 * @param connectPointString deviceid:portnumber
Ray Milkey82e50312015-01-22 17:01:42 -0800206 * @return JSON representation of true if the connect point is broadcast,
andrea1ce2bc82015-11-18 16:58:10 -0800207 * false otherwise
Andrea Campanella10c4adc2015-12-03 15:27:54 -0800208 * @onos.rsModel TopologyInfrastructure
Ray Milkey82e50312015-01-22 17:01:42 -0800209 */
210 @GET
211 @Produces(MediaType.APPLICATION_JSON)
212 @Path("infrastructure/{connectPoint}")
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700213 public Response getConnectPointInfrastructure(@PathParam("connectPoint") String connectPointString) {
Ray Milkey82e50312015-01-22 17:01:42 -0800214 Topology topology = get(TopologyService.class).currentTopology();
215
216 DeviceId deviceId = DeviceId.deviceId(getDeviceId(connectPointString));
217 PortNumber portNumber = PortNumber.portNumber(getPortNumber(connectPointString));
218 ConnectPoint connectPoint = new ConnectPoint(deviceId, portNumber);
219 boolean isInfrastructure = get(TopologyService.class).isInfrastructure(topology, connectPoint);
220
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800221 return ok(mapper()
andrea1ce2bc82015-11-18 16:58:10 -0800222 .createObjectNode()
223 .put("infrastructure", isInfrastructure))
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800224 .build();
Ray Milkey82e50312015-01-22 17:01:42 -0800225 }
226
227}