2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.protocol.bmp.impl.session;
11 import com.google.common.base.Optional;
12 import io.netty.bootstrap.Bootstrap;
13 import io.netty.channel.Channel;
14 import io.netty.channel.ChannelHandler;
15 import io.netty.channel.ChannelHandlerContext;
16 import io.netty.channel.ChannelInitializer;
17 import io.netty.channel.ChannelOption;
18 import io.netty.channel.nio.NioEventLoopGroup;
19 import io.netty.channel.socket.SocketChannel;
20 import io.netty.channel.socket.nio.NioSocketChannel;
21 import java.net.InetSocketAddress;
22 import org.junit.After;
23 import org.junit.Assert;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.mockito.Mock;
27 import org.mockito.Mockito;
28 import org.mockito.MockitoAnnotations;
29 import org.opendaylight.protocol.bgp.parser.impl.BGPActivator;
30 import org.opendaylight.protocol.bgp.parser.spi.BGPExtensionProviderContext;
31 import org.opendaylight.protocol.bgp.parser.spi.pojo.SimpleBGPExtensionProviderContext;
32 import org.opendaylight.protocol.bmp.api.BmpDispatcher;
33 import org.opendaylight.protocol.bmp.api.BmpSession;
34 import org.opendaylight.protocol.bmp.api.BmpSessionFactory;
35 import org.opendaylight.protocol.bmp.api.BmpSessionListenerFactory;
36 import org.opendaylight.protocol.bmp.impl.BmpActivator;
37 import org.opendaylight.protocol.bmp.impl.BmpDispatcherImpl;
38 import org.opendaylight.protocol.bmp.spi.registry.BmpMessageRegistry;
39 import org.opendaylight.protocol.bmp.spi.registry.SimpleBmpExtensionProviderContext;
40 import org.opendaylight.tcpmd5.api.KeyMapping;
41 import org.opendaylight.tcpmd5.netty.MD5ServerChannelFactory;
43 public class BmpDispatcherImplTest {
45 private static final int PORT = 45678;
46 private static final InetSocketAddress CLIENT_REMOTE = new InetSocketAddress("127.0.0.10", PORT);
47 private static final InetSocketAddress SERVER = new InetSocketAddress("0.0.0.0", PORT);
49 private BmpDispatcher dispatcher;
50 private BGPActivator bgpActivator;
51 private BmpActivator bmpActivator;
54 private BmpSession mockedSession;
56 private BmpSessionListenerFactory mockedListenerFactory;
58 private ChannelHandler mockedChannelHandler;
61 public void setUp() throws Exception {
62 MockitoAnnotations.initMocks(this);
63 Mockito.doNothing().when(this.mockedSession).handlerRemoved(Mockito.any(ChannelHandlerContext.class));
64 Mockito.doNothing().when(this.mockedSession).handlerAdded(Mockito.any(ChannelHandlerContext.class));
65 Mockito.doNothing().when(this.mockedSession).channelRegistered(Mockito.any(ChannelHandlerContext.class));
66 Mockito.doNothing().when(this.mockedSession).channelActive(Mockito.any(ChannelHandlerContext.class));
67 Mockito.doNothing().when(this.mockedSession).channelInactive(Mockito.any(ChannelHandlerContext.class));
68 Mockito.doNothing().when(this.mockedSession).channelUnregistered(Mockito.any(ChannelHandlerContext.class));
69 Mockito.doNothing().when(this.mockedSession).channelReadComplete(Mockito.any(ChannelHandlerContext.class));
71 this.bgpActivator = new BGPActivator();
72 final BGPExtensionProviderContext context = new SimpleBGPExtensionProviderContext();
73 this.bgpActivator.start(context);
74 final SimpleBmpExtensionProviderContext ctx = new SimpleBmpExtensionProviderContext();
75 this.bmpActivator = new BmpActivator(context);
76 this.bmpActivator.start(ctx);
77 final BmpMessageRegistry messageRegistry = ctx.getBmpMessageRegistry();
78 this.dispatcher = new BmpDispatcherImpl(new NioEventLoopGroup(), new NioEventLoopGroup(), messageRegistry, new BmpSessionFactory() {
80 public BmpSession getSession(final Channel channel,
81 final BmpSessionListenerFactory sessionListenerFactory) {
82 return BmpDispatcherImplTest.this.mockedSession;
84 }, Optional.<MD5ServerChannelFactory<?>>absent());
88 public void tearDown() throws Exception {
89 this.bgpActivator.close();
90 this.bmpActivator.close();
91 this.dispatcher.close();
95 public void testCreateServer() throws Exception {
96 final Channel serverChannel = this.dispatcher.createServer(SERVER, this.mockedListenerFactory, Optional.<KeyMapping>absent()).await().channel();
97 Assert.assertTrue(serverChannel.isActive());
98 final Channel clientChannel = connectTestClient();
99 Assert.assertTrue(clientChannel.isActive());
101 Mockito.verify(this.mockedSession, Mockito.times(1)).handlerAdded(Mockito.any(ChannelHandlerContext.class));
102 Mockito.verify(this.mockedSession, Mockito.times(1)).channelRegistered(Mockito.any(ChannelHandlerContext.class));
103 Mockito.verify(this.mockedSession, Mockito.times(1)).channelActive(Mockito.any(ChannelHandlerContext.class));
104 clientChannel.close().get();
105 serverChannel.close().get();
108 private Channel connectTestClient() throws InterruptedException {
109 final Bootstrap b = new Bootstrap();
110 b.group(new NioEventLoopGroup());
111 b.option(ChannelOption.SO_KEEPALIVE, true);
112 b.channel(NioSocketChannel.class);
113 b.handler(new ChannelInitializer<SocketChannel>() {
115 protected void initChannel(final SocketChannel ch) throws Exception {
119 return b.connect(CLIENT_REMOTE).sync().channel();