Clean up StressClient a bit
[netconf.git] / netconf / tools / netconf-testtool / src / main / java / org / opendaylight / netconf / test / tool / client / stress / StressClient.java
index 7d1fbcb761b63ff42133188e716d29c396b96ac5..a9d0d38a4f4faa3cc7f9bccfe3b3a3dea6e27cf3 100644 (file)
@@ -5,34 +5,30 @@
  * 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.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;
 import java.io.IOException;
-import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.concurrent.ExecutionException;
-import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
-import java.util.concurrent.Future;
 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.messages.NetconfMessage;
+import org.opendaylight.netconf.api.xml.XmlUtil;
 import org.opendaylight.netconf.client.NetconfClientDispatcherImpl;
+import org.opendaylight.netconf.client.mdsal.NetconfDeviceCommunicator;
+import org.opendaylight.netconf.client.mdsal.api.NetconfSessionPreferences;
+import org.opendaylight.netconf.client.mdsal.api.RemoteDevice;
 import org.opendaylight.netconf.nettyutil.handler.ssh.client.AsyncSshHandler;
-import org.opendaylight.netconf.sal.connect.api.RemoteDevice;
-import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCommunicator;
-import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
 import org.opendaylight.netconf.test.tool.TestToolUtils;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.CommitInput;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.EditConfigInput;
@@ -41,54 +37,42 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
-import org.w3c.dom.Node;
 import org.xml.sax.SAXException;
 
 public final class StressClient {
-
     private static final Logger LOG = LoggerFactory.getLogger(StressClient.class);
 
     static final QName COMMIT_QNAME = QName.create(CommitInput.QNAME, "commit");
-    public static final NetconfMessage COMMIT_MSG;
-
-    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 (final SAXException | IOException e) {
-            throw new ExceptionInInitializerError(e);
-        }
-    }
+    public static final NetconfMessage COMMIT_MSG = new NetconfMessage(readString("""
+        <rpc message-id="commit-batch" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
+            <commit/>
+        </rpc>"""));
 
     static final QName EDIT_QNAME = QName.create(EditConfigInput.QNAME, "edit-config");
-    static final org.w3c.dom.Document EDIT_CANDIDATE_BLUEPRINT;
-    static final org.w3c.dom.Document EDIT_RUNNING_BLUEPRINT;
-
-    static {
+    static final Document EDIT_CANDIDATE_BLUEPRINT = readString("""
+        <rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
+            <edit-config>
+                <target>
+                    <candidate/>
+                </target>
+                <default-operation>none</default-operation>
+                <config/>
+            </edit-config>
+        </rpc>""");
+    static final Document EDIT_RUNNING_BLUEPRINT  = readString("""
+        <rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
+            <edit-config>
+                <target>
+                    <running/>
+                </target>
+                <default-operation>none</default-operation>
+                <config/>
+            </edit-config>
+        </rpc>""");
+
+    private static Document readString(final String str) {
         try {
-            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>");
+            return XmlUtil.readXmlToDocument(str);
         } catch (SAXException | IOException e) {
             throw new ExceptionInInitializerError(e);
         }
@@ -101,85 +85,80 @@ public final class StressClient {
 
     private static Parameters params;
 
-    public static void main(final String[] args) {
+    private StressClient() {
+        // Hidden on purpose
+    }
 
-        params = parseArgs(args, Parameters.getParser());
+    public static void main(final String[] args) throws ExecutionException, InterruptedException, TimeoutException {
+        if (initParameters(args)) {
+            return;
+        }
         params.validate();
 
-        final ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) LoggerFactory
-            .getLogger(Logger.ROOT_LOGGER_NAME);
+        final var 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);
+        final var allPreparedMessages = new ArrayList<List<NetconfMessage>>(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.readString(params.editContent.toPath());
         } 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++) {
-            final List<NetconfMessage> preparedMessages = allPreparedMessages.get(i);
+            final var preparedMessages = allPreparedMessages.get(i);
             int padding = 0;
             if (i == threadAmount - 1) {
                 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));
             }
         }
 
-        final NioEventLoopGroup nioGroup = new NioEventLoopGroup();
-        final Timer timer = new HashedWheelTimer();
+        final var nioGroup = new NioEventLoopGroup();
+        final var timer = new HashedWheelTimer();
 
-        final NetconfClientDispatcherImpl netconfClientDispatcher = configureClientDispatcher(params, nioGroup, timer);
+        final var netconfClientDispatcher = configureClientDispatcher(nioGroup, timer);
 
