@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)); }
@Override public <R extends OFMessage> ListenableFuture<R> writeRequest(OFRequest<R> request) { if (!isConnected()) { return Futures.immediateFailedFuture(new SwitchDisconnectedException(getDatapathId())); } DeliverableListenableFuture<R> future = new DeliverableListenableFuture<R>(); xidDeliverableMap.put(request.getXid(), future); listener.messageWritten(this, request); this.write(request); return future; }
public void disconnected() { SwitchDisconnectedException exception = new SwitchDisconnectedException(getDatapathId()); for (Long xid : xidDeliverableMap.keySet()) { // protect against other mechanisms running at the same time // (timeout) Deliverable<?> removed = xidDeliverableMap.remove(xid); if (removed != null) { removed.deliverError(exception); } } }
@Override public <REPLY extends OFStatsReply> ListenableFuture<List<REPLY>> writeStatsRequest( OFStatsRequest<REPLY> request) { if (!isConnected()) { return Futures.immediateFailedFuture(new SwitchDisconnectedException(getDatapathId())); } final DeliverableListenableFuture<List<REPLY>> future = new DeliverableListenableFuture<List<REPLY>>(); Deliverable<REPLY> deliverable = new Deliverable<REPLY>() { private final List<REPLY> results = Collections .synchronizedList(new ArrayList<REPLY>()); @Override public void deliver(REPLY reply) { results.add(reply); if (!reply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) { // done future.deliver(results); } } @Override public void deliverError(Throwable cause) { future.deliverError(cause); } @Override public boolean isDone() { return future.isDone(); } @Override public boolean cancel(boolean mayInterruptIfRunning) { return future.cancel(mayInterruptIfRunning); } }; registerDeliverable(request.getXid(), deliverable); this.write(request); return future; }
@Override public <REPLY extends OFStatsReply> ListenableFuture<List<REPLY>> writeStatsRequest( OFStatsRequest<REPLY> request) { return Futures.immediateFailedFuture(new SwitchDisconnectedException(getDatapathId())); }
@Override public <R extends OFMessage> ListenableFuture<R> writeRequest(OFRequest<R> request) { return Futures.immediateFailedFuture(new SwitchDisconnectedException(getDatapathId())); }