Merge "Remove a FindBugs suppression"
[netconf.git] / netconf / tools / netconf-testtool / src / main / java / org / opendaylight / netconf / test / tool / TesttoolParameters.java
index 775aa7670b0a55ac6eb222102a346ea79655657f..6522de76751fb342a0de420499d11b1bb13b88c3 100644 (file)
@@ -13,6 +13,7 @@ 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;
@@ -23,8 +24,10 @@ 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.Objects;
 import java.util.StringJoiner;
 import java.util.concurrent.TimeUnit;
 import java.util.regex.Matcher;
@@ -34,6 +37,7 @@ 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}";
@@ -44,7 +48,7 @@ public class TesttoolParameters {
         "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 Pattern REVISION_DATE_PATTERN = Pattern.compile("revision\\s+\"?(\\d{4}-\\d{2}-\\d{2})\"?");
 
     private static final String RESOURCE = "/config-template.json";
     @Arg(dest = "edit-content")
@@ -78,7 +82,7 @@ public class TesttoolParameters {
     @Arg(dest = "ssh")
     public boolean ssh;
     @Arg(dest = "exi")
-    public boolean exi;
+    public boolean exi = true;
     @Arg(dest = "debug")
     public boolean debug;
     @Arg(dest = "notification-file")
@@ -109,7 +113,7 @@ public class TesttoolParameters {
 
         parser.addArgument("--async-requests")
                 .type(Boolean.class)
-                .setDefault(false)
+                .setDefault(Boolean.FALSE)
                 .dest("async");
 
         parser.addArgument("--thread-amount")
@@ -196,25 +200,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");
 
@@ -303,20 +307,19 @@ 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()) {
-                    final 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()) {
+                        while (Objects.nonNull(line) && !REVISION_DATE_PATTERN.matcher(line).find()) {
                             line = reader.readLine();
                         }
-                        final Matcher m = DATE_PATTERN.matcher(line);
-
-                        if (m.find()) {
+                        if (Objects.nonNull(line)) {
+                            final Matcher m = REVISION_DATE_PATTERN.matcher(line);
+                            Preconditions.checkState(m.find());
                             String moduleName = file.getAbsolutePath();
                             if (file.getName().endsWith(".yang")) {
                                 moduleName = moduleName.substring(0, moduleName.length() - 5);
@@ -324,7 +327,9 @@ public class TesttoolParameters {
                             final String revision = m.group(1);
                             final String correctName = moduleName + "@" + revision + ".yang";
                             final File correctNameFile = new File(correctName);
-                            file.renameTo(correctNameFile);
+                            if (!file.renameTo(correctNameFile)) {
+                                throw new IllegalStateException("Failed to rename '%s'." + file);
+                            }
                         }
                     } catch (final IOException e) {
                         // print error to console (test tool is running from console)
@@ -344,12 +349,12 @@ public class TesttoolParameters {
         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;
@@ -361,8 +366,8 @@ 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),
@@ -370,16 +375,16 @@ public class TesttoolParameters {
                 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()));
             }
             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());
@@ -413,7 +418,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));
@@ -464,13 +469,13 @@ public class TesttoolParameters {
         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);
+                payload.append(modifyMessage(payloadBuilder, j, generateConfigBatchSize));
             }
-            payloads.add(new Execution.DestToPayload(destination, payload));
+            payloads.add(new Execution.DestToPayload(destination, payload.toString()));
         }
         return payloads;
     }