package org.opendaylight.protocol.pcep.impl;
+import static java.util.Objects.requireNonNull;
import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.doReturn;
import static org.opendaylight.protocol.util.CheckUtil.waitFutureSuccess;
-import com.google.common.base.Preconditions;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import org.mockito.MockitoAnnotations;
import org.opendaylight.protocol.concepts.KeyMapping;
import org.opendaylight.protocol.pcep.PCEPCapability;
+import org.opendaylight.protocol.pcep.PCEPDispatcherDependencies;
import org.opendaylight.protocol.pcep.PCEPSession;
import org.opendaylight.protocol.pcep.PCEPSessionListenerFactory;
import org.opendaylight.protocol.pcep.PCEPSessionNegotiatorFactory;
@Mock
private Channel mockChannel;
+ @Mock
+ private PCEPDispatcherDependencies dispatcherDependencies;
+ @Mock
+ private PCEPSessionListenerFactory listenerFactory;
private PCCMock pccMock;
MockitoAnnotations.initMocks(this);
final List<PCEPCapability> capList = new ArrayList<>();
final PCEPSessionProposalFactory sessionProposal = new BasePCEPSessionProposalFactory(DEAD_TIMER, KEEP_ALIVE,
- capList);
+ capList);
final EventLoopGroup eventLoopGroup;
if (Epoll.isAvailable()) {
eventLoopGroup = new EpollEventLoopGroup();
eventLoopGroup = new NioEventLoopGroup();
}
final MessageRegistry msgReg = ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance()
- .getMessageHandlerRegistry();
- this.dispatcher = new PCEPDispatcherImpl(msgReg, new DefaultPCEPSessionNegotiatorFactory(sessionProposal, 0),
- eventLoopGroup, eventLoopGroup);
+ .getMessageHandlerRegistry();
+ this.dispatcher = new PCEPDispatcherImpl(msgReg,
+ new DefaultPCEPSessionNegotiatorFactory(sessionProposal, 0),
+ eventLoopGroup, eventLoopGroup);
+
+ doReturn(KeyMapping.getKeyMapping()).when(this.dispatcherDependencies).getKeys();
+ doReturn(null).when(this.dispatcherDependencies).getPeerProposal();
- Mockito.doReturn("mockChannel").when(this.mockChannel).toString();
+ doReturn("mockChannel").when(this.mockChannel).toString();
final PCEPDispatcherImpl dispatcher2 = new PCEPDispatcherImpl(msgReg,
- new DefaultPCEPSessionNegotiatorFactory(sessionProposal, 0),
- eventLoopGroup, eventLoopGroup);
+ new DefaultPCEPSessionNegotiatorFactory(sessionProposal, 0),
+ eventLoopGroup, eventLoopGroup);
this.disp2Spy = Mockito.spy(dispatcher2);
this.pccMock = new PCCMock(new DefaultPCEPSessionNegotiatorFactory(sessionProposal, 0),
- new PCEPHandlerFactory(msgReg));
+ new PCEPHandlerFactory(msgReg));
}
@Test
final InetSocketAddress serverAddr = new InetSocketAddress("0.0.0.0", port);
final InetSocketAddress clientAddr1 = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress(port);
final InetSocketAddress clientAddr2 = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress(port);
- final ChannelFuture futureChannel = this.dispatcher.createServer(serverAddr,
- SimpleSessionListener::new, null);
+
+ doReturn(serverAddr).when(this.dispatcherDependencies).getAddress();
+ doReturn(this.listenerFactory).when(this.dispatcherDependencies).getListenerFactory();
+ doReturn(new SimpleSessionListener()).when(this.listenerFactory).getSessionListener();
+ final ChannelFuture futureChannel = this.dispatcher.createServer(this.dispatcherDependencies);
waitFutureSuccess(futureChannel);
- final PCEPSessionImpl session1 = (PCEPSessionImpl) this.pccMock.createClient(clientAddr1,
- RETRY_TIMER, CONNECT_TIMEOUT, SimpleSessionListener::new).get();
+ final PCEPSessionImpl session1 = this.pccMock.createClient(clientAddr1,
+ RETRY_TIMER, CONNECT_TIMEOUT, SimpleSessionListener::new).get();
- final PCEPSessionImpl session2 = (PCEPSessionImpl) this.pccMock.createClient(clientAddr2,
- RETRY_TIMER, CONNECT_TIMEOUT, SimpleSessionListener::new).get();
+ final PCEPSessionImpl session2 = this.pccMock.createClient(clientAddr2,
+ RETRY_TIMER, CONNECT_TIMEOUT, SimpleSessionListener::new).get();
Assert.assertTrue(futureChannel.channel().isActive());
assertEquals(clientAddr1.getAddress().getHostAddress(), session1.getPeerPref().getIpAddress());
final int port = InetSocketAddressUtil.getRandomPort();
final InetSocketAddress serverAddr = new InetSocketAddress("0.0.0.0", port);
final InetSocketAddress clientAddr = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress(port);
- waitFutureSuccess(this.dispatcher.createServer(serverAddr, SimpleSessionListener::new, null));
- final Future<PCEPSession> futureClient = this.pccMock.createClient(clientAddr, RETRY_TIMER, CONNECT_TIMEOUT,
- SimpleSessionListener::new);
+
+ doReturn(serverAddr).when(this.dispatcherDependencies).getAddress();
+ doReturn(this.listenerFactory).when(this.dispatcherDependencies).getListenerFactory();
+ doReturn(new SimpleSessionListener()).when(this.listenerFactory).getSessionListener();
+
+ waitFutureSuccess(this.dispatcher.createServer(this.dispatcherDependencies));
+ final Future<PCEPSessionImpl> futureClient = this.pccMock.createClient(clientAddr, RETRY_TIMER, CONNECT_TIMEOUT,
+ SimpleSessionListener::new);
waitFutureSuccess(futureClient);
- final PCEPSessionImpl session1 = (PCEPSessionImpl) futureClient.get();
+ final PCEPSessionImpl session1 = futureClient.get();
try {
this.pccMock.createClient(clientAddr, RETRY_TIMER, CONNECT_TIMEOUT,
- SimpleSessionListener::new).get();
+ SimpleSessionListener::new).get();
Assert.fail();
} catch (final ExecutionException e) {
Assert.assertTrue(e.getMessage().contains("A conflicting session for address"));
public void testReconectClient() throws InterruptedException, ExecutionException {
final int port = InetSocketAddressUtil.getRandomPort();
final InetSocketAddress clientAddr = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress(port);
- waitFutureSuccess(this.dispatcher.createServer(new InetSocketAddress("0.0.0.0", port),
- SimpleSessionListener::new, null));
- final PCEPSessionImpl session1 = (PCEPSessionImpl) this.pccMock.createClient(clientAddr,
- RETRY_TIMER, CONNECT_TIMEOUT, SimpleSessionListener::new).get();
+
+ doReturn(new InetSocketAddress("0.0.0.0", port)).when(this.dispatcherDependencies).getAddress();
+ doReturn(this.listenerFactory).when(this.dispatcherDependencies).getListenerFactory();
+ doReturn(new SimpleSessionListener()).when(this.listenerFactory).getSessionListener();
+ waitFutureSuccess(this.dispatcher.createServer(this.dispatcherDependencies));
+ final PCEPSessionImpl session1 = this.pccMock.createClient(clientAddr,
+ RETRY_TIMER, CONNECT_TIMEOUT, SimpleSessionListener::new).get();
assertEquals(clientAddr.getAddress(), session1.getRemoteAddress());
assertEquals(DEAD_TIMER, session1.getDeadTimerValue().shortValue());
assertEquals(KEEP_ALIVE, session1.getKeepAliveTimerValue().shortValue());
waitFutureSuccess(session1.closeChannel());
- final PCEPSessionImpl session2 = (PCEPSessionImpl) this.pccMock.createClient(clientAddr,
- RETRY_TIMER, CONNECT_TIMEOUT, SimpleSessionListener::new).get();
+ final PCEPSessionImpl session2 = this.pccMock.createClient(clientAddr,
+ RETRY_TIMER, CONNECT_TIMEOUT, SimpleSessionListener::new).get();
assertEquals(clientAddr.getAddress(), session1.getRemoteAddress());
assertEquals(DEAD_TIMER, session2.getDeadTimerValue().shortValue());
final KeyMapping keys = KeyMapping.getKeyMapping(clientAddr1.getAddress(), "CLIENT1_ADDRESS");
keys.put(clientAddr2.getAddress(), "CLIENT2_ADDRESS".getBytes());
- final ChannelFuture futureChannel = this.disp2Spy.createServer(new InetSocketAddress("0.0.0.0", port),
- SimpleSessionListener::new, null);
+ doReturn(new InetSocketAddress("0.0.0.0", port)).when(this.dispatcherDependencies).getAddress();
+ doReturn(this.listenerFactory).when(this.dispatcherDependencies).getListenerFactory();
+ doReturn(new SimpleSessionListener()).when(this.listenerFactory).getSessionListener();
+
+ final ChannelFuture futureChannel = this.disp2Spy.createServer(this.dispatcherDependencies);
waitFutureSuccess(futureChannel);
Mockito.verify(this.disp2Spy).createServerBootstrap(any(PCEPDispatcherImpl.ChannelPipelineInitializer.class));
}
}
private static class PCCMock {
- private final PCEPSessionNegotiatorFactory negotiatorFactory;
+ private final PCEPSessionNegotiatorFactory<PCEPSessionImpl> negotiatorFactory;
private final PCEPHandlerFactory factory;
private final EventExecutor executor;
private final EventLoopGroup workerGroup;
- PCCMock(final PCEPSessionNegotiatorFactory negotiatorFactory, final PCEPHandlerFactory factory) {
- this.workerGroup = Preconditions.checkNotNull(new NioEventLoopGroup());
- this.negotiatorFactory = Preconditions.checkNotNull(negotiatorFactory);
- this.factory = Preconditions.checkNotNull(factory);
- this.executor = Preconditions.checkNotNull(GlobalEventExecutor.INSTANCE);
+ PCCMock(final PCEPSessionNegotiatorFactory<PCEPSessionImpl> negotiatorFactory,
+ final PCEPHandlerFactory factory) {
+ this.workerGroup = new NioEventLoopGroup();
+ this.negotiatorFactory = requireNonNull(negotiatorFactory);
+ this.factory = requireNonNull(factory);
+ this.executor = requireNonNull(GlobalEventExecutor.INSTANCE);
}
- Future<PCEPSession> createClient(final InetSocketAddress address, final int retryTimer,
- final int connectTimeout, final PCEPSessionListenerFactory listenerFactory) {
+ Future<PCEPSessionImpl> createClient(final InetSocketAddress address, final int retryTimer,
+ final int connectTimeout, final PCEPSessionListenerFactory listenerFactory) {
return createClient(address, retryTimer, connectTimeout, (ch, promise) -> {
ch.pipeline().addLast(this.factory.getDecoders());
- ch.pipeline().addLast("negotiator", this.negotiatorFactory.getSessionNegotiator(listenerFactory, ch,
- promise, null));
+ ch.pipeline().addLast("negotiator", this.negotiatorFactory.getSessionNegotiator(
+ () -> listenerFactory,
+ ch,
+ promise));
ch.pipeline().addLast(this.factory.getEncoders());
});
}
- Future<PCEPSession> createClient(final InetSocketAddress address, final int retryTimer,
- final int connectTimeout, final PCEPDispatcherImpl.ChannelPipelineInitializer initializer) {
+ Future<PCEPSessionImpl> createClient(final InetSocketAddress address, final int retryTimer,
+ final int connectTimeout, final PCEPDispatcherImpl.ChannelPipelineInitializer initializer) {
final Bootstrap b = new Bootstrap();
- final PCEPProtocolSessionPromise p = new PCEPProtocolSessionPromise(this.executor, address, retryTimer,
- connectTimeout, b);
- (b.option(ChannelOption.SO_KEEPALIVE, Boolean.TRUE)).handler(new ChannelInitializer<SocketChannel>() {
+ final PCEPProtocolSessionPromise<PCEPSessionImpl> p = new PCEPProtocolSessionPromise<>(this.executor,
+ address, retryTimer, connectTimeout, b);
+ b.option(ChannelOption.SO_KEEPALIVE, Boolean.TRUE).handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(final SocketChannel ch) {
initializer.initializeChannel(ch, p);
return p;
}
- private void setChannelFactory(final Bootstrap b) {
+ private static void setChannelFactory(final Bootstrap b) {
try {
b.channel(NioSocketChannel.class);
} catch (final IllegalStateException ignored) {
}
-
}
private void setWorkerGroup(final Bootstrap b) {
- if (b.group() == null) {
+ if (b.config().group() == null) {
b.group(this.workerGroup);
}
}