Cleanup yang-parser-impl
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / ModuleEffectiveStatementImpl.java
index db3f31110a7dbdf42e67d04cb380fd28820b12d1..a6aeb7ca92147ecb65fe176c5788b4401f567cfd 100644 (file)
@@ -7,56 +7,58 @@
  */
 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
 
-import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName;
-
-import org.opendaylight.yangtools.yang.model.api.stmt.ModuleStatement;
 import com.google.common.collect.ImmutableList;
-import com.google.common.collect.ImmutableSet;
-import org.opendaylight.yangtools.yang.model.util.ExtendedType;
-import java.util.LinkedHashSet;
-import java.util.LinkedHashMap;
 import com.google.common.collect.ImmutableMap;
-import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
-import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
-import org.opendaylight.yangtools.yang.model.api.UsesNode;
-import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
-import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleStatement;
-import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
-import org.opendaylight.yangtools.yang.parser.spi.SubmoduleNamespace;
-import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
-import java.util.Map;
-import org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToIdentifier;
+import com.google.common.collect.ImmutableSet;
 import java.net.URI;
 import java.util.Collection;
 import java.util.Date;
 import java.util.HashSet;
+import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
 import java.util.LinkedList;
 import java.util.List;
+import java.util.Map;
 import java.util.Set;
 import org.opendaylight.yangtools.concepts.Immutable;
+import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.QNameModule;
+import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
+import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.Deviation;
 import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
 import org.opendaylight.yangtools.yang.model.api.FeatureDefinition;
+import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
 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.NotificationDefinition;
 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
+import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.UsesNode;
 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.SubmoduleStatement;
+import org.opendaylight.yangtools.yang.model.util.ExtendedType;
+import org.opendaylight.yangtools.yang.model.util.ModuleImportImpl;
+import org.opendaylight.yangtools.yang.parser.spi.SubmoduleNamespace;
 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.source.DeclarationInTextSource;
+import org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToIdentifier;
+import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName;
+import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
 
 public class ModuleEffectiveStatementImpl extends
-        AbstractEffectiveDocumentedNode<String, ModuleStatement>
-        implements Module, Immutable {
+        AbstractEffectiveDocumentedNode<String, ModuleStatement> implements
+        Module, Immutable {
 
     private final QNameModule qNameModule;
     private final String name;
-    private String sourcePath; // TODO fill
+    private final String sourcePath;
     private String prefix;
     private String yangVersion;
     private String organization;
@@ -71,8 +73,8 @@ public class ModuleEffectiveStatementImpl extends
     private ImmutableList<ExtensionDefinition> extensionNodes;
     private ImmutableSet<IdentitySchemaNode> identities;
     private ImmutableList<UnknownSchemaNode> unknownNodes;
-    private String source;
-    private ImmutableList<EffectiveStatement<?,?>> substatementsOfSubmodules;
+    private final String source;
+    private ImmutableList<EffectiveStatement<?, ?>> substatementsOfSubmodules;
 
     private ImmutableMap<QName, DataSchemaNode> childNodes;
     private ImmutableSet<GroupingDefinition> groupings;
@@ -81,7 +83,7 @@ public class ModuleEffectiveStatementImpl extends
     private ImmutableSet<DataSchemaNode> publicChildNodes;
 
     public ModuleEffectiveStatementImpl(
-            StmtContext<String, ModuleStatement, EffectiveStatement<String, ModuleStatement>> ctx) {
+            final StmtContext<String, ModuleStatement, EffectiveStatement<String, ModuleStatement>> ctx) {
         super(ctx);
 
         name = argument();
@@ -111,15 +113,20 @@ public class ModuleEffectiveStatementImpl extends
             }
         }
 
-        // TODO init source, sourcePath
-        source = ctx.getStatementSource().name();
+        if (yangVersion == null) {
+            yangVersion = "1";
+        }
+
+        DeclarationInTextSource sourceReference = (DeclarationInTextSource) ctx.getStatementSourceReference();
+        sourcePath = sourceReference.getSourceName();
+        source = sourceReference.getSourceText();
 
         initSubmodules(ctx);
         initSubstatementCollections(ctx);
     }
 
     private void initSubmodules(
-            StmtContext<String, ModuleStatement, EffectiveStatement<String, ModuleStatement>> ctx) {
+            final StmtContext<String, ModuleStatement, EffectiveStatement<String, ModuleStatement>> ctx) {
         Map<String, ModuleIdentifier> includedSubmodulesMap = ctx
                 .getAllFromCurrentStmtCtxNamespace(IncludedSubmoduleNameToIdentifier.class);
 
@@ -133,21 +140,25 @@ public class ModuleEffectiveStatementImpl extends
                 .values();
 
         Set<Module> submodulesInit = new HashSet<>();
-        List<EffectiveStatement<?,?>> substatementsOfSubmodulesInit = new LinkedList<>();
+        List<EffectiveStatement<?, ?>> substatementsOfSubmodulesInit = new LinkedList<>();
         for (ModuleIdentifier submoduleIdentifier : includedSubmodules) {
             Mutable<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>> submoduleCtx = (Mutable<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>) ctx
                     .getFromNamespace(SubmoduleNamespace.class,
                             submoduleIdentifier);
-            SubmoduleEffectiveStatementImpl submodule = (SubmoduleEffectiveStatementImpl) submoduleCtx.buildEffective();
+            SubmoduleEffectiveStatementImpl submodule = (SubmoduleEffectiveStatementImpl) submoduleCtx
+                    .buildEffective();
             submodulesInit.add(submodule);
-            substatementsOfSubmodulesInit.addAll(submodule.effectiveSubstatements());
+            substatementsOfSubmodulesInit.addAll(submodule
+                    .effectiveSubstatements());
         }
 
         this.submodules = ImmutableSet.copyOf(submodulesInit);
-        this.substatementsOfSubmodules = ImmutableList.copyOf(substatementsOfSubmodulesInit);
+        this.substatementsOfSubmodules = ImmutableList
+                .copyOf(substatementsOfSubmodulesInit);
     }
 
