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 c9a2d2c3590293445a050860ea1489be176c9ef1..4e95d34dafb950bb41356e030c63e6e312552436 100644 (file)
@@ -7,36 +7,72 @@
  */
 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
 
-import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
-
-import java.util.Date;
+import com.google.common.base.MoreObjects;
 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.common.Revision;
 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
-import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
 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.meta.StmtContextUtils;
+import org.opendaylight.yangtools.yang.parser.spi.source.ImportPrefixToSemVerSourceIdentifier;
+import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
 
-public class ImportEffectiveStatementImpl extends EffectiveStatementBase<String, ImportStatement> implements
-        ModuleImport {
+public class ImportEffectiveStatementImpl extends DeclaredEffectiveStatementBase<String, ImportStatement>
+        implements ModuleImport {
 
-    private String moduleName;
-    private Date revision;
-    private String prefix;
+    private final String moduleName;
+    private final Revision revision;
+    private final SemVer semVer;
+    private final String prefix;
+    private final String description;
+    private final String reference;
 
-    public ImportEffectiveStatementImpl(StmtContext<String, ImportStatement, ?> ctx) {
+    public ImportEffectiveStatementImpl(final StmtContext<String, ImportStatement, ?> ctx) {
         super(ctx);
 
         moduleName = ctx.getStatementArgument();
-        revision = SimpleDateFormatUtil.DEFAULT_DATE_IMP;
-
-        for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
-            if (effectiveStatement instanceof RevisionDateEffectiveStatementImpl) {
-                revision = ((RevisionDateEffectiveStatementImpl) effectiveStatement).argument();
-            }
-            if (effectiveStatement instanceof PrefixEffectiveStatementImpl) {
-                prefix = ((PrefixEffectiveStatementImpl) effectiveStatement).argument();
-            }
+        final PrefixEffectiveStatementImpl prefixStmt = firstEffective(PrefixEffectiveStatementImpl.class);
+        if (prefixStmt != null) {
+            this.prefix = prefixStmt.argument();
+        } else {
+            throw new MissingSubstatementException("Prefix is mandatory substatement of import statement",
+                    ctx.getStatementSourceReference());
+        }
+
+        if (!ctx.isEnabledSemanticVersioning()) {
+            final RevisionDateEffectiveStatementImpl revisionDateStmt = firstEffective(
+                RevisionDateEffectiveStatementImpl.class);
+            this.revision = revisionDateStmt == null ? getImportedRevision(ctx) : revisionDateStmt.argument();
+            this.semVer = null;
+        } else {
+            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 ReferenceEffectiveStatementImpl referenceStmt = firstEffective(ReferenceEffectiveStatementImpl.class);
+        this.reference = referenceStmt != null ? referenceStmt.argument() : null;
+    }
+
+    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 = 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().orElse(null);
     }
 
     @Override
@@ -45,8 +81,13 @@ public class ImportEffectiveStatementImpl extends EffectiveStatementBase<String,
     }
 
     @Override
-    public Date getRevision() {
-        return revision;
+    public Optional<Revision> getRevision() {
+        return Optional.ofNullable(revision);
+    }
+
+    @Override
+    public Optional<SemVer> getSemanticVersion() {
+        return Optional.ofNullable(semVer);
     }
 
     @Override
@@ -54,18 +95,23 @@ public class ImportEffectiveStatementImpl extends EffectiveStatementBase<String,
         return prefix;
     }
 
+    @Override
+    public String getDescription() {
+        return description;
+    }
+
+    @Override
+    public String getReference() {
+        return reference;
+    }
+
     @Override
     public int hashCode() {
-        final int prime = 31;
-        int result = 1;
-        result = prime * result + Objects.hashCode(moduleName);
-        result = prime * result + Objects.hashCode(revision);
-        result = prime * result + Objects.hashCode(prefix);
-        return result;
+        return Objects.hash(moduleName, revision, prefix, semVer, description, reference);
     }
 
     @Override
-    public boolean equals(Object obj) {
+    public boolean equals(final Object obj) {
         if (this == obj) {
             return true;
         }
@@ -75,34 +121,17 @@ public class ImportEffectiveStatementImpl extends EffectiveStatementBase<String,
         if (getClass() != obj.getClass()) {
             return false;
         }
-        ImportEffectiveStatementImpl other = (ImportEffectiveStatementImpl) obj;
-        if (getModuleName() == null) {
-            if (other.getModuleName() != null) {
-                return false;
-            }
-        } else if (!getModuleName().equals(other.getModuleName())) {
-            return false;
-        }
-        if (getRevision() == null) {
-            if (other.getRevision() != null) {
-                return false;
-            }
-        } else if (!getRevision().equals(other.getRevision())) {
-            return false;
-        }
-        if (getPrefix() == null) {
-            if (other.getPrefix() != null) {
-                return false;
-            }
-        } else if (!getPrefix().equals(other.getPrefix())) {
-            return false;
-        }
-        return true;
+        final ImportEffectiveStatementImpl other = (ImportEffectiveStatementImpl) obj;
+        return Objects.equals(moduleName, other.moduleName) && Objects.equals(revision, other.revision)
+                && Objects.equals(semVer, other.semVer) && Objects.equals(prefix, other.prefix)
+                && Objects.equals(description, other.description) && Objects.equals(reference, other.reference);
     }
 
     @Override
     public String toString() {
-        return ImportEffectiveStatementImpl.class.getSimpleName() + "[moduleName=" + moduleName + ", revision="
-                + revision + ", prefix=" + prefix + "]";
+        return MoreObjects.toStringHelper(this).add("moduleName", getModuleName())
+                .add("revision", getRevision()).add("semantic version", getSemanticVersion())
+                .add("prefix", getPrefix()).add("description", getDescription())
+                .add("reference", getReference()).toString();
     }
 }