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