Bug 4662: Introduce a SemanticVersion concept - SchemaContextFactory
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / repo / SharedSchemaContextFactory.java
index ab146fcfc6d732196966f3697c36cee12aad0c3d..7b40342003c575eca5692f0f88d4ca6ad827a441 100644 (file)
@@ -33,11 +33,13 @@ import org.antlr.v4.runtime.ParserRuleContext;
 import org.opendaylight.yangtools.antlrv4.code.gen.YangStatementParser.StatementContext;
 import org.opendaylight.yangtools.util.concurrent.ExceptionMapper;
 import org.opendaylight.yangtools.util.concurrent.ReflectiveExceptionMapper;
+import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactory;
 import org.opendaylight.yangtools.yang.model.repo.api.SchemaResolutionException;
 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.parser.impl.util.YangModelDependencyInfo;
 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
@@ -59,41 +61,7 @@ final class SharedSchemaContextFactory implements SchemaContextFactory {
         }
     };
     private final Cache<Collection<SourceIdentifier>, SchemaContext> cache = CacheBuilder.newBuilder().weakValues().build();
-
-    private final AsyncFunction<List<ASTSchemaSource>, SchemaContext> assembleSources = new AsyncFunction<List<ASTSchemaSource>, SchemaContext>() {
-        @Override
-        public ListenableFuture<SchemaContext> apply(final List<ASTSchemaSource> sources) throws SchemaResolutionException, SourceException, ReactorException {
-            final Map<SourceIdentifier, ASTSchemaSource> srcs =
-                    Maps.uniqueIndex(sources, ASTSchemaSource.GET_IDENTIFIER);
-            final Map<SourceIdentifier, YangModelDependencyInfo> deps =
-                    Maps.transformValues(srcs, ASTSchemaSource.GET_DEPINFO);
-
-            LOG.debug("Resolving dependency reactor {}", deps);
-
-            final DependencyResolver res = DependencyResolver.create(deps);
-            if (!res.getUnresolvedSources().isEmpty()) {
-                LOG.debug("Omitting models {} due to unsatisfied imports {}", res.getUnresolvedSources(), res.getUnsatisfiedImports());
-                throw new SchemaResolutionException("Failed to resolve required models",
-                        res.getResolvedSources(), res.getUnsatisfiedImports());
-            }
-
-            final Map<SourceIdentifier, ParserRuleContext> asts = Maps.transformValues(srcs, ASTSchemaSource.GET_AST);
-            final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
-
-            for (final Entry<SourceIdentifier, ParserRuleContext> e : asts.entrySet()) {
-                final ParserRuleContext parserRuleCtx = e.getValue();
-                Preconditions.checkArgument(parserRuleCtx instanceof StatementContext,
-                    "Unsupported context class %s for source %s", parserRuleCtx.getClass(), e.getKey());
-
-                reactor.addSource(new YangStatementSourceImpl(e.getKey(), (StatementContext) parserRuleCtx));
-            }
-
-            SchemaContext schemaContext = reactor.buildEffective();
-
-            return Futures.immediateCheckedFuture(schemaContext);
-        }
-    };
-
+    private final Cache<Collection<SourceIdentifier>, SchemaContext> semVerCache = CacheBuilder.newBuilder().weakValues().build();
     private final SharedSchemaRepository repository;
     // FIXME: ignored right now
     private final SchemaSourceFilter filter;
@@ -105,7 +73,15 @@ final class SharedSchemaContextFactory implements SchemaContextFactory {
     }
 
     @Override
