Bug 4646: Parser accepts invalid models
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / FractionDigitsStatementImpl.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. 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.stmt.rfc6020;
9
10 import com.google.common.collect.Range;
11 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
12 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
13 import org.opendaylight.yangtools.yang.model.api.stmt.FractionDigitsStatement;
14 import org.opendaylight.yangtools.yang.parser.spi.SubstatementValidator;
15 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
16 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
17 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
18 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
19 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.FractionDigitsEffectiveStatementImpl;
20
21 public class FractionDigitsStatementImpl extends AbstractDeclaredStatement<Integer> implements FractionDigitsStatement {
22     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(Rfc6020Mapping
23             .FRACTION_DIGITS)
24             .build();
25
26     private static final Range<Integer> FRAC_DIGITS_ALLOWED = Range.closed(1, 18);
27
28     protected FractionDigitsStatementImpl(StmtContext<Integer, FractionDigitsStatement, ?> context) {
29         super(context);
30     }
31
32     public static class Definition
33             extends
34             AbstractStatementSupport<Integer, FractionDigitsStatement, EffectiveStatement<Integer, FractionDigitsStatement>> {
35
36         public Definition() {
37             super(Rfc6020Mapping.FRACTION_DIGITS);
38         }
39
40         @Override
41         public Integer parseArgumentValue(StmtContext<?, ?, ?> ctx, String value) {
42
43             int fractionDigits;
44
45             try {
46                 fractionDigits = Integer.parseInt(value);
47             } catch (NumberFormatException e) {
48                 throw new IllegalArgumentException(String.format("%s is not valid fraction-digits integer argument",
49                         value), e);
50             }
51
52             if (!FRAC_DIGITS_ALLOWED.contains(fractionDigits)) {
53                 throw new IllegalArgumentException(String.format("fraction-digits argument should be integer within %s",
54                         FRAC_DIGITS_ALLOWED));
55             }
56
57             return fractionDigits;
58         }
59
60         @Override
61         public FractionDigitsStatement createDeclared(StmtContext<Integer, FractionDigitsStatement, ?> ctx) {
62             return new FractionDigitsStatementImpl(ctx);
63         }
64
65         @Override
66         public EffectiveStatement<Integer, FractionDigitsStatement> createEffective(
67                 StmtContext<Integer, FractionDigitsStatement, EffectiveStatement<Integer, FractionDigitsStatement>> ctx) {
68             return new FractionDigitsEffectiveStatementImpl(ctx);
69         }
70
71         @Override
72         public void onFullDefinitionDeclared(StmtContext.Mutable<Integer, FractionDigitsStatement,
73                 EffectiveStatement<Integer, FractionDigitsStatement>> stmt) throws SourceException {
74             super.onFullDefinitionDeclared(stmt);
75             SUBSTATEMENT_VALIDATOR.validate(stmt);
76         }
77     }
78
79     @Override
80     public Integer getValue() {
81         return argument();
82     }
83 }