Populate SubmoduleEffectiveModule with import namespaces
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / AbstractEffectiveModule.java
index 3b7dc1f8aa8f9de01a4b9a279704615bea88aea1..8eeb12bf3f5ab92d25ded239f91892bc3dcf1b8b 100644 (file)
@@ -7,26 +7,21 @@
  */
 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt;
 
-import static com.google.common.base.Preconditions.checkState;
+import static com.google.common.base.Verify.verifyNotNull;
 import static java.util.Objects.requireNonNull;
 
 import com.google.common.annotations.Beta;
 import com.google.common.base.MoreObjects;
-import com.google.common.base.Verify;
 import com.google.common.collect.ImmutableList;
-import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableMap.Builder;
 import com.google.common.collect.ImmutableSet;
-import com.google.common.collect.Iterables;
 import java.net.URI;
 import java.util.ArrayList;
-import java.util.HashSet;
-import java.util.LinkedHashMap;
+import java.util.Collection;
 import java.util.LinkedHashSet;
 import java.util.List;
-import java.util.Map;
 import java.util.Optional;
 import java.util.Set;
-import java.util.stream.Collectors;
 import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.yangtools.concepts.SemVer;
 import org.opendaylight.yangtools.openconfig.model.api.OpenConfigVersionEffectiveStatement;
@@ -34,7 +29,6 @@ import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.Revision;
 import org.opendaylight.yangtools.yang.common.YangVersion;
 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
-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;
@@ -45,77 +39,54 @@ import org.opendaylight.yangtools.yang.model.api.Module;
 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.BelongsToEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.ContactEffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.ImportEffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.OrganizationEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.PrefixEffectiveStatement;
-import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleEffectiveStatement;
-import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.PrefixStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.TypedefEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.YangVersionEffectiveStatement;
-import org.opendaylight.yangtools.yang.parser.spi.meta.MutableStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.compat.NotificationNodeContainerCompat;
 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.IncludedSubmoduleNameToModuleCtx;
+import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
+import org.opendaylight.yangtools.yang.parser.spi.source.ImportPrefixToModuleCtx;
 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
 
 @Beta
 public abstract class AbstractEffectiveModule<D extends DeclaredStatement<String>> extends
