blob: 9a232c145653f315bdb02a66e847a475134144ef [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;
51import static org.onlab.onos.net.Link.Type.INDIRECT;
52
tomdc361b62014-09-09 20:36:52 -070053/**
54 * Default implementation of the topology descriptor. This carries the
55 * backing topology data.
56 */
57public class DefaultTopology extends AbstractModel implements Topology {
58
tom97937552014-09-11 10:48:42 -070059 private static final DijkstraGraphSearch<TopologyVertex, TopologyEdge> DIJKSTRA =
60 new DijkstraGraphSearch<>();
61 private static final TarjanGraphSearch<TopologyVertex, TopologyEdge> TARJAN =
62 new TarjanGraphSearch<>();
63
tom7e02cda2014-09-18 12:05:46 -070064 private static final ProviderId PID = new ProviderId("core", "org.onlab.onos.net");
tom97937552014-09-11 10:48:42 -070065
tomdc361b62014-09-09 20:36:52 -070066 private final long time;
tom97937552014-09-11 10:48:42 -070067 private final TopologyGraph graph;
tomdc361b62014-09-09 20:36:52 -070068
tom97937552014-09-11 10:48:42 -070069 private final SCCResult<TopologyVertex, TopologyEdge> clusterResults;
70 private final ImmutableMap<DeviceId, Result<TopologyVertex, TopologyEdge>> results;
71 private final ImmutableSetMultimap<PathKey, Path> paths;
tome52ce702014-09-11 00:12:54 -070072
tom97937552014-09-11 10:48:42 -070073 private final ImmutableMap<ClusterId, TopologyCluster> clusters;
74 private final ImmutableSet<ConnectPoint> infrastructurePoints;
75 private final ImmutableSetMultimap<ClusterId, ConnectPoint> broadcastSets;
76
77 private ImmutableMap<DeviceId, TopologyCluster> clustersByDevice;
78 private ImmutableSetMultimap<TopologyCluster, DeviceId> devicesByCluster;
79 private ImmutableSetMultimap<TopologyCluster, Link> linksByCluster;
80
tome52ce702014-09-11 00:12:54 -070081
tomdc361b62014-09-09 20:36:52 -070082 /**
83 * Creates a topology descriptor attributed to the specified provider.
84 *
tome52ce702014-09-11 00:12:54 -070085 * @param providerId identity of the provider
86 * @param description data describing the new topology
tomdc361b62014-09-09 20:36:52 -070087 */
tom97937552014-09-11 10:48:42 -070088 DefaultTopology(ProviderId providerId, GraphDescription description) {
tomdc361b62014-09-09 20:36:52 -070089 super(providerId);
tome52ce702014-09-11 00:12:54 -070090 this.time = description.timestamp();
tome52ce702014-09-11 00:12:54 -070091
tom97937552014-09-11 10:48:42 -070092 // Build the graph
93 this.graph = new DefaultTopologyGraph(description.vertexes(),
94 description.edges());
95
96 this.results = searchForShortestPaths();
97 this.paths = buildPaths();
98
99 this.clusterResults = searchForClusters();
100 this.clusters = buildTopologyClusters();
101
102 buildIndexes();
103
104 this.broadcastSets = buildBroadcastSets();
105 this.infrastructurePoints = findInfrastructurePoints();
tomdc361b62014-09-09 20:36:52 -0700106 }
107
108 @Override
109 public long time() {
110 return time;
111 }
112
113 @Override
114 public int clusterCount() {
tome52ce702014-09-11 00:12:54 -0700115 return clusters.size();
tomdc361b62014-09-09 20:36:52 -0700116 }
117
118 @Override
119 public int deviceCount() {
tome52ce702014-09-11 00:12:54 -0700120 return graph.getVertexes().size();
tomdc361b62014-09-09 20:36:52 -0700121 }
122
123 @Override
124 public int linkCount() {
tome52ce702014-09-11 00:12:54 -0700125 return graph.getEdges().size();
tomdc361b62014-09-09 20:36:52 -0700126 }
127
128 @Override
129 public int pathCount() {
tom97937552014-09-11 10:48:42 -0700130 return paths.size();
tomdc361b62014-09-09 20:36:52 -0700131 }
132
tom97937552014-09-11 10:48:42 -0700133 /**
134 * Returns the backing topology graph.
135 *
136 * @return topology graph
137 */
138 TopologyGraph getGraph() {
tome52ce702014-09-11 00:12:54 -0700139 return graph;
140 }
141
tom97937552014-09-11 10:48:42 -0700142 /**
143 * Returns the set of topology clusters.
144 *
145 * @return set of clusters
146 */
147 Set<TopologyCluster> getClusters() {
148 return ImmutableSet.copyOf(clusters.values());
149 }
150
151 /**
tom13cb4852014-09-11 12:44:17 -0700152 * Returns the specified topology cluster.
153 *
154 * @param clusterId cluster identifier
155 * @return topology cluster
156 */
157 TopologyCluster getCluster(ClusterId clusterId) {
158 return clusters.get(clusterId);
159 }
160
tomdc95b8a2014-09-17 08:07:26 -0700161 /**
162 * Returns the topology cluster that contains the given device.
163 *
164 * @param deviceId device identifier
165 * @return topology cluster
166 */
167 TopologyCluster getCluster(DeviceId deviceId) {
168 return clustersByDevice.get(deviceId);
169 }
tom13cb4852014-09-11 12:44:17 -0700170
171 /**
tom97937552014-09-11 10:48:42 -0700172 * Returns the set of cluster devices.
173 *
174 * @param cluster topology cluster
175 * @return cluster devices
176 */
177 Set<DeviceId> getClusterDevices(TopologyCluster cluster) {
178 return devicesByCluster.get(cluster);
179 }
180
181 /**
182 * Returns the set of cluster links.
183 *
184 * @param cluster topology cluster
185 * @return cluster links
186 */
187 Set<Link> getClusterLinks(TopologyCluster cluster) {
188 return linksByCluster.get(cluster);
189 }
190
191 /**
192 * Indicates whether the given point is an infrastructure link end-point.
193 *
194 * @param connectPoint connection point
195 * @return true if infrastructure
196 */
tome52ce702014-09-11 00:12:54 -0700197 boolean isInfrastructure(ConnectPoint connectPoint) {
tom97937552014-09-11 10:48:42 -0700198 return infrastructurePoints.contains(connectPoint);
tome52ce702014-09-11 00:12:54 -0700199 }
200
tom97937552014-09-11 10:48:42 -0700201 /**
tomdc95b8a2014-09-17 08:07:26 -0700202 * Indicates whether the given point is part of a broadcast set.
tom97937552014-09-11 10:48:42 -0700203 *
204 * @param connectPoint connection point
tomdc95b8a2014-09-17 08:07:26 -0700205 * @return true if in broadcast set
tom97937552014-09-11 10:48:42 -0700206 */
tomdc95b8a2014-09-17 08:07:26 -0700207 boolean isBroadcastPoint(ConnectPoint connectPoint) {
208 // Any non-infrastructure, i.e. edge points are assumed to be OK.
tom97937552014-09-11 10:48:42 -0700209 if (!isInfrastructure(connectPoint)) {
210 return true;
211 }
212
213 // Find the cluster to which the device belongs.
214 TopologyCluster cluster = clustersByDevice.get(connectPoint.deviceId());
215 if (cluster == null) {
216 throw new IllegalArgumentException("No cluster found for device " + connectPoint.deviceId());
217 }
218
tomdc95b8a2014-09-17 08:07:26 -0700219 // If the broadcast set is null or empty, or if the point explicitly
220 // belongs to it, return true;
tom97937552014-09-11 10:48:42 -0700221 Set<ConnectPoint> points = broadcastSets.get(cluster.id());
222 return points == null || points.isEmpty() || points.contains(connectPoint);
tome52ce702014-09-11 00:12:54 -0700223 }
224
tom97937552014-09-11 10:48:42 -0700225 /**
tomdc95b8a2014-09-17 08:07:26 -0700226 * Returns the size of the cluster broadcast set.
227 *
228 * @param clusterId cluster identifier
229 * @return size of the cluster broadcast set
230 */
231 int broadcastSetSize(ClusterId clusterId) {
232 return broadcastSets.get(clusterId).size();
233 }
234
235 /**
tom97937552014-09-11 10:48:42 -0700236 * Returns the set of pre-computed shortest paths between source and
237 * destination devices.
238 *
239 * @param src source device
240 * @param dst destination device
241 * @return set of shortest paths
242 */
tome52ce702014-09-11 00:12:54 -0700243 Set<Path> getPaths(DeviceId src, DeviceId dst) {
tom97937552014-09-11 10:48:42 -0700244 return paths.get(new PathKey(src, dst));
245 }
246
247 /**
248 * Computes on-demand the set of shortest paths between source and
249 * destination devices.
250 *
251 * @param src source device
252 * @param dst destination device
253 * @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 }
298 return new DefaultPath(PID, links, path.cost());
299 }
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();
tome52ce702014-09-11 00:12:54 -0700458 }
tomdc361b62014-09-09 20:36:52 -0700459}