Allow SshClient to be customized via NetconfClientConfiguration
[netconf.git] / netconf / tools / netconf-testtool / src / main / java / org / opendaylight / netconf / test / tool / client / stress / StressClient.java
index e32c99c10e84a17f32a9c31acf615be9cd054741..6d02ba259a94585aa3c4fdedcfc763ce522f8967 100644 (file)
@@ -11,6 +11,7 @@ package org.opendaylight.netconf.test.tool.client.stress;
 import ch.qos.logback.classic.Level;
 import com.google.common.base.Stopwatch;
 import com.google.common.io.Files;
+import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 import io.netty.channel.nio.NioEventLoopGroup;
 import io.netty.util.HashedWheelTimer;
 import io.netty.util.Timer;
@@ -26,8 +27,8 @@ import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
 import net.sourceforge.argparse4j.inf.ArgumentParser;
 import net.sourceforge.argparse4j.inf.ArgumentParserException;
-import org.opendaylight.controller.config.util.xml.XmlUtil;
 import org.opendaylight.netconf.api.NetconfMessage;
+import org.opendaylight.netconf.api.xml.XmlUtil;
 import org.opendaylight.netconf.client.NetconfClientDispatcherImpl;
 import org.opendaylight.netconf.nettyutil.handler.ssh.client.AsyncSshHandler;
 import org.opendaylight.netconf.sal.connect.api.RemoteDevice;
@@ -44,6 +45,7 @@ import org.w3c.dom.Element;
 import org.w3c.dom.Node;
 import org.xml.sax.SAXException;
 