-        AbstractSchemaEffectiveDocumentedNode<String, D> implements Module, MutableStatement {
+        AbstractSchemaEffectiveDocumentedNode<String, D> implements Module,
+        NotificationNodeContainerCompat<String, D> {
     private final String name;
     private final String prefix;
     private final YangVersion yangVersion;
     private final String organization;
     private final String contact;
-    private final Set<ModuleImport> imports;
-    private final Set<FeatureDefinition> features;
-    private final Set<NotificationDefinition> notifications;
-    private final Set<AugmentationSchemaNode> augmentations;
-    private final Set<RpcDefinition> rpcs;
-    private final Set<Deviation> deviations;
-    private final List<ExtensionDefinition> extensionNodes;
-    private final Set<IdentitySchemaNode> identities;
-    private final @NonNull List<UnknownSchemaNode> unknownNodes;
-    private final Map<QName, DataSchemaNode> childNodes;
-    private final Set<GroupingDefinition> groupings;
-    private final Set<UsesNode> uses;
-    private final Set<TypeDefinition<?>> typeDefinitions;
-    private final Set<DataSchemaNode> publicChildNodes;
+    private final ImmutableSet<ModuleImport> imports;
+    private final ImmutableSet<FeatureDefinition> features;
+    private final @NonNull ImmutableSet<NotificationDefinition> notifications;
+    private final ImmutableSet<AugmentationSchemaNode> augmentations;
+    private final ImmutableSet<RpcDefinition> rpcs;
+    private final ImmutableSet<Deviation> deviations;
+    private final ImmutableList<ExtensionDefinition> extensionNodes;
+    private final ImmutableSet<IdentitySchemaNode> identities;
+    private final ImmutableSet<GroupingDefinition> groupings;
+    private final ImmutableSet<UsesNode> uses;
+    private final ImmutableSet<TypeDefinition<?>> typeDefinitions;
+    private final ImmutableSet<DataSchemaNode> publicChildNodes;
     private final SemVer semanticVersion;
 
-    private Set<StmtContext<?, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>>
-        submoduleContextsToBuild;
-    private Set<Module> submodules;
-    private boolean sealed;
-
-    protected AbstractEffectiveModule(final StmtContext<String, D, ? extends EffectiveStatement<String, ?>> ctx) {
+    protected AbstractEffectiveModule(
+            final @NonNull StmtContext<String, D, ? extends EffectiveStatement<String, ?>> ctx,
+            final @NonNull String prefix) {
         super(ctx);
 
         this.name = argument();
-
-        final EffectiveStatement<?, ?> parentOfPrefix;
-        if (ctx.getPublicDefinition() == YangStmtMapping.SUBMODULE) {
-            final Optional<BelongsToEffectiveStatement> optParent =
-                    findFirstEffectiveSubstatement(BelongsToEffectiveStatement.class);
-            SourceException.throwIf(!optParent.isPresent(), ctx.getStatementSourceReference(),
-                    "Unable to find belongs-to statement in submodule %s.", ctx.getStatementArgument());
-            parentOfPrefix = optParent.get();
-        } else {
-            parentOfPrefix = this;
-        }
-
-        final Optional<@NonNull PrefixEffectiveStatement> prefixStmt = parentOfPrefix.findFirstEffectiveSubstatement(
-            PrefixEffectiveStatement.class);
-        SourceException.throwIf(!prefixStmt.isPresent(), ctx.getStatementSourceReference(),
-                "Unable to resolve prefix for module or submodule %s.", ctx.getStatementArgument());
-        this.prefix = prefixStmt.get().argument();
+        this.prefix = requireNonNull(prefix);
         this.yangVersion = findFirstEffectiveSubstatementArgument(YangVersionEffectiveStatement.class)
                 .orElse(YangVersion.VERSION_1);
         this.semanticVersion = findFirstEffectiveSubstatementArgument(OpenConfigVersionEffectiveStatement.class)
@@ -125,85 +96,21 @@ public abstract class AbstractEffectiveModule<D extends DeclaredStatement<String
         this.contact = findFirstEffectiveSubstatementArgument(ContactEffectiveStatement.class)
                 .orElse(null);
 
-        // init submodules and substatements of submodules
-        final List<EffectiveStatement<?, ?>> substatementsOfSubmodules;
-        final Map<String, StmtContext<?, ?, ?>> includedSubmodulesMap = ctx
-                .getAllFromCurrentStmtCtxNamespace(IncludedSubmoduleNameToModuleCtx.class);
-
-        if (includedSubmodulesMap == null || includedSubmodulesMap.isEmpty()) {
-            this.submodules = ImmutableSet.of();
-            this.submoduleContextsToBuild = ImmutableSet.of();
-            substatementsOfSubmodules = ImmutableList.of();
-        } 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 Set<Module> submodulesInit = new HashSet<>();
-            final List<EffectiveStatement<?, ?>> substatementsOfSubmodulesInit = new ArrayList<>();
-            for (final StmtContext<?, ?, ?> submoduleCtx : includedSubmodulesMap.values()) {
-                final EffectiveStatement<?, ?> submodule = submoduleCtx.buildEffective();
-                Verify.verify(submodule instanceof SubmoduleEffectiveStatement);
-                Verify.verify(submodule instanceof Module, "Submodule statement %s is not a Module", submodule);
-                submodulesInit.add((Module) submodule);
-                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 StmtContext<?, ?, ?> submoduleCtx : includedSubmodulesMap.values()) {
-                submoduleContextsInit.add(
-                    (StmtContext<?, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>)submoduleCtx);
-            }
-
-            this.submoduleContextsToBuild = ImmutableSet.copyOf(submoduleContextsInit);
-            substatementsOfSubmodules = ImmutableList.of();
-        }
-
-        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);
-
-        final List<UnknownSchemaNode> unknownNodesInit = new ArrayList<>();
         final Set<AugmentationSchemaNode> 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 Set<ModuleImport> importsInit = new LinkedHashSet<>();
+        final Set<NotificationDefinition> notificationsInit = new LinkedHashSet<>();
+        final Set<RpcDefinition> rpcsInit = new LinkedHashSet<>();
+        final Set<Deviation> deviationsInit = new LinkedHashSet<>();
+        final Set<IdentitySchemaNode> identitiesInit = new LinkedHashSet<>();
+        final Set<FeatureDefinition> featuresInit = new LinkedHashSet<>();
         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<GroupingDefinition> mutableGroupings = new LinkedHashSet<>();
+        final Set<UsesNode> mutableUses = new LinkedHashSet<>();
         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);
-            }
+        for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
             if (effectiveStatement instanceof AugmentationSchemaNode) {
                 augmentationsInit.add((AugmentationSchemaNode) effectiveStatement);
             }
