BUG-4688: switch revisions from Date to Revision
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / ImportEffectiveStatementImpl.java
index 70d968d97c4feb199e9bb97fa71982d18064b7b0..4e95d34dafb950bb41356e030c63e6e312552436 100644 (file)
@@ -8,25 +8,25 @@
 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
 
 import com.google.common.base.MoreObjects;
-import java.util.Date;
 import java.util.Objects;
+import java.util.Optional;
 import org.opendaylight.yangtools.concepts.SemVer;
 import org.opendaylight.yangtools.yang.common.QNameModule;
-import org.opendaylight.yangtools.yang.model.api.Module;
-import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
+import org.opendaylight.yangtools.yang.common.Revision;
 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
 import org.opendaylight.yangtools.yang.model.api.stmt.ImportStatement;
+import org.opendaylight.yangtools.yang.model.repo.api.SemVerSourceIdentifier;
 import org.opendaylight.yangtools.yang.parser.spi.meta.MissingSubstatementException;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
-import org.opendaylight.yangtools.yang.parser.spi.source.ImpPrefixToSemVerModuleIdentifier;
+import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
+import org.opendaylight.yangtools.yang.parser.spi.source.ImportPrefixToSemVerSourceIdentifier;
 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
-import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
 
-public class ImportEffectiveStatementImpl extends DeclaredEffectiveStatementBase<String, ImportStatement> implements
-        ModuleImport {
+public class ImportEffectiveStatementImpl extends DeclaredEffectiveStatementBase<String, ImportStatement>
+        implements ModuleImport {
 
     private final String moduleName;
-    private final Date revision;
+    private final Revision revision;
     private final SemVer semVer;
     private final String prefix;
     private final String description;
@@ -45,32 +45,34 @@ public class ImportEffectiveStatementImpl extends DeclaredEffectiveStatementBase
         }
 
         if (!ctx.isEnabledSemanticVersioning()) {
-            final RevisionDateEffectiveStatementImpl revisionDateStmt = firstEffective(RevisionDateEffectiveStatementImpl.class);
-            this.revision = (revisionDateStmt == null) ? getImportedRevision(ctx) : revisionDateStmt
-                    .argument();
-            this.semVer = Module.DEFAULT_SEMANTIC_VERSION;
+            final RevisionDateEffectiveStatementImpl revisionDateStmt = firstEffective(
+                RevisionDateEffectiveStatementImpl.class);
+            this.revision = revisionDateStmt == null ? getImportedRevision(ctx) : revisionDateStmt.argument();
+            this.semVer = null;
         } else {
-            final ModuleIdentifier importedModuleIdentifier = ctx.getFromNamespace(ImpPrefixToSemVerModuleIdentifier.class, prefix);
-            revision = importedModuleIdentifier.getRevision();
-            semVer = importedModuleIdentifier.getSemanticVersion();
+            final SemVerSourceIdentifier importedModuleIdentifier = ctx.getFromNamespace(
+                ImportPrefixToSemVerSourceIdentifier.class, prefix);
+            revision = Revision.valueOf(importedModuleIdentifier.getRevision());
+            semVer = importedModuleIdentifier.getSemanticVersion().orElse(null);
         }
 
-        final DescriptionEffectiveStatementImpl descriptionStmt = firstEffective(DescriptionEffectiveStatementImpl.class);
-        this.description = (descriptionStmt != null) ? descriptionStmt.argument() : null;
+        final DescriptionEffectiveStatementImpl descriptionStmt = firstEffective(
+            DescriptionEffectiveStatementImpl.class);
+        this.description = descriptionStmt != null ? descriptionStmt.argument() : null;
 
         final ReferenceEffectiveStatementImpl referenceStmt = firstEffective(ReferenceEffectiveStatementImpl.class);
-        this.reference = (referenceStmt != null) ? referenceStmt.argument() : null;
+        this.reference = referenceStmt != null ? referenceStmt.argument() : null;
     }
 
-    private Date getImportedRevision(final StmtContext<String, ImportStatement, ?> ctx) {
+    private Revision getImportedRevision(final StmtContext<String, ImportStatement, ?> ctx) {
         /*
          * When 'revision-date' of an import is not specified in yang source, we
          * need to find revision of imported module.
          */
-        final QNameModule importedModule = Utils.getModuleQNameByPrefix(ctx, this.prefix);
+        final QNameModule importedModule = StmtContextUtils.getModuleQNameByPrefix(ctx, this.prefix);
         SourceException.throwIfNull(importedModule, ctx.getStatementSourceReference(),
                 "Unable to find import of module %s with prefix %s.", this.moduleName, this.prefix);
-        return importedModule.getRevision();
+        return importedModule.getRevision().orElse(null);
     }
 
     @Override
@@ -79,13 +81,13 @@ public class ImportEffectiveStatementImpl extends DeclaredEffectiveStatementBase
     }
 
     @Override
-    public Date getRevision() {
-        return revision;
+    public Optional<Revision> getRevision() {
+        return Optional.ofNullable(revision);
     }
 
     @Override
-    public SemVer getSemanticVersion() {
-        return semVer;
+    public Optional<SemVer> getSemanticVersion() {
+        return Optional.ofNullable(semVer);
     }
 
     @Override