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