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