Migrate Decimal64SpecificationSupport
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / type / Decimal64SpecificationSupport.java
index 59163ada70d250c2077421ad32e7ac17b1d400b5..fca77cc55fb319c46f8246cc95a8f8aabf17b789 100644 (file)
@@ -7,14 +7,21 @@
  */
 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.type;
 
+import com.google.common.collect.ImmutableList;
 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
+import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.FractionDigitsEffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.RangeEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.Decimal64Specification;
-import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
+import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
+import org.opendaylight.yangtools.yang.model.util.type.DecimalTypeBuilder;
+import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseStatementSupport;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
+import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
 
-final class Decimal64SpecificationSupport extends AbstractStatementSupport<String, Decimal64Specification,
+final class Decimal64SpecificationSupport extends BaseStatementSupport<String, Decimal64Specification,
         EffectiveStatement<String, Decimal64Specification>> {
     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
         YangStmtMapping.TYPE)
@@ -32,18 +39,55 @@ final class Decimal64SpecificationSupport extends AbstractStatementSupport<Strin
     }
 
     @Override
-    public Decimal64Specification createDeclared(final StmtContext<String, Decimal64Specification, ?> ctx) {
-        return new Decimal64SpecificationImpl(ctx);
+    protected SubstatementValidator getSubstatementValidator() {
+        return SUBSTATEMENT_VALIDATOR;
     }
 
     @Override
-    public EffectiveStatement<String, Decimal64Specification> createEffective(final StmtContext<String,
-            Decimal64Specification, EffectiveStatement<String, Decimal64Specification>> ctx) {
-        return new Decimal64SpecificationEffectiveStatement(ctx);
+    protected Decimal64Specification createDeclared(final StmtContext<String, Decimal64Specification, ?> ctx,
+            final ImmutableList<? extends DeclaredStatement<?>> substatements) {
+        return new Decimal64SpecificationImpl(ctx, substatements);
     }
 
     @Override
-    protected SubstatementValidator getSubstatementValidator() {
-        return SUBSTATEMENT_VALIDATOR;
+    protected Decimal64Specification createEmptyDeclared(final StmtContext<String, Decimal64Specification, ?> ctx) {
+        throw noFracDigits(ctx);
+    }
+
+    @Override
+    protected EffectiveStatement<String, Decimal64Specification> createEffective(
+            final StmtContext<String, Decimal64Specification, EffectiveStatement<String, Decimal64Specification>> ctx,
+            final Decimal64Specification declared,
+            final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
+        final DecimalTypeBuilder builder = BaseTypes.decimalTypeBuilder(ctx.getSchemaPath().get());
+        for (final EffectiveStatement<?, ?> stmt : substatements) {
+            if (stmt instanceof FractionDigitsEffectiveStatement) {
+                builder.setFractionDigits(((FractionDigitsEffectiveStatement) stmt).argument());
+            }
+            if (stmt instanceof RangeEffectiveStatement) {
+                final RangeEffectiveStatement range = (RangeEffectiveStatement) stmt;
+                builder.setRangeConstraint(range, range.argument());
+            }
+        }
+
+        return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
+    }
+
+    @Override
+    protected EffectiveStatement<String, Decimal64Specification> createEmptyEffective(
+            final StmtContext<String, Decimal64Specification, EffectiveStatement<String, Decimal64Specification>> ctx,
+            final Decimal64Specification declared) {
+        throw noFracDigits(ctx);
+    }
+
+    private static SourceException noFracDigits(final StmtContext<?, ?, ?> ctx) {
+        /*
+         *  https://tools.ietf.org/html/rfc7950#section-9.3.4
+         *
+         *     The "fraction-digits" statement, which is a substatement to the
+         *     "type" statement, MUST be present if the type is "decimal64".
+         */
+        return new SourceException("At least one fraction-digits statement has to be present",
+            ctx.getStatementSourceReference());
     }
 }
\ No newline at end of file