X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=netconf%2Fnetconf-client%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fnetconf%2Fclient%2FNetconfClientSessionNegotiator.java;h=5ee52bd073ac1f125176282127bce30faec4c3d3;hb=57bfa6dd35ae51496e2c1aecd2ef7755b5203cba;hp=608450225f52d84d71cff3d1443b739905edaa5d;hpb=6d7e12bf3ef64e5004703a1d540e7e26f30a9595;p=netconf.git diff --git a/netconf/netconf-client/src/main/java/org/opendaylight/netconf/client/NetconfClientSessionNegotiator.java b/netconf/netconf-client/src/main/java/org/opendaylight/netconf/client/NetconfClientSessionNegotiator.java index 608450225f..5ee52bd073 100644 --- a/netconf/netconf-client/src/main/java/org/opendaylight/netconf/client/NetconfClientSessionNegotiator.java +++ b/netconf/netconf-client/src/main/java/org/opendaylight/netconf/client/NetconfClientSessionNegotiator.java @@ -5,16 +5,14 @@ * 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.netconf.client; import com.google.common.base.Strings; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Interner; import com.google.common.collect.Interners; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import io.netty.channel.Channel; -import io.netty.channel.ChannelFuture; -import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.util.Timer; @@ -22,12 +20,12 @@ import io.netty.util.concurrent.Promise; import java.util.Set; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpression; -import org.opendaylight.controller.config.util.xml.XmlUtil; import org.opendaylight.netconf.api.NetconfClientSessionPreferences; import org.opendaylight.netconf.api.NetconfDocumentedException; import org.opendaylight.netconf.api.NetconfMessage; import org.opendaylight.netconf.api.messages.NetconfHelloMessage; import org.opendaylight.netconf.api.xml.XmlNetconfConstants; +import org.opendaylight.netconf.api.xml.XmlUtil; import org.opendaylight.netconf.nettyutil.AbstractChannelInitializer; import org.opendaylight.netconf.nettyutil.AbstractNetconfSessionNegotiator; import org.opendaylight.netconf.nettyutil.handler.exi.NetconfStartExiMessage; @@ -39,8 +37,9 @@ import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; -public class NetconfClientSessionNegotiator extends - AbstractNetconfSessionNegotiator { private static final Logger LOG = LoggerFactory.getLogger(NetconfClientSessionNegotiator.class); @@ -54,26 +53,36 @@ public class NetconfClientSessionNegotiator extends private static final Interner> INTERNER = Interners.newWeakInterner(); - protected NetconfClientSessionNegotiator(final NetconfClientSessionPreferences sessionPreferences, - final Promise promise, - final Channel channel, - final Timer timer, - final NetconfClientSessionListener sessionListener, - final long connectionTimeoutMillis) { + NetconfClientSessionNegotiator(final NetconfClientSessionPreferences sessionPreferences, + final Promise promise, final Channel channel, final Timer timer, + final NetconfClientSessionListener sessionListener, final long connectionTimeoutMillis) { super(sessionPreferences, promise, channel, timer, sessionListener, connectionTimeoutMillis); } + @SuppressWarnings("checkstyle:IllegalCatch") + @SuppressFBWarnings(value = "BC_UNCONFIRMED_CAST", + justification = "SpotBugs does not understand generic cast of sessionPreferences") @Override protected void handleMessage(final NetconfHelloMessage netconfMessage) throws NetconfDocumentedException { + if (!ifNegotiatedAlready()) { + LOG.debug("Server hello message received, starting negotiation on channel {}", channel); + try { + startNegotiation(); + } catch (final Exception e) { + LOG.warn("Unexpected negotiation failure on channel {}", channel, e); + negotiationFailed(e); + return; + } + } final NetconfClientSession session = getSessionForHelloMessage(netconfMessage); replaceHelloMessageInboundHandler(session); // If exi should be used, try to initiate exi communication // Call negotiationSuccessFul after exi negotiation is finished successfully or not - if (shouldUseExi(netconfMessage)) { + final NetconfMessage startExiMessage = sessionPreferences.getStartExiMessage(); + if (shouldUseExi(netconfMessage) && startExiMessage instanceof NetconfStartExiMessage) { LOG.debug("Netconf session {} should use exi.", session); - NetconfStartExiMessage startExiMessage = (NetconfStartExiMessage) sessionPreferences.getStartExiMessage(); - tryToInitiateExi(session, startExiMessage); + tryToInitiateExi(session, (NetconfStartExiMessage) startExiMessage); } else { // Exi is not supported, release session immediately LOG.debug("Netconf session {} isn't capable of using exi.", session); @@ -91,20 +100,19 @@ public class NetconfClientSessionNegotiator extends ExiConfirmationInboundHandler.EXI_CONFIRMED_HANDLER, new ExiConfirmationInboundHandler(session, startExiMessage)); - session.sendMessage(startExiMessage).addListener(new ChannelFutureListener() { - @Override - public void operationComplete(final ChannelFuture channelFuture) { - if (!channelFuture.isSuccess()) { - LOG.warn("Failed to send start-exi message {} on session {}", startExiMessage, this, - channelFuture.cause()); - channel.pipeline().remove(ExiConfirmationInboundHandler.EXI_CONFIRMED_HANDLER); - } else { - LOG.trace("Start-exi message {} sent to socket on session {}", startExiMessage, this); - } + session.sendMessage(startExiMessage).addListener(channelFuture -> { + if (!channelFuture.isSuccess()) { + LOG.warn("Failed to send start-exi message {} on session {}", startExiMessage, session, + channelFuture.cause()); + channel.pipeline().remove(ExiConfirmationInboundHandler.EXI_CONFIRMED_HANDLER); + } else { + LOG.trace("Start-exi message {} sent to socket on session {}", startExiMessage, session); } }); } + @SuppressFBWarnings(value = "BC_UNCONFIRMED_CAST", + justification = "SpotBugs does not understand generic cast of sessionPreferences") private boolean shouldUseExi(final NetconfHelloMessage helloMsg) { return containsExi10Capability(helloMsg.getDocument()) && containsExi10Capability(sessionPreferences.getHelloMessage().getDocument()); @@ -130,7 +138,7 @@ public class NetconfClientSessionNegotiator extends } } - return Long.valueOf(textContent); + return Long.parseLong(textContent); } private static String getSessionIdWithXPath(final Document doc, final XPathExpression sessionIdXPath) { @@ -140,7 +148,7 @@ public class NetconfClientSessionNegotiator extends @Override protected NetconfClientSession getSession(final NetconfClientSessionListener sessionListener, final Channel channel, - final NetconfHelloMessage message) throws NetconfDocumentedException { + final NetconfHelloMessage message) { final long sessionId = extractSessionId(message.getDocument()); // Copy here is important: it disconnects the strings from the document