BUG-4328: Update BMP implementation according the draft version 15
[bgpcep.git] / bgp / bmp-impl / src / test / java / org / opendaylight / protocol / bmp / impl / session / BmpDispatcherImplTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.protocol.bmp.impl.session;
10
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;
42
43 public class BmpDispatcherImplTest {
44
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);
48
49     private BmpDispatcher dispatcher;
50     private BGPActivator bgpActivator;
51     private BmpActivator bmpActivator;
52
53     @Mock
54     private BmpSession mockedSession;
55     @Mock
56     private BmpSessionListenerFactory mockedListenerFactory;
57     @Mock
58     private ChannelHandler mockedChannelHandler;
59
60     @Before
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));
70
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() {
79             @Override
80             public BmpSession getSession(final Channel channel,
81                     final BmpSessionListenerFactory sessionListenerFactory) {
82                 return BmpDispatcherImplTest.this.mockedSession;
83             }
84         }, Optional.<MD5ServerChannelFactory<?>>absent());
85     }
86
87     @After
88     public void tearDown() throws Exception {
89         this.bgpActivator.close();
90         this.bmpActivator.close();
91         this.dispatcher.close();
92     }
93
94     @Test
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());
100         Thread.sleep(500);
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();
106     }
107
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>() {
114             @Override
115             protected void initChannel(final SocketChannel ch) throws Exception {
116                 //no initialization
117             }
118         });
119         return b.connect(CLIENT_REMOTE).sync().channel();
120     }
121 }