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