controller.addHAListener(haListener);
verify(haListener);
reset(haListener);
// Add switch listener
IOFSwitchListener switchListener = createMock(IOFSwitchListener.class);
controller.addOFSwitchListener(switchListener);
// Add readyForReconcile listener
IReadyForReconcileListener readyForReconcileListener =
createMock(IReadyForReconcileListener.class);
controller.addReadyForReconcileListener(readyForReconcileListener);
//---------------------------------------
// Initialization
//---------------------------------------
// Switch 1
// no actual IOFSwitch here because we simply add features reply
// and desc stats to store
OFFeaturesReply fr1a = createOFFeaturesReply();
fr1a.setDatapathId(1L);
OFPhysicalPort p = createOFPhysicalPort("P1", 1);
ImmutablePort sw1p1 = ImmutablePort.fromOFPhysicalPort(p);
List<OFPhysicalPort> ports1a = Collections.singletonList(p);
fr1a.setPorts(ports1a);
List<ImmutablePort> ports1aImmutable =
ImmutablePort.immutablePortListOf(ports1a);
// an alternative featuers reply
OFFeaturesReply fr1b = createOFFeaturesReply();
fr1b.setDatapathId(1L);
p = new OFPhysicalPort();
p = createOFPhysicalPort("P1", 1); // same port as above
List<OFPhysicalPort> ports1b = new ArrayList<OFPhysicalPort>();
ports1b.add(p);
p = createOFPhysicalPort("P2", 42000);
ImmutablePort sw1p2 = ImmutablePort.fromOFPhysicalPort(p);
ports1b.add(p);
fr1b.setPorts(ports1b);
List<ImmutablePort> ports1bImmutable =
ImmutablePort.immutablePortListOf(ports1b);
// Switch 2
// no actual IOFSwitch here because we simply add features reply
// and desc stats to store
OFFeaturesReply fr2a = createOFFeaturesReply();
fr2a.setDatapathId(2L);
ImmutablePort sw2p1 = sw1p1;
List<OFPhysicalPort> ports2a = new ArrayList<OFPhysicalPort>(ports1a);
fr2a.setPorts(ports2a);
List<ImmutablePort> ports2aImmutable =
ImmutablePort.immutablePortListOf(ports2a);
// an alternative features reply
OFFeaturesReply fr2b = createOFFeaturesReply();
fr2b.setDatapathId(2L);
p = new OFPhysicalPort();
p = createOFPhysicalPort("P1", 2); // port number changed
ImmutablePort sw2p1Changed = ImmutablePort.fromOFPhysicalPort(p);
List<OFPhysicalPort> ports2b = Collections.singletonList(p);
fr2b.setPorts(ports2b);
// Switches 3 and 4 are create with default features reply and desc
// so nothing to do here
//---------------------------------------
// Adding switches to store
//---------------------------------------
replay(haListener); // nothing should happen to haListener
replay(readyForReconcileListener); // nothing should happen to
// readyForReconcileListener
// add switch1 with fr1a to store
reset(switchListener);
switchListener.switchAdded(1L);
expectLastCall().once();
replay(switchListener);
doAddSwitchToStore(1L, null, fr1a);
controller.processUpdateQueueForTesting();
verify(switchListener);
reset(switchListener);
IOFSwitch sw = controller.getSwitch(1L);
assertNotNull("Switch should be present", sw);
assertEquals(1L, sw.getId());
assertFalse("Switch should be inactive", sw.isActive());
assertEquals(new HashSet<ImmutablePort>(ports1aImmutable),
new HashSet<ImmutablePort>(sw.getPorts()));
// add switch 2 with fr2a to store
reset(switchListener);
switchListener.switchAdded(2L);
expectLastCall().once();
replay(switchListener);
doAddSwitchToStore(2L, null, fr2a);
controller.processUpdateQueueForTesting();
verify(switchListener);
reset(switchListener);
sw = controller.getSwitch(2L);
assertNotNull("Switch should be present", sw);
assertEquals(2L, sw.getId());
assertFalse("Switch should be inactive", sw.isActive());
assertEquals(new HashSet<ImmutablePort>(ports2aImmutable),
new HashSet<ImmutablePort>(sw.getPorts()));
// add switch 3 to store
reset(switchListener);
switchListener.switchAdded(3L);
expectLastCall().once();
replay(switchListener);
doAddSwitchToStore(3L, null, null);
controller.processUpdateQueueForTesting();
verify(switchListener);
reset(switchListener);
sw = controller.getSwitch(3L);
assertNotNull("Switch should be present", sw);
assertEquals(3L, sw.getId());
assertFalse("Switch should be inactive", sw.isActive());
// add switch 4 to store
reset(switchListener);
switchListener.switchAdded(4L);
expectLastCall().once();
replay(switchListener);
doAddSwitchToStore(4L, null, null);
controller.processUpdateQueueForTesting();
verify(switchListener);
reset(switchListener);
sw = controller.getSwitch(4L);
assertNotNull("Switch should be present", sw);
assertEquals(4L, sw.getId());
assertFalse("Switch should be inactive", sw.isActive());
// update switch 1 with fr1b
reset(switchListener);
switchListener.switchPortChanged(1L, sw1p2, PortChangeType.ADD);
expectLastCall().once();
replay(switchListener);
doAddSwitchToStore(1L, null, fr1b);
controller.processUpdateQueueForTesting();
verify(switchListener);
reset(switchListener);
sw = controller.getSwitch(1L);
assertNotNull("Switch should be present", sw);
assertEquals(1L, sw.getId());
assertFalse("Switch should be inactive", sw.isActive());
assertEquals(new HashSet<ImmutablePort>(ports1bImmutable),
new HashSet<ImmutablePort>(sw.getPorts()));
// Check getAllSwitchDpids() and getAllSwitchMap()
Set<Long> expectedDpids = new HashSet<Long>();
expectedDpids.add(1L);
expectedDpids.add(2L);
expectedDpids.add(3L);
expectedDpids.add(4L);
assertEquals(expectedDpids, controller.getAllSwitchDpids());
Map<Long, IOFSwitch> expectedSwitchMap = new HashMap<Long, IOFSwitch>();
expectedSwitchMap.put(1L, controller.getSwitch(1L));
expectedSwitchMap.put(2L, controller.getSwitch(2L));
expectedSwitchMap.put(3L, controller.getSwitch(3L));
expectedSwitchMap.put(4L, controller.getSwitch(4L));
assertEquals(expectedSwitchMap, controller.getAllSwitchMap());
verify(haListener);
//--------------------------------------
// Transition to master
//--------------------------------------
reset(haListener);
haListener.transitionToMaster();
expectLastCall().once();
replay(haListener);
controller.setConsolidateStoreTaskDelay(consolidateStoreDelayMs);
controller.setRole(Role.MASTER, "FooBar");
controller.processUpdateQueueForTesting();
verify(haListener);
reset(haListener);
replay(haListener);
//--------------------------------------
// Activate switches
//--------------------------------------
// Activate switch 1
IOFSwitch sw1 = createMock(IOFSwitch.class);
setupSwitchForAddSwitch(sw1, 1L, null, fr1b);
reset(switchListener);
switchListener.switchActivated(1L);
expectLastCall().once();
replay(sw1);
replay(switchListener);
controller.switchActivated(sw1);
controller.processUpdateQueueForTesting();
verify(switchListener);
verify(sw1);
sw = controller.getSwitch(1L);
assertNotNull("Switch should be present", sw);
assertSame(sw1, sw); // the mock switch should be returned
// Activate switch 2 with different features reply
// should get portChanged
// also set alwaysClearFlorModOnSwAcitvate to true;
controller.setAlwaysClearFlowsOnSwActivate(true);
IOFSwitch sw2 = createMock(IOFSwitch.class);
setupSwitchForAddSwitch(sw2, 2L, null, fr2b);
sw2.clearAllFlowMods();
expectLastCall().once();
reset(switchListener);
switchListener.switchActivated(2L);
expectLastCall().once();
switchListener.switchPortChanged(2L, sw2p1, PortChangeType.DELETE);
switchListener.switchPortChanged(2L, sw2p1Changed, PortChangeType.ADD);
expectLastCall().once();
replay(sw2);
replay(switchListener);
controller.switchActivated(sw2);
controller.processUpdateQueueForTesting();
verify(switchListener);
verify(sw2);
sw = controller.getSwitch(2L);
assertNotNull("Switch should be present", sw);
assertSame(sw2, sw); // the mock switch should be returned
// Do not activate switch 3, but it should still be present
sw = controller.getSwitch(3L);
IOFSwitch sw3 = sw;
assertNotNull("Switch should be present", sw);
assertEquals(3L, sw.getId());
assertFalse("Switch should be inactive", sw.isActive());
// Do not activate switch 4, but it should still be present
sw = controller.getSwitch(4L);
IOFSwitch sw4 = sw;
assertNotNull("Switch should be present", sw);
assertEquals(4L, sw.getId());
assertFalse("Switch should be inactive", sw.isActive());
// Check getAllSwitchDpids() and getAllSwitchMap()
expectedDpids = new HashSet<Long>();
expectedDpids.add(1L);
expectedDpids.add(2L);
expectedDpids.add(3L);
expectedDpids.add(4L);
assertEquals(expectedDpids, controller.getAllSwitchDpids());
expectedSwitchMap = new HashMap<Long, IOFSwitch>();
expectedSwitchMap.put(1L, sw1);
expectedSwitchMap.put(2L, sw2);
expectedSwitchMap.put(3L, sw3);
expectedSwitchMap.put(4L, sw4);
assertEquals(expectedSwitchMap, controller.getAllSwitchMap());
// silently remove switch 4 from the store and notify the
// store listener. Since the controller is MASTER it will ignore
// this notification.
reset(switchListener);
replay(switchListener);
doRemoveSwitchFromStore(4L);
controller.processUpdateQueueForTesting();
verify(switchListener);
// Switch should still be queryable
sw = controller.getSwitch(4L);
assertNotNull("Switch should be present", sw);
assertEquals(4L, sw.getId());
assertFalse("Switch should be inactive", sw.isActive());
//--------------------------------
// Wait for consolidateStore
//--------------------------------
verify(readyForReconcileListener);
reset(readyForReconcileListener);
readyForReconcileListener.readyForReconcile();
replay(readyForReconcileListener);
reset(switchListener);
switchListener.switchRemoved(3L);
switchListener.switchRemoved(4L);
replay(switchListener);
Thread.sleep(2*consolidateStoreDelayMs);
controller.processUpdateQueueForTesting();
verify(switchListener);
verify(readyForReconcileListener);