import static java.util.Objects.requireNonNull;
import com.google.common.annotations.Beta;
-import com.google.common.base.MoreObjects;
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;
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);
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
return uses;
}
- @Override
- public Optional<SemVer> getSemanticVersion() {
- return findFirstEffectiveSubstatementArgument(OpenConfigVersionEffectiveStatement.class);
- }
-
- @Override
- public String toString() {
- return MoreObjects.toStringHelper(this).omitNullValues()
- .add("name", getName())
- .add("namespace", getNamespace())
- .add("revision", getRevision().orElse(null))
- .add("prefix", prefix)
- .add("yangVersion", getYangVersion())
- .toString();
- }
-
protected static final @NonNull String findPrefix(final CommonStmtCtx stmt,
final Collection<? extends EffectiveStatement<?, ?>> substatements, final String type, final String name) {
return substatements.stream()
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());
});
}