Introduce NetconfTimer
[netconf.git] / protocol / netconf-client / src / main / java / org / opendaylight / netconf / client / NetconfClientSessionNegotiator.java
index ab26db4e170aee68bd1e5e30a4a5926f66bbf909..8455821ef5aca833b14fff2dfef0e6ac52589f4d 100644 (file)
@@ -7,6 +7,7 @@
  */
 package org.opendaylight.netconf.client;
 
+import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Strings;
 import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Interner;
@@ -14,11 +15,11 @@ import com.google.common.collect.Interners;
 import io.netty.channel.Channel;
 import io.netty.channel.ChannelHandlerContext;
 import io.netty.channel.ChannelInboundHandlerAdapter;
-import io.netty.util.Timer;
 import io.netty.util.concurrent.Promise;
 import java.util.Set;
 import javax.xml.xpath.XPathConstants;
 import javax.xml.xpath.XPathExpression;
+import javax.xml.xpath.XPathExpressionException;
 import org.checkerframework.checker.index.qual.NonNegative;
 import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.netconf.api.NetconfDocumentedException;
@@ -27,8 +28,9 @@ import org.opendaylight.netconf.api.messages.NetconfMessage;
 import org.opendaylight.netconf.api.messages.RpcMessage;
 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
 import org.opendaylight.netconf.api.xml.XmlUtil;
+import org.opendaylight.netconf.common.NetconfTimer;
 import org.opendaylight.netconf.nettyutil.AbstractChannelInitializer;
-import org.opendaylight.netconf.nettyutil.AbstractNetconfSessionNegotiator;
+import org.opendaylight.netconf.nettyutil.NetconfSessionNegotiator;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.SessionIdType;
 import org.opendaylight.yangtools.yang.common.Uint32;
 import org.slf4j.Logger;
@@ -39,7 +41,7 @@ import org.w3c.dom.NodeList;
 
 // Non-final for mocking
 class NetconfClientSessionNegotiator
-        extends AbstractNetconfSessionNegotiator<NetconfClientSession, NetconfClientSessionListener> {
+        extends NetconfSessionNegotiator<NetconfClientSession, NetconfClientSessionListener> {
     private static final Logger LOG = LoggerFactory.getLogger(NetconfClientSessionNegotiator.class);
 
     private static final XPathExpression SESSION_ID_X_PATH = XMLNetconfUtil
@@ -55,7 +57,7 @@ class NetconfClientSessionNegotiator
     private final RpcMessage startExi;
 
     NetconfClientSessionNegotiator(final HelloMessage hello, final RpcMessage startExi,
-            final Promise<NetconfClientSession> promise, final Channel channel, final Timer timer,
+            final Promise<NetconfClientSession> promise, final Channel channel, final NetconfTimer timer,
             final NetconfClientSessionListener sessionListener, final long connectionTimeoutMillis,
             final @NonNegative int maximumIncomingChunkSize) {
         super(hello, promise, channel, timer, sessionListener, connectionTimeoutMillis, maximumIncomingChunkSize);
@@ -138,7 +140,7 @@ class NetconfClientSessionNegotiator
     }
 
     private static String getSessionIdWithXPath(final Document doc, final XPathExpression sessionIdXPath) {
-        final Node sessionIdNode = (Node) XmlUtil.evaluateXPath(sessionIdXPath, doc, XPathConstants.NODE);
+        final var sessionIdNode = evaluateXPath(sessionIdXPath, doc);
         return sessionIdNode != null ? sessionIdNode.getTextContent() : null;
     }
 
@@ -154,6 +156,15 @@ class NetconfClientSessionNegotiator
         return new NetconfClientSession(sessionListener, channel, sessionId, capabilities);
     }
 
+    @VisibleForTesting
+    static Node evaluateXPath(final XPathExpression expr, final Object rootNode) {
+        try {
+            return (Node) expr.evaluate(rootNode, XPathConstants.NODE);
+        } catch (final XPathExpressionException e) {
+            throw new IllegalStateException("Error while evaluating xpath expression " + expr, e);
+        }
+    }
+
     /**
      * Handler to process response for start-exi message.
      */
@@ -195,10 +206,9 @@ class NetconfClientSessionNegotiator
 
                 // Unexpected response to start-exi, throwing message away, continue without exi
             } else {
-                LOG.warn("Unexpected response to start-exi message, should be ok, was {}, "
-                        + "Communication will continue without exi "
-                        + "and response message will be thrown away on session {}",
-                        netconfMessage, session);
+                LOG.warn("""
+                    Unexpected response to start-exi message, should be ok, was {}. Communication will continue \
+                    without EXI and response message will be thrown away on session {}""", netconfMessage, session);
             }
 
             negotiationSuccessful(session);