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 com.google.common.base.Preconditions;
11 import io.netty.bootstrap.Bootstrap;
12 import io.netty.bootstrap.ServerBootstrap;
13 import io.netty.channel.ChannelFuture;
14 import io.netty.channel.EventLoopGroup;
15 import io.netty.channel.socket.SocketChannel;
16 import io.netty.util.Timer;
17 import io.netty.util.concurrent.Future;
18 import io.netty.util.concurrent.Promise;
19 import java.net.InetSocketAddress;
20 import org.opendaylight.bgpcep.tcpmd5.KeyMapping;
21 import org.opendaylight.bgpcep.tcpmd5.netty.MD5ChannelFactory;
22 import org.opendaylight.bgpcep.tcpmd5.netty.MD5ChannelOption;
23 import org.opendaylight.bgpcep.tcpmd5.netty.MD5ServerChannelFactory;
24 import org.opendaylight.protocol.bgp.parser.BGPSessionListener;
25 import org.opendaylight.protocol.bgp.parser.spi.MessageRegistry;
26 import org.opendaylight.protocol.bgp.rib.impl.client.BGPClientSessionNegotiatorFactory;
27 import org.opendaylight.protocol.bgp.rib.impl.server.BGPServerSessionNegotiatorFactory;
28 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPDispatcher;
29 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry;
30 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPServerDispatcher;
31 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionValidator;
32 import org.opendaylight.protocol.framework.AbstractDispatcher;
33 import org.opendaylight.protocol.framework.ReconnectStrategy;
34 import org.opendaylight.protocol.framework.ReconnectStrategyFactory;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
38 * Implementation of BGPDispatcher.
40 public final class BGPDispatcherImpl extends AbstractDispatcher<BGPSessionImpl, BGPSessionListener> implements BGPDispatcher, BGPServerDispatcher, AutoCloseable {
41 private final MD5ServerChannelFactory<?> scf;
42 private final MD5ChannelFactory<?> cf;
43 private final BGPHandlerFactory hf;
44 private final Timer timer;
45 private KeyMapping keys;
47 public BGPDispatcherImpl(final MessageRegistry messageRegistry, final Timer timer, final EventLoopGroup bossGroup,
48 final EventLoopGroup workerGroup) {
49 this(messageRegistry, timer, bossGroup, workerGroup, null, null);
52 public BGPDispatcherImpl(final MessageRegistry messageRegistry, final Timer timer, final EventLoopGroup bossGroup,
53 final EventLoopGroup workerGroup, final MD5ChannelFactory<?> cf, final MD5ServerChannelFactory<?> scf) {
54 super(bossGroup, workerGroup);
55 this.timer = Preconditions.checkNotNull(timer);
56 this.hf = new BGPHandlerFactory(messageRegistry);
62 public synchronized Future<BGPSessionImpl> createClient(final InetSocketAddress address,
63 final AsNumber remoteAs, final BGPPeerRegistry listener, final ReconnectStrategy strategy) {
64 final BGPClientSessionNegotiatorFactory snf = new BGPClientSessionNegotiatorFactory(this.timer, remoteAs, listener);
65 return super.createClient(address, strategy, new PipelineInitializer<BGPSessionImpl>() {
67 public void initializeChannel(final SocketChannel ch, final Promise<BGPSessionImpl> promise) {
68 ch.pipeline().addLast(BGPDispatcherImpl.this.hf.getDecoders());
69 ch.pipeline().addLast("negotiator", snf.getSessionNegotiator(null, ch, promise));
70 ch.pipeline().addLast(BGPDispatcherImpl.this.hf.getEncoders());
76 public Future<Void> createReconnectingClient(final InetSocketAddress address,
77 final AsNumber remoteAs, final BGPPeerRegistry listener, final ReconnectStrategyFactory connectStrategyFactory,
78 final ReconnectStrategyFactory reestablishStrategyFactory) {
79 return this.createReconnectingClient(address, remoteAs, listener, connectStrategyFactory, reestablishStrategyFactory,
88 public synchronized Future<Void> createReconnectingClient(final InetSocketAddress address,
89 final AsNumber remoteAs, final BGPPeerRegistry peerRegistry, final ReconnectStrategyFactory connectStrategyFactory,
90 final ReconnectStrategyFactory reestablishStrategyFactory, final KeyMapping keys) {
91 final BGPClientSessionNegotiatorFactory snf = new BGPClientSessionNegotiatorFactory(this.timer, remoteAs, peerRegistry);
94 final Future<Void> ret = super.createReconnectingClient(address, connectStrategyFactory,
95 reestablishStrategyFactory.createReconnectStrategy(), new PipelineInitializer<BGPSessionImpl>() {
97 public void initializeChannel(final SocketChannel ch, final Promise<BGPSessionImpl> promise) {
98 ch.pipeline().addLast(BGPDispatcherImpl.this.hf.getDecoders());
99 ch.pipeline().addLast("negotiator", snf.getSessionNegotiator(null, ch, promise));
100 ch.pipeline().addLast(BGPDispatcherImpl.this.hf.getEncoders());
109 public ChannelFuture createServer(final BGPPeerRegistry registry, final InetSocketAddress address, final BGPSessionValidator sessionValidator) {
110 return this.createServer(registry, address, sessionValidator, null);
114 public ChannelFuture createServer(final BGPPeerRegistry registry, final InetSocketAddress address, final BGPSessionValidator sessionValidator, final KeyMapping keys) {
115 final BGPServerSessionNegotiatorFactory snf = new BGPServerSessionNegotiatorFactory(this.timer, sessionValidator, registry);
118 final ChannelFuture ret = super.createServer(address, new PipelineInitializer<BGPSessionImpl>() {
120 public void initializeChannel(final SocketChannel ch, final Promise<BGPSessionImpl> promise) {
121 ch.pipeline().addLast(BGPDispatcherImpl.this.hf.getDecoders());
122 ch.pipeline().addLast("negotiator", snf.getSessionNegotiator(null, ch, promise));
123 ch.pipeline().addLast(BGPDispatcherImpl.this.hf.getEncoders());
132 protected void customizeBootstrap(final Bootstrap b) {
133 if (keys != null && !keys.isEmpty()) {
135 throw new UnsupportedOperationException("No key access instance available, cannot use key mapping");
137 b.channelFactory(cf);
138 b.option(MD5ChannelOption.TCP_MD5SIG, keys);
143 protected void customizeBootstrap(final ServerBootstrap b) {
144 if (keys != null && !keys.isEmpty()) {
146 throw new UnsupportedOperationException("No key access instance available, cannot use key mapping");
148 b.channelFactory(scf);
149 b.option(MD5ChannelOption.TCP_MD5SIG, keys);