Specialize protocol-framework to netconf
[netconf.git] / netconf / netconf-netty-util / src / main / java / org / opendaylight / netconf / nettyutil / ReconnectPromise.java
similarity index 70%
rename from protocol-framework/src/main/java/org/opendaylight/protocol/framework/ReconnectPromise.java
rename to netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/ReconnectPromise.java
index a0728514ed1a4a568938e7595903b808f5185917..696d9f7ad02587e934ba19c95c2c72c12d1d149e 100644 (file)
@@ -5,7 +5,7 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-package org.opendaylight.protocol.framework;
+package org.opendaylight.netconf.nettyutil;
 
 import com.google.common.base.Preconditions;
 import io.netty.bootstrap.Bootstrap;
@@ -15,24 +15,28 @@ import io.netty.util.concurrent.DefaultPromise;
 import io.netty.util.concurrent.EventExecutor;
 import io.netty.util.concurrent.Future;
 import java.net.InetSocketAddress;
+import org.opendaylight.netconf.api.NetconfSession;
+import org.opendaylight.netconf.api.NetconfSessionListener;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 @Deprecated
-final class ReconnectPromise<S extends ProtocolSession<?>, L extends SessionListener<?, ?, ?>> extends DefaultPromise<Void> {
+final class ReconnectPromise<S extends NetconfSession, L extends NetconfSessionListener<? super S>>
+        extends DefaultPromise<Void> {
     private static final Logger LOG = LoggerFactory.getLogger(ReconnectPromise.class);
 
-    private final AbstractDispatcher<S, L> dispatcher;
+    private final AbstractNetconfDispatcher<S, L> dispatcher;
     private final InetSocketAddress address;
     private final ReconnectStrategyFactory strategyFactory;
-    private final Bootstrap b;
-    private final AbstractDispatcher.PipelineInitializer<S> initializer;
+    private final Bootstrap bootstrap;
+    private final AbstractNetconfDispatcher.PipelineInitializer<S> initializer;
     private Future<?> pending;
 
-    public ReconnectPromise(final EventExecutor executor, final AbstractDispatcher<S, L> dispatcher, final InetSocketAddress address,
-                            final ReconnectStrategyFactory connectStrategyFactory, final Bootstrap b, final AbstractDispatcher.PipelineInitializer<S> initializer) {
+    ReconnectPromise(final EventExecutor executor, final AbstractNetconfDispatcher<S, L> dispatcher,
+            final InetSocketAddress address, final ReconnectStrategyFactory connectStrategyFactory,
+            final Bootstrap bootstrap, final AbstractNetconfDispatcher.PipelineInitializer<S> initializer) {
         super(executor);
-        this.b = b;
+        this.bootstrap = bootstrap;
         this.initializer = Preconditions.checkNotNull(initializer);
         this.dispatcher = Preconditions.checkNotNull(dispatcher);
         this.address = Preconditions.checkNotNull(address);
@@ -42,13 +46,17 @@ final class ReconnectPromise<S extends ProtocolSession<?>, L extends SessionList
     synchronized void connect() {
         final ReconnectStrategy cs = this.strategyFactory.createReconnectStrategy();
 
-        // Set up a client with pre-configured bootstrap, but add a closed channel handler into the pipeline to support reconnect attempts
-        pending = this.dispatcher.createClient(this.address, cs, b, (channel, promise) -> {
+        // Set up a client with pre-configured bootstrap, but add a closed channel handler into the pipeline to support
+        // reconnect attempts
+        pending = this.dispatcher.createClient(this.address, cs, bootstrap, (channel, promise) -> {
             initializer.initializeChannel(channel, promise);
             // add closed channel handler
-            // This handler has to be added as last channel handler and the channel inactive event has to be caught by it
-            // Handlers in front of it can react to channelInactive event, but have to forward the event or the reconnect will not work
-            // 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
+            // This handler has to be added as last channel handler and the channel inactive event has to be caught by
+            // it
+            // Handlers in front of it can react to channelInactive event, but have to forward the event or the
+            // reconnect will not work
+            // 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
             channel.pipeline().addLast(new ClosedChannelHandler(ReconnectPromise.this));
         });
 
@@ -60,10 +68,12 @@ final class ReconnectPromise<S extends ProtocolSession<?>, L extends SessionList
     }
 
     /**
+     * Indicate if the initial connection succeeded.
      *
-     * @return true if initial connection was established successfully, false if initial connection failed due to e.g. Connection refused, Negotiation failed
+     * @return true if initial connection was established successfully, false if initial connection failed due to e.g.
+     *         Connection refused, Negotiation failed
      */
-    private boolean isInitialConnectFinished() {
+    private synchronized boolean isInitialConnectFinished() {
         Preconditions.checkNotNull(pending);
         return pending.isDone() && pending.isSuccess();
     }
@@ -86,7 +96,7 @@ final class ReconnectPromise<S extends ProtocolSession<?>, L extends SessionList
     private static final class ClosedChannelHandler extends ChannelInboundHandlerAdapter {
         private final ReconnectPromise<?, ?> promise;
 
-        public ClosedChannelHandler(final ReconnectPromise<?, ?> promise) {
+        ClosedChannelHandler(final ReconnectPromise<?, ?> promise) {
             this.promise = promise;
         }
 
@@ -105,5 +115,4 @@ final class ReconnectPromise<S extends ProtocolSession<?>, L extends SessionList
             promise.connect();
         }
     }
-
 }