Remove Module.getSource() and SchemaContext.getModuleSource()
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / AbstractEffectiveModule.java
index 671d1a368f7a791a579d457757acea20a5a56d96..7e01ba968a44d99ac7afd28ae3eaac6634ba66ec 100644 (file)
@@ -7,24 +7,28 @@
  */
 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
 
+import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Iterables;
 import java.net.URI;
+import java.util.ArrayList;
 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 java.util.stream.Collectors;
+import javax.annotation.Nonnull;
+import org.opendaylight.yangtools.concepts.SemVer;
 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.common.YangVersion;
 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
+import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.Deviation;
 import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
@@ -36,31 +40,29 @@ 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.SchemaNode;
 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.YangStmtMapping;
 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleStatement;
-import org.opendaylight.yangtools.yang.model.util.ModuleImportImpl;
 import org.opendaylight.yangtools.yang.parser.spi.SubmoduleNamespace;
+import org.opendaylight.yangtools.yang.parser.spi.meta.MutableStatement;
 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.stmt.rfc6020.Utils;
+import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
 
 abstract class AbstractEffectiveModule<D extends DeclaredStatement<String>> extends
-        AbstractEffectiveDocumentedNode<String, D> implements Module, Immutable {
-
+        AbstractEffectiveDocumentedNode<String, D> implements Module, MutableStatement {
     private final String name;
-    private final String sourcePath;
     private final String prefix;
-    private final String yangVersion;
+    private final YangVersion yangVersion;
     private final String organization;
     private final String contact;
     private final Set<ModuleImport> imports;
-    private final Set<Module> submodules;
     private final Set<FeatureDefinition> features;
     private final Set<NotificationDefinition> notifications;
     private final Set<AugmentationSchema> augmentations;
@@ -74,29 +76,45 @@ abstract class AbstractEffectiveModule<D extends DeclaredStatement<String>> exte
     private final Set<UsesNode> uses;
     private final Set<TypeDefinition<?>> typeDefinitions;
     private final Set<DataSchemaNode> publicChildNodes;
+    private final SemVer semanticVersion;
+
+    private Set<StmtContext<?, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>>
+        submoduleContextsToBuild;
+    private Set<Module> submodules;
+    private boolean sealed;
 
     AbstractEffectiveModule(final StmtContext<String, D, ? extends EffectiveStatement<String, ?>> ctx) {
         super(ctx);
 
         this.name = argument();
 
-        PrefixEffectiveStatementImpl prefixStmt = firstEffective(PrefixEffectiveStatementImpl.class);
-        this.prefix = (prefixStmt == null) ? null : prefixStmt.argument();
+        EffectiveStatementBase<?, ?> parentOfPrefix = this;
+        if (ctx.getPublicDefinition() == YangStmtMapping.SUBMODULE) {
+            parentOfPrefix = firstEffective(BelongsToEffectiveStatementImpl.class);
+            SourceException.throwIfNull(parentOfPrefix, ctx.getStatementSourceReference(),
+                    "Unable to find belongs-to statement in submodule %s.", ctx.getStatementArgument());
+        }
 
-        YangVersionEffectiveStatementImpl yangVersionStmt = firstEffective(YangVersionEffectiveStatementImpl.class);
-        this.yangVersion = (yangVersionStmt == null) ? "1" : yangVersionStmt.argument();
+        final PrefixEffectiveStatementImpl prefixStmt = parentOfPrefix
+                .firstEffective(PrefixEffectiveStatementImpl.class);
+        SourceException.throwIfNull(prefixStmt, ctx.getStatementSourceReference(),
+                "Unable to resolve prefix for module or submodule %s.", ctx.getStatementArgument());
+        this.prefix = prefixStmt.argument();
 
-        OrganizationEffectiveStatementImpl organizationStmt = firstEffective(OrganizationEffectiveStatementImpl.class);
-        this.organization = (organizationStmt == null) ? null : organizationStmt.argument();
+        final YangVersionEffectiveStatementImpl yangVersionStmt =
+                firstEffective(YangVersionEffectiveStatementImpl.class);
+        this.yangVersion = yangVersionStmt == null ? YangVersion.VERSION_1 : yangVersionStmt.argument();
 
-        ContactEffectiveStatementImpl contactStmt = firstEffective(ContactEffectiveStatementImpl.class);
-        this.contact = (contactStmt == null) ? null : contactStmt.argument();
+        final OpenconfigVersionEffectiveStatementImpl semanticVersionStmt =
+                firstEffective(OpenconfigVersionEffectiveStatementImpl.class);
+        this.semanticVersion = semanticVersionStmt == null ? DEFAULT_SEMANTIC_VERSION : semanticVersionStmt.argument();
 
-        if (ctx.getStatementSourceReference() instanceof DeclarationInTextSource) {
-            this.sourcePath = ((DeclarationInTextSource) ctx.getStatementSourceReference()).getSourceName();
-        } else {
-            this.sourcePath = null;
-        }
+        final OrganizationEffectiveStatementImpl organizationStmt =
+                firstEffective(OrganizationEffectiveStatementImpl.class);
+        this.organization = organizationStmt == null ? null : organizationStmt.argument();
+
+        final ContactEffectiveStatementImpl contactStmt = firstEffective(ContactEffectiveStatementImpl.class);
+        this.contact = contactStmt == null ? null : contactStmt.argument();
 
         // init submodules and substatements of submodules
         final List<EffectiveStatement<?, ?>> substatementsOfSubmodules;
@@ -105,48 +123,80 @@ abstract class AbstractEffectiveModule<D extends DeclaredStatement<String>> exte
 
         if (includedSubmodulesMap == null || includedSubmodulesMap.isEmpty()) {
             this.submodules = ImmutableSet.of();
+            this.submoduleContextsToBuild = ImmutableSet.of();
             substatementsOfSubmodules = ImmutableList.of();
-        } else {
-            Collection<ModuleIdentifier> includedSubmodules = includedSubmodulesMap.values();
-            Set<Module> submodulesInit = new HashSet<>();
-            List<EffectiveStatement<?, ?>> substatementsOfSubmodulesInit = new LinkedList<>();
-            for (ModuleIdentifier submoduleIdentifier : includedSubmodules) {
+        } else if (YangStmtMapping.MODULE.equals(ctx.getPublicDefinition())) {
+            /*
+             * Aggregation of substatements from submodules should be done only
+             * for modules. In case of submodules it does not make sense because
+             * of possible circular chains of includes between submodules.
+             */
+            final Collection<ModuleIdentifier> includedSubmodules = includedSubmodulesMap.values();
+            final Set<Module> submodulesInit = new HashSet<>();
+            final List<EffectiveStatement<?, ?>> substatementsOfSubmodulesInit = new ArrayList<>();
+            for (final ModuleIdentifier submoduleIdentifier : includedSubmodules) {
                 @SuppressWarnings("unchecked")
-                Mutable<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>> submoduleCtx = (Mutable<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>) ctx
+                final Mutable<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>> submoduleCtx =
+                        (Mutable<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>) ctx
                         .getFromNamespace(SubmoduleNamespace.class, submoduleIdentifier);
-                SubmoduleEffectiveStatementImpl submodule = (SubmoduleEffectiveStatementImpl) submoduleCtx
+                final SubmoduleEffectiveStatementImpl submodule = (SubmoduleEffectiveStatementImpl) submoduleCtx
                         .buildEffective();
                 submodulesInit.add(submodule);
-                substatementsOfSubmodulesInit.addAll(submodule.effectiveSubstatements());
+                substatementsOfSubmodulesInit.addAll(submodule.effectiveSubstatements().stream()
+                        .filter(sub -> sub instanceof SchemaNode || sub instanceof DataNodeContainer)
+                        .collect(Collectors.toList()));
             }
 
             this.submodules = ImmutableSet.copyOf(submodulesInit);
+            this.submoduleContextsToBuild = ImmutableSet.of();
             substatementsOfSubmodules = ImmutableList.copyOf(substatementsOfSubmodulesInit);
+        } else {
+            /*
+             * Because of possible circular chains of includes between submodules we can
+             * collect only submodule contexts here and then build them during
+             * sealing of this statement.
+             */
+            final Set<StmtContext<?, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>>
+                submoduleContextsInit = new HashSet<>();
+            for (final ModuleIdentifier submoduleIdentifier : includedSubmodulesMap.values()) {
+                final StmtContext<?, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>> submoduleCtx =
+                        ctx.getFromNamespace(SubmoduleNamespace.class, submoduleIdentifier);
+                submoduleContextsInit.add(submoduleCtx);
+            }
+
+            this.submoduleContextsToBuild = ImmutableSet.copyOf(submoduleContextsInit);
+            substatementsOfSubmodules = ImmutableList.of();
         }
 
-        // init substatements collections
-        List<EffectiveStatement<?, ?>> effectiveSubstatements = new LinkedList<>();
+        if (!submoduleContextsToBuild.isEmpty()) {
+            ((Mutable<?, ?, ?>) ctx).addMutableStmtToSeal(this);
+            sealed = false;
+        } else {
+            sealed = true;
+        }
 
+        // init substatements collections
+        final List<EffectiveStatement<?, ?>> effectiveSubstatements = new ArrayList<>();
         effectiveSubstatements.addAll(effectiveSubstatements());
         effectiveSubstatements.addAll(substatementsOfSubmodules);
 
-        List<UnknownSchemaNode> unknownNodesInit = new LinkedList<>();
-        Set<AugmentationSchema> augmentationsInit = new HashSet<>();
-        Set<ModuleImport> importsInit = new HashSet<>();
-        Set<NotificationDefinition> notificationsInit = new HashSet<>();
-        Set<RpcDefinition> rpcsInit = new HashSet<>();
-        Set<Deviation> deviationsInit = new HashSet<>();
-        Set<IdentitySchemaNode> identitiesInit = new HashSet<>();
-        Set<FeatureDefinition> featuresInit = new HashSet<>();
-        List<ExtensionDefinition> extensionNodesInit = new LinkedList<>();
-
-        Map<QName, DataSchemaNode> mutableChildNodes = new LinkedHashMap<>();
-        Set<GroupingDefinition> mutableGroupings = new HashSet<>();
-        Set<UsesNode> mutableUses = new HashSet<>();
-        Set<TypeDefinition<?>> mutableTypeDefinitions = new LinkedHashSet<>();
-        Set<DataSchemaNode> mutablePublicChildNodes = new LinkedHashSet<>();
-
-        for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
+        final List<UnknownSchemaNode> unknownNodesInit = new ArrayList<>();
+        final Set<AugmentationSchema> augmentationsInit = new LinkedHashSet<>();
+        final Set<ModuleImport> importsInit = new HashSet<>();
+        final Set<NotificationDefinition> notificationsInit = new HashSet<>();
+        final Set<RpcDefinition> rpcsInit = new HashSet<>();
+        final Set<Deviation> deviationsInit = new HashSet<>();
+        final Set<IdentitySchemaNode> identitiesInit = new HashSet<>();
+        final Set<FeatureDefinition> featuresInit = new HashSet<>();
+        final List<ExtensionDefinition> extensionNodesInit = new ArrayList<>();
+
+        final Map<QName, DataSchemaNode> mutableChildNodes = new LinkedHashMap<>();
+        final Set<GroupingDefinition> mutableGroupings = new HashSet<>();
+        final Set<UsesNode> mutableUses = new HashSet<>();
+        final Set<TypeDefinition<?>> mutableTypeDefinitions = new LinkedHashSet<>();
+        final Set<DataSchemaNode> mutablePublicChildNodes = new LinkedHashSet<>();
+
+        for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
             if (effectiveStatement instanceof UnknownSchemaNode) {
                 unknownNodesInit.add((UnknownSchemaNode) effectiveStatement);
             }
@@ -171,11 +221,11 @@ abstract class AbstractEffectiveModule<D extends DeclaredStatement<String>> exte
             if (effectiveStatement instanceof FeatureDefinition) {
                 featuresInit.add((FeatureDefinition) effectiveStatement);
             }
-            if (effectiveStatement instanceof ExtensionDefinition) {
-                extensionNodesInit.add((ExtensionDefinition) effectiveStatement);
+            if (effectiveStatement instanceof ExtensionEffectiveStatementImpl) {
+                extensionNodesInit.add((ExtensionEffectiveStatementImpl) effectiveStatement);
             }
             if (effectiveStatement instanceof DataSchemaNode) {
-                DataSchemaNode dataSchemaNode = (DataSchemaNode) effectiveStatement;
+                final DataSchemaNode dataSchemaNode = (DataSchemaNode) effectiveStatement;
                 if (!mutableChildNodes.containsKey(dataSchemaNode.getQName())) {
                     mutableChildNodes.put(dataSchemaNode.getQName(), dataSchemaNode);
                     mutablePublicChildNodes.add(dataSchemaNode);
@@ -184,7 +234,7 @@ abstract class AbstractEffectiveModule<D extends DeclaredStatement<String>> exte
                 }
             }
             if (effectiveStatement instanceof UsesNode) {
-                UsesNode usesNode = (UsesNode) effectiveStatement;
+                final UsesNode usesNode = (UsesNode) effectiveStatement;
                 if (!mutableUses.contains(usesNode)) {
                     mutableUses.add(usesNode);
                 } else {
@@ -192,8 +242,8 @@ abstract class AbstractEffectiveModule<D extends DeclaredStatement<String>> exte
                 }
             }
             if (effectiveStatement instanceof TypeDefEffectiveStatementImpl) {
-                TypeDefEffectiveStatementImpl typeDef = (TypeDefEffectiveStatementImpl) effectiveStatement;
-                TypeDefinition<?> type = typeDef.getTypeDefinition();
+                final TypeDefEffectiveStatementImpl typeDef = (TypeDefEffectiveStatementImpl) effectiveStatement;
+                final TypeDefinition<?> type = typeDef.getTypeDefinition();
                 if (!mutableTypeDefinitions.contains(type)) {
                     mutableTypeDefinitions.add(type);
                 } else {
@@ -201,7 +251,7 @@ abstract class AbstractEffectiveModule<D extends DeclaredStatement<String>> exte
                 }
             }
             if (effectiveStatement instanceof GroupingDefinition) {
-                GroupingDefinition grp = (GroupingDefinition) effectiveStatement;
+                final GroupingDefinition grp = (GroupingDefinition) effectiveStatement;
                 if (!mutableGroupings.contains(grp)) {
                     mutableGroupings.add(grp);
                 } else {
@@ -212,7 +262,7 @@ abstract class AbstractEffectiveModule<D extends DeclaredStatement<String>> exte
 
         this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
         this.augmentations = ImmutableSet.copyOf(augmentationsInit);
-        this.imports = ImmutableSet.copyOf(resolveModuleImports(importsInit, ctx));
+        this.imports = ImmutableSet.copyOf(importsInit);
         this.notifications = ImmutableSet.copyOf(notificationsInit);
         this.rpcs = ImmutableSet.copyOf(rpcsInit);
         this.deviations = ImmutableSet.copyOf(deviationsInit);
@@ -228,34 +278,6 @@ abstract class AbstractEffectiveModule<D extends DeclaredStatement<String>> exte
 
     }
 
-    private static Set<ModuleImport> resolveModuleImports(final Set<ModuleImport> importsInit,
-            final StmtContext<String, ? extends DeclaredStatement<String>, ? extends EffectiveStatement<String, ?>> 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;
-    }
-
-    @Override
-    public String getSource() {
-        return null;
-    }
-
     @Override
     public URI getNamespace() {
         return getQNameModule().getNamespace();
@@ -278,7 +300,7 @@ abstract class AbstractEffectiveModule<D extends DeclaredStatement<String>> exte
 
     @Override
     public String getYangVersion() {
-        return yangVersion;
+        return yangVersion.toString();
     }
 
     @Override
@@ -298,6 +320,8 @@ abstract class AbstractEffectiveModule<D extends DeclaredStatement<String>> exte
 
     @Override
     public Set<Module> getSubmodules() {
+        Preconditions.checkState(sealed,
+            "Attempt to get base submodules from unsealed submodule effective statement %s", getQNameModule());
         return submodules;
     }
 
@@ -336,6 +360,7 @@ abstract class AbstractEffectiveModule<D extends DeclaredStatement<String>> exte
         return identities;
     }
 
+    @Nonnull
     @Override
     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
         return unknownNodes;
@@ -364,31 +389,33 @@ abstract class AbstractEffectiveModule<D extends DeclaredStatement<String>> exte
     }
 
     @Override
-    public final DataSchemaNode getDataChildByName(final String name) {
-        for (DataSchemaNode node : childNodes.values()) {
-            if (node.getQName().getLocalName().equals(name)) {
-                return node;
-            }
-        }
-        return null;
+    public Set<UsesNode> getUses() {
+        return uses;
     }
 
     @Override
-    public Set<UsesNode> getUses() {
-        return uses;
+    public SemVer getSemanticVersion() {
+        return semanticVersion;
     }
 
     @Override
     public String toString() {
-        StringBuilder sb = new StringBuilder(this.getClass().getSimpleName());
-        sb.append("[");
-        sb.append("name=").append(name);
-        sb.append(", namespace=").append(getNamespace());
-        sb.append(", revision=").append(getRevision());
-        sb.append(", prefix=").append(prefix);
-        sb.append(", yangVersion=").append(yangVersion);
-        sb.append("]");
-        return sb.toString();
+        return this.getClass().getSimpleName() + "["
+                + "name=" + name
+                + ", namespace=" + getNamespace()
+                + ", revision=" + getQNameModule().getFormattedRevision()
+                + ", prefix=" + prefix
+                + ", yangVersion=" + yangVersion
+                + "]";
     }
 
+    @Override
+    public void seal() {
+        if (!sealed) {
+            submodules = ImmutableSet.copyOf(Iterables.transform(submoduleContextsToBuild,
+                ctx -> (Module) ctx.buildEffective()));
+            submoduleContextsToBuild = ImmutableSet.of();
+            sealed = true;
+        }
+    }
 }