Use Files.readString()
[netconf.git] / netconf / tools / netconf-testtool / src / main / java / org / opendaylight / netconf / test / tool / client / stress / StressClient.java
index 763a7b8b67d7c4ad5624edb83a67478027c27ea5..0a61735b26263407b0029089dfb307c508d2487a 100644 (file)
@@ -5,18 +5,16 @@
  * 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;
@@ -27,13 +25,13 @@ 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.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;
@@ -47,50 +45,39 @@ import org.xml.sax.SAXException;
 
 @SuppressFBWarnings("DM_EXIT")
 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);
         }
@@ -117,28 +104,27 @@ public final class StressClient {
         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.readString(params.editContent.toPath());
         } catch (final IOException e) {
             throw new IllegalArgumentException("Cannot read content of " + params.editContent, e);
         }
@@ -150,7 +136,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));
             }
         }
@@ -175,12 +161,12 @@ public final class StressClient {
                 try {
                     future.get(4L, TimeUnit.MINUTES);
                 } catch (ExecutionException | TimeoutException e) {
-                    throw new RuntimeException(e);
+                    throw new IllegalStateException(e);
                 }
             }
             executorService.shutdownNow();
         } catch (final InterruptedException e) {
-            throw new RuntimeException("Unable to execute requests", e);
+            throw new IllegalStateException("Unable to execute requests", e);
         }
         started.stop();
 
@@ -239,12 +225,10 @@ public final class StressClient {
             } else {
                 netconfClientDispatcher = ConfigurableClientDispatcher.createChunkedExi(nioGroup, nioGroup, timer);
             }
+        } else if (params.legacyFraming) {
+            netconfClientDispatcher = ConfigurableClientDispatcher.createLegacy(nioGroup, nioGroup, timer);
         } else {
-            if (params.legacyFraming) {
-                netconfClientDispatcher = ConfigurableClientDispatcher.createLegacy(nioGroup, nioGroup, timer);
-            } else {
-                netconfClientDispatcher = ConfigurableClientDispatcher.createChunked(nioGroup, nioGroup, timer);
-            }
+            netconfClientDispatcher = ConfigurableClientDispatcher.createChunked(nioGroup, nioGroup, timer);
         }
         return netconfClientDispatcher;
     }
@@ -262,8 +246,7 @@ public final class StressClient {
         return null;
     }
 
-    static class LoggingRemoteDevice
-            implements RemoteDevice<NetconfSessionPreferences, NetconfMessage, NetconfDeviceCommunicator> {
+    static class LoggingRemoteDevice implements RemoteDevice<NetconfDeviceCommunicator> {
         @Override
         public void onRemoteSessionUp(final NetconfSessionPreferences remoteSessionCapabilities,
                                       final NetconfDeviceCommunicator netconfDeviceCommunicator) {
@@ -275,15 +258,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);
         }
     }
-
 }