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; |
| 5 | import org.onlab.onos.net.ConnectPoint; |
| 6 | import org.onlab.onos.net.Device; |
| 7 | import org.onlab.onos.net.DeviceId; |
| 8 | import org.onlab.onos.net.Link; |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 9 | |
| 10 | import java.util.Map; |
| 11 | |
| 12 | /** |
| 13 | * Default implementation of an immutable topology graph data carrier. |
| 14 | */ |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 15 | public class DefaultGraphDescription implements GraphDescription { |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 16 | |
| 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 | */ |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 32 | public DefaultGraphDescription(long nanos, Iterable<Device> devices, Iterable<Link> links) { |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 33 | 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 | } |