Merge "Remove use of deprecated Guava methods from testtool"
[netconf.git] / netconf / tools / netconf-testtool / src / main / java / org / opendaylight / netconf / test / tool / TesttoolParameters.java
index 79e849a32428e5046f1d6173a717cf5bc1bffa90..5d8b0bf3abb1830c0d1051ab0dd926bdcd9777be 100644 (file)
@@ -13,17 +13,21 @@ import static com.google.common.base.Preconditions.checkArgument;
 import com.google.common.base.Preconditions;
 import com.google.common.io.CharStreams;
 import com.google.common.io.Files;
+import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileReader;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
+import java.lang.reflect.Field;
 import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
+import java.util.StringJoiner;
 import java.util.concurrent.TimeUnit;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
@@ -32,14 +36,17 @@ import net.sourceforge.argparse4j.annotation.Arg;
 import net.sourceforge.argparse4j.inf.ArgumentParser;
 import net.sourceforge.argparse4j.inf.ArgumentParserException;
 
+@SuppressFBWarnings({"DM_EXIT", "DM_DEFAULT_ENCODING"})
 public class TesttoolParameters {
 
     private static final String HOST_KEY = "{HOST}";
     private static final String PORT_KEY = "{PORT}";
     private static final String TCP_ONLY = "{TCP_ONLY}";
     private static final String ADDRESS_PORT = "{ADDRESS:PORT}";
-    private static final String dest = "http://{ADDRESS:PORT}/restconf/config/network-topology:network-topology/topology/topology-netconf/";
-    private static final Pattern YANG_FILENAME_PATTERN = Pattern.compile("(?<name>.*)@(?<revision>\\d{4}-\\d{2}-\\d{2})\\.yang");
+    private static final String DEST =
+        "http://{ADDRESS:PORT}/restconf/config/network-topology:network-topology/topology/topology-netconf/";
+    private static final Pattern YANG_FILENAME_PATTERN = Pattern
+        .compile("(?<name>.*)@(?<revision>\\d{4}-\\d{2}-\\d{2})\\.yang");
     private static final Pattern DATE_PATTERN = Pattern.compile("(\\d{4}-\\d{2}-\\d{2})");
 
     private static final String RESOURCE = "/config-template.json";
@@ -86,13 +93,14 @@ public class TesttoolParameters {
     @Arg(dest = "time-out")
     public long timeOut;
     private InputStream stream;
-
     @Arg(dest = "ip")
     public String ip;
-
     @Arg(dest = "thread-pool-size")
     public int threadPoolSize;
+    @Arg(dest = "rpc-config")
+    public File rpcConfig;
 
+    @SuppressWarnings("checkstyle:lineLength")
     static ArgumentParser getParser() {
         final ArgumentParser parser = ArgumentParsers.newArgumentParser("netconf testtool");
 
@@ -104,7 +112,7 @@ public class TesttoolParameters {
 
         parser.addArgument("--async-requests")
                 .type(Boolean.class)
-                .setDefault(false)
+                .setDefault(Boolean.FALSE)
                 .dest("async");
 
         parser.addArgument("--thread-amount")
@@ -116,8 +124,8 @@ public class TesttoolParameters {
         parser.addArgument("--throttle")
                 .type(Integer.class)
                 .setDefault(5000)
-                .help("Maximum amount of async requests that can be open at a time, " +
-                        "with mutltiple threads this gets divided among all threads")
+                .help("Maximum amount of async requests that can be open at a time, "
+                        "with mutltiple threads this gets divided among all threads")
                 .dest("throttle");
 
         parser.addArgument("--auth")
@@ -127,10 +135,10 @@ public class TesttoolParameters {
 
         parser.addArgument("--controller-destination")
                 .type(String.class)
-                .help("Ip address and port of controller. Must be in following format <ip>:<port> " +
-                        "if available it will be used for spawning netconf connectors via topology configuration as " +
-                        "a part of URI. Example (http://<controller destination>/restconf/config/network-topology:network-topology/topology/topology-netconf/node/<node-id>)" +
-                        "otherwise it will just start simulated devices and skip the execution of PUT requests")
+                .help("Ip address and port of controller. Must be in following format <ip>:<port> "
+                        + "if available it will be used for spawning netconf connectors via topology configuration as "
+                        + "a part of URI. Example (http://<controller destination>/restconf/config/network-topology:network-topology/topology/topology-netconf/node/<node-id>)"
+                        "otherwise it will just start simulated devices and skip the execution of PUT requests")
                 .dest("controller-destination");
 
         parser.addArgument("--device-count")
@@ -191,25 +199,25 @@ public class TesttoolParameters {
 
         parser.addArgument("--ssh")
                 .type(Boolean.class)
-                .setDefault(true)
+                .setDefault(Boolean.TRUE)
                 .help("Whether to use ssh for transport or just pure tcp")
                 .dest("ssh");
 
         parser.addArgument("--exi")
                 .type(Boolean.class)
-                .setDefault(true)
+                .setDefault(Boolean.TRUE)
                 .help("Whether to use exi to transport xml content")
                 .dest("exi");
 
         parser.addArgument("--debug")
                 .type(Boolean.class)
-                .setDefault(false)
+                .setDefault(Boolean.FALSE)
                 .help("Whether to use debug log level instead of INFO")
                 .dest("debug");
 
         parser.addArgument("--md-sal")
                 .type(Boolean.class)
-                .setDefault(false)
+                .setDefault(Boolean.FALSE)
                 .help("Whether to use md-sal datastore instead of default simulated datastore.")
                 .dest("md-sal");
 
@@ -222,9 +230,9 @@ public class TesttoolParameters {
         parser.addArgument("-ip")
                 .type(String.class)
                 .setDefault("0.0.0.0")
-                .help("Ip address which will be used for creating a socket address." +
-                        "It can either be a machine name, such as " +
-                        "java.sun.com, or a textual representation of its IP address.")
+                .help("Ip address which will be used for creating a socket address."
+                        + "It can either be a machine name, such as "
+                        "java.sun.com, or a textual representation of its IP address.")
                 .dest("ip");
 
         parser.addArgument("--thread-pool-size")
@@ -232,6 +240,11 @@ public class TesttoolParameters {
                 .setDefault(8)
                 .help("The number of threads to keep in the pool, when creating a device simulator. Even if they are idle.")
                 .dest("thread-pool-size");
+        parser.addArgument("--rpc-config")
+                .type(File.class)
+                .help("Rpc config file. It can be used to define custom rpc behavior, or override the default one."
+                    + "Usable for testing buggy device behavior.")
+                .dest("rpc-config");
 
         return parser;
     }
@@ -268,6 +281,7 @@ public class TesttoolParameters {
         return payloadBuilder.toString();
     }
 
+    @SuppressWarnings("checkstyle:regexpSinglelineJava")
     void validate() {
         if (editContent == null) {
             stream = TesttoolParameters.class.getResourceAsStream(RESOURCE);
@@ -277,8 +291,9 @@ public class TesttoolParameters {
         }
 
         if (controllerDestination != null) {
-            Preconditions.checkArgument(controllerDestination.contains(":"), "Controller Destination needs to be in a following format <ip>:<port>");
-            String[] parts = controllerDestination.split(Pattern.quote(":"));
+            Preconditions.checkArgument(controllerDestination.contains(":"),
+                "Controller Destination needs to be in a following format <ip>:<port>");
+            final String[] parts = controllerDestination.split(Pattern.quote(":"));
             Preconditions.checkArgument(Integer.parseInt(parts[1]) > 0, "Port =< 0");
         }
 
@@ -291,18 +306,17 @@ public class TesttoolParameters {
             checkArgument(schemasDir.isDirectory(), "Schemas dir has to be a directory");
             checkArgument(schemasDir.canRead(), "Schemas dir has to be readable");
 
-            final List<File> files = Arrays.asList(schemasDir.listFiles());
+            final File[] filesArray = schemasDir.listFiles();
+            final List<File> files = filesArray != null ? Arrays.asList(filesArray) : Collections.emptyList();
             for (final File file : files) {
                 final Matcher matcher = YANG_FILENAME_PATTERN.matcher(file.getName());
                 if (!matcher.matches()) {
-                    BufferedReader reader;
-                    try {
-                        reader = new BufferedReader(new FileReader(file));
+                    try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
                         String line = reader.readLine();
                         while (!DATE_PATTERN.matcher(line).find()) {
                             line = reader.readLine();
                         }
-                        Matcher m = DATE_PATTERN.matcher(line);
+                        final Matcher m = DATE_PATTERN.matcher(line);
 
                         if (m.find()) {
                             String moduleName = file.getAbsolutePath();
@@ -310,32 +324,40 @@ public class TesttoolParameters {
                                 moduleName = moduleName.substring(0, moduleName.length() - 5);
                             }
                             final String revision = m.group(1);
-                            String correctName = moduleName + "@" + revision + ".yang";
-                            File correctNameFile = new File(correctName);
-                            file.renameTo(correctNameFile);
+                            final String correctName = moduleName + "@" + revision + ".yang";
+                            final File correctNameFile = new File(correctName);
+                            if (!file.renameTo(correctNameFile)) {
+                                System.err.println("Failed to rename " + file);
+                            }
                         }
-
-                    } catch (IOException e) {
+                    } catch (final IOException e) {
+                        // print error to console (test tool is running from console)
                         e.printStackTrace();
                     }
                 }
             }
         }
+        if (rpcConfig != null) {
+            checkArgument(rpcConfig.exists(), "Rpc config file has to exist");
+            checkArgument(!rpcConfig.isDirectory(), "Rpc config file can't be a directory");
+            checkArgument(rpcConfig.canRead(), "Rpc config file to be readable");
+        }
     }
 
     public ArrayList<ArrayList<Execution.DestToPayload>> getThreadsPayloads(final List<Integer> openDevices) {
         final String editContentString;
         try {
             if (stream == null) {
-                editContentString = Files.toString(editContent, StandardCharsets.UTF_8);
+                editContentString = Files.asCharSource(editContent, StandardCharsets.UTF_8).read();
             } else {
                 editContentString = CharStreams.toString(new InputStreamReader(stream, StandardCharsets.UTF_8));
             }
         } catch (final IOException e) {
-            throw new IllegalArgumentException("Cannot read content of " + editContent);
+            throw new IllegalArgumentException("Cannot read content of " + editContent, e);
         }
 
-        int from, to;
+        int from;
+        int to;
         Iterator<Integer> iterator;
 
         final ArrayList<ArrayList<Execution.DestToPayload>> allThreadsPayloads = new ArrayList<>();
@@ -343,24 +365,28 @@ public class TesttoolParameters {
 
             final int batchedRequests = openDevices.size() / generateConfigBatchSize;
             final int batchedRequestsPerThread = batchedRequests / threadAmount;
-            final int leftoverBatchedRequests = (batchedRequests) % threadAmount;
-            final int leftoverRequests = openDevices.size() - (batchedRequests * generateConfigBatchSize);
+            final int leftoverBatchedRequests = batchedRequests % threadAmount;
+            final int leftoverRequests = openDevices.size() - batchedRequests * generateConfigBatchSize;
 
-            final StringBuilder destBuilder = new StringBuilder(dest);
-            destBuilder.replace(destBuilder.indexOf(ADDRESS_PORT), destBuilder.indexOf(ADDRESS_PORT) + ADDRESS_PORT.length(), controllerDestination);
+            final StringBuilder destBuilder = new StringBuilder(DEST);
+            destBuilder.replace(destBuilder.indexOf(ADDRESS_PORT),
+                destBuilder.indexOf(ADDRESS_PORT) + ADDRESS_PORT.length(),
+                controllerDestination);
 
             for (int l = 0; l < threadAmount; l++) {
-                from = l * (batchedRequests * batchedRequestsPerThread);
-                to = from + (batchedRequests * batchedRequestsPerThread);
+                from = l * batchedRequests * batchedRequestsPerThread;
+                to = from + batchedRequests * batchedRequestsPerThread;
                 iterator = openDevices.subList(from, to).iterator();
-                allThreadsPayloads.add(createBatchedPayloads(batchedRequestsPerThread, iterator, editContentString, destBuilder.toString()));
+                allThreadsPayloads.add(createBatchedPayloads(batchedRequestsPerThread, iterator, editContentString,
+                    destBuilder.toString()));
             }
             ArrayList<Execution.DestToPayload> payloads = null;
             if (leftoverBatchedRequests > 0) {
-                from = threadAmount * (batchedRequests * batchedRequestsPerThread);
-                to = from + (batchedRequests * batchedRequestsPerThread);
+                from = threadAmount * batchedRequests * batchedRequestsPerThread;
+                to = from + batchedRequests * batchedRequestsPerThread;
                 iterator = openDevices.subList(from, to).iterator();
-                payloads = createBatchedPayloads(leftoverBatchedRequests, iterator, editContentString, destBuilder.toString());
+                payloads = createBatchedPayloads(leftoverBatchedRequests, iterator, editContentString,
+                    destBuilder.toString());
             }
             String payload = "";
 
@@ -368,7 +394,8 @@ public class TesttoolParameters {
                 from = openDevices.size() - leftoverRequests;
                 to = openDevices.size();
                 iterator = openDevices.subList(from, to).iterator();
-                final StringBuilder payloadBuilder = new StringBuilder(prepareMessage(iterator.next(), editContentString));
+                final StringBuilder payloadBuilder = new StringBuilder(
+                    prepareMessage(iterator.next(), editContentString));
                 payload += modifyMessage(payloadBuilder, j, leftoverRequests);
             }
             if (leftoverRequests > 0 || leftoverBatchedRequests > 0) {
@@ -390,7 +417,7 @@ public class TesttoolParameters {
             }
 
             if (leftoverRequests > 0) {
-                from = (threadAmount) * requestPerThreads;
+                from = threadAmount * requestPerThreads;
                 to = from + leftoverRequests;
                 iterator = openDevices.subList(from, to).iterator();
                 allThreadsPayloads.add(createPayloads(iterator, editContentString));
@@ -400,74 +427,74 @@ public class TesttoolParameters {
     }
 
     private String prepareMessage(final int openDevice, final String editContentString) {
-        StringBuilder messageBuilder = new StringBuilder(editContentString);
+        final StringBuilder messageBuilder = new StringBuilder(editContentString);
 
         if (editContentString.contains(HOST_KEY)) {
-            messageBuilder.replace(messageBuilder.indexOf(HOST_KEY), messageBuilder.indexOf(HOST_KEY) + HOST_KEY.length(), generateConfigsAddress);
+            messageBuilder.replace(messageBuilder.indexOf(HOST_KEY),
+                messageBuilder.indexOf(HOST_KEY) + HOST_KEY.length(),
+                generateConfigsAddress);
         }
         if (editContentString.contains(PORT_KEY)) {
-            while (messageBuilder.indexOf(PORT_KEY) != -1)
-                messageBuilder.replace(messageBuilder.indexOf(PORT_KEY), messageBuilder.indexOf(PORT_KEY) + PORT_KEY.length(), Integer.toString(openDevice));
+            while (messageBuilder.indexOf(PORT_KEY) != -1) {
+                messageBuilder.replace(messageBuilder.indexOf(PORT_KEY),
+                    messageBuilder.indexOf(PORT_KEY) + PORT_KEY.length(),
+                    Integer.toString(openDevice));
+            }
         }
         if (editContentString.contains(TCP_ONLY)) {
-            messageBuilder.replace(messageBuilder.indexOf(TCP_ONLY), messageBuilder.indexOf(TCP_ONLY) + TCP_ONLY.length(), Boolean.toString(!ssh));
+            messageBuilder.replace(messageBuilder.indexOf(TCP_ONLY),
+                messageBuilder.indexOf(TCP_ONLY) + TCP_ONLY.length(),
+                Boolean.toString(!ssh));
         }
         return messageBuilder.toString();
     }
 
-    private ArrayList<Execution.DestToPayload> createPayloads(final Iterator<Integer> openDevices, final String editContentString) {
+    private ArrayList<Execution.DestToPayload> createPayloads(final Iterator<Integer> openDevices,
+                                                              final String editContentString) {
         final ArrayList<Execution.DestToPayload> payloads = new ArrayList<>();
 
         while (openDevices.hasNext()) {
-            final StringBuilder destBuilder = new StringBuilder(dest);
-            destBuilder.replace(destBuilder.indexOf(ADDRESS_PORT), destBuilder.indexOf(ADDRESS_PORT) + ADDRESS_PORT.length(), controllerDestination);
-            payloads.add(new Execution.DestToPayload(destBuilder.toString(), prepareMessage(openDevices.next(), editContentString)));
+            final StringBuilder destBuilder = new StringBuilder(DEST);
+            destBuilder.replace(destBuilder.indexOf(ADDRESS_PORT),
+                destBuilder.indexOf(ADDRESS_PORT) + ADDRESS_PORT.length(), controllerDestination);
+            payloads.add(new Execution.DestToPayload(
+                destBuilder.toString(), prepareMessage(openDevices.next(), editContentString)));
         }
         return payloads;
     }
 
-    private ArrayList<Execution.DestToPayload> createBatchedPayloads(final int batchedRequestsCount, final Iterator<Integer> openDevices, final String editContentString,
-                                                                     final String destination) {
+    private ArrayList<Execution.DestToPayload> createBatchedPayloads(final int batchedRequestsCount,
+            final Iterator<Integer> openDevices, final String editContentString, final String destination) {
         final ArrayList<Execution.DestToPayload> payloads = new ArrayList<>();
 
         for (int i = 0; i < batchedRequestsCount; i++) {
-            String payload = "";
+            StringBuilder payload = new StringBuilder();
             for (int j = 0; j < generateConfigBatchSize; j++) {
-                final StringBuilder payloadBuilder = new StringBuilder(prepareMessage(openDevices.next(), editContentString));
-                payload += modifyMessage(payloadBuilder, j, generateConfigBatchSize);
+                final StringBuilder payloadBuilder = new StringBuilder(
+                    prepareMessage(openDevices.next(), editContentString));
+                payload.append(modifyMessage(payloadBuilder, j, generateConfigBatchSize));
             }
-            payloads.add(new Execution.DestToPayload(destination, payload));
+            payloads.add(new Execution.DestToPayload(destination, payload.toString()));
         }
         return payloads;
     }
 
-    //TODO This may be more scalable enumerating parameters via reflection
     @Override
     public String toString() {
-        StringBuffer params = new StringBuffer("TesttoolParameters{");
-        params.append("edit-content='").append(editContent).append('\'');
-        params.append(", async='").append(async).append('\'');
-        params.append(", thread-amount='").append(threadAmount).append('\'');
-        params.append(", throttle='").append(throttle).append('\'');
-        params.append(", auth='").append(auth).append('\'');
-        params.append(", controller-destination='").append(controllerDestination).append('\'');
-        params.append(", schemas-dir='").append(schemasDir).append('\'');
-        params.append(", devices-count='").append(deviceCount).append('\'');
-        params.append(", devices-per-port='").append(devicesPerPort).append('\'');
-        params.append(", starting-port='").append(startingPort).append('\'');
-        params.append(", generate-config-connection-timeout='").append(generateConfigsTimeout).append('\'');
-        params.append(", generate-config-address='").append(generateConfigsAddress).append('\'');
-        params.append(", distro-folder='").append(distroFolder).append('\'');
-        params.append(", generate-configs-batch-size='").append(generateConfigBatchSize).append('\'');
-        params.append(", ssh='").append(ssh).append('\'');
-        params.append(", exi='").append(exi).append('\'');
-        params.append(", debug='").append(debug).append('\'');
-        params.append(", notification-file='").append(notificationFile).append('\'');
-        params.append(", md-sal='").append(mdSal).append('\'');
-        params.append(", initial-config-xml-file='").append(initialConfigXMLFile).append('\'');
-        params.append(", time-out='").append(timeOut).append('\'');
-        params.append('}');
-
-        return params.toString();
+        final List<Field> fields = Arrays.asList(this.getClass().getDeclaredFields());
+        final StringJoiner joiner = new StringJoiner(", \n", "TesttoolParameters{", "}\n");
+        fields.stream()
+                .filter(field -> field.getAnnotation(Arg.class) != null)
+                .map(this::getFieldString)
+                .forEach(joiner::add);
+        return joiner.toString();
+    }
+
+    private String getFieldString(final Field field) {
+        try {
+            return field.getName() + "='" + field.get(this) + "'";
+        } catch (final IllegalAccessException e) {
+            return field.getName() + "= UNKNOWN";
+        }
     }
 }