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.meta.StmtContextUtils;
34 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
36 abstract class AbstractChoiceStatementSupport
37 extends BaseSchemaTreeStatementSupport<ChoiceStatement, ChoiceEffectiveStatement>
38 implements ImplicitParentAwareStatementSupport {
39 AbstractChoiceStatementSupport() {
40 super(YangStmtMapping.CHOICE);
44 public final QName parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
45 return StmtContextUtils.parseIdentifier(ctx, value);
49 public final Optional<StatementSupport<?, ?, ?>> getImplicitParentFor(final StatementDefinition stmtDef) {
50 return YangValidationBundles.SUPPORTED_CASE_SHORTHANDS.contains(stmtDef) ? Optional.of(implictCase())
55 protected final ChoiceStatement createDeclared(@NonNull final StmtContext<QName, ChoiceStatement, ?> ctx,
56 final ImmutableList<? extends DeclaredStatement<?>> substatements) {
57 return new RegularChoiceStatement(ctx.coerceStatementArgument(), substatements);
61 protected final ChoiceStatement createEmptyDeclared(@NonNull final StmtContext<QName, ChoiceStatement, ?> ctx) {
62 return new EmptyChoiceStatement(ctx.coerceStatementArgument());
66 protected final ChoiceEffectiveStatement createEffective(
67 final StmtContext<QName, ChoiceStatement, ChoiceEffectiveStatement> ctx,
68 final ChoiceStatement declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
69 final String defaultArg = findFirstArgument(substatements, DefaultEffectiveStatement.class, null);
70 final CaseSchemaNode defaultCase;
71 if (defaultArg != null) {
74 qname = QName.create(ctx.coerceStatementArgument(), defaultArg);
75 } catch (IllegalArgumentException e) {
76 throw new SourceException(ctx.getStatementSourceReference(), "Default statement has invalid name '%s'",
80 // FIXME: this does not work with submodules, as they are
81 defaultCase = InferenceException.throwIfNull(findCase(qname, substatements),
82 ctx.getStatementSourceReference(), "Default statement refers to missing case %s", qname);
87 final int flags = new FlagsBuilder()
88 .setHistory(ctx.getCopyHistory())
89 .setStatus(findFirstArgument(substatements, StatusEffectiveStatement.class, Status.CURRENT))
90 .setConfiguration(ctx.isConfiguration())
91 .setMandatory(findFirstArgument(substatements, MandatoryEffectiveStatement.class, Boolean.FALSE))
94 return new ChoiceEffectiveStatementImpl(declared, ctx, substatements, flags, defaultCase,
95 (ChoiceSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective).orElse(null));
99 protected final ChoiceEffectiveStatement createEmptyEffective(
100 final StmtContext<QName, ChoiceStatement, ChoiceEffectiveStatement> ctx, final ChoiceStatement declared) {
101 return createEffective(ctx, declared, ImmutableList.of());
104 abstract StatementSupport<?, ?, ?> implictCase();
106 private static CaseSchemaNode findCase(final QName qname,
107 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
108 for (final EffectiveStatement<?, ?> effectiveStatement : substatements) {
109 if (effectiveStatement instanceof CaseSchemaNode) {
110 final CaseSchemaNode choiceCaseNode = (CaseSchemaNode) effectiveStatement;
111 if (qname.equals(choiceCaseNode.getQName())) {
112 return choiceCaseNode;