BUG-7052: remove StmtContext.createCopy()
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / ImportEffectiveStatementImpl.java
index da2e3f65442a28050f6be68c106f967868ff7a40..4390debe0dec043ef5664b32d6776988a3cfd8cf 100644 (file)
@@ -11,14 +11,16 @@ import com.google.common.base.MoreObjects;
 import java.util.Date;
 import java.util.Objects;
 import org.opendaylight.yangtools.concepts.SemVer;
-import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
+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.model.api.ModuleImport;
 import org.opendaylight.yangtools.yang.model.api.stmt.ImportStatement;
 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.ImpPrefixToOpenconfigVerModuleIdentifier;
+import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
+import org.opendaylight.yangtools.yang.parser.spi.source.ImpPrefixToSemVerModuleIdentifier;
+import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
 
 public class ImportEffectiveStatementImpl extends DeclaredEffectiveStatementBase<String, ImportStatement> implements
         ModuleImport {
@@ -34,7 +36,7 @@ public class ImportEffectiveStatementImpl extends DeclaredEffectiveStatementBase
         super(ctx);
 
         moduleName = ctx.getStatementArgument();
-        PrefixEffectiveStatementImpl prefixStmt = firstEffective(PrefixEffectiveStatementImpl.class);
+        final PrefixEffectiveStatementImpl prefixStmt = firstEffective(PrefixEffectiveStatementImpl.class);
         if (prefixStmt != null) {
             this.prefix = prefixStmt.argument();
         } else {
@@ -42,24 +44,35 @@ public class ImportEffectiveStatementImpl extends DeclaredEffectiveStatementBase
                     ctx.getStatementSourceReference());
         }
 
-        if (!ctx.isEnabledOpenconfigVersioning()) {
-            RevisionDateEffectiveStatementImpl revisionDateStmt = firstEffective(RevisionDateEffectiveStatementImpl.class);
-            this.revision = (revisionDateStmt == null) ? SimpleDateFormatUtil.DEFAULT_DATE_IMP : revisionDateStmt
+        if (!ctx.isEnabledSemanticVersioning()) {
+            final RevisionDateEffectiveStatementImpl revisionDateStmt = firstEffective(RevisionDateEffectiveStatementImpl.class);
+            this.revision = (revisionDateStmt == null) ? getImportedRevision(ctx) : revisionDateStmt
                     .argument();
-            this.semVer = Module.DEFAULT_OPENCONFIG_VERSION;
+            this.semVer = Module.DEFAULT_SEMANTIC_VERSION;
         } else {
-            ModuleIdentifier importedModuleIdentifier = ctx.getFromNamespace(ImpPrefixToOpenconfigVerModuleIdentifier.class, prefix);
+            final ModuleIdentifier importedModuleIdentifier = ctx.getFromNamespace(ImpPrefixToSemVerModuleIdentifier.class, prefix);
             revision = importedModuleIdentifier.getRevision();
-            semVer = importedModuleIdentifier.getOpenconfigVersion();
+            semVer = importedModuleIdentifier.getSemanticVersion();
         }
 
-        DescriptionEffectiveStatementImpl descriptionStmt = firstEffective(DescriptionEffectiveStatementImpl.class);
+        final DescriptionEffectiveStatementImpl descriptionStmt = firstEffective(DescriptionEffectiveStatementImpl.class);
         this.description = (descriptionStmt != null) ? descriptionStmt.argument() : null;
 
-        ReferenceEffectiveStatementImpl referenceStmt = firstEffective(ReferenceEffectiveStatementImpl.class);
+        final ReferenceEffectiveStatementImpl referenceStmt = firstEffective(ReferenceEffectiveStatementImpl.class);
         this.reference = (referenceStmt != null) ? referenceStmt.argument() : null;
     }
 
+    private Date 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();
+    }
+
     @Override
     public String getModuleName() {
         return moduleName;
@@ -71,7 +84,7 @@ public class ImportEffectiveStatementImpl extends DeclaredEffectiveStatementBase
     }
 
     @Override
-    public SemVer getOpenconfigVersion() {
+    public SemVer getSemanticVersion() {
         return semVer;
     }
 
@@ -106,7 +119,7 @@ public class ImportEffectiveStatementImpl extends DeclaredEffectiveStatementBase
         if (getClass() != obj.getClass()) {
             return false;
         }
-        ImportEffectiveStatementImpl other = (ImportEffectiveStatementImpl) obj;
+        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);
@@ -115,7 +128,7 @@ public class ImportEffectiveStatementImpl extends DeclaredEffectiveStatementBase
     @Override
     public String toString() {
         return MoreObjects.toStringHelper(this).add("moduleName", getModuleName())
-                .add("revision", getRevision()).add("openconfig version", getOpenconfigVersion())
+                .add("revision", getRevision()).add("semantic version", getSemanticVersion())
                 .add("prefix", getPrefix()).add("description", getDescription())
                 .add("reference", getReference()).toString();
     }