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