Use reconnect strategy
[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.channel.EventLoopGroup;
11 import io.netty.channel.socket.SocketChannel;
12 import io.netty.util.HashedWheelTimer;
13 import io.netty.util.Timer;
14 import io.netty.util.concurrent.Future;
15 import io.netty.util.concurrent.Promise;
16
17 import java.net.InetSocketAddress;
18
19 import org.opendaylight.protocol.bgp.parser.BGPMessageFactory;
20 import org.opendaylight.protocol.bgp.parser.BGPSessionListener;
21 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPDispatcher;
22 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
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.protocol.framework.SessionListenerFactory;
27
28 /**
29  * Implementation of BGPDispatcher.
30  */
31 public final class BGPDispatcherImpl extends AbstractDispatcher<BGPSessionImpl, BGPSessionListener> implements BGPDispatcher, AutoCloseable {
32         private final Timer timer = new HashedWheelTimer();
33
34         private final BGPHandlerFactory hf;
35
36         public BGPDispatcherImpl(final BGPMessageFactory parser, final EventLoopGroup bossGroup, final EventLoopGroup workerGroup) {
37                 super(bossGroup, workerGroup);
38                 this.hf = new BGPHandlerFactory(parser);
39         }
40
41         @Override
42         public Future<BGPSessionImpl> createClient(final InetSocketAddress address, final BGPSessionPreferences preferences,
43                         final BGPSessionListener listener, final ReconnectStrategy strategy) {
44                 final BGPSessionNegotiatorFactory snf = new BGPSessionNegotiatorFactory(this.timer, preferences);
45                 final SessionListenerFactory<BGPSessionListener> slf = new SessionListenerFactory<BGPSessionListener>() {
46                         @Override
47                         public BGPSessionListener getSessionListener() {
48                                 return listener;
49                         }
50                 };
51                 return super.createClient(address, strategy, new PipelineInitializer<BGPSessionImpl>() {
52                         @Override
53                         public void initializeChannel(final SocketChannel ch, final Promise<BGPSessionImpl> promise) {
54                                 ch.pipeline().addLast(hf.getDecoders());
55                                 ch.pipeline().addLast("negotiator", snf.getSessionNegotiator(slf, ch, promise));
56                                 ch.pipeline().addLast(hf.getEncoders());
57                         }
58                 });
59         }
60
61         @Override
62         public Future<Void> createReconnectingClient(final InetSocketAddress address,
63                         final BGPSessionPreferences preferences, final BGPSessionListener listener,
64                         final ReconnectStrategyFactory connectStrategyFactory,
65                         final ReconnectStrategy reestablishStrategy) {
66                 final BGPSessionNegotiatorFactory snf = new BGPSessionNegotiatorFactory(this.timer, preferences);
67                 final SessionListenerFactory<BGPSessionListener> slf = new SessionListenerFactory<BGPSessionListener>() {
68                         @Override
69                         public BGPSessionListener getSessionListener() {
70                                 return listener;
71                         }
72                 };
73
74                 return super.createReconnectingClient(address, connectStrategyFactory, reestablishStrategy, new PipelineInitializer<BGPSessionImpl>() {
75                         @Override
76                         public void initializeChannel(final SocketChannel ch, final Promise<BGPSessionImpl> promise) {
77                                 ch.pipeline().addLast(hf.getDecoders());
78                                 ch.pipeline().addLast("negotiator", snf.getSessionNegotiator(slf, ch, promise));
79                                 ch.pipeline().addLast(hf.getEncoders());
80                         }
81                 });
82         }
83
84         @Override
85         public void close() {
86         }
87 }