Removed checkstyle warnings.
[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
12 import io.netty.bootstrap.Bootstrap;
13 import io.netty.bootstrap.ServerBootstrap;
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
20 import java.net.InetSocketAddress;
21
22 import org.opendaylight.bgpcep.tcpmd5.KeyMapping;
23 import org.opendaylight.bgpcep.tcpmd5.netty.MD5ChannelFactory;
24 import org.opendaylight.bgpcep.tcpmd5.netty.MD5ChannelOption;
25 import org.opendaylight.bgpcep.tcpmd5.netty.MD5ServerChannelFactory;
26 import org.opendaylight.protocol.bgp.parser.BGPSessionListener;
27 import org.opendaylight.protocol.bgp.parser.spi.MessageRegistry;
28 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPDispatcher;
29 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
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.protocol.framework.SessionListenerFactory;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
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,
47             final EventLoopGroup workerGroup) {
48         this(messageRegistry, timer, bossGroup, workerGroup, null, null);
49     }
50
51     public BGPDispatcherImpl(final MessageRegistry messageRegistry, final Timer timer, final EventLoopGroup bossGroup,
52             final EventLoopGroup workerGroup, final MD5ChannelFactory<?> cf, final MD5ServerChannelFactory<?> scf) {
53         super(bossGroup, workerGroup);
54         this.timer = Preconditions.checkNotNull(timer);
55         this.hf = new BGPHandlerFactory(messageRegistry);
56         this.cf = cf;
57         this.scf = scf;
58     }
59
60     @Override
61     public synchronized Future<BGPSessionImpl> createClient(final InetSocketAddress address, final BGPSessionPreferences preferences,
62             final AsNumber remoteAs, final BGPSessionListener listener, final ReconnectStrategy strategy) {
63         final BGPSessionNegotiatorFactory snf = new BGPSessionNegotiatorFactory(this.timer, preferences, remoteAs);
64         final SessionListenerFactory<BGPSessionListener> slf = new SessionListenerFactory<BGPSessionListener>() {
65             @Override
66             public BGPSessionListener getSessionListener() {
67                 return listener;
68             }
69         };
70         return super.createClient(address, strategy, new PipelineInitializer<BGPSessionImpl>() {
71             @Override
72             public void initializeChannel(final SocketChannel ch, final Promise<BGPSessionImpl> promise) {
73                 ch.pipeline().addLast(BGPDispatcherImpl.this.hf.getDecoders());
74                 ch.pipeline().addLast("negotiator", snf.getSessionNegotiator(slf, ch, promise));
75                 ch.pipeline().addLast(BGPDispatcherImpl.this.hf.getEncoders());
76             }
77         });
78     }
79
80     @Override
81     public Future<Void> createReconnectingClient(final InetSocketAddress address, final BGPSessionPreferences preferences,
82             final AsNumber remoteAs, final BGPSessionListener listener, final ReconnectStrategyFactory connectStrategyFactory,
83             final ReconnectStrategyFactory reestablishStrategyFactory) {
84         return this.createReconnectingClient(address, preferences, remoteAs, listener, connectStrategyFactory, reestablishStrategyFactory,
85                 null);
86     }
87
88     @Override
89     public void close() {
90     }
91
92     @Override
93     public synchronized Future<Void> createReconnectingClient(final InetSocketAddress address, final BGPSessionPreferences preferences,
94             final AsNumber remoteAs, final BGPSessionListener listener, 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,
106                 reestablishStrategyFactory.createReconnectStrategy(), new PipelineInitializer<BGPSessionImpl>() {
107                     @Override
108                     public void initializeChannel(final SocketChannel ch, final Promise<BGPSessionImpl> promise) {
109                         ch.pipeline().addLast(BGPDispatcherImpl.this.hf.getDecoders());
110                         ch.pipeline().addLast("negotiator", snf.getSessionNegotiator(slf, ch, promise));
111                         ch.pipeline().addLast(BGPDispatcherImpl.this.hf.getEncoders());
112                     }
113                 });
114         this.keys = null;
115
116         return ret;
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 }