BUG-338 Allow incomming BGP connections.
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / BGPDispatcherImpl.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.bgp.rib.impl;
9
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;
36
37 /**
38  * Implementation of BGPDispatcher.
39  */
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;
46
47     public BGPDispatcherImpl(final MessageRegistry messageRegistry, final Timer timer, final EventLoopGroup bossGroup,
48             final EventLoopGroup workerGroup) {
49         this(messageRegistry, timer, bossGroup, workerGroup, null, null);
50     }
51
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);
57         this.cf = cf;
58         this.scf = scf;
59     }
60
61     @Override
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>() {
66             @Override
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());
71             }
72         });
73     }
74
75     @Override
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,
80                 null);
81     }
82
83     @Override
84     public void close() {
85     }
86
87     @Override
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);
92
93         this.keys = keys;
94         final Future<Void> ret = super.createReconnectingClient(address, connectStrategyFactory,
95                 reestablishStrategyFactory.createReconnectStrategy(), new PipelineInitializer<BGPSessionImpl>() {
96                     @Override
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());
101                     }
102                 });
103         this.keys = null;
104
105         return ret;
106     }
107
108     @Override
109     public ChannelFuture createServer(final BGPPeerRegistry registry, final InetSocketAddress address, final BGPSessionValidator sessionValidator) {
110         return this.createServer(registry, address, sessionValidator, null);
111     }
112
113     @Override
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);
116
117         this.keys = keys;
118         final ChannelFuture ret = super.createServer(address, new PipelineInitializer<BGPSessionImpl>() {
119             @Override
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());
124             }
125         });
126         this.keys = null;
127
128         return ret;
129     }
130
131     @Override
132     protected void customizeBootstrap(final Bootstrap b) {
133         if (keys != null && !keys.isEmpty()) {
134             if (cf == null) {
135                 throw new UnsupportedOperationException("No key access instance available, cannot use key mapping");
136             }
137             b.channelFactory(cf);
138             b.option(MD5ChannelOption.TCP_MD5SIG, keys);
139         }
140     }
141
142     @Override
143     protected void customizeBootstrap(final ServerBootstrap b) {
144         if (keys != null && !keys.isEmpty()) {
145             if (scf == null) {
146                 throw new UnsupportedOperationException("No key access instance available, cannot use key mapping");
147             }
148             b.channelFactory(scf);
149             b.option(MD5ChannelOption.TCP_MD5SIG, keys);
150         }
151     }
152
153 }