Do not subclass YangTextSchemaSource in yang-parser-impl
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / repo / YangTextSchemaContextResolver.java
index 1955136d169564484f44575d8c78fde1f804994f..6cdcaac81c57ef724385921f9845e798714468fa 100644 (file)
@@ -13,7 +13,6 @@ import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediate
 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFluentFuture;
 
 import com.google.common.annotations.Beta;
-import com.google.common.base.MoreObjects.ToStringHelper;
 import com.google.common.base.Verify;
 import com.google.common.collect.ArrayListMultimap;
 import com.google.common.collect.ImmutableSet;
@@ -21,7 +20,6 @@ import com.google.common.collect.Multimap;
 import com.google.common.util.concurrent.FluentFuture;
 import com.google.common.util.concurrent.ListenableFuture;
 import java.io.IOException;
-import java.io.InputStream;
 import java.net.URL;
 import java.util.Collection;
 import java.util.Optional;
@@ -32,15 +30,16 @@ import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicReference;
 import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.yangtools.yang.common.Revision;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
+import org.opendaylight.yangtools.yang.model.parser.api.YangParserFactory;
 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
+import org.opendaylight.yangtools.yang.model.repo.api.EffectiveModelContextFactory;
 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.SchemaContextFactoryConfiguration;
 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
 import org.opendaylight.yangtools.yang.model.repo.api.SchemaResolutionException;
 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
-import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceFilter;
 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
 import org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode;
 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
@@ -51,8 +50,8 @@ import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistration;
 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
 import org.opendaylight.yangtools.yang.model.repo.util.InMemorySchemaSourceCache;
-import org.opendaylight.yangtools.yang.parser.rfc7950.repo.ASTSchemaSource;
-import org.opendaylight.yangtools.yang.parser.rfc7950.repo.TextToASTTransformer;
+import org.opendaylight.yangtools.yang.parser.rfc7950.ir.IRSchemaSource;
+import org.opendaylight.yangtools.yang.parser.rfc7950.repo.TextToIRTransformer;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -62,9 +61,9 @@ public final class YangTextSchemaContextResolver implements AutoCloseable, Schem
 
     private final Collection<SourceIdentifier> requiredSources = new ConcurrentLinkedDeque<>();
     private final Multimap<SourceIdentifier, YangTextSchemaSource> texts = ArrayListMultimap.create();
-    private final AtomicReference<Optional<SchemaContext>> currentSchemaContext =
+    private final AtomicReference<Optional<EffectiveModelContext>> currentSchemaContext =
             new AtomicReference<>(Optional.empty());
-    private final InMemorySchemaSourceCache<ASTSchemaSource> cache;
+    private final InMemorySchemaSourceCache<IRSchemaSource> cache;
     private final SchemaListenerRegistration transReg;
     private final SchemaSourceRegistry registry;
     private final SchemaRepository repository;
@@ -75,18 +74,23 @@ public final class YangTextSchemaContextResolver implements AutoCloseable, Schem
         this.repository = requireNonNull(repository);
         this.registry = requireNonNull(registry);
 
-        final TextToASTTransformer t = TextToASTTransformer.create(repository, registry);
+        final TextToIRTransformer t = TextToIRTransformer.create(repository, registry);
         transReg = registry.registerSchemaSourceListener(t);
 
-        cache = InMemorySchemaSourceCache.createSoftCache(registry, ASTSchemaSource.class, SOURCE_LIFETIME_SECONDS,
+        cache = InMemorySchemaSourceCache.createSoftCache(registry, IRSchemaSource.class, SOURCE_LIFETIME_SECONDS,
             TimeUnit.SECONDS);
     }
 
