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 | |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 26 | /** |
| 27 | * Creates a minimal topology graph description to allow core to construct |
| 28 | * and process the topology graph. |
| 29 | * |
tom | 27ae0e6 | 2014-10-01 20:35:01 -0700 | [diff] [blame] | 30 | * @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 |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 34 | */ |
tom | 27ae0e6 | 2014-10-01 20:35:01 -0700 | [diff] [blame] | 35 | public DefaultGraphDescription(long nanos, Iterable<Device> devices, |
| 36 | Iterable<Link> links, |
tom | f5d85d4 | 2014-10-02 05:27:56 -0700 | [diff] [blame] | 37 | SparseAnnotations... annotations) { |
tom | 27ae0e6 | 2014-10-01 20:35:01 -0700 | [diff] [blame] | 38 | super(annotations); |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 39 | 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) { |
tom | 747a213 | 2014-10-02 08:18:41 -0700 | [diff] [blame^] | 86 | throw new IllegalArgumentException("Vertex missing for " + id); |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 87 | } |
| 88 | return vertex; |
| 89 | } |
| 90 | |
| 91 | } |