blob: b723f11555d3386b87895ff082e5db7f7e92e3da [file] [log] [blame]
tombe988312014-09-19 18:38:47 -07001package org.onlab.onos.net.topology;
tom97937552014-09-11 10:48:42 -07002
3import com.google.common.collect.ImmutableSet;
4import com.google.common.collect.Maps;
tomf5d85d42014-10-02 05:27:56 -07005import org.onlab.onos.net.AbstractDescription;
tom97937552014-09-11 10:48:42 -07006import org.onlab.onos.net.ConnectPoint;
7import org.onlab.onos.net.Device;
8import org.onlab.onos.net.DeviceId;
9import org.onlab.onos.net.Link;
tomf5d85d42014-10-02 05:27:56 -070010import org.onlab.onos.net.SparseAnnotations;
tom97937552014-09-11 10:48:42 -070011
12import java.util.Map;
13
14/**
15 * Default implementation of an immutable topology graph data carrier.
16 */
tomf5d85d42014-10-02 05:27:56 -070017public class DefaultGraphDescription extends AbstractDescription
tom27ae0e62014-10-01 20:35:01 -070018 implements GraphDescription {
tom97937552014-09-11 10:48:42 -070019
20 private final long nanos;
21 private final ImmutableSet<TopologyVertex> vertexes;
22 private final ImmutableSet<TopologyEdge> edges;
23
24 private final Map<DeviceId, TopologyVertex> vertexesById = Maps.newHashMap();
25
tom97937552014-09-11 10:48:42 -070026 /**
27 * Creates a minimal topology graph description to allow core to construct
28 * and process the topology graph.
29 *
tom27ae0e62014-10-01 20:35:01 -070030 * @param nanos time in nanos of when the topology description was created
31 * @param devices collection of infrastructure devices
32 * @param links collection of infrastructure links
33 * @param annotations optional key/value annotations map
tom97937552014-09-11 10:48:42 -070034 */
tom27ae0e62014-10-01 20:35:01 -070035 public DefaultGraphDescription(long nanos, Iterable<Device> devices,
36 Iterable<Link> links,
tomf5d85d42014-10-02 05:27:56 -070037 SparseAnnotations... annotations) {
tom27ae0e62014-10-01 20:35:01 -070038 super(annotations);
tom97937552014-09-11 10:48:42 -070039 this.nanos = nanos;
40 this.vertexes = buildVertexes(devices);
41 this.edges = buildEdges(links);
42 vertexesById.clear();
43 }
44
45 @Override
46 public long timestamp() {
47 return nanos;
48 }
49
50 @Override
51 public ImmutableSet<TopologyVertex> vertexes() {
52 return vertexes;
53 }
54
55 @Override
56 public ImmutableSet<TopologyEdge> edges() {
57 return edges;
58 }
59
60 // Builds a set of topology vertexes from the specified list of devices
61 private ImmutableSet<TopologyVertex> buildVertexes(Iterable<Device> devices) {
62 ImmutableSet.Builder<TopologyVertex> vertexes = ImmutableSet.builder();
63 for (Device device : devices) {
64 TopologyVertex vertex = new DefaultTopologyVertex(device.id());
65 vertexes.add(vertex);
66 vertexesById.put(vertex.deviceId(), vertex);
67 }
68 return vertexes.build();
69 }
70
71 // Builds a set of topology vertexes from the specified list of links
72 private ImmutableSet<TopologyEdge> buildEdges(Iterable<Link> links) {
73 ImmutableSet.Builder<TopologyEdge> edges = ImmutableSet.builder();
74 for (Link link : links) {
75 edges.add(new DefaultTopologyEdge(vertexOf(link.src()),
76 vertexOf(link.dst()), link));
77 }
78 return edges.build();
79 }
80
81 // Fetches a vertex corresponding to the given connection point device.
82 private TopologyVertex vertexOf(ConnectPoint connectPoint) {
83 DeviceId id = connectPoint.deviceId();
84 TopologyVertex vertex = vertexesById.get(id);
85 if (vertex == null) {
tom747a2132014-10-02 08:18:41 -070086 throw new IllegalArgumentException("Vertex missing for " + id);
tom97937552014-09-11 10:48:42 -070087 }
88 return vertex;
89 }
90
91}