Merge "Bug 7172 - Correct error-info for missing-attribute errors"
[netconf.git] / netconf / tools / netconf-testtool / src / main / java / org / opendaylight / netconf / test / tool / TesttoolParameters.java
index f2f3e09173361561722adfff9a89153bfea78183..f08daee52008fcd3e8ba6e0d8113cbffd8f4fef1 100644 (file)
@@ -10,18 +10,24 @@ package org.opendaylight.netconf.test.tool;
 
 import static com.google.common.base.Preconditions.checkArgument;
 
-import com.google.common.base.Charsets;
 import com.google.common.base.Preconditions;
 import com.google.common.io.CharStreams;
 import com.google.common.io.Files;
+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.Iterator;
 import java.util.List;
+import java.util.StringJoiner;
 import java.util.concurrent.TimeUnit;
+import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 import net.sourceforge.argparse4j.ArgumentParsers;
 import net.sourceforge.argparse4j.annotation.Arg;
@@ -35,6 +41,8 @@ public class TesttoolParameters {
     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 Pattern DATE_PATTERN = Pattern.compile("(\\d{4}-\\d{2}-\\d{2})");
 
     private static final String RESOURCE = "/config-template.json";
     @Arg(dest = "edit-content")
@@ -86,6 +94,8 @@ public class TesttoolParameters {
 
     @Arg(dest = "thread-pool-size")
     public int threadPoolSize;
+    @Arg(dest = "rpc-config")
+    public File rpcConfig;
 
     static ArgumentParser getParser() {
         final ArgumentParser parser = ArgumentParsers.newArgumentParser("netconf testtool");
@@ -226,6 +236,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;
     }
@@ -272,7 +287,7 @@ 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(":"));
+            final String[] parts = controllerDestination.split(Pattern.quote(":"));
             Preconditions.checkArgument(Integer.parseInt(parts[1]) > 0, "Port =< 0");
         }
 
@@ -284,6 +299,41 @@ public class TesttoolParameters {
             checkArgument(schemasDir.exists(), "Schemas dir has to exist");
             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());
+            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));
+                        String line = reader.readLine();
+                        while (!DATE_PATTERN.matcher(line).find()) {
+                            line = reader.readLine();
+                        }
+                        final Matcher m = DATE_PATTERN.matcher(line);
+
+                        if (m.find()) {
+                            String moduleName = file.getAbsolutePath();
+                            if (file.getName().endsWith(".yang")) {
+                                moduleName = moduleName.substring(0, moduleName.length() - 5);
+                            }
+                            final String revision = m.group(1);
+                            final String correctName = moduleName + "@" + revision + ".yang";
+                            final File correctNameFile = new File(correctName);
+                            file.renameTo(correctNameFile);
+                        }
+
+                    } catch (final IOException e) {
+                        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");
         }
     }
 
@@ -291,9 +341,9 @@ public class TesttoolParameters {
         final String editContentString;
         try {
             if (stream == null) {
-                editContentString = Files.toString(editContent, Charsets.UTF_8);
+                editContentString = Files.toString(editContent, StandardCharsets.UTF_8);
             } else {
-                editContentString = CharStreams.toString(new InputStreamReader(stream, Charsets.UTF_8));
+                editContentString = CharStreams.toString(new InputStreamReader(stream, StandardCharsets.UTF_8));
             }
         } catch (final IOException e) {
             throw new IllegalArgumentException("Cannot read content of " + editContent);
@@ -364,7 +414,7 @@ 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);
@@ -404,4 +454,23 @@ public class TesttoolParameters {
         }
         return payloads;
     }
+
+    @Override
+    public String 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";
+        }
+    }
 }