alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 1 | package org.onlab.onos.provider.lldp.impl; |
| 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; |
alshabib | 875d626 | 2014-10-17 16:19:40 -0700 | [diff] [blame] | 8 | import org.onlab.onos.mastership.MastershipService; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 9 | import org.onlab.onos.net.ConnectPoint; |
| 10 | import org.onlab.onos.net.Device; |
| 11 | import org.onlab.onos.net.DeviceId; |
alshabib | acd9183 | 2014-10-17 14:38:41 -0700 | [diff] [blame] | 12 | import org.onlab.onos.net.Port; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 13 | import org.onlab.onos.net.device.DeviceEvent; |
| 14 | import org.onlab.onos.net.device.DeviceListener; |
| 15 | import org.onlab.onos.net.device.DeviceService; |
| 16 | import org.onlab.onos.net.link.LinkProvider; |
| 17 | import org.onlab.onos.net.link.LinkProviderRegistry; |
| 18 | import org.onlab.onos.net.link.LinkProviderService; |
| 19 | import org.onlab.onos.net.packet.PacketContext; |
| 20 | import org.onlab.onos.net.packet.PacketProcessor; |
| 21 | import org.onlab.onos.net.packet.PacketService; |
| 22 | import org.onlab.onos.net.provider.AbstractProvider; |
| 23 | import org.onlab.onos.net.provider.ProviderId; |
| 24 | import org.slf4j.Logger; |
| 25 | |
| 26 | import java.util.Map; |
| 27 | import java.util.concurrent.ConcurrentHashMap; |
| 28 | |
| 29 | import static org.slf4j.LoggerFactory.getLogger; |
| 30 | |
| 31 | |
| 32 | /** |
| 33 | * Provider which uses an OpenFlow controller to detect network |
| 34 | * infrastructure links. |
| 35 | */ |
| 36 | @Component(immediate = true) |
| 37 | public class LLDPLinkProvider extends AbstractProvider implements LinkProvider { |
| 38 | |
| 39 | private final Logger log = getLogger(getClass()); |
| 40 | |
| 41 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 42 | protected LinkProviderRegistry providerRegistry; |
| 43 | |
| 44 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 45 | protected DeviceService deviceService; |
| 46 | |
| 47 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 48 | protected PacketService packetSevice; |
| 49 | |
alshabib | 875d626 | 2014-10-17 16:19:40 -0700 | [diff] [blame] | 50 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 51 | protected MastershipService masterService; |
| 52 | |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 53 | private LinkProviderService providerService; |
| 54 | |
| 55 | private final boolean useBDDP = true; |
| 56 | |
| 57 | |
| 58 | private final InternalLinkProvider listener = new InternalLinkProvider(); |
| 59 | |
| 60 | protected final Map<DeviceId, LinkDiscovery> discoverers = new ConcurrentHashMap<>(); |
| 61 | |
| 62 | /** |
| 63 | * Creates an OpenFlow link provider. |
| 64 | */ |
| 65 | public LLDPLinkProvider() { |
| 66 | super(new ProviderId("lldp", "org.onlab.onos.provider.lldp")); |
| 67 | } |
| 68 | |
| 69 | @Activate |
| 70 | public void activate() { |
| 71 | providerService = providerRegistry.register(this); |
| 72 | deviceService.addListener(listener); |
| 73 | packetSevice.addProcessor(listener, 0); |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame] | 74 | LinkDiscovery ld; |
| 75 | for (Device device : deviceService.getDevices()) { |
| 76 | ld = new LinkDiscovery(device, packetSevice, masterService, |
| 77 | providerService, useBDDP); |
| 78 | discoverers.put(device.id(), ld); |
| 79 | for (Port p : deviceService.getPorts(device.id())) { |
Yuta HIGUCHI | 00b476f | 2014-10-25 21:33:07 -0700 | [diff] [blame] | 80 | if (!p.number().isLogical()) { |
| 81 | ld.addPort(p); |
| 82 | } |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame] | 83 | } |
| 84 | } |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 85 | |
| 86 | log.info("Started"); |
| 87 | } |
| 88 | |
| 89 | @Deactivate |
| 90 | public void deactivate() { |
| 91 | for (LinkDiscovery ld : discoverers.values()) { |
| 92 | ld.stop(); |
| 93 | } |
| 94 | providerRegistry.unregister(this); |
| 95 | deviceService.removeListener(listener); |
| 96 | packetSevice.removeProcessor(listener); |
| 97 | providerService = null; |
| 98 | |
| 99 | log.info("Stopped"); |
| 100 | } |
| 101 | |
| 102 | |
| 103 | private class InternalLinkProvider implements PacketProcessor, DeviceListener { |
| 104 | |
| 105 | @Override |
| 106 | public void event(DeviceEvent event) { |
| 107 | LinkDiscovery ld = null; |
| 108 | Device device = event.subject(); |
alshabib | acd9183 | 2014-10-17 14:38:41 -0700 | [diff] [blame] | 109 | Port port = event.port(); |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame] | 110 | if (device == null) { |
| 111 | log.error("Device is null."); |
| 112 | return; |
| 113 | } |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 114 | log.trace("{} {} {}", event.type(), event.subject(), event); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 115 | switch (event.type()) { |
| 116 | case DEVICE_ADDED: |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 117 | case DEVICE_UPDATED: |
| 118 | ld = discoverers.get(device.id()); |
| 119 | if (ld == null) { |
| 120 | log.debug("Device added ({}) {}", event.type(), device.id()); |
| 121 | discoverers.put(device.id(), |
| 122 | new LinkDiscovery(device, packetSevice, masterService, |
| 123 | providerService, useBDDP)); |
| 124 | } else { |
| 125 | if (ld.isStopped()) { |
| 126 | log.debug("Device restarted ({}) {}", event.type(), device.id()); |
| 127 | ld.start(); |
| 128 | } |
| 129 | } |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 130 | break; |
| 131 | case PORT_ADDED: |
| 132 | case PORT_UPDATED: |
| 133 | if (event.port().isEnabled()) { |
alshabib | acd9183 | 2014-10-17 14:38:41 -0700 | [diff] [blame] | 134 | ld = discoverers.get(device.id()); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 135 | if (ld == null) { |
| 136 | return; |
| 137 | } |
Yuta HIGUCHI | 00b476f | 2014-10-25 21:33:07 -0700 | [diff] [blame] | 138 | if (!port.number().isLogical()) { |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 139 | log.debug("Port added {}", port); |
Yuta HIGUCHI | 00b476f | 2014-10-25 21:33:07 -0700 | [diff] [blame] | 140 | ld.addPort(port); |
| 141 | } |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 142 | } else { |
alshabib | acd9183 | 2014-10-17 14:38:41 -0700 | [diff] [blame] | 143 | ConnectPoint point = new ConnectPoint(device.id(), |
| 144 | port.number()); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 145 | providerService.linksVanished(point); |
| 146 | } |
| 147 | break; |
| 148 | case PORT_REMOVED: |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 149 | log.debug("Port removed {}", port); |
alshabib | acd9183 | 2014-10-17 14:38:41 -0700 | [diff] [blame] | 150 | ConnectPoint point = new ConnectPoint(device.id(), |
| 151 | port.number()); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 152 | providerService.linksVanished(point); |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 153 | // TODO: Don't we need to removePort from ld? |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 154 | break; |
| 155 | case DEVICE_REMOVED: |
| 156 | case DEVICE_SUSPENDED: |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 157 | log.debug("Device removed {}", device.id()); |
alshabib | acd9183 | 2014-10-17 14:38:41 -0700 | [diff] [blame] | 158 | ld = discoverers.get(device.id()); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 159 | if (ld == null) { |
| 160 | return; |
| 161 | } |
| 162 | ld.stop(); |
alshabib | acd9183 | 2014-10-17 14:38:41 -0700 | [diff] [blame] | 163 | providerService.linksVanished(device.id()); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 164 | break; |
| 165 | case DEVICE_AVAILABILITY_CHANGED: |
alshabib | acd9183 | 2014-10-17 14:38:41 -0700 | [diff] [blame] | 166 | ld = discoverers.get(device.id()); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 167 | if (ld == null) { |
| 168 | return; |
| 169 | } |
alshabib | acd9183 | 2014-10-17 14:38:41 -0700 | [diff] [blame] | 170 | if (deviceService.isAvailable(device.id())) { |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 171 | log.debug("Device up {}", device.id()); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 172 | ld.start(); |
| 173 | } else { |
alshabib | acd9183 | 2014-10-17 14:38:41 -0700 | [diff] [blame] | 174 | providerService.linksVanished(device.id()); |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 175 | log.debug("Device down {}", device.id()); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 176 | ld.stop(); |
| 177 | } |
| 178 | break; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 179 | case DEVICE_MASTERSHIP_CHANGED: |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame] | 180 | if (!discoverers.containsKey(device.id())) { |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 181 | // TODO: ideally, should never reach here |
| 182 | log.debug("Device mastership changed ({}) {}", event.type(), device.id()); |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame] | 183 | discoverers.put(device.id(), |
| 184 | new LinkDiscovery(device, packetSevice, masterService, |
| 185 | providerService, useBDDP)); |
| 186 | } |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 187 | break; |
| 188 | default: |
| 189 | log.debug("Unknown event {}", event); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | @Override |
| 194 | public void process(PacketContext context) { |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 195 | if (context == null) { |
| 196 | return; |
| 197 | } |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 198 | LinkDiscovery ld = discoverers.get( |
| 199 | context.inPacket().receivedFrom().deviceId()); |
| 200 | if (ld == null) { |
| 201 | return; |
| 202 | } |
| 203 | |
| 204 | if (ld.handleLLDP(context)) { |
| 205 | context.block(); |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | } |