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.type;
10 import com.google.common.collect.ImmutableList;
11 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
12 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
13 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
14 import org.opendaylight.yangtools.yang.model.api.stmt.FractionDigitsEffectiveStatement;
15 import org.opendaylight.yangtools.yang.model.api.stmt.RangeEffectiveStatement;
16 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.Decimal64Specification;
17 import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
18 import org.opendaylight.yangtools.yang.model.util.type.DecimalTypeBuilder;
19 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseStatementSupport;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
22 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
24 final class Decimal64SpecificationSupport extends BaseStatementSupport<String, Decimal64Specification,
25 EffectiveStatement<String, Decimal64Specification>> {
26 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
28 .addMandatory(YangStmtMapping.FRACTION_DIGITS)
29 .addOptional(YangStmtMapping.RANGE)
32 Decimal64SpecificationSupport() {
33 super(YangStmtMapping.TYPE);
37 public String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
42 protected SubstatementValidator getSubstatementValidator() {
43 return SUBSTATEMENT_VALIDATOR;
47 protected Decimal64Specification createDeclared(final StmtContext<String, Decimal64Specification, ?> ctx,
48 final ImmutableList<? extends DeclaredStatement<?>> substatements) {
49 return new Decimal64SpecificationImpl(ctx.coerceRawStatementArgument(), substatements);
53 protected Decimal64Specification createEmptyDeclared(final StmtContext<String, Decimal64Specification, ?> ctx) {
54 throw noFracDigits(ctx);
58 protected EffectiveStatement<String, Decimal64Specification> createEffective(
59 final StmtContext<String, Decimal64Specification, EffectiveStatement<String, Decimal64Specification>> ctx,
60 final Decimal64Specification declared,
61 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
62 final DecimalTypeBuilder builder = BaseTypes.decimalTypeBuilder(ctx.getSchemaPath().get());
63 for (final EffectiveStatement<?, ?> stmt : substatements) {
64 if (stmt instanceof FractionDigitsEffectiveStatement) {
65 builder.setFractionDigits(((FractionDigitsEffectiveStatement) stmt).argument());
67 if (stmt instanceof RangeEffectiveStatement) {
68 final RangeEffectiveStatement range = (RangeEffectiveStatement) stmt;
69 builder.setRangeConstraint(range, range.argument());
73 return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
77 protected EffectiveStatement<String, Decimal64Specification> createEmptyEffective(
78 final StmtContext<String, Decimal64Specification, EffectiveStatement<String, Decimal64Specification>> ctx,
79 final Decimal64Specification declared) {
80 throw noFracDigits(ctx);
83 private static SourceException noFracDigits(final StmtContext<?, ?, ?> ctx) {
85 * https://tools.ietf.org/html/rfc7950#section-9.3.4
87 * The "fraction-digits" statement, which is a substatement to the
88 * "type" statement, MUST be present if the type is "decimal64".
90 return new SourceException("At least one fraction-digits statement has to be present",
91 ctx.getStatementSourceReference());