blob: 4aafa35f372ca0d3b472e0fb5b405ed80748df39 [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;
68 private final TopologyGraph graph;
69
70 private final SCCResult<TopologyVertex, TopologyEdge> clusterResults;
71 private final ImmutableMap<DeviceId, Result<TopologyVertex, TopologyEdge>> results;
72 private final ImmutableSetMultimap<PathKey, Path> paths;
73
74 private final ImmutableMap<ClusterId, TopologyCluster> clusters;
75 private final ImmutableSet<ConnectPoint> infrastructurePoints;
76 private final ImmutableSetMultimap<ClusterId, ConnectPoint> broadcastSets;
77
78 private ImmutableMap<DeviceId, TopologyCluster> clustersByDevice;
79 private ImmutableSetMultimap<TopologyCluster, DeviceId> devicesByCluster;
80 private ImmutableSetMultimap<TopologyCluster, Link> linksByCluster;
81
82
83 /**
84 * Creates a topology descriptor attributed to the specified provider.
85 *
86 * @param providerId identity of the provider
87 * @param description data describing the new topology
88 */
89 DefaultTopology(ProviderId providerId, GraphDescription description) {
90 super(providerId);
91 this.time = description.timestamp();
92
93 // Build the graph
94 this.graph = new DefaultTopologyGraph(description.vertexes(),
95 description.edges());
96
97 this.results = searchForShortestPaths();
98 this.paths = buildPaths();
99
100 this.clusterResults = searchForClusters();
101 this.clusters = buildTopologyClusters();
102
103 buildIndexes();
104
105 this.broadcastSets = buildBroadcastSets();
106 this.infrastructurePoints = findInfrastructurePoints();
107 }
108
109 @Override
110 public long time() {
111 return time;
112 }
113
114 @Override
115 public int clusterCount() {
116 return clusters.size();
117 }
118
119 @Override
120 public int deviceCount() {
121 return graph.getVertexes().size();
122 }
123
124 @Override
125 public int linkCount() {
126 return graph.getEdges().size();
127 }
128
129 @Override
130 public int pathCount() {
131 return paths.size();
132 }
133
134 /**
135 * Returns the backing topology graph.
136 *
137 * @return topology graph
138 */
139 TopologyGraph getGraph() {
140 return graph;
141 }
142
143 /**
144 * Returns the set of topology clusters.
145 *
146 * @return set of clusters
147 */
148 Set<TopologyCluster> getClusters() {
149 return ImmutableSet.copyOf(clusters.values());
150 }
151
152 /**
153 * Returns the specified topology cluster.
154 *
155 * @param clusterId cluster identifier
156 * @return topology cluster
157 */
158 TopologyCluster getCluster(ClusterId clusterId) {
159 return clusters.get(clusterId);
160 }
161
162 /**
163 * Returns the topology cluster that contains the given device.
164 *
165 * @param deviceId device identifier
166 * @return topology cluster
167 */
168 TopologyCluster getCluster(DeviceId deviceId) {
169 return clustersByDevice.get(deviceId);
170 }
171
172 /**
173 * Returns the set of cluster devices.
174 *
175 * @param cluster topology cluster
176 * @return cluster devices
177 */
178 Set<DeviceId> getClusterDevices(TopologyCluster cluster) {
179 return devicesByCluster.get(cluster);
180 }
181
182 /**
183 * Returns the set of cluster links.
184 *
185 * @param cluster topology cluster
186 * @return cluster links
187 */
188 Set<Link> getClusterLinks(TopologyCluster cluster) {
189 return linksByCluster.get(cluster);
190 }
191
192 /**
193 * Indicates whether the given point is an infrastructure link end-point.
194 *
195 * @param connectPoint connection point
196 * @return true if infrastructure
197 */
198 boolean isInfrastructure(ConnectPoint connectPoint) {
199 return infrastructurePoints.contains(connectPoint);
200 }
201
202 /**
203 * Indicates whether the given point is part of a broadcast set.
204 *
205 * @param connectPoint connection point
206 * @return true if in broadcast set
207 */
208 boolean isBroadcastPoint(ConnectPoint connectPoint) {
209 // Any non-infrastructure, i.e. edge points are assumed to be OK.
210 if (!isInfrastructure(connectPoint)) {
211 return true;
212 }
213
214 // Find the cluster to which the device belongs.
215 TopologyCluster cluster = clustersByDevice.get(connectPoint.deviceId());
216 if (cluster == null) {
217 throw new IllegalArgumentException("No cluster found for device " + connectPoint.deviceId());
218 }
219
220 // If the broadcast set is null or empty, or if the point explicitly
221 // belongs to it, return true;
222 Set<ConnectPoint> points = broadcastSets.get(cluster.id());
223 return points == null || points.isEmpty() || points.contains(connectPoint);
224 }
225
226 /**
227 * Returns the size of the cluster broadcast set.
228 *
229 * @param clusterId cluster identifier
230 * @return size of the cluster broadcast set
231 */
232 int broadcastSetSize(ClusterId clusterId) {
233 return broadcastSets.get(clusterId).size();
234 }
235
236 /**
237 * Returns the set of pre-computed shortest paths between source and
238 * destination devices.
239 *
240 * @param src source device
241 * @param dst destination device
242 * @return set of shortest paths
243 */
244 Set<Path> getPaths(DeviceId src, DeviceId dst) {
245 return paths.get(new PathKey(src, dst));
246 }
247
248 /**
249 * Computes on-demand the set of shortest paths between source and
250 * destination devices.
251 *
Thomas Vachuskab14c77a2014-11-04 18:08:01 -0800252 * @param src source device
253 * @param dst destination device
254 * @param weight link weight function
alshabib339a3d92014-09-26 17:54:32 -0700255 * @return set of shortest paths
256 */
257 Set<Path> getPaths(DeviceId src, DeviceId dst, LinkWeight weight) {
258 GraphPathSearch.Result<TopologyVertex, TopologyEdge> result =
259 DIJKSTRA.search(graph, new DefaultTopologyVertex(src),
260 new DefaultTopologyVertex(dst), weight);
261 ImmutableSet.Builder<Path> builder = ImmutableSet.builder();
262 for (org.onlab.graph.Path<TopologyVertex, TopologyEdge> path : result.paths()) {
263 builder.add(networkPath(path));
264 }
265 return builder.build();
266 }
267
268
269 // Searches the graph for all shortest paths and returns the search results.
270 private ImmutableMap<DeviceId, Result<TopologyVertex, TopologyEdge>> searchForShortestPaths() {
271 ImmutableMap.Builder<DeviceId, Result<TopologyVertex, TopologyEdge>> builder = ImmutableMap.builder();
272
273 // Search graph paths for each source to all destinations.
274 LinkWeight weight = new HopCountLinkWeight(graph.getVertexes().size());
275 for (TopologyVertex src : graph.getVertexes()) {
276 builder.put(src.deviceId(), DIJKSTRA.search(graph, src, null, weight));
277 }
278 return builder.build();
279 }
280
281 // Builds network paths from the graph path search results
282 private ImmutableSetMultimap<PathKey, Path> buildPaths() {
283 Builder<PathKey, Path> builder = ImmutableSetMultimap.builder();
284 for (DeviceId deviceId : results.keySet()) {
285 Result<TopologyVertex, TopologyEdge> result = results.get(deviceId);
286 for (org.onlab.graph.Path<TopologyVertex, TopologyEdge> path : result.paths()) {
287 builder.put(new PathKey(path.src().deviceId(), path.dst().deviceId()),
288 networkPath(path));
289 }
290 }
291 return builder.build();
292 }
293
294 // Converts graph path to a network path with the same cost.
295 private Path networkPath(org.onlab.graph.Path<TopologyVertex, TopologyEdge> path) {
296 List<Link> links = new ArrayList<>();
297 for (TopologyEdge edge : path.edges()) {
298 links.add(edge.link());
299 }
Thomas Vachuska6acd3bb2014-11-09 23:44:22 -0800300 return new DefaultPath(CORE_PROVIDER_ID, links, path.cost());
alshabib339a3d92014-09-26 17:54:32 -0700301 }
302
303
304 // Searches for SCC clusters in the network topology graph using Tarjan
305 // algorithm.
306 private SCCResult<TopologyVertex, TopologyEdge> searchForClusters() {
307 return TARJAN.search(graph, new NoIndirectLinksWeight());
308 }
309
310 // Builds the topology clusters and returns the id-cluster bindings.
311 private ImmutableMap<ClusterId, TopologyCluster> buildTopologyClusters() {
312 ImmutableMap.Builder<ClusterId, TopologyCluster> clusterBuilder = ImmutableMap.builder();
313 SCCResult<TopologyVertex, TopologyEdge> result =
314 TARJAN.search(graph, new NoIndirectLinksWeight());
315
316 // Extract both vertexes and edges from the results; the lists form
317 // pairs along the same index.
318 List<Set<TopologyVertex>> clusterVertexes = result.clusterVertexes();
319 List<Set<TopologyEdge>> clusterEdges = result.clusterEdges();
320
321 // Scan over the lists and create a cluster from the results.
322 for (int i = 0, n = result.clusterCount(); i < n; i++) {
323 Set<TopologyVertex> vertexSet = clusterVertexes.get(i);
324 Set<TopologyEdge> edgeSet = clusterEdges.get(i);
325
326 ClusterId cid = ClusterId.clusterId(i);
327 DefaultTopologyCluster cluster =
328 new DefaultTopologyCluster(cid, vertexSet.size(), edgeSet.size(),
329 findRoot(vertexSet).deviceId());
330 clusterBuilder.put(cid, cluster);
331 }
332 return clusterBuilder.build();
333 }
334
335 // Finds the vertex whose device id is the lexicographical minimum in the
336 // specified set.
337 private TopologyVertex findRoot(Set<TopologyVertex> vertexSet) {
338 TopologyVertex minVertex = null;
339 for (TopologyVertex vertex : vertexSet) {
340 if (minVertex == null ||
341 minVertex.deviceId().toString()
342 .compareTo(minVertex.deviceId().toString()) < 0) {
343 minVertex = vertex;
344 }
345 }
346 return minVertex;
347 }
348
349 // Processes a map of broadcast sets for each cluster.
350 private ImmutableSetMultimap<ClusterId, ConnectPoint> buildBroadcastSets() {
351 Builder<ClusterId, ConnectPoint> builder = ImmutableSetMultimap.builder();
352 for (TopologyCluster cluster : clusters.values()) {
353 addClusterBroadcastSet(cluster, builder);
354 }
355 return builder.build();
356 }
357
358 // Finds all broadcast points for the cluster. These are those connection
359 // points which lie along the shortest paths between the cluster root and
360 // all other devices within the cluster.
361 private void addClusterBroadcastSet(TopologyCluster cluster,
362 Builder<ClusterId, ConnectPoint> builder) {
363 // Use the graph root search results to build the broadcast set.
364 Result<TopologyVertex, TopologyEdge> result = results.get(cluster.root());
365 for (Map.Entry<TopologyVertex, Set<TopologyEdge>> entry : result.parents().entrySet()) {
366 TopologyVertex vertex = entry.getKey();
367
368 // Ignore any parents that lead outside the cluster.
369 if (clustersByDevice.get(vertex.deviceId()) != cluster) {
370 continue;
371 }
372
373 // Ignore any back-link sets that are empty.
374 Set<TopologyEdge> parents = entry.getValue();
375 if (parents.isEmpty()) {
376 continue;
377 }
378
379 // Use the first back-link source and destinations to add to the
380 // broadcast set.
381 Link link = parents.iterator().next().link();
382 builder.put(cluster.id(), link.src());
383 builder.put(cluster.id(), link.dst());
384 }
385 }
386
387 // Collects and returns an set of all infrastructure link end-points.
388 private ImmutableSet<ConnectPoint> findInfrastructurePoints() {
389 ImmutableSet.Builder<ConnectPoint> builder = ImmutableSet.builder();
390 for (TopologyEdge edge : graph.getEdges()) {
391 builder.add(edge.link().src());
392 builder.add(edge.link().dst());
393 }
394 return builder.build();
395 }
396
397 // Builds cluster-devices, cluster-links and device-cluster indexes.
398 private void buildIndexes() {
399 // Prepare the index builders
400 ImmutableMap.Builder<DeviceId, TopologyCluster> clusterBuilder = ImmutableMap.builder();
401 ImmutableSetMultimap.Builder<TopologyCluster, DeviceId> devicesBuilder = ImmutableSetMultimap.builder();
402 ImmutableSetMultimap.Builder<TopologyCluster, Link> linksBuilder = ImmutableSetMultimap.builder();
403
404 // Now scan through all the clusters
405 for (TopologyCluster cluster : clusters.values()) {
406 int i = cluster.id().index();
407
408 // Scan through all the cluster vertexes.
409 for (TopologyVertex vertex : clusterResults.clusterVertexes().get(i)) {
410 devicesBuilder.put(cluster, vertex.deviceId());
411 clusterBuilder.put(vertex.deviceId(), cluster);
412 }
413
414 // Scan through all the cluster edges.
415 for (TopologyEdge edge : clusterResults.clusterEdges().get(i)) {
416 linksBuilder.put(cluster, edge.link());
417 }
418 }
419
420 // Finalize all indexes.
421 clustersByDevice = clusterBuilder.build();
422 devicesByCluster = devicesBuilder.build();
423 linksByCluster = linksBuilder.build();
424 }
425
426 // Link weight for measuring link cost as hop count with indirect links
427 // being as expensive as traversing the entire graph to assume the worst.
428 private static class HopCountLinkWeight implements LinkWeight {
429 private final int indirectLinkCost;
430
431 HopCountLinkWeight(int indirectLinkCost) {
432 this.indirectLinkCost = indirectLinkCost;
433 }
434
435 @Override
436 public double weight(TopologyEdge edge) {
437 // To force preference to use direct paths first, make indirect
438 // links as expensive as the linear vertex traversal.
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800439 return edge.link().state() == ACTIVE ?
440 (edge.link().type() == INDIRECT ? indirectLinkCost : 1) : -1;
alshabib339a3d92014-09-26 17:54:32 -0700441 }
442 }
443
444 // Link weight for preventing traversal over indirect links.
445 private static class NoIndirectLinksWeight implements LinkWeight {
446 @Override
447 public double weight(TopologyEdge edge) {
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800448 return edge.link().state() == INACTIVE || edge.link().type() == INDIRECT ? -1 : 1;
alshabib339a3d92014-09-26 17:54:32 -0700449 }
450 }
451
452 @Override
453 public String toString() {
454 return toStringHelper(this)
455 .add("time", time)
456 .add("clusters", clusterCount())
457 .add("devices", deviceCount())
458 .add("links", linkCount())
459 .add("pathCount", pathCount())
460 .toString();
461 }
462}