tom | 202175a | 2014-09-19 19:00:11 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net.trivial.impl; |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 2 | |
tom | e52ce70 | 2014-09-11 00:12:54 -0700 | [diff] [blame] | 3 | import com.google.common.collect.ImmutableMap; |
| 4 | import com.google.common.collect.ImmutableSet; |
| 5 | import com.google.common.collect.ImmutableSetMultimap; |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 6 | import org.onlab.graph.DijkstraGraphSearch; |
tom | e52ce70 | 2014-09-11 00:12:54 -0700 | [diff] [blame] | 7 | import org.onlab.graph.GraphPathSearch; |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 8 | import org.onlab.graph.TarjanGraphSearch; |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 9 | import org.onlab.onos.net.AbstractModel; |
tom | e52ce70 | 2014-09-11 00:12:54 -0700 | [diff] [blame] | 10 | import org.onlab.onos.net.ConnectPoint; |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 11 | import org.onlab.onos.net.DefaultPath; |
tom | e52ce70 | 2014-09-11 00:12:54 -0700 | [diff] [blame] | 12 | import org.onlab.onos.net.DeviceId; |
| 13 | import org.onlab.onos.net.Link; |
| 14 | import org.onlab.onos.net.Path; |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 15 | import org.onlab.onos.net.provider.ProviderId; |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 16 | import org.onlab.onos.net.topology.ClusterId; |
| 17 | import org.onlab.onos.net.topology.DefaultTopologyCluster; |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 18 | import org.onlab.onos.net.topology.DefaultTopologyVertex; |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 19 | import org.onlab.onos.net.topology.GraphDescription; |
| 20 | import org.onlab.onos.net.topology.LinkWeight; |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 21 | import org.onlab.onos.net.topology.Topology; |
tom | e52ce70 | 2014-09-11 00:12:54 -0700 | [diff] [blame] | 22 | import org.onlab.onos.net.topology.TopologyCluster; |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 23 | import org.onlab.onos.net.topology.TopologyEdge; |
| 24 | import org.onlab.onos.net.topology.TopologyGraph; |
| 25 | import org.onlab.onos.net.topology.TopologyVertex; |
tom | e52ce70 | 2014-09-11 00:12:54 -0700 | [diff] [blame] | 26 | |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 27 | import java.util.ArrayList; |
| 28 | import java.util.List; |
| 29 | import java.util.Map; |
tom | e52ce70 | 2014-09-11 00:12:54 -0700 | [diff] [blame] | 30 | import java.util.Set; |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 31 | |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 32 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 33 | import static com.google.common.collect.ImmutableSetMultimap.Builder; |
| 34 | import static org.onlab.graph.GraphPathSearch.Result; |
| 35 | import static org.onlab.graph.TarjanGraphSearch.SCCResult; |
| 36 | import static org.onlab.onos.net.Link.Type.INDIRECT; |
| 37 | |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 38 | /** |
| 39 | * Default implementation of the topology descriptor. This carries the |
| 40 | * backing topology data. |
| 41 | */ |
| 42 | public class DefaultTopology extends AbstractModel implements Topology { |
| 43 | |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 44 | private static final DijkstraGraphSearch<TopologyVertex, TopologyEdge> DIJKSTRA = |
| 45 | new DijkstraGraphSearch<>(); |
| 46 | private static final TarjanGraphSearch<TopologyVertex, TopologyEdge> TARJAN = |
| 47 | new TarjanGraphSearch<>(); |
| 48 | |
tom | 7e02cda | 2014-09-18 12:05:46 -0700 | [diff] [blame] | 49 | private static final ProviderId PID = new ProviderId("core", "org.onlab.onos.net"); |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 50 | |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 51 | private final long time; |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 52 | private final TopologyGraph graph; |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 53 | |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 54 | private final SCCResult<TopologyVertex, TopologyEdge> clusterResults; |
| 55 | private final ImmutableMap<DeviceId, Result<TopologyVertex, TopologyEdge>> results; |
| 56 | private final ImmutableSetMultimap<PathKey, Path> paths; |
tom | e52ce70 | 2014-09-11 00:12:54 -0700 | [diff] [blame] | 57 | |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 58 | private final ImmutableMap<ClusterId, TopologyCluster> clusters; |
| 59 | private final ImmutableSet<ConnectPoint> infrastructurePoints; |
| 60 | private final ImmutableSetMultimap<ClusterId, ConnectPoint> broadcastSets; |
| 61 | |
| 62 | private ImmutableMap<DeviceId, TopologyCluster> clustersByDevice; |
| 63 | private ImmutableSetMultimap<TopologyCluster, DeviceId> devicesByCluster; |
| 64 | private ImmutableSetMultimap<TopologyCluster, Link> linksByCluster; |
| 65 | |
tom | e52ce70 | 2014-09-11 00:12:54 -0700 | [diff] [blame] | 66 | |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 67 | /** |
| 68 | * Creates a topology descriptor attributed to the specified provider. |
| 69 | * |
tom | e52ce70 | 2014-09-11 00:12:54 -0700 | [diff] [blame] | 70 | * @param providerId identity of the provider |
| 71 | * @param description data describing the new topology |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 72 | */ |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 73 | DefaultTopology(ProviderId providerId, GraphDescription description) { |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 74 | super(providerId); |
tom | e52ce70 | 2014-09-11 00:12:54 -0700 | [diff] [blame] | 75 | this.time = description.timestamp(); |
tom | e52ce70 | 2014-09-11 00:12:54 -0700 | [diff] [blame] | 76 | |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 77 | // Build the graph |
| 78 | this.graph = new DefaultTopologyGraph(description.vertexes(), |
| 79 | description.edges()); |
| 80 | |
| 81 | this.results = searchForShortestPaths(); |
| 82 | this.paths = buildPaths(); |
| 83 | |
| 84 | this.clusterResults = searchForClusters(); |
| 85 | this.clusters = buildTopologyClusters(); |
| 86 | |
| 87 | buildIndexes(); |
| 88 | |
| 89 | this.broadcastSets = buildBroadcastSets(); |
| 90 | this.infrastructurePoints = findInfrastructurePoints(); |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public long time() { |
| 95 | return time; |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public int clusterCount() { |
tom | e52ce70 | 2014-09-11 00:12:54 -0700 | [diff] [blame] | 100 | return clusters.size(); |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public int deviceCount() { |
tom | e52ce70 | 2014-09-11 00:12:54 -0700 | [diff] [blame] | 105 | return graph.getVertexes().size(); |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public int linkCount() { |
tom | e52ce70 | 2014-09-11 00:12:54 -0700 | [diff] [blame] | 110 | return graph.getEdges().size(); |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public int pathCount() { |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 115 | return paths.size(); |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 116 | } |
| 117 | |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 118 | /** |
| 119 | * Returns the backing topology graph. |
| 120 | * |
| 121 | * @return topology graph |
| 122 | */ |
| 123 | TopologyGraph getGraph() { |
tom | e52ce70 | 2014-09-11 00:12:54 -0700 | [diff] [blame] | 124 | return graph; |
| 125 | } |
| 126 | |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 127 | /** |
| 128 | * Returns the set of topology clusters. |
| 129 | * |
| 130 | * @return set of clusters |
| 131 | */ |
| 132 | Set<TopologyCluster> getClusters() { |
| 133 | return ImmutableSet.copyOf(clusters.values()); |
| 134 | } |
| 135 | |
| 136 | /** |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 137 | * Returns the specified topology cluster. |
| 138 | * |
| 139 | * @param clusterId cluster identifier |
| 140 | * @return topology cluster |
| 141 | */ |
| 142 | TopologyCluster getCluster(ClusterId clusterId) { |
| 143 | return clusters.get(clusterId); |
| 144 | } |
| 145 | |
tom | dc95b8a | 2014-09-17 08:07:26 -0700 | [diff] [blame] | 146 | /** |
| 147 | * Returns the topology cluster that contains the given device. |
| 148 | * |
| 149 | * @param deviceId device identifier |
| 150 | * @return topology cluster |
| 151 | */ |
| 152 | TopologyCluster getCluster(DeviceId deviceId) { |
| 153 | return clustersByDevice.get(deviceId); |
| 154 | } |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 155 | |
| 156 | /** |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 157 | * Returns the set of cluster devices. |
| 158 | * |
| 159 | * @param cluster topology cluster |
| 160 | * @return cluster devices |
| 161 | */ |
| 162 | Set<DeviceId> getClusterDevices(TopologyCluster cluster) { |
| 163 | return devicesByCluster.get(cluster); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Returns the set of cluster links. |
| 168 | * |
| 169 | * @param cluster topology cluster |
| 170 | * @return cluster links |
| 171 | */ |
| 172 | Set<Link> getClusterLinks(TopologyCluster cluster) { |
| 173 | return linksByCluster.get(cluster); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Indicates whether the given point is an infrastructure link end-point. |
| 178 | * |
| 179 | * @param connectPoint connection point |
| 180 | * @return true if infrastructure |
| 181 | */ |
tom | e52ce70 | 2014-09-11 00:12:54 -0700 | [diff] [blame] | 182 | boolean isInfrastructure(ConnectPoint connectPoint) { |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 183 | return infrastructurePoints.contains(connectPoint); |
tom | e52ce70 | 2014-09-11 00:12:54 -0700 | [diff] [blame] | 184 | } |
| 185 | |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 186 | /** |
tom | dc95b8a | 2014-09-17 08:07:26 -0700 | [diff] [blame] | 187 | * Indicates whether the given point is part of a broadcast set. |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 188 | * |
| 189 | * @param connectPoint connection point |
tom | dc95b8a | 2014-09-17 08:07:26 -0700 | [diff] [blame] | 190 | * @return true if in broadcast set |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 191 | */ |
tom | dc95b8a | 2014-09-17 08:07:26 -0700 | [diff] [blame] | 192 | boolean isBroadcastPoint(ConnectPoint connectPoint) { |
| 193 | // Any non-infrastructure, i.e. edge points are assumed to be OK. |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 194 | if (!isInfrastructure(connectPoint)) { |
| 195 | return true; |
| 196 | } |
| 197 | |
| 198 | // Find the cluster to which the device belongs. |
| 199 | TopologyCluster cluster = clustersByDevice.get(connectPoint.deviceId()); |
| 200 | if (cluster == null) { |
| 201 | throw new IllegalArgumentException("No cluster found for device " + connectPoint.deviceId()); |
| 202 | } |
| 203 | |
tom | dc95b8a | 2014-09-17 08:07:26 -0700 | [diff] [blame] | 204 | // If the broadcast set is null or empty, or if the point explicitly |
| 205 | // belongs to it, return true; |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 206 | Set<ConnectPoint> points = broadcastSets.get(cluster.id()); |
| 207 | return points == null || points.isEmpty() || points.contains(connectPoint); |
tom | e52ce70 | 2014-09-11 00:12:54 -0700 | [diff] [blame] | 208 | } |
| 209 | |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 210 | /** |
tom | dc95b8a | 2014-09-17 08:07:26 -0700 | [diff] [blame] | 211 | * Returns the size of the cluster broadcast set. |
| 212 | * |
| 213 | * @param clusterId cluster identifier |
| 214 | * @return size of the cluster broadcast set |
| 215 | */ |
| 216 | int broadcastSetSize(ClusterId clusterId) { |
| 217 | return broadcastSets.get(clusterId).size(); |
| 218 | } |
| 219 | |
| 220 | /** |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 221 | * Returns the set of pre-computed shortest paths between source and |
| 222 | * destination devices. |
| 223 | * |
| 224 | * @param src source device |
| 225 | * @param dst destination device |
| 226 | * @return set of shortest paths |
| 227 | */ |
tom | e52ce70 | 2014-09-11 00:12:54 -0700 | [diff] [blame] | 228 | Set<Path> getPaths(DeviceId src, DeviceId dst) { |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 229 | return paths.get(new PathKey(src, dst)); |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Computes on-demand the set of shortest paths between source and |
| 234 | * destination devices. |
| 235 | * |
| 236 | * @param src source device |
| 237 | * @param dst destination device |
| 238 | * @return set of shortest paths |
| 239 | */ |
| 240 | Set<Path> getPaths(DeviceId src, DeviceId dst, LinkWeight weight) { |
| 241 | GraphPathSearch.Result<TopologyVertex, TopologyEdge> result = |
| 242 | DIJKSTRA.search(graph, new DefaultTopologyVertex(src), |
| 243 | new DefaultTopologyVertex(dst), weight); |
| 244 | ImmutableSet.Builder<Path> builder = ImmutableSet.builder(); |
| 245 | for (org.onlab.graph.Path<TopologyVertex, TopologyEdge> path : result.paths()) { |
| 246 | builder.add(networkPath(path)); |
| 247 | } |
| 248 | return builder.build(); |
| 249 | } |
| 250 | |
| 251 | |
| 252 | // Searches the graph for all shortest paths and returns the search results. |
| 253 | private ImmutableMap<DeviceId, Result<TopologyVertex, TopologyEdge>> searchForShortestPaths() { |
| 254 | ImmutableMap.Builder<DeviceId, Result<TopologyVertex, TopologyEdge>> builder = ImmutableMap.builder(); |
| 255 | |
| 256 | // Search graph paths for each source to all destinations. |
| 257 | LinkWeight weight = new HopCountLinkWeight(graph.getVertexes().size()); |
| 258 | for (TopologyVertex src : graph.getVertexes()) { |
| 259 | builder.put(src.deviceId(), DIJKSTRA.search(graph, src, null, weight)); |
| 260 | } |
| 261 | return builder.build(); |
| 262 | } |
| 263 | |
| 264 | // Builds network paths from the graph path search results |
| 265 | private ImmutableSetMultimap<PathKey, Path> buildPaths() { |
| 266 | Builder<PathKey, Path> builder = ImmutableSetMultimap.builder(); |
| 267 | for (DeviceId deviceId : results.keySet()) { |
| 268 | Result<TopologyVertex, TopologyEdge> result = results.get(deviceId); |
| 269 | for (org.onlab.graph.Path<TopologyVertex, TopologyEdge> path : result.paths()) { |
| 270 | builder.put(new PathKey(path.src().deviceId(), path.dst().deviceId()), |
| 271 | networkPath(path)); |
| 272 | } |
| 273 | } |
| 274 | return builder.build(); |
| 275 | } |
| 276 | |
| 277 | // Converts graph path to a network path with the same cost. |
| 278 | private Path networkPath(org.onlab.graph.Path<TopologyVertex, TopologyEdge> path) { |
| 279 | List<Link> links = new ArrayList<>(); |
| 280 | for (TopologyEdge edge : path.edges()) { |
| 281 | links.add(edge.link()); |
| 282 | } |
| 283 | return new DefaultPath(PID, links, path.cost()); |
| 284 | } |
| 285 | |
| 286 | |
| 287 | // Searches for SCC clusters in the network topology graph using Tarjan |
| 288 | // algorithm. |
| 289 | private SCCResult<TopologyVertex, TopologyEdge> searchForClusters() { |
| 290 | return TARJAN.search(graph, new NoIndirectLinksWeight()); |
| 291 | } |
| 292 | |
| 293 | // Builds the topology clusters and returns the id-cluster bindings. |
| 294 | private ImmutableMap<ClusterId, TopologyCluster> buildTopologyClusters() { |
| 295 | ImmutableMap.Builder<ClusterId, TopologyCluster> clusterBuilder = ImmutableMap.builder(); |
| 296 | SCCResult<TopologyVertex, TopologyEdge> result = |
| 297 | TARJAN.search(graph, new NoIndirectLinksWeight()); |
| 298 | |
| 299 | // Extract both vertexes and edges from the results; the lists form |
| 300 | // pairs along the same index. |
| 301 | List<Set<TopologyVertex>> clusterVertexes = result.clusterVertexes(); |
| 302 | List<Set<TopologyEdge>> clusterEdges = result.clusterEdges(); |
| 303 | |
| 304 | // Scan over the lists and create a cluster from the results. |
| 305 | for (int i = 0, n = result.clusterCount(); i < n; i++) { |
| 306 | Set<TopologyVertex> vertexSet = clusterVertexes.get(i); |
| 307 | Set<TopologyEdge> edgeSet = clusterEdges.get(i); |
| 308 | |
| 309 | ClusterId cid = ClusterId.clusterId(i); |
| 310 | DefaultTopologyCluster cluster = |
| 311 | new DefaultTopologyCluster(cid, vertexSet.size(), edgeSet.size(), |
| 312 | findRoot(vertexSet).deviceId()); |
| 313 | clusterBuilder.put(cid, cluster); |
| 314 | } |
| 315 | return clusterBuilder.build(); |
| 316 | } |
| 317 | |
| 318 | // Finds the vertex whose device id is the lexicographical minimum in the |
| 319 | // specified set. |
| 320 | private TopologyVertex findRoot(Set<TopologyVertex> vertexSet) { |
| 321 | TopologyVertex minVertex = null; |
| 322 | for (TopologyVertex vertex : vertexSet) { |
| 323 | if (minVertex == null || |
| 324 | minVertex.deviceId().toString() |
| 325 | .compareTo(minVertex.deviceId().toString()) < 0) { |
| 326 | minVertex = vertex; |
| 327 | } |
| 328 | } |
| 329 | return minVertex; |
| 330 | } |
| 331 | |
| 332 | // Processes a map of broadcast sets for each cluster. |
| 333 | private ImmutableSetMultimap<ClusterId, ConnectPoint> buildBroadcastSets() { |
| 334 | Builder<ClusterId, ConnectPoint> builder = ImmutableSetMultimap.builder(); |
| 335 | for (TopologyCluster cluster : clusters.values()) { |
| 336 | addClusterBroadcastSet(cluster, builder); |
| 337 | } |
| 338 | return builder.build(); |
| 339 | } |
| 340 | |
| 341 | // Finds all broadcast points for the cluster. These are those connection |
| 342 | // points which lie along the shortest paths between the cluster root and |
| 343 | // all other devices within the cluster. |
| 344 | private void addClusterBroadcastSet(TopologyCluster cluster, |
| 345 | Builder<ClusterId, ConnectPoint> builder) { |
| 346 | // Use the graph root search results to build the broadcast set. |
| 347 | Result<TopologyVertex, TopologyEdge> result = results.get(cluster.root()); |
| 348 | for (Map.Entry<TopologyVertex, Set<TopologyEdge>> entry : result.parents().entrySet()) { |
| 349 | TopologyVertex vertex = entry.getKey(); |
| 350 | |
| 351 | // Ignore any parents that lead outside the cluster. |
| 352 | if (clustersByDevice.get(vertex.deviceId()) != cluster) { |
| 353 | continue; |
| 354 | } |
| 355 | |
| 356 | // Ignore any back-link sets that are empty. |
| 357 | Set<TopologyEdge> parents = entry.getValue(); |
| 358 | if (parents.isEmpty()) { |
| 359 | continue; |
| 360 | } |
| 361 | |
| 362 | // Use the first back-link source and destinations to add to the |
| 363 | // broadcast set. |
| 364 | Link link = parents.iterator().next().link(); |
| 365 | builder.put(cluster.id(), link.src()); |
| 366 | builder.put(cluster.id(), link.dst()); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | // Collects and returns an set of all infrastructure link end-points. |
| 371 | private ImmutableSet<ConnectPoint> findInfrastructurePoints() { |
| 372 | ImmutableSet.Builder<ConnectPoint> builder = ImmutableSet.builder(); |
| 373 | for (TopologyEdge edge : graph.getEdges()) { |
| 374 | builder.add(edge.link().src()); |
| 375 | builder.add(edge.link().dst()); |
| 376 | } |
| 377 | return builder.build(); |
| 378 | } |
| 379 | |
| 380 | // Builds cluster-devices, cluster-links and device-cluster indexes. |
| 381 | private void buildIndexes() { |
| 382 | // Prepare the index builders |
| 383 | ImmutableMap.Builder<DeviceId, TopologyCluster> clusterBuilder = ImmutableMap.builder(); |
| 384 | ImmutableSetMultimap.Builder<TopologyCluster, DeviceId> devicesBuilder = ImmutableSetMultimap.builder(); |
| 385 | ImmutableSetMultimap.Builder<TopologyCluster, Link> linksBuilder = ImmutableSetMultimap.builder(); |
| 386 | |
| 387 | // Now scan through all the clusters |
| 388 | for (TopologyCluster cluster : clusters.values()) { |
| 389 | int i = cluster.id().index(); |
| 390 | |
| 391 | // Scan through all the cluster vertexes. |
| 392 | for (TopologyVertex vertex : clusterResults.clusterVertexes().get(i)) { |
| 393 | devicesBuilder.put(cluster, vertex.deviceId()); |
| 394 | clusterBuilder.put(vertex.deviceId(), cluster); |
| 395 | } |
| 396 | |
| 397 | // Scan through all the cluster edges. |
| 398 | for (TopologyEdge edge : clusterResults.clusterEdges().get(i)) { |
| 399 | linksBuilder.put(cluster, edge.link()); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | // Finalize all indexes. |
| 404 | clustersByDevice = clusterBuilder.build(); |
| 405 | devicesByCluster = devicesBuilder.build(); |
| 406 | linksByCluster = linksBuilder.build(); |
| 407 | } |
| 408 | |
| 409 | // Link weight for measuring link cost as hop count with indirect links |
| 410 | // being as expensive as traversing the entire graph to assume the worst. |
| 411 | private static class HopCountLinkWeight implements LinkWeight { |
| 412 | private final int indirectLinkCost; |
| 413 | |
| 414 | HopCountLinkWeight(int indirectLinkCost) { |
| 415 | this.indirectLinkCost = indirectLinkCost; |
| 416 | } |
| 417 | |
| 418 | @Override |
| 419 | public double weight(TopologyEdge edge) { |
| 420 | // To force preference to use direct paths first, make indirect |
| 421 | // links as expensive as the linear vertex traversal. |
| 422 | return edge.link().type() == INDIRECT ? indirectLinkCost : 1; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | // Link weight for preventing traversal over indirect links. |
| 427 | private static class NoIndirectLinksWeight implements LinkWeight { |
| 428 | @Override |
| 429 | public double weight(TopologyEdge edge) { |
| 430 | return edge.link().type() == INDIRECT ? -1 : 1; |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | @Override |
| 435 | public String toString() { |
| 436 | return toStringHelper(this) |
| 437 | .add("time", time) |
| 438 | .add("clusters", clusterCount()) |
| 439 | .add("devices", deviceCount()) |
| 440 | .add("links", linkCount()) |
| 441 | .add("pathCount", pathCount()) |
| 442 | .toString(); |
tom | e52ce70 | 2014-09-11 00:12:54 -0700 | [diff] [blame] | 443 | } |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 444 | } |