RoleChanger { @LogMessageDoc(level="WARN", message="Failed to send role request message " + "to switch {switch}: {message}. Disconnecting", explanation="An I/O error occurred while attempting to change " + "the switch HA role.", recommendation=LogMessageDoc.CHECK_SWITCH) protected void sendRoleRequest(Collection<OFSwitchImpl> switches, Role role, long cookie) { Iterator<OFSwitchImpl> iter = switches.iterator(); while(iter.hasNext()) { OFSwitchImpl sw = iter.next(); try { Boolean supportsNxRole = (Boolean) sw.getAttribute(IOFSwitch.SWITCH_SUPPORTS_NX_ROLE); if ((supportsNxRole == null) || supportsNxRole) { sw.sendNxRoleRequest(role, cookie); } else { if (role == Role.SLAVE) { log.debug("Disconnecting switch {} that doesn't support " + "role request messages from a controller that went to SLAVE mode"); sw.getChannel().close(); iter.remove(); } } } catch (IOException e) { log.warn("Failed to send role request message " + "to switch {}: {}. Disconnecting", sw, e); sw.getChannel().close(); iter.remove(); } } } RoleChanger(); synchronized void submitRequest(Collection<OFSwitchImpl> switches, Role role);  }
@Test public void testSendRoleRequestSlaveNotSupported() { LinkedList<OFSwitchImpl> switches = new LinkedList<OFSwitchImpl>(); OFSwitchImpl sw1 = EasyMock.createMock(OFSwitchImpl.class); Channel channel1 = createMock(Channel.class); expect(sw1.getChannel()).andReturn(channel1); expect(sw1.getAttribute(IOFSwitch.SWITCH_SUPPORTS_NX_ROLE)) .andReturn(false); expect(channel1.close()).andReturn(null); switches.add(sw1); replay(sw1, channel1); roleChanger.sendRoleRequest(switches, Role.SLAVE, 123456); verify(sw1, channel1); assertTrue(switches.isEmpty()); }
@Test public void testSendRoleRequestMasterNotSupported() { LinkedList<OFSwitchImpl> switches = new LinkedList<OFSwitchImpl>(); OFSwitchImpl sw1 = EasyMock.createMock(OFSwitchImpl.class); expect(sw1.getAttribute(IOFSwitch.SWITCH_SUPPORTS_NX_ROLE)) .andReturn(false); switches.add(sw1); replay(sw1); roleChanger.sendRoleRequest(switches, Role.MASTER, 123456); verify(sw1); assertEquals(1, switches.size()); }
@Test public void testSendRoleRequestErrorHandling () throws Exception { LinkedList<OFSwitchImpl> switches = new LinkedList<OFSwitchImpl>(); OFSwitchImpl sw1 = EasyMock.createMock(OFSwitchImpl.class); expect(sw1.getAttribute(IOFSwitch.SWITCH_SUPPORTS_NX_ROLE)) .andReturn(true); expect(sw1.sendNxRoleRequest(Role.MASTER, 123456)) .andThrow(new IOException()).once(); Channel channel1 = createMock(Channel.class); expect(sw1.getChannel()).andReturn(channel1); expect(channel1.close()).andReturn(null); switches.add(sw1); replay(sw1); roleChanger.sendRoleRequest(switches, Role.MASTER, 123456); verify(sw1); assertTrue(switches.isEmpty()); }
@Test public void testSendRoleRequestSupported() throws Exception { LinkedList<OFSwitchImpl> switches = new LinkedList<OFSwitchImpl>(); OFSwitchImpl sw1 = EasyMock.createMock(OFSwitchImpl.class); expect(sw1.getAttribute(IOFSwitch.SWITCH_SUPPORTS_NX_ROLE)) .andReturn(true); expect(sw1.sendNxRoleRequest(Role.MASTER, 123456)).andReturn(1).once(); switches.add(sw1); OFSwitchImpl sw2 = EasyMock.createMock(OFSwitchImpl.class); expect(sw2.getAttribute(IOFSwitch.SWITCH_SUPPORTS_NX_ROLE)) .andReturn(null); expect(sw2.sendNxRoleRequest(Role.MASTER, 123456)).andReturn(1).once(); switches.add(sw2); replay(sw1, sw2); roleChanger.sendRoleRequest(switches, Role.MASTER, 123456); verify(sw1, sw2); assertEquals(2, switches.size()); }