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.leaf;
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.EffectiveStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.DefaultEffectiveStatement;
18 import org.opendaylight.yangtools.yang.model.api.stmt.LeafEffectiveStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.LeafStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.MandatoryEffectiveStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.StatusEffectiveStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
23 import org.opendaylight.yangtools.yang.parser.rfc7950.namespace.ChildSchemaNodeNamespace;
24 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseQNameStatementSupport;
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;
33 public final class LeafStatementSupport extends BaseQNameStatementSupport<LeafStatement, LeafEffectiveStatement> {
34 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(YangStmtMapping
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)
48 private static final LeafStatementSupport INSTANCE = new LeafStatementSupport();
50 private LeafStatementSupport() {
51 super(YangStmtMapping.LEAF);
54 public static LeafStatementSupport getInstance() {
59 public QName parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
60 return StmtContextUtils.parseIdentifier(ctx,value);
64 public void onStatementAdded(final Mutable<QName, LeafStatement, LeafEffectiveStatement> stmt) {
65 stmt.coerceParentContext().addToNs(ChildSchemaNodeNamespace.class, stmt.coerceStatementArgument(), stmt);
69 public void onFullDefinitionDeclared(final Mutable<QName, LeafStatement, LeafEffectiveStatement> ctx) {
70 super.onFullDefinitionDeclared(ctx);
71 StmtContextUtils.validateIfFeatureAndWhenOnListKeys(ctx);
75 public LeafStatement createDeclared(final StmtContext<QName, LeafStatement, ?> ctx) {
76 return new LeafStatementImpl(ctx);
80 protected SubstatementValidator getSubstatementValidator() {
81 return SUBSTATEMENT_VALIDATOR;
85 protected LeafEffectiveStatement createEffective(
86 final StmtContext<QName, LeafStatement, LeafEffectiveStatement> ctx, final LeafStatement declared,
87 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
88 final TypeEffectiveStatement<?> typeStmt = SourceException.throwIfNull(
89 findFirstStatement(substatements, TypeEffectiveStatement.class), ctx.getStatementSourceReference(),
90 "Leaf is missing a 'type' statement");
91 final String dflt = findFirstArgument(substatements, DefaultEffectiveStatement.class, null);
92 SourceException.throwIf(
93 EffectiveStmtUtils.hasDefaultValueMarkedWithIfFeature(ctx.getRootVersion(), typeStmt, dflt),
94 ctx.getStatementSourceReference(),
95 "Leaf '%s' has default value '%s' marked with an if-feature statement.", ctx.getStatementArgument(), dflt);
97 final SchemaPath path = ctx.getSchemaPath().get();
98 final LeafSchemaNode original = (LeafSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective)
100 final int flags = new FlagsBuilder()
101 .setHistory(ctx.getCopyHistory())
102 .setStatus(findFirstArgument(substatements, StatusEffectiveStatement.class, Status.CURRENT))
103 .setConfiguration(ctx.isConfiguration())
104 .setMandatory(findFirstArgument(substatements, MandatoryEffectiveStatement.class, Boolean.FALSE))
107 return original == null ? new EmptyLeafEffectiveStatement(declared, path, flags, substatements)
108 : new RegularLeafEffectiveStatement(declared, path, flags, substatements, original);
112 protected LeafEffectiveStatement createEmptyEffective(
113 final StmtContext<QName, LeafStatement, LeafEffectiveStatement> ctx, final LeafStatement declared) {
114 throw new UnsupportedOperationException("Leaf statements must have at least one substatement");