BUG-2873 : Remove dependency protocol-framework on BGP
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / protocol / BGPReconnectPromise.java
1 /*
2  * Copyright (c) 2015 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.protocol;
9
10 import com.google.common.base.Preconditions;
11 import io.netty.bootstrap.Bootstrap;
12 import io.netty.channel.ChannelHandler;
13 import io.netty.channel.ChannelHandlerContext;
14 import io.netty.channel.ChannelInboundHandlerAdapter;
15 import io.netty.channel.socket.SocketChannel;
16 import io.netty.util.concurrent.DefaultPromise;
17 import io.netty.util.concurrent.EventExecutor;
18 import io.netty.util.concurrent.Future;
19 import io.netty.util.concurrent.GenericFutureListener;
20 import io.netty.util.concurrent.Promise;
21 import java.net.InetSocketAddress;
22 import org.opendaylight.protocol.bgp.rib.impl.BGPDispatcherImpl;
23 import org.opendaylight.protocol.bgp.rib.impl.BGPSessionImpl;
24 import org.opendaylight.protocol.framework.ReconnectStrategy;
25 import org.opendaylight.protocol.framework.ReconnectStrategyFactory;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 public class BGPReconnectPromise extends DefaultPromise<Void> {
30     private static final Logger LOG = LoggerFactory.getLogger(BGPReconnectPromise.class);
31
32     private final InetSocketAddress address;
33     private final ReconnectStrategyFactory strategyFactory;
34     private final Bootstrap b;
35     private final BGPDispatcherImpl.ChannelPipelineInitializer initializer;
36     private final EventExecutor executor;
37     private Future<BGPSessionImpl> pending;
38
39     public BGPReconnectPromise(final EventExecutor executor, final InetSocketAddress address,
40                                final ReconnectStrategyFactory connectStrategyFactory, final Bootstrap b,
41                                final BGPDispatcherImpl.ChannelPipelineInitializer initializer) {
42         super(executor);
43         this.executor = executor;
44         this.b = b;
45         this.initializer = Preconditions.checkNotNull(initializer);
46         this.address = Preconditions.checkNotNull(address);
47         this.strategyFactory = Preconditions.checkNotNull(connectStrategyFactory);
48     }
49
50     public synchronized void connect() {
51         final ReconnectStrategy cs = this.strategyFactory.createReconnectStrategy();
52
53         // Set up a client with pre-configured bootstrap, but add a closed channel handler into the pipeline to support reconnect attempts
54         pending = createClient(this.address, cs, b, new BGPDispatcherImpl.ChannelPipelineInitializer() {
55             @Override
56             public void initializeChannel(final SocketChannel channel, final Promise<BGPSessionImpl> promise) {
57                 initializer.initializeChannel(channel, promise);
58                 // add closed channel handler
59                 // This handler has to be added as last channel handler and the channel inactive event has to be caught by it
60                 // Handlers in front of it can react to channelInactive event, but have to forward the event or the reconnect will not work
61                 // This handler is last so all handlers in front of it can handle channel inactive (to e.g. resource cleanup) before a new connection is started
62                 channel.pipeline().addLast(new ClosedChannelHandler(BGPReconnectPromise.this));
63             }
64         });
65
66         pending.addListener(new GenericFutureListener<Future<Object>>() {
67             @Override
68             public void operationComplete(Future<Object> future) throws Exception {
69                 if (!future.isSuccess()) {
70                     BGPReconnectPromise.this.setFailure(future.cause());
71                 }
72             }
73         });
74     }
75
76     public Future<BGPSessionImpl> createClient(final InetSocketAddress address, final ReconnectStrategy strategy, final Bootstrap bootstrap,
77                                                final BGPDispatcherImpl.ChannelPipelineInitializer initializer) {
78         final BGPProtocolSessionPromise p = new BGPProtocolSessionPromise(this.executor, address, strategy, bootstrap);
79         final ChannelHandler chInit = BGPDispatcherImpl.BGPChannel.createChannelInitializer(initializer, p);
80         bootstrap.handler(chInit);
81         p.connect();
82         LOG.debug("Client created.");
83         return p;
84     }
85
86     /**
87      * @return true if initial connection was established successfully, false if initial connection failed due to e.g. Connection refused, Negotiation failed
88      */
89     private boolean isInitialConnectFinished() {
90         Preconditions.checkNotNull(pending);
91         return pending.isDone() && pending.isSuccess();
92     }
93
94     @Override
95     public synchronized boolean cancel(final boolean mayInterruptIfRunning) {
96         if (super.cancel(mayInterruptIfRunning)) {
97             Preconditions.checkNotNull(pending);
98             this.pending.cancel(mayInterruptIfRunning);
99             return true;
100         }
101
102         return false;
103     }
104
105     /**
106      * Channel handler that responds to channelInactive event and reconnects the session.
107      * Only if the promise was not canceled.
108      */
109     private static final class ClosedChannelHandler extends ChannelInboundHandlerAdapter {
110         private final BGPReconnectPromise promise;
111
112         public ClosedChannelHandler(final BGPReconnectPromise promise) {
113             this.promise = promise;
114         }
115
116         @Override
117         public void channelInactive(final ChannelHandlerContext ctx) throws Exception {
118             // This is the ultimate channel inactive handler, not forwarding
119             if (promise.isCancelled()) {
120                 return;
121             }
122
123             if (!promise.isInitialConnectFinished()) {
124                 LOG.debug("Connection to {} was dropped during negotiation, reattempting", promise.address);
125             }
126
127             LOG.debug("Reconnecting after connection to {} was dropped", promise.address);
128             promise.connect();
129         }
130     }
131 }