tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net.topology; |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 2 | |
| 3 | import com.google.common.collect.ImmutableSet; |
| 4 | import com.google.common.collect.Maps; |
tom | f5d85d4 | 2014-10-02 05:27:56 -0700 | [diff] [blame] | 5 | import org.onlab.onos.net.AbstractDescription; |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 6 | import org.onlab.onos.net.ConnectPoint; |
| 7 | import org.onlab.onos.net.Device; |
| 8 | import org.onlab.onos.net.DeviceId; |
| 9 | import org.onlab.onos.net.Link; |
tom | f5d85d4 | 2014-10-02 05:27:56 -0700 | [diff] [blame] | 10 | import org.onlab.onos.net.SparseAnnotations; |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 11 | |
| 12 | import java.util.Map; |
| 13 | |
| 14 | /** |
| 15 | * Default implementation of an immutable topology graph data carrier. |
| 16 | */ |
tom | f5d85d4 | 2014-10-02 05:27:56 -0700 | [diff] [blame] | 17 | public class DefaultGraphDescription extends AbstractDescription |
tom | 27ae0e6 | 2014-10-01 20:35:01 -0700 | [diff] [blame] | 18 | implements GraphDescription { |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 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 | * |
tom | 27ae0e6 | 2014-10-01 20:35:01 -0700 | [diff] [blame] | 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 | * @param annotations optional key/value annotations map |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 35 | */ |
tom | 27ae0e6 | 2014-10-01 20:35:01 -0700 | [diff] [blame] | 36 | public DefaultGraphDescription(long nanos, Iterable<Device> devices, |
| 37 | Iterable<Link> links, |
tom | f5d85d4 | 2014-10-02 05:27:56 -0700 | [diff] [blame] | 38 | SparseAnnotations... annotations) { |
tom | 27ae0e6 | 2014-10-01 20:35:01 -0700 | [diff] [blame] | 39 | super(annotations); |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 40 | 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 | } |