-    private void initSubstatementCollections(StmtContext<String, ModuleStatement, EffectiveStatement<String, ModuleStatement>> ctx) {
+    private void initSubstatementCollections(
+            final StmtContext<String, ModuleStatement, EffectiveStatement<String, ModuleStatement>> ctx) {
         List<EffectiveStatement<?, ?>> effectiveSubstatements = new LinkedList<>();
 
         effectiveSubstatements.addAll(effectiveSubstatements());
@@ -206,7 +217,9 @@ public class ModuleEffectiveStatementImpl extends
                             dataSchemaNode);
                     mutablePublicChildNodes.add(dataSchemaNode);
                 } else {
-                    throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
+                    throw EffectiveStmtUtils
+                            .createNameCollisionSourceException(ctx,
+                                    effectiveStatement);
                 }
             }
             if (effectiveStatement instanceof UsesNode) {
@@ -214,7 +227,9 @@ public class ModuleEffectiveStatementImpl extends
                 if (!mutableUses.contains(usesNode)) {
                     mutableUses.add(usesNode);
                 } else {
-                    throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
+                    throw EffectiveStmtUtils
+                            .createNameCollisionSourceException(ctx,
+                                    effectiveStatement);
                 }
             }
             if (effectiveStatement instanceof TypeDefEffectiveStatementImpl) {
@@ -223,7 +238,9 @@ public class ModuleEffectiveStatementImpl extends
                 if (!mutableTypeDefinitions.contains(extendedType)) {
                     mutableTypeDefinitions.add(extendedType);
                 } else {
-                    throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
+                    throw EffectiveStmtUtils
+                            .createNameCollisionSourceException(ctx,
+                                    effectiveStatement);
                 }
             }
             if (effectiveStatement instanceof GroupingDefinition) {
@@ -231,14 +248,17 @@ public class ModuleEffectiveStatementImpl extends
                 if (!mutableGroupings.contains(grp)) {
                     mutableGroupings.add(grp);
                 } else {
-                    throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
+                    throw EffectiveStmtUtils
+                            .createNameCollisionSourceException(ctx,
+                                    effectiveStatement);
                 }
             }
         }
 
         this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
         this.augmentations = ImmutableSet.copyOf(augmentationsInit);
-        this.imports = ImmutableSet.copyOf(importsInit);
+        this.imports = ImmutableSet.copyOf(resolveModuleImports(importsInit,
+                ctx));
         this.notifications = ImmutableSet.copyOf(notificationsInit);
         this.rpcs = ImmutableSet.copyOf(rpcsInit);
         this.deviations = ImmutableSet.copyOf(deviationsInit);
@@ -253,6 +273,30 @@ public class ModuleEffectiveStatementImpl extends
         this.uses = ImmutableSet.copyOf(mutableUses);
     }
 
+    private static Set<ModuleImport> resolveModuleImports(
+            final Set<ModuleImport> importsInit,
+            final StmtContext<String, ModuleStatement, EffectiveStatement<String, ModuleStatement>> ctx) {
+        Set<ModuleImport> resolvedModuleImports = new LinkedHashSet<>();
+        for (ModuleImport moduleImport : importsInit) {
+            if (moduleImport.getRevision().equals(
+                    SimpleDateFormatUtil.DEFAULT_DATE_IMP)) {
+                QNameModule impModuleQName = Utils.getModuleQNameByPrefix(ctx,
+                        moduleImport.getPrefix());
+                if (!impModuleQName.getRevision().equals(
+                        SimpleDateFormatUtil.DEFAULT_DATE_REV)) {
+                    ModuleImport resolvedModuleImport = new ModuleImportImpl(
+                            moduleImport.getModuleName(),
+                            impModuleQName.getRevision(),
+                            moduleImport.getPrefix());
+                    resolvedModuleImports.add(resolvedModuleImport);
+                }
+            } else {
+                resolvedModuleImports.add(moduleImport);
+            }
+        }
+        return resolvedModuleImports;
+    }
+
     @Override
     public String getModuleSourcePath() {
         return sourcePath;
@@ -430,7 +474,8 @@ public class ModuleEffectiveStatementImpl extends
 
     @Override
     public String toString() {
-        StringBuilder sb = new StringBuilder(ModuleEffectiveStatementImpl.class.getSimpleName());
+        StringBuilder sb = new StringBuilder(
+                ModuleEffectiveStatementImpl.class.getSimpleName());
         sb.append("[");
         sb.append("name=").append(name);
         sb.append(", namespace=").append(getNamespace());