More warnings down:
[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.ChannelOption;
14 import io.netty.channel.EventLoopGroup;
15 import io.netty.channel.socket.SocketChannel;
16 import io.netty.util.concurrent.Future;
17 import io.netty.util.concurrent.Promise;
18 import java.net.InetSocketAddress;
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.bgp.rib.spi.BGPSessionListener;
24 import org.opendaylight.protocol.framework.AbstractDispatcher;
25 import org.opendaylight.protocol.framework.ReconnectStrategy;
26 import org.opendaylight.protocol.framework.ReconnectStrategyFactory;
27 import org.opendaylight.tcpmd5.api.KeyMapping;
28 import org.opendaylight.tcpmd5.netty.MD5ChannelFactory;
29 import org.opendaylight.tcpmd5.netty.MD5ChannelOption;
30 import org.opendaylight.tcpmd5.netty.MD5ServerChannelFactory;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
32
33 /**
34  * Implementation of BGPDispatcher.
35  */
36 public final class BGPDispatcherImpl extends AbstractDispatcher<BGPSessionImpl, BGPSessionListener> implements BGPDispatcher, AutoCloseable {
37     private final MD5ServerChannelFactory<?> scf;
38     private final MD5ChannelFactory<?> cf;
39     private final BGPHandlerFactory hf;
40     private KeyMapping keys;
41     private static final String NEGOTIATOR = "negotiator";
42
43     public BGPDispatcherImpl(final MessageRegistry messageRegistry, final EventLoopGroup bossGroup, final EventLoopGroup workerGroup) {
44         this(messageRegistry, bossGroup, workerGroup, null, null);
45     }
46
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);
50         this.cf = cf;
51         this.scf = scf;
52     }
53
54     @Override
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>() {
59             @Override
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());
64             }
65         });
66     }
67
68     @Override
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,
73             null);
74     }
75
76     @Override
77     public void close() {
78     }
79
80     @Override
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);
85
86         this.keys = keys;
87         final Future<Void> ret = super.createReconnectingClient(address, connectStrategyFactory, reestablishStrategyFactory.createReconnectStrategy(), new PipelineInitializer<BGPSessionImpl>() {
88             @Override
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());
93             }
94         });
95         this.keys = null;
96
97         return ret;
98     }
99
100     @Override
101     public ChannelFuture createServer(final BGPPeerRegistry registry, final InetSocketAddress address, final BGPSessionValidator sessionValidator) {
102         return this.createServer(registry, address, sessionValidator, null);
103     }
104
105     @Override
106     public ChannelFuture createServer(final BGPPeerRegistry registry, final InetSocketAddress address, final BGPSessionValidator sessionValidator, final KeyMapping keys) {
107         final BGPServerSessionNegotiatorFactory snf = new BGPServerSessionNegotiatorFactory(sessionValidator, registry);
108
109         this.keys = keys;
110         final ChannelFuture ret = super.createServer(address, new PipelineInitializer<BGPSessionImpl>() {
111             @Override
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());
116             }
117         });
118         this.keys = null;
119
120         return ret;
121     }
122
123     @Override
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");
128             }
129             b.channelFactory(this.cf);
130             b.option(MD5ChannelOption.TCP_MD5SIG, this.keys);
131             // Make sure we are doing round-robin processing
132             b.option(ChannelOption.MAX_MESSAGES_PER_READ, 1);
133         }
134     }
135
136     @Override
137     protected void customizeBootstrap(final ServerBootstrap b) {
138         if (this.keys != null && !this.keys.isEmpty()) {
139             if (this.scf == null) {
140                 throw new UnsupportedOperationException("No key access instance available, cannot use key mapping");
141             }
142             b.channelFactory(this.scf);
143             b.option(MD5ChannelOption.TCP_MD5SIG, this.keys);
144             // Make sure we are doing round-robin processing
145             b.childOption(ChannelOption.MAX_MESSAGES_PER_READ, 1);
146         }
147     }
148
149 }