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