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