blob: 939e558a2f1cbc92d28faf04160bee6edf51fd34 [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) {
265 GraphPathSearch.Result<TopologyVertex, TopologyEdge> result =
266 DIJKSTRA.search(graph, new DefaultTopologyVertex(src),
267 new DefaultTopologyVertex(dst), weight);
268 ImmutableSet.Builder<Path> builder = ImmutableSet.builder();
269 for (org.onlab.graph.Path<TopologyVertex, TopologyEdge> path : result.paths()) {
270 builder.add(networkPath(path));
271 }
272 return builder.build();
273 }
274
275
276 // Searches the graph for all shortest paths and returns the search results.
277 private ImmutableMap<DeviceId, Result<TopologyVertex, TopologyEdge>> searchForShortestPaths() {
278 ImmutableMap.Builder<DeviceId, Result<TopologyVertex, TopologyEdge>> builder = ImmutableMap.builder();
279
280 // Search graph paths for each source to all destinations.
281 LinkWeight weight = new HopCountLinkWeight(graph.getVertexes().size());
282 for (TopologyVertex src : graph.getVertexes()) {
283 builder.put(src.deviceId(), DIJKSTRA.search(graph, src, null, weight));
284 }
285 return builder.build();
286 }
287
288 // Builds network paths from the graph path search results
289 private ImmutableSetMultimap<PathKey, Path> buildPaths() {
290 Builder<PathKey, Path> builder = ImmutableSetMultimap.builder();
291 for (DeviceId deviceId : results.keySet()) {
292 Result<TopologyVertex, TopologyEdge> result = results.get(deviceId);
293 for (org.onlab.graph.Path<TopologyVertex, TopologyEdge> path : result.paths()) {
294 builder.put(new PathKey(path.src().deviceId(), path.dst().deviceId()),
295 networkPath(path));
296 }
297 }
298 return builder.build();
299 }
300
301 // Converts graph path to a network path with the same cost.
302 private Path networkPath(org.onlab.graph.Path<TopologyVertex, TopologyEdge> path) {
303 List<Link> links = new ArrayList<>();
304 for (TopologyEdge edge : path.edges()) {
305 links.add(edge.link());
306 }
Thomas Vachuska6acd3bb2014-11-09 23:44:22 -0800307 return new DefaultPath(CORE_PROVIDER_ID, links, path.cost());
alshabib339a3d92014-09-26 17:54:32 -0700308 }
309
310
311 // Searches for SCC clusters in the network topology graph using Tarjan
312 // algorithm.
313 private SCCResult<TopologyVertex, TopologyEdge> searchForClusters() {
314 return TARJAN.search(graph, new NoIndirectLinksWeight());
315 }
316
317 // Builds the topology clusters and returns the id-cluster bindings.
318 private ImmutableMap<ClusterId, TopologyCluster> buildTopologyClusters() {
319 ImmutableMap.Builder<ClusterId, TopologyCluster> clusterBuilder = ImmutableMap.builder();
320 SCCResult<TopologyVertex, TopologyEdge> result =
321 TARJAN.search(graph, new NoIndirectLinksWeight());
322
323 // Extract both vertexes and edges from the results; the lists form
324 // pairs along the same index.
325 List<Set<TopologyVertex>> clusterVertexes = result.clusterVertexes();
326 List<Set<TopologyEdge>> clusterEdges = result.clusterEdges();
327
328 // Scan over the lists and create a cluster from the results.
329 for (int i = 0, n = result.clusterCount(); i < n; i++) {
330 Set<TopologyVertex> vertexSet = clusterVertexes.get(i);
331 Set<TopologyEdge> edgeSet = clusterEdges.get(i);
332
333 ClusterId cid = ClusterId.clusterId(i);
334 DefaultTopologyCluster cluster =
335 new DefaultTopologyCluster(cid, vertexSet.size(), edgeSet.size(),
336 findRoot(vertexSet).deviceId());
337 clusterBuilder.put(cid, cluster);
338 }
339 return clusterBuilder.build();
340 }
341
342 // Finds the vertex whose device id is the lexicographical minimum in the
343 // specified set.
344 private TopologyVertex findRoot(Set<TopologyVertex> vertexSet) {
345 TopologyVertex minVertex = null;
346 for (TopologyVertex vertex : vertexSet) {
347 if (minVertex == null ||
348 minVertex.deviceId().toString()
349 .compareTo(minVertex.deviceId().toString()) < 0) {
350 minVertex = vertex;
351 }
352 }
353 return minVertex;
354 }
355
356 // Processes a map of broadcast sets for each cluster.
357 private ImmutableSetMultimap<ClusterId, ConnectPoint> buildBroadcastSets() {
358 Builder<ClusterId, ConnectPoint> builder = ImmutableSetMultimap.builder();
359 for (TopologyCluster cluster : clusters.values()) {
360 addClusterBroadcastSet(cluster, builder);
361 }
362 return builder.build();
363 }
364
365 // Finds all broadcast points for the cluster. These are those connection
366 // points which lie along the shortest paths between the cluster root and
367 // all other devices within the cluster.
368 private void addClusterBroadcastSet(TopologyCluster cluster,
369 Builder<ClusterId, ConnectPoint> builder) {
370 // Use the graph root search results to build the broadcast set.
371 Result<TopologyVertex, TopologyEdge> result = results.get(cluster.root());
372 for (Map.Entry<TopologyVertex, Set<TopologyEdge>> entry : result.parents().entrySet()) {
373 TopologyVertex vertex = entry.getKey();
374
375 // Ignore any parents that lead outside the cluster.
376 if (clustersByDevice.get(vertex.deviceId()) != cluster) {
377 continue;
378 }
379
380 // Ignore any back-link sets that are empty.
381 Set<TopologyEdge> parents = entry.getValue();
382 if (parents.isEmpty()) {
383 continue;
384 }
385
386 // Use the first back-link source and destinations to add to the
387 // broadcast set.
388 Link link = parents.iterator().next().link();
389 builder.put(cluster.id(), link.src());
390 builder.put(cluster.id(), link.dst());
391 }
392 }
393
394 // Collects and returns an set of all infrastructure link end-points.
395 private ImmutableSet<ConnectPoint> findInfrastructurePoints() {
396 ImmutableSet.Builder<ConnectPoint> builder = ImmutableSet.builder();
397 for (TopologyEdge edge : graph.getEdges()) {
398 builder.add(edge.link().src());
399 builder.add(edge.link().dst());
400 }
401 return builder.build();
402 }
403
404 // Builds cluster-devices, cluster-links and device-cluster indexes.
405 private void buildIndexes() {
406 // Prepare the index builders
407 ImmutableMap.Builder<DeviceId, TopologyCluster> clusterBuilder = ImmutableMap.builder();
408 ImmutableSetMultimap.Builder<TopologyCluster, DeviceId> devicesBuilder = ImmutableSetMultimap.builder();
409 ImmutableSetMultimap.Builder<TopologyCluster, Link> linksBuilder = ImmutableSetMultimap.builder();
410
411 // Now scan through all the clusters
412 for (TopologyCluster cluster : clusters.values()) {
413 int i = cluster.id().index();
414
415 // Scan through all the cluster vertexes.
416 for (TopologyVertex vertex : clusterResults.clusterVertexes().get(i)) {
417 devicesBuilder.put(cluster, vertex.deviceId());
418 clusterBuilder.put(vertex.deviceId(), cluster);
419 }
420
421 // Scan through all the cluster edges.
422 for (TopologyEdge edge : clusterResults.clusterEdges().get(i)) {
423 linksBuilder.put(cluster, edge.link());
424 }
425 }
426
427 // Finalize all indexes.
428 clustersByDevice = clusterBuilder.build();
429 devicesByCluster = devicesBuilder.build();
430 linksByCluster = linksBuilder.build();
431 }
432
433 // Link weight for measuring link cost as hop count with indirect links
434 // being as expensive as traversing the entire graph to assume the worst.
435 private static class HopCountLinkWeight implements LinkWeight {
436 private final int indirectLinkCost;
437
438 HopCountLinkWeight(int indirectLinkCost) {
439 this.indirectLinkCost = indirectLinkCost;
440 }
441
442 @Override
443 public double weight(TopologyEdge edge) {
444 // To force preference to use direct paths first, make indirect
445 // links as expensive as the linear vertex traversal.
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800446 return edge.link().state() == ACTIVE ?
447 (edge.link().type() == INDIRECT ? indirectLinkCost : 1) : -1;
alshabib339a3d92014-09-26 17:54:32 -0700448 }
449 }
450
451 // Link weight for preventing traversal over indirect links.
452 private static class NoIndirectLinksWeight implements LinkWeight {
453 @Override
454 public double weight(TopologyEdge edge) {
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800455 return edge.link().state() == INACTIVE || edge.link().type() == INDIRECT ? -1 : 1;
alshabib339a3d92014-09-26 17:54:32 -0700456 }
457 }
458
459 @Override
460 public String toString() {
461 return toStringHelper(this)
462 .add("time", time)
Thomas Vachuska6b7920d2014-11-25 19:48:39 -0800463 .add("computeCost", computeCost)
alshabib339a3d92014-09-26 17:54:32 -0700464 .add("clusters", clusterCount())
465 .add("devices", deviceCount())
466 .add("links", linkCount())
467 .add("pathCount", pathCount())
468 .toString();
469 }
470}