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/test/java/net/onrc/onos/core/hostmanager/HostManagerTest.java b/src/test/java/net/onrc/onos/core/hostmanager/HostManagerTest.java
index 7e552ed..3c4bfee 100644
--- a/src/test/java/net/onrc/onos/core/hostmanager/HostManagerTest.java
+++ b/src/test/java/net/onrc/onos/core/hostmanager/HostManagerTest.java
@@ -86,7 +86,7 @@
controllerRegistryService = createMock(IControllerRegistryService.class);
eventChannel = createMock(IEventChannel.class);
expect(networkGraphService.getTopology()).andReturn(topology).anyTimes();
- networkGraphService.registerTopologyListener(anyObject(ITopologyListener.class));
+ networkGraphService.addListener(anyObject(ITopologyListener.class), eq(false));
expectLastCall();
expect(datagridService.createChannel("onos.host", Long.class, Host.class))
diff --git a/src/test/java/net/onrc/onos/core/intent/runtime/IntentTestMocks.java b/src/test/java/net/onrc/onos/core/intent/runtime/IntentTestMocks.java
index a6dde8b..486604c 100644
--- a/src/test/java/net/onrc/onos/core/intent/runtime/IntentTestMocks.java
+++ b/src/test/java/net/onrc/onos/core/intent/runtime/IntentTestMocks.java
@@ -88,8 +88,8 @@
expect(topologyService.getTopology()).andReturn(topology)
.anyTimes();
- topologyService.registerTopologyListener(
- anyObject(ITopologyListener.class));
+ topologyService.addListener(
+ anyObject(ITopologyListener.class), eq(false));
expectLastCall();
expect(datagridService.createChannel("onos.pathintent",
diff --git a/src/test/java/net/onrc/onos/core/intent/runtime/UseCaseTest.java b/src/test/java/net/onrc/onos/core/intent/runtime/UseCaseTest.java
index 1d85984..69f8e01 100644
--- a/src/test/java/net/onrc/onos/core/intent/runtime/UseCaseTest.java
+++ b/src/test/java/net/onrc/onos/core/intent/runtime/UseCaseTest.java
@@ -106,7 +106,8 @@
.andReturn(restApi).once();
expect(topologyService.getTopology()).andReturn(mockTopology).anyTimes();
- topologyService.registerTopologyListener(anyObject(ITopologyListener.class));
+ topologyService.addListener(anyObject(ITopologyListener.class),
+ eq(false));
expectLastCall();
expect(datagridService.createChannel("onos.pathintent", Long.class, IntentOperationList.class))
diff --git a/src/test/java/net/onrc/onos/core/topology/TopologyManagerTest.java b/src/test/java/net/onrc/onos/core/topology/TopologyManagerTest.java
index 231ba6f..a84b00f 100644
--- a/src/test/java/net/onrc/onos/core/topology/TopologyManagerTest.java
+++ b/src/test/java/net/onrc/onos/core/topology/TopologyManagerTest.java
@@ -145,14 +145,10 @@
// Setup the Registry Service
expect(registryService.getOnosInstanceId()).andReturn(ONOS_INSTANCE_ID_1).anyTimes();
- try {
- expect(registryService.getControllerForSwitch(DPID_1.value())).
- andReturn(ONOS_INSTANCE_ID_1.toString()).anyTimes();
- expect(registryService.getControllerForSwitch(DPID_2.value())).
- andReturn(ONOS_INSTANCE_ID_2.toString()).anyTimes();
- } catch (RegistryException ex) {
- throw new IllegalStateException(ex);
- }
+ expect(registryService.getControllerForSwitch(DPID_1.value()))
+ .andReturn(ONOS_INSTANCE_ID_1.toString()).anyTimes();
+ expect(registryService.getControllerForSwitch(DPID_2.value()))
+ .andReturn(ONOS_INSTANCE_ID_2.toString()).anyTimes();
allTopologyEvents = new CopyOnWriteArrayList<>();
expect(eventChannel.getAllEntries())
@@ -186,7 +182,7 @@
private void setupTopologyManagerWithEventHandler() {
// Create a TopologyManager object for testing
theTopologyManager = new TopologyManager(registryService);
- theTopologyManager.registerTopologyListener(theTopologyListener);
+ theTopologyManager.addListener(theTopologyListener, true);
// Allocate the Event Handler, so we can have direct access to it
theEventHandler = theTopologyManager.new EventHandler();
@@ -1432,9 +1428,12 @@
* instances are processed - both events should be delivered.
* - Finally, a REMOVE Switch Event is received from the first ONOS
* instance - no event should be delivered.
+ *
+ * @throws RegistryException
*/
@Test
- public void testProcessSwitchMastershipSwitchover() {
+ public void testProcessSwitchMastershipSwitchover()
+ throws RegistryException {
TopologyEvents topologyEvents;
List<EventEntry<TopologyEvent>> events = new LinkedList<>();
EventEntry<TopologyEvent> eventEntry;
@@ -1484,12 +1483,8 @@
// Master.
//
reset(registryService);
- try {
- expect(registryService.getControllerForSwitch(DPID_1.value())).
- andReturn(ONOS_INSTANCE_ID_2.toString()).anyTimes();
- } catch (RegistryException ex) {
- throw new IllegalStateException(ex);
- }
+ expect(registryService.getControllerForSwitch(DPID_1.value()))
+ .andReturn(ONOS_INSTANCE_ID_2.toString()).anyTimes();
replay(registryService);
// Prepare the Mastership Event from the second ONOS instance