Remove AbstractStatementSupport.createEmptyDeclared()
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / repo / SharedSchemaContextFactory.java
index 87ab72d55587327d390d8cf57d0fdc5b41658fee..e9a4fc6d842f55158ae155a19327b69881e30f84 100644 (file)
@@ -35,6 +35,7 @@ import java.util.Set;
 import java.util.concurrent.ExecutionException;
 import org.eclipse.jdt.annotation.NonNull;
 import org.gaul.modernizer_maven_annotations.SuppressModernizer;
+import org.opendaylight.yangtools.concepts.SemVer;
 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
 import org.opendaylight.yangtools.yang.model.parser.api.YangParser;
 import org.opendaylight.yangtools.yang.model.parser.api.YangParserException;
@@ -43,9 +44,10 @@ 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.SchemaContextFactoryConfiguration;
 import org.opendaylight.yangtools.yang.model.repo.api.SchemaResolutionException;
+import org.opendaylight.yangtools.yang.model.repo.api.SemVerSourceIdentifier;
 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
 import org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode;
-import org.opendaylight.yangtools.yang.parser.rfc7950.repo.ASTSchemaSource;
+import org.opendaylight.yangtools.yang.parser.rfc7950.ir.IRSchemaSource;
 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangModelDependencyInfo;
 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
 import org.slf4j.Logger;
