blob: 94a6abf585e2ff732aa8f269f818d3ae26954305 [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;
5import org.onlab.onos.net.ConnectPoint;
6import org.onlab.onos.net.Device;
7import org.onlab.onos.net.DeviceId;
8import org.onlab.onos.net.Link;
tom97937552014-09-11 10:48:42 -07009
10import java.util.Map;
11
12/**
13 * Default implementation of an immutable topology graph data carrier.
14 */
tombe988312014-09-19 18:38:47 -070015public class DefaultGraphDescription implements GraphDescription {
tom97937552014-09-11 10:48:42 -070016
17 private final long nanos;
18 private final ImmutableSet<TopologyVertex> vertexes;
19 private final ImmutableSet<TopologyEdge> edges;
20
21 private final Map<DeviceId, TopologyVertex> vertexesById = Maps.newHashMap();
22
23
24 /**
25 * Creates a minimal topology graph description to allow core to construct
26 * and process the topology graph.
27 *
28 * @param nanos time in nanos of when the topology description was created
29 * @param devices collection of infrastructure devices
30 * @param links collection of infrastructure links
31 */
tombe988312014-09-19 18:38:47 -070032 public DefaultGraphDescription(long nanos, Iterable<Device> devices, Iterable<Link> links) {
tom97937552014-09-11 10:48:42 -070033 this.nanos = nanos;
34 this.vertexes = buildVertexes(devices);
35 this.edges = buildEdges(links);
36 vertexesById.clear();
37 }
38
39 @Override
40 public long timestamp() {
41 return nanos;
42 }
43
44 @Override
45 public ImmutableSet<TopologyVertex> vertexes() {
46 return vertexes;
47 }
48
49 @Override
50 public ImmutableSet<TopologyEdge> edges() {
51 return edges;
52 }
53
54 // Builds a set of topology vertexes from the specified list of devices
55 private ImmutableSet<TopologyVertex> buildVertexes(Iterable<Device> devices) {
56 ImmutableSet.Builder<TopologyVertex> vertexes = ImmutableSet.builder();
57 for (Device device : devices) {
58 TopologyVertex vertex = new DefaultTopologyVertex(device.id());
59 vertexes.add(vertex);
60 vertexesById.put(vertex.deviceId(), vertex);
61 }
62 return vertexes.build();
63 }
64
65 // Builds a set of topology vertexes from the specified list of links
66 private ImmutableSet<TopologyEdge> buildEdges(Iterable<Link> links) {
67 ImmutableSet.Builder<TopologyEdge> edges = ImmutableSet.builder();
68 for (Link link : links) {
69 edges.add(new DefaultTopologyEdge(vertexOf(link.src()),
70 vertexOf(link.dst()), link));
71 }
72 return edges.build();
73 }
74
75 // Fetches a vertex corresponding to the given connection point device.
76 private TopologyVertex vertexOf(ConnectPoint connectPoint) {
77 DeviceId id = connectPoint.deviceId();
78 TopologyVertex vertex = vertexesById.get(id);
79 if (vertex == null) {
80 // If vertex does not exist, create one and register it.
81 vertex = new DefaultTopologyVertex(id);
82 vertexesById.put(id, vertex);
83 }
84 return vertex;
85 }
86
87}