@Test public void deliverRoleReply() { RoleRecvStatus status; RoleReplyInfo asserted = new RoleReplyInfo(MASTER, GID, XID); RoleReplyInfo unasserted = new RoleReplyInfo(SLAVE, GID, XID); try { status = manager.deliverRoleReply(asserted); assertEquals("expectation wrong", OTHER_EXPECTATION, status); sw.setRole(MASTER); assertEquals("expectation wrong", OTHER_EXPECTATION, status); sw.setRole(SLAVE); manager.sendRoleRequest(MASTER, MATCHED_CURRENT_ROLE); status = manager.deliverRoleReply(asserted); assertEquals("expectation wrong", MATCHED_CURRENT_ROLE, status); status = manager.deliverRoleReply(unasserted); assertEquals("expectation wrong", OTHER_EXPECTATION, status); assertEquals("pending role mismatch", MASTER, ((TestSwitchDriver) sw).failed); } catch (IOException | SwitchStateException e) { assertEquals("unexpected error thrown", SwitchStateException.class, e.getClass()); } }
@Override public synchronized RoleRecvStatus deliverRoleReply(RoleReplyInfo rri) throws SwitchStateException { int xid = (int) rri.getXid(); RoleState receivedRole = rri.getRole(); RoleState expectedRole = pendingReplies.getIfPresent(xid); if (expectedRole == null) { RoleState currentRole = (sw != null) ? sw.getRole() : null; if (currentRole != null) { if (currentRole == rri.getRole()) { log.debug("Received unexpected RoleReply from " + "Switch: {}. " + "Role in reply is same as current role of this " + "controller for this sw. Ignoring ...", sw.getStringId()); return RoleRecvStatus.OTHER_EXPECTATION; } else { String msg = String.format("Switch: [%s], " + "received unexpected RoleReply[%s]. " + "No roles are pending, and this controller's " + "current role:[%s] does not match reply. " + "Disconnecting switch ... ", sw.getStringId(), rri, currentRole); throw new SwitchStateException(msg); } } log.debug("Received unexpected RoleReply {} from " + "Switch: {}. " + "This controller has no current role for this sw. " + "Ignoring ...", rri, sw == null ? "(null)" : sw.getStringId()); return RoleRecvStatus.OTHER_EXPECTATION; } sw.returnRoleReply(expectedRole, receivedRole); if (expectedRole == receivedRole) { log.debug("Received role reply message from {} that matched " + "expected role-reply {} with expectations {}", sw.getStringId(), receivedRole, expectation); pendingReplies.invalidate(xid); if (expectation == RoleRecvStatus.MATCHED_CURRENT_ROLE || expectation == RoleRecvStatus.MATCHED_SET_ROLE) { return expectation; } else { return RoleRecvStatus.OTHER_EXPECTATION; } } pendingReplies.invalidate(xid); if (expectation == RoleRecvStatus.REPLY_QUERY) { return expectation; } return RoleRecvStatus.OTHER_EXPECTATION; }
RoleManager implements RoleHandler { @Override public synchronized RoleRecvStatus deliverRoleReply(RoleReplyInfo rri) throws SwitchStateException { int xid = (int) rri.getXid(); RoleState receivedRole = rri.getRole(); RoleState expectedRole = pendingReplies.getIfPresent(xid); if (expectedRole == null) { RoleState currentRole = (sw != null) ? sw.getRole() : null; if (currentRole != null) { if (currentRole == rri.getRole()) { log.debug("Received unexpected RoleReply from " + "Switch: {}. " + "Role in reply is same as current role of this " + "controller for this sw. Ignoring ...", sw.getStringId()); return RoleRecvStatus.OTHER_EXPECTATION; } else { String msg = String.format("Switch: [%s], " + "received unexpected RoleReply[%s]. " + "No roles are pending, and this controller's " + "current role:[%s] does not match reply. " + "Disconnecting switch ... ", sw.getStringId(), rri, currentRole); throw new SwitchStateException(msg); } } log.debug("Received unexpected RoleReply {} from " + "Switch: {}. " + "This controller has no current role for this sw. " + "Ignoring ...", rri, sw == null ? "(null)" : sw.getStringId()); return RoleRecvStatus.OTHER_EXPECTATION; } sw.returnRoleReply(expectedRole, receivedRole); if (expectedRole == receivedRole) { log.debug("Received role reply message from {} that matched " + "expected role-reply {} with expectations {}", sw.getStringId(), receivedRole, expectation); pendingReplies.invalidate(xid); if (expectation == RoleRecvStatus.MATCHED_CURRENT_ROLE || expectation == RoleRecvStatus.MATCHED_SET_ROLE) { return expectation; } else { return RoleRecvStatus.OTHER_EXPECTATION; } } pendingReplies.invalidate(xid); if (expectation == RoleRecvStatus.REPLY_QUERY) { return expectation; } return RoleRecvStatus.OTHER_EXPECTATION; } }
RoleManager implements RoleHandler { @Override public synchronized RoleRecvStatus deliverRoleReply(RoleReplyInfo rri) throws SwitchStateException { int xid = (int) rri.getXid(); RoleState receivedRole = rri.getRole(); RoleState expectedRole = pendingReplies.getIfPresent(xid); if (expectedRole == null) { RoleState currentRole = (sw != null) ? sw.getRole() : null; if (currentRole != null) { if (currentRole == rri.getRole()) { log.debug("Received unexpected RoleReply from " + "Switch: {}. " + "Role in reply is same as current role of this " + "controller for this sw. Ignoring ...", sw.getStringId()); return RoleRecvStatus.OTHER_EXPECTATION; } else { String msg = String.format("Switch: [%s], " + "received unexpected RoleReply[%s]. " + "No roles are pending, and this controller's " + "current role:[%s] does not match reply. " + "Disconnecting switch ... ", sw.getStringId(), rri, currentRole); throw new SwitchStateException(msg); } } log.debug("Received unexpected RoleReply {} from " + "Switch: {}. " + "This controller has no current role for this sw. " + "Ignoring ...", rri, sw == null ? "(null)" : sw.getStringId()); return RoleRecvStatus.OTHER_EXPECTATION; } sw.returnRoleReply(expectedRole, receivedRole); if (expectedRole == receivedRole) { log.debug("Received role reply message from {} that matched " + "expected role-reply {} with expectations {}", sw.getStringId(), receivedRole, expectation); pendingReplies.invalidate(xid); if (expectation == RoleRecvStatus.MATCHED_CURRENT_ROLE || expectation == RoleRecvStatus.MATCHED_SET_ROLE) { return expectation; } else { return RoleRecvStatus.OTHER_EXPECTATION; } } pendingReplies.invalidate(xid); if (expectation == RoleRecvStatus.REPLY_QUERY) { return expectation; } return RoleRecvStatus.OTHER_EXPECTATION; } RoleManager(OpenFlowSwitchDriver sw); }
RoleManager implements RoleHandler { @Override public synchronized RoleRecvStatus deliverRoleReply(RoleReplyInfo rri) throws SwitchStateException { int xid = (int) rri.getXid(); RoleState receivedRole = rri.getRole(); RoleState expectedRole = pendingReplies.getIfPresent(xid); if (expectedRole == null) { RoleState currentRole = (sw != null) ? sw.getRole() : null; if (currentRole != null) { if (currentRole == rri.getRole()) { log.debug("Received unexpected RoleReply from " + "Switch: {}. " + "Role in reply is same as current role of this " + "controller for this sw. Ignoring ...", sw.getStringId()); return RoleRecvStatus.OTHER_EXPECTATION; } else { String msg = String.format("Switch: [%s], " + "received unexpected RoleReply[%s]. " + "No roles are pending, and this controller's " + "current role:[%s] does not match reply. " + "Disconnecting switch ... ", sw.getStringId(), rri, currentRole); throw new SwitchStateException(msg); } } log.debug("Received unexpected RoleReply {} from " + "Switch: {}. " + "This controller has no current role for this sw. " + "Ignoring ...", rri, sw == null ? "(null)" : sw.getStringId()); return RoleRecvStatus.OTHER_EXPECTATION; } sw.returnRoleReply(expectedRole, receivedRole); if (expectedRole == receivedRole) { log.debug("Received role reply message from {} that matched " + "expected role-reply {} with expectations {}", sw.getStringId(), receivedRole, expectation); pendingReplies.invalidate(xid); if (expectation == RoleRecvStatus.MATCHED_CURRENT_ROLE || expectation == RoleRecvStatus.MATCHED_SET_ROLE) { return expectation; } else { return RoleRecvStatus.OTHER_EXPECTATION; } } pendingReplies.invalidate(xid); if (expectation == RoleRecvStatus.REPLY_QUERY) { return expectation; } return RoleRecvStatus.OTHER_EXPECTATION; } RoleManager(OpenFlowSwitchDriver sw); @Override synchronized boolean sendRoleRequest(RoleState role, RoleRecvStatus exp); @Override synchronized RoleRecvStatus deliverRoleReply(RoleReplyInfo rri); @Override synchronized RoleRecvStatus deliverError(OFErrorMsg error); @Override RoleState extractNiciraRoleReply(OFExperimenter experimenterMsg); @Override RoleReplyInfo extractOFRoleReply(OFRoleReply rrmsg); }
RoleManager implements RoleHandler { @Override public synchronized RoleRecvStatus deliverRoleReply(RoleReplyInfo rri) throws SwitchStateException { int xid = (int) rri.getXid(); RoleState receivedRole = rri.getRole(); RoleState expectedRole = pendingReplies.getIfPresent(xid); if (expectedRole == null) { RoleState currentRole = (sw != null) ? sw.getRole() : null; if (currentRole != null) { if (currentRole == rri.getRole()) { log.debug("Received unexpected RoleReply from " + "Switch: {}. " + "Role in reply is same as current role of this " + "controller for this sw. Ignoring ...", sw.getStringId()); return RoleRecvStatus.OTHER_EXPECTATION; } else { String msg = String.format("Switch: [%s], " + "received unexpected RoleReply[%s]. " + "No roles are pending, and this controller's " + "current role:[%s] does not match reply. " + "Disconnecting switch ... ", sw.getStringId(), rri, currentRole); throw new SwitchStateException(msg); } } log.debug("Received unexpected RoleReply {} from " + "Switch: {}. " + "This controller has no current role for this sw. " + "Ignoring ...", rri, sw == null ? "(null)" : sw.getStringId()); return RoleRecvStatus.OTHER_EXPECTATION; } sw.returnRoleReply(expectedRole, receivedRole); if (expectedRole == receivedRole) { log.debug("Received role reply message from {} that matched " + "expected role-reply {} with expectations {}", sw.getStringId(), receivedRole, expectation); pendingReplies.invalidate(xid); if (expectation == RoleRecvStatus.MATCHED_CURRENT_ROLE || expectation == RoleRecvStatus.MATCHED_SET_ROLE) { return expectation; } else { return RoleRecvStatus.OTHER_EXPECTATION; } } pendingReplies.invalidate(xid); if (expectation == RoleRecvStatus.REPLY_QUERY) { return expectation; } return RoleRecvStatus.OTHER_EXPECTATION; } RoleManager(OpenFlowSwitchDriver sw); @Override synchronized boolean sendRoleRequest(RoleState role, RoleRecvStatus exp); @Override synchronized RoleRecvStatus deliverRoleReply(RoleReplyInfo rri); @Override synchronized RoleRecvStatus deliverError(OFErrorMsg error); @Override RoleState extractNiciraRoleReply(OFExperimenter experimenterMsg); @Override RoleReplyInfo extractOFRoleReply(OFRoleReply rrmsg);  }