Enforce decimal64 fraction-digits in derived types
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / ModuleStatementSupport.java
index 935f4309414cad1ac6a98270b2380450864566dc..28fb20bca28cbe731a034aabbea982a0a4c05300 100644 (file)
@@ -10,20 +10,18 @@ package org.opendaylight.yangtools.yang.parser.stmt.rfc6020;
 import static org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils.firstAttributeOf;
 
 import java.net.URI;
-import java.util.Date;
-import java.util.NavigableMap;
 import java.util.Optional;
-import java.util.TreeMap;
 import org.opendaylight.yangtools.concepts.SemVer;
 import org.opendaylight.yangtools.yang.common.QNameModule;
-import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
-import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
+import org.opendaylight.yangtools.yang.common.Revision;
 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.NamespaceStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.PrefixStatement;
-import org.opendaylight.yangtools.yang.model.util.ModuleIdentifierImpl;
+import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
+import org.opendaylight.yangtools.yang.model.repo.api.SemVerSourceIdentifier;
+import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
 import org.opendaylight.yangtools.yang.parser.spi.ModuleNamespace;
 import org.opendaylight.yangtools.yang.parser.spi.NamespaceToModule;
 import org.opendaylight.yangtools.yang.parser.spi.PreLinkageModuleNamespace;
@@ -34,11 +32,10 @@ import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
-import org.opendaylight.yangtools.yang.parser.spi.source.ImpPrefixToModuleIdentifier;
 import org.opendaylight.yangtools.yang.parser.spi.source.ImpPrefixToNamespace;
-import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleIdentifier;
+import org.opendaylight.yangtools.yang.parser.spi.source.ImportPrefixToModuleCtx;
 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName;
-import org.opendaylight.yangtools.yang.parser.spi.source.ModuleIdentifierToModuleQName;
+import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToSourceIdentifier;
 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNameToModuleQName;
 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNameToNamespace;
 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNamespaceForBelongsTo;
@@ -117,17 +114,11 @@ public class ModuleStatementSupport extends
 
         stmt.addContext(PreLinkageModuleNamespace.class, moduleName, stmt);
 
-        Optional<Date> revisionDate = Optional.ofNullable(StmtContextUtils.getLatestRevision(
-            stmt.declaredSubstatements()));
-        if (!revisionDate.isPresent()) {
-            revisionDate = Optional.of(SimpleDateFormatUtil.DEFAULT_DATE_REV);
-        }
-
+        final Optional<Revision> revisionDate = StmtContextUtils.getLatestRevision(stmt.declaredSubstatements());
         final QNameModule qNameModule = QNameModule.create(moduleNs, revisionDate.orElse(null)).intern();
 
         stmt.addToNs(ModuleCtxToModuleQName.class, stmt, qNameModule);
-        stmt.setRootIdentifier(ModuleIdentifierImpl.create(stmt.getStatementArgument(),
-                Optional.empty(), revisionDate));
+        stmt.setRootIdentifier(RevisionSourceIdentifier.create(stmt.getStatementArgument(), revisionDate));
     }
 
     @Override
@@ -139,12 +130,7 @@ public class ModuleStatementSupport extends
         SourceException.throwIf(!moduleNs.isPresent(), stmt.getStatementSourceReference(),
             "Namespace of the module [%s] is missing", stmt.getStatementArgument());
 
-        Optional<Date> revisionDate = Optional.ofNullable(StmtContextUtils.getLatestRevision(
-            stmt.declaredSubstatements()));
-        if (!revisionDate.isPresent()) {
-            revisionDate = Optional.of(SimpleDateFormatUtil.DEFAULT_DATE_REV);
-        }
-
+        final Optional<Revision> revisionDate = StmtContextUtils.getLatestRevision(stmt.declaredSubstatements());
         final QNameModule qNameModule = QNameModule.create(moduleNs.get(), revisionDate.orElse(null)).intern();
         final StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>> possibleDuplicateModule =
                 stmt.getFromNamespace(NamespaceToModule.class, qNameModule);
@@ -153,8 +139,8 @@ public class ModuleStatementSupport extends
                     qNameModule.getNamespace(), possibleDuplicateModule.getStatementSourceReference());
         }
 
-        final ModuleIdentifier moduleIdentifier = ModuleIdentifierImpl.create(stmt.getStatementArgument(),
-                Optional.empty(), revisionDate);
+        final SourceIdentifier moduleIdentifier = RevisionSourceIdentifier.create(stmt.getStatementArgument(),
+                revisionDate);
 
         stmt.addContext(ModuleNamespace.class, moduleIdentifier, stmt);
         stmt.addContext(ModuleNamespaceForBelongsTo.class, moduleIdentifier.getName(), stmt);
@@ -167,35 +153,22 @@ public class ModuleStatementSupport extends
         stmt.addToNs(PrefixToModule.class, modulePrefix, qNameModule);
         stmt.addToNs(ModuleNameToModuleQName.class, stmt.getStatementArgument(), qNameModule);
         stmt.addToNs(ModuleCtxToModuleQName.class, stmt, qNameModule); // tu
-        stmt.addToNs(ModuleCtxToModuleIdentifier.class, stmt, moduleIdentifier);
+        stmt.addToNs(ModuleCtxToSourceIdentifier.class, stmt, moduleIdentifier);
         stmt.addToNs(ModuleQNameToModuleName.class, qNameModule, stmt.getStatementArgument());
-        stmt.addToNs(ModuleIdentifierToModuleQName.class, moduleIdentifier, qNameModule);
-        stmt.addToNs(ImpPrefixToModuleIdentifier.class, modulePrefix, moduleIdentifier);
+        stmt.addToNs(ImportPrefixToModuleCtx.class, modulePrefix, stmt);
 
         if (stmt.isEnabledSemanticVersioning()) {
-            addToSemVerModuleNamespace(stmt);
+            addToSemVerModuleNamespace(stmt, moduleIdentifier);
         }
     }
 
-    private static int compareNullableSemVer(final SemVer ver1, final SemVer ver2) {
-        if (ver1 == null) {
-            return ver2 == null ? 0 : -1;
-        }
-
-        return ver2 == null ? 1 : ver1.compareTo(ver2);
-    }
-
     private static void addToSemVerModuleNamespace(
-            final Mutable<String, ModuleStatement, EffectiveStatement<String, ModuleStatement>> stmt) {
+            final Mutable<String, ModuleStatement, EffectiveStatement<String, ModuleStatement>> stmt,
+            final SourceIdentifier moduleIdentifier) {
         final String moduleName = stmt.getStatementArgument();
-        NavigableMap<SemVer, StmtContext<?, ?, ?>> modulesMap = stmt.getFromNamespace(
-                SemanticVersionModuleNamespace.class, moduleName);
-        if (modulesMap == null) {
-            modulesMap = new TreeMap<>(ModuleStatementSupport::compareNullableSemVer);
-        }
         final SemVer moduleSemVer = stmt.getFromNamespace(SemanticVersionNamespace.class, stmt);
-        modulesMap.put(moduleSemVer, stmt);
-        stmt.addToNs(SemanticVersionModuleNamespace.class, moduleName, modulesMap);
+        final SemVerSourceIdentifier id = SemVerSourceIdentifier.create(moduleName, moduleSemVer);
+        stmt.addToNs(SemanticVersionModuleNamespace.class, id, stmt);
     }
 
     @Override