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