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.util.InetSocketAddressUtil;
35 import org.opendaylight.yangtools.yang.binding.Notification;
37 public class BmpMockTest {
39 private final BmpSessionListener sessionListener = Mockito.mock(BmpSessionListener.class);
40 private BmpExtensionProviderActivator bmpActivator;
41 private BmpDispatcher bmpDispatcher;
45 final BmpExtensionProviderContext ctx = new SimpleBmpExtensionProviderContext();
46 this.bmpActivator = new BmpActivator(
47 ServiceLoaderBGPExtensionProviderContext.getSingletonInstance());
48 this.bmpActivator.start(ctx);
49 this.bmpDispatcher = new BmpDispatcherImpl(new NioEventLoopGroup(), new NioEventLoopGroup(), ctx.getBmpMessageRegistry(),
50 new DefaultBmpSessionFactory());
54 public void tearDown() throws Exception {
55 this.bmpActivator.stop();
56 this.bmpDispatcher.close();
60 public void testMain() throws Exception {
61 final InetSocketAddress serverAddr = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress();
62 final BmpSessionListenerFactory bmpSessionListenerFactory = () -> BmpMockTest.this.sessionListener;
63 final ChannelFuture futureServer = this.bmpDispatcher.createServer(serverAddr,
64 bmpSessionListenerFactory, Optional.absent());
65 waitFutureComplete(futureServer);
66 Channel serverChannel;
68 if (futureServer.isSuccess()) {
69 serverChannel = futureServer.channel();
73 // wait longer for the reconnection attempt
77 BmpMock.main(new String[]{"--remote_address", InetSocketAddressUtil.toHostAndPort(serverAddr).toString(), "--peers_count", "3", "--pre_policy_routes", "3"});
79 Mockito.verify(this.sessionListener, Mockito.timeout(TimeUnit.SECONDS.toMillis(sessionUpWait))).onSessionUp(Mockito.any(BmpSession.class));
80 //1 * Initiate message + 3 * PeerUp Notification + 9 * Route Monitoring message
81 Mockito.verify(this.sessionListener, Mockito.timeout(TimeUnit.SECONDS.toMillis(10)).times(13)).onMessage(Mockito.any(Notification.class));
83 if (serverChannel != null) {
84 serverChannel.close().sync();
89 public void testMainInPassiveMode() throws Exception {
90 final InetSocketAddress serverAddr = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress();
91 final BmpSessionListenerFactory bmpSessionListenerFactory = () -> BmpMockTest.this.sessionListener;
93 // create a local server in passive mode instead
94 BmpMock.main(new String[]{"--local_address", InetSocketAddressUtil.toHostAndPort(serverAddr).toString(),
95 "--peers_count", "3", "--pre_policy_routes", "3", "--passive"});
96 final ChannelFuture futureServer = this.bmpDispatcher.createClient(serverAddr,
97 bmpSessionListenerFactory, Optional.absent());
98 waitFutureComplete(futureServer);
99 Channel serverChannel;
101 if (futureServer.isSuccess()) {
102 serverChannel = futureServer.channel();
105 serverChannel = null;
106 // wait longer for the reconnection attempt
110 Mockito.verify(this.sessionListener, Mockito.timeout(TimeUnit.SECONDS.toMillis(sessionUpWait))).onSessionUp(Mockito.any(BmpSession.class));
111 //1 * Initiate message + 3 * PeerUp Notification + 9 * Route Monitoring message
112 Mockito.verify(this.sessionListener, Mockito.timeout(TimeUnit.SECONDS.toMillis(10)).times(13)).onMessage(Mockito.any(Notification.class));
114 if (serverChannel != null) {
115 serverChannel.close().sync();
119 static void waitFutureComplete(final ChannelFuture future) throws InterruptedException {
120 final CountDownLatch latch = new CountDownLatch(1);
121 future.addListener(future1 -> latch.countDown());
122 Uninterruptibles.awaitUninterruptibly(latch, 10, TimeUnit.SECONDS);