import com.google.common.base.Optional;
import com.google.common.collect.ImmutableSet;
import io.netty.channel.Channel;
-import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.ChannelProgressivePromise;
+import io.netty.channel.ChannelPromise;
import io.netty.channel.EventLoop;
import io.netty.handler.ssl.SslHandler;
import io.netty.util.HashedWheelTimer;
import org.junit.Before;
import org.junit.Test;
import org.mockito.internal.util.collections.Sets;
-import org.mockito.invocation.InvocationOnMock;
-import org.mockito.stubbing.Answer;
import org.opendaylight.controller.config.util.xml.XmlUtil;
import org.opendaylight.netconf.api.NetconfClientSessionPreferences;
+import org.opendaylight.netconf.api.NetconfDocumentedException;
import org.opendaylight.netconf.api.NetconfMessage;
import org.opendaylight.netconf.api.messages.NetconfHelloMessage;
-import org.opendaylight.netconf.api.messages.NetconfHelloMessageAdditionalHeader;
import org.opendaylight.netconf.nettyutil.handler.ChunkedFramingMechanismEncoder;
import org.opendaylight.netconf.nettyutil.handler.NetconfXMLToHelloMessageDecoder;
import org.opendaylight.netconf.nettyutil.handler.NetconfXMLToMessageDecoder;
private NetconfHelloMessage helloMessage;
private ChannelPipeline pipeline;
- private ChannelFuture future;
+ private ChannelPromise future;
private Channel channel;
private ChannelInboundHandlerAdapter channelInboundHandlerAdapter;
@Before
public void setUp() throws Exception {
- helloMessage = NetconfHelloMessage.createClientHello(Sets.newSet("exi:1.0"), Optional
- .<NetconfHelloMessageAdditionalHeader>absent());
+ helloMessage = NetconfHelloMessage.createClientHello(Sets.newSet("exi:1.0"), Optional.absent());
pipeline = mockChannelPipeline();
future = mockChannelFuture();
channel = mockChannel();
Channel ret = mock(Channel.class);
ChannelHandler channelHandler = mockChannelHandler();
doReturn("").when(ret).toString();
+ doReturn(future).when(ret).newPromise();
doReturn(future).when(ret).close();
doReturn(future).when(ret).writeAndFlush(anyObject());
+ doReturn(future).when(ret).writeAndFlush(anyObject(), anyObject());
doReturn(true).when(ret).isOpen();
doReturn(pipeline).when(ret).pipeline();
doReturn("").when(pipeline).toString();
return ret;
}
- private static ChannelFuture mockChannelFuture() {
- ChannelFuture future = mock(ChannelFuture.class);
+ private static ChannelPromise mockChannelFuture() {
+ ChannelPromise future = mock(ChannelPromise.class);
doReturn(future).when(future).addListener(any(GenericFutureListener.class));
return future;
}
private void mockEventLoop() {
final EventLoop eventLoop = mock(EventLoop.class);
doReturn(eventLoop).when(channel).eventLoop();
- doAnswer(new Answer<Void>() {
- @Override
- public Void answer(InvocationOnMock invocation) throws Throwable {
- final Object[] args = invocation.getArguments();
- final Runnable runnable = (Runnable) args[0];
- runnable.run();
- return null;
- }
+ doAnswer(invocation -> {
+ invocation.getArgumentAt(0, Runnable.class).run();
+ return null;
}).when(eventLoop).execute(any(Runnable.class));
}
return new NetconfClientSessionNegotiator(preferences, promise, channel, timer, sessionListener, timeout);
}
- private NetconfHelloMessage createHelloMsg(final String name) throws Exception {
+ private static NetconfHelloMessage createHelloMsg(final String name) throws Exception {
final InputStream stream = NetconfClientSessionNegotiatorTest.class.getResourceAsStream(name);
final Document doc = XmlUtil.readXmlToDocument(stream);
return new NetconfHelloMessage(doc);
}
- private Set<String> createCapabilities(String name) throws Exception {
+ private static Set<String> createCapabilities(final String name) throws Exception {
NetconfHelloMessage hello = createHelloMsg(name);
return ImmutableSet.copyOf(NetconfMessageUtil.extractCapabilitiesFromHello(hello.getDocument()));
}
@Test
- public void testNetconfClientSessionNegotiator() throws Exception {
+ public void testNetconfClientSessionNegotiator() throws NetconfDocumentedException {
Promise<NetconfClientSession> promise = mock(Promise.class);
doReturn(promise).when(promise).setSuccess(anyObject());
NetconfClientSessionNegotiator negotiator = createNetconfClientSessionNegotiator(promise, null);
Set<String> caps = Sets.newSet("exi:1.0");
NetconfHelloMessage message = NetconfHelloMessage.createServerHello(caps, 10);
- doAnswer(new Answer<Object>() {
- @Override
- public Object answer(final InvocationOnMock invocationOnMock) throws Throwable {
- channelInboundHandlerAdapter = ((ChannelInboundHandlerAdapter) invocationOnMock.getArguments()[2]);
- return null;
- }
+ doAnswer(invocationOnMock -> {
+ channelInboundHandlerAdapter = (ChannelInboundHandlerAdapter) invocationOnMock.getArguments()[2];
+ return null;
}).when(pipeline).addAfter(anyString(), anyString(), any(ChannelHandler.class));
ChannelHandlerContext handlerContext = mock(ChannelHandlerContext.class);
import static org.mockito.Mockito.verify;
import io.netty.channel.Channel;
-import io.netty.channel.ChannelFuture;
+import io.netty.channel.ChannelPromise;
import io.netty.channel.EventLoop;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import org.junit.Before;
import org.junit.Test;
import org.mockito.internal.util.collections.Sets;
-import org.mockito.invocation.InvocationOnMock;
-import org.mockito.stubbing.Answer;
+import org.opendaylight.netconf.api.NetconfDocumentedException;
import org.opendaylight.netconf.api.NetconfMessage;
import org.opendaylight.netconf.api.messages.NetconfHelloMessage;
public class SimpleNetconfClientSessionListenerTest {
private Channel channel;
- private ChannelFuture channelFuture;
+ private ChannelPromise channelFuture;
Set<String> caps;
private NetconfHelloMessage helloMessage;
private NetconfMessage message;
private NetconfClientSession clientSession;
@Before
- public void setUp() throws Exception {
+ public void setUp() throws NetconfDocumentedException {
channel = mock(Channel.class);
- channelFuture = mock(ChannelFuture.class);
+ channelFuture = mock(ChannelPromise.class);
mockEventLoop();
+ doReturn(channelFuture).when(channel).newPromise();
doReturn(channelFuture).when(channel).writeAndFlush(anyObject());
+ doReturn(channelFuture).when(channel).writeAndFlush(anyObject(), any(ChannelPromise.class));
doReturn(channelFuture).when(channelFuture).addListener(any(GenericFutureListener.class));
caps = Sets.newSet("a", "b");
helloMessage = NetconfHelloMessage.createServerHello(caps, 10);
private void mockEventLoop() {
final EventLoop eventLoop = mock(EventLoop.class);
doReturn(eventLoop).when(channel).eventLoop();
- doAnswer(new Answer<Void>() {
- @Override
- public Void answer(InvocationOnMock invocation) throws Throwable {
- final Object[] args = invocation.getArguments();
- final Runnable runnable = (Runnable) args[0];
- runnable.run();
- return null;
- }
+ doAnswer(invocation -> {
+ invocation.getArgumentAt(0, Runnable.class).run();
+ return null;
}).when(eventLoop).execute(any(Runnable.class));
}
SimpleNetconfClientSessionListener simpleListener = new SimpleNetconfClientSessionListener();
final Future<NetconfMessage> promise = simpleListener.sendRequest(message);
simpleListener.onSessionUp(clientSession);
- verify(channel, times(1)).writeAndFlush(anyObject());
+ verify(channel, times(1)).writeAndFlush(anyObject(), anyObject());
simpleListener.onSessionDown(clientSession, new Exception());
assertFalse(promise.isSuccess());
}
@Test
- public void testSendRequest() throws Exception {
+ public void testSendRequest() {
SimpleNetconfClientSessionListener simpleListener = new SimpleNetconfClientSessionListener();
final Future<NetconfMessage> promise = simpleListener.sendRequest(message);
simpleListener.onSessionUp(clientSession);
- verify(channel, times(1)).writeAndFlush(anyObject());
+ verify(channel, times(1)).writeAndFlush(anyObject(), anyObject());
simpleListener.sendRequest(message);
assertFalse(promise.isSuccess());
}
@Test
- public void testOnMessage() throws Exception {
+ public void testOnMessage() {
SimpleNetconfClientSessionListener simpleListener = new SimpleNetconfClientSessionListener();
final Future<NetconfMessage> promise = simpleListener.sendRequest(message);
simpleListener.onSessionUp(clientSession);
- verify(channel, times(1)).writeAndFlush(anyObject());
+ verify(channel, times(1)).writeAndFlush(anyObject(), anyObject());
simpleListener.onMessage(clientSession, message);
assertTrue(promise.isSuccess());
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
+import io.netty.channel.ChannelPromise;
import io.netty.channel.EventLoop;
import io.netty.util.concurrent.GenericFutureListener;
import org.junit.Test;
final EventLoop eventLoop = mock(EventLoop.class);
doReturn(eventLoop).when(channel).eventLoop();
doAnswer(invocation -> {
- final Object[] args = invocation.getArguments();
- final Runnable runnable = (Runnable) args[0];
- runnable.run();
+ invocation.getArgumentAt(0, Runnable.class).run();
return null;
}).when(eventLoop).execute(any(Runnable.class));
doReturn(true).when(eventLoop).inEventLoop();
doReturn(channelFuture).when(channel).close();
doReturn(channelFuture).when(channelFuture).addListener(any(GenericFutureListener.class));
- final ChannelFuture sendFuture = mock(ChannelFuture.class);
+ final ChannelPromise sendFuture = mock(ChannelPromise.class);
doAnswer(invocation -> {
- ((GenericFutureListener) invocation.getArguments()[0]).operationComplete(sendFuture);
+ invocation.getArgumentAt(0, GenericFutureListener.class).operationComplete(sendFuture);
return null;
}).when(sendFuture).addListener(any(GenericFutureListener.class));
- doReturn(sendFuture).when(channel).writeAndFlush(anyObject());
+ doReturn(sendFuture).when(channel).newPromise();
+ doReturn(sendFuture).when(channel).writeAndFlush(anyObject(), anyObject());
doReturn(true).when(sendFuture).isSuccess();
final NetconfServerSessionListener listener = mock(NetconfServerSessionListener.class);
doNothing().when(listener).onSessionTerminated(any(NetconfServerSession.class),
AutoCloseable res = mock(AutoCloseable.class);
doThrow(NetconfDocumentedException.class).when(res).close();
DefaultCloseSession session = new DefaultCloseSession("", res);
- Document doc = XmlUtil.newDocument();
XmlElement elem = XmlElement.fromDomElement(XmlUtil.readXmlToElement("<elem/>"));
- session.handleWithNoSubsequentOperations(doc, elem);
+ session.handleWithNoSubsequentOperations(XmlUtil.newDocument(), elem);
}
}
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandler;
-import io.netty.channel.DefaultChannelPromise;
+import io.netty.channel.ChannelPromise;
import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.handler.codec.MessageToByteEncoder;
-import io.netty.util.concurrent.Future;
-import io.netty.util.concurrent.FutureListener;
import java.io.IOException;
import org.opendaylight.controller.config.util.xml.XmlElement;
import org.opendaylight.netconf.api.NetconfExiSession;
// Restconf writes to a netconf mountpoint execute multiple messages
// and one of these was executed from a restconf thread thus breaking ordering so
// we need to execute all messages from an EventLoop thread.
- final DefaultChannelPromise proxyFuture = new DefaultChannelPromise(channel);
- channel.eventLoop().execute(new Runnable() {
- @Override
- public void run() {
- final ChannelFuture future = channel.writeAndFlush(netconfMessage);
- future.addListener(new FutureListener<Void>() {
- @Override
- public void operationComplete(final Future<Void> future) throws Exception {
- if (future.isSuccess()) {
- proxyFuture.setSuccess();
- } else {
- proxyFuture.setFailure(future.cause());
- }
- }
- });
- if (delayedEncoder != null) {
- replaceMessageEncoder(delayedEncoder);
- delayedEncoder = null;
- }
+
+ final ChannelPromise promise = channel.newPromise();
+ channel.eventLoop().execute(() -> {
+ channel.writeAndFlush(netconfMessage, promise);
+ if (delayedEncoder != null) {
+ replaceMessageEncoder(delayedEncoder);
+ delayedEncoder = null;
}
});
- return proxyFuture;
+ return promise;
}
@Override
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelPipeline;
+import io.netty.channel.ChannelPromise;
import io.netty.channel.EventLoop;
import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.handler.codec.MessageToByteEncoder;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
-import org.mockito.invocation.InvocationOnMock;
-import org.mockito.stubbing.Answer;
import org.opendaylight.netconf.api.NetconfMessage;
import org.opendaylight.netconf.api.NetconfSessionListener;
import org.opendaylight.netconf.api.NetconfTerminationReason;
import org.opendaylight.netconf.api.messages.NetconfHelloMessage;
-import org.opendaylight.netconf.api.messages.NetconfHelloMessageAdditionalHeader;
import org.opendaylight.netconf.nettyutil.handler.exi.EXIParameters;
import org.opendaylight.netconf.nettyutil.handler.exi.NetconfStartExiMessage;
@Mock
private EventLoop eventLoop;
@Mock
- private ChannelFuture writeFuture;
+ private ChannelPromise writeFuture;
private NetconfHelloMessage clientHello;
doReturn(writeFuture).when(writeFuture).addListener(any(GenericFutureListener.class));
+ doReturn(writeFuture).when(channel).newPromise();
doReturn(writeFuture).when(channel).writeAndFlush(any(NetconfMessage.class));
+ doReturn(writeFuture).when(channel).writeAndFlush(any(NetconfMessage.class), any(ChannelPromise.class));
doReturn(pipeline).when(channel).pipeline();
doReturn("mockChannel").when(channel).toString();
doReturn(mock(ChannelFuture.class)).when(channel).close();
doReturn(null).when(pipeline).replace(anyString(), anyString(), any(ChannelHandler.class));
doReturn(eventLoop).when(channel).eventLoop();
- doAnswer(new Answer<Void>() {
- @Override
- public Void answer(final InvocationOnMock invocation) throws Throwable {
- final Object[] args = invocation.getArguments();
- final Runnable runnable = (Runnable) args[0];
- runnable.run();
- return null;
- }
+ doAnswer(invocation -> {
+ invocation.getArgumentAt(0, Runnable.class).run();
+ return null;
}).when(eventLoop).execute(any(Runnable.class));
- clientHello = NetconfHelloMessage.createClientHello(Collections.<String>emptySet(),
- Optional.<NetconfHelloMessageAdditionalHeader>absent());
+ clientHello = NetconfHelloMessage.createClientHello(Collections.emptySet(), Optional.absent());
}
@Test
@Test
public void testSendMessage() throws Exception {
final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, 1L);
- final NetconfHelloMessage hello = NetconfHelloMessage.createClientHello(Collections.<String>emptySet(),
- Optional.<NetconfHelloMessageAdditionalHeader>absent());
+ final NetconfHelloMessage hello = NetconfHelloMessage.createClientHello(Collections.emptySet(),
+ Optional.absent());
testingNetconfSession.sendMessage(hello);
- verify(channel).writeAndFlush(hello);
+ verify(channel).writeAndFlush(hello, writeFuture);
}
}