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.EventLoopGroup;
13 import io.netty.channel.socket.SocketChannel;
14 import io.netty.util.Timer;
15 import io.netty.util.concurrent.Future;
16 import io.netty.util.concurrent.Promise;
18 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.spi.BGPDispatcher;
27 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
28 import org.opendaylight.protocol.framework.AbstractDispatcher;
29 import org.opendaylight.protocol.framework.ReconnectStrategy;
30 import org.opendaylight.protocol.framework.ReconnectStrategyFactory;
31 import org.opendaylight.protocol.framework.SessionListenerFactory;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
34 import com.google.common.base.Preconditions;
37 * Implementation of BGPDispatcher.
39 public final class BGPDispatcherImpl extends AbstractDispatcher<BGPSessionImpl, BGPSessionListener> implements BGPDispatcher, AutoCloseable {
40 private final MD5ServerChannelFactory<?> scf;
41 private final MD5ChannelFactory<?> cf;
42 private final BGPHandlerFactory hf;
43 private final Timer timer;
44 private KeyMapping keys;
46 public BGPDispatcherImpl(final MessageRegistry messageRegistry, final Timer timer, final EventLoopGroup bossGroup, final EventLoopGroup workerGroup) {
47 this(messageRegistry, timer, bossGroup, workerGroup, null, null);
50 public BGPDispatcherImpl(final MessageRegistry messageRegistry, final Timer timer, final EventLoopGroup bossGroup, final EventLoopGroup workerGroup, final MD5ChannelFactory<?> cf, final MD5ServerChannelFactory<?> scf) {
51 super(bossGroup, workerGroup);
52 this.timer = Preconditions.checkNotNull(timer);
53 this.hf = new BGPHandlerFactory(messageRegistry);
59 public synchronized Future<BGPSessionImpl> createClient(final InetSocketAddress address, final BGPSessionPreferences preferences,
60 final AsNumber remoteAs, final BGPSessionListener listener, final ReconnectStrategy strategy) {
61 final BGPSessionNegotiatorFactory snf = new BGPSessionNegotiatorFactory(this.timer, preferences, remoteAs);
62 final SessionListenerFactory<BGPSessionListener> slf = new SessionListenerFactory<BGPSessionListener>() {
64 public BGPSessionListener getSessionListener() {
68 return super.createClient(address, strategy, new PipelineInitializer<BGPSessionImpl>() {
70 public void initializeChannel(final SocketChannel ch, final Promise<BGPSessionImpl> promise) {
71 ch.pipeline().addLast(BGPDispatcherImpl.this.hf.getDecoders());
72 ch.pipeline().addLast("negotiator", snf.getSessionNegotiator(slf, ch, promise));
73 ch.pipeline().addLast(BGPDispatcherImpl.this.hf.getEncoders());
79 public Future<Void> createReconnectingClient(final InetSocketAddress address,
80 final BGPSessionPreferences preferences, final AsNumber remoteAs,
81 final BGPSessionListener listener, final ReconnectStrategyFactory connectStrategyFactory,
82 final ReconnectStrategyFactory reestablishStrategyFactory) {
83 return this.createReconnectingClient(address, preferences, remoteAs, listener, connectStrategyFactory, reestablishStrategyFactory, null);
91 public synchronized Future<Void> createReconnectingClient(final InetSocketAddress address,
92 final BGPSessionPreferences preferences, final AsNumber remoteAs,
93 final BGPSessionListener listener,
94 final ReconnectStrategyFactory connectStrategyFactory,
95 final ReconnectStrategyFactory reestablishStrategyFactory, final KeyMapping keys) {
96 final BGPSessionNegotiatorFactory snf = new BGPSessionNegotiatorFactory(this.timer, preferences, remoteAs);
97 final SessionListenerFactory<BGPSessionListener> slf = new SessionListenerFactory<BGPSessionListener>() {
99 public BGPSessionListener getSessionListener() {
105 final Future<Void> ret = super.createReconnectingClient(address, connectStrategyFactory, reestablishStrategyFactory.createReconnectStrategy(), new PipelineInitializer<BGPSessionImpl>() {
107 public void initializeChannel(final SocketChannel ch, final Promise<BGPSessionImpl> promise) {
108 ch.pipeline().addLast(BGPDispatcherImpl.this.hf.getDecoders());
109 ch.pipeline().addLast("negotiator", snf.getSessionNegotiator(slf, ch, promise));
110 ch.pipeline().addLast(BGPDispatcherImpl.this.hf.getEncoders());
120 protected void customizeBootstrap(final Bootstrap b) {
121 if (keys != null && !keys.isEmpty()) {
123 throw new UnsupportedOperationException("No key access instance available, cannot use key mapping");
125 b.channelFactory(cf);
126 b.option(MD5ChannelOption.TCP_MD5SIG, keys);
131 protected void customizeBootstrap(final ServerBootstrap b) {
132 if (keys != null && !keys.isEmpty()) {
134 throw new UnsupportedOperationException("No key access instance available, cannot use key mapping");
136 b.channelFactory(scf);
137 b.option(MD5ChannelOption.TCP_MD5SIG, keys);