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 | /** |
| 42 | * REST resource for interacting with the inventory of clusters. |
| 43 | */ |
| 44 | |
| 45 | @Path("topology") |
| 46 | public class TopologyWebResource extends AbstractWebResource { |
| 47 | |
| 48 | public static final String CLUSTER_NOT_FOUND = "Cluster is not found"; |
| 49 | |
| 50 | /** |
| 51 | * Gets the topology overview for a REST GET operation. |
| 52 | * |
| 53 | * @return topology overview |
| 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 | /** |
| 64 | * Gets the topology clusters overview for a REST GET operation. |
| 65 | * |
| 66 | * @return topology clusters overview |
| 67 | */ |
| 68 | @GET |
| 69 | @Produces(MediaType.APPLICATION_JSON) |
| 70 | @Path("clusters") |
| 71 | public Response getClusters() { |
Thomas Vachuska | f8cac48 | 2015-04-08 19:40:12 -0700 | [diff] [blame] | 72 | TopologyService service = get(TopologyService.class); |
| 73 | Topology topology = service.currentTopology(); |
| 74 | Iterable<TopologyCluster> clusters = service.getClusters(topology); |
| 75 | ObjectNode root = encodeArray(TopologyCluster.class, "clusters", clusters); |
Ray Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 76 | return ok(root).build(); |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Gets details for a topology cluster for a REST GET operation. |
| 81 | * |
Ray Milkey | f25d135 | 2015-01-23 15:14:08 -0800 | [diff] [blame] | 82 | * @param clusterId id of the cluster to query |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 83 | * @return topology cluster details |
| 84 | */ |
| 85 | @GET |
| 86 | @Produces(MediaType.APPLICATION_JSON) |
| 87 | @Path("clusters/{id}") |
| 88 | public Response getCluster(@PathParam("id") int clusterId) { |
| 89 | Topology topology = get(TopologyService.class).currentTopology(); |
Thomas Vachuska | f8cac48 | 2015-04-08 19:40:12 -0700 | [diff] [blame] | 90 | TopologyCluster cluster = getTopologyCluster(clusterId, topology); |
| 91 | ObjectNode root = codec(TopologyCluster.class).encode(cluster, this); |
Ray Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 92 | return ok(root).build(); |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Thomas Vachuska | f8cac48 | 2015-04-08 19:40:12 -0700 | [diff] [blame] | 95 | private TopologyCluster getTopologyCluster(int clusterId, Topology topology) { |
| 96 | return nullIsNotFound( |
| 97 | get(TopologyService.class) |
| 98 | .getCluster(topology, ClusterId.clusterId(clusterId)), |
| 99 | CLUSTER_NOT_FOUND); |
| 100 | } |
| 101 | |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 102 | /** |
| 103 | * Gets devices for a topology cluster for a REST GET operation. |
| 104 | * |
Ray Milkey | f25d135 | 2015-01-23 15:14:08 -0800 | [diff] [blame] | 105 | * @param clusterId id of the cluster to query |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 106 | * @return topology cluster devices |
| 107 | */ |
| 108 | @GET |
| 109 | @Produces(MediaType.APPLICATION_JSON) |
| 110 | @Path("clusters/{id}/devices") |
| 111 | public Response getClusterDevices(@PathParam("id") int clusterId) { |
Thomas Vachuska | f8cac48 | 2015-04-08 19:40:12 -0700 | [diff] [blame] | 112 | TopologyService service = get(TopologyService.class); |
| 113 | Topology topology = service.currentTopology(); |
| 114 | TopologyCluster cluster = getTopologyCluster(clusterId, topology); |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 115 | |
| 116 | List<DeviceId> deviceIds = |
Thomas Vachuska | f8cac48 | 2015-04-08 19:40:12 -0700 | [diff] [blame] | 117 | Lists.newArrayList(service.getClusterDevices(topology, cluster)); |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 118 | |
| 119 | ObjectNode root = mapper().createObjectNode(); |
| 120 | ArrayNode devicesNode = root.putArray("devices"); |
Thomas Vachuska | f8cac48 | 2015-04-08 19:40:12 -0700 | [diff] [blame] | 121 | deviceIds.forEach(id -> devicesNode.add(id.toString())); |
Ray Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 122 | return ok(root).build(); |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Gets links for a topology cluster for a REST GET operation. |
| 127 | * |
Ray Milkey | f25d135 | 2015-01-23 15:14:08 -0800 | [diff] [blame] | 128 | * @param clusterId id of the cluster to query |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 129 | * @return topology cluster links |
| 130 | */ |
| 131 | @GET |
| 132 | @Produces(MediaType.APPLICATION_JSON) |
| 133 | @Path("clusters/{id}/links") |
| 134 | public Response getClusterLinks(@PathParam("id") int clusterId) { |
| 135 | Topology topology = get(TopologyService.class).currentTopology(); |
Thomas Vachuska | f8cac48 | 2015-04-08 19:40:12 -0700 | [diff] [blame] | 136 | TopologyCluster cluster = getTopologyCluster(clusterId, topology); |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 137 | |
| 138 | List<Link> links = |
| 139 | Lists.newArrayList(get(TopologyService.class) |
| 140 | .getClusterLinks(topology, cluster)); |
| 141 | |
Ray Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 142 | return ok(encodeArray(Link.class, "links", links)).build(); |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Extracts the port number portion of the ConnectPoint. |
| 147 | * |
| 148 | * @param deviceString string representing the device/port |
| 149 | * @return port number as a string, empty string if the port is not found |
| 150 | */ |
| 151 | private static String getPortNumber(String deviceString) { |
| 152 | int separator = deviceString.lastIndexOf(':'); |
| 153 | if (separator <= 0) { |
| 154 | return ""; |
| 155 | } |
| 156 | return deviceString.substring(separator + 1, deviceString.length()); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Extracts the device ID portion of the ConnectPoint. |
| 161 | * |
| 162 | * @param deviceString string representing the device/port |
| 163 | * @return device ID string |
| 164 | */ |
| 165 | private static String getDeviceId(String deviceString) { |
| 166 | int separator = deviceString.lastIndexOf(':'); |
| 167 | if (separator <= 0) { |
| 168 | return ""; |
| 169 | } |
| 170 | return deviceString.substring(0, separator); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Gets the broadcast flag of a connect point for a REST GET operation. |
| 175 | * |
Ray Milkey | f25d135 | 2015-01-23 15:14:08 -0800 | [diff] [blame] | 176 | * @param connectPointString string representation of the connect point to query. |
| 177 | * Format is deviceid:portnumber |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 178 | * @return JSON representation of true if the connect point is broadcast, |
| 179 | * false otherwise |
| 180 | */ |
| 181 | @GET |
| 182 | @Produces(MediaType.APPLICATION_JSON) |
| 183 | @Path("broadcast/{connectPoint}") |
| 184 | public Response getConnectPointBroadcast( |
| 185 | @PathParam("connectPoint") String connectPointString) { |
| 186 | Topology topology = get(TopologyService.class).currentTopology(); |
| 187 | |
| 188 | DeviceId deviceId = DeviceId.deviceId(getDeviceId(connectPointString)); |
| 189 | PortNumber portNumber = PortNumber.portNumber(getPortNumber(connectPointString)); |
| 190 | ConnectPoint connectPoint = new ConnectPoint(deviceId, portNumber); |
| 191 | boolean isBroadcast = get(TopologyService.class).isBroadcastPoint(topology, connectPoint); |
| 192 | |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 193 | return ok(mapper() |
| 194 | .createObjectNode() |
Ray Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 195 | .put("broadcast", isBroadcast)) |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 196 | .build(); |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Gets the infrastructure flag of a connect point for a REST GET operation. |
| 201 | * |
Ray Milkey | f25d135 | 2015-01-23 15:14:08 -0800 | [diff] [blame] | 202 | * @param connectPointString string representation of the connect point to query. |
| 203 | * Format is deviceid:portnumber |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 204 | * @return JSON representation of true if the connect point is broadcast, |
| 205 | * false otherwise |
| 206 | */ |
| 207 | @GET |
| 208 | @Produces(MediaType.APPLICATION_JSON) |
| 209 | @Path("infrastructure/{connectPoint}") |
| 210 | public Response getConnectPointInfrastructure( |
| 211 | @PathParam("connectPoint") String connectPointString) { |
| 212 | Topology topology = get(TopologyService.class).currentTopology(); |
| 213 | |
| 214 | DeviceId deviceId = DeviceId.deviceId(getDeviceId(connectPointString)); |
| 215 | PortNumber portNumber = PortNumber.portNumber(getPortNumber(connectPointString)); |
| 216 | ConnectPoint connectPoint = new ConnectPoint(deviceId, portNumber); |
| 217 | boolean isInfrastructure = get(TopologyService.class).isInfrastructure(topology, connectPoint); |
| 218 | |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 219 | return ok(mapper() |
| 220 | .createObjectNode() |
Ray Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 221 | .put("infrastructure", isInfrastructure)) |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 222 | .build(); |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | } |