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.rfc7950.stmt.SubstatementIndexingException;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.ImplicitParentAwareStatementSupport;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupport;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
35 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
37 abstract class AbstractChoiceStatementSupport
38 extends BaseSchemaTreeStatementSupport<ChoiceStatement, ChoiceEffectiveStatement>
39 implements ImplicitParentAwareStatementSupport {
40 AbstractChoiceStatementSupport() {
41 super(YangStmtMapping.CHOICE);
45 public final Optional<StatementSupport<?, ?, ?>> getImplicitParentFor(final StatementDefinition stmtDef) {
46 return YangValidationBundles.SUPPORTED_CASE_SHORTHANDS.contains(stmtDef) ? Optional.of(implictCase())
51 protected final ChoiceStatement createDeclared(@NonNull final StmtContext<QName, ChoiceStatement, ?> ctx,
52 final ImmutableList<? extends DeclaredStatement<?>> substatements) {
53 return new RegularChoiceStatement(ctx.getArgument(), substatements);
57 protected final ChoiceStatement createEmptyDeclared(@NonNull final StmtContext<QName, ChoiceStatement, ?> ctx) {
58 return new EmptyChoiceStatement(ctx.getArgument());
62 protected ChoiceEffectiveStatement createEffective(final Current<QName, ChoiceStatement> stmt,
63 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
64 final String defaultArg = findFirstArgument(substatements, DefaultEffectiveStatement.class, null);
65 final CaseSchemaNode defaultCase;
66 if (defaultArg != null) {
69 qname = QName.create(stmt.getArgument(), defaultArg);
70 } catch (IllegalArgumentException e) {
71 throw new SourceException(stmt.sourceReference(), "Default statement has invalid name '%s'", defaultArg,
75 // FIXME: this does not work with submodules, as they are
76 defaultCase = InferenceException.throwIfNull(findCase(qname, substatements),
77 stmt.sourceReference(), "Default statement refers to missing case %s", qname);
82 final int flags = new FlagsBuilder()
83 .setHistory(stmt.history())
84 .setStatus(findFirstArgument(substatements, StatusEffectiveStatement.class, Status.CURRENT))
85 .setConfiguration(stmt.effectiveConfig())
86 .setMandatory(findFirstArgument(substatements, MandatoryEffectiveStatement.class, Boolean.FALSE))
89 return new ChoiceEffectiveStatementImpl(stmt.declared(), substatements, flags, stmt.getSchemaPath(),
90 defaultCase, (ChoiceSchemaNode) stmt.original());
91 } catch (SubstatementIndexingException e) {
92 throw new SourceException(e.getMessage(), stmt.sourceReference(), e);
96 abstract StatementSupport<?, ?, ?> implictCase();
98 private static CaseSchemaNode findCase(final QName qname,
99 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
100 for (final EffectiveStatement<?, ?> effectiveStatement : substatements) {
101 if (effectiveStatement instanceof CaseSchemaNode) {
102 final CaseSchemaNode choiceCaseNode = (CaseSchemaNode) effectiveStatement;
103 if (qname.equals(choiceCaseNode.getQName())) {
104 return choiceCaseNode;