Fix a critical bug when removing entries from the Topology.

Also, fix method toString() for PortEvent and SwitchEvent so it
prints properly the switch DPID.

Change-Id: Ic9a7fe9422ab98b0d108cb5fd14bfbe4b08bc4ca
diff --git a/src/main/java/net/onrc/onos/core/topology/PortEvent.java b/src/main/java/net/onrc/onos/core/topology/PortEvent.java
index f23f907..9907a19 100644
--- a/src/main/java/net/onrc/onos/core/topology/PortEvent.java
+++ b/src/main/java/net/onrc/onos/core/topology/PortEvent.java
@@ -134,7 +134,7 @@
 
     @Override
     public String toString() {
-        return "[PortEvent 0x" + getDpid() + "@" + getPortNumber() + "]";
+        return "[PortEvent " + getDpid() + "@" + getPortNumber() + "]";
     }
 
     public static final int PORTID_BYTES = SwitchEvent.SWITCHID_BYTES + 2 + 8;
diff --git a/src/main/java/net/onrc/onos/core/topology/SwitchEvent.java b/src/main/java/net/onrc/onos/core/topology/SwitchEvent.java
index a3b6a93..47f8e81 100644
--- a/src/main/java/net/onrc/onos/core/topology/SwitchEvent.java
+++ b/src/main/java/net/onrc/onos/core/topology/SwitchEvent.java
@@ -95,7 +95,7 @@
 
     @Override
     public String toString() {
-        return "[SwitchEvent 0x" + Long.toHexString(dpid.value()) + "]";
+        return "[SwitchEvent " + dpid + "]";
     }
 
     public static final int SWITCHID_BYTES = 2 + 8;
diff --git a/src/main/java/net/onrc/onos/core/topology/TopologyManager.java b/src/main/java/net/onrc/onos/core/topology/TopologyManager.java
index 28c23da..1e3dccd 100644
--- a/src/main/java/net/onrc/onos/core/topology/TopologyManager.java
+++ b/src/main/java/net/onrc/onos/core/topology/TopologyManager.java
@@ -513,7 +513,10 @@
     @Override
     public void removeSwitchMastershipEvent(MastershipEvent mastershipEvent) {
         // Send out notification
-        eventChannel.removeEntry(mastershipEvent.getID());
+        TopologyEvent topologyEvent =
+            new TopologyEvent(mastershipEvent,
+                              registryService.getOnosInstanceId());
+        eventChannel.removeEntry(topologyEvent.getID());
     }
 
     /**
@@ -587,6 +590,8 @@
      */
     @Override
     public void removeSwitchDiscoveryEvent(SwitchEvent switchEvent) {
+        TopologyEvent topologyEvent;
+
         // Get the old Port Events
         Map<ByteBuffer, PortEvent> oldPortEvents =
                 discoveredAddedPortEvents.get(switchEvent.getDpid());
@@ -597,7 +602,10 @@
         if (datastore.deactivateSwitch(switchEvent, oldPortEvents.values())) {
             log.debug("Sending remove switch: {}", switchEvent);
             // Send out notification
-            eventChannel.removeEntry(switchEvent.getID());
+            topologyEvent =
+                new TopologyEvent(switchEvent,
+                                  registryService.getOnosInstanceId());
+            eventChannel.removeEntry(topologyEvent.getID());
 
             //
             // Send out notification for each port.
@@ -608,7 +616,10 @@
             //
             for (PortEvent portEvent : oldPortEvents.values()) {
                 log.debug("Sending remove port:", portEvent);
-                eventChannel.removeEntry(portEvent.getID());
+                topologyEvent =
+                    new TopologyEvent(portEvent,
+                                      registryService.getOnosInstanceId());
+                eventChannel.removeEntry(topologyEvent.getID());
             }
             discoveredAddedPortEvents.remove(switchEvent.getDpid());
 
@@ -672,7 +683,10 @@
         if (datastore.deactivatePort(portEvent)) {
             log.debug("Sending remove port: {}", portEvent);
             // Send out notification
-            eventChannel.removeEntry(portEvent.getID());
+            TopologyEvent topologyEvent =
+                new TopologyEvent(portEvent,
+                                  registryService.getOnosInstanceId());
+            eventChannel.removeEntry(topologyEvent.getID());
 
             // Cleanup the Port Event from the local cache
             Map<ByteBuffer, PortEvent> oldPortEvents =
@@ -754,7 +768,10 @@
         if (datastore.removeLink(linkEvent)) {
             log.debug("Sending remove link: {}", linkEvent);
             // Send out notification
-            eventChannel.removeEntry(linkEvent.getID());
+            TopologyEvent topologyEvent =
+                new TopologyEvent(linkEvent,
+                                  registryService.getOnosInstanceId());
+            eventChannel.removeEntry(topologyEvent.getID());
 
             // Cleanup the Link Event from the local cache
             Map<ByteBuffer, LinkEvent> oldLinkEvents =
@@ -806,7 +823,10 @@
     public void removeHostDiscoveryEvent(HostEvent hostEvent) {
         if (datastore.removeHost(hostEvent)) {
             // Send out notification
-            eventChannel.removeEntry(hostEvent.getID());
+            TopologyEvent topologyEvent =
+                new TopologyEvent(hostEvent,
+                                  registryService.getOnosInstanceId());
+            eventChannel.removeEntry(topologyEvent.getID());
             log.debug("Remove the host info into the cache of the topology. mac {}", hostEvent.getMac());
 
             // Cleanup the Host Event from the local cache