2 * Copyright (c) 2015 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.bgp.rib.impl;
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;
21 public class TestClientDispatcher {
23 private final BGPHandlerFactory hf;
24 private final InetSocketAddress defaulAddress;
25 private InetSocketAddress localAddress;
26 private final BGPDispatcherImpl disp;
28 protected TestClientDispatcher(final EventLoopGroup bossGroup, final EventLoopGroup workerGroup, final MessageRegistry messageRegistry,
29 final InetSocketAddress locaAddress) {
30 this.disp = new BGPDispatcherImpl(messageRegistry, bossGroup, workerGroup) {
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);
39 if (bootstrap.group() == null) {
40 bootstrap.group(workerGroup);
42 bootstrap.localAddress(locaAddress);
43 bootstrap.option(ChannelOption.SO_REUSEADDR, true);
47 this.hf = new BGPHandlerFactory(messageRegistry);
48 this.localAddress = locaAddress;
49 this.defaulAddress = locaAddress;
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);
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);
64 private synchronized void setLocalAddress(final Optional<InetSocketAddress> localAddress) {
65 if (localAddress.isPresent()) {
66 this.localAddress = localAddress.get();
68 this.localAddress = this.defaulAddress;