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