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