Migrate Decimal64SpecificationSupport
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / type / Decimal64SpecificationSupport.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.type;
9
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;
23
24 final class Decimal64SpecificationSupport extends BaseStatementSupport<String, Decimal64Specification,
25         EffectiveStatement<String, Decimal64Specification>> {
26     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
27         YangStmtMapping.TYPE)
28         .addMandatory(YangStmtMapping.FRACTION_DIGITS)
29         .addOptional(YangStmtMapping.RANGE)
30         .build();
31
32     Decimal64SpecificationSupport() {
33         super(YangStmtMapping.TYPE);
34     }
35
36     @Override
37     public String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
38         return value;
39     }
40
41     @Override
42     protected SubstatementValidator getSubstatementValidator() {
43         return SUBSTATEMENT_VALIDATOR;
44     }
45
46     @Override
47     protected Decimal64Specification createDeclared(final StmtContext<String, Decimal64Specification, ?> ctx,
48             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
49         return new Decimal64SpecificationImpl(ctx, substatements);
50     }
51
52     @Override
53     protected Decimal64Specification createEmptyDeclared(final StmtContext<String, Decimal64Specification, ?> ctx) {
54         throw noFracDigits(ctx);
55     }
56
57     @Override
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());
66             }
67             if (stmt instanceof RangeEffectiveStatement) {
68                 final RangeEffectiveStatement range = (RangeEffectiveStatement) stmt;
69                 builder.setRangeConstraint(range, range.argument());
70             }
71         }
72
73         return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
74     }
75
76     @Override
77     protected EffectiveStatement<String, Decimal64Specification> createEmptyEffective(
78             final StmtContext<String, Decimal64Specification, EffectiveStatement<String, Decimal64Specification>> ctx,
79             final Decimal64Specification declared) {
80         throw noFracDigits(ctx);
81     }
82
83     private static SourceException noFracDigits(final StmtContext<?, ?, ?> ctx) {
84         /*
85          *  https://tools.ietf.org/html/rfc7950#section-9.3.4
86          *
87          *     The "fraction-digits" statement, which is a substatement to the
88          *     "type" statement, MUST be present if the type is "decimal64".
89          */
90         return new SourceException("At least one fraction-digits statement has to be present",
91             ctx.getStatementSourceReference());
92     }
93 }