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 | */ |
| 16 | package org.onosproject.rest; |
| 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; |
| 29 | |
Thomas Vachuska | f8cac48 | 2015-04-08 19:40:12 -0700 | [diff] [blame] | 30 | import javax.ws.rs.GET; |
| 31 | import javax.ws.rs.Path; |
| 32 | import javax.ws.rs.PathParam; |
| 33 | import javax.ws.rs.Produces; |
| 34 | import javax.ws.rs.core.MediaType; |
| 35 | import javax.ws.rs.core.Response; |
| 36 | import java.util.List; |
| 37 | |
| 38 | import static org.onlab.util.Tools.nullIsNotFound; |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * REST resource for interacting with the inventory of clusters. |
| 42 | */ |
| 43 | |
| 44 | @Path("topology") |
| 45 | public 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(); |
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 | /** |
| 63 | * Gets the topology clusters overview for a REST GET operation. |
| 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 | /** |
| 79 | * Gets details for a topology cluster for a REST GET operation. |
| 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 | /** |
| 102 | * Gets devices for a topology cluster for a REST GET operation. |
| 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 | /** |
| 125 | * Gets links for a topology cluster for a REST GET operation. |
| 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 | /** |
| 173 | * Gets the broadcast flag of a connect point for a REST GET operation. |
| 174 | * |
Ray Milkey | f25d135 | 2015-01-23 15:14:08 -0800 | [diff] [blame] | 175 | * @param connectPointString string representation of the connect point to query. |
| 176 | * Format is deviceid:portnumber |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 177 | * @return JSON representation of true if the connect point is broadcast, |
| 178 | * false otherwise |
| 179 | */ |
| 180 | @GET |
| 181 | @Produces(MediaType.APPLICATION_JSON) |
| 182 | @Path("broadcast/{connectPoint}") |
| 183 | public Response getConnectPointBroadcast( |
| 184 | @PathParam("connectPoint") String connectPointString) { |
| 185 | Topology topology = get(TopologyService.class).currentTopology(); |
| 186 | |
| 187 | DeviceId deviceId = DeviceId.deviceId(getDeviceId(connectPointString)); |
| 188 | PortNumber portNumber = PortNumber.portNumber(getPortNumber(connectPointString)); |
| 189 | ConnectPoint connectPoint = new ConnectPoint(deviceId, portNumber); |
| 190 | boolean isBroadcast = get(TopologyService.class).isBroadcastPoint(topology, connectPoint); |
| 191 | |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 192 | return ok(mapper() |
| 193 | .createObjectNode() |
Ray Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 194 | .put("broadcast", isBroadcast)) |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 195 | .build(); |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Gets the infrastructure flag of a connect point for a REST GET operation. |
| 200 | * |
Ray Milkey | f25d135 | 2015-01-23 15:14:08 -0800 | [diff] [blame] | 201 | * @param connectPointString string representation of the connect point to query. |
| 202 | * Format is deviceid:portnumber |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 203 | * @return JSON representation of true if the connect point is broadcast, |
| 204 | * false otherwise |
| 205 | */ |
| 206 | @GET |
| 207 | @Produces(MediaType.APPLICATION_JSON) |
| 208 | @Path("infrastructure/{connectPoint}") |
| 209 | public Response getConnectPointInfrastructure( |
| 210 | @PathParam("connectPoint") String connectPointString) { |
| 211 | Topology topology = get(TopologyService.class).currentTopology(); |
| 212 | |
| 213 | DeviceId deviceId = DeviceId.deviceId(getDeviceId(connectPointString)); |
| 214 | PortNumber portNumber = PortNumber.portNumber(getPortNumber(connectPointString)); |
| 215 | ConnectPoint connectPoint = new ConnectPoint(deviceId, portNumber); |
| 216 | boolean isInfrastructure = get(TopologyService.class).isInfrastructure(topology, connectPoint); |
| 217 | |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 218 | return ok(mapper() |
| 219 | .createObjectNode() |
Ray Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 220 | .put("infrastructure", isInfrastructure)) |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 221 | .build(); |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | } |