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 | |
| 18 | import java.util.List; |
| 19 | |
| 20 | import javax.ws.rs.GET; |
| 21 | import javax.ws.rs.Path; |
| 22 | import javax.ws.rs.PathParam; |
| 23 | import javax.ws.rs.Produces; |
| 24 | import javax.ws.rs.core.MediaType; |
| 25 | import javax.ws.rs.core.Response; |
| 26 | |
| 27 | import org.onosproject.net.ConnectPoint; |
| 28 | import org.onosproject.net.DeviceId; |
| 29 | import org.onosproject.net.Link; |
| 30 | import org.onosproject.net.PortNumber; |
| 31 | import org.onosproject.net.topology.ClusterId; |
| 32 | import org.onosproject.net.topology.Topology; |
| 33 | import org.onosproject.net.topology.TopologyCluster; |
| 34 | import org.onosproject.net.topology.TopologyService; |
| 35 | |
| 36 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 37 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 38 | import com.google.common.collect.Lists; |
| 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(); |
| 58 | ObjectNode root = |
| 59 | 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() { |
| 72 | Topology topology = get(TopologyService.class).currentTopology(); |
| 73 | Iterable<TopologyCluster> clusters = |
| 74 | get(TopologyService.class).getClusters(topology); |
| 75 | ObjectNode root = |
| 76 | 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 | /** |
| 81 | * Gets details for a topology cluster for a REST GET operation. |
| 82 | * |
Ray Milkey | f25d135 | 2015-01-23 15:14:08 -0800 | [diff] [blame] | 83 | * @param clusterId id of the cluster to query |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 84 | * @return topology cluster details |
| 85 | */ |
| 86 | @GET |
| 87 | @Produces(MediaType.APPLICATION_JSON) |
| 88 | @Path("clusters/{id}") |
| 89 | public Response getCluster(@PathParam("id") int clusterId) { |
| 90 | Topology topology = get(TopologyService.class).currentTopology(); |
| 91 | TopologyCluster cluster = |
| 92 | nullIsNotFound( |
| 93 | get(TopologyService.class) |
| 94 | .getCluster(topology, |
| 95 | ClusterId.clusterId(clusterId)), |
| 96 | CLUSTER_NOT_FOUND); |
| 97 | ObjectNode root = |
| 98 | codec(TopologyCluster.class).encode(cluster, this); |
Ray Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 99 | return ok(root).build(); |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 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) { |
| 112 | Topology topology = get(TopologyService.class).currentTopology(); |
| 113 | TopologyCluster cluster = |
| 114 | nullIsNotFound( |
| 115 | get(TopologyService.class) |
| 116 | .getCluster(topology, |
| 117 | ClusterId.clusterId(clusterId)), |
| 118 | CLUSTER_NOT_FOUND); |
| 119 | |
| 120 | List<DeviceId> deviceIds = |
| 121 | Lists.newArrayList(get(TopologyService.class) |
| 122 | .getClusterDevices(topology, cluster)); |
| 123 | |
| 124 | ObjectNode root = mapper().createObjectNode(); |
| 125 | ArrayNode devicesNode = root.putArray("devices"); |
| 126 | |
| 127 | for (DeviceId deviceId : deviceIds) { |
| 128 | devicesNode.add(deviceId.toString()); |
| 129 | } |
Ray Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 130 | return ok(root).build(); |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Gets links for a topology cluster for a REST GET operation. |
| 135 | * |
Ray Milkey | f25d135 | 2015-01-23 15:14:08 -0800 | [diff] [blame] | 136 | * @param clusterId id of the cluster to query |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 137 | * @return topology cluster links |
| 138 | */ |
| 139 | @GET |
| 140 | @Produces(MediaType.APPLICATION_JSON) |
| 141 | @Path("clusters/{id}/links") |
| 142 | public Response getClusterLinks(@PathParam("id") int clusterId) { |
| 143 | Topology topology = get(TopologyService.class).currentTopology(); |
| 144 | TopologyCluster cluster = |
| 145 | nullIsNotFound(get(TopologyService.class).getCluster(topology, |
| 146 | ClusterId.clusterId(clusterId)), |
| 147 | CLUSTER_NOT_FOUND); |
| 148 | |
| 149 | List<Link> links = |
| 150 | Lists.newArrayList(get(TopologyService.class) |
| 151 | .getClusterLinks(topology, cluster)); |
| 152 | |
Ray Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 153 | return ok(encodeArray(Link.class, "links", links)).build(); |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Extracts the port number portion of the ConnectPoint. |
| 158 | * |
| 159 | * @param deviceString string representing the device/port |
| 160 | * @return port number as a string, empty string if the port is not found |
| 161 | */ |
| 162 | private static String getPortNumber(String deviceString) { |
| 163 | int separator = deviceString.lastIndexOf(':'); |
| 164 | if (separator <= 0) { |
| 165 | return ""; |
| 166 | } |
| 167 | return deviceString.substring(separator + 1, deviceString.length()); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Extracts the device ID portion of the ConnectPoint. |
| 172 | * |
| 173 | * @param deviceString string representing the device/port |
| 174 | * @return device ID string |
| 175 | */ |
| 176 | private static String getDeviceId(String deviceString) { |
| 177 | int separator = deviceString.lastIndexOf(':'); |
| 178 | if (separator <= 0) { |
| 179 | return ""; |
| 180 | } |
| 181 | return deviceString.substring(0, separator); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Gets the broadcast flag of a connect point for a REST GET operation. |
| 186 | * |
Ray Milkey | f25d135 | 2015-01-23 15:14:08 -0800 | [diff] [blame] | 187 | * @param connectPointString string representation of the connect point to query. |
| 188 | * Format is deviceid:portnumber |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 189 | * @return JSON representation of true if the connect point is broadcast, |
| 190 | * false otherwise |
| 191 | */ |
| 192 | @GET |
| 193 | @Produces(MediaType.APPLICATION_JSON) |
| 194 | @Path("broadcast/{connectPoint}") |
| 195 | public Response getConnectPointBroadcast( |
| 196 | @PathParam("connectPoint") String connectPointString) { |
| 197 | Topology topology = get(TopologyService.class).currentTopology(); |
| 198 | |
| 199 | DeviceId deviceId = DeviceId.deviceId(getDeviceId(connectPointString)); |
| 200 | PortNumber portNumber = PortNumber.portNumber(getPortNumber(connectPointString)); |
| 201 | ConnectPoint connectPoint = new ConnectPoint(deviceId, portNumber); |
| 202 | boolean isBroadcast = get(TopologyService.class).isBroadcastPoint(topology, connectPoint); |
| 203 | |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 204 | return ok(mapper() |
| 205 | .createObjectNode() |
Ray Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 206 | .put("broadcast", isBroadcast)) |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 207 | .build(); |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Gets the infrastructure flag of a connect point for a REST GET operation. |
| 212 | * |
Ray Milkey | f25d135 | 2015-01-23 15:14:08 -0800 | [diff] [blame] | 213 | * @param connectPointString string representation of the connect point to query. |
| 214 | * Format is deviceid:portnumber |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 215 | * @return JSON representation of true if the connect point is broadcast, |
| 216 | * false otherwise |
| 217 | */ |
| 218 | @GET |
| 219 | @Produces(MediaType.APPLICATION_JSON) |
| 220 | @Path("infrastructure/{connectPoint}") |
| 221 | public Response getConnectPointInfrastructure( |
| 222 | @PathParam("connectPoint") String connectPointString) { |
| 223 | Topology topology = get(TopologyService.class).currentTopology(); |
| 224 | |
| 225 | DeviceId deviceId = DeviceId.deviceId(getDeviceId(connectPointString)); |
| 226 | PortNumber portNumber = PortNumber.portNumber(getPortNumber(connectPointString)); |
| 227 | ConnectPoint connectPoint = new ConnectPoint(deviceId, portNumber); |
| 228 | boolean isInfrastructure = get(TopologyService.class).isInfrastructure(topology, connectPoint); |
| 229 | |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 230 | return ok(mapper() |
| 231 | .createObjectNode() |
Ray Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 232 | .put("infrastructure", isInfrastructure)) |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 233 | .build(); |
Ray Milkey | 82e5031 | 2015-01-22 17:01:42 -0800 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | } |