tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net.link.impl; |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 2 | |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 3 | import static com.google.common.base.Preconditions.checkNotNull; |
| 4 | import static org.slf4j.LoggerFactory.getLogger; |
| 5 | |
| 6 | import java.util.Set; |
| 7 | |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 8 | import org.apache.felix.scr.annotations.Activate; |
| 9 | import org.apache.felix.scr.annotations.Component; |
| 10 | import org.apache.felix.scr.annotations.Deactivate; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 11 | import org.apache.felix.scr.annotations.Reference; |
| 12 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 13 | import org.apache.felix.scr.annotations.Service; |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 14 | import org.onlab.onos.event.AbstractListenerRegistry; |
| 15 | import org.onlab.onos.event.EventDeliveryService; |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 16 | import org.onlab.onos.net.ConnectPoint; |
| 17 | import org.onlab.onos.net.DeviceId; |
| 18 | import org.onlab.onos.net.Link; |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 19 | import org.onlab.onos.net.MastershipRole; |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 20 | import org.onlab.onos.net.device.DeviceEvent; |
| 21 | import org.onlab.onos.net.device.DeviceListener; |
| 22 | import org.onlab.onos.net.device.DeviceService; |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 23 | import org.onlab.onos.net.link.LinkAdminService; |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 24 | import org.onlab.onos.net.link.LinkDescription; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 25 | import org.onlab.onos.net.link.LinkEvent; |
| 26 | import org.onlab.onos.net.link.LinkListener; |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 27 | import org.onlab.onos.net.link.LinkProvider; |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 28 | import org.onlab.onos.net.link.LinkProviderRegistry; |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 29 | import org.onlab.onos.net.link.LinkProviderService; |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 30 | import org.onlab.onos.net.link.LinkService; |
tom | 35c0dc3 | 2014-09-19 10:00:58 -0700 | [diff] [blame] | 31 | import org.onlab.onos.net.link.LinkStore; |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 32 | import org.onlab.onos.net.link.LinkStoreDelegate; |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 33 | import org.onlab.onos.net.provider.AbstractProviderRegistry; |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 34 | import org.onlab.onos.net.provider.AbstractProviderService; |
| 35 | import org.slf4j.Logger; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 36 | |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 37 | import com.google.common.collect.Sets; |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * Provides basic implementation of the link SB & NB APIs. |
| 41 | */ |
| 42 | @Component(immediate = true) |
| 43 | @Service |
tom | 35c0dc3 | 2014-09-19 10:00:58 -0700 | [diff] [blame] | 44 | public class LinkManager |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 45 | extends AbstractProviderRegistry<LinkProvider, LinkProviderService> |
| 46 | implements LinkService, LinkAdminService, LinkProviderRegistry { |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 47 | |
| 48 | private static final String DEVICE_ID_NULL = "Device ID cannot be null"; |
| 49 | private static final String LINK_DESC_NULL = "Link description cannot be null"; |
| 50 | private static final String CONNECT_POINT_NULL = "Connection point cannot be null"; |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 51 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 52 | private final Logger log = getLogger(getClass()); |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 53 | |
tom | b86066b | 2014-09-15 09:52:24 -0700 | [diff] [blame] | 54 | protected final AbstractListenerRegistry<LinkEvent, LinkListener> |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 55 | listenerRegistry = new AbstractListenerRegistry<>(); |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 56 | |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 57 | private final LinkStoreDelegate delegate = new InternalStoreDelegate(); |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 58 | |
| 59 | private final DeviceListener deviceListener = new InternalDeviceListener(); |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 60 | |
| 61 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
tom | 35c0dc3 | 2014-09-19 10:00:58 -0700 | [diff] [blame] | 62 | protected LinkStore store; |
| 63 | |
| 64 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 65 | protected DeviceService deviceService; |
tom | 4c6606f | 2014-09-07 11:11:21 -0700 | [diff] [blame] | 66 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 67 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
tom | 0d39526 | 2014-09-07 16:53:40 -0700 | [diff] [blame] | 68 | protected EventDeliveryService eventDispatcher; |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 69 | |
| 70 | @Activate |
| 71 | public void activate() { |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 72 | store.setDelegate(delegate); |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 73 | eventDispatcher.addSink(LinkEvent.class, listenerRegistry); |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 74 | deviceService.addListener(deviceListener); |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 75 | log.info("Started"); |
| 76 | } |
| 77 | |
| 78 | @Deactivate |
| 79 | public void deactivate() { |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 80 | store.unsetDelegate(delegate); |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 81 | eventDispatcher.removeSink(LinkEvent.class); |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 82 | deviceService.removeListener(deviceListener); |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 83 | log.info("Stopped"); |
| 84 | } |
| 85 | |
| 86 | @Override |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 87 | public int getLinkCount() { |
| 88 | return store.getLinkCount(); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public Iterable<Link> getLinks() { |
| 93 | return store.getLinks(); |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public Set<Link> getDeviceLinks(DeviceId deviceId) { |
| 98 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 99 | return Sets.union(store.getDeviceEgressLinks(deviceId), |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 100 | store.getDeviceIngressLinks(deviceId)); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public Set<Link> getDeviceEgressLinks(DeviceId deviceId) { |
| 105 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 106 | return store.getDeviceEgressLinks(deviceId); |
| 107 | } |
| 108 | |
| 109 | @Override |
tom | d176fc4 | 2014-09-08 00:12:30 -0700 | [diff] [blame] | 110 | public Set<Link> getDeviceIngressLinks(DeviceId deviceId) { |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 111 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 112 | return store.getDeviceIngressLinks(deviceId); |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public Set<Link> getLinks(ConnectPoint connectPoint) { |
| 117 | checkNotNull(connectPoint, CONNECT_POINT_NULL); |
| 118 | return Sets.union(store.getEgressLinks(connectPoint), |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 119 | store.getIngressLinks(connectPoint)); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public Set<Link> getEgressLinks(ConnectPoint connectPoint) { |
| 124 | checkNotNull(connectPoint, CONNECT_POINT_NULL); |
| 125 | return store.getEgressLinks(connectPoint); |
| 126 | } |
| 127 | |
| 128 | @Override |
tom | d176fc4 | 2014-09-08 00:12:30 -0700 | [diff] [blame] | 129 | public Set<Link> getIngressLinks(ConnectPoint connectPoint) { |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 130 | checkNotNull(connectPoint, CONNECT_POINT_NULL); |
| 131 | return store.getIngressLinks(connectPoint); |
| 132 | } |
| 133 | |
| 134 | @Override |
tom | d176fc4 | 2014-09-08 00:12:30 -0700 | [diff] [blame] | 135 | public Link getLink(ConnectPoint src, ConnectPoint dst) { |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 136 | checkNotNull(src, CONNECT_POINT_NULL); |
| 137 | checkNotNull(dst, CONNECT_POINT_NULL); |
tom | d176fc4 | 2014-09-08 00:12:30 -0700 | [diff] [blame] | 138 | return store.getLink(src, dst); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public void removeLinks(ConnectPoint connectPoint) { |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 143 | if (deviceService.getRole(connectPoint.deviceId()) != MastershipRole.MASTER) { |
| 144 | return; |
| 145 | } |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 146 | removeLinks(getLinks(connectPoint)); |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public void removeLinks(DeviceId deviceId) { |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 151 | if (deviceService.getRole(deviceId) != MastershipRole.MASTER) { |
| 152 | return; |
| 153 | } |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 154 | removeLinks(getDeviceLinks(deviceId)); |
| 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public void addListener(LinkListener listener) { |
| 159 | listenerRegistry.addListener(listener); |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public void removeListener(LinkListener listener) { |
| 164 | listenerRegistry.removeListener(listener); |
| 165 | } |
| 166 | |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 167 | // Auxiliary interceptor for device remove events to prune links that |
| 168 | // are associated with the removed device or its port. |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 169 | private class InternalDeviceListener implements DeviceListener { |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 170 | @Override |
| 171 | public void event(DeviceEvent event) { |
| 172 | if (event.type() == DeviceEvent.Type.DEVICE_REMOVED) { |
| 173 | removeLinks(event.subject().id()); |
| 174 | } else if (event.type() == DeviceEvent.Type.PORT_REMOVED) { |
| 175 | removeLinks(new ConnectPoint(event.subject().id(), |
| 176 | event.port().number())); |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 181 | @Override |
| 182 | protected LinkProviderService createProviderService(LinkProvider provider) { |
| 183 | return new InternalLinkProviderService(provider); |
| 184 | } |
| 185 | |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 186 | // Personalized link provider service issued to the supplied provider. |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 187 | private class InternalLinkProviderService |
| 188 | extends AbstractProviderService<LinkProvider> |
| 189 | implements LinkProviderService { |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 190 | |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 191 | InternalLinkProviderService(LinkProvider provider) { |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 192 | super(provider); |
| 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public void linkDetected(LinkDescription linkDescription) { |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 197 | checkNotNull(linkDescription, LINK_DESC_NULL); |
| 198 | checkValidity(); |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 199 | |
| 200 | ConnectPoint src = linkDescription.src(); |
| 201 | ConnectPoint dst = linkDescription.dst(); |
| 202 | // if we aren't master for the device associated with the ConnectPoint |
| 203 | // we probably shouldn't be doing this. |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame^] | 204 | |
| 205 | // if (deviceService.getRole(dst.deviceId()) != MastershipRole.MASTER) { |
| 206 | // return; |
| 207 | // } |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 208 | LinkEvent event = store.createOrUpdateLink(provider().id(), |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 209 | linkDescription); |
| 210 | if (event != null) { |
| 211 | log.debug("Link {} detected", linkDescription); |
| 212 | post(event); |
| 213 | } |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | @Override |
| 217 | public void linkVanished(LinkDescription linkDescription) { |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 218 | checkNotNull(linkDescription, LINK_DESC_NULL); |
| 219 | checkValidity(); |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 220 | |
| 221 | ConnectPoint src = linkDescription.src(); |
| 222 | ConnectPoint dst = linkDescription.dst(); |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame^] | 223 | |
| 224 | LinkEvent event = store.removeLink(src, dst); |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 225 | if (event != null) { |
| 226 | log.info("Link {} vanished", linkDescription); |
| 227 | post(event); |
| 228 | } |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | @Override |
| 232 | public void linksVanished(ConnectPoint connectPoint) { |
| 233 | checkNotNull(connectPoint, "Connect point cannot be null"); |
| 234 | checkValidity(); |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 235 | // if we aren't master for the device associated with the ConnectPoint |
| 236 | // we probably shouldn't be doing this. |
| 237 | if (deviceService.getRole(connectPoint.deviceId()) != MastershipRole.MASTER) { |
| 238 | return; |
| 239 | } |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 240 | log.info("Links for connection point {} vanished", connectPoint); |
Yuta HIGUCHI | e794cbe | 2014-10-17 13:21:23 -0700 | [diff] [blame] | 241 | // FIXME: This will remove links registered by other providers |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 242 | removeLinks(getLinks(connectPoint)); |
| 243 | } |
| 244 | |
| 245 | @Override |
| 246 | public void linksVanished(DeviceId deviceId) { |
| 247 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 248 | checkValidity(); |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 249 | // if we aren't master for the device associated with the ConnectPoint |
| 250 | // we probably shouldn't be doing this. |
| 251 | if (deviceService.getRole(deviceId) != MastershipRole.MASTER) { |
| 252 | return; |
| 253 | } |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 254 | log.info("Links for device {} vanished", deviceId); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 255 | removeLinks(getDeviceLinks(deviceId)); |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 256 | } |
| 257 | } |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 258 | |
| 259 | // Removes all links in the specified set and emits appropriate events. |
| 260 | private void removeLinks(Set<Link> links) { |
| 261 | for (Link link : links) { |
| 262 | LinkEvent event = store.removeLink(link.src(), link.dst()); |
| 263 | post(event); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | // Posts the specified event to the local event dispatcher. |
| 268 | private void post(LinkEvent event) { |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 269 | if (event != null) { |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 270 | eventDispatcher.post(event); |
| 271 | } |
| 272 | } |
| 273 | |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 274 | // Store delegate to re-post events emitted from the store. |
| 275 | private class InternalStoreDelegate implements LinkStoreDelegate { |
| 276 | @Override |
| 277 | public void notify(LinkEvent event) { |
| 278 | post(event); |
| 279 | } |
| 280 | } |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 281 | } |