Rework BGP timers to work with channel
[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.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
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.BGPPeerRegistry;
28 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPServerDispatcher;
29 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionValidator;
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.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
34
35 /**
36  * Implementation of BGPDispatcher.
37  */
38 public final class BGPDispatcherImpl extends AbstractDispatcher<BGPSessionImpl, BGPSessionListener> implements BGPDispatcher, BGPServerDispatcher, AutoCloseable {
39     private final MD5ServerChannelFactory<?> scf;
40     private final MD5ChannelFactory<?> cf;
41     private final BGPHandlerFactory hf;
42     private KeyMapping keys;
43
44     public BGPDispatcherImpl(final MessageRegistry messageRegistry, final EventLoopGroup bossGroup, final EventLoopGroup workerGroup) {
45         this(messageRegistry, bossGroup, workerGroup, null, null);
46     }
47
48     public BGPDispatcherImpl(final MessageRegistry messageRegistry, final EventLoopGroup bossGroup, final EventLoopGroup workerGroup, final MD5ChannelFactory<?> cf, final MD5ServerChannelFactory<?> scf) {
49         super(bossGroup, workerGroup);
50         this.hf = new BGPHandlerFactory(messageRegistry);
51         this.cf = cf;
52         this.scf = scf;
53     }
54
55     @Override
56     public synchronized Future<BGPSessionImpl> createClient(final InetSocketAddress address,
57             final AsNumber remoteAs, final BGPPeerRegistry listener, final ReconnectStrategy strategy) {
58         final BGPClientSessionNegotiatorFactory snf = new BGPClientSessionNegotiatorFactory(remoteAs, listener);
59         return super.createClient(address, strategy, new PipelineInitializer<BGPSessionImpl>() {
60             @Override
61             public void initializeChannel(final SocketChannel ch, final Promise<BGPSessionImpl> promise) {
62                 ch.pipeline().addLast(BGPDispatcherImpl.this.hf.getDecoders());
63                 ch.pipeline().addLast("negotiator", snf.getSessionNegotiator(null, ch, promise));
64                 ch.pipeline().addLast(BGPDispatcherImpl.this.hf.getEncoders());
65             }
66         });
67     }
68
69     @Override
70     public Future<Void> createReconnectingClient(final InetSocketAddress address,
71             final AsNumber remoteAs, final BGPPeerRegistry listener, final ReconnectStrategyFactory connectStrategyFactory,
72             final ReconnectStrategyFactory reestablishStrategyFactory) {
73         return this.createReconnectingClient(address, remoteAs, listener, connectStrategyFactory, reestablishStrategyFactory,
74                 null);
75     }
76
77     @Override
78     public void close() {
79     }
80
81     @Override
82     public synchronized Future<Void> createReconnectingClient(final InetSocketAddress address,
83             final AsNumber remoteAs, final BGPPeerRegistry peerRegistry, final ReconnectStrategyFactory connectStrategyFactory,
84             final ReconnectStrategyFactory reestablishStrategyFactory, final KeyMapping keys) {
85         final BGPClientSessionNegotiatorFactory snf = new BGPClientSessionNegotiatorFactory(remoteAs, peerRegistry);
86
87         this.keys = keys;
88         final Future<Void> ret = super.createReconnectingClient(address, connectStrategyFactory, reestablishStrategyFactory.createReconnectStrategy(), new PipelineInitializer<BGPSessionImpl>() {
89             @Override
90             public void initializeChannel(final SocketChannel ch, final Promise<BGPSessionImpl> promise) {
91                 ch.pipeline().addLast(BGPDispatcherImpl.this.hf.getDecoders());
92                 ch.pipeline().addLast("negotiator", snf.getSessionNegotiator(null, ch, promise));
93                 ch.pipeline().addLast(BGPDispatcherImpl.this.hf.getEncoders());
94             }
95         });
96         this.keys = null;
97
98         return ret;
99     }
100
101     @Override
102     public ChannelFuture createServer(final BGPPeerRegistry registry, final InetSocketAddress address, final BGPSessionValidator sessionValidator) {
103         return this.createServer(registry, address, sessionValidator, null);
104     }
105
106     @Override
107     public ChannelFuture createServer(final BGPPeerRegistry registry, final InetSocketAddress address, final BGPSessionValidator sessionValidator, final KeyMapping keys) {
108         final BGPServerSessionNegotiatorFactory snf = new BGPServerSessionNegotiatorFactory(sessionValidator, registry);
109
110         this.keys = keys;
111         final ChannelFuture ret = super.createServer(address, new PipelineInitializer<BGPSessionImpl>() {
112             @Override
113             public void initializeChannel(final SocketChannel ch, final Promise<BGPSessionImpl> promise) {
114                 ch.pipeline().addLast(BGPDispatcherImpl.this.hf.getDecoders());
115                 ch.pipeline().addLast("negotiator", snf.getSessionNegotiator(null, ch, promise));
116                 ch.pipeline().addLast(BGPDispatcherImpl.this.hf.getEncoders());
117             }
118         });
119         this.keys = null;
120
121         return ret;
122     }
123
124     @Override
125     protected void customizeBootstrap(final Bootstrap b) {
126         if (keys != null && !keys.isEmpty()) {
127             if (cf == null) {
128                 throw new UnsupportedOperationException("No key access instance available, cannot use key mapping");
129             }
130             b.channelFactory(cf);
131             b.option(MD5ChannelOption.TCP_MD5SIG, keys);
132         }
133     }
134
135     @Override
136     protected void customizeBootstrap(final ServerBootstrap b) {
137         if (keys != null && !keys.isEmpty()) {
138             if (scf == null) {
139                 throw new UnsupportedOperationException("No key access instance available, cannot use key mapping");
140             }
141             b.channelFactory(scf);
142             b.option(MD5ChannelOption.TCP_MD5SIG, keys);
143         }
144     }
145
146 }