blob: 7dd739eae1b50770d4bc674ba01f47b3999d27c9 [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;
tom97937552014-09-11 10:48:42 -070052import static org.onlab.onos.net.Link.Type.INDIRECT;
53
tomdc361b62014-09-09 20:36:52 -070054/**
55 * Default implementation of the topology descriptor. This carries the
56 * backing topology data.
57 */
58public class DefaultTopology extends AbstractModel implements Topology {
59
tom97937552014-09-11 10:48:42 -070060 private static final DijkstraGraphSearch<TopologyVertex, TopologyEdge> DIJKSTRA =
61 new DijkstraGraphSearch<>();
62 private static final TarjanGraphSearch<TopologyVertex, TopologyEdge> TARJAN =
63 new TarjanGraphSearch<>();
64
tomdc361b62014-09-09 20:36:52 -070065 private final long time;
tom97937552014-09-11 10:48:42 -070066 private final TopologyGraph graph;
tomdc361b62014-09-09 20:36:52 -070067
tom97937552014-09-11 10:48:42 -070068 private final SCCResult<TopologyVertex, TopologyEdge> clusterResults;
69 private final ImmutableMap<DeviceId, Result<TopologyVertex, TopologyEdge>> results;
70 private final ImmutableSetMultimap<PathKey, Path> paths;
tome52ce702014-09-11 00:12:54 -070071
tom97937552014-09-11 10:48:42 -070072 private final ImmutableMap<ClusterId, TopologyCluster> clusters;
73 private final ImmutableSet<ConnectPoint> infrastructurePoints;
74 private final ImmutableSetMultimap<ClusterId, ConnectPoint> broadcastSets;
75
76 private ImmutableMap<DeviceId, TopologyCluster> clustersByDevice;
77 private ImmutableSetMultimap<TopologyCluster, DeviceId> devicesByCluster;
78 private ImmutableSetMultimap<TopologyCluster, Link> linksByCluster;
79
tome52ce702014-09-11 00:12:54 -070080
tomdc361b62014-09-09 20:36:52 -070081 /**
82 * Creates a topology descriptor attributed to the specified provider.
83 *
tome52ce702014-09-11 00:12:54 -070084 * @param providerId identity of the provider
85 * @param description data describing the new topology
tomdc361b62014-09-09 20:36:52 -070086 */
tom97937552014-09-11 10:48:42 -070087 DefaultTopology(ProviderId providerId, GraphDescription description) {
tomdc361b62014-09-09 20:36:52 -070088 super(providerId);
tome52ce702014-09-11 00:12:54 -070089 this.time = description.timestamp();
tome52ce702014-09-11 00:12:54 -070090
tom97937552014-09-11 10:48:42 -070091 // Build the graph
92 this.graph = new DefaultTopologyGraph(description.vertexes(),
93 description.edges());
94
95 this.results = searchForShortestPaths();
96 this.paths = buildPaths();
97
98 this.clusterResults = searchForClusters();
99 this.clusters = buildTopologyClusters();
100
101 buildIndexes();
102
103 this.broadcastSets = buildBroadcastSets();
104 this.infrastructurePoints = findInfrastructurePoints();
tomdc361b62014-09-09 20:36:52 -0700105 }
106
107 @Override
108 public long time() {
109 return time;
110 }
111
112 @Override
113 public int clusterCount() {
tome52ce702014-09-11 00:12:54 -0700114 return clusters.size();
tomdc361b62014-09-09 20:36:52 -0700115 }
116
117 @Override
118 public int deviceCount() {
tome52ce702014-09-11 00:12:54 -0700119 return graph.getVertexes().size();
tomdc361b62014-09-09 20:36:52 -0700120 }
121
122 @Override
123 public int linkCount() {
tome52ce702014-09-11 00:12:54 -0700124 return graph.getEdges().size();
tomdc361b62014-09-09 20:36:52 -0700125 }
126
127 @Override
128 public int pathCount() {
tom97937552014-09-11 10:48:42 -0700129 return paths.size();
tomdc361b62014-09-09 20:36:52 -0700130 }
131
tom97937552014-09-11 10:48:42 -0700132 /**
133 * Returns the backing topology graph.
134 *
135 * @return topology graph
136 */
137 TopologyGraph getGraph() {
tome52ce702014-09-11 00:12:54 -0700138 return graph;
139 }
140
tom97937552014-09-11 10:48:42 -0700141 /**
142 * Returns the set of topology clusters.
143 *
144 * @return set of clusters
145 */
146 Set<TopologyCluster> getClusters() {
147 return ImmutableSet.copyOf(clusters.values());
148 }
149
150 /**
tom13cb4852014-09-11 12:44:17 -0700151 * Returns the specified topology cluster.
152 *
153 * @param clusterId cluster identifier
154 * @return topology cluster
155 */
156 TopologyCluster getCluster(ClusterId clusterId) {
157 return clusters.get(clusterId);
158 }
159
tomdc95b8a2014-09-17 08:07:26 -0700160 /**
161 * Returns the topology cluster that contains the given device.
162 *
163 * @param deviceId device identifier
164 * @return topology cluster
165 */
166 TopologyCluster getCluster(DeviceId deviceId) {
167 return clustersByDevice.get(deviceId);
168 }
tom13cb4852014-09-11 12:44:17 -0700169
170 /**
tom97937552014-09-11 10:48:42 -0700171 * Returns the set of cluster devices.
172 *
173 * @param cluster topology cluster
174 * @return cluster devices
175 */
176 Set<DeviceId> getClusterDevices(TopologyCluster cluster) {
177 return devicesByCluster.get(cluster);
178 }
179
180 /**
181 * Returns the set of cluster links.
182 *
183 * @param cluster topology cluster
184 * @return cluster links
185 */
186 Set<Link> getClusterLinks(TopologyCluster cluster) {
187 return linksByCluster.get(cluster);
188 }
189
190 /**
191 * Indicates whether the given point is an infrastructure link end-point.
192 *
193 * @param connectPoint connection point
194 * @return true if infrastructure
195 */
tome52ce702014-09-11 00:12:54 -0700196 boolean isInfrastructure(ConnectPoint connectPoint) {
tom97937552014-09-11 10:48:42 -0700197 return infrastructurePoints.contains(connectPoint);
tome52ce702014-09-11 00:12:54 -0700198 }
199
tom97937552014-09-11 10:48:42 -0700200 /**
tomdc95b8a2014-09-17 08:07:26 -0700201 * Indicates whether the given point is part of a broadcast set.
tom97937552014-09-11 10:48:42 -0700202 *
203 * @param connectPoint connection point
tomdc95b8a2014-09-17 08:07:26 -0700204 * @return true if in broadcast set
tom97937552014-09-11 10:48:42 -0700205 */
tomdc95b8a2014-09-17 08:07:26 -0700206 boolean isBroadcastPoint(ConnectPoint connectPoint) {
207 // Any non-infrastructure, i.e. edge points are assumed to be OK.
tom97937552014-09-11 10:48:42 -0700208 if (!isInfrastructure(connectPoint)) {
209 return true;
210 }
211
212 // Find the cluster to which the device belongs.
213 TopologyCluster cluster = clustersByDevice.get(connectPoint.deviceId());
214 if (cluster == null) {
215 throw new IllegalArgumentException("No cluster found for device " + connectPoint.deviceId());
216 }
217
tomdc95b8a2014-09-17 08:07:26 -0700218 // If the broadcast set is null or empty, or if the point explicitly
219 // belongs to it, return true;
tom97937552014-09-11 10:48:42 -0700220 Set<ConnectPoint> points = broadcastSets.get(cluster.id());
221 return points == null || points.isEmpty() || points.contains(connectPoint);
tome52ce702014-09-11 00:12:54 -0700222 }
223
tom97937552014-09-11 10:48:42 -0700224 /**
tomdc95b8a2014-09-17 08:07:26 -0700225 * Returns the size of the cluster broadcast set.
226 *
227 * @param clusterId cluster identifier
228 * @return size of the cluster broadcast set
229 */
230 int broadcastSetSize(ClusterId clusterId) {
231 return broadcastSets.get(clusterId).size();
232 }
233
234 /**
tom97937552014-09-11 10:48:42 -0700235 * Returns the set of pre-computed shortest paths between source and
236 * destination devices.
237 *
238 * @param src source device
239 * @param dst destination device
240 * @return set of shortest paths
241 */
tome52ce702014-09-11 00:12:54 -0700242 Set<Path> getPaths(DeviceId src, DeviceId dst) {
tom97937552014-09-11 10:48:42 -0700243 return paths.get(new PathKey(src, dst));
244 }
245
246 /**
247 * Computes on-demand the set of shortest paths between source and
248 * destination devices.
249 *
250 * @param src source device
251 * @param dst destination device
252 * @return set of shortest paths
253 */
254 Set<Path> getPaths(DeviceId src, DeviceId dst, LinkWeight weight) {
255 GraphPathSearch.Result<TopologyVertex, TopologyEdge> result =
256 DIJKSTRA.search(graph, new DefaultTopologyVertex(src),
257 new DefaultTopologyVertex(dst), weight);
258 ImmutableSet.Builder<Path> builder = ImmutableSet.builder();
259 for (org.onlab.graph.Path<TopologyVertex, TopologyEdge> path : result.paths()) {
260 builder.add(networkPath(path));
261 }
262 return builder.build();
263 }
264
265
266 // Searches the graph for all shortest paths and returns the search results.
267 private ImmutableMap<DeviceId, Result<TopologyVertex, TopologyEdge>> searchForShortestPaths() {
268 ImmutableMap.Builder<DeviceId, Result<TopologyVertex, TopologyEdge>> builder = ImmutableMap.builder();
269
270 // Search graph paths for each source to all destinations.
271 LinkWeight weight = new HopCountLinkWeight(graph.getVertexes().size());
272 for (TopologyVertex src : graph.getVertexes()) {
273 builder.put(src.deviceId(), DIJKSTRA.search(graph, src, null, weight));
274 }
275 return builder.build();
276 }
277
278 // Builds network paths from the graph path search results
279 private ImmutableSetMultimap<PathKey, Path> buildPaths() {
280 Builder<PathKey, Path> builder = ImmutableSetMultimap.builder();
281 for (DeviceId deviceId : results.keySet()) {
282 Result<TopologyVertex, TopologyEdge> result = results.get(deviceId);
283 for (org.onlab.graph.Path<TopologyVertex, TopologyEdge> path : result.paths()) {
284 builder.put(new PathKey(path.src().deviceId(), path.dst().deviceId()),
285 networkPath(path));
286 }
287 }
288 return builder.build();
289 }
290
291 // Converts graph path to a network path with the same cost.
292 private Path networkPath(org.onlab.graph.Path<TopologyVertex, TopologyEdge> path) {
293 List<Link> links = new ArrayList<>();
294 for (TopologyEdge edge : path.edges()) {
295 links.add(edge.link());
296 }
Thomas Vachuska6acd3bb2014-11-09 23:44:22 -0800297 return new DefaultPath(CORE_PROVIDER_ID, links, path.cost());
tom97937552014-09-11 10:48:42 -0700298 }
299
300
301 // Searches for SCC clusters in the network topology graph using Tarjan
302 // algorithm.
303 private SCCResult<TopologyVertex, TopologyEdge> searchForClusters() {
304 return TARJAN.search(graph, new NoIndirectLinksWeight());
305 }
306
307 // Builds the topology clusters and returns the id-cluster bindings.
308 private ImmutableMap<ClusterId, TopologyCluster> buildTopologyClusters() {
309 ImmutableMap.Builder<ClusterId, TopologyCluster> clusterBuilder = ImmutableMap.builder();
310 SCCResult<TopologyVertex, TopologyEdge> result =
311 TARJAN.search(graph, new NoIndirectLinksWeight());
312
313 // Extract both vertexes and edges from the results; the lists form
314 // pairs along the same index.
315 List<Set<TopologyVertex>> clusterVertexes = result.clusterVertexes();
316 List<Set<TopologyEdge>> clusterEdges = result.clusterEdges();
317
318 // Scan over the lists and create a cluster from the results.
319 for (int i = 0, n = result.clusterCount(); i < n; i++) {
320 Set<TopologyVertex> vertexSet = clusterVertexes.get(i);
321 Set<TopologyEdge> edgeSet = clusterEdges.get(i);
322
323 ClusterId cid = ClusterId.clusterId(i);
324 DefaultTopologyCluster cluster =
325 new DefaultTopologyCluster(cid, vertexSet.size(), edgeSet.size(),
326 findRoot(vertexSet).deviceId());
327 clusterBuilder.put(cid, cluster);
328 }
329 return clusterBuilder.build();
330 }
331
332 // Finds the vertex whose device id is the lexicographical minimum in the
333 // specified set.
334 private TopologyVertex findRoot(Set<TopologyVertex> vertexSet) {
335 TopologyVertex minVertex = null;
336 for (TopologyVertex vertex : vertexSet) {
337 if (minVertex == null ||
338 minVertex.deviceId().toString()
339 .compareTo(minVertex.deviceId().toString()) < 0) {
340 minVertex = vertex;
341 }
342 }
343 return minVertex;
344 }
345
346 // Processes a map of broadcast sets for each cluster.
347 private ImmutableSetMultimap<ClusterId, ConnectPoint> buildBroadcastSets() {
348 Builder<ClusterId, ConnectPoint> builder = ImmutableSetMultimap.builder();
349 for (TopologyCluster cluster : clusters.values()) {
350 addClusterBroadcastSet(cluster, builder);
351 }
352 return builder.build();
353 }
354
355 // Finds all broadcast points for the cluster. These are those connection
356 // points which lie along the shortest paths between the cluster root and
357 // all other devices within the cluster.
358 private void addClusterBroadcastSet(TopologyCluster cluster,
359 Builder<ClusterId, ConnectPoint> builder) {
360 // Use the graph root search results to build the broadcast set.
361 Result<TopologyVertex, TopologyEdge> result = results.get(cluster.root());
362 for (Map.Entry<TopologyVertex, Set<TopologyEdge>> entry : result.parents().entrySet()) {
363 TopologyVertex vertex = entry.getKey();
364
365 // Ignore any parents that lead outside the cluster.
366 if (clustersByDevice.get(vertex.deviceId()) != cluster) {
367 continue;
368 }
369
370 // Ignore any back-link sets that are empty.
371 Set<TopologyEdge> parents = entry.getValue();
372 if (parents.isEmpty()) {
373 continue;
374 }
375
376 // Use the first back-link source and destinations to add to the
377 // broadcast set.
378 Link link = parents.iterator().next().link();
379 builder.put(cluster.id(), link.src());
380 builder.put(cluster.id(), link.dst());
381 }
382 }
383
384 // Collects and returns an set of all infrastructure link end-points.
385 private ImmutableSet<ConnectPoint> findInfrastructurePoints() {
386 ImmutableSet.Builder<ConnectPoint> builder = ImmutableSet.builder();
387 for (TopologyEdge edge : graph.getEdges()) {
388 builder.add(edge.link().src());
389 builder.add(edge.link().dst());
390 }
391 return builder.build();
392 }
393
394 // Builds cluster-devices, cluster-links and device-cluster indexes.
395 private void buildIndexes() {
396 // Prepare the index builders
397 ImmutableMap.Builder<DeviceId, TopologyCluster> clusterBuilder = ImmutableMap.builder();
398 ImmutableSetMultimap.Builder<TopologyCluster, DeviceId> devicesBuilder = ImmutableSetMultimap.builder();
399 ImmutableSetMultimap.Builder<TopologyCluster, Link> linksBuilder = ImmutableSetMultimap.builder();
400
401 // Now scan through all the clusters
402 for (TopologyCluster cluster : clusters.values()) {
403 int i = cluster.id().index();
404
405 // Scan through all the cluster vertexes.
406 for (TopologyVertex vertex : clusterResults.clusterVertexes().get(i)) {
407 devicesBuilder.put(cluster, vertex.deviceId());
408 clusterBuilder.put(vertex.deviceId(), cluster);
409 }
410
411 // Scan through all the cluster edges.
412 for (TopologyEdge edge : clusterResults.clusterEdges().get(i)) {
413 linksBuilder.put(cluster, edge.link());
414 }
415 }
416
417 // Finalize all indexes.
418 clustersByDevice = clusterBuilder.build();
419 devicesByCluster = devicesBuilder.build();
420 linksByCluster = linksBuilder.build();
421 }
422
423 // Link weight for measuring link cost as hop count with indirect links
424 // being as expensive as traversing the entire graph to assume the worst.
425 private static class HopCountLinkWeight implements LinkWeight {
426 private final int indirectLinkCost;
427
428 HopCountLinkWeight(int indirectLinkCost) {
429 this.indirectLinkCost = indirectLinkCost;
430 }
431
432 @Override
433 public double weight(TopologyEdge edge) {
434 // To force preference to use direct paths first, make indirect
435 // links as expensive as the linear vertex traversal.
436 return edge.link().type() == INDIRECT ? indirectLinkCost : 1;
437 }
438 }
439
440 // Link weight for preventing traversal over indirect links.
441 private static class NoIndirectLinksWeight implements LinkWeight {
442 @Override
443 public double weight(TopologyEdge edge) {
444 return edge.link().type() == INDIRECT ? -1 : 1;
445 }
446 }
447
448 @Override
449 public String toString() {
450 return toStringHelper(this)
451 .add("time", time)
452 .add("clusters", clusterCount())
453 .add("devices", deviceCount())
454 .add("links", linkCount())
455 .add("pathCount", pathCount())
456 .toString();
tome52ce702014-09-11 00:12:54 -0700457 }
tomdc361b62014-09-09 20:36:52 -0700458}