Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 16 | package org.onlab.onos.provider.lldp.impl; |
| 17 | |
| 18 | import org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
| 21 | import org.apache.felix.scr.annotations.Reference; |
| 22 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 23 | import org.onlab.onos.mastership.MastershipEvent; |
| 24 | import org.onlab.onos.mastership.MastershipListener; |
alshabib | 875d626 | 2014-10-17 16:19:40 -0700 | [diff] [blame] | 25 | import org.onlab.onos.mastership.MastershipService; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 26 | import org.onlab.onos.net.ConnectPoint; |
| 27 | import org.onlab.onos.net.Device; |
| 28 | import org.onlab.onos.net.DeviceId; |
alshabib | acd9183 | 2014-10-17 14:38:41 -0700 | [diff] [blame] | 29 | import org.onlab.onos.net.Port; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 30 | import org.onlab.onos.net.device.DeviceEvent; |
| 31 | import org.onlab.onos.net.device.DeviceListener; |
| 32 | import org.onlab.onos.net.device.DeviceService; |
| 33 | import org.onlab.onos.net.link.LinkProvider; |
| 34 | import org.onlab.onos.net.link.LinkProviderRegistry; |
| 35 | import org.onlab.onos.net.link.LinkProviderService; |
| 36 | import org.onlab.onos.net.packet.PacketContext; |
| 37 | import org.onlab.onos.net.packet.PacketProcessor; |
| 38 | import org.onlab.onos.net.packet.PacketService; |
| 39 | import org.onlab.onos.net.provider.AbstractProvider; |
| 40 | import org.onlab.onos.net.provider.ProviderId; |
| 41 | import org.slf4j.Logger; |
| 42 | |
| 43 | import java.util.Map; |
| 44 | import java.util.concurrent.ConcurrentHashMap; |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 45 | import java.util.concurrent.ScheduledExecutorService; |
| 46 | import java.util.concurrent.TimeUnit; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 47 | |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 48 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
| 49 | import static org.onlab.util.Tools.namedThreads; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 50 | import static org.slf4j.LoggerFactory.getLogger; |
| 51 | |
| 52 | |
| 53 | /** |
| 54 | * Provider which uses an OpenFlow controller to detect network |
| 55 | * infrastructure links. |
| 56 | */ |
| 57 | @Component(immediate = true) |
| 58 | public class LLDPLinkProvider extends AbstractProvider implements LinkProvider { |
| 59 | |
| 60 | private final Logger log = getLogger(getClass()); |
| 61 | |
| 62 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 63 | protected LinkProviderRegistry providerRegistry; |
| 64 | |
| 65 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 66 | protected DeviceService deviceService; |
| 67 | |
| 68 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 69 | protected PacketService packetSevice; |
| 70 | |
alshabib | 875d626 | 2014-10-17 16:19:40 -0700 | [diff] [blame] | 71 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 72 | protected MastershipService masterService; |
| 73 | |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 74 | private LinkProviderService providerService; |
| 75 | |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 76 | private ScheduledExecutorService executor; |
| 77 | |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 78 | private final boolean useBDDP = true; |
| 79 | |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 80 | private static final long INIT_DELAY = 5; |
| 81 | private static final long DELAY = 5; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 82 | |
| 83 | private final InternalLinkProvider listener = new InternalLinkProvider(); |
| 84 | |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 85 | private final InternalRoleListener roleListener = new InternalRoleListener(); |
| 86 | |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 87 | protected final Map<DeviceId, LinkDiscovery> discoverers = new ConcurrentHashMap<>(); |
| 88 | |
| 89 | /** |
| 90 | * Creates an OpenFlow link provider. |
| 91 | */ |
| 92 | public LLDPLinkProvider() { |
| 93 | super(new ProviderId("lldp", "org.onlab.onos.provider.lldp")); |
| 94 | } |
| 95 | |
| 96 | @Activate |
| 97 | public void activate() { |
| 98 | providerService = providerRegistry.register(this); |
| 99 | deviceService.addListener(listener); |
| 100 | packetSevice.addProcessor(listener, 0); |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 101 | masterService.addListener(roleListener); |
| 102 | |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame] | 103 | LinkDiscovery ld; |
| 104 | for (Device device : deviceService.getDevices()) { |
| 105 | ld = new LinkDiscovery(device, packetSevice, masterService, |
| 106 | providerService, useBDDP); |
| 107 | discoverers.put(device.id(), ld); |
| 108 | for (Port p : deviceService.getPorts(device.id())) { |
Yuta HIGUCHI | 00b476f | 2014-10-25 21:33:07 -0700 | [diff] [blame] | 109 | if (!p.number().isLogical()) { |
| 110 | ld.addPort(p); |
| 111 | } |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame] | 112 | } |
| 113 | } |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 114 | |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 115 | executor = newSingleThreadScheduledExecutor(namedThreads("device-sync-%d")); |
| 116 | executor.scheduleAtFixedRate(new SyncDeviceInfoTask(), INIT_DELAY, |
| 117 | DELAY, TimeUnit.SECONDS); |
| 118 | |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 119 | log.info("Started"); |
| 120 | } |
| 121 | |
| 122 | @Deactivate |
| 123 | public void deactivate() { |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 124 | executor.shutdownNow(); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 125 | for (LinkDiscovery ld : discoverers.values()) { |
| 126 | ld.stop(); |
| 127 | } |
| 128 | providerRegistry.unregister(this); |
| 129 | deviceService.removeListener(listener); |
| 130 | packetSevice.removeProcessor(listener); |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 131 | masterService.removeListener(roleListener); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 132 | providerService = null; |
| 133 | |
| 134 | log.info("Stopped"); |
| 135 | } |
| 136 | |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 137 | private class InternalRoleListener implements MastershipListener { |
| 138 | |
| 139 | @Override |
| 140 | public void event(MastershipEvent event) { |
| 141 | |
| 142 | if (MastershipEvent.Type.BACKUPS_CHANGED.equals(event.type())) { |
| 143 | // only need new master events |
| 144 | return; |
| 145 | } |
| 146 | |
| 147 | DeviceId deviceId = event.subject(); |
| 148 | Device device = deviceService.getDevice(deviceId); |
| 149 | if (device == null) { |
| 150 | log.warn("Device {} doesn't exist, or isn't there yet", deviceId); |
| 151 | return; |
| 152 | } |
| 153 | synchronized (discoverers) { |
| 154 | if (!discoverers.containsKey(deviceId)) { |
| 155 | // TODO: ideally, should never reach here |
| 156 | log.debug("Device mastership changed ({}) {}", |
| 157 | event.type(), deviceId); |
| 158 | discoverers.put(deviceId, new LinkDiscovery(device, |
| 159 | packetSevice, masterService, providerService, |
| 160 | useBDDP)); |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | } |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 166 | |
| 167 | private class InternalLinkProvider implements PacketProcessor, DeviceListener { |
| 168 | |
| 169 | @Override |
| 170 | public void event(DeviceEvent event) { |
| 171 | LinkDiscovery ld = null; |
| 172 | Device device = event.subject(); |
alshabib | acd9183 | 2014-10-17 14:38:41 -0700 | [diff] [blame] | 173 | Port port = event.port(); |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame] | 174 | if (device == null) { |
| 175 | log.error("Device is null."); |
| 176 | return; |
| 177 | } |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 178 | log.trace("{} {} {}", event.type(), event.subject(), event); |
Yuta HIGUCHI | d19f670 | 2014-10-31 15:23:25 -0700 | [diff] [blame] | 179 | final DeviceId deviceId = device.id(); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 180 | switch (event.type()) { |
| 181 | case DEVICE_ADDED: |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 182 | case DEVICE_UPDATED: |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 183 | synchronized (discoverers) { |
Yuta HIGUCHI | d19f670 | 2014-10-31 15:23:25 -0700 | [diff] [blame] | 184 | ld = discoverers.get(deviceId); |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 185 | if (ld == null) { |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 186 | log.debug("Device added ({}) {}", event.type(), |
| 187 | deviceId); |
| 188 | discoverers.put(deviceId, new LinkDiscovery(device, |
| 189 | packetSevice, masterService, providerService, |
| 190 | useBDDP)); |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 191 | } else { |
| 192 | if (ld.isStopped()) { |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 193 | log.debug("Device restarted ({}) {}", event.type(), |
| 194 | deviceId); |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 195 | ld.start(); |
| 196 | } |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 197 | } |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 198 | } |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 199 | break; |
| 200 | case PORT_ADDED: |
| 201 | case PORT_UPDATED: |
Yuta HIGUCHI | f672588 | 2014-10-29 15:25:51 -0700 | [diff] [blame] | 202 | if (port.isEnabled()) { |
Yuta HIGUCHI | d19f670 | 2014-10-31 15:23:25 -0700 | [diff] [blame] | 203 | ld = discoverers.get(deviceId); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 204 | if (ld == null) { |
| 205 | return; |
| 206 | } |
Yuta HIGUCHI | 00b476f | 2014-10-25 21:33:07 -0700 | [diff] [blame] | 207 | if (!port.number().isLogical()) { |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 208 | log.debug("Port added {}", port); |
Yuta HIGUCHI | 00b476f | 2014-10-25 21:33:07 -0700 | [diff] [blame] | 209 | ld.addPort(port); |
| 210 | } |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 211 | } else { |
Yuta HIGUCHI | f672588 | 2014-10-29 15:25:51 -0700 | [diff] [blame] | 212 | log.debug("Port down {}", port); |
Yuta HIGUCHI | d19f670 | 2014-10-31 15:23:25 -0700 | [diff] [blame] | 213 | ConnectPoint point = new ConnectPoint(deviceId, |
alshabib | acd9183 | 2014-10-17 14:38:41 -0700 | [diff] [blame] | 214 | port.number()); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 215 | providerService.linksVanished(point); |
| 216 | } |
| 217 | break; |
| 218 | case PORT_REMOVED: |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 219 | log.debug("Port removed {}", port); |
Yuta HIGUCHI | d19f670 | 2014-10-31 15:23:25 -0700 | [diff] [blame] | 220 | ConnectPoint point = new ConnectPoint(deviceId, |
alshabib | acd9183 | 2014-10-17 14:38:41 -0700 | [diff] [blame] | 221 | port.number()); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 222 | providerService.linksVanished(point); |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 223 | // TODO: Don't we need to removePort from ld? |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 224 | break; |
| 225 | case DEVICE_REMOVED: |
| 226 | case DEVICE_SUSPENDED: |
Yuta HIGUCHI | d19f670 | 2014-10-31 15:23:25 -0700 | [diff] [blame] | 227 | log.debug("Device removed {}", deviceId); |
| 228 | ld = discoverers.get(deviceId); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 229 | if (ld == null) { |
| 230 | return; |
| 231 | } |
| 232 | ld.stop(); |
Yuta HIGUCHI | d19f670 | 2014-10-31 15:23:25 -0700 | [diff] [blame] | 233 | providerService.linksVanished(deviceId); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 234 | break; |
| 235 | case DEVICE_AVAILABILITY_CHANGED: |
Yuta HIGUCHI | d19f670 | 2014-10-31 15:23:25 -0700 | [diff] [blame] | 236 | ld = discoverers.get(deviceId); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 237 | if (ld == null) { |
| 238 | return; |
| 239 | } |
Yuta HIGUCHI | d19f670 | 2014-10-31 15:23:25 -0700 | [diff] [blame] | 240 | if (deviceService.isAvailable(deviceId)) { |
| 241 | log.debug("Device up {}", deviceId); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 242 | ld.start(); |
| 243 | } else { |
Yuta HIGUCHI | d19f670 | 2014-10-31 15:23:25 -0700 | [diff] [blame] | 244 | providerService.linksVanished(deviceId); |
| 245 | log.debug("Device down {}", deviceId); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 246 | ld.stop(); |
| 247 | } |
| 248 | break; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 249 | default: |
| 250 | log.debug("Unknown event {}", event); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | @Override |
| 255 | public void process(PacketContext context) { |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 256 | if (context == null) { |
| 257 | return; |
| 258 | } |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 259 | LinkDiscovery ld = discoverers.get( |
| 260 | context.inPacket().receivedFrom().deviceId()); |
| 261 | if (ld == null) { |
| 262 | return; |
| 263 | } |
| 264 | |
| 265 | if (ld.handleLLDP(context)) { |
| 266 | context.block(); |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 271 | private final class SyncDeviceInfoTask implements Runnable { |
| 272 | |
| 273 | @Override |
| 274 | public void run() { |
| 275 | if (Thread.currentThread().isInterrupted()) { |
| 276 | log.info("Interrupted, quitting"); |
| 277 | return; |
| 278 | } |
| 279 | // check what deviceService sees, to see if we are missing anything |
| 280 | try { |
| 281 | LinkDiscovery ld = null; |
| 282 | for (Device dev : deviceService.getDevices()) { |
| 283 | DeviceId did = dev.id(); |
| 284 | synchronized (discoverers) { |
| 285 | if (!discoverers.containsKey(did)) { |
| 286 | ld = new LinkDiscovery(dev, packetSevice, |
| 287 | masterService, providerService, useBDDP); |
| 288 | discoverers.put(did, ld); |
| 289 | for (Port p : deviceService.getPorts(did)) { |
| 290 | if (!p.number().isLogical()) { |
| 291 | ld.addPort(p); |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | } catch (Exception e) { |
| 298 | // catch all Exception to avoid Scheduled task being suppressed. |
| 299 | log.error("Exception thrown during synchronization process", e); |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 304 | } |