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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.provider.lldp.impl; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 17 | |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 18 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
| 19 | import static org.onlab.util.Tools.namedThreads; |
| 20 | import static org.slf4j.LoggerFactory.getLogger; |
| 21 | |
| 22 | import java.io.IOException; |
| 23 | import java.util.Dictionary; |
| 24 | import java.util.EnumSet; |
| 25 | import java.util.Map; |
| 26 | import java.util.concurrent.ConcurrentHashMap; |
| 27 | import java.util.concurrent.ScheduledExecutorService; |
| 28 | import java.util.concurrent.TimeUnit; |
| 29 | |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 30 | import org.apache.felix.scr.annotations.Activate; |
| 31 | import org.apache.felix.scr.annotations.Component; |
| 32 | import org.apache.felix.scr.annotations.Deactivate; |
Yuta HIGUCHI | 4128938 | 2014-12-19 17:47:12 -0800 | [diff] [blame] | 33 | import org.apache.felix.scr.annotations.Modified; |
| 34 | import org.apache.felix.scr.annotations.Property; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 35 | import org.apache.felix.scr.annotations.Reference; |
| 36 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 37 | import org.onlab.packet.Ethernet; |
| 38 | import org.onosproject.core.ApplicationId; |
| 39 | import org.onosproject.core.CoreService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 40 | import org.onosproject.mastership.MastershipEvent; |
| 41 | import org.onosproject.mastership.MastershipListener; |
| 42 | import org.onosproject.mastership.MastershipService; |
| 43 | import org.onosproject.net.ConnectPoint; |
| 44 | import org.onosproject.net.Device; |
| 45 | import org.onosproject.net.DeviceId; |
| 46 | import org.onosproject.net.Port; |
| 47 | import org.onosproject.net.device.DeviceEvent; |
| 48 | import org.onosproject.net.device.DeviceListener; |
| 49 | import org.onosproject.net.device.DeviceService; |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 50 | import org.onosproject.net.flow.DefaultTrafficSelector; |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 51 | import org.onosproject.net.flow.TrafficSelector; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 52 | import org.onosproject.net.link.LinkProvider; |
| 53 | import org.onosproject.net.link.LinkProviderRegistry; |
| 54 | import org.onosproject.net.link.LinkProviderService; |
| 55 | import org.onosproject.net.packet.PacketContext; |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 56 | import org.onosproject.net.packet.PacketPriority; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 57 | import org.onosproject.net.packet.PacketProcessor; |
| 58 | import org.onosproject.net.packet.PacketService; |
| 59 | import org.onosproject.net.provider.AbstractProvider; |
| 60 | import org.onosproject.net.provider.ProviderId; |
Yuta HIGUCHI | 4128938 | 2014-12-19 17:47:12 -0800 | [diff] [blame] | 61 | import org.osgi.service.component.ComponentContext; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 62 | import org.slf4j.Logger; |
| 63 | |
Yuta HIGUCHI | 4128938 | 2014-12-19 17:47:12 -0800 | [diff] [blame] | 64 | import com.google.common.base.Strings; |
| 65 | import com.google.common.collect.ImmutableMap; |
| 66 | import com.google.common.collect.ImmutableSet; |
| 67 | |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 68 | |
| 69 | /** |
| 70 | * Provider which uses an OpenFlow controller to detect network |
| 71 | * infrastructure links. |
| 72 | */ |
| 73 | @Component(immediate = true) |
| 74 | public class LLDPLinkProvider extends AbstractProvider implements LinkProvider { |
| 75 | |
Yuta HIGUCHI | 4128938 | 2014-12-19 17:47:12 -0800 | [diff] [blame] | 76 | private static final String PROP_USE_BDDP = "useBDDP"; |
| 77 | |
| 78 | private static final String PROP_LLDP_SUPPRESSION = "lldpSuppression"; |
| 79 | |
| 80 | private static final String DEFAULT_LLDP_SUPPRESSION_CONFIG = "../config/lldp_suppression.json"; |
| 81 | |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 82 | private final Logger log = getLogger(getClass()); |
| 83 | |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 84 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 85 | protected CoreService coreService; |
| 86 | |
| 87 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 88 | protected LinkProviderRegistry providerRegistry; |
| 89 | |
| 90 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 91 | protected DeviceService deviceService; |
| 92 | |
| 93 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 94 | protected PacketService packetSevice; |
| 95 | |
alshabib | 875d626 | 2014-10-17 16:19:40 -0700 | [diff] [blame] | 96 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 97 | protected MastershipService masterService; |
| 98 | |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 99 | private LinkProviderService providerService; |
| 100 | |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 101 | private ScheduledExecutorService executor; |
| 102 | |
Yuta HIGUCHI | 4128938 | 2014-12-19 17:47:12 -0800 | [diff] [blame] | 103 | @Property(name = PROP_USE_BDDP, boolValue = true, |
| 104 | label = "use BDDP for link discovery") |
| 105 | private boolean useBDDP = true; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 106 | |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 107 | private static final long INIT_DELAY = 5; |
| 108 | private static final long DELAY = 5; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 109 | |
Yuta HIGUCHI | 4128938 | 2014-12-19 17:47:12 -0800 | [diff] [blame] | 110 | @Property(name = PROP_LLDP_SUPPRESSION, value = DEFAULT_LLDP_SUPPRESSION_CONFIG, |
| 111 | label = "Path to LLDP suppression configuration file") |
| 112 | private String filePath = DEFAULT_LLDP_SUPPRESSION_CONFIG; |
| 113 | |
| 114 | |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 115 | private final InternalLinkProvider listener = new InternalLinkProvider(); |
| 116 | |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 117 | private final InternalRoleListener roleListener = new InternalRoleListener(); |
| 118 | |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 119 | protected final Map<DeviceId, LinkDiscovery> discoverers = new ConcurrentHashMap<>(); |
| 120 | |
Yuta HIGUCHI | 4128938 | 2014-12-19 17:47:12 -0800 | [diff] [blame] | 121 | private SuppressionRules rules; |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 122 | private ApplicationId appId; |
Yuta HIGUCHI | 4128938 | 2014-12-19 17:47:12 -0800 | [diff] [blame] | 123 | |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 124 | /** |
| 125 | * Creates an OpenFlow link provider. |
| 126 | */ |
| 127 | public LLDPLinkProvider() { |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 128 | super(new ProviderId("lldp", "org.onosproject.provider.lldp")); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | @Activate |
| 132 | public void activate() { |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 133 | appId = |
| 134 | coreService.registerApplication("org.onosproject.provider.lldp"); |
| 135 | |
Yuta HIGUCHI | 4128938 | 2014-12-19 17:47:12 -0800 | [diff] [blame] | 136 | loadSuppressionRules(); |
| 137 | |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 138 | providerService = providerRegistry.register(this); |
| 139 | deviceService.addListener(listener); |
| 140 | packetSevice.addProcessor(listener, 0); |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 141 | masterService.addListener(roleListener); |
| 142 | |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame] | 143 | LinkDiscovery ld; |
alshabib | 5dc5a34 | 2014-12-03 14:11:16 -0800 | [diff] [blame] | 144 | for (Device device : deviceService.getAvailableDevices()) { |
Yuta HIGUCHI | 4128938 | 2014-12-19 17:47:12 -0800 | [diff] [blame] | 145 | if (rules.isSuppressed(device)) { |
| 146 | log.debug("LinkDiscovery from {} disabled by configuration", device.id()); |
| 147 | continue; |
| 148 | } |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame] | 149 | ld = new LinkDiscovery(device, packetSevice, masterService, |
| 150 | providerService, useBDDP); |
| 151 | discoverers.put(device.id(), ld); |
| 152 | for (Port p : deviceService.getPorts(device.id())) { |
Yuta HIGUCHI | 4128938 | 2014-12-19 17:47:12 -0800 | [diff] [blame] | 153 | if (rules.isSuppressed(p)) { |
| 154 | log.debug("LinkDiscovery from {}@{} disabled by configuration", |
| 155 | p.number(), device.id()); |
| 156 | continue; |
| 157 | } |
Yuta HIGUCHI | 00b476f | 2014-10-25 21:33:07 -0700 | [diff] [blame] | 158 | if (!p.number().isLogical()) { |
| 159 | ld.addPort(p); |
| 160 | } |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame] | 161 | } |
| 162 | } |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 163 | |
Thomas Vachuska | 9ea3e6f | 2015-01-23 16:34:22 -0800 | [diff] [blame] | 164 | executor = newSingleThreadScheduledExecutor(namedThreads("onos-device-sync-%d")); |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 165 | executor.scheduleAtFixedRate(new SyncDeviceInfoTask(), INIT_DELAY, |
| 166 | DELAY, TimeUnit.SECONDS); |
| 167 | |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 168 | requestPackets(); |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 169 | |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 170 | log.info("Started"); |
| 171 | } |
| 172 | |
| 173 | @Deactivate |
| 174 | public void deactivate() { |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 175 | executor.shutdownNow(); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 176 | for (LinkDiscovery ld : discoverers.values()) { |
| 177 | ld.stop(); |
| 178 | } |
| 179 | providerRegistry.unregister(this); |
| 180 | deviceService.removeListener(listener); |
| 181 | packetSevice.removeProcessor(listener); |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 182 | masterService.removeListener(roleListener); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 183 | providerService = null; |
| 184 | |
| 185 | log.info("Stopped"); |
| 186 | } |
| 187 | |
Yuta HIGUCHI | 4128938 | 2014-12-19 17:47:12 -0800 | [diff] [blame] | 188 | @Modified |
| 189 | public void modified(ComponentContext context) { |
| 190 | if (context == null) { |
| 191 | return; |
| 192 | } |
| 193 | @SuppressWarnings("rawtypes") |
| 194 | Dictionary properties = context.getProperties(); |
| 195 | |
| 196 | String s = (String) properties.get(PROP_USE_BDDP); |
| 197 | if (Strings.isNullOrEmpty(s)) { |
| 198 | useBDDP = true; |
| 199 | } else { |
| 200 | useBDDP = Boolean.valueOf(s); |
| 201 | } |
| 202 | s = (String) properties.get(PROP_LLDP_SUPPRESSION); |
| 203 | if (Strings.isNullOrEmpty(s)) { |
| 204 | filePath = DEFAULT_LLDP_SUPPRESSION_CONFIG; |
| 205 | } else { |
| 206 | filePath = s; |
| 207 | } |
| 208 | |
| 209 | loadSuppressionRules(); |
| 210 | } |
| 211 | |
| 212 | private void loadSuppressionRules() { |
| 213 | SuppressionRulesStore store = new SuppressionRulesStore(filePath); |
| 214 | try { |
| 215 | rules = store.read(); |
| 216 | } catch (IOException e) { |
| 217 | log.info("Failed to load {}, using built-in rules", filePath); |
| 218 | // default rule to suppress ROADM to maintain compatibility |
| 219 | rules = new SuppressionRules(ImmutableSet.of(), |
| 220 | EnumSet.of(Device.Type.ROADM), |
| 221 | ImmutableMap.of()); |
| 222 | } |
| 223 | |
| 224 | // should refresh discoverers when we need dynamic reconfiguration |
| 225 | } |
| 226 | |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 227 | private void requestPackets() { |
| 228 | TrafficSelector.Builder lldpSelector = DefaultTrafficSelector.builder(); |
| 229 | lldpSelector.matchEthType(Ethernet.TYPE_LLDP); |
| 230 | packetSevice.requestPackets(lldpSelector.build(), |
| 231 | PacketPriority.CONTROL, appId); |
| 232 | |
| 233 | if (useBDDP) { |
| 234 | TrafficSelector.Builder bddpSelector = DefaultTrafficSelector.builder(); |
| 235 | bddpSelector.matchEthType(Ethernet.TYPE_BSN); |
| 236 | packetSevice.requestPackets(bddpSelector.build(), |
| 237 | PacketPriority.CONTROL, appId); |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 241 | private class InternalRoleListener implements MastershipListener { |
| 242 | |
| 243 | @Override |
| 244 | public void event(MastershipEvent event) { |
| 245 | |
| 246 | if (MastershipEvent.Type.BACKUPS_CHANGED.equals(event.type())) { |
| 247 | // only need new master events |
| 248 | return; |
| 249 | } |
| 250 | |
| 251 | DeviceId deviceId = event.subject(); |
| 252 | Device device = deviceService.getDevice(deviceId); |
| 253 | if (device == null) { |
| 254 | log.warn("Device {} doesn't exist, or isn't there yet", deviceId); |
| 255 | return; |
| 256 | } |
Yuta HIGUCHI | 4128938 | 2014-12-19 17:47:12 -0800 | [diff] [blame] | 257 | if (rules.isSuppressed(device)) { |
| 258 | return; |
| 259 | } |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 260 | synchronized (discoverers) { |
| 261 | if (!discoverers.containsKey(deviceId)) { |
alshabib | 4785eec | 2014-12-04 16:45:45 -0800 | [diff] [blame] | 262 | // ideally, should never reach here |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 263 | log.debug("Device mastership changed ({}) {}", |
| 264 | event.type(), deviceId); |
| 265 | discoverers.put(deviceId, new LinkDiscovery(device, |
| 266 | packetSevice, masterService, providerService, |
| 267 | useBDDP)); |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | } |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 273 | |
| 274 | private class InternalLinkProvider implements PacketProcessor, DeviceListener { |
| 275 | |
| 276 | @Override |
| 277 | public void event(DeviceEvent event) { |
| 278 | LinkDiscovery ld = null; |
| 279 | Device device = event.subject(); |
alshabib | acd9183 | 2014-10-17 14:38:41 -0700 | [diff] [blame] | 280 | Port port = event.port(); |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame] | 281 | if (device == null) { |
| 282 | log.error("Device is null."); |
| 283 | return; |
| 284 | } |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 285 | log.trace("{} {} {}", event.type(), event.subject(), event); |
Yuta HIGUCHI | d19f670 | 2014-10-31 15:23:25 -0700 | [diff] [blame] | 286 | final DeviceId deviceId = device.id(); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 287 | switch (event.type()) { |
| 288 | case DEVICE_ADDED: |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 289 | case DEVICE_UPDATED: |
Yuta HIGUCHI | 4128938 | 2014-12-19 17:47:12 -0800 | [diff] [blame] | 290 | synchronized (discoverers) { |
| 291 | ld = discoverers.get(deviceId); |
| 292 | if (ld == null) { |
| 293 | if (rules.isSuppressed(device)) { |
| 294 | log.debug("LinkDiscovery from {} disabled by configuration", device.id()); |
| 295 | return; |
| 296 | } |
| 297 | log.debug("Device added ({}) {}", event.type(), |
| 298 | deviceId); |
| 299 | discoverers.put(deviceId, new LinkDiscovery(device, |
| 300 | packetSevice, masterService, providerService, |
| 301 | useBDDP)); |
| 302 | } else { |
| 303 | if (ld.isStopped()) { |
| 304 | log.debug("Device restarted ({}) {}", event.type(), |
| 305 | deviceId); |
| 306 | ld.start(); |
| 307 | } |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 308 | } |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 309 | } |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 310 | break; |
| 311 | case PORT_ADDED: |
| 312 | case PORT_UPDATED: |
Yuta HIGUCHI | f672588 | 2014-10-29 15:25:51 -0700 | [diff] [blame] | 313 | if (port.isEnabled()) { |
Yuta HIGUCHI | d19f670 | 2014-10-31 15:23:25 -0700 | [diff] [blame] | 314 | ld = discoverers.get(deviceId); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 315 | if (ld == null) { |
| 316 | return; |
| 317 | } |
Yuta HIGUCHI | 4128938 | 2014-12-19 17:47:12 -0800 | [diff] [blame] | 318 | if (rules.isSuppressed(port)) { |
| 319 | log.debug("LinkDiscovery from {}@{} disabled by configuration", |
| 320 | port.number(), device.id()); |
| 321 | return; |
| 322 | } |
Yuta HIGUCHI | 00b476f | 2014-10-25 21:33:07 -0700 | [diff] [blame] | 323 | if (!port.number().isLogical()) { |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 324 | log.debug("Port added {}", port); |
Yuta HIGUCHI | 00b476f | 2014-10-25 21:33:07 -0700 | [diff] [blame] | 325 | ld.addPort(port); |
| 326 | } |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 327 | } else { |
Yuta HIGUCHI | f672588 | 2014-10-29 15:25:51 -0700 | [diff] [blame] | 328 | log.debug("Port down {}", port); |
Yuta HIGUCHI | d19f670 | 2014-10-31 15:23:25 -0700 | [diff] [blame] | 329 | ConnectPoint point = new ConnectPoint(deviceId, |
alshabib | acd9183 | 2014-10-17 14:38:41 -0700 | [diff] [blame] | 330 | port.number()); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 331 | providerService.linksVanished(point); |
| 332 | } |
| 333 | break; |
| 334 | case PORT_REMOVED: |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 335 | log.debug("Port removed {}", port); |
Yuta HIGUCHI | d19f670 | 2014-10-31 15:23:25 -0700 | [diff] [blame] | 336 | ConnectPoint point = new ConnectPoint(deviceId, |
alshabib | acd9183 | 2014-10-17 14:38:41 -0700 | [diff] [blame] | 337 | port.number()); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 338 | providerService.linksVanished(point); |
alshabib | 4785eec | 2014-12-04 16:45:45 -0800 | [diff] [blame] | 339 | |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 340 | break; |
| 341 | case DEVICE_REMOVED: |
| 342 | case DEVICE_SUSPENDED: |
Yuta HIGUCHI | d19f670 | 2014-10-31 15:23:25 -0700 | [diff] [blame] | 343 | log.debug("Device removed {}", deviceId); |
| 344 | ld = discoverers.get(deviceId); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 345 | if (ld == null) { |
| 346 | return; |
| 347 | } |
| 348 | ld.stop(); |
Yuta HIGUCHI | d19f670 | 2014-10-31 15:23:25 -0700 | [diff] [blame] | 349 | providerService.linksVanished(deviceId); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 350 | break; |
| 351 | case DEVICE_AVAILABILITY_CHANGED: |
Yuta HIGUCHI | d19f670 | 2014-10-31 15:23:25 -0700 | [diff] [blame] | 352 | ld = discoverers.get(deviceId); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 353 | if (ld == null) { |
| 354 | return; |
| 355 | } |
Yuta HIGUCHI | d19f670 | 2014-10-31 15:23:25 -0700 | [diff] [blame] | 356 | if (deviceService.isAvailable(deviceId)) { |
| 357 | log.debug("Device up {}", deviceId); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 358 | ld.start(); |
| 359 | } else { |
Yuta HIGUCHI | d19f670 | 2014-10-31 15:23:25 -0700 | [diff] [blame] | 360 | providerService.linksVanished(deviceId); |
| 361 | log.debug("Device down {}", deviceId); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 362 | ld.stop(); |
| 363 | } |
| 364 | break; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 365 | default: |
| 366 | log.debug("Unknown event {}", event); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | @Override |
| 371 | public void process(PacketContext context) { |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 372 | if (context == null) { |
| 373 | return; |
| 374 | } |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 375 | LinkDiscovery ld = discoverers.get( |
| 376 | context.inPacket().receivedFrom().deviceId()); |
| 377 | if (ld == null) { |
| 378 | return; |
| 379 | } |
| 380 | |
| 381 | if (ld.handleLLDP(context)) { |
| 382 | context.block(); |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 387 | private final class SyncDeviceInfoTask implements Runnable { |
| 388 | |
| 389 | @Override |
| 390 | public void run() { |
| 391 | if (Thread.currentThread().isInterrupted()) { |
| 392 | log.info("Interrupted, quitting"); |
| 393 | return; |
| 394 | } |
| 395 | // check what deviceService sees, to see if we are missing anything |
| 396 | try { |
| 397 | LinkDiscovery ld = null; |
| 398 | for (Device dev : deviceService.getDevices()) { |
Yuta HIGUCHI | 4128938 | 2014-12-19 17:47:12 -0800 | [diff] [blame] | 399 | if (rules.isSuppressed(dev)) { |
| 400 | continue; |
| 401 | } |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 402 | DeviceId did = dev.id(); |
| 403 | synchronized (discoverers) { |
| 404 | if (!discoverers.containsKey(did)) { |
| 405 | ld = new LinkDiscovery(dev, packetSevice, |
| 406 | masterService, providerService, useBDDP); |
| 407 | discoverers.put(did, ld); |
| 408 | for (Port p : deviceService.getPorts(did)) { |
Yuta HIGUCHI | 4128938 | 2014-12-19 17:47:12 -0800 | [diff] [blame] | 409 | if (rules.isSuppressed(p)) { |
| 410 | continue; |
| 411 | } |
Ayaka Koshibe | ccfa94c | 2014-11-20 11:15:52 -0800 | [diff] [blame] | 412 | if (!p.number().isLogical()) { |
| 413 | ld.addPort(p); |
| 414 | } |
| 415 | } |
| 416 | } |
| 417 | } |
| 418 | } |
| 419 | } catch (Exception e) { |
| 420 | // catch all Exception to avoid Scheduled task being suppressed. |
| 421 | log.error("Exception thrown during synchronization process", e); |
| 422 | } |
| 423 | } |
| 424 | } |
| 425 | |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 426 | } |