Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 1 | /* |
| 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 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; |
| 25 | import org.onosproject.net.topology.ClusterId; |
| 26 | import org.onosproject.net.topology.Topology; |
| 27 | import org.onosproject.net.topology.TopologyCluster; |
| 28 | import org.onosproject.net.topology.TopologyService; |
Jonathan Hart | 9bb32ab | 2015-05-05 18:17:31 -0700 | [diff] [blame] | 29 | import org.onosproject.rest.AbstractWebResource; |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 30 | |
Thomas Vachuska | f8cac48 | 2015-04-08 19:40:12 -0700 | [diff] [blame] | 31 | import javax.ws.rs.GET; |
| 32 | import javax.ws.rs.Path; |
| 33 | import javax.ws.rs.PathParam; |
| 34 | import javax.ws.rs.Produces; |
| 35 | import javax.ws.rs.core.MediaType; |
| 36 | import javax.ws.rs.core.Response; |
| 37 | import java.util.List; |
| 38 | |
| 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 | |
| 47 | public static final String CLUSTER_NOT_FOUND = "Cluster is not found"; |
| 48 | |
| 49 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame^] | 50 | * Get overview of current topology. |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 51 | * |
| 52 | * @return topology overview |
| 53 | */ |
| 54 | @GET |
| 55 | @Produces(MediaType.APPLICATION_JSON) |
| 56 | public Response getTopology() { |
| 57 | Topology topology = get(TopologyService.class).currentTopology(); |
Thomas Vachuska | f8cac48 | 2015-04-08 19:40:12 -0700 | [diff] [blame] | 58 | ObjectNode root = codec(Topology.class).encode(topology, this); |
Ray Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 59 | return ok(root).build(); |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame^] | 63 | * Get overview of topology SCCs. |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 64 | * |
| 65 | * @return topology clusters overview |
| 66 | */ |
| 67 | @GET |
| 68 | @Produces(MediaType.APPLICATION_JSON) |
| 69 | @Path("clusters") |
| 70 | public Response getClusters() { |
Thomas Vachuska | f8cac48 | 2015-04-08 19:40:12 -0700 | [diff] [blame] | 71 | 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 Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 75 | return ok(root).build(); |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame^] | 79 | * Get details of a specific SCC. |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 80 | * |
Ray Milkey | f25d135 | 2015-01-23 15:14:08 -0800 | [diff] [blame] | 81 | * @param clusterId id of the cluster to query |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 82 | * @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 Vachuska | f8cac48 | 2015-04-08 19:40:12 -0700 | [diff] [blame] | 89 | TopologyCluster cluster = getTopologyCluster(clusterId, topology); |
| 90 | ObjectNode root = codec(TopologyCluster.class).encode(cluster, this); |
Ray Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 91 | return ok(root).build(); |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Thomas Vachuska | f8cac48 | 2015-04-08 19:40:12 -0700 | [diff] [blame] | 94 | 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 Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 101 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame^] | 102 | * Get devices in a specific SCC. |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 103 | * |
Ray Milkey | f25d135 | 2015-01-23 15:14:08 -0800 | [diff] [blame] | 104 | * @param clusterId id of the cluster to query |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 105 | * @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 Vachuska | f8cac48 | 2015-04-08 19:40:12 -0700 | [diff] [blame] | 111 | TopologyService service = get(TopologyService.class); |
| 112 | Topology topology = service.currentTopology(); |
| 113 | TopologyCluster cluster = getTopologyCluster(clusterId, topology); |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 114 | |
| 115 | List<DeviceId> deviceIds = |
Thomas Vachuska | f8cac48 | 2015-04-08 19:40:12 -0700 | [diff] [blame] | 116 | Lists.newArrayList(service.getClusterDevices(topology, cluster)); |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 117 | |
| 118 | ObjectNode root = mapper().createObjectNode(); |
| 119 | ArrayNode devicesNode = root.putArray("devices"); |
Thomas Vachuska | f8cac48 | 2015-04-08 19:40:12 -0700 | [diff] [blame] | 120 | deviceIds.forEach(id -> devicesNode.add(id.toString())); |
Ray Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 121 | return ok(root).build(); |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame^] | 125 | * Get links in specific SCC. |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 126 | * |
Ray Milkey | f25d135 | 2015-01-23 15:14:08 -0800 | [diff] [blame] | 127 | * @param clusterId id of the cluster to query |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 128 | * @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 Vachuska | f8cac48 | 2015-04-08 19:40:12 -0700 | [diff] [blame] | 135 | TopologyCluster cluster = getTopologyCluster(clusterId, topology); |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 136 | |
| 137 | List<Link> links = |
| 138 | Lists.newArrayList(get(TopologyService.class) |
| 139 | .getClusterLinks(topology, cluster)); |
| 140 | |
Ray Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 141 | return ok(encodeArray(Link.class, "links", links)).build(); |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 142 | } |
| 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 Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame^] | 173 | * Test if a connect point is in broadcast set. |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 174 | * |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame^] | 175 | * @param connectPointString deviceid:portnumber |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 176 | * @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 Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame^] | 182 | public Response getConnectPointBroadcast(@PathParam("connectPoint") String connectPointString) { |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 183 | 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 Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 190 | return ok(mapper() |
| 191 | .createObjectNode() |
Ray Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 192 | .put("broadcast", isBroadcast)) |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 193 | .build(); |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame^] | 197 | * Test if a connect point is infrastructure or edge. |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 198 | * |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame^] | 199 | * @param connectPointString deviceid:portnumber |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 200 | * @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 Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame^] | 206 | public Response getConnectPointInfrastructure(@PathParam("connectPoint") String connectPointString) { |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 207 | 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 Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 214 | return ok(mapper() |
| 215 | .createObjectNode() |
Ray Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 216 | .put("infrastructure", isInfrastructure)) |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 217 | .build(); |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | } |