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;
12 import io.netty.bootstrap.Bootstrap;
13 import io.netty.bootstrap.ServerBootstrap;
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;
20 import java.net.InetSocketAddress;
22 import org.opendaylight.bgpcep.tcpmd5.KeyMapping;
23 import org.opendaylight.bgpcep.tcpmd5.netty.MD5ChannelFactory;
24 import org.opendaylight.bgpcep.tcpmd5.netty.MD5ChannelOption;
25 import org.opendaylight.bgpcep.tcpmd5.netty.MD5ServerChannelFactory;
26 import org.opendaylight.protocol.bgp.parser.BGPSessionListener;
27 import org.opendaylight.protocol.bgp.parser.spi.MessageRegistry;
28 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPDispatcher;
29 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
30 import org.opendaylight.protocol.framework.AbstractDispatcher;
31 import org.opendaylight.protocol.framework.ReconnectStrategy;
32 import org.opendaylight.protocol.framework.ReconnectStrategyFactory;
33 import org.opendaylight.protocol.framework.SessionListenerFactory;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
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,
47 final EventLoopGroup workerGroup) {
48 this(messageRegistry, timer, bossGroup, workerGroup, null, null);
51 public BGPDispatcherImpl(final MessageRegistry messageRegistry, final Timer timer, final EventLoopGroup bossGroup,
52 final EventLoopGroup workerGroup, final MD5ChannelFactory<?> cf, final MD5ServerChannelFactory<?> scf) {
53 super(bossGroup, workerGroup);
54 this.timer = Preconditions.checkNotNull(timer);
55 this.hf = new BGPHandlerFactory(messageRegistry);
61 public synchronized Future<BGPSessionImpl> createClient(final InetSocketAddress address, final BGPSessionPreferences preferences,
62 final AsNumber remoteAs, final BGPSessionListener listener, final ReconnectStrategy strategy) {
63 final BGPSessionNegotiatorFactory snf = new BGPSessionNegotiatorFactory(this.timer, preferences, remoteAs);
64 final SessionListenerFactory<BGPSessionListener> slf = new SessionListenerFactory<BGPSessionListener>() {
66 public BGPSessionListener getSessionListener() {
70 return super.createClient(address, strategy, new PipelineInitializer<BGPSessionImpl>() {
72 public void initializeChannel(final SocketChannel ch, final Promise<BGPSessionImpl> promise) {
73 ch.pipeline().addLast(BGPDispatcherImpl.this.hf.getDecoders());
74 ch.pipeline().addLast("negotiator", snf.getSessionNegotiator(slf, ch, promise));
75 ch.pipeline().addLast(BGPDispatcherImpl.this.hf.getEncoders());
81 public Future<Void> createReconnectingClient(final InetSocketAddress address, final BGPSessionPreferences preferences,
82 final AsNumber remoteAs, final BGPSessionListener listener, final ReconnectStrategyFactory connectStrategyFactory,
83 final ReconnectStrategyFactory reestablishStrategyFactory) {
84 return this.createReconnectingClient(address, preferences, remoteAs, listener, connectStrategyFactory, reestablishStrategyFactory,
93 public synchronized Future<Void> createReconnectingClient(final InetSocketAddress address, final BGPSessionPreferences preferences,
94 final AsNumber remoteAs, final BGPSessionListener listener, 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,
106 reestablishStrategyFactory.createReconnectStrategy(), new PipelineInitializer<BGPSessionImpl>() {
108 public void initializeChannel(final SocketChannel ch, final Promise<BGPSessionImpl> promise) {
109 ch.pipeline().addLast(BGPDispatcherImpl.this.hf.getDecoders());
110 ch.pipeline().addLast("negotiator", snf.getSessionNegotiator(slf, ch, promise));
111 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);