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