blob: 86b88b3268443280e2ecbcf3d4fb832ba1c3d9fc [file] [log] [blame]
tom8bf2e6b2014-09-10 20:53:54 -07001package org.onlab.onos.net.trivial.topology.impl;
tomdc361b62014-09-09 20:36:52 -07002
tome52ce702014-09-11 00:12:54 -07003import com.google.common.collect.ImmutableMap;
4import com.google.common.collect.ImmutableSet;
5import com.google.common.collect.ImmutableSetMultimap;
tom97937552014-09-11 10:48:42 -07006import org.onlab.graph.DijkstraGraphSearch;
tome52ce702014-09-11 00:12:54 -07007import org.onlab.graph.GraphPathSearch;
tom97937552014-09-11 10:48:42 -07008import org.onlab.graph.TarjanGraphSearch;
tomdc361b62014-09-09 20:36:52 -07009import org.onlab.onos.net.AbstractModel;
tome52ce702014-09-11 00:12:54 -070010import org.onlab.onos.net.ConnectPoint;
tom97937552014-09-11 10:48:42 -070011import org.onlab.onos.net.DefaultPath;
tome52ce702014-09-11 00:12:54 -070012import org.onlab.onos.net.DeviceId;
13import org.onlab.onos.net.Link;
14import org.onlab.onos.net.Path;
tomdc361b62014-09-09 20:36:52 -070015import org.onlab.onos.net.provider.ProviderId;
tom97937552014-09-11 10:48:42 -070016import org.onlab.onos.net.topology.ClusterId;
17import org.onlab.onos.net.topology.DefaultTopologyCluster;
18import org.onlab.onos.net.topology.GraphDescription;
19import org.onlab.onos.net.topology.LinkWeight;
tomdc361b62014-09-09 20:36:52 -070020import org.onlab.onos.net.topology.Topology;
tome52ce702014-09-11 00:12:54 -070021import org.onlab.onos.net.topology.TopologyCluster;
tom97937552014-09-11 10:48:42 -070022import org.onlab.onos.net.topology.TopologyEdge;
23import org.onlab.onos.net.topology.TopologyGraph;
24import org.onlab.onos.net.topology.TopologyVertex;
tome52ce702014-09-11 00:12:54 -070025
tom97937552014-09-11 10:48:42 -070026import java.util.ArrayList;
27import java.util.List;
28import java.util.Map;
tome52ce702014-09-11 00:12:54 -070029import java.util.Set;
tomdc361b62014-09-09 20:36:52 -070030
tom97937552014-09-11 10:48:42 -070031import static com.google.common.base.MoreObjects.toStringHelper;
32import static com.google.common.collect.ImmutableSetMultimap.Builder;
33import static org.onlab.graph.GraphPathSearch.Result;
34import static org.onlab.graph.TarjanGraphSearch.SCCResult;
35import static org.onlab.onos.net.Link.Type.INDIRECT;
36
tomdc361b62014-09-09 20:36:52 -070037/**
38 * Default implementation of the topology descriptor. This carries the
39 * backing topology data.
40 */
41public class DefaultTopology extends AbstractModel implements Topology {
42
tom97937552014-09-11 10:48:42 -070043 private static final DijkstraGraphSearch<TopologyVertex, TopologyEdge> DIJKSTRA =
44 new DijkstraGraphSearch<>();
45 private static final TarjanGraphSearch<TopologyVertex, TopologyEdge> TARJAN =
46 new TarjanGraphSearch<>();
47
48 private static final ProviderId PID = new ProviderId("org.onlab.onos.net");
49
tomdc361b62014-09-09 20:36:52 -070050 private final long time;
tom97937552014-09-11 10:48:42 -070051 private final TopologyGraph graph;
tomdc361b62014-09-09 20:36:52 -070052
tom97937552014-09-11 10:48:42 -070053 private final SCCResult<TopologyVertex, TopologyEdge> clusterResults;
54 private final ImmutableMap<DeviceId, Result<TopologyVertex, TopologyEdge>> results;
55 private final ImmutableSetMultimap<PathKey, Path> paths;
tome52ce702014-09-11 00:12:54 -070056
tom97937552014-09-11 10:48:42 -070057 private final ImmutableMap<ClusterId, TopologyCluster> clusters;
58 private final ImmutableSet<ConnectPoint> infrastructurePoints;
59 private final ImmutableSetMultimap<ClusterId, ConnectPoint> broadcastSets;
60
61 private ImmutableMap<DeviceId, TopologyCluster> clustersByDevice;
62 private ImmutableSetMultimap<TopologyCluster, DeviceId> devicesByCluster;
63 private ImmutableSetMultimap<TopologyCluster, Link> linksByCluster;
64
tome52ce702014-09-11 00:12:54 -070065
tomdc361b62014-09-09 20:36:52 -070066 /**
67 * Creates a topology descriptor attributed to the specified provider.
68 *
tome52ce702014-09-11 00:12:54 -070069 * @param providerId identity of the provider
70 * @param description data describing the new topology
tomdc361b62014-09-09 20:36:52 -070071 */
tom97937552014-09-11 10:48:42 -070072 DefaultTopology(ProviderId providerId, GraphDescription description) {
tomdc361b62014-09-09 20:36:52 -070073 super(providerId);
tome52ce702014-09-11 00:12:54 -070074 this.time = description.timestamp();
tome52ce702014-09-11 00:12:54 -070075
tom97937552014-09-11 10:48:42 -070076 // Build the graph
77 this.graph = new DefaultTopologyGraph(description.vertexes(),
78 description.edges());
79
80 this.results = searchForShortestPaths();
81 this.paths = buildPaths();
82
83 this.clusterResults = searchForClusters();
84 this.clusters = buildTopologyClusters();
85
86 buildIndexes();
87
88 this.broadcastSets = buildBroadcastSets();
89 this.infrastructurePoints = findInfrastructurePoints();
tomdc361b62014-09-09 20:36:52 -070090 }
91
92 @Override
93 public long time() {
94 return time;
95 }
96
97 @Override
98 public int clusterCount() {
tome52ce702014-09-11 00:12:54 -070099 return clusters.size();
tomdc361b62014-09-09 20:36:52 -0700100 }
101
102 @Override
103 public int deviceCount() {
tome52ce702014-09-11 00:12:54 -0700104 return graph.getVertexes().size();
tomdc361b62014-09-09 20:36:52 -0700105 }
106
107 @Override
108 public int linkCount() {
tome52ce702014-09-11 00:12:54 -0700109 return graph.getEdges().size();
tomdc361b62014-09-09 20:36:52 -0700110 }
111
112 @Override
113 public int pathCount() {
tom97937552014-09-11 10:48:42 -0700114 return paths.size();
tomdc361b62014-09-09 20:36:52 -0700115 }
116
tom97937552014-09-11 10:48:42 -0700117 /**
118 * Returns the backing topology graph.
119 *
120 * @return topology graph
121 */
122 TopologyGraph getGraph() {
tome52ce702014-09-11 00:12:54 -0700123 return graph;
124 }
125
tom97937552014-09-11 10:48:42 -0700126 /**
127 * Returns the set of topology clusters.
128 *
129 * @return set of clusters
130 */
131 Set<TopologyCluster> getClusters() {
132 return ImmutableSet.copyOf(clusters.values());
133 }
134
135 /**
136 * Returns the set of cluster devices.
137 *
138 * @param cluster topology cluster
139 * @return cluster devices
140 */
141 Set<DeviceId> getClusterDevices(TopologyCluster cluster) {
142 return devicesByCluster.get(cluster);
143 }
144
145 /**
146 * Returns the set of cluster links.
147 *
148 * @param cluster topology cluster
149 * @return cluster links
150 */
151 Set<Link> getClusterLinks(TopologyCluster cluster) {
152 return linksByCluster.get(cluster);
153 }
154
155 /**
156 * Indicates whether the given point is an infrastructure link end-point.
157 *
158 * @param connectPoint connection point
159 * @return true if infrastructure
160 */
tome52ce702014-09-11 00:12:54 -0700161 boolean isInfrastructure(ConnectPoint connectPoint) {
tom97937552014-09-11 10:48:42 -0700162 return infrastructurePoints.contains(connectPoint);
tome52ce702014-09-11 00:12:54 -0700163 }
164
tom97937552014-09-11 10:48:42 -0700165 /**
166 * Indicates whether the given point is part of a broadcast tree.
167 *
168 * @param connectPoint connection point
169 * @return true if in broadcast tree
170 */
tome52ce702014-09-11 00:12:54 -0700171 boolean isInBroadcastTree(ConnectPoint connectPoint) {
tom97937552014-09-11 10:48:42 -0700172 // Any non-infrastructure, i.e. edge points are assumed to be OK
173 if (!isInfrastructure(connectPoint)) {
174 return true;
175 }
176
177 // Find the cluster to which the device belongs.
178 TopologyCluster cluster = clustersByDevice.get(connectPoint.deviceId());
179 if (cluster == null) {
180 throw new IllegalArgumentException("No cluster found for device " + connectPoint.deviceId());
181 }
182
183 // If the broadcast tree is null or empty, or if the point explicitly
184 // belongs to the broadcast tree points, return true;
185 Set<ConnectPoint> points = broadcastSets.get(cluster.id());
186 return points == null || points.isEmpty() || points.contains(connectPoint);
tome52ce702014-09-11 00:12:54 -0700187 }
188
tom97937552014-09-11 10:48:42 -0700189 /**
190 * Returns the set of pre-computed shortest paths between source and
191 * destination devices.
192 *
193 * @param src source device
194 * @param dst destination device
195 * @return set of shortest paths
196 */
tome52ce702014-09-11 00:12:54 -0700197 Set<Path> getPaths(DeviceId src, DeviceId dst) {
tom97937552014-09-11 10:48:42 -0700198 return paths.get(new PathKey(src, dst));
199 }
200
201 /**
202 * Computes on-demand the set of shortest paths between source and
203 * destination devices.
204 *
205 * @param src source device
206 * @param dst destination device
207 * @return set of shortest paths
208 */
209 Set<Path> getPaths(DeviceId src, DeviceId dst, LinkWeight weight) {
210 GraphPathSearch.Result<TopologyVertex, TopologyEdge> result =
211 DIJKSTRA.search(graph, new DefaultTopologyVertex(src),
212 new DefaultTopologyVertex(dst), weight);
213 ImmutableSet.Builder<Path> builder = ImmutableSet.builder();
214 for (org.onlab.graph.Path<TopologyVertex, TopologyEdge> path : result.paths()) {
215 builder.add(networkPath(path));
216 }
217 return builder.build();
218 }
219
220
221 // Searches the graph for all shortest paths and returns the search results.
222 private ImmutableMap<DeviceId, Result<TopologyVertex, TopologyEdge>> searchForShortestPaths() {
223 ImmutableMap.Builder<DeviceId, Result<TopologyVertex, TopologyEdge>> builder = ImmutableMap.builder();
224
225 // Search graph paths for each source to all destinations.
226 LinkWeight weight = new HopCountLinkWeight(graph.getVertexes().size());
227 for (TopologyVertex src : graph.getVertexes()) {
228 builder.put(src.deviceId(), DIJKSTRA.search(graph, src, null, weight));
229 }
230 return builder.build();
231 }
232
233 // Builds network paths from the graph path search results
234 private ImmutableSetMultimap<PathKey, Path> buildPaths() {
235 Builder<PathKey, Path> builder = ImmutableSetMultimap.builder();
236 for (DeviceId deviceId : results.keySet()) {
237 Result<TopologyVertex, TopologyEdge> result = results.get(deviceId);
238 for (org.onlab.graph.Path<TopologyVertex, TopologyEdge> path : result.paths()) {
239 builder.put(new PathKey(path.src().deviceId(), path.dst().deviceId()),
240 networkPath(path));
241 }
242 }
243 return builder.build();
244 }
245
246 // Converts graph path to a network path with the same cost.
247 private Path networkPath(org.onlab.graph.Path<TopologyVertex, TopologyEdge> path) {
248 List<Link> links = new ArrayList<>();
249 for (TopologyEdge edge : path.edges()) {
250 links.add(edge.link());
251 }
252 return new DefaultPath(PID, links, path.cost());
253 }
254
255
256 // Searches for SCC clusters in the network topology graph using Tarjan
257 // algorithm.
258 private SCCResult<TopologyVertex, TopologyEdge> searchForClusters() {
259 return TARJAN.search(graph, new NoIndirectLinksWeight());
260 }
261
262 // Builds the topology clusters and returns the id-cluster bindings.
263 private ImmutableMap<ClusterId, TopologyCluster> buildTopologyClusters() {
264 ImmutableMap.Builder<ClusterId, TopologyCluster> clusterBuilder = ImmutableMap.builder();
265 SCCResult<TopologyVertex, TopologyEdge> result =
266 TARJAN.search(graph, new NoIndirectLinksWeight());
267
268 // Extract both vertexes and edges from the results; the lists form
269 // pairs along the same index.
270 List<Set<TopologyVertex>> clusterVertexes = result.clusterVertexes();
271 List<Set<TopologyEdge>> clusterEdges = result.clusterEdges();
272
273 // Scan over the lists and create a cluster from the results.
274 for (int i = 0, n = result.clusterCount(); i < n; i++) {
275 Set<TopologyVertex> vertexSet = clusterVertexes.get(i);
276 Set<TopologyEdge> edgeSet = clusterEdges.get(i);
277
278 ClusterId cid = ClusterId.clusterId(i);
279 DefaultTopologyCluster cluster =
280 new DefaultTopologyCluster(cid, vertexSet.size(), edgeSet.size(),
281 findRoot(vertexSet).deviceId());
282 clusterBuilder.put(cid, cluster);
283 }
284 return clusterBuilder.build();
285 }
286
287 // Finds the vertex whose device id is the lexicographical minimum in the
288 // specified set.
289 private TopologyVertex findRoot(Set<TopologyVertex> vertexSet) {
290 TopologyVertex minVertex = null;
291 for (TopologyVertex vertex : vertexSet) {
292 if (minVertex == null ||
293 minVertex.deviceId().toString()
294 .compareTo(minVertex.deviceId().toString()) < 0) {
295 minVertex = vertex;
296 }
297 }
298 return minVertex;
299 }
300
301 // Processes a map of broadcast sets for each cluster.
302 private ImmutableSetMultimap<ClusterId, ConnectPoint> buildBroadcastSets() {
303 Builder<ClusterId, ConnectPoint> builder = ImmutableSetMultimap.builder();
304 for (TopologyCluster cluster : clusters.values()) {
305 addClusterBroadcastSet(cluster, builder);
306 }
307 return builder.build();
308 }
309
310 // Finds all broadcast points for the cluster. These are those connection
311 // points which lie along the shortest paths between the cluster root and
312 // all other devices within the cluster.
313 private void addClusterBroadcastSet(TopologyCluster cluster,
314 Builder<ClusterId, ConnectPoint> builder) {
315 // Use the graph root search results to build the broadcast set.
316 Result<TopologyVertex, TopologyEdge> result = results.get(cluster.root());
317 for (Map.Entry<TopologyVertex, Set<TopologyEdge>> entry : result.parents().entrySet()) {
318 TopologyVertex vertex = entry.getKey();
319
320 // Ignore any parents that lead outside the cluster.
321 if (clustersByDevice.get(vertex.deviceId()) != cluster) {
322 continue;
323 }
324
325 // Ignore any back-link sets that are empty.
326 Set<TopologyEdge> parents = entry.getValue();
327 if (parents.isEmpty()) {
328 continue;
329 }
330
331 // Use the first back-link source and destinations to add to the
332 // broadcast set.
333 Link link = parents.iterator().next().link();
334 builder.put(cluster.id(), link.src());
335 builder.put(cluster.id(), link.dst());
336 }
337 }
338
339 // Collects and returns an set of all infrastructure link end-points.
340 private ImmutableSet<ConnectPoint> findInfrastructurePoints() {
341 ImmutableSet.Builder<ConnectPoint> builder = ImmutableSet.builder();
342 for (TopologyEdge edge : graph.getEdges()) {
343 builder.add(edge.link().src());
344 builder.add(edge.link().dst());
345 }
346 return builder.build();
347 }
348
349 // Builds cluster-devices, cluster-links and device-cluster indexes.
350 private void buildIndexes() {
351 // Prepare the index builders
352 ImmutableMap.Builder<DeviceId, TopologyCluster> clusterBuilder = ImmutableMap.builder();
353 ImmutableSetMultimap.Builder<TopologyCluster, DeviceId> devicesBuilder = ImmutableSetMultimap.builder();
354 ImmutableSetMultimap.Builder<TopologyCluster, Link> linksBuilder = ImmutableSetMultimap.builder();
355
356 // Now scan through all the clusters
357 for (TopologyCluster cluster : clusters.values()) {
358 int i = cluster.id().index();
359
360 // Scan through all the cluster vertexes.
361 for (TopologyVertex vertex : clusterResults.clusterVertexes().get(i)) {
362 devicesBuilder.put(cluster, vertex.deviceId());
363 clusterBuilder.put(vertex.deviceId(), cluster);
364 }
365
366 // Scan through all the cluster edges.
367 for (TopologyEdge edge : clusterResults.clusterEdges().get(i)) {
368 linksBuilder.put(cluster, edge.link());
369 }
370 }
371
372 // Finalize all indexes.
373 clustersByDevice = clusterBuilder.build();
374 devicesByCluster = devicesBuilder.build();
375 linksByCluster = linksBuilder.build();
376 }
377
378 // Link weight for measuring link cost as hop count with indirect links
379 // being as expensive as traversing the entire graph to assume the worst.
380 private static class HopCountLinkWeight implements LinkWeight {
381 private final int indirectLinkCost;
382
383 HopCountLinkWeight(int indirectLinkCost) {
384 this.indirectLinkCost = indirectLinkCost;
385 }
386
387 @Override
388 public double weight(TopologyEdge edge) {
389 // To force preference to use direct paths first, make indirect
390 // links as expensive as the linear vertex traversal.
391 return edge.link().type() == INDIRECT ? indirectLinkCost : 1;
392 }
393 }
394
395 // Link weight for preventing traversal over indirect links.
396 private static class NoIndirectLinksWeight implements LinkWeight {
397 @Override
398 public double weight(TopologyEdge edge) {
399 return edge.link().type() == INDIRECT ? -1 : 1;
400 }
401 }
402
403 @Override
404 public String toString() {
405 return toStringHelper(this)
406 .add("time", time)
407 .add("clusters", clusterCount())
408 .add("devices", deviceCount())
409 .add("links", linkCount())
410 .add("pathCount", pathCount())
411 .toString();
tome52ce702014-09-11 00:12:54 -0700412 }
tomdc361b62014-09-09 20:36:52 -0700413}