Bug 4646: Parser accepts invalid models
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / PositionStatementImpl.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 org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
11 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
12 import org.opendaylight.yangtools.yang.model.api.stmt.PositionStatement;
13 import org.opendaylight.yangtools.yang.parser.spi.SubstatementValidator;
14 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
15 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
16 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
17 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
18 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.PositionEffectiveStatementImpl;
19
20 public class PositionStatementImpl extends AbstractDeclaredStatement<Long>
21         implements PositionStatement {
22     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(Rfc6020Mapping
23             .POSITION)
24             .build();
25
26     protected PositionStatementImpl(
27             StmtContext<Long, PositionStatement, ?> context) {
28         super(context);
29     }
30
31     public static class Definition
32             extends
33             AbstractStatementSupport<Long, PositionStatement, EffectiveStatement<Long, PositionStatement>> {
34
35         public Definition() {
36             super(Rfc6020Mapping.POSITION);
37         }
38
39         @Override
40         public Long parseArgumentValue(StmtContext<?, ?, ?> ctx, String value) {
41             try {
42                 return Long.parseLong(value);
43             } catch (NumberFormatException e) {
44                 throw new IllegalArgumentException(String.format("Position value %s is not valid integer", value), e);
45             }
46         }
47
48         @Override
49         public PositionStatement createDeclared(
50                 StmtContext<Long, PositionStatement, ?> ctx) {
51             return new PositionStatementImpl(ctx);
52         }
53
54         @Override
55         public EffectiveStatement<Long, PositionStatement> createEffective(
56                 StmtContext<Long, PositionStatement, EffectiveStatement<Long, PositionStatement>> ctx) {
57             return new PositionEffectiveStatementImpl(ctx);
58         }
59
60         @Override
61         public void onFullDefinitionDeclared(StmtContext.Mutable<Long, PositionStatement,
62                 EffectiveStatement<Long, PositionStatement>> stmt) throws SourceException {
63             super.onFullDefinitionDeclared(stmt);
64             SUBSTATEMENT_VALIDATOR.validate(stmt);
65         }
66     }
67
68     @Override
69     public Long getValue() {
70         return argument();
71     }
72
73 }