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.spi.MessageRegistry;
21 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPDispatcher;
22 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry;
23 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionValidator;
24 import org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener;
25 import org.opendaylight.protocol.framework.AbstractDispatcher;
26 import org.opendaylight.protocol.framework.ReconnectStrategy;
27 import org.opendaylight.protocol.framework.ReconnectStrategyFactory;
28 import org.opendaylight.tcpmd5.api.KeyMapping;
29 import org.opendaylight.tcpmd5.netty.MD5ChannelFactory;
30 import org.opendaylight.tcpmd5.netty.MD5ChannelOption;
31 import org.opendaylight.tcpmd5.netty.MD5ServerChannelFactory;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
35 * Implementation of BGPDispatcher.
37 public final class BGPDispatcherImpl extends AbstractDispatcher<BGPSessionImpl, BGPSessionListener> implements BGPDispatcher, AutoCloseable {
38 private final MD5ServerChannelFactory<?> scf;
39 private final MD5ChannelFactory<?> cf;
40 private final BGPHandlerFactory hf;
41 private KeyMapping keys;
43 public BGPDispatcherImpl(final MessageRegistry messageRegistry, final EventLoopGroup bossGroup, final EventLoopGroup workerGroup) {
44 this(messageRegistry, bossGroup, workerGroup, null, null);
47 public BGPDispatcherImpl(final MessageRegistry messageRegistry, final EventLoopGroup bossGroup, final EventLoopGroup workerGroup, final MD5ChannelFactory<?> cf, final MD5ServerChannelFactory<?> scf) {
48 super(bossGroup, workerGroup);
49 this.hf = new BGPHandlerFactory(messageRegistry);
55 public synchronized Future<BGPSessionImpl> createClient(final InetSocketAddress address,
56 final AsNumber remoteAs, final BGPPeerRegistry listener, final ReconnectStrategy strategy) {
57 final BGPClientSessionNegotiatorFactory snf = new BGPClientSessionNegotiatorFactory(remoteAs, listener);
58 return super.createClient(address, strategy, new PipelineInitializer<BGPSessionImpl>() {
60 public void initializeChannel(final SocketChannel ch, final Promise<BGPSessionImpl> promise) {
61 ch.pipeline().addLast(BGPDispatcherImpl.this.hf.getDecoders());
62 ch.pipeline().addLast("negotiator", snf.getSessionNegotiator(null, ch, promise));
63 ch.pipeline().addLast(BGPDispatcherImpl.this.hf.getEncoders());
69 public Future<Void> createReconnectingClient(final InetSocketAddress address,
70 final AsNumber remoteAs, final BGPPeerRegistry listener, final ReconnectStrategyFactory connectStrategyFactory,
71 final ReconnectStrategyFactory reestablishStrategyFactory) {
72 return this.createReconnectingClient(address, remoteAs, listener, connectStrategyFactory, reestablishStrategyFactory,
81 public synchronized Future<Void> createReconnectingClient(final InetSocketAddress address,
82 final AsNumber remoteAs, final BGPPeerRegistry peerRegistry, final ReconnectStrategyFactory connectStrategyFactory,
83 final ReconnectStrategyFactory reestablishStrategyFactory, final KeyMapping keys) {
84 final BGPClientSessionNegotiatorFactory snf = new BGPClientSessionNegotiatorFactory(remoteAs, peerRegistry);
87 final Future<Void> ret = super.createReconnectingClient(address, connectStrategyFactory, reestablishStrategyFactory.createReconnectStrategy(), new PipelineInitializer<BGPSessionImpl>() {
89 public void initializeChannel(final SocketChannel ch, final Promise<BGPSessionImpl> promise) {
90 ch.pipeline().addLast(BGPDispatcherImpl.this.hf.getDecoders());
91 ch.pipeline().addLast("negotiator", snf.getSessionNegotiator(null, ch, promise));
92 ch.pipeline().addLast(BGPDispatcherImpl.this.hf.getEncoders());
101 public ChannelFuture createServer(final BGPPeerRegistry registry, final InetSocketAddress address, final BGPSessionValidator sessionValidator) {
102 return this.createServer(registry, address, sessionValidator, null);
106 public ChannelFuture createServer(final BGPPeerRegistry registry, final InetSocketAddress address, final BGPSessionValidator sessionValidator, final KeyMapping keys) {
107 final BGPServerSessionNegotiatorFactory snf = new BGPServerSessionNegotiatorFactory(sessionValidator, registry);
110 final ChannelFuture ret = super.createServer(address, new PipelineInitializer<BGPSessionImpl>() {
112 public void initializeChannel(final SocketChannel ch, final Promise<BGPSessionImpl> promise) {
113 ch.pipeline().addLast(BGPDispatcherImpl.this.hf.getDecoders());
114 ch.pipeline().addLast("negotiator", snf.getSessionNegotiator(null, ch, promise));
115 ch.pipeline().addLast(BGPDispatcherImpl.this.hf.getEncoders());
124 protected void customizeBootstrap(final Bootstrap b) {
125 if (this.keys != null && !this.keys.isEmpty()) {
126 if (this.cf == null) {
127 throw new UnsupportedOperationException("No key access instance available, cannot use key mapping");
129 b.channelFactory(this.cf);
130 b.option(MD5ChannelOption.TCP_MD5SIG, this.keys);
135 protected void customizeBootstrap(final ServerBootstrap b) {
136 if (this.keys != null && !this.keys.isEmpty()) {
137 if (this.scf == null) {
138 throw new UnsupportedOperationException("No key access instance available, cannot use key mapping");
140 b.channelFactory(this.scf);
141 b.option(MD5ChannelOption.TCP_MD5SIG, this.keys);