blob: 93504102f5d4764f4a8d168760f408f33a4a79c6 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska781d18b2014-10-27 10:31:25 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.provider.lldp.impl;
alshabib7911a052014-10-16 17:49:37 -070017
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080018import com.google.common.base.Strings;
19import com.google.common.collect.ImmutableMap;
20import com.google.common.collect.ImmutableSet;
alshabib7911a052014-10-16 17:49:37 -070021import org.apache.felix.scr.annotations.Activate;
22import org.apache.felix.scr.annotations.Component;
23import org.apache.felix.scr.annotations.Deactivate;
Yuta HIGUCHI41289382014-12-19 17:47:12 -080024import org.apache.felix.scr.annotations.Modified;
25import org.apache.felix.scr.annotations.Property;
alshabib7911a052014-10-16 17:49:37 -070026import org.apache.felix.scr.annotations.Reference;
27import org.apache.felix.scr.annotations.ReferenceCardinality;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080028import org.onlab.packet.Ethernet;
29import org.onosproject.core.ApplicationId;
30import org.onosproject.core.CoreService;
Brian O'Connorabafb502014-12-02 22:26:20 -080031import org.onosproject.mastership.MastershipEvent;
32import org.onosproject.mastership.MastershipListener;
33import org.onosproject.mastership.MastershipService;
34import org.onosproject.net.ConnectPoint;
35import org.onosproject.net.Device;
36import org.onosproject.net.DeviceId;
37import org.onosproject.net.Port;
38import org.onosproject.net.device.DeviceEvent;
39import org.onosproject.net.device.DeviceListener;
40import org.onosproject.net.device.DeviceService;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080041import org.onosproject.net.flow.DefaultTrafficSelector;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080042import org.onosproject.net.flow.TrafficSelector;
Brian O'Connorabafb502014-12-02 22:26:20 -080043import org.onosproject.net.link.LinkProvider;
44import org.onosproject.net.link.LinkProviderRegistry;
45import org.onosproject.net.link.LinkProviderService;
46import org.onosproject.net.packet.PacketContext;
Jonathan Hart3cfce8e2015-01-14 16:43:27 -080047import org.onosproject.net.packet.PacketPriority;
Brian O'Connorabafb502014-12-02 22:26:20 -080048import org.onosproject.net.packet.PacketProcessor;
49import org.onosproject.net.packet.PacketService;
50import org.onosproject.net.provider.AbstractProvider;
51import org.onosproject.net.provider.ProviderId;
Yuta HIGUCHI41289382014-12-19 17:47:12 -080052import org.osgi.service.component.ComponentContext;
alshabib7911a052014-10-16 17:49:37 -070053import org.slf4j.Logger;
54
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080055import java.io.IOException;
56import java.util.Dictionary;
57import java.util.EnumSet;
58import java.util.Map;
59import java.util.concurrent.ConcurrentHashMap;
60import java.util.concurrent.ScheduledExecutorService;
61
62import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
63import static java.util.concurrent.TimeUnit.SECONDS;
64import static org.onlab.util.Tools.groupedThreads;
65import static org.slf4j.LoggerFactory.getLogger;
Yuta HIGUCHI41289382014-12-19 17:47:12 -080066
alshabib7911a052014-10-16 17:49:37 -070067
68/**
69 * Provider which uses an OpenFlow controller to detect network
70 * infrastructure links.
71 */
72@Component(immediate = true)
73public class LLDPLinkProvider extends AbstractProvider implements LinkProvider {
74
Yuta HIGUCHI41289382014-12-19 17:47:12 -080075 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
alshabib7911a052014-10-16 17:49:37 -070081 private final Logger log = getLogger(getClass());
82
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080083 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
84 protected CoreService coreService;
85
86 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
alshabib7911a052014-10-16 17:49:37 -070087 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
alshabib875d6262014-10-17 16:19:40 -070095 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
96 protected MastershipService masterService;
97
alshabib7911a052014-10-16 17:49:37 -070098 private LinkProviderService providerService;
99
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800100 private ScheduledExecutorService executor;
101
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800102 @Property(name = PROP_USE_BDDP, boolValue = true,
103 label = "use BDDP for link discovery")
104 private boolean useBDDP = true;
alshabib7911a052014-10-16 17:49:37 -0700105
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800106 private static final long INIT_DELAY = 5;
107 private static final long DELAY = 5;
alshabib7911a052014-10-16 17:49:37 -0700108
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800109 @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
alshabib7911a052014-10-16 17:49:37 -0700114 private final InternalLinkProvider listener = new InternalLinkProvider();
115
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800116 private final InternalRoleListener roleListener = new InternalRoleListener();
117
alshabib7911a052014-10-16 17:49:37 -0700118 protected final Map<DeviceId, LinkDiscovery> discoverers = new ConcurrentHashMap<>();
119
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800120 private SuppressionRules rules;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800121 private ApplicationId appId;
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800122
alshabib7911a052014-10-16 17:49:37 -0700123 /**
124 * Creates an OpenFlow link provider.
125 */
126 public LLDPLinkProvider() {
Brian O'Connorabafb502014-12-02 22:26:20 -0800127 super(new ProviderId("lldp", "org.onosproject.provider.lldp"));
alshabib7911a052014-10-16 17:49:37 -0700128 }
129
130 @Activate
131 public void activate() {
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800132 appId =
133 coreService.registerApplication("org.onosproject.provider.lldp");
134
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800135 loadSuppressionRules();
136
alshabib7911a052014-10-16 17:49:37 -0700137 providerService = providerRegistry.register(this);
138 deviceService.addListener(listener);
139 packetSevice.addProcessor(listener, 0);
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800140 masterService.addListener(roleListener);
141
alshabibdfc7afb2014-10-21 20:13:27 -0700142 LinkDiscovery ld;
alshabib5dc5a342014-12-03 14:11:16 -0800143 for (Device device : deviceService.getAvailableDevices()) {
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800144 if (rules.isSuppressed(device)) {
145 log.debug("LinkDiscovery from {} disabled by configuration", device.id());
146 continue;
147 }
alshabibdfc7afb2014-10-21 20:13:27 -0700148 ld = new LinkDiscovery(device, packetSevice, masterService,
149 providerService, useBDDP);
150 discoverers.put(device.id(), ld);
151 for (Port p : deviceService.getPorts(device.id())) {
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800152 if (rules.isSuppressed(p)) {
153 log.debug("LinkDiscovery from {}@{} disabled by configuration",
154 p.number(), device.id());
155 continue;
156 }
Yuta HIGUCHI00b476f2014-10-25 21:33:07 -0700157 if (!p.number().isLogical()) {
158 ld.addPort(p);
159 }
alshabibdfc7afb2014-10-21 20:13:27 -0700160 }
161 }
alshabib7911a052014-10-16 17:49:37 -0700162
Thomas Vachuska6f94ded2015-02-21 14:02:38 -0800163 executor = newSingleThreadScheduledExecutor(groupedThreads("onos/device", "sync-%d"));
164 executor.scheduleAtFixedRate(new SyncDeviceInfoTask(), INIT_DELAY, DELAY, SECONDS);
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800165
Jonathan Hart3cfce8e2015-01-14 16:43:27 -0800166 requestPackets();
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800167
alshabib7911a052014-10-16 17:49:37 -0700168 log.info("Started");
169 }
170
171 @Deactivate
172 public void deactivate() {
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800173 executor.shutdownNow();
alshabib7911a052014-10-16 17:49:37 -0700174 for (LinkDiscovery ld : discoverers.values()) {
175 ld.stop();
176 }
177 providerRegistry.unregister(this);
178 deviceService.removeListener(listener);
179 packetSevice.removeProcessor(listener);
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800180 masterService.removeListener(roleListener);
alshabib7911a052014-10-16 17:49:37 -0700181 providerService = null;
182
183 log.info("Stopped");
184 }
185
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800186 @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 Hart3cfce8e2015-01-14 16:43:27 -0800225 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 Radoslavovd36a74b2015-01-09 11:59:07 -0800236 }
237 }
238
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800239 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 HIGUCHI41289382014-12-19 17:47:12 -0800255 if (rules.isSuppressed(device)) {
256 return;
257 }
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800258 synchronized (discoverers) {
259 if (!discoverers.containsKey(deviceId)) {
alshabib4785eec2014-12-04 16:45:45 -0800260 // ideally, should never reach here
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800261 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 }
alshabib7911a052014-10-16 17:49:37 -0700271
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();
alshabibacd91832014-10-17 14:38:41 -0700278 Port port = event.port();
alshabibdfc7afb2014-10-21 20:13:27 -0700279 if (device == null) {
280 log.error("Device is null.");
281 return;
282 }
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700283 log.trace("{} {} {}", event.type(), event.subject(), event);
Yuta HIGUCHId19f6702014-10-31 15:23:25 -0700284 final DeviceId deviceId = device.id();
alshabib7911a052014-10-16 17:49:37 -0700285 switch (event.type()) {
286 case DEVICE_ADDED:
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700287 case DEVICE_UPDATED:
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800288 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 Koshibeccfa94c2014-11-20 11:15:52 -0800306 }
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700307 }
alshabib7911a052014-10-16 17:49:37 -0700308 break;
309 case PORT_ADDED:
310 case PORT_UPDATED:
Yuta HIGUCHIf6725882014-10-29 15:25:51 -0700311 if (port.isEnabled()) {
Yuta HIGUCHId19f6702014-10-31 15:23:25 -0700312 ld = discoverers.get(deviceId);
alshabib7911a052014-10-16 17:49:37 -0700313 if (ld == null) {
314 return;
315 }
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800316 if (rules.isSuppressed(port)) {
317 log.debug("LinkDiscovery from {}@{} disabled by configuration",
318 port.number(), device.id());
319 return;
320 }
Yuta HIGUCHI00b476f2014-10-25 21:33:07 -0700321 if (!port.number().isLogical()) {
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700322 log.debug("Port added {}", port);
Yuta HIGUCHI00b476f2014-10-25 21:33:07 -0700323 ld.addPort(port);
324 }
alshabib7911a052014-10-16 17:49:37 -0700325 } else {
Yuta HIGUCHIf6725882014-10-29 15:25:51 -0700326 log.debug("Port down {}", port);
Yuta HIGUCHId19f6702014-10-31 15:23:25 -0700327 ConnectPoint point = new ConnectPoint(deviceId,
alshabibacd91832014-10-17 14:38:41 -0700328 port.number());
alshabib7911a052014-10-16 17:49:37 -0700329 providerService.linksVanished(point);
330 }
331 break;
332 case PORT_REMOVED:
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700333 log.debug("Port removed {}", port);
Yuta HIGUCHId19f6702014-10-31 15:23:25 -0700334 ConnectPoint point = new ConnectPoint(deviceId,
alshabibacd91832014-10-17 14:38:41 -0700335 port.number());
alshabib7911a052014-10-16 17:49:37 -0700336 providerService.linksVanished(point);
alshabib4785eec2014-12-04 16:45:45 -0800337
alshabib7911a052014-10-16 17:49:37 -0700338 break;
339 case DEVICE_REMOVED:
340 case DEVICE_SUSPENDED:
Yuta HIGUCHId19f6702014-10-31 15:23:25 -0700341 log.debug("Device removed {}", deviceId);
342 ld = discoverers.get(deviceId);
alshabib7911a052014-10-16 17:49:37 -0700343 if (ld == null) {
344 return;
345 }
346 ld.stop();
Yuta HIGUCHId19f6702014-10-31 15:23:25 -0700347 providerService.linksVanished(deviceId);
alshabib7911a052014-10-16 17:49:37 -0700348 break;
349 case DEVICE_AVAILABILITY_CHANGED:
Yuta HIGUCHId19f6702014-10-31 15:23:25 -0700350 ld = discoverers.get(deviceId);
alshabib7911a052014-10-16 17:49:37 -0700351 if (ld == null) {
352 return;
353 }
Yuta HIGUCHId19f6702014-10-31 15:23:25 -0700354 if (deviceService.isAvailable(deviceId)) {
355 log.debug("Device up {}", deviceId);
alshabib7911a052014-10-16 17:49:37 -0700356 ld.start();
357 } else {
Yuta HIGUCHId19f6702014-10-31 15:23:25 -0700358 providerService.linksVanished(deviceId);
359 log.debug("Device down {}", deviceId);
alshabib7911a052014-10-16 17:49:37 -0700360 ld.stop();
361 }
362 break;
alshabib7911a052014-10-16 17:49:37 -0700363 default:
364 log.debug("Unknown event {}", event);
365 }
366 }
367
368 @Override
369 public void process(PacketContext context) {
alshabib4a179dc2014-10-17 17:17:01 -0700370 if (context == null) {
371 return;
372 }
alshabib7911a052014-10-16 17:49:37 -0700373 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 Koshibeccfa94c2014-11-20 11:15:52 -0800385 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 HIGUCHI41289382014-12-19 17:47:12 -0800397 if (rules.isSuppressed(dev)) {
398 continue;
399 }
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800400 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 HIGUCHI41289382014-12-19 17:47:12 -0800407 if (rules.isSuppressed(p)) {
408 continue;
409 }
Ayaka Koshibeccfa94c2014-11-20 11:15:52 -0800410 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
alshabib7911a052014-10-16 17:49:37 -0700424}