blob: ef87867d27d0d26ceb0e3dbed9b3ec08c6fe028d [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 /**
tom13cb4852014-09-11 12:44:17 -0700136 * Returns the specified topology cluster.
137 *
138 * @param clusterId cluster identifier
139 * @return topology cluster
140 */
141 TopologyCluster getCluster(ClusterId clusterId) {
142 return clusters.get(clusterId);
143 }
144
145
146 /**
tom97937552014-09-11 10:48:42 -0700147 * Returns the set of cluster devices.
148 *
149 * @param cluster topology cluster
150 * @return cluster devices
151 */
152 Set<DeviceId> getClusterDevices(TopologyCluster cluster) {
153 return devicesByCluster.get(cluster);
154 }
155
156 /**
157 * Returns the set of cluster links.
158 *
159 * @param cluster topology cluster
160 * @return cluster links
161 */
162 Set<Link> getClusterLinks(TopologyCluster cluster) {
163 return linksByCluster.get(cluster);
164 }
165
166 /**
167 * Indicates whether the given point is an infrastructure link end-point.
168 *
169 * @param connectPoint connection point
170 * @return true if infrastructure
171 */
tome52ce702014-09-11 00:12:54 -0700172 boolean isInfrastructure(ConnectPoint connectPoint) {
tom97937552014-09-11 10:48:42 -0700173 return infrastructurePoints.contains(connectPoint);
tome52ce702014-09-11 00:12:54 -0700174 }
175
tom97937552014-09-11 10:48:42 -0700176 /**
177 * Indicates whether the given point is part of a broadcast tree.
178 *
179 * @param connectPoint connection point
180 * @return true if in broadcast tree
181 */
tome52ce702014-09-11 00:12:54 -0700182 boolean isInBroadcastTree(ConnectPoint connectPoint) {
tom97937552014-09-11 10:48:42 -0700183 // Any non-infrastructure, i.e. edge points are assumed to be OK
184 if (!isInfrastructure(connectPoint)) {
185 return true;
186 }
187
188 // Find the cluster to which the device belongs.
189 TopologyCluster cluster = clustersByDevice.get(connectPoint.deviceId());
190 if (cluster == null) {
191 throw new IllegalArgumentException("No cluster found for device " + connectPoint.deviceId());
192 }
193
194 // If the broadcast tree is null or empty, or if the point explicitly
195 // belongs to the broadcast tree points, return true;
196 Set<ConnectPoint> points = broadcastSets.get(cluster.id());
197 return points == null || points.isEmpty() || points.contains(connectPoint);
tome52ce702014-09-11 00:12:54 -0700198 }
199
tom97937552014-09-11 10:48:42 -0700200 /**
201 * Returns the set of pre-computed shortest paths between source and
202 * destination devices.
203 *
204 * @param src source device
205 * @param dst destination device
206 * @return set of shortest paths
207 */
tome52ce702014-09-11 00:12:54 -0700208 Set<Path> getPaths(DeviceId src, DeviceId dst) {
tom97937552014-09-11 10:48:42 -0700209 return paths.get(new PathKey(src, dst));
210 }
211
212 /**
213 * Computes on-demand the set of shortest paths between source and
214 * destination devices.
215 *
216 * @param src source device
217 * @param dst destination device
218 * @return set of shortest paths
219 */
220 Set<Path> getPaths(DeviceId src, DeviceId dst, LinkWeight weight) {
221 GraphPathSearch.Result<TopologyVertex, TopologyEdge> result =
222 DIJKSTRA.search(graph, new DefaultTopologyVertex(src),
223 new DefaultTopologyVertex(dst), weight);
224 ImmutableSet.Builder<Path> builder = ImmutableSet.builder();
225 for (org.onlab.graph.Path<TopologyVertex, TopologyEdge> path : result.paths()) {
226 builder.add(networkPath(path));
227 }
228 return builder.build();
229 }
230
231
232 // Searches the graph for all shortest paths and returns the search results.
233 private ImmutableMap<DeviceId, Result<TopologyVertex, TopologyEdge>> searchForShortestPaths() {
234 ImmutableMap.Builder<DeviceId, Result<TopologyVertex, TopologyEdge>> builder = ImmutableMap.builder();
235
236 // Search graph paths for each source to all destinations.
237 LinkWeight weight = new HopCountLinkWeight(graph.getVertexes().size());
238 for (TopologyVertex src : graph.getVertexes()) {
239 builder.put(src.deviceId(), DIJKSTRA.search(graph, src, null, weight));
240 }
241 return builder.build();
242 }
243
244 // Builds network paths from the graph path search results
245 private ImmutableSetMultimap<PathKey, Path> buildPaths() {
246 Builder<PathKey, Path> builder = ImmutableSetMultimap.builder();
247 for (DeviceId deviceId : results.keySet()) {
248 Result<TopologyVertex, TopologyEdge> result = results.get(deviceId);
249 for (org.onlab.graph.Path<TopologyVertex, TopologyEdge> path : result.paths()) {
250 builder.put(new PathKey(path.src().deviceId(), path.dst().deviceId()),
251 networkPath(path));
252 }
253 }
254 return builder.build();
255 }
256
257 // Converts graph path to a network path with the same cost.
258 private Path networkPath(org.onlab.graph.Path<TopologyVertex, TopologyEdge> path) {
259 List<Link> links = new ArrayList<>();
260 for (TopologyEdge edge : path.edges()) {
261 links.add(edge.link());
262 }
263 return new DefaultPath(PID, links, path.cost());
264 }
265
266
267 // Searches for SCC clusters in the network topology graph using Tarjan
268 // algorithm.
269 private SCCResult<TopologyVertex, TopologyEdge> searchForClusters() {
270 return TARJAN.search(graph, new NoIndirectLinksWeight());
271 }
272
273 // Builds the topology clusters and returns the id-cluster bindings.
274 private ImmutableMap<ClusterId, TopologyCluster> buildTopologyClusters() {
275 ImmutableMap.Builder<ClusterId, TopologyCluster> clusterBuilder = ImmutableMap.builder();
276 SCCResult<TopologyVertex, TopologyEdge> result =
277 TARJAN.search(graph, new NoIndirectLinksWeight());
278
279 // Extract both vertexes and edges from the results; the lists form
280 // pairs along the same index.
281 List<Set<TopologyVertex>> clusterVertexes = result.clusterVertexes();
282 List<Set<TopologyEdge>> clusterEdges = result.clusterEdges();
283
284 // Scan over the lists and create a cluster from the results.
285 for (int i = 0, n = result.clusterCount(); i < n; i++) {
286 Set<TopologyVertex> vertexSet = clusterVertexes.get(i);
287 Set<TopologyEdge> edgeSet = clusterEdges.get(i);
288
289 ClusterId cid = ClusterId.clusterId(i);
290 DefaultTopologyCluster cluster =
291 new DefaultTopologyCluster(cid, vertexSet.size(), edgeSet.size(),
292 findRoot(vertexSet).deviceId());
293 clusterBuilder.put(cid, cluster);
294 }
295 return clusterBuilder.build();
296 }
297
298 // Finds the vertex whose device id is the lexicographical minimum in the
299 // specified set.
300 private TopologyVertex findRoot(Set<TopologyVertex> vertexSet) {
301 TopologyVertex minVertex = null;
302 for (TopologyVertex vertex : vertexSet) {
303 if (minVertex == null ||
304 minVertex.deviceId().toString()
305 .compareTo(minVertex.deviceId().toString()) < 0) {
306 minVertex = vertex;
307 }
308 }
309 return minVertex;
310 }
311
312 // Processes a map of broadcast sets for each cluster.
313 private ImmutableSetMultimap<ClusterId, ConnectPoint> buildBroadcastSets() {
314 Builder<ClusterId, ConnectPoint> builder = ImmutableSetMultimap.builder();
315 for (TopologyCluster cluster : clusters.values()) {
316 addClusterBroadcastSet(cluster, builder);
317 }
318 return builder.build();
319 }
320
321 // Finds all broadcast points for the cluster. These are those connection
322 // points which lie along the shortest paths between the cluster root and
323 // all other devices within the cluster.
324 private void addClusterBroadcastSet(TopologyCluster cluster,
325 Builder<ClusterId, ConnectPoint> builder) {
326 // Use the graph root search results to build the broadcast set.
327 Result<TopologyVertex, TopologyEdge> result = results.get(cluster.root());
328 for (Map.Entry<TopologyVertex, Set<TopologyEdge>> entry : result.parents().entrySet()) {
329 TopologyVertex vertex = entry.getKey();
330
331 // Ignore any parents that lead outside the cluster.
332 if (clustersByDevice.get(vertex.deviceId()) != cluster) {
333 continue;
334 }
335
336 // Ignore any back-link sets that are empty.
337 Set<TopologyEdge> parents = entry.getValue();
338 if (parents.isEmpty()) {
339 continue;
340 }
341
342 // Use the first back-link source and destinations to add to the
343 // broadcast set.
344 Link link = parents.iterator().next().link();
345 builder.put(cluster.id(), link.src());
346 builder.put(cluster.id(), link.dst());
347 }
348 }
349
350 // Collects and returns an set of all infrastructure link end-points.
351 private ImmutableSet<ConnectPoint> findInfrastructurePoints() {
352 ImmutableSet.Builder<ConnectPoint> builder = ImmutableSet.builder();
353 for (TopologyEdge edge : graph.getEdges()) {
354 builder.add(edge.link().src());
355 builder.add(edge.link().dst());
356 }
357 return builder.build();
358 }
359
360 // Builds cluster-devices, cluster-links and device-cluster indexes.
361 private void buildIndexes() {
362 // Prepare the index builders
363 ImmutableMap.Builder<DeviceId, TopologyCluster> clusterBuilder = ImmutableMap.builder();
364 ImmutableSetMultimap.Builder<TopologyCluster, DeviceId> devicesBuilder = ImmutableSetMultimap.builder();
365 ImmutableSetMultimap.Builder<TopologyCluster, Link> linksBuilder = ImmutableSetMultimap.builder();
366
367 // Now scan through all the clusters
368 for (TopologyCluster cluster : clusters.values()) {
369 int i = cluster.id().index();
370
371 // Scan through all the cluster vertexes.
372 for (TopologyVertex vertex : clusterResults.clusterVertexes().get(i)) {
373 devicesBuilder.put(cluster, vertex.deviceId());
374 clusterBuilder.put(vertex.deviceId(), cluster);
375 }
376
377 // Scan through all the cluster edges.
378 for (TopologyEdge edge : clusterResults.clusterEdges().get(i)) {
379 linksBuilder.put(cluster, edge.link());
380 }
381 }
382
383 // Finalize all indexes.
384 clustersByDevice = clusterBuilder.build();
385 devicesByCluster = devicesBuilder.build();
386 linksByCluster = linksBuilder.build();
387 }
388
389 // Link weight for measuring link cost as hop count with indirect links
390 // being as expensive as traversing the entire graph to assume the worst.
391 private static class HopCountLinkWeight implements LinkWeight {
392 private final int indirectLinkCost;
393
394 HopCountLinkWeight(int indirectLinkCost) {
395 this.indirectLinkCost = indirectLinkCost;
396 }
397
398 @Override
399 public double weight(TopologyEdge edge) {
400 // To force preference to use direct paths first, make indirect
401 // links as expensive as the linear vertex traversal.
402 return edge.link().type() == INDIRECT ? indirectLinkCost : 1;
403 }
404 }
405
406 // Link weight for preventing traversal over indirect links.
407 private static class NoIndirectLinksWeight implements LinkWeight {
408 @Override
409 public double weight(TopologyEdge edge) {
410 return edge.link().type() == INDIRECT ? -1 : 1;
411 }
412 }
413
414 @Override
415 public String toString() {
416 return toStringHelper(this)
417 .add("time", time)
418 .add("clusters", clusterCount())
419 .add("devices", deviceCount())
420 .add("links", linkCount())
421 .add("pathCount", pathCount())
422 .toString();
tome52ce702014-09-11 00:12:54 -0700423 }
tomdc361b62014-09-09 20:36:52 -0700424}