Do not use InstanceIdentifier.getPathArguments()
[yangtools.git] / parser / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / AbstractEffectiveModule.java
index 5335338f4dd50114e8e7cae93565c4139cd6db54..0871de975e3a429d1560168e9da11f6e722a9d65 100644 (file)
@@ -15,13 +15,12 @@ import com.google.common.collect.Collections2;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap.Builder;
 import com.google.common.collect.ImmutableSet;
+import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 import java.util.Collection;
 import java.util.LinkedHashSet;
 import java.util.Optional;
 import java.util.Set;
 import org.eclipse.jdt.annotation.NonNull;
-import org.opendaylight.yangtools.concepts.SemVer;
-import org.opendaylight.yangtools.openconfig.model.api.OpenConfigVersionEffectiveStatement;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.UnresolvedQName.Unqualified;
 import org.opendaylight.yangtools.yang.common.YangVersion;
@@ -51,22 +50,23 @@ import org.opendaylight.yangtools.yang.model.api.stmt.YangVersionEffectiveStatem
 import org.opendaylight.yangtools.yang.model.api.stmt.compat.NotificationNodeContainerCompat;
 import org.opendaylight.yangtools.yang.model.spi.meta.AbstractDeclaredEffectiveStatement.DefaultWithDataTree.WithTypedefNamespace;
 import org.opendaylight.yangtools.yang.model.spi.meta.EffectiveStatementMixins.DocumentedNodeMixin;
+import org.opendaylight.yangtools.yang.parser.spi.ParserNamespaces;
 import org.opendaylight.yangtools.yang.parser.spi.meta.CommonStmtCtx;
 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
-import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
-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<Unqualified>,
         E extends DataTreeAwareEffectiveStatement<Unqualified, D>>
-        extends WithTypedefNamespace<Unqualified, D, E>
+        extends WithTypedefNamespace<Unqualified, D>
         implements ModuleLike, DocumentedNodeMixin<Unqualified, D>, NotificationNodeContainerCompat<Unqualified, D, E> {
     private final String prefix;
     private final ImmutableSet<GroupingDefinition> groupings;
     private final ImmutableSet<UsesNode> uses;
     private final ImmutableSet<TypeDefinition<?>> typeDefinitions;
 
+    // FIXME: these should be detected in inference
+    @SuppressFBWarnings(value = "CT_CONSTRUCTOR_THROW", justification = "Legacy namespace indexing")
     protected AbstractEffectiveModule(final Current<Unqualified, D> stmt,
             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements, final String prefix) {
         super(stmt.declared(), substatements);
@@ -77,25 +77,22 @@ public abstract class AbstractEffectiveModule<D extends DeclaredStatement<Unqual
         final Set<UsesNode> mutableUses = new LinkedHashSet<>();
         final Set<TypeDefinition<?>> mutableTypeDefinitions = new LinkedHashSet<>();
 
-        for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
-            if (effectiveStatement instanceof UsesNode && !mutableUses.add((UsesNode) effectiveStatement)) {
+        for (var effectiveStatement : effectiveSubstatements()) {
+            if (effectiveStatement instanceof UsesNode usesNode && !mutableUses.add(usesNode)) {
                 throw EffectiveStmtUtils.createNameCollisionSourceException(stmt, effectiveStatement);
             }
-            if (effectiveStatement instanceof TypedefEffectiveStatement) {
-                final TypeDefinition<?> type = ((TypedefEffectiveStatement) effectiveStatement).getTypeDefinition();
-                if (!mutableTypeDefinitions.add(type)) {
-                    throw EffectiveStmtUtils.createNameCollisionSourceException(stmt, effectiveStatement);
-                }
+            if (effectiveStatement instanceof TypedefEffectiveStatement typedef
+                    && !mutableTypeDefinitions.add(typedef.getTypeDefinition())) {
+                throw EffectiveStmtUtils.createNameCollisionSourceException(stmt, effectiveStatement);
             }
-            if (effectiveStatement instanceof GroupingDefinition
-                    && !mutableGroupings.add((GroupingDefinition) effectiveStatement)) {
+            if (effectiveStatement instanceof GroupingDefinition grouping && !mutableGroupings.add(grouping)) {
                 throw EffectiveStmtUtils.createNameCollisionSourceException(stmt, effectiveStatement);
             }
         }
 
-        this.groupings = ImmutableSet.copyOf(mutableGroupings);
-        this.typeDefinitions = ImmutableSet.copyOf(mutableTypeDefinitions);
-        this.uses = ImmutableSet.copyOf(mutableUses);
+        groupings = ImmutableSet.copyOf(mutableGroupings);
+        typeDefinitions = ImmutableSet.copyOf(mutableTypeDefinitions);
+        uses = ImmutableSet.copyOf(mutableUses);
     }
 
     @Override
@@ -195,11 +192,6 @@ public abstract class AbstractEffectiveModule<D extends DeclaredStatement<Unqual
         return uses;
     }
 
-    @Override
-    public Optional<SemVer> getSemanticVersion() {
-        return findFirstEffectiveSubstatementArgument(OpenConfigVersionEffectiveStatement.class);
-    }
-
     protected static final @NonNull String findPrefix(final CommonStmtCtx stmt,
             final Collection<? extends EffectiveStatement<?, ?>> substatements, final String type, final String name) {
         return substatements.stream()
@@ -213,11 +205,11 @@ public abstract class AbstractEffectiveModule<D extends DeclaredStatement<Unqual
     protected final void appendPrefixes(final Current<?, ?> stmt,
             final Builder<String, ModuleEffectiveStatement> builder) {
         streamEffectiveSubstatements(ImportEffectiveStatement.class)
-            .map(imp -> imp.findFirstEffectiveSubstatementArgument(PrefixEffectiveStatement.class).get())
+            .map(imp -> imp.prefix().argument())
             .forEach(pfx -> {
-                final StmtContext<?, ?, ?> importedCtx =
-                        verifyNotNull(stmt.getFromNamespace(ImportPrefixToModuleCtx.class, pfx),
-                            "Failed to resolve prefix %s", pfx);
+                final var importedCtx =
+                    verifyNotNull(stmt.namespaceItem(ParserNamespaces.IMPORT_PREFIX_TO_MODULECTX, pfx),
+                        "Failed to resolve prefix %s", pfx);
                 builder.put(pfx, (ModuleEffectiveStatement) importedCtx.buildEffective());
             });
     }