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