X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fnetconf%2Fnetconf-netty-util%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fnetconf%2Fnettyutil%2FAbstractNetconfSessionNegotiator.java;h=635baab924d0ec6f43eb65ce70614f2318eeaf39;hb=b2e81149739c87f0ecc2ce7f06448d7a5d3162b8;hp=c770bde9206c03bba89027bc77b5fcb835e8dd03;hpb=7d753ff9887cb803bdcd222aec2ab2a0a9c87906;p=controller.git
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..635baab924 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;
@@ -26,6 +27,7 @@ import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
import org.opendaylight.controller.netconf.api.NetconfMessage;
import org.opendaylight.controller.netconf.api.NetconfSessionListener;
import org.opendaylight.controller.netconf.api.NetconfSessionPreferences;
+import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
import org.opendaylight.controller.netconf.nettyutil.handler.FramingMechanismHandlerFactory;
import org.opendaylight.controller.netconf.nettyutil.handler.NetconfChunkAggregator;
import org.opendaylight.controller.netconf.nettyutil.handler.NetconfMessageToXMLEncoder;
@@ -33,7 +35,6 @@ import org.opendaylight.controller.netconf.nettyutil.handler.NetconfXMLToHelloMe
import org.opendaylight.controller.netconf.nettyutil.handler.NetconfXMLToMessageDecoder;
import org.opendaylight.controller.netconf.util.messages.FramingMechanism;
import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessage;
-import org.opendaylight.controller.netconf.util.xml.XmlUtil;
import org.opendaylight.protocol.framework.AbstractSessionNegotiator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -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,14 +61,16 @@ extends AbstractSessionNegotiator {
}
private State state = State.IDLE;
+ private final Promise promise;
private final Timer timer;
private final long connectionTimeoutMillis;
// TODO shrink constructor
- protected AbstractNetconfSessionNegotiator(P sessionPreferences, Promise promise, Channel channel, Timer timer,
- L sessionListener, long connectionTimeoutMillis) {
+ protected AbstractNetconfSessionNegotiator(final P sessionPreferences, final Promise promise, final Channel channel, final Timer timer,
+ final L sessionListener, final long connectionTimeoutMillis) {
super(promise, channel);
this.sessionPreferences = sessionPreferences;
+ this.promise = promise;
this.timer = timer;
this.sessionListener = sessionListener;
this.connectionTimeoutMillis = connectionTimeoutMillis;
@@ -80,9 +83,9 @@ extends AbstractSessionNegotiator {
Future future = sslHandler.get().handshakeFuture();
future.addListener(new GenericFutureListener>() {
@Override
- public void operationComplete(Future super Channel> future) {
+ public void operationComplete(final Future super Channel> future) {
Preconditions.checkState(future.isSuccess(), "Ssl handshake was not successful");
- logger.debug("Ssl handshake complete");
+ LOG.debug("Ssl handshake complete");
start();
}
});
@@ -91,7 +94,7 @@ extends AbstractSessionNegotiator {
}
}
- private static Optional getSslHandler(Channel channel) {
+ private static Optional getSslHandler(final Channel channel) {
final SslHandler sslHandler = channel.pipeline().get(SslHandler.class);
return sslHandler == null ? Optional. absent() : Optional.of(sslHandler);
}
@@ -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 {}", helloMessage, 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(final 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() {
@@ -136,7 +159,7 @@ extends AbstractSessionNegotiator {
}
}
- protected final S getSessionForHelloMessage(NetconfHelloMessage netconfMessage) throws NetconfDocumentedException {
+ protected final S getSessionForHelloMessage(final NetconfHelloMessage netconfMessage) throws NetconfDocumentedException {
Preconditions.checkNotNull(netconfMessage, "netconfMessage");
final Document doc = netconfMessage.getDocument();
@@ -159,7 +182,7 @@ extends AbstractSessionNegotiator {
new NetconfChunkAggregator());
}
- private boolean shouldUseChunkFraming(Document doc) {
+ private boolean shouldUseChunkFraming(final Document doc) {
return containsBase11Capability(doc)
&& containsBase11Capability(sessionPreferences.getHelloMessage().getDocument());
}
@@ -193,30 +216,30 @@ extends AbstractSessionNegotiator {
replaceChannelHandler(channel, AbstractChannelInitializer.NETCONF_MESSAGE_ENCODER, new NetconfMessageToXMLEncoder());
}
- private static ChannelHandler replaceChannelHandler(Channel channel, String handlerKey, ChannelHandler decoder) {
+ private static ChannelHandler replaceChannelHandler(final Channel channel, final String handlerKey, final ChannelHandler decoder) {
return channel.pipeline().replace(handlerKey, handlerKey, decoder);
}
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;
}
- private boolean containsBase11Capability(final Document doc) {
- final NodeList nList = doc.getElementsByTagName("capability");
+ private static boolean containsBase11Capability(final Document doc) {
+ final NodeList nList = doc.getElementsByTagName(XmlNetconfConstants.CAPABILITY);
for (int i = 0; i < nList.getLength(); i++) {
- if (nList.item(i).getTextContent().contains("base:1.1")) {
+ if (nList.item(i).getTextContent().contains(XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_1)) {
return true;
}
}
return false;
}
- private static boolean isStateChangePermitted(State state, State newState) {
+ private static boolean isStateChangePermitted(final State state, final State newState) {
if (state == State.IDLE && newState == State.OPEN_WAIT) {
return true;
}
@@ -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;
}
@@ -235,8 +258,8 @@ 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);
+ public void exceptionCaught(final ChannelHandlerContext ctx, final Throwable cause) {
+ LOG.warn("An exception occurred during negotiation with {}", channel.remoteAddress(), cause);
cancelTimeout();
negotiationFailed(cause);
changeState(State.FAILED);