tom | 8bf2e6b | 2014-09-10 20:53:54 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net.trivial.topology.impl; |
tom | 0efbb1d | 2014-09-09 11:54:28 -0700 | [diff] [blame] | 2 | |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 3 | import org.onlab.onos.event.Event; |
| 4 | import org.onlab.onos.net.ConnectPoint; |
| 5 | import org.onlab.onos.net.DeviceId; |
| 6 | import org.onlab.onos.net.Path; |
tom | e52ce70 | 2014-09-11 00:12:54 -0700 | [diff] [blame] | 7 | import org.onlab.onos.net.provider.ProviderId; |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 8 | import org.onlab.onos.net.topology.GraphDescription; |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 9 | import org.onlab.onos.net.topology.LinkWeight; |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 10 | import org.onlab.onos.net.topology.Topology; |
| 11 | import org.onlab.onos.net.topology.TopologyCluster; |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 12 | import org.onlab.onos.net.topology.TopologyEvent; |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 13 | import org.onlab.onos.net.topology.TopologyGraph; |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 14 | |
| 15 | import java.util.List; |
| 16 | import java.util.Set; |
| 17 | |
tom | 0efbb1d | 2014-09-09 11:54:28 -0700 | [diff] [blame] | 18 | /** |
| 19 | * Manages inventory of topology snapshots using trivial in-memory |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 20 | * structures implementation. |
tom | 0efbb1d | 2014-09-09 11:54:28 -0700 | [diff] [blame] | 21 | */ |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 22 | class SimpleTopologyStore { |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 23 | |
| 24 | private volatile DefaultTopology current; |
| 25 | |
| 26 | /** |
| 27 | * Returns the current topology snapshot. |
| 28 | * |
| 29 | * @return current topology descriptor |
| 30 | */ |
| 31 | Topology currentTopology() { |
| 32 | return current; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Indicates whether the topology is the latest. |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 37 | * |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 38 | * @param topology topology descriptor |
| 39 | * @return true if topology is the most recent one |
| 40 | */ |
| 41 | boolean isLatest(Topology topology) { |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 42 | // Topology is current only if it is the same as our current topology |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 43 | return topology == current; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Returns the set of topology SCC clusters. |
| 48 | * |
| 49 | * @param topology topology descriptor |
| 50 | * @return set of clusters |
| 51 | */ |
tom | e52ce70 | 2014-09-11 00:12:54 -0700 | [diff] [blame] | 52 | Set<TopologyCluster> getClusters(DefaultTopology topology) { |
| 53 | return topology.getClusters(); |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Returns the immutable graph view of the current topology. |
| 58 | * |
| 59 | * @param topology topology descriptor |
| 60 | * @return graph view |
| 61 | */ |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 62 | TopologyGraph getGraph(DefaultTopology topology) { |
tom | e52ce70 | 2014-09-11 00:12:54 -0700 | [diff] [blame] | 63 | return topology.getGraph(); |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Returns the set of pre-computed shortest paths between src and dest. |
| 68 | * |
| 69 | * @param topology topology descriptor |
| 70 | * @param src source device |
| 71 | * @param dst destination device |
| 72 | * @return set of shortest paths |
| 73 | */ |
tom | e52ce70 | 2014-09-11 00:12:54 -0700 | [diff] [blame] | 74 | Set<Path> getPaths(DefaultTopology topology, DeviceId src, DeviceId dst) { |
| 75 | return topology.getPaths(src, dst); |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Computes and returns the set of shortest paths between src and dest. |
| 80 | * |
| 81 | * @param topology topology descriptor |
| 82 | * @param src source device |
| 83 | * @param dst destination device |
| 84 | * @param weight link weight function |
| 85 | * @return set of shortest paths |
| 86 | */ |
tom | e52ce70 | 2014-09-11 00:12:54 -0700 | [diff] [blame] | 87 | Set<Path> getPaths(DefaultTopology topology, DeviceId src, DeviceId dst, |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 88 | LinkWeight weight) { |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 89 | return topology.getPaths(src, dst, weight); |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Indicates whether the given connect point is part of the network fabric. |
| 94 | * |
| 95 | * @param topology topology descriptor |
| 96 | * @param connectPoint connection point |
| 97 | * @return true if infrastructure; false otherwise |
| 98 | */ |
tom | e52ce70 | 2014-09-11 00:12:54 -0700 | [diff] [blame] | 99 | boolean isInfrastructure(DefaultTopology topology, ConnectPoint connectPoint) { |
| 100 | return topology.isInfrastructure(connectPoint); |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Indicates whether the given connect point is part of the broadcast tree. |
| 105 | * |
| 106 | * @param topology topology descriptor |
| 107 | * @param connectPoint connection point |
| 108 | * @return true if in broadcast tree; false otherwise |
| 109 | */ |
tom | e52ce70 | 2014-09-11 00:12:54 -0700 | [diff] [blame] | 110 | boolean isInBroadcastTree(DefaultTopology topology, ConnectPoint connectPoint) { |
| 111 | return topology.isInBroadcastTree(connectPoint); |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Generates a new topology snapshot from the specified description. |
| 116 | * |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 117 | * @param providerId provider identification |
| 118 | * @param graphDescription topology graph description |
| 119 | * @param reasons list of events that triggered the update |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 120 | * @return topology update event or null if the description is old |
| 121 | */ |
tom | e52ce70 | 2014-09-11 00:12:54 -0700 | [diff] [blame] | 122 | TopologyEvent updateTopology(ProviderId providerId, |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 123 | GraphDescription graphDescription, |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 124 | List<Event> reasons) { |
tom | e52ce70 | 2014-09-11 00:12:54 -0700 | [diff] [blame] | 125 | // First off, make sure that what we're given is indeed newer than |
| 126 | // what we already have. |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 127 | if (current != null && graphDescription.timestamp() < current.time()) { |
tom | e52ce70 | 2014-09-11 00:12:54 -0700 | [diff] [blame] | 128 | return null; |
| 129 | } |
| 130 | |
| 131 | // Have the default topology construct self from the description data. |
| 132 | DefaultTopology newTopology = |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 133 | new DefaultTopology(providerId, graphDescription); |
tom | e52ce70 | 2014-09-11 00:12:54 -0700 | [diff] [blame] | 134 | |
| 135 | // Promote the new topology to current and return a ready-to-send event. |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 136 | synchronized (this) { |
| 137 | current = newTopology; |
| 138 | return new TopologyEvent(TopologyEvent.Type.TOPOLOGY_CHANGED, current); |
| 139 | } |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 140 | } |
| 141 | |
tom | 0efbb1d | 2014-09-09 11:54:28 -0700 | [diff] [blame] | 142 | } |