0e294f0815fe383c3d3df73cb81c434efb6f1860
[bgpcep.git] / bgp / rib-impl / src / test / java / org / opendaylight / protocol / bgp / rib / impl / TestClientDispatcher.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.bgp.rib.impl;
10
11 import com.google.common.base.Optional;
12 import io.netty.bootstrap.Bootstrap;
13 import io.netty.channel.ChannelOption;
14 import io.netty.channel.EventLoopGroup;
15 import io.netty.channel.epoll.Epoll;
16 import io.netty.channel.epoll.EpollSocketChannel;
17 import io.netty.channel.socket.nio.NioSocketChannel;
18 import io.netty.util.concurrent.Future;
19 import java.net.InetSocketAddress;
20 import org.opendaylight.protocol.bgp.parser.spi.MessageRegistry;
21 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry;
22 import org.opendaylight.protocol.concepts.KeyMapping;
23
24 public class TestClientDispatcher {
25
26     private final BGPHandlerFactory hf;
27     private final InetSocketAddress defaulAddress;
28     private InetSocketAddress localAddress;
29     private final BGPDispatcherImpl disp;
30
31     protected TestClientDispatcher(final EventLoopGroup bossGroup, final EventLoopGroup workerGroup, final MessageRegistry messageRegistry,
32             final InetSocketAddress locaAddress) {
33         this.disp = new BGPDispatcherImpl(messageRegistry, bossGroup, workerGroup) {
34             @Override
35             protected Bootstrap createClientBootStrap(final Optional<KeyMapping> keys, final EventLoopGroup workerGroup) {
36                 final Bootstrap bootstrap = new Bootstrap();
37                 if (Epoll.isAvailable()) {
38                     bootstrap.channel(EpollSocketChannel.class);
39                 } else {
40                     bootstrap.channel(NioSocketChannel.class);
41                 }
42                 // Make sure we are doing round-robin processing
43                 bootstrap.option(ChannelOption.MAX_MESSAGES_PER_READ, 1);
44                 bootstrap.option(ChannelOption.SO_KEEPALIVE, Boolean.TRUE);
45
46                 if (bootstrap.group() == null) {
47                     bootstrap.group(workerGroup);
48                 }
49                 bootstrap.localAddress(locaAddress);
50                 bootstrap.option(ChannelOption.SO_REUSEADDR, true);
51                 return bootstrap;
52             }
53         };
54         this.hf = new BGPHandlerFactory(messageRegistry);
55         this.localAddress = locaAddress;
56         this.defaulAddress = locaAddress;
57     }
58
59     public synchronized Future<BGPSessionImpl> createClient(final InetSocketAddress remoteAddress,
60             final BGPPeerRegistry listener, final int retryTimer, final Optional<InetSocketAddress> localAddress) {
61         setLocalAddress(localAddress);
62         return this.disp.createClient(remoteAddress, listener, retryTimer);
63     }
64
65     public synchronized Future<Void> createReconnectingClient(final InetSocketAddress address, final BGPPeerRegistry peerRegistry,
66             final int retryTimer, final Optional<InetSocketAddress> localAddress) {
67         setLocalAddress(localAddress);
68         return this.disp.createReconnectingClient(address, peerRegistry, retryTimer, Optional.<KeyMapping>absent());
69     }
70
71     private synchronized void setLocalAddress(final Optional<InetSocketAddress> localAddress) {
72         if (localAddress.isPresent()) {
73             this.localAddress = localAddress.get();
74         } else {
75             this.localAddress = this.defaulAddress;
76         }
77     }
78 }