Merge "Bump to odlparent 3.1.0 and yangtools 2.0.3"
[netconf.git] / netconf / netconf-netty-util / src / main / java / org / opendaylight / netconf / nettyutil / AbstractNetconfSessionNegotiator.java
index 2f30023f541f94914fac34bac68d8852f383cb68..f8f486b2a0f63770b984e66a532ec332c7c9e7d0 100644 (file)
@@ -79,22 +79,31 @@ public abstract class AbstractNetconfSessionNegotiator<P extends NetconfSessionP
 
     @Override
     protected final void startNegotiation() {
-        final Optional<SslHandler> sslHandler = getSslHandler(channel);
-        if (sslHandler.isPresent()) {
-            Future<Channel> future = sslHandler.get().handshakeFuture();
-            future.addListener(new GenericFutureListener<Future<? super Channel>>() {
-                @Override
-                public void operationComplete(final Future<? super Channel> future) {
-                    Preconditions.checkState(future.isSuccess(), "Ssl handshake was not successful");
-                    LOG.debug("Ssl handshake complete");
-                    start();
-                }
-            });
+        if (ifNegotiatedAlready()) {
+            LOG.debug("Negotiation on channel {} already started", channel);
         } else {
-            start();
+            final Optional<SslHandler> sslHandler = getSslHandler(channel);
+            if (sslHandler.isPresent()) {
+                Future<Channel> future = sslHandler.get().handshakeFuture();
+                future.addListener(new GenericFutureListener<Future<? super Channel>>() {
+                    @Override
+                    public void operationComplete(final Future<? super Channel> future) {
+                        Preconditions.checkState(future.isSuccess(), "Ssl handshake was not successful");
+                        LOG.debug("Ssl handshake complete");
+                        start();
+                    }
+                });
+            } else {
+                start();
+            }
         }
     }
 
+    protected final boolean ifNegotiatedAlready() {
+        // Indicates whether negotiation already started
+        return this.state != State.IDLE;
+    }
+
     private static Optional<SslHandler> getSslHandler(final Channel channel) {
         final SslHandler sslHandler = channel.pipeline().get(SslHandler.class);
         return sslHandler == null ? Optional.<SslHandler>absent() : Optional.of(sslHandler);
@@ -117,6 +126,7 @@ public abstract class AbstractNetconfSessionNegotiator<P extends NetconfSessionP
 
         timeout = this.timer.newTimeout(new TimerTask() {
             @Override
+            @SuppressWarnings("checkstyle:hiddenField")
             public void run(final Timeout timeout) {
                 synchronized (this) {
                     if (state != State.ESTABLISHED) {
@@ -125,7 +135,7 @@ public abstract class AbstractNetconfSessionNegotiator<P extends NetconfSessionP
 
                         // Do not fail negotiation if promise is done or canceled
                         // It would result in setting result of the promise second time and that throws exception
-                        if (isPromiseFinished() == false) {
+                        if (!isPromiseFinished()) {
                             LOG.warn("Netconf session was not established after {}", connectionTimeoutMillis);
                             changeState(State.FAILED);
 
@@ -226,6 +236,7 @@ public abstract class AbstractNetconfSessionNegotiator<P extends NetconfSessionP
         return channel.pipeline().replace(handlerKey, handlerKey, decoder);
     }
 
+    @SuppressWarnings("checkstyle:hiddenField")
     protected abstract S getSession(L sessionListener, Channel channel, NetconfHelloMessage message)
             throws NetconfDocumentedException;