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