Link discovery bug fixes.

* Don't send link events if you're not master for the destination switch
* Also, inter-instance links are now discovered unidirectionally (one by
  each instance) rather than bidirectionally by one of the instances.
  The first fix coupled with the strange inter-instance link discovery
  semantics meant that one of the links was dropped.

Change-Id: Ie94090f6ca41863959ecf8d2e8ae194c420bdf17
diff --git a/src/main/java/net/onrc/onos/core/linkdiscovery/internal/LinkDiscoveryManager.java b/src/main/java/net/onrc/onos/core/linkdiscovery/internal/LinkDiscoveryManager.java
index 3bbb60b..9be2313 100644
--- a/src/main/java/net/onrc/onos/core/linkdiscovery/internal/LinkDiscoveryManager.java
+++ b/src/main/java/net/onrc/onos/core/linkdiscovery/internal/LinkDiscoveryManager.java
@@ -861,7 +861,11 @@
                     log.trace("Getting standard LLDP from a different controller and quelching it.");
                 }
                 return Command.STOP;
-            } else if (sw <= remoteSwitch.getId()) {
+            }
+            // XXX ONOS: Don't disregard any BDDP messages from other
+            // controllers because they're used for inter-instance link detection
+
+            /*else if (sw <= remoteSwitch.getId()) {
                 if (log.isTraceEnabled()) {
                     log.trace("Getting BBDP from a different controller. myId {}: remoteId {}", myId, otherId);
                     log.trace("and my controller id is smaller than the other, so quelching it. myPort {}: rPort {}", pi.getInPort(), remotePort);
@@ -869,7 +873,7 @@
                 //XXX ONOS: Fix the BDDP broadcast issue
                 //return Command.CONTINUE;
                 return Command.STOP;
-            }
+            }*/
             /*
             else if (myId < otherId)  {
                 if (log.isTraceEnabled()) {
@@ -958,8 +962,12 @@
             }
         }
 
-        // If the received packet is a BDDP packet, then create a reverse BDDP
-        // link as well.
+        // XXX ONOS: Don't do this:
+        //   If the received packet is a BDDP packet, then create a reverse BDDP
+        //   link as well.
+        // We want to preserve our semantic of the instance that controls the
+        // destination switch is the one who adds the link to the database.
+        /*
         if (!isStandard) {
             Link reverseLink = new Link(lt.getDst(), lt.getDstPort(),
                     lt.getSrc(), lt.getSrcPort());
@@ -971,6 +979,7 @@
 
             addOrUpdateLink(reverseLink, reverseInfo);
         }
+        */
 
         // Remove the node ports from the quarantine and maintenance queues.
         NodePortTuple nptSrc = new NodePortTuple(lt.getSrc(), lt.getSrcPort());
diff --git a/src/main/java/net/onrc/onos/core/topology/TopologyPublisher.java b/src/main/java/net/onrc/onos/core/topology/TopologyPublisher.java
index 904ca3f..37e71c9 100644
--- a/src/main/java/net/onrc/onos/core/topology/TopologyPublisher.java
+++ b/src/main/java/net/onrc/onos/core/topology/TopologyPublisher.java
@@ -131,6 +131,12 @@
 
     @Override
     public void linkDiscoveryUpdate(LDUpdate update) {
+        if (!registryService.hasControl(update.getDst())) {
+            // Don't process or send a link event if we're not master for the
+            // destination switch
+            return;
+        }
+
         LinkEvent linkEvent = new LinkEvent(update.getSrc(),
                 (long) update.getSrcPort(), update.getDst(),
                 (long) update.getDstPort());