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