-    public CheckedFuture<SchemaContext, SchemaResolutionException> createSchemaContext(final Collection<SourceIdentifier> requiredSources) {
+    public CheckedFuture<SchemaContext, SchemaResolutionException> createSchemaContext(
+            final Collection<SourceIdentifier> requiredSources, final StatementParserMode statementParserMode,
+            final java.util.function.Predicate<QName> isFeatureSupported) {
+        return createSchemaContext(requiredSources,
+                statementParserMode == StatementParserMode.SEMVER_MODE ? this.semVerCache : this.cache,
+                new AssembleSources(isFeatureSupported, statementParserMode));
+    }
+
+    private CheckedFuture<SchemaContext, SchemaResolutionException> createSchemaContext(final Collection<SourceIdentifier> requiredSources, final Cache<Collection<SourceIdentifier>, SchemaContext> cache, final AsyncFunction<List<ASTSchemaSource>, SchemaContext> assembleSources) {
         // Make sources unique
         final List<SourceIdentifier> uniqueSourceIdentifiers = deDuplicateSources(requiredSources);
 
@@ -194,4 +170,58 @@ final class SharedSchemaContextFactory implements SchemaContextFactory {
             return ImmutableList.copyOf(filtered.values());
         }
     }
+
+    private static final class AssembleSources implements AsyncFunction<List<ASTSchemaSource>, SchemaContext> {
+
+        private final java.util.function.Predicate<QName> isFeatureSupported;
+        private final StatementParserMode statementParserMode;
+        private final Function<ASTSchemaSource, SourceIdentifier> getIdentifier;
+
+        private AssembleSources(final java.util.function.Predicate<QName> isFeatureSupported,
+                final StatementParserMode statementParserMode) {
+            this.isFeatureSupported = Preconditions.checkNotNull(isFeatureSupported);
+            this.statementParserMode = Preconditions.checkNotNull(statementParserMode);
+            switch (statementParserMode) {
+            case SEMVER_MODE:
+                this.getIdentifier = ASTSchemaSource.GET_SEMVER_IDENTIFIER;
+                break;
+            default:
+                this.getIdentifier = ASTSchemaSource.GET_IDENTIFIER;
+            }
+        }
+
+        @Override
+        public ListenableFuture<SchemaContext> apply(final List<ASTSchemaSource> sources) throws SchemaResolutionException,
+                SourceException, ReactorException {
+            final Map<SourceIdentifier, ASTSchemaSource> srcs = Maps.uniqueIndex(sources, getIdentifier);
+            final Map<SourceIdentifier, YangModelDependencyInfo> deps =
+                    Maps.transformValues(srcs, ASTSchemaSource.GET_DEPINFO);
+
+            LOG.debug("Resolving dependency reactor {}", deps);
+
+            final DependencyResolver res = this.statementParserMode == StatementParserMode.SEMVER_MODE ? SemVerDependencyResolver
+                    .create(deps) : RevisionDependencyResolver.create(deps);
+            if (!res.getUnresolvedSources().isEmpty()) {
+                LOG.debug("Omitting models {} due to unsatisfied imports {}", res.getUnresolvedSources(), res.getUnsatisfiedImports());
+                throw new SchemaResolutionException("Failed to resolve required models",
+                        res.getResolvedSources(), res.getUnsatisfiedImports());
+            }
+
+            final Map<SourceIdentifier, ParserRuleContext> asts = Maps.transformValues(srcs, ASTSchemaSource.GET_AST);
+            final CrossSourceStatementReactor.BuildAction reactor =
+                    YangInferencePipeline.RFC6020_REACTOR.newBuild(statementParserMode, isFeatureSupported);
+
+            for (final Entry<SourceIdentifier, ParserRuleContext> e : asts.entrySet()) {
+                final ParserRuleContext parserRuleCtx = e.getValue();
+                Preconditions.checkArgument(parserRuleCtx instanceof StatementContext,
+                        "Unsupported context class %s for source %s", parserRuleCtx.getClass(), e.getKey());
+
+                reactor.addSource(new YangStatementSourceImpl(e.getKey(), (StatementContext) parserRuleCtx));
+            }
+
+            final SchemaContext schemaContext = reactor.buildEffective();
+
+            return Futures.immediateCheckedFuture(schemaContext);
+        }
+    }
 }