blob: a938039708c2b3f9465e2dbd7c8287d151fa51ca [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 */
tomea961ff2014-10-01 12:45:15 -070016package org.onlab.onos.store.trivial.impl;
tomdc361b62014-09-09 20:36:52 -070017
tome52ce702014-09-11 00:12:54 -070018import com.google.common.collect.ImmutableMap;
19import com.google.common.collect.ImmutableSet;
20import com.google.common.collect.ImmutableSetMultimap;
tom97937552014-09-11 10:48:42 -070021import org.onlab.graph.DijkstraGraphSearch;
tome52ce702014-09-11 00:12:54 -070022import org.onlab.graph.GraphPathSearch;
tom97937552014-09-11 10:48:42 -070023import org.onlab.graph.TarjanGraphSearch;
tomdc361b62014-09-09 20:36:52 -070024import org.onlab.onos.net.AbstractModel;
tome52ce702014-09-11 00:12:54 -070025import org.onlab.onos.net.ConnectPoint;
tom97937552014-09-11 10:48:42 -070026import org.onlab.onos.net.DefaultPath;
tome52ce702014-09-11 00:12:54 -070027import org.onlab.onos.net.DeviceId;
28import org.onlab.onos.net.Link;
29import org.onlab.onos.net.Path;
tomdc361b62014-09-09 20:36:52 -070030import org.onlab.onos.net.provider.ProviderId;
tom97937552014-09-11 10:48:42 -070031import org.onlab.onos.net.topology.ClusterId;
32import org.onlab.onos.net.topology.DefaultTopologyCluster;
tombe988312014-09-19 18:38:47 -070033import org.onlab.onos.net.topology.DefaultTopologyVertex;
tom97937552014-09-11 10:48:42 -070034import org.onlab.onos.net.topology.GraphDescription;
35import org.onlab.onos.net.topology.LinkWeight;
tomdc361b62014-09-09 20:36:52 -070036import org.onlab.onos.net.topology.Topology;
tome52ce702014-09-11 00:12:54 -070037import org.onlab.onos.net.topology.TopologyCluster;
tom97937552014-09-11 10:48:42 -070038import org.onlab.onos.net.topology.TopologyEdge;
39import org.onlab.onos.net.topology.TopologyGraph;
40import org.onlab.onos.net.topology.TopologyVertex;
tome52ce702014-09-11 00:12:54 -070041
tom97937552014-09-11 10:48:42 -070042import java.util.ArrayList;
43import java.util.List;
44import java.util.Map;
tome52ce702014-09-11 00:12:54 -070045import java.util.Set;
tomdc361b62014-09-09 20:36:52 -070046
tom97937552014-09-11 10:48:42 -070047import 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;
tom97937552014-09-11 10:48:42 -070054import static org.onlab.onos.net.Link.Type.INDIRECT;
55
tomdc361b62014-09-09 20:36:52 -070056/**
57 * Default implementation of the topology descriptor. This carries the
58 * backing topology data.
59 */
60public class DefaultTopology extends AbstractModel implements Topology {
61
tom97937552014-09-11 10:48:42 -070062 private static final DijkstraGraphSearch<TopologyVertex, TopologyEdge> DIJKSTRA =
63 new DijkstraGraphSearch<>();
64 private static final TarjanGraphSearch<TopologyVertex, TopologyEdge> TARJAN =
65 new TarjanGraphSearch<>();
66
tomdc361b62014-09-09 20:36:52 -070067 private final long time;
Thomas Vachuska6b7920d2014-11-25 19:48:39 -080068 private final long computeCost;
tom97937552014-09-11 10:48:42 -070069 private final TopologyGraph graph;
tomdc361b62014-09-09 20:36:52 -070070
tom97937552014-09-11 10:48:42 -070071 private final SCCResult<TopologyVertex, TopologyEdge> clusterResults;
72 private final ImmutableMap<DeviceId, Result<TopologyVertex, TopologyEdge>> results;
73 private final ImmutableSetMultimap<PathKey, Path> paths;
tome52ce702014-09-11 00:12:54 -070074
tom97937552014-09-11 10:48:42 -070075 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
tome52ce702014-09-11 00:12:54 -070083
tomdc361b62014-09-09 20:36:52 -070084 /**
85 * Creates a topology descriptor attributed to the specified provider.
86 *
tome52ce702014-09-11 00:12:54 -070087 * @param providerId identity of the provider
88 * @param description data describing the new topology
tomdc361b62014-09-09 20:36:52 -070089 */
tom97937552014-09-11 10:48:42 -070090 DefaultTopology(ProviderId providerId, GraphDescription description) {
tomdc361b62014-09-09 20:36:52 -070091 super(providerId);
tome52ce702014-09-11 00:12:54 -070092 this.time = description.timestamp();
tome52ce702014-09-11 00:12:54 -070093
tom97937552014-09-11 10:48:42 -070094 // 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);
tomdc361b62014-09-09 20:36:52 -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
tomdc361b62014-09-09 20:36:52 -0700122 public int clusterCount() {
tome52ce702014-09-11 00:12:54 -0700123 return clusters.size();
tomdc361b62014-09-09 20:36:52 -0700124 }
125
126 @Override
127 public int deviceCount() {
tome52ce702014-09-11 00:12:54 -0700128 return graph.getVertexes().size();
tomdc361b62014-09-09 20:36:52 -0700129 }
130
131 @Override
132 public int linkCount() {
tome52ce702014-09-11 00:12:54 -0700133 return graph.getEdges().size();
tomdc361b62014-09-09 20:36:52 -0700134 }
135
136 @Override
137 public int pathCount() {
tom97937552014-09-11 10:48:42 -0700138 return paths.size();
tomdc361b62014-09-09 20:36:52 -0700139 }
140
tom97937552014-09-11 10:48:42 -0700141 /**
142 * Returns the backing topology graph.
143 *
144 * @return topology graph
145 */
146 TopologyGraph getGraph() {
tome52ce702014-09-11 00:12:54 -0700147 return graph;
148 }
149
tom97937552014-09-11 10:48:42 -0700150 /**
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 /**
tom13cb4852014-09-11 12:44:17 -0700160 * 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
tomdc95b8a2014-09-17 08:07:26 -0700169 /**
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 }
tom13cb4852014-09-11 12:44:17 -0700178
179 /**
tom97937552014-09-11 10:48:42 -0700180 * 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 */
tome52ce702014-09-11 00:12:54 -0700205 boolean isInfrastructure(ConnectPoint connectPoint) {
tom97937552014-09-11 10:48:42 -0700206 return infrastructurePoints.contains(connectPoint);
tome52ce702014-09-11 00:12:54 -0700207 }
208
tom97937552014-09-11 10:48:42 -0700209 /**
tomdc95b8a2014-09-17 08:07:26 -0700210 * Indicates whether the given point is part of a broadcast set.
tom97937552014-09-11 10:48:42 -0700211 *
212 * @param connectPoint connection point
tomdc95b8a2014-09-17 08:07:26 -0700213 * @return true if in broadcast set
tom97937552014-09-11 10:48:42 -0700214 */
tomdc95b8a2014-09-17 08:07:26 -0700215 boolean isBroadcastPoint(ConnectPoint connectPoint) {
216 // Any non-infrastructure, i.e. edge points are assumed to be OK.
tom97937552014-09-11 10:48:42 -0700217 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
tomdc95b8a2014-09-17 08:07:26 -0700227 // If the broadcast set is null or empty, or if the point explicitly
228 // belongs to it, return true;
tom97937552014-09-11 10:48:42 -0700229 Set<ConnectPoint> points = broadcastSets.get(cluster.id());
230 return points == null || points.isEmpty() || points.contains(connectPoint);
tome52ce702014-09-11 00:12:54 -0700231 }
232
tom97937552014-09-11 10:48:42 -0700233 /**
tomdc95b8a2014-09-17 08:07:26 -0700234 * 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 /**
tom97937552014-09-11 10:48:42 -0700244 * 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 */
tome52ce702014-09-11 00:12:54 -0700251 Set<Path> getPaths(DeviceId src, DeviceId dst) {
tom97937552014-09-11 10:48:42 -0700252 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 Vachuska22925672014-11-11 17:57:53 -0800259 * @param src source device
260 * @param dst destination device
261 * @param weight edge weight function
tom97937552014-09-11 10:48:42 -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
tom97937552014-09-11 10:48:42 -0700273 GraphPathSearch.Result<TopologyVertex, TopologyEdge> result =
Yuta HIGUCHI82e53262014-11-27 10:28:51 -0800274 DIJKSTRA.search(graph, srcV, dstV, weight);
tom97937552014-09-11 10:48:42 -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());
tom97937552014-09-11 10:48:42 -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;
tom97937552014-09-11 10:48:42 -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;
tom97937552014-09-11 10:48:42 -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)
tom97937552014-09-11 10:48:42 -0700471 .add("clusters", clusterCount())
472 .add("devices", deviceCount())
473 .add("links", linkCount())
474 .add("pathCount", pathCount())
475 .toString();
tome52ce702014-09-11 00:12:54 -0700476 }
tomdc361b62014-09-09 20:36:52 -0700477}