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