Move implementation of OnStatementAdded to single class
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / choice / AbstractChoiceStatementSupport.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.choice;
9
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;
35
36 abstract class AbstractChoiceStatementSupport
37         extends BaseSchemaTreeStatementSupport<ChoiceStatement, ChoiceEffectiveStatement>
38         implements ImplicitParentAwareStatementSupport {
39     AbstractChoiceStatementSupport() {
40         super(YangStmtMapping.CHOICE);
41     }
42
43     @Override
44     public final QName parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
45         return StmtContextUtils.parseIdentifier(ctx, value);
46     }
47
48     @Override
49     public final Optional<StatementSupport<?, ?, ?>> getImplicitParentFor(final StatementDefinition stmtDef) {
50         return YangValidationBundles.SUPPORTED_CASE_SHORTHANDS.contains(stmtDef) ? Optional.of(implictCase())
51                 : Optional.empty();
52     }
53
54     @Override
55     protected final ChoiceStatement createDeclared(@NonNull final StmtContext<QName, ChoiceStatement, ?> ctx,
56             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
57         return new RegularChoiceStatement(ctx.coerceStatementArgument(), substatements);
58     }
59
60     @Override
61     protected final ChoiceStatement createEmptyDeclared(@NonNull final StmtContext<QName, ChoiceStatement, ?> ctx) {
62         return new EmptyChoiceStatement(ctx.coerceStatementArgument());
63     }
64
65     @Override
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) {
72             final QName qname;
73             try {
74                 qname = QName.create(ctx.coerceStatementArgument(), defaultArg);
75             } catch (IllegalArgumentException e) {
76                 throw new SourceException(ctx.getStatementSourceReference(), "Default statement has invalid name '%s'",
77                     defaultArg, e);
78             }
79
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);
83         } else {
84             defaultCase = null;
85         }
86
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))
92                 .toFlags();
93
94         return new ChoiceEffectiveStatementImpl(declared, ctx, substatements, flags, defaultCase,
95             (ChoiceSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective).orElse(null));
96     }
97
98     @Override
99     protected final ChoiceEffectiveStatement createEmptyEffective(
100             final StmtContext<QName, ChoiceStatement, ChoiceEffectiveStatement> ctx, final ChoiceStatement declared) {
101         return createEffective(ctx, declared, ImmutableList.of());
102     }
103
104     abstract StatementSupport<?, ?, ?> implictCase();
105
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;
113                 }
114             }
115         }
116
117         return null;
118     }
119 }