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