blob: 066089a602a10bf4a60443b403af05f09de5fc21 [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) {
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());
tom97937552014-09-11 10:48:42 -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;
tom97937552014-09-11 10:48:42 -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;
tom97937552014-09-11 10:48:42 -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)
tom97937552014-09-11 10:48:42 -0700464 .add("clusters", clusterCount())
465 .add("devices", deviceCount())
466 .add("links", linkCount())
467 .add("pathCount", pathCount())
468 .toString();
tome52ce702014-09-11 00:12:54 -0700469 }
tomdc361b62014-09-09 20:36:52 -0700470}