@@ -78,7 +80,7 @@ final class SharedSchemaContextFactory implements EffectiveModelContextFactory {
     private @NonNull ListenableFuture<EffectiveModelContext> createSchemaContext(
             final Collection<SourceIdentifier> requiredSources,
             final Cache<Collection<SourceIdentifier>, EffectiveModelContext> cache,
-            final AsyncFunction<List<ASTSchemaSource>, EffectiveModelContext> assembleSources) {
+            final AsyncFunction<List<IRSchemaSource>, EffectiveModelContext> assembleSources) {
         // Make sources unique
         final List<SourceIdentifier> uniqueSourceIdentifiers = deDuplicateSources(requiredSources);
 
@@ -89,7 +91,7 @@ final class SharedSchemaContextFactory implements EffectiveModelContextFactory {
         }
 
         // Request all sources be loaded
-        ListenableFuture<List<ASTSchemaSource>> sf = Futures.allAsList(Collections2.transform(uniqueSourceIdentifiers,
+        ListenableFuture<List<IRSchemaSource>> sf = Futures.allAsList(Collections2.transform(uniqueSourceIdentifiers,
             this::requestSource));
 
         // Detect mismatch between requested Source IDs and IDs that are extracted from parsed source
@@ -129,8 +131,8 @@ final class SharedSchemaContextFactory implements EffectiveModelContextFactory {
         return rf;
     }
 
-    private ListenableFuture<ASTSchemaSource> requestSource(final @NonNull SourceIdentifier identifier) {
-        return repository.getSchemaSource(identifier, ASTSchemaSource.class);
+    private ListenableFuture<IRSchemaSource> requestSource(final @NonNull SourceIdentifier identifier) {
+        return repository.getSchemaSource(identifier, IRSchemaSource.class);
     }
 
     /**
@@ -151,8 +153,8 @@ final class SharedSchemaContextFactory implements EffectiveModelContextFactory {
     }
 
     @SuppressModernizer
-    private static final class SourceIdMismatchDetector implements Function<List<ASTSchemaSource>,
-            List<ASTSchemaSource>> {
+    private static final class SourceIdMismatchDetector implements Function<List<IRSchemaSource>,
+            List<IRSchemaSource>> {
         private final List<SourceIdentifier> sourceIdentifiers;
 
         SourceIdMismatchDetector(final List<SourceIdentifier> sourceIdentifiers) {
@@ -160,14 +162,14 @@ final class SharedSchemaContextFactory implements EffectiveModelContextFactory {
         }
 
         @Override
-        public List<ASTSchemaSource> apply(final List<ASTSchemaSource> input) {
-            final Map<SourceIdentifier, ASTSchemaSource> filtered = new LinkedHashMap<>();
+        public List<IRSchemaSource> apply(final List<IRSchemaSource> input) {
+            final Map<SourceIdentifier, IRSchemaSource> filtered = new LinkedHashMap<>();
 
             for (int i = 0; i < input.size(); i++) {
 
                 final SourceIdentifier expectedSId = sourceIdentifiers.get(i);
-                final ASTSchemaSource astSchemaSource = input.get(i);
-                final SourceIdentifier realSId = astSchemaSource.getIdentifier();
+                final IRSchemaSource irSchemaSource = input.get(i);
+                final SourceIdentifier realSId = irSchemaSource.getIdentifier();
 
                 if (!expectedSId.equals(realSId)) {
                     LOG.warn("Source identifier mismatch for module \"{}\", requested as {} but actually is {}. "
@@ -178,17 +180,17 @@ final class SharedSchemaContextFactory implements EffectiveModelContextFactory {
                     LOG.warn("Duplicate source for module {} detected in reactor", realSId);
                 }
 
-                filtered.put(realSId, astSchemaSource);
+                filtered.put(realSId, irSchemaSource);
 
             }
             return ImmutableList.copyOf(filtered.values());
         }
     }
 
-    private static final class AssembleSources implements AsyncFunction<List<ASTSchemaSource>, EffectiveModelContext> {
+    private static final class AssembleSources implements AsyncFunction<List<IRSchemaSource>, EffectiveModelContext> {
         private final @NonNull YangParserFactory parserFactory;
         private final @NonNull SchemaContextFactoryConfiguration config;
-        private final @NonNull Function<ASTSchemaSource, SourceIdentifier> getIdentifier;
+        private final @NonNull Function<IRSchemaSource, SourceIdentifier> getIdentifier;
 
         private AssembleSources(final @NonNull YangParserFactory parserFactory,
                 final @NonNull SchemaContextFactoryConfiguration config) {
@@ -196,19 +198,19 @@ final class SharedSchemaContextFactory implements EffectiveModelContextFactory {
             this.config = config;
             switch (config.getStatementParserMode()) {
                 case SEMVER_MODE:
-                    this.getIdentifier = ASTSchemaSource::getSemVerIdentifier;
+                    this.getIdentifier = AssembleSources::getSemVerIdentifier;
                     break;
                 default:
-                    this.getIdentifier = ASTSchemaSource::getIdentifier;
+                    this.getIdentifier = IRSchemaSource::getIdentifier;
             }
         }
 
         @Override
-        public FluentFuture<EffectiveModelContext> apply(final List<ASTSchemaSource> sources)
+        public FluentFuture<EffectiveModelContext> apply(final List<IRSchemaSource> sources)
                 throws SchemaResolutionException, ReactorException {
-            final Map<SourceIdentifier, ASTSchemaSource> srcs = Maps.uniqueIndex(sources, getIdentifier);
+            final Map<SourceIdentifier, IRSchemaSource> srcs = Maps.uniqueIndex(sources, getIdentifier);
             final Map<SourceIdentifier, YangModelDependencyInfo> deps =
-                    Maps.transformValues(srcs, ASTSchemaSource::getDependencyInformation);
+                    Maps.transformValues(srcs, YangModelDependencyInfo::forIR);
 
             LOG.debug("Resolving dependency reactor {}", deps);
 
@@ -226,10 +228,9 @@ final class SharedSchemaContextFactory implements EffectiveModelContextFactory {
             config.getSupportedFeatures().ifPresent(parser::setSupportedFeatures);
             config.getModulesDeviatedByModules().ifPresent(parser::setModulesWithSupportedDeviations);
 
-            for (final Entry<SourceIdentifier, ASTSchemaSource> entry : srcs.entrySet()) {
-                final ASTSchemaSource ast = entry.getValue();
+            for (final Entry<SourceIdentifier, IRSchemaSource> entry : srcs.entrySet()) {
                 try {
-                    parser.addSource(ast);
+                    parser.addSource(entry.getValue());
                 } catch (YangSyntaxErrorException | IOException e) {
                     throw new SchemaResolutionException("Failed to add source " + entry.getKey(), e);
                 }
@@ -244,5 +245,15 @@ final class SharedSchemaContextFactory implements EffectiveModelContextFactory {
 
             return immediateFluentFuture(schemaContext);
         }
+
+        private static SemVerSourceIdentifier getSemVerIdentifier(final IRSchemaSource source) {
+            final SourceIdentifier identifier = source.getIdentifier();
+            final SemVer semver = YangModelDependencyInfo.findSemanticVersion(source.getRootStatement(), identifier);
+            if (identifier instanceof SemVerSourceIdentifier && semver == null) {
+                return (SemVerSourceIdentifier) identifier;
+            }
+
+            return SemVerSourceIdentifier.create(identifier.getName(), identifier.getRevision(), semver);
+        }
     }
 }