@@ -229,42 +136,23 @@ public abstract class AbstractEffectiveModule<D extends DeclaredStatement<String
                 extensionNodesInit.add((ExtensionDefinition) effectiveStatement);
             }
             if (effectiveStatement instanceof DataSchemaNode) {
-                final DataSchemaNode dataSchemaNode = (DataSchemaNode) effectiveStatement;
-                if (!mutableChildNodes.containsKey(dataSchemaNode.getQName())) {
-                    mutableChildNodes.put(dataSchemaNode.getQName(), dataSchemaNode);
-                    mutablePublicChildNodes.add(dataSchemaNode);
-                } else {
-                    throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
-                }
+                mutablePublicChildNodes.add((DataSchemaNode) effectiveStatement);
             }
-            if (effectiveStatement instanceof UsesNode) {
-                final UsesNode usesNode = (UsesNode) effectiveStatement;
-                if (!mutableUses.contains(usesNode)) {
-                    mutableUses.add(usesNode);
-                } else {
-                    throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
-                }
+            if (effectiveStatement instanceof UsesNode && !mutableUses.add((UsesNode) effectiveStatement)) {
+                throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
             }
             if (effectiveStatement instanceof TypedefEffectiveStatement) {
-                final TypedefEffectiveStatement typeDef = (TypedefEffectiveStatement) effectiveStatement;
-                final TypeDefinition<?> type = typeDef.getTypeDefinition();
-                if (!mutableTypeDefinitions.contains(type)) {
-                    mutableTypeDefinitions.add(type);
-                } else {
+                final TypeDefinition<?> type = ((TypedefEffectiveStatement) effectiveStatement).getTypeDefinition();
+                if (!mutableTypeDefinitions.add(type)) {
                     throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
                 }
             }
-            if (effectiveStatement instanceof GroupingDefinition) {
-                final GroupingDefinition grp = (GroupingDefinition) effectiveStatement;
-                if (!mutableGroupings.contains(grp)) {
-                    mutableGroupings.add(grp);
-                } else {
-                    throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
-                }
+            if (effectiveStatement instanceof GroupingDefinition
+                    && !mutableGroupings.add((GroupingDefinition) effectiveStatement)) {
+                throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
             }
         }
 
-        this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
         this.augmentations = ImmutableSet.copyOf(augmentationsInit);
         this.imports = ImmutableSet.copyOf(importsInit);
         this.notifications = ImmutableSet.copyOf(notificationsInit);
@@ -274,12 +162,10 @@ public abstract class AbstractEffectiveModule<D extends DeclaredStatement<String
         this.features = ImmutableSet.copyOf(featuresInit);
         this.extensionNodes = ImmutableList.copyOf(extensionNodesInit);
 
-        this.childNodes = ImmutableMap.copyOf(mutableChildNodes);
         this.groupings = ImmutableSet.copyOf(mutableGroupings);
         this.publicChildNodes = ImmutableSet.copyOf(mutablePublicChildNodes);
         this.typeDefinitions = ImmutableSet.copyOf(mutableTypeDefinitions);
         this.uses = ImmutableSet.copyOf(mutableUses);
-
     }
 
     @Override
@@ -318,81 +204,68 @@ public abstract class AbstractEffectiveModule<D extends DeclaredStatement<String
     }
 
     @Override
