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.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.EffectiveStmtCtx.Current;
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;
34 public final class LeafStatementSupport extends BaseSchemaTreeStatementSupport<LeafStatement, LeafEffectiveStatement> {
35 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(YangStmtMapping
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)
49 private static final LeafStatementSupport INSTANCE = new LeafStatementSupport();
51 private LeafStatementSupport() {
52 super(YangStmtMapping.LEAF, StatementPolicy.legacyDeclaredCopy());
55 public static LeafStatementSupport getInstance() {
60 public void onFullDefinitionDeclared(final Mutable<QName, LeafStatement, LeafEffectiveStatement> ctx) {
61 super.onFullDefinitionDeclared(ctx);
62 StmtContextUtils.validateIfFeatureAndWhenOnListKeys(ctx);
66 protected SubstatementValidator getSubstatementValidator() {
67 return SUBSTATEMENT_VALIDATOR;
71 protected LeafStatement createDeclared(final StmtContext<QName, LeafStatement, ?> ctx,
72 final ImmutableList<? extends DeclaredStatement<?>> substatements) {
73 return new RegularLeafStatement(ctx.getArgument(), substatements);
77 protected LeafStatement createEmptyDeclared(final StmtContext<QName, LeafStatement, ?> ctx) {
78 return new EmptyLeafStatement(ctx.getArgument());
82 protected LeafEffectiveStatement createEffective(final Current<QName, LeafStatement> stmt,
83 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
84 final TypeEffectiveStatement<?> typeStmt = SourceException.throwIfNull(
85 findFirstStatement(substatements, TypeEffectiveStatement.class), stmt,
86 "Leaf is missing a 'type' statement");
87 final String dflt = findFirstArgument(substatements, DefaultEffectiveStatement.class, null);
88 SourceException.throwIf(
89 EffectiveStmtUtils.hasDefaultValueMarkedWithIfFeature(stmt.yangVersion(), typeStmt, dflt), stmt,
90 "Leaf '%s' has default value '%s' marked with an if-feature statement.", stmt.argument(), dflt);
92 final LeafSchemaNode original = (LeafSchemaNode) stmt.original();
93 final int flags = new FlagsBuilder()
94 .setHistory(stmt.history())
95 .setStatus(findFirstArgument(substatements, StatusEffectiveStatement.class, Status.CURRENT))
96 .setConfiguration(stmt.effectiveConfig().asNullable())
97 .setMandatory(findFirstArgument(substatements, MandatoryEffectiveStatement.class, Boolean.FALSE))
100 final LeafStatement declared = stmt.declared();
101 final SchemaPath path = stmt.wrapSchemaPath();
102 return original == null ? new EmptyLeafEffectiveStatement(declared, path, flags, substatements)
103 : new RegularLeafEffectiveStatement(declared, path, flags, substatements, original);