388ed30c4099270b8898d26c3d34363fc81f6af3
[bgpcep.git] / bgp / bmp-mock / src / test / java / org / opendaylight / protocol / bmp / mock / BmpMockTest.java
1 /*
2  * Copyright (c) 2016 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.mock;
10
11 import com.google.common.base.Optional;
12 import com.google.common.util.concurrent.Uninterruptibles;
13 import io.netty.channel.Channel;
14 import io.netty.channel.ChannelFuture;
15 import io.netty.channel.nio.NioEventLoopGroup;
16 import java.net.InetSocketAddress;
17 import java.util.concurrent.CountDownLatch;
18 import java.util.concurrent.TimeUnit;
19 import org.junit.After;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.mockito.Mockito;
23 import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext;
24 import org.opendaylight.protocol.bmp.api.BmpDispatcher;
25 import org.opendaylight.protocol.bmp.api.BmpSession;
26 import org.opendaylight.protocol.bmp.api.BmpSessionListener;
27 import org.opendaylight.protocol.bmp.api.BmpSessionListenerFactory;
28 import org.opendaylight.protocol.bmp.impl.BmpDispatcherImpl;
29 import org.opendaylight.protocol.bmp.impl.session.DefaultBmpSessionFactory;
30 import org.opendaylight.protocol.bmp.parser.BmpActivator;
31 import org.opendaylight.protocol.bmp.spi.registry.BmpExtensionProviderActivator;
32 import org.opendaylight.protocol.bmp.spi.registry.BmpExtensionProviderContext;
33 import org.opendaylight.protocol.bmp.spi.registry.SimpleBmpExtensionProviderContext;
34 import org.opendaylight.protocol.concepts.KeyMapping;
35 import org.opendaylight.protocol.util.InetSocketAddressUtil;
36 import org.opendaylight.yangtools.yang.binding.Notification;
37
38 public class BmpMockTest {
39
40     private final BmpSessionListener sessionListener = Mockito.mock(BmpSessionListener.class);
41     private BmpExtensionProviderActivator bmpActivator;
42     private BmpDispatcher bmpDispatcher;
43
44     @Before
45     public void setUp() {
46         final BmpExtensionProviderContext ctx = new SimpleBmpExtensionProviderContext();
47         this.bmpActivator = new BmpActivator(
48             ServiceLoaderBGPExtensionProviderContext.getSingletonInstance());
49         this.bmpActivator.start(ctx);
50         this.bmpDispatcher = new BmpDispatcherImpl(new NioEventLoopGroup(), new NioEventLoopGroup(), ctx.getBmpMessageRegistry(),
51             new DefaultBmpSessionFactory());
52     }
53
54     @After
55     public void tearDown() throws Exception {
56         this.bmpActivator.stop();
57         this.bmpDispatcher.close();
58     }
59
60     @Test
61     public void testMain() throws Exception {
62         final InetSocketAddress serverAddr = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress();
63         final BmpSessionListenerFactory bmpSessionListenerFactory = () -> BmpMockTest.this.sessionListener;
64         final ChannelFuture futureServer = bmpDispatcher.createServer(serverAddr,
65             bmpSessionListenerFactory, Optional.<KeyMapping>absent());
66         waitFutureComplete(futureServer);
67         Channel serverChannel;
68         int sessionUpWait;
69         if (futureServer.isSuccess()) {
70             serverChannel = futureServer.channel();
71             sessionUpWait = 10;
72         } else {
73             serverChannel = null;
74             // wait longer for the reconnection attempt
75             sessionUpWait = 40;
76         }
77
78         BmpMock.main(new String[]{"--remote_address", InetSocketAddressUtil.toHostAndPort(serverAddr).toString(), "--peers_count", "3", "--pre_policy_routes", "3"});
79
80         Mockito.verify(this.sessionListener, Mockito.timeout(TimeUnit.SECONDS.toMillis(sessionUpWait))).onSessionUp(Mockito.any(BmpSession.class));
81         //1 * Initiate message + 3 * PeerUp Notification + 9 * Route Monitoring message
82         Mockito.verify(this.sessionListener, Mockito.timeout(TimeUnit.SECONDS.toMillis(10)).times(13)).onMessage(Mockito.any(Notification.class));
83
84         if (serverChannel != null) {
85             serverChannel.close().sync();
86         }
87     }
88
89     @Test
90     public void testMainInPassiveMode() throws Exception {
91         final InetSocketAddress serverAddr = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress();
92         final BmpSessionListenerFactory bmpSessionListenerFactory = () -> BmpMockTest.this.sessionListener;
93
94         // create a local server in passive mode instead
95         BmpMock.main(new String[]{"--local_address", InetSocketAddressUtil.toHostAndPort(serverAddr).toString(),
96             "--peers_count", "3", "--pre_policy_routes", "3", "--passive"});
97         final ChannelFuture futureServer = bmpDispatcher.createClient(serverAddr,
98             bmpSessionListenerFactory, Optional.<KeyMapping>absent());
99         waitFutureComplete(futureServer);
100         Channel serverChannel;
101         int sessionUpWait;
102         if (futureServer.isSuccess()) {
103             serverChannel = futureServer.channel();
104             sessionUpWait = 10;
105         } else {
106             serverChannel = null;
107             // wait longer for the reconnection attempt
108             sessionUpWait = 40;
109         }
110
111         Mockito.verify(this.sessionListener, Mockito.timeout(TimeUnit.SECONDS.toMillis(sessionUpWait))).onSessionUp(Mockito.any(BmpSession.class));
112         //1 * Initiate message + 3 * PeerUp Notification + 9 * Route Monitoring message
113         Mockito.verify(this.sessionListener, Mockito.timeout(TimeUnit.SECONDS.toMillis(10)).times(13)).onMessage(Mockito.any(Notification.class));
114
115         if (serverChannel != null) {
116             serverChannel.close().sync();
117         }
118     }
119
120     static void waitFutureComplete(final ChannelFuture future) throws InterruptedException {
121         final CountDownLatch latch = new CountDownLatch(1);
122         future.addListener(future1 -> latch.countDown());
123         Uninterruptibles.awaitUninterruptibly(latch, 10, TimeUnit.SECONDS);
124     }
125 }