void processOFEchoRequest(OFChannelHandler h, OFEchoRequest m) throws IOException { if (h.ofVersion == null) { log.error("No OF version set for {}. Not sending Echo REPLY", h.channel.getRemoteAddress()); return; } OFFactory factory = (h.ofVersion == OFVersion.OF_13) ? h.controller.getOFMessageFactory13() : h.controller.getOFMessageFactory10(); OFEchoReply reply = factory .buildEchoReply() .setXid(m.getXid()) .setData(m.getData()) .build(); h.channel.write(Collections.singletonList(reply)); }
@Test(timeout = 5000) public void testWriteRequestNotConnectedFailure() throws InterruptedException, ExecutionException { EasyMock.expect(channel.isActive()).andReturn(Boolean.FALSE).anyTimes(); replay(channel); OFEchoRequest echoRequest = factory.echoRequest(new byte[] {}); ListenableFuture<OFEchoReply> future = conn.writeRequest(echoRequest); SwitchDisconnectedException e = FutureTestUtils.assertFutureFailedWithException(future, SwitchDisconnectedException.class); assertThat(e.getId(), equalTo(switchId)); assertThat("Connection should have no pending requests", conn.getPendingRequestIds().isEmpty(), equalTo(true)); }
@Test(timeout = 5000) public void testWriteRequestDisconnectFailure() throws InterruptedException, ExecutionException { prepareChannelForWriteList(); OFEchoRequest echoRequest = factory.echoRequest(new byte[] {}); ListenableFuture<OFEchoReply> future = conn.writeRequest(echoRequest); assertThat("Connection should have 1 pending request", conn.getPendingRequestIds().size(), equalTo(1)); assertThat("Future should not be complete yet", future.isDone(), equalTo(false)); conn.disconnected(); SwitchDisconnectedException e = FutureTestUtils.assertFutureFailedWithException(future, SwitchDisconnectedException.class); assertThat(e.getId(), equalTo(switchId)); assertThat("Connection should have no pending requests", conn.getPendingRequestIds().isEmpty(), equalTo(true)); }
@Test(timeout = 5000) public void testWriteRequestSuccess() throws InterruptedException, ExecutionException { Capture<List<OFMessage>> cMsgList = prepareChannelForWriteList(); OFEchoRequest echoRequest = factory.echoRequest(new byte[] {}); ListenableFuture<OFEchoReply> future = conn.writeRequest(echoRequest); assertThat("Connection should have 1 pending request", conn.getPendingRequestIds().size(), equalTo(1)); assertThat("Should have captured MsgList", cMsgList.getValue(), Matchers.<OFMessage> contains(echoRequest)); assertThat("Future should not be complete yet", future.isDone(), equalTo(false)); OFEchoReply echoReply = factory.buildEchoReply() .setXid(echoRequest.getXid()) .build(); assertThat("Connection should have accepted the response", conn.deliverResponse(echoReply), equalTo(true)); assertThat("Future should be complete ", future.isDone(), equalTo(true)); assertThat(future.get(), equalTo(echoReply)); assertThat("Connection should have no pending requests", conn.getPendingRequestIds().isEmpty(), equalTo(true)); }
@Test(timeout = 5000) public void testWriteRequestNotConnectedFailure() throws InterruptedException, ExecutionException { EasyMock.expect(channel.isConnected()).andReturn(Boolean.FALSE).anyTimes(); replay(channel); OFEchoRequest echoRequest = factory.echoRequest(new byte[] {}); ListenableFuture<OFEchoReply> future = conn.writeRequest(echoRequest); SwitchDisconnectedException e = FutureTestUtils.assertFutureFailedWithException(future, SwitchDisconnectedException.class); assertThat(e.getId(), equalTo(switchId)); assertThat("Connection should have no pending requests", conn.getPendingRequestIds().isEmpty(), equalTo(true)); }
/** * Construct a suitable OpenFlow echo request based upon the OpenFlow version the upstream channel advertised * version. * * @return Container with a suitable echo request */ public Container createPing() { ByteBuf byteBuf = upstream.alloc().buffer(8); NettyCompatibilityChannelBuffer compatBuffer = new NettyCompatibilityChannelBuffer(byteBuf); OFEchoRequest request = OFFactories.getFactory(upstreamVersion).echoRequest(ECHO_DATA); request.writeTo(compatBuffer); byte[] rawData = new byte[compatBuffer.readableBytes()]; compatBuffer.resetReaderIndex(); compatBuffer.readBytes(rawData); ReferenceCountUtil.release(byteBuf); Header header = new Header((short) upstreamVersion.getWireVersion(), (short) Type.OFPT_ECHO_REQUEST.getId(), 8, request.getXid()); return new Container(header, rawData, Type.OFPT_ECHO_REQUEST, request); }
@Override void processOFMessage(OFChannelHandler h, OFMessage m) throws IOException, SwitchStateException { if (h.sw.isDriverHandshakeComplete()) { moveToActive(h); h.state.processOFMessage(h, m); return; } if (m.getType() == OFType.ECHO_REQUEST) { processOFEchoRequest(h, (OFEchoRequest) m); } else if (m.getType() == OFType.ECHO_REPLY) { processOFEchoReply(h, (OFEchoReply) m); } else if (m.getType() == OFType.ROLE_REPLY) { h.sw.handleRole(m); } else if (m.getType() == OFType.ERROR) { if (!h.sw.handleRoleError((OFErrorMsg)m)) { h.sw.processDriverHandshakeMessage(m); if (h.sw.isDriverHandshakeComplete()) { moveToActive(h); } } } else { if (m.getType() == OFType.EXPERIMENTER && ((OFExperimenter) m).getExperimenter() == RoleManager.NICIRA_EXPERIMENTER) { h.sw.handleNiciraRole(m); } else { h.sw.processDriverHandshakeMessage(m); if (h.sw.isDriverHandshakeComplete()) { moveToActive(h); } } } }
private void sendEchoRequest() { OFEchoRequest request = factory.buildEchoRequest() .setXid(handshakeTransactionIds--) .build(); /* Record for latency calculation */ echoSendTime = System.currentTimeMillis(); write(request); }
private void sendEchoReply(OFEchoRequest request) { OFEchoReply reply = factory.buildEchoReply() .setXid(request.getXid()) .setData(request.getData()) .build(); write(reply); }
@Test(timeout = 5000) public void testWriteRequestSuccess() throws InterruptedException, ExecutionException { Capture<List<OFMessage>> cMsgList = prepareChannelForWriteList(); OFEchoRequest echoRequest = factory.echoRequest(new byte[] {}); ListenableFuture<OFEchoReply> future = conn.writeRequest(echoRequest); assertThat("Connection should have 1 pending request", conn.getPendingRequestIds().size(), equalTo(1)); eventLoop.runTasks(); assertThat("Should have captured MsgList", cMsgList.getValue(), Matchers.<OFMessage> contains(echoRequest)); assertThat("Future should not be complete yet", future.isDone(), equalTo(false)); OFEchoReply echoReply = factory.buildEchoReply() .setXid(echoRequest.getXid()) .build(); assertThat("Connection should have accepted the response", conn.deliverResponse(echoReply), equalTo(true)); assertThat("Future should be complete ", future.isDone(), equalTo(true)); assertThat(future.get(), equalTo(echoReply)); assertThat("Connection should have no pending requests", conn.getPendingRequestIds().isEmpty(), equalTo(true)); }
private void sendEchoReply(OFEchoRequest request) { OFEchoReply reply = factory.buildEchoReply() .setXid(request.getXid()) .setData(request.getData()) .build(); channel.write(Collections.singletonList(reply)); }