ONOS-1871: Send Topology MastershipEvent info to Topology listeners.
The MastershipEvent info is needed by applications such as the GUI
(via the Websocket client).
NOTE: Previously, the Mastership Events were sent only when there
was any change, but no Mastership Events were sent when a listener
has just subscribed.
The solution is to add a mechanism for creating and sending Topology
Snapshot event to new listeners, and that event also includes
the Mastership Events.
The modifications are:
* Renamed and updated the ITopologyService API for adding/removing
Topology listeners:
OLD: registerTopologyListener() and deregisterTopologyListener()
NEW: addListener() and removeListener()
Also, addListener() has a second argument:
"boolean startFromSnapshot"
If that argument is true, and if the topology is not empty, the first
(expected) event to that listener should be a snapshot of the current
topology.
* Added TopologyEvents() constructor for ADDED events only. Such event
can be used to represent a snapshot of the topology.
* Added new APIs to TopologyInternal:
getAllSwitchEvents(), getAllPortEvents(), get AllLinkEvents(),
getAllHostEvents()
Those APIs are needed for creating a snapshot of the topology.
* Added a mechanism for creating empty (NO-OP) TopologyEvent instance,
and use that mechanism to "wake-up" the EventHandler processing thread
when it needs to send Topology Snapshot to a new listener.
This solution is (kind-of) a hack.
Change-Id: Ie1eb52242f58682aac61f54af29c3b5d291ac0bd
diff --git a/src/main/java/net/onrc/onos/apps/websocket/TopologyWebSocket.java b/src/main/java/net/onrc/onos/apps/websocket/TopologyWebSocket.java
index 1a83b94..9d9ed6f 100644
--- a/src/main/java/net/onrc/onos/apps/websocket/TopologyWebSocket.java
+++ b/src/main/java/net/onrc/onos/apps/websocket/TopologyWebSocket.java
@@ -2,7 +2,6 @@
import net.onrc.onos.core.topology.ITopologyListener;
import net.onrc.onos.core.topology.ITopologyService;
-import net.onrc.onos.core.topology.Topology;
import net.onrc.onos.core.topology.TopologyEvents;
import java.io.IOException;
@@ -65,7 +64,7 @@
*/
private void shutdown() {
ITopologyService topologyService = WebSocketManager.topologyService;
- topologyService.deregisterTopologyListener(this);
+ topologyService.removeListener(this);
this.isOpen = false; // Stop the thread
}
@@ -124,34 +123,8 @@
// Initialization and Topology Service registration
//
this.socketSession = session;
- ObjectMapper mapper = new ObjectMapper();
- String topologyJson = null;
ITopologyService topologyService = WebSocketManager.topologyService;
- topologyService.registerTopologyListener(this);
-
- //
- // Get the initial topology and encode it in JSON
- //
- Topology topology = topologyService.getTopology();
- topology.acquireReadLock();
- try {
- topologyJson = mapper.writeValueAsString(topology);
- } catch (IOException e) {
- log.debug("Exception encoding topology as JSON: ", e);
- } finally {
- topology.releaseReadLock();
- }
-
- //
- // Send the initial topology
- //
- if (topologyJson != null) {
- try {
- session.getBasicRemote().sendText(topologyJson);
- } catch (IOException e) {
- log.debug("Exception sending TopologyWebSocket topology: ", e);
- }
- }
+ topologyService.addListener(this, true);
// Start the thread
start();