2 * Copyright (c) 2017 Pantheon Technologies, s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.choice;
10 import com.google.common.collect.ImmutableList;
11 import java.util.Optional;
12 import org.eclipse.jdt.annotation.NonNull;
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
15 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
16 import org.opendaylight.yangtools.yang.model.api.Status;
17 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
18 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
19 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
20 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
21 import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceEffectiveStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.DefaultEffectiveStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.MandatoryEffectiveStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.StatusEffectiveStatement;
26 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.YangValidationBundles;
27 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseSchemaTreeStatementSupport;
28 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.EffectiveStatementMixins.EffectiveStatementWithFlags.FlagsBuilder;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.ImplicitParentAwareStatementSupport;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupport;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
33 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
35 abstract class AbstractChoiceStatementSupport
36 extends BaseSchemaTreeStatementSupport<ChoiceStatement, ChoiceEffectiveStatement>
37 implements ImplicitParentAwareStatementSupport {
38 AbstractChoiceStatementSupport() {
39 super(YangStmtMapping.CHOICE);
43 public final Optional<StatementSupport<?, ?, ?>> getImplicitParentFor(final StatementDefinition stmtDef) {
44 return YangValidationBundles.SUPPORTED_CASE_SHORTHANDS.contains(stmtDef) ? Optional.of(implictCase())
49 protected final ChoiceStatement createDeclared(@NonNull final StmtContext<QName, ChoiceStatement, ?> ctx,
50 final ImmutableList<? extends DeclaredStatement<?>> substatements) {
51 return new RegularChoiceStatement(ctx.coerceStatementArgument(), substatements);
55 protected final ChoiceStatement createEmptyDeclared(@NonNull final StmtContext<QName, ChoiceStatement, ?> ctx) {
56 return new EmptyChoiceStatement(ctx.coerceStatementArgument());
60 protected final ChoiceEffectiveStatement createEffective(
61 final StmtContext<QName, ChoiceStatement, ChoiceEffectiveStatement> ctx,
62 final ChoiceStatement declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
63 final String defaultArg = findFirstArgument(substatements, DefaultEffectiveStatement.class, null);
64 final CaseSchemaNode defaultCase;
65 if (defaultArg != null) {
68 qname = QName.create(ctx.coerceStatementArgument(), defaultArg);
69 } catch (IllegalArgumentException e) {
70 throw new SourceException(ctx.getStatementSourceReference(), "Default statement has invalid name '%s'",
74 // FIXME: this does not work with submodules, as they are
75 defaultCase = InferenceException.throwIfNull(findCase(qname, substatements),
76 ctx.getStatementSourceReference(), "Default statement refers to missing case %s", qname);
81 final int flags = new FlagsBuilder()
82 .setHistory(ctx.getCopyHistory())
83 .setStatus(findFirstArgument(substatements, StatusEffectiveStatement.class, Status.CURRENT))
84 .setConfiguration(ctx.isConfiguration())
85 .setMandatory(findFirstArgument(substatements, MandatoryEffectiveStatement.class, Boolean.FALSE))
88 return new ChoiceEffectiveStatementImpl(declared, ctx, substatements, flags, defaultCase,
89 (ChoiceSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective).orElse(null));
93 protected final ChoiceEffectiveStatement createEmptyEffective(
94 final StmtContext<QName, ChoiceStatement, ChoiceEffectiveStatement> ctx, final ChoiceStatement declared) {
95 return createEffective(ctx, declared, ImmutableList.of());
98 abstract StatementSupport<?, ?, ?> implictCase();
100 private static CaseSchemaNode findCase(final QName qname,
101 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
102 for (final EffectiveStatement<?, ?> effectiveStatement : substatements) {
103 if (effectiveStatement instanceof CaseSchemaNode) {
104 final CaseSchemaNode choiceCaseNode = (CaseSchemaNode) effectiveStatement;
105 if (qname.equals(choiceCaseNode.getQName())) {
106 return choiceCaseNode;