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())) { |
| 80 | ld.addPort(p); |
| 81 | } |
| 82 | } |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 83 | |
| 84 | log.info("Started"); |
| 85 | } |
| 86 | |
| 87 | @Deactivate |
| 88 | public void deactivate() { |
| 89 | for (LinkDiscovery ld : discoverers.values()) { |
| 90 | ld.stop(); |
| 91 | } |
| 92 | providerRegistry.unregister(this); |
| 93 | deviceService.removeListener(listener); |
| 94 | packetSevice.removeProcessor(listener); |
| 95 | providerService = null; |
| 96 | |
| 97 | log.info("Stopped"); |
| 98 | } |
| 99 | |
| 100 | |
| 101 | private class InternalLinkProvider implements PacketProcessor, DeviceListener { |
| 102 | |
| 103 | @Override |
| 104 | public void event(DeviceEvent event) { |
| 105 | LinkDiscovery ld = null; |
| 106 | Device device = event.subject(); |
alshabib | acd9183 | 2014-10-17 14:38:41 -0700 | [diff] [blame] | 107 | Port port = event.port(); |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame^] | 108 | if (device == null) { |
| 109 | log.error("Device is null."); |
| 110 | return; |
| 111 | } |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 112 | switch (event.type()) { |
| 113 | case DEVICE_ADDED: |
alshabib | acd9183 | 2014-10-17 14:38:41 -0700 | [diff] [blame] | 114 | discoverers.put(device.id(), |
alshabib | 875d626 | 2014-10-17 16:19:40 -0700 | [diff] [blame] | 115 | new LinkDiscovery(device, packetSevice, masterService, |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 116 | providerService, useBDDP)); |
| 117 | break; |
| 118 | case PORT_ADDED: |
| 119 | case PORT_UPDATED: |
| 120 | if (event.port().isEnabled()) { |
alshabib | acd9183 | 2014-10-17 14:38:41 -0700 | [diff] [blame] | 121 | ld = discoverers.get(device.id()); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 122 | if (ld == null) { |
| 123 | return; |
| 124 | } |
alshabib | acd9183 | 2014-10-17 14:38:41 -0700 | [diff] [blame] | 125 | ld.addPort(port); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 126 | } else { |
alshabib | acd9183 | 2014-10-17 14:38:41 -0700 | [diff] [blame] | 127 | ConnectPoint point = new ConnectPoint(device.id(), |
| 128 | port.number()); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 129 | providerService.linksVanished(point); |
| 130 | } |
| 131 | break; |
| 132 | case PORT_REMOVED: |
alshabib | acd9183 | 2014-10-17 14:38:41 -0700 | [diff] [blame] | 133 | ConnectPoint point = new ConnectPoint(device.id(), |
| 134 | port.number()); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 135 | providerService.linksVanished(point); |
| 136 | break; |
| 137 | case DEVICE_REMOVED: |
| 138 | case DEVICE_SUSPENDED: |
alshabib | acd9183 | 2014-10-17 14:38:41 -0700 | [diff] [blame] | 139 | ld = discoverers.get(device.id()); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 140 | if (ld == null) { |
| 141 | return; |
| 142 | } |
| 143 | ld.stop(); |
alshabib | acd9183 | 2014-10-17 14:38:41 -0700 | [diff] [blame] | 144 | providerService.linksVanished(device.id()); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 145 | break; |
| 146 | case DEVICE_AVAILABILITY_CHANGED: |
alshabib | acd9183 | 2014-10-17 14:38:41 -0700 | [diff] [blame] | 147 | ld = discoverers.get(device.id()); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 148 | if (ld == null) { |
| 149 | return; |
| 150 | } |
alshabib | acd9183 | 2014-10-17 14:38:41 -0700 | [diff] [blame] | 151 | if (deviceService.isAvailable(device.id())) { |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 152 | ld.start(); |
| 153 | } else { |
alshabib | acd9183 | 2014-10-17 14:38:41 -0700 | [diff] [blame] | 154 | providerService.linksVanished(device.id()); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 155 | ld.stop(); |
| 156 | } |
| 157 | break; |
| 158 | case DEVICE_UPDATED: |
| 159 | case DEVICE_MASTERSHIP_CHANGED: |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame^] | 160 | if (!discoverers.containsKey(device.id())) { |
| 161 | discoverers.put(device.id(), |
| 162 | new LinkDiscovery(device, packetSevice, masterService, |
| 163 | providerService, useBDDP)); |
| 164 | } |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 165 | break; |
| 166 | default: |
| 167 | log.debug("Unknown event {}", event); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public void process(PacketContext context) { |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 173 | if (context == null) { |
| 174 | return; |
| 175 | } |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 176 | LinkDiscovery ld = discoverers.get( |
| 177 | context.inPacket().receivedFrom().deviceId()); |
| 178 | if (ld == null) { |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | if (ld.handleLLDP(context)) { |
| 183 | context.block(); |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | } |