2 * Copyright (c) 2016 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.mock;
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;
38 public class BmpMockTest {
40 private final BmpSessionListener sessionListener = Mockito.mock(BmpSessionListener.class);
41 private BmpExtensionProviderActivator bmpActivator;
42 private BmpDispatcher bmpDispatcher;
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());
55 public void tearDown() throws Exception {
56 this.bmpActivator.stop();
57 this.bmpDispatcher.close();
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;
69 if (futureServer.isSuccess()) {
70 serverChannel = futureServer.channel();
74 // wait longer for the reconnection attempt
78 BmpMock.main(new String[]{"--remote_address", InetSocketAddressUtil.toHostAndPort(serverAddr).toString(), "--peers_count", "3", "--pre_policy_routes", "3"});
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));
84 if (serverChannel != null) {
85 serverChannel.close().sync();
90 public void testMainInPassiveMode() throws Exception {
91 final InetSocketAddress serverAddr = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress();
92 final BmpSessionListenerFactory bmpSessionListenerFactory = () -> BmpMockTest.this.sessionListener;
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;
102 if (futureServer.isSuccess()) {
103 serverChannel = futureServer.channel();
106 serverChannel = null;
107 // wait longer for the reconnection attempt
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));
115 if (serverChannel != null) {
116 serverChannel.close().sync();
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);