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;
17 import java.net.InetSocketAddress;
18 import org.opendaylight.protocol.bgp.parser.BGPSessionListener;
19 import org.opendaylight.protocol.bgp.parser.spi.MessageRegistry;
20 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPDispatcher;
21 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry;
22 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionValidator;
23 import org.opendaylight.protocol.framework.AbstractDispatcher;
24 import org.opendaylight.protocol.framework.ReconnectStrategy;
25 import org.opendaylight.protocol.framework.ReconnectStrategyFactory;
26 import org.opendaylight.tcpmd5.api.KeyMapping;
27 import org.opendaylight.tcpmd5.netty.MD5ChannelFactory;
28 import org.opendaylight.tcpmd5.netty.MD5ChannelOption;
29 import org.opendaylight.tcpmd5.netty.MD5ServerChannelFactory;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
33 * Implementation of BGPDispatcher.
35 public final class BGPDispatcherImpl extends AbstractDispatcher<BGPSessionImpl, BGPSessionListener> implements BGPDispatcher, AutoCloseable {
36 private final MD5ServerChannelFactory<?> scf;
37 private final MD5ChannelFactory<?> cf;
38 private final BGPHandlerFactory hf;
39 private KeyMapping keys;
41 public BGPDispatcherImpl(final MessageRegistry messageRegistry, final EventLoopGroup bossGroup, final EventLoopGroup workerGroup) {
42 this(messageRegistry, bossGroup, workerGroup, null, null);
45 public BGPDispatcherImpl(final MessageRegistry messageRegistry, final EventLoopGroup bossGroup, final EventLoopGroup workerGroup, final MD5ChannelFactory<?> cf, final MD5ServerChannelFactory<?> scf) {
46 super(bossGroup, workerGroup);
47 this.hf = new BGPHandlerFactory(messageRegistry);
53 public synchronized Future<BGPSessionImpl> createClient(final InetSocketAddress address,
54 final AsNumber remoteAs, final BGPPeerRegistry listener, final ReconnectStrategy strategy) {
55 final BGPClientSessionNegotiatorFactory snf = new BGPClientSessionNegotiatorFactory(remoteAs, listener);
56 return super.createClient(address, strategy, new PipelineInitializer<BGPSessionImpl>() {
58 public void initializeChannel(final SocketChannel ch, final Promise<BGPSessionImpl> promise) {
59 ch.pipeline().addLast(BGPDispatcherImpl.this.hf.getDecoders());
60 ch.pipeline().addLast("negotiator", snf.getSessionNegotiator(null, ch, promise));
61 ch.pipeline().addLast(BGPDispatcherImpl.this.hf.getEncoders());
67 public Future<Void> createReconnectingClient(final InetSocketAddress address,
68 final AsNumber remoteAs, final BGPPeerRegistry listener, final ReconnectStrategyFactory connectStrategyFactory,
69 final ReconnectStrategyFactory reestablishStrategyFactory) {
70 return this.createReconnectingClient(address, remoteAs, listener, connectStrategyFactory, reestablishStrategyFactory,
79 public synchronized Future<Void> createReconnectingClient(final InetSocketAddress address,
80 final AsNumber remoteAs, final BGPPeerRegistry peerRegistry, final ReconnectStrategyFactory connectStrategyFactory,
81 final ReconnectStrategyFactory reestablishStrategyFactory, final KeyMapping keys) {
82 final BGPClientSessionNegotiatorFactory snf = new BGPClientSessionNegotiatorFactory(remoteAs, peerRegistry);
85 final Future<Void> ret = super.createReconnectingClient(address, connectStrategyFactory, reestablishStrategyFactory.createReconnectStrategy(), new PipelineInitializer<BGPSessionImpl>() {
87 public void initializeChannel(final SocketChannel ch, final Promise<BGPSessionImpl> promise) {
88 ch.pipeline().addLast(BGPDispatcherImpl.this.hf.getDecoders());
89 ch.pipeline().addLast("negotiator", snf.getSessionNegotiator(null, ch, promise));
90 ch.pipeline().addLast(BGPDispatcherImpl.this.hf.getEncoders());
99 public ChannelFuture createServer(final BGPPeerRegistry registry, final InetSocketAddress address, final BGPSessionValidator sessionValidator) {
100 return this.createServer(registry, address, sessionValidator, null);
104 public ChannelFuture createServer(final BGPPeerRegistry registry, final InetSocketAddress address, final BGPSessionValidator sessionValidator, final KeyMapping keys) {
105 final BGPServerSessionNegotiatorFactory snf = new BGPServerSessionNegotiatorFactory(sessionValidator, registry);
108 final ChannelFuture ret = super.createServer(address, new PipelineInitializer<BGPSessionImpl>() {
110 public void initializeChannel(final SocketChannel ch, final Promise<BGPSessionImpl> promise) {
111 ch.pipeline().addLast(BGPDispatcherImpl.this.hf.getDecoders());
112 ch.pipeline().addLast("negotiator", snf.getSessionNegotiator(null, ch, promise));
113 ch.pipeline().addLast(BGPDispatcherImpl.this.hf.getEncoders());
122 protected void customizeBootstrap(final Bootstrap b) {
123 if (this.keys != null && !this.keys.isEmpty()) {
124 if (this.cf == null) {
125 throw new UnsupportedOperationException("No key access instance available, cannot use key mapping");
127 b.channelFactory(this.cf);
128 b.option(MD5ChannelOption.TCP_MD5SIG, this.keys);
133 protected void customizeBootstrap(final ServerBootstrap b) {
134 if (this.keys != null && !this.keys.isEmpty()) {
135 if (this.scf == null) {
136 throw new UnsupportedOperationException("No key access instance available, cannot use key mapping");
138 b.channelFactory(this.scf);
139 b.option(MD5ChannelOption.TCP_MD5SIG, this.keys);