*/
package org.opendaylight.yangtools.yang.parser.rfc7950.stmt;
+import static java.util.Objects.requireNonNull;
+
+import com.google.common.annotations.Beta;
import com.google.common.collect.Collections2;
import com.google.common.collect.ImmutableList;
import java.util.ArrayList;
import java.util.Collection;
-import java.util.List;
+import java.util.HashSet;
import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
import java.util.function.Predicate;
-import javax.annotation.Nonnull;
-import javax.annotation.Nullable;
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.Nullable;
import org.opendaylight.yangtools.yang.model.api.SchemaNode;
-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.meta.IdentifierNamespace;
import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
-import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
public abstract class EffectiveStatementBase<A, D extends DeclaredStatement<A>> implements EffectiveStatement<A, D> {
- private final List<? extends EffectiveStatement<?, ?>> substatements;
+ private final @NonNull ImmutableList<? extends EffectiveStatement<?, ?>> substatements;
/**
* Constructor.
*
- * @param ctx
- * context of statement.
+ * @param ctx context of statement.
*/
protected EffectiveStatementBase(final StmtContext<A, D, ?> ctx) {
- final Collection<? extends StmtContext<?, ?, ?>> effectiveSubstatements = ctx.effectiveSubstatements();
final Collection<StmtContext<?, ?, ?>> substatementsInit = new ArrayList<>();
- final Collection<? extends StmtContext<?, ?, ?>> supportedDeclaredSubStmts = Collections2.filter(
- ctx.declaredSubstatements(), StmtContext::isSupportedByFeatures);
- for (final StmtContext<?, ?, ?> declaredSubstatement : supportedDeclaredSubStmts) {
- if (YangStmtMapping.USES == declaredSubstatement.getPublicDefinition()) {
- substatementsInit.add(declaredSubstatement);
- substatementsInit.addAll(declaredSubstatement.getEffectOfStatement());
- ((StatementContextBase<?, ?, ?>) ctx).removeStatementsFromEffectiveSubstatements(declaredSubstatement
- .getEffectOfStatement());
- } else {
+ /*
+ * This dance is required to ensure that effects of 'uses' nodes are applied in the same order as
+ * the statements were defined -- i.e. if we have something like this:
+ *
+ * container foo {
+ * uses bar;
+ * uses baz;
+ * }
+ *
+ * grouping baz {
+ * leaf baz {
+ * type string;
+ * }
+ * }
+ *
+ * grouping bar {
+ * leaf bar {
+ * type string;
+ * }
+ * }
+ *
+ * The reactor would first inline 'uses baz' as that definition is the first one completely resolved and then
+ * inline 'uses bar'. Here we are iterating in declaration order re-inline the statements.
+ *
+ * TODO: this really should be handled by UsesStatementSupport such that 'uses baz' would have a prerequisite
+ * of a resolved 'uses bar'.
+ */
+ Set<StmtContext<?, ?, ?>> filteredStatements = null;
+ for (final StmtContext<?, ?, ?> declaredSubstatement : ctx.declaredSubstatements()) {
+ if (declaredSubstatement.isSupportedByFeatures()) {
substatementsInit.add(declaredSubstatement);
+
+ final Collection<? extends StmtContext<?, ?, ?>> effect = declaredSubstatement.getEffectOfStatement();
+ if (!effect.isEmpty()) {
+ if (filteredStatements == null) {
+ filteredStatements = new HashSet<>();
+ }
+ filteredStatements.addAll(effect);
+ substatementsInit.addAll(effect);
+ }
+ }
+ }
+
+ if (filteredStatements != null) {
+ for (StmtContext<?, ?, ?> stmt : ctx.effectiveSubstatements()) {
+ if (!filteredStatements.contains(stmt)) {
+ substatementsInit.add(stmt);
+ }
}
+ } else {
+ substatementsInit.addAll(ctx.effectiveSubstatements());
}
- substatementsInit.addAll(effectiveSubstatements);
- this.substatements = ImmutableList.copyOf(initSubstatements(substatementsInit));
+ this.substatements = ImmutableList.copyOf(initSubstatements(ctx, substatementsInit));
+ }
+
+ @Beta
+ protected Collection<? extends EffectiveStatement<?, ?>> initSubstatements(final StmtContext<A, D, ?> ctx,
+ final Collection<? extends StmtContext<?, ?, ?>> substatementsInit) {
+ return initSubstatements(substatementsInit);
}
/**
}
@Override
- public final <K, V, N extends IdentifierNamespace<K, V>> V get(@Nonnull final Class<N> namespace,
- @Nonnull final K identifier) {
- throw new UnsupportedOperationException("Not implemented yet.");
- }
-
- @Override
- public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAll(@Nonnull final Class<N> namespace) {
- throw new UnsupportedOperationException("Not implemented yet.");
+ public final <K, V, N extends IdentifierNamespace<K, V>> V get(final Class<N> namespace, final K identifier) {
+ return findAll(namespace).get(requireNonNull(identifier));
}
- @Nonnull
@Override
- public final Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
- return substatements;
+ public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAll(final Class<N> namespace) {
+ return getNamespaceContents(requireNonNull(namespace)).orElse(null);
}
/**
- * Find first substatement of specified type.
- *
- * @param type Requested type
- * @return First matching substatement, or null if no match is found.
+ * Return the statement-specific contents of specified namespace, if available.
*
- * @deprecated Use {@link #findFirstEffectiveSubstatement(Class)} instead.
+ * @param namespace Requested namespace
+ * @return Namespace contents, if available.
*/
- @Deprecated
- public final <S extends EffectiveStatement<?, ?>> S firstEffective(final Class<S> type) {
- return findFirstEffectiveSubstatement(type).orElse(null);
+ @Beta
+ protected <K, V, N extends IdentifierNamespace<K, V>> Optional<? extends Map<K, V>> getNamespaceContents(
+ final @NonNull Class<N> namespace) {
+ return Optional.empty();
+ }
+
+ @Override
+ public final Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
+ return substatements;
}
protected final <S extends SchemaNode> S firstSchemaNode(final Class<S> type) {
return Collection.class.cast(Collections2.filter(substatements, type::isInstance));
}
- @Nullable protected final <T> T firstSubstatementOfType(final Class<T> type) {
+ protected final <T> @Nullable T firstSubstatementOfType(final Class<T> type) {
return substatements.stream().filter(type::isInstance).findFirst().map(type::cast).orElse(null);
}