@Override @Test public void moveToWaitInitialRole() throws Exception { moveToWaitAppHandshakeState(); assertThat(switchHandler.getStateForTesting(), CoreMatchers.instanceOf(WaitAppHandshakeState.class)); reset(sw); expect(sw.getAttribute(IOFSwitchBackend.SWITCH_SUPPORTS_NX_ROLE)).andReturn(true).anyTimes(); replay(sw); reset(roleManager); expect(roleManager.getOFControllerRole()).andReturn(OFControllerRole.ROLE_MASTER).anyTimes(); roleManager.notifyControllerConnectionUpdate(); expectLastCall().once(); replay(roleManager); WaitAppHandshakeState state = (WaitAppHandshakeState) switchHandler.getStateForTesting(); state.enterNextPlugin(); // Expect wait initial role's enterState message to be written OFMessage msg = connection.retrieveMessage(); assertThat(msg, CoreMatchers.instanceOf(OFRoleRequest.class)); verifyUniqueXids(msg); assertThat(switchHandler.getStateForTesting(), CoreMatchers.instanceOf(OFSwitchHandshakeHandler.WaitInitialRoleState.class)); }
void processWrittenOFMessage(OFMessage m) { switch(m.getType()) { case ROLE_REQUEST: processOFRoleRequest((OFRoleRequest) m); break; case EXPERIMENTER: if (m instanceof OFNiciraControllerRoleRequest) { processOFNiciraControllerRoleRequest((OFNiciraControllerRoleRequest) m); } break; default: /* * no-op: * we can send any request at any time */ break; } }
@Override @Test public void moveToWaitInitialRole() throws Exception { moveToWaitAppHandshakeState(); assertThat(switchHandler.getStateForTesting(), CoreMatchers.instanceOf(WaitAppHandshakeState.class)); reset(sw); expect(sw.getAttribute(IOFSwitchBackend.SWITCH_SUPPORTS_NX_ROLE)).andReturn(true).anyTimes(); replay(sw); reset(roleManager); expect(roleManager.getOFControllerRole(null)).andReturn(OFControllerRole.ROLE_MASTER).anyTimes(); roleManager.notifyControllerConnectionUpdate(); expectLastCall().once(); replay(roleManager); WaitAppHandshakeState state = (WaitAppHandshakeState) switchHandler.getStateForTesting(); state.enterNextPlugin(); // Expect wait initial role's enterState message to be written OFMessage msg = connection.retrieveMessage(); assertThat(msg, CoreMatchers.instanceOf(OFRoleRequest.class)); verifyUniqueXids(msg); assertThat(switchHandler.getStateForTesting(), CoreMatchers.instanceOf(OFSwitchHandshakeHandler.WaitInitialRoleState.class)); }
@Override public void processRoleRequest(Channel channel, OFMessage msg) { OFRoleRequest ofRoleRequest = (OFRoleRequest) msg; OFControllerRole oldRole = role(channel); OFControllerRole newRole = ofRoleRequest.getRole(); if (oldRole.equals(newRole)) { log.trace("No change needed to existing role {}", oldRole); } else { log.trace("Changing role from {} to {}", oldRole, newRole); setRole(channel, newRole); } OFRoleReply ofRoleReply = FACTORY.buildRoleReply() .setRole(role(channel)) .setXid(msg.getXid()) .build(); channel.writeAndFlush(Collections.singletonList(ofRoleReply)); log.trace("request {}; reply {}", msg, ofRoleReply); }
@Override public final void sendRoleRequest(OFMessage msg) { if (msg instanceof OFRoleRequest || msg instanceof OFNiciraControllerRoleRequest) { sendMsgsOnChannel(Collections.singletonList(msg)); return; } throw new IllegalArgumentException("Someone is trying to send " + "a non role request message"); }
private int sendOF13RoleRequest(RoleState role) throws IOException { // Convert the role enum to the appropriate role to send OFControllerRole roleToSend = OFControllerRole.ROLE_NOCHANGE; switch (role) { case EQUAL: roleToSend = OFControllerRole.ROLE_EQUAL; break; case MASTER: roleToSend = OFControllerRole.ROLE_MASTER; break; case SLAVE: roleToSend = OFControllerRole.ROLE_SLAVE; break; default: log.warn("Sending default role.noChange to switch {}." + " Should only be used for queries.", sw); } int xid = sw.getNextTransactionId(); OFRoleRequest rrm = OFFactories.getFactory(OFVersion.OF_13) .buildRoleRequest() .setRole(roleToSend) .setXid(xid) //FIXME fix below when we actually use generation ids .setGenerationId(U64.ZERO) .build(); sw.sendRoleRequest(rrm); return xid; }
void processWrittenOFMessage(OFMessage m) { switch(m.getType()) { case ROLE_REQUEST: processOFRoleRequest((OFRoleRequest) m); break; case EXPERIMENTER: if (m instanceof OFNiciraControllerRoleRequest) { processOFNiciraControllerRoleRequest((OFNiciraControllerRoleRequest) m); } break; default: break; } }
void sendRoleRequestIfNotPending(OFRoleRequest role) { try { roleChanger.sendRoleRequestIfNotPending(role.getRole(), 0); } catch (IOException e) { log.error("Disconnecting switch {} due to IO Error: {}", getSwitchInfoString(), e.getMessage()); mainConnection.disconnect(); } }
void sendRoleRequest(OFRoleRequest role) { try { roleChanger.sendRoleRequest(role.getRole(), role.getXid()); } catch (IOException e) { log.error("Disconnecting switch {} due to IO Error: {}", getSwitchInfoString(), e.getMessage()); mainConnection.disconnect(); } }
/** write a request which triggers an OFErrorMsg response */ @Test(timeout = 5000) public void testWriteRequestOFErrorMsg() throws InterruptedException, ExecutionException { Capture<List<OFMessage>> cMsgList = prepareChannelForWriteList(); OFRoleRequest roleRequest = factory.buildRoleRequest().setRole(OFControllerRole.ROLE_MASTER).build(); ListenableFuture<OFRoleReply> future = conn.writeRequest(roleRequest); assertThat("Connection should have 1 pending request", conn.getPendingRequestIds().size(), equalTo(1)); eventLoop.runTasks(); assertThat("Should have captured MsgList", cMsgList.getValue(), Matchers.<OFMessage> contains(roleRequest)); assertThat("Future should not be complete yet", future.isDone(), equalTo(false)); OFRoleRequestFailedErrorMsg roleError = factory.errorMsgs().buildRoleRequestFailedErrorMsg() .setXid(roleRequest.getXid()) .setCode(OFRoleRequestFailedCode.STALE) .build(); assertThat("Connection should have accepted the response", conn.deliverResponse(roleError), equalTo(true)); OFErrorMsgException e = FutureTestUtils.assertFutureFailedWithException(future, OFErrorMsgException.class); assertThat(e.getErrorMessage(), CoreMatchers.<OFErrorMsg>equalTo(roleError)); }
void processWrittenOFMessage(OFMessage m) { switch(m.getType()) { case ROLE_REQUEST: processOFRoleRequest((OFRoleRequest) m); break; case EXPERIMENTER: if (m instanceof OFNiciraControllerRoleRequest) { processOFNiciraControllerRoleRequest((OFNiciraControllerRoleRequest) m); } break; default: illegalMessageReceived(m); break; } }
/** write a request which triggers an OFErrorMsg response */ @Test(timeout = 5000) public void testWriteRequestOFErrorMsg() throws InterruptedException, ExecutionException { Capture<List<OFMessage>> cMsgList = prepareChannelForWriteList(); OFRoleRequest roleRequest = factory.buildRoleRequest().setRole(OFControllerRole.ROLE_MASTER).build(); ListenableFuture<OFRoleReply> future = conn.writeRequest(roleRequest); assertThat("Connection should have 1 pending request", conn.getPendingRequestIds().size(), equalTo(1)); assertThat("Should have captured MsgList", cMsgList.getValue(), Matchers.<OFMessage> contains(roleRequest)); assertThat("Future should not be complete yet", future.isDone(), equalTo(false)); OFRoleRequestFailedErrorMsg roleError = factory.errorMsgs().buildRoleRequestFailedErrorMsg() .setXid(roleRequest.getXid()) .setCode(OFRoleRequestFailedCode.STALE) .build(); assertThat("Connection should have accepted the response", conn.deliverResponse(roleError), equalTo(true)); OFErrorMsgException e = FutureTestUtils.assertFutureFailedWithException(future, OFErrorMsgException.class); assertThat(e.getErrorMessage(), CoreMatchers.<OFErrorMsg>equalTo(roleError)); }