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.EventLoopGroup;
14 import io.netty.channel.socket.SocketChannel;
15 import io.netty.util.concurrent.Future;
16 import io.netty.util.concurrent.Promise;
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 import org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener;
21 import org.opendaylight.protocol.framework.AbstractDispatcher;
22 import org.opendaylight.protocol.framework.ReconnectStrategy;
23 import org.opendaylight.protocol.framework.ReconnectStrategyFactory;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
26 public class TestClientDispatcher extends AbstractDispatcher<BGPSessionImpl, BGPSessionListener> {
28 private static final String NEGOTIATOR = "negotiator";
30 private final BGPHandlerFactory hf;
31 private InetSocketAddress localAddress;
32 private final InetSocketAddress defaulAddress;
34 protected TestClientDispatcher(final EventLoopGroup bossGroup, final EventLoopGroup workerGroup, final MessageRegistry messageRegistry,
35 final InetSocketAddress locaAddress) {
36 super(bossGroup, workerGroup);
37 this.hf = new BGPHandlerFactory(messageRegistry);
38 this.localAddress = locaAddress;
39 this.defaulAddress = locaAddress;
42 public synchronized Future<BGPSessionImpl> createClient(final InetSocketAddress remoteAddress,
43 final AsNumber remoteAs, final BGPPeerRegistry listener, final ReconnectStrategy strategy, final Optional<InetSocketAddress> localAddress) {
44 setLocalAddress(localAddress);
45 final BGPClientSessionNegotiatorFactory snf = new BGPClientSessionNegotiatorFactory(remoteAs, listener);
46 return super.createClient(remoteAddress, strategy, new PipelineInitializer<BGPSessionImpl>() {
49 public void initializeChannel(SocketChannel ch, Promise<BGPSessionImpl> promise) {
50 ch.pipeline().addLast(TestClientDispatcher.this.hf.getDecoders());
51 ch.pipeline().addLast(NEGOTIATOR, snf.getSessionNegotiator(null, ch, promise));
52 ch.pipeline().addLast(TestClientDispatcher.this.hf.getEncoders());
57 public synchronized Future<Void> createReconnectingClient(final InetSocketAddress address,
58 final AsNumber remoteAs, final BGPPeerRegistry peerRegistry, final ReconnectStrategyFactory reconnectStrategyFactory,
59 final Optional<InetSocketAddress> localAddress) {
60 setLocalAddress(localAddress);
61 final BGPClientSessionNegotiatorFactory snf = new BGPClientSessionNegotiatorFactory(remoteAs, peerRegistry);
62 final Future<Void> ret = super.createReconnectingClient(address, reconnectStrategyFactory, new PipelineInitializer<BGPSessionImpl>() {
64 public void initializeChannel(final SocketChannel ch, final Promise<BGPSessionImpl> promise) {
65 ch.pipeline().addLast(TestClientDispatcher.this.hf.getDecoders());
66 ch.pipeline().addLast(NEGOTIATOR, snf.getSessionNegotiator(null, ch, promise));
67 ch.pipeline().addLast(TestClientDispatcher.this.hf.getEncoders());
75 protected void customizeBootstrap(Bootstrap b) {
76 b.localAddress(this.localAddress);
77 super.customizeBootstrap(b);
80 private synchronized void setLocalAddress(final Optional<InetSocketAddress> localAddress) {
81 if (localAddress.isPresent()) {
82 this.localAddress = localAddress.get();
84 this.localAddress = defaulAddress;