BUG-8123: be better at guessing identifiers 58/54958/3
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 12 Apr 2017 13:54:23 +0000 (15:54 +0200)
committerRobert Varga <nite@hq.sk>
Tue, 18 Apr 2017 08:17:01 +0000 (08:17 +0000)
Given an URI we need to attempt to interpret it to improve our
guess of the module name and revision.

Change-Id: I4d74b06fb9f949c538911d215b6d90a383560f1f
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit 8bfdcd0056b08db284a87b55913691c0187c34bb)
(cherry picked from commit 9f390185b2869fd215a7cb1ee86b1760506d8662)

yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/repo/YangTextSchemaContextResolver.java

index 7880f38b37cfc3aaf8467bc4804d25d9d8214ec4..ec4f65b9a321097123dfb5a9fc1ee5350956be15 100644 (file)
@@ -163,7 +163,7 @@ public final class YangTextSchemaContextResolver implements AutoCloseable, Schem
             IOException, YangSyntaxErrorException {
         checkArgument(url != null, "Supplied URL must not be null");
 
-        final SourceIdentifier guessedId = RevisionSourceIdentifier.create(url.getFile(), Optional.absent());
+        final SourceIdentifier guessedId = guessSourceIdentifier(url.getFile());
         return registerSource(new YangTextSchemaSource(guessedId) {
             @Override
             public InputStream openStream() throws IOException {
@@ -177,6 +177,15 @@ public final class YangTextSchemaContextResolver implements AutoCloseable, Schem
         });
     }
 
+    private static SourceIdentifier guessSourceIdentifier(final String fileName) {
+        try {
+            return YangTextSchemaSource.identifierFromFilename(fileName);
+        } catch (IllegalArgumentException e) {
+            LOG.warn("Invalid file name format in '{}'", fileName, e);
+            return RevisionSourceIdentifier.create(fileName);
+        }
+    }
+
     /**
      * Try to parse all currently available yang files and build new schema context.
      * @return new schema context iif there is at least 1 yang file registered and