+@SuppressFBWarnings("DM_EXIT")
 public final class StressClient {
 
     private static final Logger LOG = LoggerFactory.getLogger(StressClient.class);
@@ -53,41 +55,42 @@ public final class StressClient {
 
     static {
         try {
-            COMMIT_MSG = new NetconfMessage(XmlUtil.readXmlToDocument("<rpc message-id=\"commit-batch\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
-                    "    <commit/>\n" +
-                    "</rpc>"));
-        } catch (SAXException | IOException e) {
+            COMMIT_MSG = new NetconfMessage(XmlUtil.readXmlToDocument(
+                "<rpc message-id=\"commit-batch\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
+                    + "    <commit/>\n"
+                    + "</rpc>"));
+        } catch (final SAXException | IOException e) {
             throw new ExceptionInInitializerError(e);
         }
     }
 
     static final QName EDIT_QNAME = QName.create(EditConfigInput.QNAME, "edit-config");
-    static final org.w3c.dom.Document editCandidateBlueprint;
-    static final org.w3c.dom.Document editRunningBlueprint;
+    static final org.w3c.dom.Document EDIT_CANDIDATE_BLUEPRINT;
+    static final org.w3c.dom.Document EDIT_RUNNING_BLUEPRINT;
 
     static {
         try {
-            editCandidateBlueprint = XmlUtil.readXmlToDocument(
-                    "<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
-                            "    <edit-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
-                            "        <target>\n" +
-                            "            <candidate/>\n" +
-                            "        </target>\n" +
-                            "        <default-operation>none</default-operation>" +
-                            "        <config/>\n" +
-                            "    </edit-config>\n" +
-                            "</rpc>");
-
-            editRunningBlueprint = XmlUtil.readXmlToDocument(
-                    "<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
-                            "    <edit-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
-                            "        <target>\n" +
-                            "            <running/>\n" +
-                            "        </target>\n" +
-                            "        <default-operation>none</default-operation>" +
-                            "        <config/>\n" +
-                            "    </edit-config>\n" +
-                            "</rpc>");
+            EDIT_CANDIDATE_BLUEPRINT = XmlUtil.readXmlToDocument(
+                    "<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
+                            + "    <edit-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
+                            + "        <target>\n"
+                            + "            <candidate/>\n"
+                            + "        </target>\n"
+                            + "        <default-operation>none</default-operation>"
+                            + "        <config/>\n"
+                            + "    </edit-config>\n"
+                            "</rpc>");
+
+            EDIT_RUNNING_BLUEPRINT = XmlUtil.readXmlToDocument(
+                    "<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
+                            + "    <edit-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
+                            + "        <target>\n"
+                            + "            <running/>\n"
+                            + "        </target>\n"
+                            + "        <default-operation>none</default-operation>"
+                            + "        <config/>\n"
+                            + "    </edit-config>\n"
+                            "</rpc>");
         } catch (SAXException | IOException e) {
             throw new ExceptionInInitializerError(e);
         }
@@ -100,39 +103,43 @@ public final class StressClient {
 
     private static Parameters params;
 
+    private StressClient() {
+
+    }
+
     public static void main(final String[] args) {
 
         params = parseArgs(args, Parameters.getParser());
         params.validate();
 
-        final ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
+        final ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) LoggerFactory
+            .getLogger(Logger.ROOT_LOGGER_NAME);
         root.setLevel(params.debug ? Level.DEBUG : Level.INFO);
 
         final int threadAmount = params.threadAmount;
-        LOG.info("thread amount: " + threadAmount);
+        LOG.info("thread amount: {}", threadAmount);
         final int requestsPerThread = params.editCount / params.threadAmount;
-        LOG.info("requestsPerThread: " + requestsPerThread);
+        LOG.info("requestsPerThread: {}", requestsPerThread);
         final int leftoverRequests = params.editCount % params.threadAmount;
-        LOG.info("leftoverRequests: " + leftoverRequests);
-
+        LOG.info("leftoverRequests: {}", leftoverRequests);
 
         LOG.info("Preparing messages");
         // Prepare all msgs up front
         final List<List<NetconfMessage>> allPreparedMessages = new ArrayList<>(threadAmount);
         for (int i = 0; i < threadAmount; i++) {
             if (i != threadAmount - 1) {
-                allPreparedMessages.add(new ArrayList<NetconfMessage>(requestsPerThread));
+                allPreparedMessages.add(new ArrayList<>(requestsPerThread));
             } else {
-                allPreparedMessages.add(new ArrayList<NetconfMessage>(requestsPerThread + leftoverRequests));
+                allPreparedMessages.add(new ArrayList<>(requestsPerThread + leftoverRequests));
             }
         }
 
 
         final String editContentString;
         try {
-            editContentString = Files.toString(params.editContent, StandardCharsets.UTF_8);
+            editContentString = Files.asCharSource(params.editContent, StandardCharsets.UTF_8).read();
         } catch (final IOException e) {
-            throw new IllegalArgumentException("Cannot read content of " + params.editContent);
+            throw new IllegalArgumentException("Cannot read content of " + params.editContent, e);
         }
 
         for (int i = 0; i < threadAmount; i++) {
@@ -142,7 +149,7 @@ public final class StressClient {
                 padding = leftoverRequests;
             }
             for (int j = 0; j < requestsPerThread + padding; j++) {
-                LOG.debug("id: " + (i * requestsPerThread + j));
+                LOG.debug("id: {}", i * requestsPerThread + j);
                 preparedMessages.add(prepareMessage(i * requestsPerThread + j, editContentString));
             }
         }
@@ -150,7 +157,7 @@ public final class StressClient {
         final NioEventLoopGroup nioGroup = new NioEventLoopGroup();
         final Timer timer = new HashedWheelTimer();
 
-        final NetconfClientDispatcherImpl netconfClientDispatcher = configureClientDispatcher(params, nioGroup, timer);
+        final NetconfClientDispatcherImpl netconfClientDispatcher = configureClientDispatcher(nioGroup, timer);
 
         final List<StressClientCallable> callables = new ArrayList<>(threadAmount);
         for (final List<NetconfMessage> messages : allPreparedMessages) {
@@ -177,7 +184,7 @@ public final class StressClient {
         started.stop();
 
         LOG.info("FINISHED. Execution time: {}", started);
-        LOG.info("Requests per second: {}", (params.editCount * 1000.0 / started.elapsed(TimeUnit.MILLISECONDS)));
+        LOG.info("Requests per second: {}", params.editCount * 1000.0 / started.elapsed(TimeUnit.MILLISECONDS));
 
         // Cleanup
         timer.stop();
@@ -193,7 +200,8 @@ public final class StressClient {
     }
 
     static NetconfMessage prepareMessage(final int id, final String editContentString) {
-        final Document msg = XmlUtil.createDocumentCopy(params.candidateDatastore ? editCandidateBlueprint : editRunningBlueprint);
+        final Document msg = XmlUtil.createDocumentCopy(
+            params.candidateDatastore ? EDIT_CANDIDATE_BLUEPRINT : EDIT_RUNNING_BLUEPRINT);
         msg.getDocumentElement().setAttribute("message-id", Integer.toString(id));
         final NetconfMessage netconfMessage = new NetconfMessage(msg);
 
@@ -204,15 +212,15 @@ public final class StressClient {
 
             final StringBuilder stringBuilder = new StringBuilder(specificEditContent);
             int idx = stringBuilder.indexOf(PHYS_ADDR_PLACEHOLDER);
-            while (idx!= -1) {
+            while (idx != -1) {
                 stringBuilder.replace(idx, idx + PHYS_ADDR_PLACEHOLDER.length(), TestToolUtils.getMac(macStart++));
                 idx = stringBuilder.indexOf(PHYS_ADDR_PLACEHOLDER);
             }
             specificEditContent = stringBuilder.toString();
 
             editContentElement = XmlUtil.readXmlToElement(specificEditContent);
-            final Node config = ((Element) msg.getDocumentElement().getElementsByTagName("edit-config").item(0)).
-                    getElementsByTagName("config").item(0);
+            final Node config = ((Element) msg.getDocumentElement().getElementsByTagName("edit-config").item(0))
+                    .getElementsByTagName("config").item(0);
             config.appendChild(msg.importNode(editContentElement, true));
         } catch (final IOException | SAXException e) {
             throw new IllegalArgumentException("Edit content file is unreadable", e);
@@ -221,16 +229,17 @@ public final class StressClient {
         return netconfMessage;
     }
 
-    private static NetconfClientDispatcherImpl configureClientDispatcher(final Parameters params, final NioEventLoopGroup nioGroup, final Timer timer) {
+    private static NetconfClientDispatcherImpl configureClientDispatcher(final NioEventLoopGroup nioGroup,
+            final Timer timer) {
         final NetconfClientDispatcherImpl netconfClientDispatcher;
-        if(params.exi) {
-            if(params.legacyFraming) {
-                netconfClientDispatcher= ConfigurableClientDispatcher.createLegacyExi(nioGroup, nioGroup, timer);
+        if (params.exi) {
+            if (params.legacyFraming) {
+                netconfClientDispatcher = ConfigurableClientDispatcher.createLegacyExi(nioGroup, nioGroup, timer);
             } else {
                 netconfClientDispatcher = ConfigurableClientDispatcher.createChunkedExi(nioGroup, nioGroup, timer);
             }
         } else {
-            if(params.legacyFraming) {
+            if (params.legacyFraming) {
                 netconfClientDispatcher = ConfigurableClientDispatcher.createLegacy(nioGroup, nioGroup, timer);
             } else {
                 netconfClientDispatcher = ConfigurableClientDispatcher.createChunked(nioGroup, nioGroup, timer);
@@ -252,10 +261,11 @@ public final class StressClient {
         return null;
     }
 
-
-    static class LoggingRemoteDevice implements RemoteDevice<NetconfSessionPreferences, NetconfMessage, NetconfDeviceCommunicator> {
+    static class LoggingRemoteDevice
+            implements RemoteDevice<NetconfSessionPreferences, NetconfMessage, NetconfDeviceCommunicator> {
         @Override
-        public void onRemoteSessionUp(final NetconfSessionPreferences remoteSessionCapabilities, final NetconfDeviceCommunicator netconfDeviceCommunicator) {
+        public void onRemoteSessionUp(final NetconfSessionPreferences remoteSessionCapabilities,
+                                      final NetconfDeviceCommunicator netconfDeviceCommunicator) {
             LOG.info("Session established");
         }