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.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;
24 public class TestClientDispatcher {
26 private final BGPHandlerFactory hf;
27 private final InetSocketAddress defaultAddress;
28 private InetSocketAddress localAddress;
29 private final BGPDispatcherImpl disp;
31 protected TestClientDispatcher(final EventLoopGroup bossGroup, final EventLoopGroup workerGroup, final MessageRegistry messageRegistry,
32 final InetSocketAddress localAddress) {
33 this.disp = new BGPDispatcherImpl(messageRegistry, bossGroup, workerGroup) {
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);
40 bootstrap.channel(NioSocketChannel.class);
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);
46 if (bootstrap.group() == null) {
47 bootstrap.group(workerGroup);
49 bootstrap.localAddress(localAddress);
50 bootstrap.option(ChannelOption.SO_REUSEADDR, true);
54 this.hf = new BGPHandlerFactory(messageRegistry);
55 this.localAddress = localAddress;
56 this.defaultAddress = localAddress;
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);
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());
71 private synchronized void setLocalAddress(final Optional<InetSocketAddress> localAddress) {
72 if (localAddress.isPresent()) {
73 this.localAddress = localAddress.get();
75 this.localAddress = this.defaultAddress;