blob: 59ef8df8cea3449d616c392a9fa03ec158355727 [file] [log] [blame]
tom97937552014-09-11 10:48:42 -07001package org.onlab.onos.net.trivial.topology.impl;
2
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;
9import org.onlab.onos.net.topology.GraphDescription;
10import org.onlab.onos.net.topology.TopologyEdge;
11import org.onlab.onos.net.topology.TopologyVertex;
12
13import java.util.Map;
14
15/**
16 * Default implementation of an immutable topology graph data carrier.
17 */
18class DefaultGraphDescription implements GraphDescription {
19
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 *
31 * @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 */
35 DefaultGraphDescription(long nanos, Iterable<Device> devices, Iterable<Link> links) {
36 this.nanos = nanos;
37 this.vertexes = buildVertexes(devices);
38 this.edges = buildEdges(links);
39 vertexesById.clear();
40 }
41
42 @Override
43 public long timestamp() {
44 return nanos;
45 }
46
47 @Override
48 public ImmutableSet<TopologyVertex> vertexes() {
49 return vertexes;
50 }
51
52 @Override
53 public ImmutableSet<TopologyEdge> edges() {
54 return edges;
55 }
56
57 // Builds a set of topology vertexes from the specified list of devices
58 private ImmutableSet<TopologyVertex> buildVertexes(Iterable<Device> devices) {
59 ImmutableSet.Builder<TopologyVertex> vertexes = ImmutableSet.builder();
60 for (Device device : devices) {
61 TopologyVertex vertex = new DefaultTopologyVertex(device.id());
62 vertexes.add(vertex);
63 vertexesById.put(vertex.deviceId(), vertex);
64 }
65 return vertexes.build();
66 }
67
68 // Builds a set of topology vertexes from the specified list of links
69 private ImmutableSet<TopologyEdge> buildEdges(Iterable<Link> links) {
70 ImmutableSet.Builder<TopologyEdge> edges = ImmutableSet.builder();
71 for (Link link : links) {
72 edges.add(new DefaultTopologyEdge(vertexOf(link.src()),
73 vertexOf(link.dst()), link));
74 }
75 return edges.build();
76 }
77
78 // Fetches a vertex corresponding to the given connection point device.
79 private TopologyVertex vertexOf(ConnectPoint connectPoint) {
80 DeviceId id = connectPoint.deviceId();
81 TopologyVertex vertex = vertexesById.get(id);
82 if (vertex == null) {
83 // If vertex does not exist, create one and register it.
84 vertex = new DefaultTopologyVertex(id);
85 vertexesById.put(id, vertex);
86 }
87 return vertex;
88 }
89
90}