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 java.util.SortedMap;
13 import java.util.TreeMap;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
17 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.Status;
19 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
20 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
21 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
22 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
23 import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceEffectiveStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.DefaultEffectiveStatement;
26 import org.opendaylight.yangtools.yang.model.api.stmt.MandatoryEffectiveStatement;
27 import org.opendaylight.yangtools.yang.model.api.stmt.StatusEffectiveStatement;
28 import org.opendaylight.yangtools.yang.parser.rfc7950.namespace.ChildSchemaNodeNamespace;
29 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.YangValidationBundles;
30 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseQNameStatementSupport;
31 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.EffectiveStatementMixins.EffectiveStatementWithFlags.FlagsBuilder;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.ImplicitParentAwareStatementSupport;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupport;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
38 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
40 abstract class AbstractChoiceStatementSupport
41 extends BaseQNameStatementSupport<ChoiceStatement, ChoiceEffectiveStatement>
42 implements ImplicitParentAwareStatementSupport {
43 AbstractChoiceStatementSupport() {
44 super(YangStmtMapping.CHOICE);
48 public final QName parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
49 return StmtContextUtils.parseIdentifier(ctx, value);
53 public final Optional<StatementSupport<?, ?, ?>> getImplicitParentFor(final StatementDefinition stmtDef) {
54 return YangValidationBundles.SUPPORTED_CASE_SHORTHANDS.contains(stmtDef) ? Optional.of(implictCase())
59 public final void onStatementAdded(final Mutable<QName, ChoiceStatement, ChoiceEffectiveStatement> stmt) {
60 stmt.coerceParentContext().addToNs(ChildSchemaNodeNamespace.class, stmt.coerceStatementArgument(), stmt);
64 protected final ChoiceStatement createDeclared(@NonNull final StmtContext<QName, ChoiceStatement, ?> ctx,
65 final ImmutableList<? extends DeclaredStatement<?>> substatements) {
66 return new RegularChoiceStatement(ctx.coerceStatementArgument(), substatements);
70 protected final ChoiceStatement createEmptyDeclared(@NonNull final StmtContext<QName, ChoiceStatement, ?> ctx) {
71 return new EmptyChoiceStatement(ctx.coerceStatementArgument());
75 protected final ChoiceEffectiveStatement createEffective(
76 final StmtContext<QName, ChoiceStatement, ChoiceEffectiveStatement> ctx,
77 final ChoiceStatement declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
78 // FIXME: YANGTOOLS-652: this is rather unnecessary
79 final SortedMap<QName, CaseSchemaNode> cases = new TreeMap<>();
80 for (final EffectiveStatement<?, ?> effectiveStatement : substatements) {
81 if (effectiveStatement instanceof CaseSchemaNode) {
82 final CaseSchemaNode choiceCaseNode = (CaseSchemaNode) effectiveStatement;
83 // FIXME: we may be overwriting a previous entry, is that really okay?
84 cases.put(choiceCaseNode.getQName(), choiceCaseNode);
88 final String defaultArg = findFirstArgument(substatements, DefaultEffectiveStatement.class, null);
89 final CaseSchemaNode defaultCase;
90 if (defaultArg != null) {
93 qname = QName.create(ctx.coerceStatementArgument(), defaultArg);
94 } catch (IllegalArgumentException e) {
95 throw new SourceException(ctx.getStatementSourceReference(), "Default statement has invalid name '%s'",
99 // FIXME: this does not work with submodules, as they are
100 defaultCase = InferenceException.throwIfNull(cases.get(qname), ctx.getStatementSourceReference(),
101 "Default statement refers to missing case %s", qname);
106 final int flags = new FlagsBuilder()
107 .setHistory(ctx.getCopyHistory())
108 .setStatus(findFirstArgument(substatements, StatusEffectiveStatement.class, Status.CURRENT))
109 .setConfiguration(ctx.isConfiguration())
110 .setMandatory(findFirstArgument(substatements, MandatoryEffectiveStatement.class, Boolean.FALSE))
113 return new ChoiceEffectiveStatementImpl(declared, ctx, substatements, flags, cases, defaultCase,
114 (ChoiceSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective).orElse(null));
118 protected final ChoiceEffectiveStatement createEmptyEffective(
119 final StmtContext<QName, ChoiceStatement, ChoiceEffectiveStatement> ctx, final ChoiceStatement declared) {
120 return createEffective(ctx, declared, ImmutableList.of());
123 abstract StatementSupport<?, ?, ?> implictCase();