-    public static YangTextSchemaContextResolver create(final String name) {
+    public static @NonNull YangTextSchemaContextResolver create(final String name) {
         final SharedSchemaRepository sharedRepo = new SharedSchemaRepository(name);
         return new YangTextSchemaContextResolver(sharedRepo, sharedRepo);
     }
 
+    public static @NonNull YangTextSchemaContextResolver create(final String name, final YangParserFactory factory) {
+        final SharedSchemaRepository sharedRepo = new SharedSchemaRepository(name, factory);
+        return new YangTextSchemaContextResolver(sharedRepo, sharedRepo);
+    }
+
     /**
      * Register a {@link YangTextSchemaSource}.
      *
@@ -100,7 +104,7 @@ public final class YangTextSchemaContextResolver implements AutoCloseable, Schem
             throws SchemaSourceException, IOException, YangSyntaxErrorException {
         checkArgument(source != null);
 
-        final ASTSchemaSource ast = TextToASTTransformer.transformText(source);
+        final IRSchemaSource ast = TextToIRTransformer.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
@@ -171,18 +175,7 @@ public final class YangTextSchemaContextResolver implements AutoCloseable, Schem
 
         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 {
-                return url.openStream();
-            }
-
-            @Override
-            protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
-                return toStringHelper.add("url", url);
-            }
-        });
+        return registerSource(YangTextSchemaSource.forURL(url, guessSourceIdentifier(fileName)));
     }
 
     private static SourceIdentifier guessSourceIdentifier(final @NonNull String fileName) {
@@ -200,8 +193,8 @@ 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() {
-        return getSchemaContext(StatementParserMode.DEFAULT_MODE);
+    public Optional<? extends EffectiveModelContext> getEffectiveModelContext() {
+        return getEffectiveModelContext(StatementParserMode.DEFAULT_MODE);
     }
 
     /**
@@ -211,9 +204,11 @@ 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(final StatementParserMode statementParserMode) {
-        final SchemaContextFactory factory = repository.createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
-        Optional<SchemaContext> sc;
+    public Optional<? extends EffectiveModelContext> getEffectiveModelContext(
+            final StatementParserMode statementParserMode) {
+        final EffectiveModelContextFactory factory = repository.createEffectiveModelContextFactory(
+            config(statementParserMode));
+        Optional<EffectiveModelContext> sc;
         Object ver;
         do {
             // Spin get stable context version
@@ -234,7 +229,7 @@ public final class YangTextSchemaContextResolver implements AutoCloseable, Schem
             } while (ver != version);
 
             while (true) {
-                final ListenableFuture<SchemaContext> f = factory.createSchemaContext(sources, statementParserMode);
+                final ListenableFuture<EffectiveModelContext> f = factory.createEffectiveModelContext(sources);
                 try {
                     sc = Optional.of(f.get());
                     break;
@@ -262,7 +257,7 @@ public final class YangTextSchemaContextResolver implements AutoCloseable, Schem
     }
 
     @Override
-    public synchronized FluentFuture<YangTextSchemaSource> getSource(
+    public synchronized @NonNull FluentFuture<YangTextSchemaSource> getSource(
             final SourceIdentifier sourceIdentifier) {
         final Collection<YangTextSchemaSource> ret = texts.get(sourceIdentifier);
 
@@ -290,17 +285,17 @@ public final class YangTextSchemaContextResolver implements AutoCloseable, Schem
     }
 
     @Beta
-    public SchemaContext trySchemaContext() throws SchemaResolutionException {
+    public EffectiveModelContext trySchemaContext() throws SchemaResolutionException {
         return trySchemaContext(StatementParserMode.DEFAULT_MODE);
     }
 
     @Beta
     @SuppressWarnings("checkstyle:avoidHidingCauseException")
-    public SchemaContext trySchemaContext(final StatementParserMode statementParserMode)
+    public EffectiveModelContext trySchemaContext(final StatementParserMode statementParserMode)
             throws SchemaResolutionException {
-        final ListenableFuture<SchemaContext> future = repository
-                .createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT)
-                .createSchemaContext(ImmutableSet.copyOf(requiredSources), statementParserMode);
+        final ListenableFuture<EffectiveModelContext> future = repository
+                .createEffectiveModelContextFactory(config(statementParserMode))
+                .createEffectiveModelContext(ImmutableSet.copyOf(requiredSources));
 
         try {
             return future.get();
@@ -320,4 +315,8 @@ public final class YangTextSchemaContextResolver implements AutoCloseable, Schem
     public void close() {
         transReg.close();
     }
+
+    private static SchemaContextFactoryConfiguration config(final StatementParserMode statementParserMode) {
+        return SchemaContextFactoryConfiguration.builder().setStatementParserMode(statementParserMode).build();
+    }
 }