Improve validation for invalid files in schemas dir 92/99892/1
authorOleksandrZharov <Oleksandr.Zharov@pantheon.tech>
Wed, 2 Mar 2022 14:33:24 +0000 (15:33 +0100)
committerTomas Cere <tomas.cere@pantheon.tech>
Thu, 3 Mar 2022 12:51:53 +0000 (12:51 +0000)
Previous validation of schema files was not very specific if
there was problems with files. Now there is more direct message if
something wrong with yang files.

Removed logic that rename not yang files into .yang in schema dir.
We are storing yang models only in .yang files. Therefore all other
files should be rejected.

JIRA: NETCONF-707
Change-Id: Iad4b91fd534eef8ea134a9463de3dab618fe969e
Signed-off-by: OleksandrZharov <Oleksandr.Zharov@pantheon.tech>
(cherry picked from commit 0a1d0c2b779ae95ae8b0e67f02c820cb89402b30)

netconf/tools/netconf-testtool/src/main/java/org/opendaylight/netconf/test/tool/NetconfDeviceSimulator.java
netconf/tools/netconf-testtool/src/main/java/org/opendaylight/netconf/test/tool/TesttoolParameters.java

index 3cb2e19222e530ca1976f9d00bbc1b5552e191ff..0f157c52f685baa070c405c4ea9e5cf7c0105060 100644 (file)
@@ -330,7 +330,8 @@ public class NetconfDeviceSimulator implements Closeable {
             this.schemaContext = consumer.createEffectiveModelContextFactory()
                     .createEffectiveModelContext(loadedSources).get();
         } catch (final InterruptedException | ExecutionException e) {
-            throw new RuntimeException("Cannot parse schema context", e);
+            throw new RuntimeException("Cannot parse schema context. "
+                    + "Please read stack trace and check YANG files in schema directory.", e);
         }
 
         final Set<Capability> capabilities = new HashSet<>();
index e95fc6bb78dd4c12ab60873cd1962aca0f73a053..74965fba3b2cf36d018e0e4687dea409a610f8c4 100644 (file)
@@ -8,25 +8,15 @@
 package org.opendaylight.netconf.test.tool;
 
 import static com.google.common.base.Preconditions.checkArgument;
-import static com.google.common.base.Preconditions.checkState;
 
 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.lang.reflect.Field;
-import java.nio.charset.StandardCharsets;
-import java.nio.file.Files;
-import java.nio.file.Paths;
-import java.nio.file.StandardCopyOption;
 import java.util.Arrays;
 import java.util.Collections;
 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;
 import net.sourceforge.argparse4j.inf.ArgumentParser;
@@ -35,9 +25,6 @@ import org.opendaylight.yangtools.yang.common.YangConstants;
 
 @SuppressFBWarnings({"DM_EXIT"})
 public final class TesttoolParameters {
-    private static final Pattern YANG_FILENAME_PATTERN = Pattern
-            .compile("(?<name>.*)@(?<revision>\\d{4}-\\d{2}-\\d{2})\\.yang");
-    private static final Pattern REVISION_DATE_PATTERN = Pattern.compile("revision\\s+\"?(\\d{4}-\\d{2}-\\d{2})\"?");
 
     @Arg(dest = "async")
     public boolean async;
@@ -293,18 +280,9 @@ public final class TesttoolParameters {
             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()) {
-                    try {
-                        final String correctName = correctedName(file);
-                        if (correctName != null) {
-                            Files.move(file.toPath(), Paths.get(correctName), StandardCopyOption.ATOMIC_MOVE);
-                        }
-                    } catch (final IOException e) {
-                        // print error to console (test tool is running from console)
-                        e.printStackTrace();
-                    }
-                }
+                checkArgument(file.canRead(), "Files in schemas dir has to be readable");
+                checkArgument(file.getName().endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION),
+                        "Files in schemas dir has to be YANG files");
             }
         }
         if (rpcConfig != null) {
@@ -314,26 +292,6 @@ public final class TesttoolParameters {
         }
     }
 
-    private static String correctedName(final File file) throws IOException {
-        try (BufferedReader reader = new BufferedReader(new FileReader(file, StandardCharsets.UTF_8))) {
-            String line = reader.readLine();
-            while (line != null && !REVISION_DATE_PATTERN.matcher(line).find()) {
-                line = reader.readLine();
-            }
-            if (line != null) {
-                final Matcher m = REVISION_DATE_PATTERN.matcher(line);
-                checkState(m.find(), "Revision pattern %s did not match line %s", REVISION_DATE_PATTERN, line);
-                String moduleName = file.getAbsolutePath();
-                if (file.getName().endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION)) {
-                    moduleName = moduleName.substring(0, moduleName.length() - 5);
-                }
-
-                return moduleName + "@" + m.group(1) + YangConstants.RFC6020_YANG_FILE_EXTENSION;
-            }
-        }
-        return null;
-    }
-
     @Override
     public String toString() {
         final List<Field> fields = Arrays.asList(this.getClass().getDeclaredFields());