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