Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 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 Hart | 9bb32ab | 2015-05-05 18:17:31 -0700 | [diff] [blame] | 16 | package org.onosproject.rest.resources; |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 17 | |
Thomas Vachuska | f8cac48 | 2015-04-08 19:40:12 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import com.google.common.collect.Lists; |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 21 | import org.onosproject.net.ConnectPoint; |
| 22 | import org.onosproject.net.DeviceId; |
| 23 | import org.onosproject.net.Link; |
| 24 | import org.onosproject.net.PortNumber; |
Kavitha Alagesan | 98315a2 | 2016-08-17 08:13:32 +0530 | [diff] [blame] | 25 | import org.onosproject.net.device.DeviceService; |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 26 | import org.onosproject.net.topology.ClusterId; |
| 27 | import org.onosproject.net.topology.Topology; |
| 28 | import org.onosproject.net.topology.TopologyCluster; |
| 29 | import org.onosproject.net.topology.TopologyService; |
Jonathan Hart | 9bb32ab | 2015-05-05 18:17:31 -0700 | [diff] [blame] | 30 | import org.onosproject.rest.AbstractWebResource; |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 31 | |
Thomas Vachuska | f8cac48 | 2015-04-08 19:40:12 -0700 | [diff] [blame] | 32 | import javax.ws.rs.GET; |
| 33 | import javax.ws.rs.Path; |
| 34 | import javax.ws.rs.PathParam; |
| 35 | import javax.ws.rs.Produces; |
| 36 | import javax.ws.rs.core.MediaType; |
| 37 | import javax.ws.rs.core.Response; |
| 38 | import java.util.List; |
Thomas Vachuska | f8cac48 | 2015-04-08 19:40:12 -0700 | [diff] [blame] | 39 | import static org.onlab.util.Tools.nullIsNotFound; |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 40 | |
| 41 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 42 | * Query network topology graph and its components. |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 43 | */ |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 44 | @Path("topology") |
| 45 | public class TopologyWebResource extends AbstractWebResource { |
| 46 | |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 47 | private static final String CLUSTER_NOT_FOUND = "Cluster is not found"; |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 48 | |
| 49 | /** |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 50 | * Gets overview of current topology. |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 51 | * |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 52 | * @return 200 OK with topology overview |
Andrea Campanella | 10c4adc | 2015-12-03 15:27:54 -0800 | [diff] [blame] | 53 | * @onos.rsModel Topology |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 54 | */ |
| 55 | @GET |
| 56 | @Produces(MediaType.APPLICATION_JSON) |
| 57 | public Response getTopology() { |
| 58 | Topology topology = get(TopologyService.class).currentTopology(); |
Thomas Vachuska | f8cac48 | 2015-04-08 19:40:12 -0700 | [diff] [blame] | 59 | ObjectNode root = codec(Topology.class).encode(topology, this); |
Ray Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 60 | return ok(root).build(); |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /** |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 64 | * Gets overview of topology SCCs. |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 65 | * |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 66 | * @return 200 OK with topology clusters overview |
Andrea Campanella | 10c4adc | 2015-12-03 15:27:54 -0800 | [diff] [blame] | 67 | * @onos.rsModel TopologyClusters |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 68 | */ |
| 69 | @GET |
| 70 | @Produces(MediaType.APPLICATION_JSON) |
| 71 | @Path("clusters") |
| 72 | public Response getClusters() { |
Thomas Vachuska | f8cac48 | 2015-04-08 19:40:12 -0700 | [diff] [blame] | 73 | 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 Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 77 | return ok(root).build(); |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | /** |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 81 | * Gets details of a specific SCC. |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 82 | * |
Ray Milkey | f25d135 | 2015-01-23 15:14:08 -0800 | [diff] [blame] | 83 | * @param clusterId id of the cluster to query |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 84 | * @return 200 OK with topology cluster details |
Andrea Campanella | 10c4adc | 2015-12-03 15:27:54 -0800 | [diff] [blame] | 85 | * @onos.rsModel TopologyCluster |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 86 | */ |
| 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 Vachuska | f8cac48 | 2015-04-08 19:40:12 -0700 | [diff] [blame] | 92 | TopologyCluster cluster = getTopologyCluster(clusterId, topology); |
| 93 | ObjectNode root = codec(TopologyCluster.class).encode(cluster, this); |
Ray Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 94 | return ok(root).build(); |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 95 | } |
| 96 | |
Thomas Vachuska | f8cac48 | 2015-04-08 19:40:12 -0700 | [diff] [blame] | 97 | 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 Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 104 | /** |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 105 | * Gets devices in a specific SCC. |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 106 | * |
Ray Milkey | f25d135 | 2015-01-23 15:14:08 -0800 | [diff] [blame] | 107 | * @param clusterId id of the cluster to query |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 108 | * @return 200 OK with topology cluster devices |
Andrea Campanella | 10c4adc | 2015-12-03 15:27:54 -0800 | [diff] [blame] | 109 | * @onos.rsModel TopologyClustersDevices |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 110 | */ |
| 111 | @GET |
| 112 | @Produces(MediaType.APPLICATION_JSON) |
| 113 | @Path("clusters/{id}/devices") |
| 114 | public Response getClusterDevices(@PathParam("id") int clusterId) { |
Thomas Vachuska | f8cac48 | 2015-04-08 19:40:12 -0700 | [diff] [blame] | 115 | TopologyService service = get(TopologyService.class); |
| 116 | Topology topology = service.currentTopology(); |
| 117 | TopologyCluster cluster = getTopologyCluster(clusterId, topology); |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 118 | |
| 119 | List<DeviceId> deviceIds = |
Thomas Vachuska | f8cac48 | 2015-04-08 19:40:12 -0700 | [diff] [blame] | 120 | Lists.newArrayList(service.getClusterDevices(topology, cluster)); |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 121 | |
| 122 | ObjectNode root = mapper().createObjectNode(); |
| 123 | ArrayNode devicesNode = root.putArray("devices"); |
Thomas Vachuska | f8cac48 | 2015-04-08 19:40:12 -0700 | [diff] [blame] | 124 | deviceIds.forEach(id -> devicesNode.add(id.toString())); |
Ray Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 125 | return ok(root).build(); |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /** |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 129 | * Gets links in specific SCC. |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 130 | * |
Ray Milkey | f25d135 | 2015-01-23 15:14:08 -0800 | [diff] [blame] | 131 | * @param clusterId id of the cluster to query |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 132 | * @return 200 OK with topology cluster links |
Andrea Campanella | 10c4adc | 2015-12-03 15:27:54 -0800 | [diff] [blame] | 133 | * @onos.rsModel LinksGet |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 134 | */ |
| 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 Vachuska | f8cac48 | 2015-04-08 19:40:12 -0700 | [diff] [blame] | 140 | TopologyCluster cluster = getTopologyCluster(clusterId, topology); |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 141 | |
| 142 | List<Link> links = |
| 143 | Lists.newArrayList(get(TopologyService.class) |
andrea | 1ce2bc8 | 2015-11-18 16:58:10 -0800 | [diff] [blame] | 144 | .getClusterLinks(topology, cluster)); |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 145 | |
Ray Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 146 | return ok(encodeArray(Link.class, "links", links)).build(); |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 147 | } |
| 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 | /** |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 178 | * Tests if a connect point is in broadcast set. |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 179 | * |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 180 | * @param connectPointString deviceid:portnumber |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 181 | * @return 200 OK with JSON representation of true if the connect point is |
| 182 | * broadcast, false otherwise |
Andrea Campanella | 10c4adc | 2015-12-03 15:27:54 -0800 | [diff] [blame] | 183 | * @onos.rsModel TopologyBroadcast |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 184 | */ |
| 185 | @GET |
| 186 | @Produces(MediaType.APPLICATION_JSON) |
| 187 | @Path("broadcast/{connectPoint}") |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 188 | public Response getConnectPointBroadcast(@PathParam("connectPoint") String connectPointString) { |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 189 | Topology topology = get(TopologyService.class).currentTopology(); |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 190 | DeviceId deviceId = DeviceId.deviceId(getDeviceId(connectPointString)); |
Kavitha Alagesan | 98315a2 | 2016-08-17 08:13:32 +0530 | [diff] [blame] | 191 | nullIsNotFound(get(DeviceService.class).getDevice(deviceId), "Device not found " + connectPointString); |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 192 | PortNumber portNumber = PortNumber.portNumber(getPortNumber(connectPointString)); |
Kavitha Alagesan | 98315a2 | 2016-08-17 08:13:32 +0530 | [diff] [blame] | 193 | nullIsNotFound(get(DeviceService.class).getPort(deviceId, portNumber), "Port not found " + connectPointString); |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 194 | ConnectPoint connectPoint = new ConnectPoint(deviceId, portNumber); |
| 195 | boolean isBroadcast = get(TopologyService.class).isBroadcastPoint(topology, connectPoint); |
| 196 | |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 197 | return ok(mapper().createObjectNode() |
andrea | 1ce2bc8 | 2015-11-18 16:58:10 -0800 | [diff] [blame] | 198 | .put("broadcast", isBroadcast)) |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 199 | .build(); |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | /** |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 203 | * Tests if a connect point is infrastructure or edge. |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 204 | * |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 205 | * @param connectPointString deviceid:portnumber |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 206 | * @return 200 OK with JSON representation of true if the connect point is broadcast, |
andrea | 1ce2bc8 | 2015-11-18 16:58:10 -0800 | [diff] [blame] | 207 | * false otherwise |
Andrea Campanella | 10c4adc | 2015-12-03 15:27:54 -0800 | [diff] [blame] | 208 | * @onos.rsModel TopologyInfrastructure |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 209 | */ |
| 210 | @GET |
| 211 | @Produces(MediaType.APPLICATION_JSON) |
| 212 | @Path("infrastructure/{connectPoint}") |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 213 | public Response getConnectPointInfrastructure(@PathParam("connectPoint") String connectPointString) { |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 214 | Topology topology = get(TopologyService.class).currentTopology(); |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 215 | DeviceId deviceId = DeviceId.deviceId(getDeviceId(connectPointString)); |
Kavitha Alagesan | 98315a2 | 2016-08-17 08:13:32 +0530 | [diff] [blame] | 216 | nullIsNotFound(get(DeviceService.class).getDevice(deviceId), "Device not found " + connectPointString); |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 217 | PortNumber portNumber = PortNumber.portNumber(getPortNumber(connectPointString)); |
Kavitha Alagesan | 98315a2 | 2016-08-17 08:13:32 +0530 | [diff] [blame] | 218 | nullIsNotFound(get(DeviceService.class).getPort(deviceId, portNumber), "Port not found " + connectPointString); |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 219 | ConnectPoint connectPoint = new ConnectPoint(deviceId, portNumber); |
| 220 | boolean isInfrastructure = get(TopologyService.class).isInfrastructure(topology, connectPoint); |
| 221 | |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 222 | return ok(mapper().createObjectNode() |
andrea | 1ce2bc8 | 2015-11-18 16:58:10 -0800 | [diff] [blame] | 223 | .put("infrastructure", isInfrastructure)) |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 224 | .build(); |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 225 | } |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 226 | } |