-    public Set<ModuleImport> getImports() {
+    public Collection<? extends ModuleImport> getImports() {
         return imports;
     }
 
     @Override
-    public Set<Module> getSubmodules() {
-        checkState(sealed, "Attempt to get base submodules from unsealed submodule effective statement %s",
-            getQNameModule());
-        return submodules;
-    }
-
-    @Override
-    public Set<FeatureDefinition> getFeatures() {
+    public Collection<? extends FeatureDefinition> getFeatures() {
         return features;
     }
 
     @Override
-    public Set<NotificationDefinition> getNotifications() {
+    public Collection<? extends NotificationDefinition> getNotifications() {
         return notifications;
     }
 
     @Override
-    public Set<AugmentationSchemaNode> getAugmentations() {
+    public Collection<? extends AugmentationSchemaNode> getAugmentations() {
         return augmentations;
     }
 
     @Override
-    public Set<RpcDefinition> getRpcs() {
+    public Collection<? extends RpcDefinition> getRpcs() {
         return rpcs;
     }
 
     @Override
-    public Set<Deviation> getDeviations() {
+    public Collection<? extends Deviation> getDeviations() {
         return deviations;
     }
 
     @Override
-    public List<ExtensionDefinition> getExtensionSchemaNodes() {
+    public Collection<? extends ExtensionDefinition> getExtensionSchemaNodes() {
         return extensionNodes;
     }
 
     @Override
-    public Set<IdentitySchemaNode> getIdentities() {
+    public Collection<? extends IdentitySchemaNode> getIdentities() {
         return identities;
     }
 
     @Override
-    public List<UnknownSchemaNode> getUnknownSchemaNodes() {
-        return unknownNodes;
-    }
-
-    @Override
-    public final Set<TypeDefinition<?>> getTypeDefinitions() {
+    public final Collection<? extends TypeDefinition<?>> getTypeDefinitions() {
         return typeDefinitions;
     }
 
     @Override
-    public final Set<DataSchemaNode> getChildNodes() {
+    public final Collection<? extends DataSchemaNode> getChildNodes() {
         return publicChildNodes;
     }
 
     @Override
-    public final Set<GroupingDefinition> getGroupings() {
+    public final Collection<? extends GroupingDefinition> getGroupings() {
         return groupings;
     }
 
     @Override
     @SuppressWarnings("checkstyle:hiddenField")
     public final Optional<DataSchemaNode> findDataChildByName(final QName name) {
-        // Child nodes are keyed by their container name, so we can do a direct lookup
-        return Optional.ofNullable(childNodes.get(requireNonNull(name)));
+        return findDataSchemaNode(name);
     }
 
     @Override
-    public Set<UsesNode> getUses() {
+    public Collection<? extends UsesNode> getUses() {
         return uses;
     }
 
@@ -412,13 +285,23 @@ public abstract class AbstractEffectiveModule<D extends DeclaredStatement<String
                 .toString();
     }
 
-    @Override
-    public void seal() {
-        if (!sealed) {
-            submodules = ImmutableSet.copyOf(Iterables.transform(submoduleContextsToBuild,
-                ctx -> (Module) ctx.buildEffective()));
-            submoduleContextsToBuild = ImmutableSet.of();
-            sealed = true;
-        }
+    protected static final @NonNull String findPrefix(final @NonNull StmtContext<?, ?, ?> ctx,
+            final String type, final String name) {
+        return SourceException.throwIfNull(
+            StmtContextUtils.firstAttributeOf(ctx.declaredSubstatements(), PrefixStatement.class),
+            ctx.getStatementSourceReference(), "Unable to resolve prefix for %s %s.", type, name);
+    }
+
+    // Alright. this is quite ugly
+    protected final void appendPrefixes(final StmtContext<?, ?, ?> ctx,
+            final Builder<String, ModuleEffectiveStatement> builder) {
+        streamEffectiveSubstatements(ImportEffectiveStatement.class)
+            .map(imp -> imp.findFirstEffectiveSubstatementArgument(PrefixEffectiveStatement.class).get())
+            .forEach(pfx -> {
+                final StmtContext<?, ?, ?> importedCtx =
+                        verifyNotNull(ctx.getFromNamespace(ImportPrefixToModuleCtx.class, pfx),
+                            "Failed to resolve prefix %s", pfx);
+                builder.put(pfx, (ModuleEffectiveStatement) importedCtx.buildEffective());
+            });
     }
 }