Bug 3670 (part 1/5): Use of new statement parser in yang-maven-plugin
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / ImportStatementDefinition.java
index c1fc892c0ef359b4c8385b93875b5852dc9969d9..05b74316906d8fc4b16445eb424246d5d092a569 100644 (file)
@@ -9,6 +9,12 @@ package org.opendaylight.yangtools.yang.parser.stmt.rfc6020;
 
 import static org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase.SOURCE_LINKAGE;
 import static org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils.firstAttributeOf;
+
+import java.util.Set;
+
+import java.util.Map.Entry;
+import java.util.Map;
+import org.opendaylight.yangtools.yang.model.api.stmt.ModuleStatement;
 import com.google.common.base.Optional;
 import java.net.URI;
 import java.util.Collection;
@@ -33,7 +39,8 @@ import org.opendaylight.yangtools.yang.parser.spi.source.ImpPrefixToModuleIdenti
 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.ImportEffectiveStatementImpl;
 
-public class ImportStatementDefinition extends
+public class ImportStatementDefinition
+        extends
         AbstractStatementSupport<String, ImportStatement, EffectiveStatement<String, ImportStatement>> {
 
     public ImportStatementDefinition() {
@@ -46,7 +53,8 @@ public class ImportStatementDefinition extends
     }
 
     @Override
-    public ImportStatement createDeclared(StmtContext<String, ImportStatement, ?> ctx) {
+    public ImportStatement createDeclared(
+            StmtContext<String, ImportStatement, ?> ctx) {
         return new ImportStatementImpl(ctx);
     }
 
@@ -61,43 +69,94 @@ public class ImportStatementDefinition extends
             final Mutable<String, ImportStatement, EffectiveStatement<String, ImportStatement>> stmt)
             throws SourceException {
         final ModuleIdentifier impIdentifier = getImportedModuleIdentifier(stmt);
-        ModelActionBuilder importAction = stmt.newInferenceAction(SOURCE_LINKAGE);
+        ModelActionBuilder importAction = stmt
+                .newInferenceAction(SOURCE_LINKAGE);
         final Prerequisite<StmtContext<?, ?, ?>> imported;
         final Prerequisite<Mutable<?, ?, ?>> linkageTarget;
-        imported = importAction.requiresCtx(stmt, ModuleNamespace.class, impIdentifier, SOURCE_LINKAGE);
+        imported = importAction.requiresCtx(stmt, ModuleNamespace.class,
+                impIdentifier, SOURCE_LINKAGE);
         linkageTarget = importAction.mutatesCtx(stmt.getRoot(), SOURCE_LINKAGE);
 
-        String impPrefix = firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class);
-        stmt.addToNs(ImpPrefixToModuleIdentifier.class, impPrefix, impIdentifier);
-
         importAction.apply(new InferenceAction() {
 
             @Override
             public void apply() throws InferenceException {
-                StmtContext<?, ?, ?> importedModule = imported.get();
-                linkageTarget.get().addToNs(ImportedModuleContext.class, impIdentifier, importedModule);
+                StmtContext<?, ?, ?> importedModule = null;
+                ModuleIdentifier importedModuleIdentifier = null;
+                if (impIdentifier.getRevision() == SimpleDateFormatUtil.DEFAULT_DATE_IMP) {
+                    Entry<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> recentModuleEntry = findRecentModule(
+                            impIdentifier,
+                            stmt.getAllFromNamespace(ModuleNamespace.class));
+                    if (recentModuleEntry != null) {
+                        importedModuleIdentifier = recentModuleEntry.getKey();
+                        importedModule = recentModuleEntry.getValue();
+                    }
+                }
+
+                if(importedModule == null || importedModuleIdentifier == null) {
+                    importedModule = imported.get();
+                    importedModuleIdentifier = impIdentifier;
+                }
+
+                linkageTarget.get().addToNs(ImportedModuleContext.class,
+                        importedModuleIdentifier, importedModule);
+                String impPrefix = firstAttributeOf(stmt.declaredSubstatements(),
+                        PrefixStatement.class);
+                stmt.addToNs(ImpPrefixToModuleIdentifier.class, impPrefix,
+                        importedModuleIdentifier);
+            }
+
+            private Entry<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> findRecentModule(
+                    ModuleIdentifier impIdentifier,
+                    Map<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> allModules) {
+
+                ModuleIdentifier recentModuleIdentifier = impIdentifier;
+                Entry<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> recentModuleEntry = null;
+
+                Set<Entry<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>>> moduleEntrySet = allModules
+                        .entrySet();
+                for (Entry<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> moduleEntry : moduleEntrySet) {
+                    if (moduleEntry.getKey().getName()
+                            .equals(impIdentifier.getName())
+                            && moduleEntry
+                                    .getKey()
+                                    .getRevision()
+                                    .compareTo(
+                                            recentModuleIdentifier
+                                                    .getRevision()) > 0) {
+                        recentModuleIdentifier = moduleEntry.getKey();
+                        recentModuleEntry = moduleEntry;
+                    }
+                }
+
+                return recentModuleEntry;
             }
 
             @Override
-            public void prerequisiteFailed(Collection<? extends Prerequisite<?>> failed) throws InferenceException {
+            public void prerequisiteFailed(
+                    Collection<? extends Prerequisite<?>> failed)
+                    throws InferenceException {
                 if (failed.contains(imported)) {
-                    throw new InferenceException(String.format("Imported module [%s] was not found.", impIdentifier),
-                            stmt.getStatementSourceReference());
+                    throw new InferenceException(String.format(
+                            "Imported module [%s] was not found.",
+                            impIdentifier), stmt.getStatementSourceReference());
                 }
             }
         });
     }
 
-    private static ModuleIdentifier getImportedModuleIdentifier(Mutable<String, ImportStatement, ?> stmt)
-            throws SourceException {
+    private static ModuleIdentifier getImportedModuleIdentifier(
+            Mutable<String, ImportStatement, ?> stmt) throws SourceException {
 
         String moduleName = stmt.getStatementArgument();
-        Date revision = firstAttributeOf(stmt.declaredSubstatements(), RevisionDateStatement.class);
+        Date revision = firstAttributeOf(stmt.declaredSubstatements(),
+                RevisionDateStatement.class);
         if (revision == null) {
             revision = SimpleDateFormatUtil.DEFAULT_DATE_IMP;
         }
 
-        return new ModuleIdentifierImpl(moduleName, Optional.<URI> absent(), Optional.<Date> of(revision));
+        return new ModuleIdentifierImpl(moduleName, Optional.<URI> absent(),
+                Optional.<Date> of(revision));
     }
 
 }
\ No newline at end of file