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