BUG-7568: Use YangTextSchemaSource to emit schema files
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / repo / YangTextSchemaContextResolver.java
index a2bbcd84015e50646c24a4f3cb4087590a64e2de..1e1b3882e3764f191505665dd76f56e4d0291db0 100644 (file)
@@ -9,8 +9,7 @@ package org.opendaylight.yangtools.yang.parser.repo;
 
 import static com.google.common.base.Preconditions.checkArgument;
 
-import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
-
+import com.google.common.annotations.Beta;
 import com.google.common.base.MoreObjects.ToStringHelper;
 import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
@@ -31,6 +30,7 @@ import javax.annotation.Nonnull;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
 import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
+import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactory;
 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
 import org.opendaylight.yangtools.yang.model.repo.api.SchemaResolutionException;
@@ -95,7 +95,7 @@ public final class YangTextSchemaContextResolver implements AutoCloseable, Schem
             throws SchemaSourceException, IOException, YangSyntaxErrorException {
         checkArgument(source != null);
 
-        final ASTSchemaSource ast = TextToASTTransformer.TRANSFORMATION.apply(source).checkedGet();
+        final ASTSchemaSource ast = TextToASTTransformer.transformText(source);
         LOG.trace("Resolved source {} to source {}", source, ast);
 
         // AST carries an accurate identifier, check if it matches the one supplied by the source. If it
@@ -105,12 +105,12 @@ public final class YangTextSchemaContextResolver implements AutoCloseable, Schem
         final YangTextSchemaSource text;
         if (!parsedId.equals(providedId)) {
             if (!parsedId.getName().equals(providedId.getName())) {
-                LOG.info("Provided module name {} does not match actual text {}, corrected", providedId.toYangFilename(),
-                    parsedId.toYangFilename());
+                LOG.info("Provided module name {} does not match actual text {}, corrected",
+                    providedId.toYangFilename(), parsedId.toYangFilename());
             } else {
                 final String sourceRev = providedId.getRevision();
                 final String astRev = parsedId.getRevision();
-                if (sourceRev != null) {
+                if (sourceRev != null && !SourceIdentifier.NOT_PRESENT_FORMATTED_REVISION.equals(sourceRev)) {
                     if (!sourceRev.equals(astRev)) {
                         LOG.info("Provided module revision {} does not match actual text {}, corrected",
                             providedId.toYangFilename(), parsedId.toYangFilename());
@@ -160,10 +160,13 @@ public final class YangTextSchemaContextResolver implements AutoCloseable, Schem
      * @throws SchemaSourceException When parsing encounters general error
      * @return a YangTextSchemaSourceRegistration for this URL
      */
-    public YangTextSchemaSourceRegistration registerSource(@Nonnull final URL url) throws SchemaSourceException, IOException, YangSyntaxErrorException {
+    public YangTextSchemaSourceRegistration registerSource(@Nonnull final URL url) throws SchemaSourceException,
+            IOException, YangSyntaxErrorException {
         checkArgument(url != null, "Supplied URL must not be null");
 
-        final SourceIdentifier guessedId = RevisionSourceIdentifier.create(url.getFile(), Optional.absent());
+        final String path = url.getPath();
+        final String fileName = path.substring(path.lastIndexOf('/') + 1);
+        final SourceIdentifier guessedId = guessSourceIdentifier(fileName);
         return registerSource(new YangTextSchemaSource(guessedId) {
             @Override
             public InputStream openStream() throws IOException {
@@ -177,6 +180,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
@@ -194,7 +206,7 @@ public final class YangTextSchemaContextResolver implements AutoCloseable, Schem
      * @return new schema context iif there is at least 1 yang file registered and
      *         new schema context was successfully built.
      */
-    public Optional<SchemaContext> getSchemaContext(StatementParserMode statementParserMode) {
+    public Optional<SchemaContext> getSchemaContext(final StatementParserMode statementParserMode) {
         final SchemaContextFactory factory = repository.createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
         Optional<SchemaContext> sc;
         Object v;
@@ -217,7 +229,8 @@ public final class YangTextSchemaContextResolver implements AutoCloseable, Schem
             } while (v != version);
 
             while (true) {
-                final CheckedFuture<SchemaContext, SchemaResolutionException> f = factory.createSchemaContext(sources, statementParserMode);
+                final CheckedFuture<SchemaContext, SchemaResolutionException> f = factory.createSchemaContext(sources,
+                    statementParserMode);
                 try {
                     sc = Optional.of(f.checkedGet());
                     break;
@@ -241,13 +254,14 @@ public final class YangTextSchemaContextResolver implements AutoCloseable, Schem
     }
 
     @Override
-    public synchronized CheckedFuture<YangTextSchemaSource, SchemaSourceException> getSource(final SourceIdentifier sourceIdentifier) {
+    public synchronized CheckedFuture<YangTextSchemaSource, SchemaSourceException> getSource(
+            final SourceIdentifier sourceIdentifier) {
         final Collection<YangTextSchemaSource> ret = texts.get(sourceIdentifier);
 
         LOG.debug("Lookup {} result {}", sourceIdentifier, ret);
         if (ret.isEmpty()) {
-            return Futures.immediateFailedCheckedFuture(
-                    new MissingSchemaSourceException("URL for " + sourceIdentifier + " not registered", sourceIdentifier));
+            return Futures.immediateFailedCheckedFuture(new MissingSchemaSourceException(
+                "URL for " + sourceIdentifier + " not registered", sourceIdentifier));
         }
 
         return Futures.immediateCheckedFuture(ret.iterator().next());
@@ -262,6 +276,23 @@ public final class YangTextSchemaContextResolver implements AutoCloseable, Schem
         return ImmutableSet.copyOf(texts.keySet());
     }
 
+    @Beta
+    public synchronized Collection<YangTextSchemaSource> getSourceTexts(final SourceIdentifier sourceIdentifier) {
+        return ImmutableSet.copyOf(texts.get(sourceIdentifier));
+    }
+
+    @Beta
+    public SchemaContext trySchemaContext() throws SchemaResolutionException {
+        return trySchemaContext(StatementParserMode.DEFAULT_MODE);
+    }
+
+    @Beta
+    public SchemaContext trySchemaContext(final StatementParserMode statementParserMode)
+            throws SchemaResolutionException {
+        final SchemaContextFactory factory = repository.createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
+        return factory.createSchemaContext(ImmutableSet.copyOf(requiredSources), statementParserMode).checkedGet();
+    }
+
     @Override
     public void close() {
         transReg.close();