blob: dfc3ad6da709d0b928d4c9f5bc9fced3485f6eaa [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
26
27 /**
28 * Creates a minimal topology graph description to allow core to construct
29 * and process the topology graph.
30 *
tom27ae0e62014-10-01 20:35:01 -070031 * @param nanos time in nanos of when the topology description was created
32 * @param devices collection of infrastructure devices
33 * @param links collection of infrastructure links
34 * @param annotations optional key/value annotations map
tom97937552014-09-11 10:48:42 -070035 */
tom27ae0e62014-10-01 20:35:01 -070036 public DefaultGraphDescription(long nanos, Iterable<Device> devices,
37 Iterable<Link> links,
tomf5d85d42014-10-02 05:27:56 -070038 SparseAnnotations... annotations) {
tom27ae0e62014-10-01 20:35:01 -070039 super(annotations);
tom97937552014-09-11 10:48:42 -070040 this.nanos = nanos;
41 this.vertexes = buildVertexes(devices);
42 this.edges = buildEdges(links);
43 vertexesById.clear();
44 }
45
46 @Override
47 public long timestamp() {
48 return nanos;
49 }
50
51 @Override
52 public ImmutableSet<TopologyVertex> vertexes() {
53 return vertexes;
54 }
55
56 @Override
57 public ImmutableSet<TopologyEdge> edges() {
58 return edges;
59 }
60
61 // Builds a set of topology vertexes from the specified list of devices
62 private ImmutableSet<TopologyVertex> buildVertexes(Iterable<Device> devices) {
63 ImmutableSet.Builder<TopologyVertex> vertexes = ImmutableSet.builder();
64 for (Device device : devices) {
65 TopologyVertex vertex = new DefaultTopologyVertex(device.id());
66 vertexes.add(vertex);
67 vertexesById.put(vertex.deviceId(), vertex);
68 }
69 return vertexes.build();
70 }
71
72 // Builds a set of topology vertexes from the specified list of links
73 private ImmutableSet<TopologyEdge> buildEdges(Iterable<Link> links) {
74 ImmutableSet.Builder<TopologyEdge> edges = ImmutableSet.builder();
75 for (Link link : links) {
76 edges.add(new DefaultTopologyEdge(vertexOf(link.src()),
77 vertexOf(link.dst()), link));
78 }
79 return edges.build();
80 }
81
82 // Fetches a vertex corresponding to the given connection point device.
83 private TopologyVertex vertexOf(ConnectPoint connectPoint) {
84 DeviceId id = connectPoint.deviceId();
85 TopologyVertex vertex = vertexesById.get(id);
86 if (vertex == null) {
87 // If vertex does not exist, create one and register it.
88 vertex = new DefaultTopologyVertex(id);
89 vertexesById.put(id, vertex);
90 }
91 return vertex;
92 }
93
94}