import com.google.common.collect.ImmutableList;
import java.util.Optional;
-import java.util.SortedMap;
-import java.util.TreeMap;
import org.eclipse.jdt.annotation.NonNull;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
import org.opendaylight.yangtools.yang.model.api.stmt.DefaultEffectiveStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.MandatoryEffectiveStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.StatusEffectiveStatement;
-import org.opendaylight.yangtools.yang.parser.rfc7950.namespace.ChildSchemaNodeNamespace;
import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.YangValidationBundles;
-import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseQNameStatementSupport;
+import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseSchemaTreeStatementSupport;
import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.EffectiveStatementMixins.EffectiveStatementWithFlags.FlagsBuilder;
import org.opendaylight.yangtools.yang.parser.spi.meta.ImplicitParentAwareStatementSupport;
import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupport;
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.meta.StmtContextUtils;
import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
abstract class AbstractChoiceStatementSupport
- extends BaseQNameStatementSupport<ChoiceStatement, ChoiceEffectiveStatement>
+ extends BaseSchemaTreeStatementSupport<ChoiceStatement, ChoiceEffectiveStatement>
implements ImplicitParentAwareStatementSupport {
AbstractChoiceStatementSupport() {
super(YangStmtMapping.CHOICE);
}
- @Override
- public final QName parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
- return StmtContextUtils.parseIdentifier(ctx, value);
- }
-
@Override
public final Optional<StatementSupport<?, ?, ?>> getImplicitParentFor(final StatementDefinition stmtDef) {
return YangValidationBundles.SUPPORTED_CASE_SHORTHANDS.contains(stmtDef) ? Optional.of(implictCase())
: Optional.empty();
}
- @Override
- public final void onStatementAdded(final Mutable<QName, ChoiceStatement, ChoiceEffectiveStatement> stmt) {
- stmt.coerceParentContext().addToNs(ChildSchemaNodeNamespace.class, stmt.coerceStatementArgument(), stmt);
- }
-
@Override
protected final ChoiceStatement createDeclared(@NonNull final StmtContext<QName, ChoiceStatement, ?> ctx,
final ImmutableList<? extends DeclaredStatement<?>> substatements) {
protected final ChoiceEffectiveStatement createEffective(
final StmtContext<QName, ChoiceStatement, ChoiceEffectiveStatement> ctx,
final ChoiceStatement declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
- // FIXME: YANGTOOLS-652: this is rather unnecessary
- final SortedMap<QName, CaseSchemaNode> cases = new TreeMap<>();
- for (final EffectiveStatement<?, ?> effectiveStatement : substatements) {
- if (effectiveStatement instanceof CaseSchemaNode) {
- final CaseSchemaNode choiceCaseNode = (CaseSchemaNode) effectiveStatement;
- // FIXME: we may be overwriting a previous entry, is that really okay?
- cases.put(choiceCaseNode.getQName(), choiceCaseNode);
- }
- }
-
final String defaultArg = findFirstArgument(substatements, DefaultEffectiveStatement.class, null);
final CaseSchemaNode defaultCase;
if (defaultArg != null) {
}
// FIXME: this does not work with submodules, as they are
- defaultCase = InferenceException.throwIfNull(cases.get(qname), ctx.getStatementSourceReference(),
- "Default statement refers to missing case %s", qname);
+ defaultCase = InferenceException.throwIfNull(findCase(qname, substatements),
+ ctx.getStatementSourceReference(), "Default statement refers to missing case %s", qname);
} else {
defaultCase = null;
}
.setMandatory(findFirstArgument(substatements, MandatoryEffectiveStatement.class, Boolean.FALSE))
.toFlags();
- return new ChoiceEffectiveStatementImpl(declared, ctx, substatements, flags, cases, defaultCase,
+ return new ChoiceEffectiveStatementImpl(declared, ctx, substatements, flags, defaultCase,
(ChoiceSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective).orElse(null));
}
}
abstract StatementSupport<?, ?, ?> implictCase();
+
+ private static CaseSchemaNode findCase(final QName qname,
+ final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
+ for (final EffectiveStatement<?, ?> effectiveStatement : substatements) {
+ if (effectiveStatement instanceof CaseSchemaNode) {
+ final CaseSchemaNode choiceCaseNode = (CaseSchemaNode) effectiveStatement;
+ if (qname.equals(choiceCaseNode.getQName())) {
+ return choiceCaseNode;
+ }
+ }
+ }
+
+ return null;
+ }
}
\ No newline at end of file