blob: 1850fa41f4c9c9dc727eed74ffac3876df5d9a56 [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;
alshabib339a3d92014-09-26 17:54:32 -070052import static org.onlab.onos.net.Link.Type.INDIRECT;
53
54/**
55 * Default implementation of the topology descriptor. This carries the
56 * backing topology data.
57 */
58public class DefaultTopology extends AbstractModel implements Topology {
59
60 private static final DijkstraGraphSearch<TopologyVertex, TopologyEdge> DIJKSTRA =
61 new DijkstraGraphSearch<>();
62 private static final TarjanGraphSearch<TopologyVertex, TopologyEdge> TARJAN =
63 new TarjanGraphSearch<>();
64
alshabib339a3d92014-09-26 17:54:32 -070065 private final long time;
66 private final TopologyGraph graph;
67
68 private final SCCResult<TopologyVertex, TopologyEdge> clusterResults;
69 private final ImmutableMap<DeviceId, Result<TopologyVertex, TopologyEdge>> results;
70 private final ImmutableSetMultimap<PathKey, Path> paths;
71
72 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
80
81 /**
82 * Creates a topology descriptor attributed to the specified provider.
83 *
84 * @param providerId identity of the provider
85 * @param description data describing the new topology
86 */
87 DefaultTopology(ProviderId providerId, GraphDescription description) {
88 super(providerId);
89 this.time = description.timestamp();
90
91 // 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();
105 }
106
107 @Override
108 public long time() {
109 return time;
110 }
111
112 @Override
113 public int clusterCount() {
114 return clusters.size();
115 }
116
117 @Override
118 public int deviceCount() {
119 return graph.getVertexes().size();
120 }
121
122 @Override
123 public int linkCount() {
124 return graph.getEdges().size();
125 }
126
127 @Override
128 public int pathCount() {
129 return paths.size();
130 }
131
132 /**
133 * Returns the backing topology graph.
134 *
135 * @return topology graph
136 */
137 TopologyGraph getGraph() {
138 return graph;
139 }
140
141 /**
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 /**
151 * 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
160 /**
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 }
169
170 /**
171 * 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 */
196 boolean isInfrastructure(ConnectPoint connectPoint) {
197 return infrastructurePoints.contains(connectPoint);
198 }
199
200 /**
201 * Indicates whether the given point is part of a broadcast set.
202 *
203 * @param connectPoint connection point
204 * @return true if in broadcast set
205 */
206 boolean isBroadcastPoint(ConnectPoint connectPoint) {
207 // Any non-infrastructure, i.e. edge points are assumed to be OK.
208 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
218 // If the broadcast set is null or empty, or if the point explicitly
219 // belongs to it, return true;
220 Set<ConnectPoint> points = broadcastSets.get(cluster.id());
221 return points == null || points.isEmpty() || points.contains(connectPoint);
222 }
223
224 /**
225 * 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 /**
235 * 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 */
242 Set<Path> getPaths(DeviceId src, DeviceId dst) {
243 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 *
Thomas Vachuskab14c77a2014-11-04 18:08:01 -0800250 * @param src source device
251 * @param dst destination device
252 * @param weight link weight function
alshabib339a3d92014-09-26 17:54:32 -0700253 * @return set of shortest paths
254 */
255 Set<Path> getPaths(DeviceId src, DeviceId dst, LinkWeight weight) {
256 GraphPathSearch.Result<TopologyVertex, TopologyEdge> result =
257 DIJKSTRA.search(graph, new DefaultTopologyVertex(src),
258 new DefaultTopologyVertex(dst), weight);
259 ImmutableSet.Builder<Path> builder = ImmutableSet.builder();
260 for (org.onlab.graph.Path<TopologyVertex, TopologyEdge> path : result.paths()) {
261 builder.add(networkPath(path));
262 }
263 return builder.build();
264 }
265
266
267 // Searches the graph for all shortest paths and returns the search results.
268 private ImmutableMap<DeviceId, Result<TopologyVertex, TopologyEdge>> searchForShortestPaths() {
269 ImmutableMap.Builder<DeviceId, Result<TopologyVertex, TopologyEdge>> builder = ImmutableMap.builder();
270
271 // Search graph paths for each source to all destinations.
272 LinkWeight weight = new HopCountLinkWeight(graph.getVertexes().size());
273 for (TopologyVertex src : graph.getVertexes()) {
274 builder.put(src.deviceId(), DIJKSTRA.search(graph, src, null, weight));
275 }
276 return builder.build();
277 }
278
279 // Builds network paths from the graph path search results
280 private ImmutableSetMultimap<PathKey, Path> buildPaths() {
281 Builder<PathKey, Path> builder = ImmutableSetMultimap.builder();
282 for (DeviceId deviceId : results.keySet()) {
283 Result<TopologyVertex, TopologyEdge> result = results.get(deviceId);
284 for (org.onlab.graph.Path<TopologyVertex, TopologyEdge> path : result.paths()) {
285 builder.put(new PathKey(path.src().deviceId(), path.dst().deviceId()),
286 networkPath(path));
287 }
288 }
289 return builder.build();
290 }
291
292 // Converts graph path to a network path with the same cost.
293 private Path networkPath(org.onlab.graph.Path<TopologyVertex, TopologyEdge> path) {
294 List<Link> links = new ArrayList<>();
295 for (TopologyEdge edge : path.edges()) {
296 links.add(edge.link());
297 }
Thomas Vachuska6acd3bb2014-11-09 23:44:22 -0800298 return new DefaultPath(CORE_PROVIDER_ID, links, path.cost());
alshabib339a3d92014-09-26 17:54:32 -0700299 }
300
301
302 // Searches for SCC clusters in the network topology graph using Tarjan
303 // algorithm.
304 private SCCResult<TopologyVertex, TopologyEdge> searchForClusters() {
305 return TARJAN.search(graph, new NoIndirectLinksWeight());
306 }
307
308 // Builds the topology clusters and returns the id-cluster bindings.
309 private ImmutableMap<ClusterId, TopologyCluster> buildTopologyClusters() {
310 ImmutableMap.Builder<ClusterId, TopologyCluster> clusterBuilder = ImmutableMap.builder();
311 SCCResult<TopologyVertex, TopologyEdge> result =
312 TARJAN.search(graph, new NoIndirectLinksWeight());
313
314 // Extract both vertexes and edges from the results; the lists form
315 // pairs along the same index.
316 List<Set<TopologyVertex>> clusterVertexes = result.clusterVertexes();
317 List<Set<TopologyEdge>> clusterEdges = result.clusterEdges();
318
319 // Scan over the lists and create a cluster from the results.
320 for (int i = 0, n = result.clusterCount(); i < n; i++) {
321 Set<TopologyVertex> vertexSet = clusterVertexes.get(i);
322 Set<TopologyEdge> edgeSet = clusterEdges.get(i);
323
324 ClusterId cid = ClusterId.clusterId(i);
325 DefaultTopologyCluster cluster =
326 new DefaultTopologyCluster(cid, vertexSet.size(), edgeSet.size(),
327 findRoot(vertexSet).deviceId());
328 clusterBuilder.put(cid, cluster);
329 }
330 return clusterBuilder.build();
331 }
332
333 // Finds the vertex whose device id is the lexicographical minimum in the
334 // specified set.
335 private TopologyVertex findRoot(Set<TopologyVertex> vertexSet) {
336 TopologyVertex minVertex = null;
337 for (TopologyVertex vertex : vertexSet) {
338 if (minVertex == null ||
339 minVertex.deviceId().toString()
340 .compareTo(minVertex.deviceId().toString()) < 0) {
341 minVertex = vertex;
342 }
343 }
344 return minVertex;
345 }
346
347 // Processes a map of broadcast sets for each cluster.
348 private ImmutableSetMultimap<ClusterId, ConnectPoint> buildBroadcastSets() {
349 Builder<ClusterId, ConnectPoint> builder = ImmutableSetMultimap.builder();
350 for (TopologyCluster cluster : clusters.values()) {
351 addClusterBroadcastSet(cluster, builder);
352 }
353 return builder.build();
354 }
355
356 // Finds all broadcast points for the cluster. These are those connection
357 // points which lie along the shortest paths between the cluster root and
358 // all other devices within the cluster.
359 private void addClusterBroadcastSet(TopologyCluster cluster,
360 Builder<ClusterId, ConnectPoint> builder) {
361 // Use the graph root search results to build the broadcast set.
362 Result<TopologyVertex, TopologyEdge> result = results.get(cluster.root());
363 for (Map.Entry<TopologyVertex, Set<TopologyEdge>> entry : result.parents().entrySet()) {
364 TopologyVertex vertex = entry.getKey();
365
366 // Ignore any parents that lead outside the cluster.
367 if (clustersByDevice.get(vertex.deviceId()) != cluster) {
368 continue;
369 }
370
371 // Ignore any back-link sets that are empty.
372 Set<TopologyEdge> parents = entry.getValue();
373 if (parents.isEmpty()) {
374 continue;
375 }
376
377 // Use the first back-link source and destinations to add to the
378 // broadcast set.
379 Link link = parents.iterator().next().link();
380 builder.put(cluster.id(), link.src());
381 builder.put(cluster.id(), link.dst());
382 }
383 }
384
385 // Collects and returns an set of all infrastructure link end-points.
386 private ImmutableSet<ConnectPoint> findInfrastructurePoints() {
387 ImmutableSet.Builder<ConnectPoint> builder = ImmutableSet.builder();
388 for (TopologyEdge edge : graph.getEdges()) {
389 builder.add(edge.link().src());
390 builder.add(edge.link().dst());
391 }
392 return builder.build();
393 }
394
395 // Builds cluster-devices, cluster-links and device-cluster indexes.
396 private void buildIndexes() {
397 // Prepare the index builders
398 ImmutableMap.Builder<DeviceId, TopologyCluster> clusterBuilder = ImmutableMap.builder();
399 ImmutableSetMultimap.Builder<TopologyCluster, DeviceId> devicesBuilder = ImmutableSetMultimap.builder();
400 ImmutableSetMultimap.Builder<TopologyCluster, Link> linksBuilder = ImmutableSetMultimap.builder();
401
402 // Now scan through all the clusters
403 for (TopologyCluster cluster : clusters.values()) {
404 int i = cluster.id().index();
405
406 // Scan through all the cluster vertexes.
407 for (TopologyVertex vertex : clusterResults.clusterVertexes().get(i)) {
408 devicesBuilder.put(cluster, vertex.deviceId());
409 clusterBuilder.put(vertex.deviceId(), cluster);
410 }
411
412 // Scan through all the cluster edges.
413 for (TopologyEdge edge : clusterResults.clusterEdges().get(i)) {
414 linksBuilder.put(cluster, edge.link());
415 }
416 }
417
418 // Finalize all indexes.
419 clustersByDevice = clusterBuilder.build();
420 devicesByCluster = devicesBuilder.build();
421 linksByCluster = linksBuilder.build();
422 }
423
424 // Link weight for measuring link cost as hop count with indirect links
425 // being as expensive as traversing the entire graph to assume the worst.
426 private static class HopCountLinkWeight implements LinkWeight {
427 private final int indirectLinkCost;
428
429 HopCountLinkWeight(int indirectLinkCost) {
430 this.indirectLinkCost = indirectLinkCost;
431 }
432
433 @Override
434 public double weight(TopologyEdge edge) {
435 // To force preference to use direct paths first, make indirect
436 // links as expensive as the linear vertex traversal.
437 return edge.link().type() == INDIRECT ? indirectLinkCost : 1;
438 }
439 }
440
441 // Link weight for preventing traversal over indirect links.
442 private static class NoIndirectLinksWeight implements LinkWeight {
443 @Override
444 public double weight(TopologyEdge edge) {
445 return edge.link().type() == INDIRECT ? -1 : 1;
446 }
447 }
448
449 @Override
450 public String toString() {
451 return toStringHelper(this)
452 .add("time", time)
453 .add("clusters", clusterCount())
454 .add("devices", deviceCount())
455 .add("links", linkCount())
456 .add("pathCount", pathCount())
457 .toString();
458 }
459}