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