2 * Copyright (c) 2013 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
8 package org.opendaylight.protocol.bgp.rib.impl;
10 import io.netty.bootstrap.Bootstrap;
11 import io.netty.bootstrap.ServerBootstrap;
12 import io.netty.channel.ChannelFuture;
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;
18 import java.net.InetSocketAddress;
20 import org.opendaylight.protocol.bgp.parser.BGPSessionListener;
21 import org.opendaylight.protocol.bgp.parser.spi.MessageRegistry;
22 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPDispatcher;
23 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry;
24 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPServerDispatcher;
25 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionValidator;
26 import org.opendaylight.protocol.framework.AbstractDispatcher;
27 import org.opendaylight.protocol.framework.ReconnectStrategy;
28 import org.opendaylight.protocol.framework.ReconnectStrategyFactory;
29 import org.opendaylight.tcpmd5.api.KeyMapping;
30 import org.opendaylight.tcpmd5.netty.MD5ChannelFactory;
31 import org.opendaylight.tcpmd5.netty.MD5ChannelOption;
32 import org.opendaylight.tcpmd5.netty.MD5ServerChannelFactory;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
36 * Implementation of BGPDispatcher.
38 public final class BGPDispatcherImpl extends AbstractDispatcher<BGPSessionImpl, BGPSessionListener> implements BGPDispatcher, BGPServerDispatcher, AutoCloseable {
39 private final MD5ServerChannelFactory<?> scf;
40 private final MD5ChannelFactory<?> cf;
41 private final BGPHandlerFactory hf;
42 private KeyMapping keys;
44 public BGPDispatcherImpl(final MessageRegistry messageRegistry, final EventLoopGroup bossGroup, final EventLoopGroup workerGroup) {
45 this(messageRegistry, bossGroup, workerGroup, null, null);
48 public BGPDispatcherImpl(final MessageRegistry messageRegistry, final EventLoopGroup bossGroup, final EventLoopGroup workerGroup, final MD5ChannelFactory<?> cf, final MD5ServerChannelFactory<?> scf) {
49 super(bossGroup, workerGroup);
50 this.hf = new BGPHandlerFactory(messageRegistry);
56 public synchronized Future<BGPSessionImpl> createClient(final InetSocketAddress address,
57 final AsNumber remoteAs, final BGPPeerRegistry listener, final ReconnectStrategy strategy) {
58 final BGPClientSessionNegotiatorFactory snf = new BGPClientSessionNegotiatorFactory(remoteAs, listener);
59 return super.createClient(address, strategy, new PipelineInitializer<BGPSessionImpl>() {
61 public void initializeChannel(final SocketChannel ch, final Promise<BGPSessionImpl> promise) {
62 ch.pipeline().addLast(BGPDispatcherImpl.this.hf.getDecoders());
63 ch.pipeline().addLast("negotiator", snf.getSessionNegotiator(null, ch, promise));
64 ch.pipeline().addLast(BGPDispatcherImpl.this.hf.getEncoders());
70 public Future<Void> createReconnectingClient(final InetSocketAddress address,
71 final AsNumber remoteAs, final BGPPeerRegistry listener, final ReconnectStrategyFactory connectStrategyFactory,
72 final ReconnectStrategyFactory reestablishStrategyFactory) {
73 return this.createReconnectingClient(address, remoteAs, listener, connectStrategyFactory, reestablishStrategyFactory,
82 public synchronized Future<Void> createReconnectingClient(final InetSocketAddress address,
83 final AsNumber remoteAs, final BGPPeerRegistry peerRegistry, final ReconnectStrategyFactory connectStrategyFactory,
84 final ReconnectStrategyFactory reestablishStrategyFactory, final KeyMapping keys) {
85 final BGPClientSessionNegotiatorFactory snf = new BGPClientSessionNegotiatorFactory(remoteAs, peerRegistry);
88 final Future<Void> ret = super.createReconnectingClient(address, connectStrategyFactory, reestablishStrategyFactory.createReconnectStrategy(), new PipelineInitializer<BGPSessionImpl>() {
90 public void initializeChannel(final SocketChannel ch, final Promise<BGPSessionImpl> promise) {
91 ch.pipeline().addLast(BGPDispatcherImpl.this.hf.getDecoders());
92 ch.pipeline().addLast("negotiator", snf.getSessionNegotiator(null, ch, promise));
93 ch.pipeline().addLast(BGPDispatcherImpl.this.hf.getEncoders());
102 public ChannelFuture createServer(final BGPPeerRegistry registry, final InetSocketAddress address, final BGPSessionValidator sessionValidator) {
103 return this.createServer(registry, address, sessionValidator, null);
107 public ChannelFuture createServer(final BGPPeerRegistry registry, final InetSocketAddress address, final BGPSessionValidator sessionValidator, final KeyMapping keys) {
108 final BGPServerSessionNegotiatorFactory snf = new BGPServerSessionNegotiatorFactory(sessionValidator, registry);
111 final ChannelFuture ret = super.createServer(address, new PipelineInitializer<BGPSessionImpl>() {
113 public void initializeChannel(final SocketChannel ch, final Promise<BGPSessionImpl> promise) {
114 ch.pipeline().addLast(BGPDispatcherImpl.this.hf.getDecoders());
115 ch.pipeline().addLast("negotiator", snf.getSessionNegotiator(null, ch, promise));
116 ch.pipeline().addLast(BGPDispatcherImpl.this.hf.getEncoders());
125 protected void customizeBootstrap(final Bootstrap b) {
126 if (keys != null && !keys.isEmpty()) {
128 throw new UnsupportedOperationException("No key access instance available, cannot use key mapping");
130 b.channelFactory(cf);
131 b.option(MD5ChannelOption.TCP_MD5SIG, keys);
136 protected void customizeBootstrap(final ServerBootstrap b) {
137 if (keys != null && !keys.isEmpty()) {
139 throw new UnsupportedOperationException("No key access instance available, cannot use key mapping");
141 b.channelFactory(scf);
142 b.option(MD5ChannelOption.TCP_MD5SIG, keys);