X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fnetconf%2Fnetconf-netty-util%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fnetconf%2Fnettyutil%2FAbstractNetconfSessionNegotiator.java;h=e5c3c12b992246516165e94f1f0dbb48de3ddefd;hp=c770bde9206c03bba89027bc77b5fcb835e8dd03;hb=d5fcdd3416922d61f0bdebbe57e351456e3a3750;hpb=ab935690fe544f82095c57f998fc9c0fd03c0a51 diff --git a/opendaylight/netconf/netconf-netty-util/src/main/java/org/opendaylight/controller/netconf/nettyutil/AbstractNetconfSessionNegotiator.java b/opendaylight/netconf/netconf-netty-util/src/main/java/org/opendaylight/controller/netconf/nettyutil/AbstractNetconfSessionNegotiator.java index c770bde920..e5c3c12b99 100644 --- a/opendaylight/netconf/netconf-netty-util/src/main/java/org/opendaylight/controller/netconf/nettyutil/AbstractNetconfSessionNegotiator.java +++ b/opendaylight/netconf/netconf-netty-util/src/main/java/org/opendaylight/controller/netconf/nettyutil/AbstractNetconfSessionNegotiator.java @@ -11,6 +11,7 @@ package org.opendaylight.controller.netconf.nettyutil; import com.google.common.base.Optional; import com.google.common.base.Preconditions; import io.netty.channel.Channel; +import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; @@ -41,9 +42,9 @@ import org.w3c.dom.Document; import org.w3c.dom.NodeList; public abstract class AbstractNetconfSessionNegotiator

, L extends NetconfSessionListener> -extends AbstractSessionNegotiator { + extends AbstractSessionNegotiator { - private static final Logger logger = LoggerFactory.getLogger(AbstractNetconfSessionNegotiator.class); + private static final Logger LOG = LoggerFactory.getLogger(AbstractNetconfSessionNegotiator.class); public static final String NAME_OF_EXCEPTION_HANDLER = "lastExceptionHandler"; @@ -60,6 +61,7 @@ extends AbstractSessionNegotiator { } private State state = State.IDLE; + private final Promise promise; private final Timer timer; private final long connectionTimeoutMillis; @@ -68,6 +70,7 @@ extends AbstractSessionNegotiator { L sessionListener, long connectionTimeoutMillis) { super(promise, channel); this.sessionPreferences = sessionPreferences; + this.promise = promise; this.timer = timer; this.sessionListener = sessionListener; this.connectionTimeoutMillis = connectionTimeoutMillis; @@ -82,7 +85,7 @@ extends AbstractSessionNegotiator { @Override public void operationComplete(Future future) { Preconditions.checkState(future.isSuccess(), "Ssl handshake was not successful"); - logger.debug("Ssl handshake complete"); + LOG.debug("Ssl handshake complete"); start(); } }); @@ -102,32 +105,52 @@ extends AbstractSessionNegotiator { private void start() { final NetconfMessage helloMessage = this.sessionPreferences.getHelloMessage(); - logger.debug("Session negotiation started with hello message {}", XmlUtil.toString(helloMessage.getDocument())); + LOG.debug("Session negotiation started with hello message {} on channel {}", XmlUtil.toString(helloMessage.getDocument()), channel); channel.pipeline().addLast(NAME_OF_EXCEPTION_HANDLER, new ExceptionHandlingInboundChannelHandler()); + // FIXME, make sessionPreferences return HelloMessage, move NetconfHelloMessage to API + sendMessage((NetconfHelloMessage)helloMessage); + + replaceHelloMessageOutboundHandler(); + changeState(State.OPEN_WAIT); + timeout = this.timer.newTimeout(new TimerTask() { @Override public void run(final Timeout timeout) { synchronized (this) { if (state != State.ESTABLISHED) { - logger.debug("Connection timeout after {}, session is in state {}", timeout, state); - final IllegalStateException cause = new IllegalStateException( - "Session was not established after " + timeout); - negotiationFailed(cause); - changeState(State.FAILED); + + LOG.debug("Connection timeout after {}, session is in state {}", timeout, state); + + // 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) { + negotiationFailed(new IllegalStateException("Session was not established after " + timeout)); + changeState(State.FAILED); + + channel.closeFuture().addListener(new GenericFutureListener() { + @Override + public void operationComplete(ChannelFuture future) throws Exception { + if(future.isSuccess()) { + LOG.debug("Channel {} closed: success", future.channel()); + } else { + LOG.warn("Channel {} closed: fail", future.channel()); + } + } + }); + } } else if(channel.isOpen()) { channel.pipeline().remove(NAME_OF_EXCEPTION_HANDLER); } } } - }, connectionTimeoutMillis, TimeUnit.MILLISECONDS); - // FIXME, make sessionPreferences return HelloMessage, move NetconfHelloMessage to API - sendMessage((NetconfHelloMessage)helloMessage); + private boolean isPromiseFinished() { + return promise.isDone() || promise.isCancelled(); + } - replaceHelloMessageOutboundHandler(); - changeState(State.OPEN_WAIT); + }, connectionTimeoutMillis, TimeUnit.MILLISECONDS); } private void cancelTimeout() { @@ -200,9 +223,9 @@ extends AbstractSessionNegotiator { protected abstract S getSession(L sessionListener, Channel channel, NetconfHelloMessage message) throws NetconfDocumentedException; private synchronized void changeState(final State newState) { - logger.debug("Changing state from : {} to : {}", state, newState); - Preconditions.checkState(isStateChangePermitted(state, newState), "Cannot change state from %s to %s", state, - newState); + LOG.debug("Changing state from : {} to : {} for channel: {}", state, newState, channel); + Preconditions.checkState(isStateChangePermitted(state, newState), "Cannot change state from %s to %s for chanel %s", state, + newState, channel); this.state = newState; } @@ -226,7 +249,7 @@ extends AbstractSessionNegotiator { if (state == State.OPEN_WAIT && newState == State.FAILED) { return true; } - logger.debug("Transition from {} to {} is not allowed", state, newState); + LOG.debug("Transition from {} to {} is not allowed", state, newState); return false; } @@ -236,7 +259,7 @@ extends AbstractSessionNegotiator { private final class ExceptionHandlingInboundChannelHandler extends ChannelInboundHandlerAdapter { @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { - logger.warn("An exception occurred during negotiation with {}", channel.remoteAddress(), cause); + LOG.warn("An exception occurred during negotiation with {}", channel.remoteAddress(), cause); cancelTimeout(); negotiationFailed(cause); changeState(State.FAILED);