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 | } |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 114 | switch (event.type()) { |
| 115 | case DEVICE_ADDED: |
alshabib | acd9183 | 2014-10-17 14:38:41 -0700 | [diff] [blame] | 116 | discoverers.put(device.id(), |
alshabib | 875d626 | 2014-10-17 16:19:40 -0700 | [diff] [blame] | 117 | new LinkDiscovery(device, packetSevice, masterService, |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 118 | providerService, useBDDP)); |
| 119 | break; |
| 120 | case PORT_ADDED: |
| 121 | case PORT_UPDATED: |
| 122 | if (event.port().isEnabled()) { |
alshabib | acd9183 | 2014-10-17 14:38:41 -0700 | [diff] [blame] | 123 | ld = discoverers.get(device.id()); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 124 | if (ld == null) { |
| 125 | return; |
| 126 | } |
Yuta HIGUCHI | 00b476f | 2014-10-25 21:33:07 -0700 | [diff] [blame^] | 127 | if (!port.number().isLogical()) { |
| 128 | ld.addPort(port); |
| 129 | } |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 130 | } else { |
alshabib | acd9183 | 2014-10-17 14:38:41 -0700 | [diff] [blame] | 131 | ConnectPoint point = new ConnectPoint(device.id(), |
| 132 | port.number()); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 133 | providerService.linksVanished(point); |
| 134 | } |
| 135 | break; |
| 136 | case PORT_REMOVED: |
alshabib | acd9183 | 2014-10-17 14:38:41 -0700 | [diff] [blame] | 137 | ConnectPoint point = new ConnectPoint(device.id(), |
| 138 | port.number()); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 139 | providerService.linksVanished(point); |
| 140 | break; |
| 141 | case DEVICE_REMOVED: |
| 142 | case DEVICE_SUSPENDED: |
alshabib | acd9183 | 2014-10-17 14:38:41 -0700 | [diff] [blame] | 143 | ld = discoverers.get(device.id()); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 144 | if (ld == null) { |
| 145 | return; |
| 146 | } |
| 147 | ld.stop(); |
alshabib | acd9183 | 2014-10-17 14:38:41 -0700 | [diff] [blame] | 148 | providerService.linksVanished(device.id()); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 149 | break; |
| 150 | case DEVICE_AVAILABILITY_CHANGED: |
alshabib | acd9183 | 2014-10-17 14:38:41 -0700 | [diff] [blame] | 151 | ld = discoverers.get(device.id()); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 152 | if (ld == null) { |
| 153 | return; |
| 154 | } |
alshabib | acd9183 | 2014-10-17 14:38:41 -0700 | [diff] [blame] | 155 | if (deviceService.isAvailable(device.id())) { |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 156 | ld.start(); |
| 157 | } else { |
alshabib | acd9183 | 2014-10-17 14:38:41 -0700 | [diff] [blame] | 158 | providerService.linksVanished(device.id()); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 159 | ld.stop(); |
| 160 | } |
| 161 | break; |
| 162 | case DEVICE_UPDATED: |
| 163 | case DEVICE_MASTERSHIP_CHANGED: |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame] | 164 | if (!discoverers.containsKey(device.id())) { |
| 165 | discoverers.put(device.id(), |
| 166 | new LinkDiscovery(device, packetSevice, masterService, |
| 167 | providerService, useBDDP)); |
| 168 | } |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 169 | break; |
| 170 | default: |
| 171 | log.debug("Unknown event {}", event); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | @Override |
| 176 | public void process(PacketContext context) { |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 177 | if (context == null) { |
| 178 | return; |
| 179 | } |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 180 | LinkDiscovery ld = discoverers.get( |
| 181 | context.inPacket().receivedFrom().deviceId()); |
| 182 | if (ld == null) { |
| 183 | return; |
| 184 | } |
| 185 | |
| 186 | if (ld.handleLLDP(context)) { |
| 187 | context.block(); |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | } |