LLDP LinkDiscovery related fix

- avoid LinkDiscovery from start()-ing multiple times
- Only check device mastership at beginning of probe interval

Change-Id: I0cece8fbcf2a031ea3ac603c6f51dbe87ad454ad
diff --git a/providers/lldp/src/main/java/org/onlab/onos/provider/lldp/impl/LLDPLinkProvider.java b/providers/lldp/src/main/java/org/onlab/onos/provider/lldp/impl/LLDPLinkProvider.java
index 50dceb3..24336aa 100644
--- a/providers/lldp/src/main/java/org/onlab/onos/provider/lldp/impl/LLDPLinkProvider.java
+++ b/providers/lldp/src/main/java/org/onlab/onos/provider/lldp/impl/LLDPLinkProvider.java
@@ -145,7 +145,7 @@
                     break;
                 case PORT_ADDED:
                 case PORT_UPDATED:
-                    if (event.port().isEnabled()) {
+                    if (port.isEnabled()) {
                         ld = discoverers.get(device.id());
                         if (ld == null) {
                             return;
@@ -155,6 +155,7 @@
                             ld.addPort(port);
                         }
                     } else {
+                        log.debug("Port down {}", port);
                         ConnectPoint point = new ConnectPoint(device.id(),
                                                               port.number());
                         providerService.linksVanished(point);
diff --git a/providers/lldp/src/main/java/org/onlab/onos/provider/lldp/impl/LinkDiscovery.java b/providers/lldp/src/main/java/org/onlab/onos/provider/lldp/impl/LinkDiscovery.java
index dd72420..07485c0 100644
--- a/providers/lldp/src/main/java/org/onlab/onos/provider/lldp/impl/LinkDiscovery.java
+++ b/providers/lldp/src/main/java/org/onlab/onos/provider/lldp/impl/LinkDiscovery.java
@@ -68,6 +68,7 @@
     // send 1 probe every probeRate milliseconds
     private final long probeRate;
     private final Set<Long> slowPorts;
+    // ports, known to have incoming links
     private final Set<Long> fastPorts;
     // number of unacknowledged probes per port
     private final Map<Long, AtomicInteger> portProbeCount;
@@ -125,6 +126,7 @@
             log.info("Using BDDP to discover network");
         }
 
+        this.isStopped = true;
         start();
         this.log.debug("Started discovery manager for switch {}",
                        device.id());
@@ -140,7 +142,10 @@
     public void addPort(final Port port) {
         this.log.debug("Sending init probe to port {}@{}",
                        port.number().toLong(), device.id());
-        sendProbes(port.number().toLong());
+        boolean isMaster = mastershipService.getLocalRole(device.id()) == MASTER;
+        if (isMaster) {
+            sendProbes(port.number().toLong());
+        }
         synchronized (this) {
             this.slowPorts.add(port.number().toLong());
         }
@@ -233,6 +238,13 @@
      */
     @Override
     public void run(final Timeout t) {
+        boolean isMaster = mastershipService.getLocalRole(device.id()) == MASTER;
+        if (!isMaster) {
+            // reschedule timer
+            timeout = Timer.getTimer().newTimeout(this, this.probeRate, MILLISECONDS);
+            return;
+        }
+
         this.log.trace("Sending probes from {}", device.id());
         synchronized (this) {
             final Iterator<Long> fastIterator = this.fastPorts.iterator();
@@ -245,6 +257,7 @@
                     sendProbes(portNumber);
 
                 } else {
+                    // Link down, demote to slowPorts
                     // Update fast and slow ports
                     fastIterator.remove();
                     this.slowPorts.add(portNumber);
@@ -274,8 +287,12 @@
     }
 
     public void start() {
-        timeout = Timer.getTimer().newTimeout(this, 0, MILLISECONDS);
-        isStopped = false;
+        if (isStopped) {
+            timeout = Timer.getTimer().newTimeout(this, 0, MILLISECONDS);
+            isStopped = false;
+        } else {
+            log.warn("LinkDiscovery started multiple times?");
+        }
     }
 
     /**
@@ -317,8 +334,8 @@
     }
 
     private void sendProbes(Long portNumber) {
-        boolean isMaster = mastershipService.getLocalRole(device.id()) == MASTER;
-        if (isMaster && device.type() != Device.Type.ROADM) {
+        // TODO: should have suppression port configuration, not by type
+        if (device.type() != Device.Type.ROADM) {
             log.debug("Sending probes out to {}@{}", portNumber, device.id());
             OutboundPacket pkt = this.createOutBoundLLDP(portNumber);
             pktService.emit(pkt);