tom | 8bf2e6b | 2014-09-10 20:53:54 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net.trivial.topology.impl; |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 2 | |
| 3 | import org.apache.felix.scr.annotations.Activate; |
| 4 | import org.apache.felix.scr.annotations.Component; |
| 5 | import org.apache.felix.scr.annotations.Deactivate; |
| 6 | import org.apache.felix.scr.annotations.Reference; |
| 7 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 8 | import org.apache.felix.scr.annotations.Service; |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 9 | import org.onlab.onos.event.AbstractListenerRegistry; |
| 10 | import org.onlab.onos.event.Event; |
| 11 | import org.onlab.onos.event.EventDeliveryService; |
| 12 | import org.onlab.onos.net.ConnectPoint; |
| 13 | import org.onlab.onos.net.DeviceId; |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 14 | import org.onlab.onos.net.Link; |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 15 | import org.onlab.onos.net.Path; |
| 16 | import org.onlab.onos.net.provider.AbstractProviderRegistry; |
| 17 | import org.onlab.onos.net.provider.AbstractProviderService; |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 18 | import org.onlab.onos.net.topology.ClusterId; |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 19 | import org.onlab.onos.net.topology.GraphDescription; |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 20 | import org.onlab.onos.net.topology.LinkWeight; |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 21 | import org.onlab.onos.net.topology.Topology; |
| 22 | import org.onlab.onos.net.topology.TopologyCluster; |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 23 | import org.onlab.onos.net.topology.TopologyEvent; |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 24 | import org.onlab.onos.net.topology.TopologyGraph; |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 25 | import org.onlab.onos.net.topology.TopologyListener; |
| 26 | import org.onlab.onos.net.topology.TopologyProvider; |
| 27 | import org.onlab.onos.net.topology.TopologyProviderRegistry; |
| 28 | import org.onlab.onos.net.topology.TopologyProviderService; |
| 29 | import org.onlab.onos.net.topology.TopologyService; |
| 30 | import org.slf4j.Logger; |
| 31 | |
| 32 | import java.util.List; |
| 33 | import java.util.Set; |
| 34 | |
| 35 | import static com.google.common.base.Preconditions.checkNotNull; |
| 36 | import static org.slf4j.LoggerFactory.getLogger; |
| 37 | |
| 38 | /** |
| 39 | * Provides basic implementation of the topology SB & NB APIs. |
| 40 | */ |
| 41 | @Component(immediate = true) |
| 42 | @Service |
| 43 | public class SimpleTopologyManager |
| 44 | extends AbstractProviderRegistry<TopologyProvider, TopologyProviderService> |
| 45 | implements TopologyService, TopologyProviderRegistry { |
| 46 | |
| 47 | public static final String TOPOLOGY_NULL = "Topology cannot be null"; |
| 48 | private static final String DEVICE_ID_NULL = "Device ID cannot be null"; |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 49 | private static final String CLUSTER_ID_NULL = "Cluster ID cannot be null"; |
| 50 | private static final String CLUSTER_NULL = "Topology cluster cannot be null"; |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 51 | public static final String CONNECTION_POINT_NULL = "Connection point cannot be null"; |
| 52 | |
| 53 | private final Logger log = getLogger(getClass()); |
| 54 | |
| 55 | private final AbstractListenerRegistry<TopologyEvent, TopologyListener> |
| 56 | listenerRegistry = new AbstractListenerRegistry<>(); |
| 57 | |
tom | 0efbb1d | 2014-09-09 11:54:28 -0700 | [diff] [blame] | 58 | private final SimpleTopologyStore store = new SimpleTopologyStore(); |
| 59 | |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 60 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 61 | private EventDeliveryService eventDispatcher; |
| 62 | |
| 63 | |
| 64 | @Activate |
| 65 | public void activate() { |
| 66 | eventDispatcher.addSink(TopologyEvent.class, listenerRegistry); |
| 67 | log.info("Started"); |
| 68 | } |
| 69 | |
| 70 | @Deactivate |
| 71 | public void deactivate() { |
| 72 | eventDispatcher.removeSink(TopologyEvent.class); |
| 73 | log.info("Stopped"); |
| 74 | } |
| 75 | |
| 76 | @Override |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 77 | public Topology currentTopology() { |
| 78 | return store.currentTopology(); |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | @Override |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 82 | public boolean isLatest(Topology topology) { |
| 83 | checkNotNull(topology, TOPOLOGY_NULL); |
tom | e52ce70 | 2014-09-11 00:12:54 -0700 | [diff] [blame] | 84 | return store.isLatest(defaultTopology(topology)); |
| 85 | } |
| 86 | |
| 87 | // Validates the specified topology and returns it as a default |
| 88 | private DefaultTopology defaultTopology(Topology topology) { |
| 89 | if (topology instanceof DefaultTopology) { |
| 90 | return (DefaultTopology) topology; |
| 91 | } |
| 92 | throw new IllegalArgumentException("Topology class " + topology.getClass() + |
| 93 | " not supported"); |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public Set<TopologyCluster> getClusters(Topology topology) { |
| 98 | checkNotNull(topology, TOPOLOGY_NULL); |
tom | e52ce70 | 2014-09-11 00:12:54 -0700 | [diff] [blame] | 99 | return store.getClusters(defaultTopology(topology)); |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | @Override |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 103 | public TopologyCluster getCluster(Topology topology, ClusterId clusterId) { |
| 104 | checkNotNull(topology, TOPOLOGY_NULL); |
| 105 | checkNotNull(topology, CLUSTER_ID_NULL); |
| 106 | return store.getCluster(defaultTopology(topology), clusterId); |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public Set<DeviceId> getClusterDevices(Topology topology, TopologyCluster cluster) { |
| 111 | checkNotNull(topology, TOPOLOGY_NULL); |
| 112 | checkNotNull(topology, CLUSTER_NULL); |
| 113 | return store.getClusterDevices(defaultTopology(topology), cluster); |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public Set<Link> getClusterLinks(Topology topology, TopologyCluster cluster) { |
| 118 | checkNotNull(topology, TOPOLOGY_NULL); |
| 119 | checkNotNull(topology, CLUSTER_NULL); |
| 120 | return store.getClusterLinks(defaultTopology(topology), cluster); |
| 121 | } |
| 122 | |
| 123 | @Override |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 124 | public TopologyGraph getGraph(Topology topology) { |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 125 | checkNotNull(topology, TOPOLOGY_NULL); |
tom | e52ce70 | 2014-09-11 00:12:54 -0700 | [diff] [blame] | 126 | return store.getGraph(defaultTopology(topology)); |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst) { |
| 131 | checkNotNull(topology, TOPOLOGY_NULL); |
| 132 | checkNotNull(src, DEVICE_ID_NULL); |
| 133 | checkNotNull(dst, DEVICE_ID_NULL); |
tom | e52ce70 | 2014-09-11 00:12:54 -0700 | [diff] [blame] | 134 | return store.getPaths(defaultTopology(topology), src, dst); |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst, LinkWeight weight) { |
| 139 | checkNotNull(topology, TOPOLOGY_NULL); |
| 140 | checkNotNull(src, DEVICE_ID_NULL); |
| 141 | checkNotNull(dst, DEVICE_ID_NULL); |
| 142 | checkNotNull(weight, "Link weight cannot be null"); |
tom | e52ce70 | 2014-09-11 00:12:54 -0700 | [diff] [blame] | 143 | return store.getPaths(defaultTopology(topology), src, dst, weight); |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public boolean isInfrastructure(Topology topology, ConnectPoint connectPoint) { |
| 148 | checkNotNull(topology, TOPOLOGY_NULL); |
| 149 | checkNotNull(connectPoint, CONNECTION_POINT_NULL); |
tom | e52ce70 | 2014-09-11 00:12:54 -0700 | [diff] [blame] | 150 | return store.isInfrastructure(defaultTopology(topology), connectPoint); |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public boolean isInBroadcastTree(Topology topology, ConnectPoint connectPoint) { |
| 155 | checkNotNull(topology, TOPOLOGY_NULL); |
| 156 | checkNotNull(connectPoint, CONNECTION_POINT_NULL); |
tom | e52ce70 | 2014-09-11 00:12:54 -0700 | [diff] [blame] | 157 | return store.isInBroadcastTree(defaultTopology(topology), connectPoint); |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public void addListener(TopologyListener listener) { |
| 162 | listenerRegistry.addListener(listener); |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public void removeListener(TopologyListener listener) { |
| 167 | listenerRegistry.removeListener(listener); |
| 168 | } |
| 169 | |
| 170 | // Personalized host provider service issued to the supplied provider. |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 171 | @Override |
| 172 | protected TopologyProviderService createProviderService(TopologyProvider provider) { |
| 173 | return new InternalTopologyProviderService(provider); |
| 174 | } |
| 175 | |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 176 | private class InternalTopologyProviderService |
| 177 | extends AbstractProviderService<TopologyProvider> |
| 178 | implements TopologyProviderService { |
| 179 | |
| 180 | InternalTopologyProviderService(TopologyProvider provider) { |
| 181 | super(provider); |
| 182 | } |
| 183 | |
| 184 | @Override |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 185 | public void topologyChanged(GraphDescription topoDescription, |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 186 | List<Event> reasons) { |
| 187 | checkNotNull(topoDescription, "Topology description cannot be null"); |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 188 | |
tom | e52ce70 | 2014-09-11 00:12:54 -0700 | [diff] [blame] | 189 | TopologyEvent event = store.updateTopology(provider().id(), |
| 190 | topoDescription, reasons); |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 191 | if (event != null) { |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame^] | 192 | log.info("Topology {} changed", event.subject()); |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 193 | eventDispatcher.post(event); |
| 194 | } |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | |
| 198 | } |