-        final List<StressClientCallable> callables = new ArrayList<>(threadAmount);
-        for (final List<NetconfMessage> messages : allPreparedMessages) {
+        final var callables = new ArrayList<StressClientCallable>(threadAmount);
+        for (var messages : allPreparedMessages) {
             callables.add(new StressClientCallable(params, netconfClientDispatcher, messages));
         }
 
-        final ExecutorService executorService = Executors.newFixedThreadPool(threadAmount);
+        final var executorService = Executors.newFixedThreadPool(threadAmount);
 
         LOG.info("Starting stress test");
-        final Stopwatch started = Stopwatch.createStarted();
-        try {
-            final List<Future<Boolean>> futures = executorService.invokeAll(callables);
-            for (final Future<Boolean> future : futures) {
-                try {
-                    future.get(4L, TimeUnit.MINUTES);
-                } catch (ExecutionException | TimeoutException e) {
-                    throw new RuntimeException(e);
-                }
-            }
-            executorService.shutdownNow();
-        } catch (final InterruptedException e) {
-            throw new RuntimeException("Unable to execute requests", e);
+        final var sw = Stopwatch.createStarted();
+        final var futures = executorService.invokeAll(callables);
+        for (var future : futures) {
+            future.get(4L, TimeUnit.MINUTES);
         }
-        started.stop();
+        executorService.shutdownNow();
+        sw.stop();
 
-        LOG.info("FINISHED. Execution time: {}", started);
-        LOG.info("Requests per second: {}", (params.editCount * 1000.0 / started.elapsed(TimeUnit.MILLISECONDS)));
+        LOG.info("FINISHED. Execution time: {}", sw);
+        LOG.info("Requests per second: {}", params.editCount * 1000.0 / sw.elapsed(TimeUnit.MILLISECONDS));
 
         // Cleanup
         timer.stop();
@@ -205,16 +184,16 @@ public final class StressClient {
             // Insert message id where needed
             String specificEditContent = editContentString.replaceAll(MSG_ID_PLACEHOLDER_REGEX, Integer.toString(id));
 
-            final StringBuilder stringBuilder = new StringBuilder(specificEditContent);
-            int idx = stringBuilder.indexOf(PHYS_ADDR_PLACEHOLDER);
+            final var sb = new StringBuilder(specificEditContent);
+            int idx = sb.indexOf(PHYS_ADDR_PLACEHOLDER);
             while (idx != -1) {
-                stringBuilder.replace(idx, idx + PHYS_ADDR_PLACEHOLDER.length(), TestToolUtils.getMac(macStart++));
-                idx = stringBuilder.indexOf(PHYS_ADDR_PLACEHOLDER);
+                sb.replace(idx, idx + PHYS_ADDR_PLACEHOLDER.length(), TestToolUtils.getMac(macStart++));
+                idx = sb.indexOf(PHYS_ADDR_PLACEHOLDER);
             }
-            specificEditContent = stringBuilder.toString();
+            specificEditContent = sb.toString();
 
             editContentElement = XmlUtil.readXmlToElement(specificEditContent);
-            final Node config = ((Element) msg.getDocumentElement().getElementsByTagName("edit-config").item(0))
+            final var config = ((Element) msg.getDocumentElement().getElementsByTagName("edit-config").item(0))
                     .getElementsByTagName("config").item(0);
             config.appendChild(msg.importNode(editContentElement, true));
         } catch (final IOException | SAXException e) {
@@ -224,40 +203,32 @@ public final class StressClient {
         return netconfMessage;
     }
 
-    private static NetconfClientDispatcherImpl configureClientDispatcher(final Parameters params,
-            final NioEventLoopGroup nioGroup, final Timer timer) {
-        final NetconfClientDispatcherImpl netconfClientDispatcher;
-        if (params.exi) {
-            if (params.legacyFraming) {
-                netconfClientDispatcher = ConfigurableClientDispatcher.createLegacyExi(nioGroup, nioGroup, timer);
-            } else {
-                netconfClientDispatcher = ConfigurableClientDispatcher.createChunkedExi(nioGroup, nioGroup, timer);
-            }
-        } else {
-            if (params.legacyFraming) {
-                netconfClientDispatcher = ConfigurableClientDispatcher.createLegacy(nioGroup, nioGroup, timer);
-            } else {
-                netconfClientDispatcher = ConfigurableClientDispatcher.createChunked(nioGroup, nioGroup, timer);
-            }
-        }
-        return netconfClientDispatcher;
-    }
-
-    private static Parameters parseArgs(final String[] args, final ArgumentParser parser) {
-        final Parameters opt = new Parameters();
+    @SuppressFBWarnings(value = "DM_EXIT", justification = "Exit from CLI with error without throwing an exception")
+    private static boolean initParameters(final String[] args) {
+        final var parser = Parameters.getParser();
+        params = new Parameters();
         try {
-            parser.parseArgs(args, opt);
-            return opt;
-        } catch (final ArgumentParserException e) {
+            parser.parseArgs(args, args);
+        } catch (ArgumentParserException e) {
             parser.handleError(e);
+            System.exit(1);
+            return true;
         }
+        return false;
+    }
 
-        System.exit(1);
-        return null;
+    @Deprecated
+    private static NetconfClientDispatcherImpl configureClientDispatcher(final NioEventLoopGroup nioGroup,
+            final Timer timer) {
+        if (params.exi) {
+            return params.legacyFraming ? ConfigurableClientDispatcher.createLegacyExi(nioGroup, nioGroup, timer)
+                : ConfigurableClientDispatcher.createChunkedExi(nioGroup, nioGroup, timer);
+        }
+        return params.legacyFraming ? ConfigurableClientDispatcher.createLegacy(nioGroup, nioGroup, timer)
+            : ConfigurableClientDispatcher.createChunked(nioGroup, nioGroup, timer);
     }
 
-    static class LoggingRemoteDevice
-            implements RemoteDevice<NetconfSessionPreferences, NetconfMessage, NetconfDeviceCommunicator> {
+    static class LoggingRemoteDevice implements RemoteDevice<NetconfDeviceCommunicator> {
         @Override
         public void onRemoteSessionUp(final NetconfSessionPreferences remoteSessionCapabilities,
                                       final NetconfDeviceCommunicator netconfDeviceCommunicator) {
@@ -269,15 +240,9 @@ public final class StressClient {
             LOG.info("Session down");
         }
 
-        @Override
-        public void onRemoteSessionFailed(final Throwable throwable) {
-            LOG.info("Session failed");
-        }
-
         @Override
         public void onNotification(final NetconfMessage notification) {
-            LOG.info("Notification received: {}", notification.toString());
+            LOG.info("Notification received: {}", notification);
         }
     }
-
 }