summaryrefslogtreecommitdiffstats
path: root/framework/src/onos/providers/lldp/src/main/java/org/onosproject/provider
diff options
context:
space:
mode:
authorAshlee Young <ashlee@onosfw.com>2015-09-09 22:15:21 -0700
committerAshlee Young <ashlee@onosfw.com>2015-09-09 22:15:21 -0700
commit13d05bc8458758ee39cb829098241e89616717ee (patch)
tree22a4d1ce65f15952f07a3df5af4b462b4697cb3a /framework/src/onos/providers/lldp/src/main/java/org/onosproject/provider
parent6139282e1e93c2322076de4b91b1c85d0bc4a8b3 (diff)
ONOS checkin based on commit tag e796610b1f721d02f9b0e213cf6f7790c10ecd60
Change-Id: Ife8810491034fe7becdba75dda20de4267bd15cd
Diffstat (limited to 'framework/src/onos/providers/lldp/src/main/java/org/onosproject/provider')
-rw-r--r--framework/src/onos/providers/lldp/src/main/java/org/onosproject/provider/lldp/impl/LLDPLinkProvider.java452
-rw-r--r--framework/src/onos/providers/lldp/src/main/java/org/onosproject/provider/lldp/impl/LinkDiscovery.java369
-rw-r--r--framework/src/onos/providers/lldp/src/main/java/org/onosproject/provider/lldp/impl/SuppressionRules.java106
-rw-r--r--framework/src/onos/providers/lldp/src/main/java/org/onosproject/provider/lldp/impl/SuppressionRulesStore.java174
-rw-r--r--framework/src/onos/providers/lldp/src/main/java/org/onosproject/provider/lldp/impl/package-info.java20
5 files changed, 1121 insertions, 0 deletions
diff --git a/framework/src/onos/providers/lldp/src/main/java/org/onosproject/provider/lldp/impl/LLDPLinkProvider.java b/framework/src/onos/providers/lldp/src/main/java/org/onosproject/provider/lldp/impl/LLDPLinkProvider.java
new file mode 100644
index 00000000..e9e2bfad
--- /dev/null
+++ b/framework/src/onos/providers/lldp/src/main/java/org/onosproject/provider/lldp/impl/LLDPLinkProvider.java
@@ -0,0 +1,452 @@
+/*
+ * Copyright 2014-2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.provider.lldp.impl;
+
+import com.google.common.base.Strings;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableSet;
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Modified;
+import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.onlab.packet.Ethernet;
+import org.onosproject.cfg.ComponentConfigService;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.mastership.MastershipEvent;
+import org.onosproject.mastership.MastershipListener;
+import org.onosproject.mastership.MastershipService;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.Device;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.Port;
+import org.onosproject.net.device.DeviceEvent;
+import org.onosproject.net.device.DeviceListener;
+import org.onosproject.net.device.DeviceService;
+import org.onosproject.net.flow.DefaultTrafficSelector;
+import org.onosproject.net.flow.TrafficSelector;
+import org.onosproject.net.link.LinkProvider;
+import org.onosproject.net.link.LinkProviderRegistry;
+import org.onosproject.net.link.LinkProviderService;
+import org.onosproject.net.packet.PacketContext;
+import org.onosproject.net.packet.PacketPriority;
+import org.onosproject.net.packet.PacketProcessor;
+import org.onosproject.net.packet.PacketService;
+import org.onosproject.net.provider.AbstractProvider;
+import org.onosproject.net.provider.ProviderId;
+import org.osgi.service.component.ComponentContext;
+import org.slf4j.Logger;
+
+import java.io.IOException;
+import java.util.Dictionary;
+import java.util.EnumSet;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ScheduledExecutorService;
+
+import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
+import static java.util.concurrent.TimeUnit.SECONDS;
+import static org.onlab.util.Tools.get;
+import static org.onlab.util.Tools.groupedThreads;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Provider which uses an OpenFlow controller to detect network
+ * infrastructure links.
+ */
+@Component(immediate = true)
+public class LLDPLinkProvider extends AbstractProvider implements LinkProvider {
+
+ private static final String PROVIDER_NAME = "org.onosproject.provider.lldp";
+
+ private static final String PROP_USE_BDDP = "useBDDP";
+ private static final String PROP_DISABLE_LD = "disableLinkDiscovery";
+ private static final String PROP_LLDP_SUPPRESSION = "lldpSuppression";
+
+ private static final String DEFAULT_LLDP_SUPPRESSION_CONFIG = "../config/lldp_suppression.json";
+
+ private final Logger log = getLogger(getClass());
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected CoreService coreService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected LinkProviderRegistry providerRegistry;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected DeviceService deviceService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected PacketService packetService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected MastershipService masterService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected ComponentConfigService cfgService;
+
+ private LinkProviderService providerService;
+
+ private ScheduledExecutorService executor;
+
+ @Property(name = PROP_USE_BDDP, boolValue = true,
+ label = "Use BDDP for link discovery")
+ private boolean useBDDP = true;
+
+ @Property(name = PROP_DISABLE_LD, boolValue = false,
+ label = "Permanently disable link discovery")
+ private boolean disableLinkDiscovery = false;
+
+ private static final long INIT_DELAY = 5;
+ private static final long DELAY = 5;
+
+ @Property(name = PROP_LLDP_SUPPRESSION, value = DEFAULT_LLDP_SUPPRESSION_CONFIG,
+ label = "Path to LLDP suppression configuration file")
+ private String lldpSuppression = DEFAULT_LLDP_SUPPRESSION_CONFIG;
+
+
+ private final InternalLinkProvider listener = new InternalLinkProvider();
+
+ private final InternalRoleListener roleListener = new InternalRoleListener();
+
+ protected final Map<DeviceId, LinkDiscovery> discoverers = new ConcurrentHashMap<>();
+
+ private SuppressionRules rules;
+ private ApplicationId appId;
+
+ /**
+ * Creates an OpenFlow link provider.
+ */
+ public LLDPLinkProvider() {
+ super(new ProviderId("lldp", PROVIDER_NAME));
+ }
+
+ @Activate
+ public void activate(ComponentContext context) {
+ cfgService.registerProperties(getClass());
+ appId = coreService.registerApplication(PROVIDER_NAME);
+
+ // to load configuration at startup
+ modified(context);
+ if (disableLinkDiscovery) {
+ log.info("LinkDiscovery has been permanently disabled by configuration");
+ return;
+ }
+
+ providerService = providerRegistry.register(this);
+ deviceService.addListener(listener);
+ packetService.addProcessor(listener, PacketProcessor.advisor(0));
+ masterService.addListener(roleListener);
+
+ LinkDiscovery ld;
+ for (Device device : deviceService.getAvailableDevices()) {
+ if (rules.isSuppressed(device)) {
+ log.debug("LinkDiscovery from {} disabled by configuration", device.id());
+ continue;
+ }
+ ld = new LinkDiscovery(device, packetService, masterService,
+ providerService, useBDDP);
+ discoverers.put(device.id(), ld);
+ addPorts(ld, device.id());
+ }
+
+ executor = newSingleThreadScheduledExecutor(groupedThreads("onos/device", "sync-%d"));
+ executor.scheduleAtFixedRate(new SyncDeviceInfoTask(), INIT_DELAY, DELAY, SECONDS);
+
+ requestIntercepts();
+
+ log.info("Started");
+ }
+
+ private void addPorts(LinkDiscovery discoverer, DeviceId deviceId) {
+ for (Port p : deviceService.getPorts(deviceId)) {
+ if (rules.isSuppressed(p)) {
+ continue;
+ }
+ if (!p.number().isLogical()) {
+ discoverer.addPort(p);
+ }
+ }
+ }
+
+ @Deactivate
+ public void deactivate() {
+ cfgService.unregisterProperties(getClass(), false);
+ if (disableLinkDiscovery) {
+ return;
+ }
+
+ withdrawIntercepts();
+
+ providerRegistry.unregister(this);
+ deviceService.removeListener(listener);
+ packetService.removeProcessor(listener);
+ masterService.removeListener(roleListener);
+
+ executor.shutdownNow();
+ discoverers.values().forEach(LinkDiscovery::stop);
+ discoverers.clear();
+ providerService = null;
+
+ log.info("Stopped");
+ }
+
+ @Modified
+ public void modified(ComponentContext context) {
+ if (context == null) {
+ loadSuppressionRules();
+ return;
+ }
+ @SuppressWarnings("rawtypes")
+ Dictionary properties = context.getProperties();
+
+ String s = get(properties, PROP_DISABLE_LD);
+ if (!Strings.isNullOrEmpty(s)) {
+ disableLinkDiscovery = Boolean.valueOf(s);
+ }
+ s = get(properties, PROP_USE_BDDP);
+ if (!Strings.isNullOrEmpty(s)) {
+ useBDDP = Boolean.valueOf(s);
+ }
+ s = get(properties, PROP_LLDP_SUPPRESSION);
+ if (!Strings.isNullOrEmpty(s)) {
+ lldpSuppression = s;
+ }
+ requestIntercepts();
+ loadSuppressionRules();
+ }
+
+ private void loadSuppressionRules() {
+ SuppressionRulesStore store = new SuppressionRulesStore(lldpSuppression);
+ try {
+ log.info("Reading suppression rules from {}", lldpSuppression);
+ rules = store.read();
+ } catch (IOException e) {
+ log.info("Failed to load {}, using built-in rules", lldpSuppression);
+ // default rule to suppress ROADM to maintain compatibility
+ rules = new SuppressionRules(ImmutableSet.of(),
+ EnumSet.of(Device.Type.ROADM),
+ ImmutableMap.of());
+ }
+
+ // should refresh discoverers when we need dynamic reconfiguration
+ }
+
+ /**
+ * Request packet intercepts.
+ */
+ private void requestIntercepts() {
+ TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
+ selector.matchEthType(Ethernet.TYPE_LLDP);
+ packetService.requestPackets(selector.build(), PacketPriority.CONTROL, appId);
+
+ selector.matchEthType(Ethernet.TYPE_BSN);
+ if (useBDDP) {
+ packetService.requestPackets(selector.build(), PacketPriority.CONTROL, appId);
+ } else {
+ packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
+ }
+ }
+
+ /**
+ * Withdraw packet intercepts.
+ */
+ private void withdrawIntercepts() {
+ TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
+ selector.matchEthType(Ethernet.TYPE_LLDP);
+ packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
+ selector.matchEthType(Ethernet.TYPE_BSN);
+ packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
+ }
+
+ private LinkDiscovery createLinkDiscovery(Device device) {
+ return new LinkDiscovery(device, packetService, masterService,
+ providerService, useBDDP);
+ }
+
+ private class InternalRoleListener implements MastershipListener {
+
+ @Override
+ public void event(MastershipEvent event) {
+ if (MastershipEvent.Type.BACKUPS_CHANGED.equals(event.type())) {
+ // only need new master events
+ return;
+ }
+
+ DeviceId deviceId = event.subject();
+ Device device = deviceService.getDevice(deviceId);
+ if (device == null) {
+ log.debug("Device {} doesn't exist, or isn't there yet", deviceId);
+ return;
+ }
+ if (rules.isSuppressed(device)) {
+ return;
+ }
+ synchronized (discoverers) {
+ if (!discoverers.containsKey(deviceId)) {
+ // ideally, should never reach here
+ log.debug("Device mastership changed ({}) {}", event.type(), deviceId);
+ discoverers.put(deviceId, createLinkDiscovery(device));
+ }
+ }
+ }
+
+ }
+
+ private class InternalLinkProvider implements PacketProcessor, DeviceListener {
+
+ @Override
+ public void event(DeviceEvent event) {
+ LinkDiscovery ld = null;
+ Device device = event.subject();
+ Port port = event.port();
+ if (device == null) {
+ log.error("Device is null.");
+ return;
+ }
+ log.trace("{} {} {}", event.type(), event.subject(), event);
+ final DeviceId deviceId = device.id();
+ switch (event.type()) {
+ case DEVICE_ADDED:
+ case DEVICE_UPDATED:
+ synchronized (discoverers) {
+ ld = discoverers.get(deviceId);
+ if (ld == null) {
+ if (rules.isSuppressed(device)) {
+ log.debug("LinkDiscovery from {} disabled by configuration", device.id());
+ return;
+ }
+ log.debug("Device added ({}) {}", event.type(), deviceId);
+ discoverers.put(deviceId, createLinkDiscovery(device));
+ } else {
+ if (ld.isStopped()) {
+ log.debug("Device restarted ({}) {}", event.type(), deviceId);
+ ld.start();
+ }
+ }
+ }
+ break;
+ case PORT_ADDED:
+ case PORT_UPDATED:
+ if (port.isEnabled()) {
+ ld = discoverers.get(deviceId);
+ if (ld == null) {
+ return;
+ }
+ if (rules.isSuppressed(port)) {
+ log.debug("LinkDiscovery from {}@{} disabled by configuration",
+ port.number(), device.id());
+ return;
+ }
+ if (!port.number().isLogical()) {
+ log.debug("Port added {}", port);
+ ld.addPort(port);
+ }
+ } else {
+ log.debug("Port down {}", port);
+ ConnectPoint point = new ConnectPoint(deviceId, port.number());
+ providerService.linksVanished(point);
+ }
+ break;
+ case PORT_REMOVED:
+ log.debug("Port removed {}", port);
+ ConnectPoint point = new ConnectPoint(deviceId, port.number());
+ providerService.linksVanished(point);
+
+ break;
+ case DEVICE_REMOVED:
+ case DEVICE_SUSPENDED:
+ log.debug("Device removed {}", deviceId);
+ ld = discoverers.get(deviceId);
+ if (ld == null) {
+ return;
+ }
+ ld.stop();
+ providerService.linksVanished(deviceId);
+ break;
+ case DEVICE_AVAILABILITY_CHANGED:
+ ld = discoverers.get(deviceId);
+ if (ld == null) {
+ return;
+ }
+ if (deviceService.isAvailable(deviceId)) {
+ log.debug("Device up {}", deviceId);
+ ld.start();
+ } else {
+ providerService.linksVanished(deviceId);
+ log.debug("Device down {}", deviceId);
+ ld.stop();
+ }
+ break;
+ case PORT_STATS_UPDATED:
+ break;
+ default:
+ log.debug("Unknown event {}", event);
+ }
+ }
+
+ @Override
+ public void process(PacketContext context) {
+ if (context == null) {
+ return;
+ }
+ LinkDiscovery ld = discoverers.get(context.inPacket().receivedFrom().deviceId());
+ if (ld == null) {
+ return;
+ }
+
+ if (ld.handleLLDP(context)) {
+ context.block();
+ }
+ }
+ }
+
+ private final class SyncDeviceInfoTask implements Runnable {
+
+ @Override
+ public void run() {
+ if (Thread.currentThread().isInterrupted()) {
+ log.info("Interrupted, quitting");
+ return;
+ }
+ // check what deviceService sees, to see if we are missing anything
+ try {
+ for (Device dev : deviceService.getDevices()) {
+ if (rules.isSuppressed(dev)) {
+ continue;
+ }
+ DeviceId did = dev.id();
+ synchronized (discoverers) {
+ LinkDiscovery discoverer = discoverers.get(did);
+ if (discoverer == null) {
+ discoverer = createLinkDiscovery(dev);
+ discoverers.put(did, discoverer);
+ }
+
+ addPorts(discoverer, did);
+ }
+ }
+ } catch (Exception e) {
+ // catch all Exception to avoid Scheduled task being suppressed.
+ log.error("Exception thrown during synchronization process", e);
+ }
+ }
+ }
+
+}
diff --git a/framework/src/onos/providers/lldp/src/main/java/org/onosproject/provider/lldp/impl/LinkDiscovery.java b/framework/src/onos/providers/lldp/src/main/java/org/onosproject/provider/lldp/impl/LinkDiscovery.java
new file mode 100644
index 00000000..a81eeb1d
--- /dev/null
+++ b/framework/src/onos/providers/lldp/src/main/java/org/onosproject/provider/lldp/impl/LinkDiscovery.java
@@ -0,0 +1,369 @@
+/*
+ * Copyright 2014-2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.provider.lldp.impl;
+
+import com.google.common.collect.Maps;
+import com.google.common.collect.Sets;
+import org.jboss.netty.util.Timeout;
+import org.jboss.netty.util.TimerTask;
+import org.onlab.packet.Ethernet;
+import org.onlab.packet.ONOSLLDP;
+import org.onlab.util.Timer;
+import org.onosproject.mastership.MastershipService;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.Device;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.Link.Type;
+import org.onosproject.net.Port;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.link.DefaultLinkDescription;
+import org.onosproject.net.link.LinkDescription;
+import org.onosproject.net.link.LinkProviderService;
+import org.onosproject.net.packet.DefaultOutboundPacket;
+import org.onosproject.net.packet.OutboundPacket;
+import org.onosproject.net.packet.PacketContext;
+import org.onosproject.net.packet.PacketService;
+import org.slf4j.Logger;
+
+import java.nio.ByteBuffer;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static java.util.concurrent.TimeUnit.MILLISECONDS;
+import static org.onosproject.net.PortNumber.portNumber;
+import static org.onosproject.net.flow.DefaultTrafficTreatment.builder;
+import static org.slf4j.LoggerFactory.getLogger;
+
+// TODO: add 'fast discovery' mode: drop LLDPs in destination switch but listen for flow_removed messages
+// FIXME: add ability to track links using port pairs or the link inventory
+
+/**
+ * Run discovery process from a physical switch. Ports are initially labeled as
+ * slow ports. When an LLDP is successfully received, label the remote port as
+ * fast. Every probeRate milliseconds, loop over all fast ports and send an
+ * LLDP, send an LLDP for a single slow port. Based on FlowVisor topology
+ * discovery implementation.
+ */
+public class LinkDiscovery implements TimerTask {
+
+ private final Logger log = getLogger(getClass());
+
+ private static final short MAX_PROBE_COUNT = 3; // probes to send before link is removed
+ private static final short DEFAULT_PROBE_RATE = 3000; // millis
+ private static final String SRC_MAC = "DE:AD:BE:EF:BA:11";
+ private static final String SERVICE_NULL = "Service cannot be null";
+
+ private final Device device;
+
+ // send 1 probe every probeRate milliseconds
+ private final long probeRate = DEFAULT_PROBE_RATE;
+
+ private final Set<Long> slowPorts = Sets.newConcurrentHashSet();
+ // ports, known to have incoming links
+ private final Set<Long> fastPorts = Sets.newConcurrentHashSet();
+
+ // number of unacknowledged probes per port
+ private final Map<Long, AtomicInteger> portProbeCount = Maps.newHashMap();
+
+ private final ONOSLLDP lldpPacket;
+ private final Ethernet ethPacket;
+ private Ethernet bddpEth;
+ private final boolean useBDDP;
+
+ private Timeout timeout;
+ private volatile boolean isStopped;
+
+ private final LinkProviderService linkProvider;
+ private final PacketService pktService;
+ private final MastershipService mastershipService;
+
+ /**
+ * Instantiates discovery manager for the given physical switch. Creates a
+ * generic LLDP packet that will be customized for the port it is sent out on.
+ * Starts the the timer for the discovery process.
+ *
+ * @param device the physical switch
+ * @param pktService packet service
+ * @param masterService mastership service
+ * @param providerService link provider service
+ * @param useBDDP flag to also use BDDP for discovery
+ */
+ public LinkDiscovery(Device device, PacketService pktService,
+ MastershipService masterService,
+ LinkProviderService providerService, Boolean... useBDDP) {
+ this.device = device;
+ this.linkProvider = checkNotNull(providerService, SERVICE_NULL);
+ this.pktService = checkNotNull(pktService, SERVICE_NULL);
+ this.mastershipService = checkNotNull(masterService, SERVICE_NULL);
+
+ lldpPacket = new ONOSLLDP();
+ lldpPacket.setChassisId(device.chassisId());
+ lldpPacket.setDevice(device.id().toString());
+
+ ethPacket = new Ethernet();
+ ethPacket.setEtherType(Ethernet.TYPE_LLDP);
+ ethPacket.setDestinationMACAddress(ONOSLLDP.LLDP_NICIRA);
+ ethPacket.setPayload(this.lldpPacket);
+ ethPacket.setPad(true);
+
+ this.useBDDP = useBDDP.length > 0 ? useBDDP[0] : false;
+ if (this.useBDDP) {
+ bddpEth = new Ethernet();
+ bddpEth.setPayload(lldpPacket);
+ bddpEth.setEtherType(Ethernet.TYPE_BSN);
+ bddpEth.setDestinationMACAddress(ONOSLLDP.BDDP_MULTICAST);
+ bddpEth.setPad(true);
+ log.info("Using BDDP to discover network");
+ }
+
+ isStopped = true;
+ start();
+ log.debug("Started discovery manager for switch {}", device.id());
+
+ }
+
+ /**
+ * Add physical port port to discovery process.
+ * Send out initial LLDP and label it as slow port.
+ *
+ * @param port the port
+ */
+ public void addPort(Port port) {
+ boolean newPort = false;
+ synchronized (this) {
+ if (!containsPort(port.number().toLong())) {
+ newPort = true;
+ slowPorts.add(port.number().toLong());
+ }
+ }
+
+ boolean isMaster = mastershipService.isLocalMaster(device.id());
+ if (newPort && isMaster) {
+ log.debug("Sending init probe to port {}@{}", port.number().toLong(), device.id());
+ sendProbes(port.number().toLong());
+ }
+ }
+
+ /**
+ * Removes physical port from discovery process.
+ *
+ * @param port the port
+ */
+ public void removePort(Port port) {
+ // Ignore ports that are not on this switch
+ long portnum = port.number().toLong();
+ synchronized (this) {
+ if (slowPorts.contains(portnum)) {
+ slowPorts.remove(portnum);
+
+ } else if (fastPorts.contains(portnum)) {
+ fastPorts.remove(portnum);
+ portProbeCount.remove(portnum);
+ // no iterator to update
+ } else {
+ log.warn("Tried to dynamically remove non-existing port {}", portnum);
+ }
+ }
+ }
+
+ /**
+ * Method called by remote port to acknowledge receipt of LLDP sent by
+ * this port. If slow port, updates label to fast. If fast port, decrements
+ * number of unacknowledged probes.
+ *
+ * @param portNumber the port
+ */
+ public void ackProbe(Long portNumber) {
+ synchronized (this) {
+ if (slowPorts.contains(portNumber)) {
+ log.debug("Setting slow port to fast: {}:{}", device.id(), portNumber);
+ slowPorts.remove(portNumber);
+ fastPorts.add(portNumber);
+ portProbeCount.put(portNumber, new AtomicInteger(0));
+ } else if (fastPorts.contains(portNumber)) {
+ portProbeCount.get(portNumber).set(0);
+ } else {
+ log.debug("Got ackProbe for non-existing port: {}", portNumber);
+ }
+ }
+ }
+
+
+ /**
+ * Handles an incoming LLDP packet. Creates link in topology and sends ACK
+ * to port where LLDP originated.
+ *
+ * @param context packet context
+ * @return true if handled
+ */
+ public boolean handleLLDP(PacketContext context) {
+ Ethernet eth = context.inPacket().parsed();
+ if (eth == null) {
+ return false;
+ }
+
+ ONOSLLDP onoslldp = ONOSLLDP.parseONOSLLDP(eth);
+ if (onoslldp != null) {
+ PortNumber srcPort = portNumber(onoslldp.getPort());
+ PortNumber dstPort = context.inPacket().receivedFrom().port();
+ DeviceId srcDeviceId = DeviceId.deviceId(onoslldp.getDeviceString());
+ DeviceId dstDeviceId = context.inPacket().receivedFrom().deviceId();
+ ackProbe(dstPort.toLong());
+
+ ConnectPoint src = new ConnectPoint(srcDeviceId, srcPort);
+ ConnectPoint dst = new ConnectPoint(dstDeviceId, dstPort);
+
+ LinkDescription ld = eth.getEtherType() == Ethernet.TYPE_LLDP ?
+ new DefaultLinkDescription(src, dst, Type.DIRECT) :
+ new DefaultLinkDescription(src, dst, Type.INDIRECT);
+
+ try {
+ linkProvider.linkDetected(ld);
+ } catch (IllegalStateException e) {
+ return true;
+ }
+ return true;
+ }
+ return false;
+ }
+
+
+ /**
+ * Execute this method every t milliseconds. Loops over all ports
+ * labeled as fast and sends out an LLDP. Send out an LLDP on a single slow
+ * port.
+ *
+ * @param t timeout
+ */
+ @Override
+ public void run(Timeout t) {
+ if (isStopped()) {
+ return;
+ }
+ if (!mastershipService.isLocalMaster(device.id())) {
+ if (!isStopped()) {
+ // reschedule timer
+ timeout = Timer.getTimer().newTimeout(this, probeRate, MILLISECONDS);
+ }
+ return;
+ }
+
+ log.trace("Sending probes from {}", device.id());
+ synchronized (this) {
+ Iterator<Long> fastIterator = fastPorts.iterator();
+ while (fastIterator.hasNext()) {
+ long portNumber = fastIterator.next();
+ int probeCount = portProbeCount.get(portNumber).getAndIncrement();
+
+ if (probeCount < LinkDiscovery.MAX_PROBE_COUNT) {
+ log.trace("Sending fast probe to port {}", portNumber);
+ sendProbes(portNumber);
+
+ } else {
+ // Link down, demote to slowPorts; update fast and slow ports
+ fastIterator.remove();
+ slowPorts.add(portNumber);
+ portProbeCount.remove(portNumber);
+
+ ConnectPoint cp = new ConnectPoint(device.id(), portNumber(portNumber));
+ log.debug("Link down -> {}", cp);
+ linkProvider.linksVanished(cp);
+ }
+ }
+
+ // send a probe for the next slow port
+ for (long portNumber : slowPorts) {
+ log.trace("Sending slow probe to port {}", portNumber);
+ sendProbes(portNumber);
+ }
+ }
+
+ if (!isStopped()) {
+ // reschedule timer
+ timeout = Timer.getTimer().newTimeout(this, probeRate, MILLISECONDS);
+ }
+ }
+
+ public synchronized void stop() {
+ isStopped = true;
+ timeout.cancel();
+ }
+
+ public synchronized void start() {
+ if (isStopped) {
+ isStopped = false;
+ timeout = Timer.getTimer().newTimeout(this, 0, MILLISECONDS);
+ } else {
+ log.warn("LinkDiscovery started multiple times?");
+ }
+ }
+
+ /**
+ * Creates packet_out LLDP for specified output port.
+ *
+ * @param port the port
+ * @return Packet_out message with LLDP data
+ */
+ private OutboundPacket createOutBoundLLDP(Long port) {
+ if (port == null) {
+ return null;
+ }
+ lldpPacket.setPortId(port.intValue());
+ ethPacket.setSourceMACAddress(SRC_MAC);
+ return new DefaultOutboundPacket(device.id(),
+ builder().setOutput(portNumber(port)).build(),
+ ByteBuffer.wrap(ethPacket.serialize()));
+ }
+
+ /**
+ * Creates packet_out BDDP for specified output port.
+ *
+ * @param port the port
+ * @return Packet_out message with LLDP data
+ */
+ private OutboundPacket createOutBoundBDDP(Long port) {
+ if (port == null) {
+ return null;
+ }
+ lldpPacket.setPortId(port.intValue());
+ bddpEth.setSourceMACAddress(SRC_MAC);
+ return new DefaultOutboundPacket(device.id(),
+ builder().setOutput(portNumber(port)).build(),
+ ByteBuffer.wrap(bddpEth.serialize()));
+ }
+
+ private void sendProbes(Long portNumber) {
+ log.trace("Sending probes out to {}@{}", portNumber, device.id());
+ OutboundPacket pkt = createOutBoundLLDP(portNumber);
+ pktService.emit(pkt);
+ if (useBDDP) {
+ OutboundPacket bpkt = createOutBoundBDDP(portNumber);
+ pktService.emit(bpkt);
+ }
+ }
+
+ public boolean containsPort(Long portNumber) {
+ return slowPorts.contains(portNumber) || fastPorts.contains(portNumber);
+ }
+
+ public synchronized boolean isStopped() {
+ return isStopped || timeout.isCancelled();
+ }
+
+}
diff --git a/framework/src/onos/providers/lldp/src/main/java/org/onosproject/provider/lldp/impl/SuppressionRules.java b/framework/src/onos/providers/lldp/src/main/java/org/onosproject/provider/lldp/impl/SuppressionRules.java
new file mode 100644
index 00000000..27c75ebd
--- /dev/null
+++ b/framework/src/onos/providers/lldp/src/main/java/org/onosproject/provider/lldp/impl/SuppressionRules.java
@@ -0,0 +1,106 @@
+/*
+ * Copyright 2014-2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.provider.lldp.impl;
+
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+
+import org.onosproject.net.Annotations;
+import org.onosproject.net.Device;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.Element;
+import org.onosproject.net.Port;
+
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableSet;
+
+public class SuppressionRules {
+
+ public static final String ANY_VALUE = "(any)";
+
+ private final Set<DeviceId> suppressedDevice;
+ private final Set<Device.Type> suppressedDeviceType;
+ private final Map<String, String> suppressedAnnotation;
+
+ public SuppressionRules(Set<DeviceId> suppressedDevice,
+ Set<Device.Type> suppressedType,
+ Map<String, String> suppressedAnnotation) {
+
+ this.suppressedDevice = ImmutableSet.copyOf(suppressedDevice);
+ this.suppressedDeviceType = ImmutableSet.copyOf(suppressedType);
+ this.suppressedAnnotation = ImmutableMap.copyOf(suppressedAnnotation);
+ }
+
+ public boolean isSuppressed(Device device) {
+ if (suppressedDevice.contains(device.id())) {
+ return true;
+ }
+ if (suppressedDeviceType.contains(device.type())) {
+ return true;
+ }
+ final Annotations annotations = device.annotations();
+ if (containsSuppressionAnnotation(annotations)) {
+ return true;
+ }
+ return false;
+ }
+
+ public boolean isSuppressed(Port port) {
+ Element parent = port.element();
+ if (parent instanceof Device) {
+ if (isSuppressed((Device) parent)) {
+ return true;
+ }
+ }
+
+ final Annotations annotations = port.annotations();
+ if (containsSuppressionAnnotation(annotations)) {
+ return true;
+ }
+ return false;
+ }
+
+ private boolean containsSuppressionAnnotation(final Annotations annotations) {
+ for (Entry<String, String> entry : suppressedAnnotation.entrySet()) {
+ final String suppValue = entry.getValue();
+ final String suppKey = entry.getKey();
+ if (suppValue == ANY_VALUE) {
+ if (annotations.keys().contains(suppKey)) {
+ return true;
+ }
+ } else {
+ if (suppValue.equals(annotations.value(suppKey))) {
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+
+ Set<DeviceId> getSuppressedDevice() {
+ return suppressedDevice;
+ }
+
+ Set<Device.Type> getSuppressedDeviceType() {
+ return suppressedDeviceType;
+ }
+
+ Map<String, String> getSuppressedAnnotation() {
+ return suppressedAnnotation;
+ }
+}
diff --git a/framework/src/onos/providers/lldp/src/main/java/org/onosproject/provider/lldp/impl/SuppressionRulesStore.java b/framework/src/onos/providers/lldp/src/main/java/org/onosproject/provider/lldp/impl/SuppressionRulesStore.java
new file mode 100644
index 00000000..360bebd2
--- /dev/null
+++ b/framework/src/onos/providers/lldp/src/main/java/org/onosproject/provider/lldp/impl/SuppressionRulesStore.java
@@ -0,0 +1,174 @@
+/*
+ * Copyright 2014-2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.provider.lldp.impl;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.slf4j.LoggerFactory.getLogger;
+
+import com.fasterxml.jackson.core.JsonEncoding;
+import com.fasterxml.jackson.core.JsonFactory;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+
+import org.onosproject.net.Device;
+import org.onosproject.net.DeviceId;
+import org.slf4j.Logger;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.EnumSet;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+
+/*
+ * JSON file example
+ *
+
+{
+ "deviceId" : [ "of:2222000000000000" ],
+ "deviceType" : [ "ROADM" ],
+ "annotation" : { "no-lldp" : null, "sendLLDP" : "false" }
+}
+ */
+
+/**
+ * Allows for reading and writing LLDP suppression definition as a JSON file.
+ */
+public class SuppressionRulesStore {
+
+ private static final String DEVICE_ID = "deviceId";
+ private static final String DEVICE_TYPE = "deviceType";
+ private static final String ANNOTATION = "annotation";
+
+ private final Logger log = getLogger(getClass());
+
+ private final File file;
+
+ /**
+ * Creates a reader/writer of the LLDP suppression definition file.
+ *
+ * @param filePath location of the definition file
+ */
+ public SuppressionRulesStore(String filePath) {
+ file = new File(filePath);
+ }
+
+ /**
+ * Creates a reader/writer of the LLDP suppression definition file.
+ *
+ * @param file definition file
+ */
+ public SuppressionRulesStore(File file) {
+ this.file = checkNotNull(file);
+ }
+
+ /**
+ * Returns SuppressionRules.
+ *
+ * @return SuppressionRules
+ * @throws IOException if error occurred while reading the data
+ */
+ public SuppressionRules read() throws IOException {
+ final Set<DeviceId> suppressedDevice = new HashSet<>();
+ final EnumSet<Device.Type> suppressedDeviceType = EnumSet.noneOf(Device.Type.class);
+ final Map<String, String> suppressedAnnotation = new HashMap<>();
+
+ ObjectMapper mapper = new ObjectMapper();
+ ObjectNode root = (ObjectNode) mapper.readTree(file);
+
+ for (JsonNode deviceId : root.get(DEVICE_ID)) {
+ if (deviceId.isTextual()) {
+ suppressedDevice.add(DeviceId.deviceId(deviceId.asText()));
+ } else {
+ log.warn("Encountered unexpected JSONNode {} for deviceId", deviceId);
+ }
+ }
+
+ for (JsonNode deviceType : root.get(DEVICE_TYPE)) {
+ if (deviceType.isTextual()) {
+ suppressedDeviceType.add(Device.Type.valueOf(deviceType.asText()));
+ } else {
+ log.warn("Encountered unexpected JSONNode {} for deviceType", deviceType);
+ }
+ }
+
+ JsonNode annotation = root.get(ANNOTATION);
+ if (annotation.isObject()) {
+ ObjectNode obj = (ObjectNode) annotation;
+ Iterator<Entry<String, JsonNode>> it = obj.fields();
+ while (it.hasNext()) {
+ Entry<String, JsonNode> entry = it.next();
+ final String key = entry.getKey();
+ final JsonNode value = entry.getValue();
+
+ if (value.isValueNode()) {
+ if (value.isNull()) {
+ suppressedAnnotation.put(key, SuppressionRules.ANY_VALUE);
+ } else {
+ suppressedAnnotation.put(key, value.asText());
+ }
+ } else {
+ log.warn("Encountered unexpected JSON field {} for annotation", entry);
+ }
+ }
+ } else {
+ log.warn("Encountered unexpected JSONNode {} for annotation", annotation);
+ }
+
+ return new SuppressionRules(suppressedDevice,
+ suppressedDeviceType,
+ suppressedAnnotation);
+ }
+
+ /**
+ * Writes the given SuppressionRules.
+ *
+ * @param rules SuppressionRules
+ * @throws IOException if error occurred while writing the data
+ */
+ public void write(SuppressionRules rules) throws IOException {
+ ObjectMapper mapper = new ObjectMapper();
+ ObjectNode root = mapper.createObjectNode();
+ ArrayNode deviceIds = mapper.createArrayNode();
+ ArrayNode deviceTypes = mapper.createArrayNode();
+ ObjectNode annotations = mapper.createObjectNode();
+ root.set(DEVICE_ID, deviceIds);
+ root.set(DEVICE_TYPE, deviceTypes);
+ root.set(ANNOTATION, annotations);
+
+ rules.getSuppressedDevice()
+ .forEach(deviceId -> deviceIds.add(deviceId.toString()));
+
+ rules.getSuppressedDeviceType()
+ .forEach(type -> deviceTypes.add(type.toString()));
+
+ rules.getSuppressedAnnotation().forEach((key, value) -> {
+ if (value == SuppressionRules.ANY_VALUE) {
+ annotations.putNull(key);
+ } else {
+ annotations.put(key, value);
+ }
+ });
+ mapper.writeTree(new JsonFactory().createGenerator(file, JsonEncoding.UTF8),
+ root);
+ }
+}
diff --git a/framework/src/onos/providers/lldp/src/main/java/org/onosproject/provider/lldp/impl/package-info.java b/framework/src/onos/providers/lldp/src/main/java/org/onosproject/provider/lldp/impl/package-info.java
new file mode 100644
index 00000000..768a6cd2
--- /dev/null
+++ b/framework/src/onos/providers/lldp/src/main/java/org/onosproject/provider/lldp/impl/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2014 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Provider that uses the core as a means of infrastructure link inference.
+ */
+package org.onosproject.provider.lldp.impl;