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