Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 3 | * |
| 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 |
| 7 | * |
| 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. |
| 15 | */ |
| 16 | package org.onosproject.provider.netcfglinks; |
| 17 | |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 18 | import org.onlab.packet.Ethernet; |
| 19 | import org.onlab.packet.ONOSLLDP; |
Ayaka Koshibe | 4822922 | 2016-05-16 18:04:26 -0700 | [diff] [blame] | 20 | import org.onosproject.cluster.ClusterMetadataService; |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 21 | import org.onosproject.core.ApplicationId; |
| 22 | import org.onosproject.core.CoreService; |
| 23 | import org.onosproject.mastership.MastershipService; |
| 24 | import org.onosproject.net.ConnectPoint; |
| 25 | import org.onosproject.net.DefaultAnnotations; |
| 26 | import org.onosproject.net.Device; |
| 27 | import org.onosproject.net.DeviceId; |
| 28 | import org.onosproject.net.Link; |
| 29 | import org.onosproject.net.LinkKey; |
| 30 | import org.onosproject.net.Port; |
| 31 | import org.onosproject.net.PortNumber; |
| 32 | import org.onosproject.net.config.NetworkConfigEvent; |
| 33 | import org.onosproject.net.config.NetworkConfigListener; |
| 34 | import org.onosproject.net.config.NetworkConfigRegistry; |
| 35 | import org.onosproject.net.config.basics.BasicLinkConfig; |
| 36 | import org.onosproject.net.device.DeviceEvent; |
| 37 | import org.onosproject.net.device.DeviceListener; |
| 38 | import org.onosproject.net.device.DeviceService; |
| 39 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 40 | import org.onosproject.net.flow.TrafficSelector; |
| 41 | import org.onosproject.net.link.DefaultLinkDescription; |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 42 | import org.onosproject.net.link.LinkProviderRegistry; |
| 43 | import org.onosproject.net.link.LinkProviderService; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 44 | import org.onosproject.net.link.ProbedLinkProvider; |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 45 | import org.onosproject.net.packet.InboundPacket; |
| 46 | import org.onosproject.net.packet.PacketContext; |
| 47 | import org.onosproject.net.packet.PacketPriority; |
| 48 | import org.onosproject.net.packet.PacketProcessor; |
| 49 | import org.onosproject.net.packet.PacketService; |
| 50 | import org.onosproject.net.provider.AbstractProvider; |
| 51 | import org.onosproject.net.provider.ProviderId; |
Ray Milkey | 957390e | 2016-02-09 10:02:46 -0800 | [diff] [blame] | 52 | import org.onosproject.provider.lldpcommon.LinkDiscovery; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 53 | import org.onosproject.provider.lldpcommon.LinkDiscoveryContext; |
| 54 | import org.osgi.service.component.annotations.Activate; |
| 55 | import org.osgi.service.component.annotations.Component; |
| 56 | import org.osgi.service.component.annotations.Deactivate; |
| 57 | import org.osgi.service.component.annotations.Reference; |
| 58 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 59 | import org.slf4j.Logger; |
| 60 | import org.slf4j.LoggerFactory; |
| 61 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 62 | import java.util.HashSet; |
| 63 | import java.util.Map; |
| 64 | import java.util.Optional; |
| 65 | import java.util.Set; |
| 66 | import java.util.concurrent.ConcurrentHashMap; |
| 67 | |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 68 | import static org.onlab.packet.Ethernet.TYPE_BSN; |
| 69 | import static org.onlab.packet.Ethernet.TYPE_LLDP; |
| 70 | import static org.onosproject.net.PortNumber.portNumber; |
Ray Milkey | d17309c | 2018-10-18 09:34:54 -0700 | [diff] [blame] | 71 | import static org.onosproject.provider.netcfglinks.OsgiPropertyConstants.DISCOVERY_DELAY_DEFAULT; |
| 72 | import static org.onosproject.provider.netcfglinks.OsgiPropertyConstants.PROP_DISCOVERY_DELAY; |
Thomas Vachuska | 4167c3f | 2018-10-16 07:16:31 -0700 | [diff] [blame] | 73 | import static org.onosproject.provider.netcfglinks.OsgiPropertyConstants.PROP_PROBE_RATE; |
| 74 | import static org.onosproject.provider.netcfglinks.OsgiPropertyConstants.PROBE_RATE_DEFAULT; |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 75 | |
| 76 | /** |
| 77 | * Provider to pre-discover links and devices based on a specified network |
| 78 | * config. |
| 79 | */ |
| 80 | |
Thomas Vachuska | 4167c3f | 2018-10-16 07:16:31 -0700 | [diff] [blame] | 81 | @Component(immediate = true, |
| 82 | property = { |
| 83 | PROP_PROBE_RATE + ":Integer=" + PROBE_RATE_DEFAULT, |
Ray Milkey | d17309c | 2018-10-18 09:34:54 -0700 | [diff] [blame] | 84 | PROP_DISCOVERY_DELAY + ":Integer=" + DISCOVERY_DELAY_DEFAULT, |
Thomas Vachuska | 4167c3f | 2018-10-16 07:16:31 -0700 | [diff] [blame] | 85 | }) |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 86 | public class NetworkConfigLinksProvider |
| 87 | extends AbstractProvider |
Ayaka Koshibe | 4822922 | 2016-05-16 18:04:26 -0700 | [diff] [blame] | 88 | implements ProbedLinkProvider { |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 89 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 90 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 91 | protected LinkProviderRegistry providerRegistry; |
| 92 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 93 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 94 | protected DeviceService deviceService; |
| 95 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 96 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 97 | protected PacketService packetService; |
| 98 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 99 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 100 | protected MastershipService masterService; |
| 101 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 102 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 103 | protected NetworkConfigRegistry netCfgService; |
| 104 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 105 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 106 | protected CoreService coreService; |
| 107 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 108 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Ayaka Koshibe | 4822922 | 2016-05-16 18:04:26 -0700 | [diff] [blame] | 109 | protected ClusterMetadataService metadataService; |
| 110 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 111 | //@Property(name = PROP_PROBE_RATE, intValue = DEFAULT_PROBE_RATE, |
| 112 | // label = "LLDP and BDDP probe rate specified in millis") |
Thomas Vachuska | 4167c3f | 2018-10-16 07:16:31 -0700 | [diff] [blame] | 113 | private int probeRate = PROBE_RATE_DEFAULT; |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 114 | |
Ray Milkey | d17309c | 2018-10-18 09:34:54 -0700 | [diff] [blame] | 115 | //@Property(name = PROP_DISCOVERY_DELAY, intValue = DEFAULT_DISCOVERY_DELAY, |
| 116 | // label = "Number of millis beyond which an LLDP packet will not be accepted") |
| 117 | private int maxDiscoveryDelayMs = DISCOVERY_DELAY_DEFAULT; |
Samuel Jero | 31e16f5 | 2018-09-21 10:34:28 -0400 | [diff] [blame] | 118 | |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 119 | // Device link discovery helpers. |
| 120 | protected final Map<DeviceId, LinkDiscovery> discoverers = new ConcurrentHashMap<>(); |
| 121 | |
Ray Milkey | 957390e | 2016-02-09 10:02:46 -0800 | [diff] [blame] | 122 | private final LinkDiscoveryContext context = new InternalDiscoveryContext(); |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 123 | |
| 124 | private LinkProviderService providerService; |
| 125 | |
| 126 | private static final String PROVIDER_NAME = |
| 127 | "org.onosproject.provider.netcfglinks"; |
| 128 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 129 | |
| 130 | private ApplicationId appId; |
| 131 | private final InternalPacketProcessor packetProcessor = new InternalPacketProcessor(); |
| 132 | private final InternalDeviceListener deviceListener = new InternalDeviceListener(); |
| 133 | private final InternalConfigListener cfgListener = new InternalConfigListener(); |
| 134 | |
Ray Milkey | cd6ab18 | 2016-02-03 11:13:09 -0800 | [diff] [blame] | 135 | protected Set<LinkKey> configuredLinks = new HashSet<>(); |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 136 | |
| 137 | public NetworkConfigLinksProvider() { |
| 138 | super(new ProviderId("lldp", PROVIDER_NAME)); |
| 139 | } |
| 140 | |
Sho SHIMIZU | 9efeb81 | 2016-08-18 09:29:20 -0700 | [diff] [blame] | 141 | private String buildSrcMac() { |
Ayaka Koshibe | 4822922 | 2016-05-16 18:04:26 -0700 | [diff] [blame] | 142 | String srcMac = ProbedLinkProvider.fingerprintMac(metadataService.getClusterMetadata()); |
| 143 | String defMac = ProbedLinkProvider.defaultMac(); |
| 144 | if (srcMac.equals(defMac)) { |
| 145 | log.warn("Couldn't generate fingerprint. Using default value {}", defMac); |
| 146 | return defMac; |
| 147 | } |
| 148 | log.trace("Generated MAC address {}", srcMac); |
| 149 | return srcMac; |
| 150 | } |
| 151 | |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 152 | private void createLinks() { |
| 153 | netCfgService.getSubjects(LinkKey.class) |
| 154 | .forEach(linkKey -> configuredLinks.add(linkKey)); |
| 155 | } |
| 156 | |
| 157 | @Activate |
| 158 | protected void activate() { |
| 159 | log.info("Activated"); |
| 160 | appId = coreService.registerApplication(PROVIDER_NAME); |
| 161 | packetService.addProcessor(packetProcessor, PacketProcessor.advisor(0)); |
| 162 | providerService = providerRegistry.register(this); |
| 163 | deviceService.addListener(deviceListener); |
| 164 | netCfgService.addListener(cfgListener); |
| 165 | requestIntercepts(); |
| 166 | loadDevices(); |
| 167 | createLinks(); |
| 168 | } |
| 169 | |
| 170 | @Deactivate |
| 171 | protected void deactivate() { |
| 172 | withdrawIntercepts(); |
| 173 | providerRegistry.unregister(this); |
Deepa Vaddireddy | 21f5ae8 | 2017-05-10 18:27:30 +0530 | [diff] [blame] | 174 | deviceService.removeListener(deviceListener); |
| 175 | netCfgService.removeListener(cfgListener); |
| 176 | packetService.removeProcessor(packetProcessor); |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 177 | disable(); |
| 178 | log.info("Deactivated"); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Loads available devices and registers their ports to be probed. |
| 183 | */ |
| 184 | private void loadDevices() { |
| 185 | deviceService.getAvailableDevices() |
| 186 | .forEach(d -> updateDevice(d) |
| 187 | .ifPresent(ld -> updatePorts(ld, d.id()))); |
| 188 | } |
| 189 | |
| 190 | private Optional<LinkDiscovery> updateDevice(Device device) { |
| 191 | if (device == null) { |
| 192 | return Optional.empty(); |
| 193 | } |
| 194 | |
| 195 | LinkDiscovery ld = discoverers.computeIfAbsent(device.id(), |
Ayaka Koshibe | 4822922 | 2016-05-16 18:04:26 -0700 | [diff] [blame] | 196 | did -> new LinkDiscovery(device, context)); |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 197 | if (ld.isStopped()) { |
| 198 | ld.start(); |
| 199 | } |
| 200 | return Optional.of(ld); |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Updates ports of the specified device to the specified discovery helper. |
| 205 | */ |
| 206 | private void updatePorts(LinkDiscovery discoverer, DeviceId deviceId) { |
| 207 | deviceService.getPorts(deviceId).forEach(p -> updatePort(discoverer, p)); |
| 208 | } |
| 209 | |
| 210 | |
| 211 | private void updatePort(LinkDiscovery discoverer, Port port) { |
| 212 | if (port == null) { |
| 213 | return; |
| 214 | } |
| 215 | if (port.number().isLogical()) { |
| 216 | // silently ignore logical ports |
| 217 | return; |
| 218 | } |
| 219 | |
| 220 | discoverer.addPort(port); |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Disables link discovery processing. |
| 225 | */ |
| 226 | private void disable() { |
| 227 | |
| 228 | providerRegistry.unregister(this); |
| 229 | discoverers.values().forEach(LinkDiscovery::stop); |
| 230 | discoverers.clear(); |
| 231 | |
| 232 | providerService = null; |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Provides processing context for the device link discovery helpers. |
| 237 | */ |
Ray Milkey | 957390e | 2016-02-09 10:02:46 -0800 | [diff] [blame] | 238 | private class InternalDiscoveryContext implements LinkDiscoveryContext { |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 239 | @Override |
| 240 | public MastershipService mastershipService() { |
| 241 | return masterService; |
| 242 | } |
| 243 | |
| 244 | @Override |
| 245 | public LinkProviderService providerService() { |
| 246 | return providerService; |
| 247 | } |
| 248 | |
| 249 | @Override |
| 250 | public PacketService packetService() { |
| 251 | return packetService; |
| 252 | } |
| 253 | |
| 254 | @Override |
| 255 | public long probeRate() { |
| 256 | return probeRate; |
| 257 | } |
| 258 | |
| 259 | @Override |
| 260 | public boolean useBddp() { |
| 261 | return true; |
| 262 | } |
| 263 | |
| 264 | @Override |
| 265 | public void touchLink(LinkKey key) { |
Ray Milkey | 957390e | 2016-02-09 10:02:46 -0800 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | @Override |
| 269 | public String fingerprint() { |
Ayaka Koshibe | 4822922 | 2016-05-16 18:04:26 -0700 | [diff] [blame] | 270 | return buildSrcMac(); |
Ray Milkey | 957390e | 2016-02-09 10:02:46 -0800 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | @Override |
| 274 | public DeviceService deviceService() { |
| 275 | return deviceService; |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 276 | } |
Samuel Jero | 31e16f5 | 2018-09-21 10:34:28 -0400 | [diff] [blame] | 277 | |
| 278 | @Override |
| 279 | public String lldpSecret() { |
| 280 | return metadataService.getClusterMetadata().getClusterSecret(); |
| 281 | } |
| 282 | |
| 283 | @Override |
| 284 | public long maxDiscoveryDelay() { |
| 285 | return maxDiscoveryDelayMs; |
| 286 | } |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 287 | } |
| 288 | |
Samuel Jero | 31e16f5 | 2018-09-21 10:34:28 -0400 | [diff] [blame] | 289 | // true if *NOT* this cluster's own probe. |
| 290 | private boolean isOthercluster(String mac) { |
| 291 | // if we are using DEFAULT_MAC, clustering hadn't initialized, so conservative 'yes' |
| 292 | String ourMac = context.fingerprint(); |
| 293 | if (ProbedLinkProvider.defaultMac().equalsIgnoreCase(ourMac)) { |
| 294 | return true; |
| 295 | } |
| 296 | return !mac.equalsIgnoreCase(ourMac); |
| 297 | } |
| 298 | |
| 299 | //doesn't validate. Used just to decide if this is expected link. |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 300 | LinkKey extractLinkKey(PacketContext packetContext) { |
| 301 | Ethernet eth = packetContext.inPacket().parsed(); |
| 302 | if (eth == null) { |
| 303 | return null; |
| 304 | } |
| 305 | |
| 306 | ONOSLLDP onoslldp = ONOSLLDP.parseONOSLLDP(eth); |
| 307 | if (onoslldp != null) { |
| 308 | PortNumber srcPort = portNumber(onoslldp.getPort()); |
| 309 | PortNumber dstPort = packetContext.inPacket().receivedFrom().port(); |
| 310 | DeviceId srcDeviceId = DeviceId.deviceId(onoslldp.getDeviceString()); |
| 311 | DeviceId dstDeviceId = packetContext.inPacket().receivedFrom().deviceId(); |
| 312 | |
| 313 | ConnectPoint src = new ConnectPoint(srcDeviceId, srcPort); |
| 314 | ConnectPoint dst = new ConnectPoint(dstDeviceId, dstPort); |
| 315 | return LinkKey.linkKey(src, dst); |
| 316 | } |
| 317 | return null; |
| 318 | } |
| 319 | |
Samuel Jero | 31e16f5 | 2018-09-21 10:34:28 -0400 | [diff] [blame] | 320 | private boolean verify(PacketContext packetContext) { |
| 321 | Ethernet eth = packetContext.inPacket().parsed(); |
| 322 | if (eth == null) { |
| 323 | return false; |
| 324 | } |
| 325 | |
| 326 | ONOSLLDP onoslldp = ONOSLLDP.parseONOSLLDP(eth); |
| 327 | if (onoslldp != null) { |
| 328 | if (!isOthercluster(eth.getSourceMAC().toString())) { |
| 329 | return false; |
| 330 | } |
| 331 | |
| 332 | if (!ONOSLLDP.verify(onoslldp, context.lldpSecret(), context.maxDiscoveryDelay())) { |
| 333 | log.warn("LLDP Packet failed to validate!"); |
| 334 | return false; |
| 335 | } |
| 336 | return true; |
| 337 | } |
| 338 | return false; |
| 339 | } |
| 340 | |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 341 | /** |
Ray Milkey | cd6ab18 | 2016-02-03 11:13:09 -0800 | [diff] [blame] | 342 | * Removes after stopping discovery helper for specified device. |
| 343 | * @param deviceId device to remove |
| 344 | */ |
| 345 | private void removeDevice(final DeviceId deviceId) { |
| 346 | discoverers.computeIfPresent(deviceId, (did, ld) -> { |
| 347 | ld.stop(); |
| 348 | return null; |
| 349 | }); |
| 350 | |
| 351 | } |
| 352 | |
| 353 | /** |
| 354 | * Removes a port from the specified discovery helper. |
| 355 | * @param port the port |
| 356 | */ |
| 357 | private void removePort(Port port) { |
| 358 | if (port.element() instanceof Device) { |
| 359 | Device d = (Device) port.element(); |
| 360 | LinkDiscovery ld = discoverers.get(d.id()); |
| 361 | if (ld != null) { |
| 362 | ld.removePort(port.number()); |
| 363 | } |
| 364 | } else { |
| 365 | log.warn("Attempted to remove non-Device port", port); |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | |
| 370 | /** |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 371 | * Processes incoming packets. |
| 372 | */ |
| 373 | private class InternalPacketProcessor implements PacketProcessor { |
| 374 | @Override |
| 375 | public void process(PacketContext context) { |
| 376 | if (context == null || context.isHandled()) { |
| 377 | return; |
| 378 | } |
| 379 | |
| 380 | Ethernet eth = context.inPacket().parsed(); |
| 381 | if (eth == null || (eth.getEtherType() != TYPE_LLDP && eth.getEtherType() != TYPE_BSN)) { |
| 382 | return; |
| 383 | } |
| 384 | |
| 385 | InboundPacket inPacket = context.inPacket(); |
| 386 | LinkKey linkKey = extractLinkKey(context); |
| 387 | if (linkKey != null) { |
| 388 | if (configuredLinks.contains(linkKey)) { |
| 389 | log.debug("Found configured link {}", linkKey); |
| 390 | LinkDiscovery ld = discoverers.get(inPacket.receivedFrom().deviceId()); |
| 391 | if (ld == null) { |
| 392 | return; |
| 393 | } |
| 394 | if (ld.handleLldp(context)) { |
| 395 | context.block(); |
| 396 | } |
| 397 | } else { |
Samuel Jero | 31e16f5 | 2018-09-21 10:34:28 -0400 | [diff] [blame] | 398 | if (verify(context)) { |
| 399 | log.debug("Found link that was not in the configuration {}", linkKey); |
| 400 | providerService.linkDetected( |
| 401 | new DefaultLinkDescription(linkKey.src(), |
| 402 | linkKey.dst(), |
| 403 | Link.Type.DIRECT, |
| 404 | DefaultLinkDescription.NOT_EXPECTED, |
| 405 | DefaultAnnotations.EMPTY)); |
| 406 | } |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 407 | } |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | /** |
| 413 | * Requests packet intercepts. |
| 414 | */ |
| 415 | private void requestIntercepts() { |
| 416 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 417 | selector.matchEthType(TYPE_LLDP); |
| 418 | packetService.requestPackets(selector.build(), PacketPriority.CONTROL, |
| 419 | appId, Optional.empty()); |
| 420 | |
| 421 | selector.matchEthType(TYPE_BSN); |
| 422 | |
| 423 | packetService.requestPackets(selector.build(), PacketPriority.CONTROL, |
| 424 | appId, Optional.empty()); |
| 425 | |
| 426 | } |
| 427 | |
| 428 | /** |
| 429 | * Withdraws packet intercepts. |
| 430 | */ |
| 431 | private void withdrawIntercepts() { |
| 432 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 433 | selector.matchEthType(TYPE_LLDP); |
| 434 | packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, |
| 435 | appId, Optional.empty()); |
| 436 | selector.matchEthType(TYPE_BSN); |
| 437 | packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, |
| 438 | appId, Optional.empty()); |
| 439 | } |
| 440 | |
| 441 | /** |
| 442 | * Processes device events. |
| 443 | */ |
| 444 | private class InternalDeviceListener implements DeviceListener { |
| 445 | @Override |
| 446 | public void event(DeviceEvent event) { |
| 447 | if (event.type() == DeviceEvent.Type.PORT_STATS_UPDATED) { |
| 448 | return; |
| 449 | } |
| 450 | Device device = event.subject(); |
| 451 | Port port = event.port(); |
| 452 | if (device == null) { |
| 453 | log.error("Device is null."); |
| 454 | return; |
| 455 | } |
| 456 | log.trace("{} {} {}", event.type(), event.subject(), event); |
| 457 | final DeviceId deviceId = device.id(); |
| 458 | switch (event.type()) { |
| 459 | case DEVICE_ADDED: |
| 460 | case DEVICE_UPDATED: |
| 461 | updateDevice(device).ifPresent(ld -> updatePorts(ld, deviceId)); |
| 462 | break; |
| 463 | case PORT_ADDED: |
| 464 | case PORT_UPDATED: |
| 465 | if (port.isEnabled()) { |
| 466 | updateDevice(device).ifPresent(ld -> updatePort(ld, port)); |
| 467 | } else { |
| 468 | log.debug("Port down {}", port); |
Ray Milkey | cd6ab18 | 2016-02-03 11:13:09 -0800 | [diff] [blame] | 469 | removePort(port); |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 470 | providerService.linksVanished(new ConnectPoint(port.element().id(), |
| 471 | port.number())); |
| 472 | } |
| 473 | break; |
| 474 | case PORT_REMOVED: |
| 475 | log.debug("Port removed {}", port); |
Ray Milkey | cd6ab18 | 2016-02-03 11:13:09 -0800 | [diff] [blame] | 476 | removePort(port); |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 477 | providerService.linksVanished(new ConnectPoint(port.element().id(), |
| 478 | port.number())); |
| 479 | break; |
| 480 | case DEVICE_REMOVED: |
| 481 | case DEVICE_SUSPENDED: |
| 482 | log.debug("Device removed {}", deviceId); |
Ray Milkey | cd6ab18 | 2016-02-03 11:13:09 -0800 | [diff] [blame] | 483 | removeDevice(deviceId); |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 484 | providerService.linksVanished(deviceId); |
| 485 | break; |
| 486 | case DEVICE_AVAILABILITY_CHANGED: |
| 487 | if (deviceService.isAvailable(deviceId)) { |
| 488 | log.debug("Device up {}", deviceId); |
| 489 | updateDevice(device).ifPresent(ld -> updatePorts(ld, deviceId)); |
| 490 | } else { |
| 491 | log.debug("Device down {}", deviceId); |
Ray Milkey | cd6ab18 | 2016-02-03 11:13:09 -0800 | [diff] [blame] | 492 | removeDevice(deviceId); |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 493 | providerService.linksVanished(deviceId); |
| 494 | } |
| 495 | break; |
| 496 | case PORT_STATS_UPDATED: |
| 497 | break; |
| 498 | default: |
| 499 | log.debug("Unknown event {}", event); |
| 500 | } |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | private class InternalConfigListener implements NetworkConfigListener { |
| 505 | |
| 506 | private void addLink(LinkKey linkKey) { |
| 507 | configuredLinks.add(linkKey); |
| 508 | } |
| 509 | |
| 510 | private void removeLink(LinkKey linkKey) { |
| 511 | DefaultLinkDescription linkDescription = |
| 512 | new DefaultLinkDescription(linkKey.src(), linkKey.dst(), |
| 513 | Link.Type.DIRECT); |
| 514 | configuredLinks.remove(linkKey); |
| 515 | providerService.linkVanished(linkDescription); |
| 516 | } |
| 517 | |
| 518 | @Override |
| 519 | public void event(NetworkConfigEvent event) { |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 520 | if (event.configClass().equals(BasicLinkConfig.class)) { |
| 521 | log.info("net config event of type {} for basic link {}", |
| 522 | event.type(), event.subject()); |
| 523 | LinkKey linkKey = (LinkKey) event.subject(); |
| 524 | if (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED) { |
| 525 | addLink(linkKey); |
| 526 | } else if (event.type() == NetworkConfigEvent.Type.CONFIG_REMOVED) { |
| 527 | removeLink(linkKey); |
| 528 | } |
Charles Chan | e527eff | 2016-05-18 14:04:57 -0700 | [diff] [blame] | 529 | log.info("Link reconfigured"); |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 530 | } |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 531 | } |
| 532 | } |
| 533 | |
| 534 | } |