Fix testtool on input modules without revision 38/79138/3
authorJaroslav Tóth <xtothj@gmail.com>
Wed, 2 Jan 2019 14:13:20 +0000 (15:13 +0100)
committerJaroslav Tóth <jtoth@frinx.io>
Thu, 10 Jan 2019 11:46:56 +0000 (12:46 +0100)
- According to RFC 7950 revision number in module is not
  mandatory (only recommended) - currently Netconf Testtool fails
  on such modules with NullPointerException because it cannot find
  revision line pattern.
- Revision number doesn't have to be placed in input YANG schema -
  the filename is kept unmodified.
- Regex pattern that matches date in revision is also supplemented
  by revision keyword and optional double-apostrophes - there could
  be a collision with date in include statement.
- YANG module capability model is modified so the revision can be
  null - reurning of Optional.absent().
- DummyMonitoringService sets empty string in place of null
  revision numbers.

Change-Id: I868d39c111d788a0de86251da10f76a448f47de4
Signed-off-by: Jaroslav Tóth <jtoth@frinx.io>
netconf/netconf-api/src/main/java/org/opendaylight/netconf/api/capability/YangModuleCapability.java
netconf/tools/netconf-testtool/src/main/java/org/opendaylight/netconf/test/tool/DummyMonitoringService.java
netconf/tools/netconf-testtool/src/main/java/org/opendaylight/netconf/test/tool/TesttoolParameters.java

index 6546f660b3b6f7630a7de073570cad72c6788877..68b8ee35c7e7914c243754d48e60f41d89bb5611 100644 (file)
@@ -58,6 +58,6 @@ public final class YangModuleCapability extends BasicCapability {
 
     @Override
     public Optional<String> getRevision() {
-        return Optional.of(revision);
+        return Optional.fromNullable(revision);
     }
 }
index d6109639eb4dd5437281ebbe0158bbfb05c58bd0..973a059586f171b8c7e2d977ae97f8f4088bfde2 100644 (file)
@@ -50,10 +50,10 @@ public class DummyMonitoringService implements NetconfMonitoringService {
                     .setIdentifier(capability.getModuleName().get())
                     .setNamespace(new Uri(capability.getModuleNamespace().get()))
                     .setFormat(Yang.class)
-                    .setVersion(capability.getRevision().get())
+                    .setVersion(capability.getRevision().or(""))
                     .setLocation(Collections.singletonList(new Location(Enumeration.NETCONF)))
                     .withKey(new SchemaKey(Yang.class, capability.getModuleName().get(),
-                        capability.getRevision().get()))
+                        capability.getRevision().or("")))
                     .build();
         }
     };
index 9115621bd348b109a9cd1ceee31cb7022f10e839..6522de76751fb342a0de420499d11b1bb13b88c3 100644 (file)
@@ -27,6 +27,7 @@ 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;
@@ -47,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")
@@ -313,12 +314,12 @@ public class TesttoolParameters {
                 if (!matcher.matches()) {
                     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);
@@ -327,7 +328,7 @@ public class TesttoolParameters {
                             final String correctName = moduleName + "@" + revision + ".yang";
                             final File correctNameFile = new File(correctName);
                             if (!file.renameTo(correctNameFile)) {
-                                System.err.println("Failed to rename " + file);
+                                throw new IllegalStateException("Failed to rename '%s'." + file);
                             }
                         }
                     } catch (final IOException e) {