Migrate QName-based declared statements
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / leaf / LeafStatementSupport.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.leaf;
9
10 import com.google.common.collect.ImmutableList;
11 import org.opendaylight.yangtools.yang.common.QName;
12 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
13 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
14 import org.opendaylight.yangtools.yang.model.api.Status;
15 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
16 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
17 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
18 import org.opendaylight.yangtools.yang.model.api.stmt.DefaultEffectiveStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.LeafEffectiveStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.LeafStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.MandatoryEffectiveStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.StatusEffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
24 import org.opendaylight.yangtools.yang.parser.rfc7950.namespace.ChildSchemaNodeNamespace;
25 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseQNameStatementSupport;
26 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.EffectiveStatementMixins.EffectiveStatementWithFlags.FlagsBuilder;
27 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.EffectiveStmtUtils;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
32 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
33
34 public final class LeafStatementSupport extends BaseQNameStatementSupport<LeafStatement, LeafEffectiveStatement> {
35     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(YangStmtMapping
36         .LEAF)
37         .addOptional(YangStmtMapping.CONFIG)
38         .addOptional(YangStmtMapping.DEFAULT)
39         .addOptional(YangStmtMapping.DESCRIPTION)
40         .addAny(YangStmtMapping.IF_FEATURE)
41         .addOptional(YangStmtMapping.MANDATORY)
42         .addAny(YangStmtMapping.MUST)
43         .addOptional(YangStmtMapping.REFERENCE)
44         .addOptional(YangStmtMapping.STATUS)
45         .addMandatory(YangStmtMapping.TYPE)
46         .addOptional(YangStmtMapping.UNITS)
47         .addOptional(YangStmtMapping.WHEN)
48         .build();
49     private static final LeafStatementSupport INSTANCE = new LeafStatementSupport();
50
51     private LeafStatementSupport() {
52         super(YangStmtMapping.LEAF);
53     }
54
55     public static LeafStatementSupport getInstance() {
56         return INSTANCE;
57     }
58
59     @Override
60     public QName parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
61         return StmtContextUtils.parseIdentifier(ctx,value);
62     }
63
64     @Override
65     public void onStatementAdded(final Mutable<QName, LeafStatement, LeafEffectiveStatement> stmt) {
66         stmt.coerceParentContext().addToNs(ChildSchemaNodeNamespace.class, stmt.coerceStatementArgument(), stmt);
67     }
68
69     @Override
70     public void onFullDefinitionDeclared(final Mutable<QName, LeafStatement, LeafEffectiveStatement> ctx) {
71         super.onFullDefinitionDeclared(ctx);
72         StmtContextUtils.validateIfFeatureAndWhenOnListKeys(ctx);
73     }
74
75     @Override
76     protected SubstatementValidator getSubstatementValidator() {
77         return SUBSTATEMENT_VALIDATOR;
78     }
79
80     @Override
81     protected LeafStatement createDeclared(final StmtContext<QName, LeafStatement, ?> ctx,
82             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
83         return new RegularLeafStatement(ctx.coerceStatementArgument(), substatements);
84     }
85
86     @Override
87     protected LeafStatement createEmptyDeclared(final StmtContext<QName, LeafStatement, ?> ctx) {
88         return new EmptyLeafStatement(ctx.coerceStatementArgument());
89     }
90
91     @Override
92     protected LeafEffectiveStatement createEffective(
93             final StmtContext<QName, LeafStatement, LeafEffectiveStatement> ctx, final LeafStatement declared,
94             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
95         final TypeEffectiveStatement<?> typeStmt = SourceException.throwIfNull(
96             findFirstStatement(substatements, TypeEffectiveStatement.class), ctx.getStatementSourceReference(),
97                 "Leaf is missing a 'type' statement");
98         final String dflt = findFirstArgument(substatements, DefaultEffectiveStatement.class, null);
99         SourceException.throwIf(
100             EffectiveStmtUtils.hasDefaultValueMarkedWithIfFeature(ctx.getRootVersion(), typeStmt, dflt),
101             ctx.getStatementSourceReference(),
102             "Leaf '%s' has default value '%s' marked with an if-feature statement.", ctx.getStatementArgument(), dflt);
103
104         final SchemaPath path = ctx.getSchemaPath().get();
105         final LeafSchemaNode original = (LeafSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective)
106                 .orElse(null);
107         final int flags = new FlagsBuilder()
108                 .setHistory(ctx.getCopyHistory())
109                 .setStatus(findFirstArgument(substatements, StatusEffectiveStatement.class, Status.CURRENT))
110                 .setConfiguration(ctx.isConfiguration())
111                 .setMandatory(findFirstArgument(substatements, MandatoryEffectiveStatement.class, Boolean.FALSE))
112                 .toFlags();
113
114         return original == null ? new EmptyLeafEffectiveStatement(declared, path, flags, substatements)
115                 : new RegularLeafEffectiveStatement(declared, path, flags, substatements, original);
116     }
117
118     @Override
119     protected LeafEffectiveStatement createEmptyEffective(
120             final StmtContext<QName, LeafStatement, LeafEffectiveStatement> ctx, final LeafStatement declared) {
121         throw new UnsupportedOperationException("Leaf statements must have at least one substatement");
122     }
123 }