BUG-635: implement MD5 auth option for BGP peers
[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 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;
17
18 import java.net.InetSocketAddress;
19
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;
33
34 import com.google.common.base.Preconditions;
35
36 /**
37  * Implementation of BGPDispatcher.
38  */
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;
45
46         public BGPDispatcherImpl(final MessageRegistry messageRegistry, final Timer timer, final EventLoopGroup bossGroup, final EventLoopGroup workerGroup) {
47                 this(messageRegistry, timer, bossGroup, workerGroup, null, null);
48         }
49
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);
54                 this.cf = cf;
55                 this.scf = scf;
56         }
57
58         @Override
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>() {
63                         @Override
64                         public BGPSessionListener getSessionListener() {
65                                 return listener;
66                         }
67                 };
68                 return super.createClient(address, strategy, new PipelineInitializer<BGPSessionImpl>() {
69                         @Override
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());
74                         }
75                 });
76         }
77
78         @Override
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);
84         }
85
86         @Override
87         public void close() {
88         }
89
90         @Override
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>() {
98                         @Override
99                         public BGPSessionListener getSessionListener() {
100                                 return listener;
101                         }
102                 };
103
104                 this.keys = keys;
105                 final Future<Void> ret = super.createReconnectingClient(address, connectStrategyFactory, reestablishStrategyFactory.createReconnectStrategy(), new PipelineInitializer<BGPSessionImpl>() {
106                         @Override
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());
111                         }
112                 });
113                 this.keys = null;
114
115                 return ret;
116         }
117
118
119         @Override
120         protected void customizeBootstrap(final Bootstrap b) {
121                 if (keys != null && !keys.isEmpty()) {
122                         if (cf == null) {
123                                 throw new UnsupportedOperationException("No key access instance available, cannot use key mapping");
124                         }
125                         b.channelFactory(cf);
126                         b.option(MD5ChannelOption.TCP_MD5SIG, keys);
127                 }
128         }
129
130         @Override
131         protected void customizeBootstrap(final ServerBootstrap b) {
132                 if (keys != null && !keys.isEmpty()) {
133                         if (scf == null) {
134                                 throw new UnsupportedOperationException("No key access instance available, cannot use key mapping");
135                         }
136                         b.channelFactory(scf);
137                         b.option(MD5ChannelOption.TCP_MD5SIG, keys);
138                 }
139         }
140
141 }