Bug 4540: Yang parser exceptions should follow consistent path
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / PositionStatementImpl.java
index 0d0de1aae4e03967d77ba3c747dbff0ebc62af79..474fbb04b4afdbcc6c7ed8bcea317b5dc7556ba4 100644 (file)
@@ -1,4 +1,4 @@
-/**
+/*
  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
@@ -7,51 +7,62 @@
  */
 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020;
 
+import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.PositionStatement;
+import org.opendaylight.yangtools.yang.parser.spi.SubstatementValidator;
 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
+import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.PositionEffectiveStatementImpl;
 
-public class PositionStatementImpl extends AbstractDeclaredStatement<String>
-        implements PositionStatement {
+public class PositionStatementImpl extends AbstractDeclaredStatement<Long> implements PositionStatement {
+    private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
+        Rfc6020Mapping.POSITION).build();
 
-    protected PositionStatementImpl(
-            StmtContext<String, PositionStatement, ?> context) {
+    protected PositionStatementImpl(final StmtContext<Long, PositionStatement, ?> context) {
         super(context);
     }
 
-    public static class Definition
-            extends
-            AbstractStatementSupport<String, PositionStatement, EffectiveStatement<String, PositionStatement>> {
+    public static class Definition extends AbstractStatementSupport<Long, PositionStatement,
+            EffectiveStatement<Long, PositionStatement>> {
 
         public Definition() {
             super(Rfc6020Mapping.POSITION);
         }
 
         @Override
-        public String parseArgumentValue(StmtContext<?, ?, ?> ctx, String value) {
-            return value;
+        public Long parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
+            try {
+                return Long.parseLong(value);
+            } catch (NumberFormatException e) {
+                throw new SourceException(String.format("Bit position value %s is not valid integer", value),
+                        ctx.getStatementSourceReference(), e);
+            }
         }
 
         @Override
-        public PositionStatement createDeclared(
-                StmtContext<String, PositionStatement, ?> ctx) {
+        public PositionStatement createDeclared(final StmtContext<Long, PositionStatement, ?> ctx) {
             return new PositionStatementImpl(ctx);
         }
 
         @Override
-        public EffectiveStatement<String, PositionStatement> createEffective(
-                StmtContext<String, PositionStatement, EffectiveStatement<String, PositionStatement>> ctx) {
-            throw new UnsupportedOperationException();
+        public EffectiveStatement<Long, PositionStatement> createEffective(
+                final StmtContext<Long, PositionStatement, EffectiveStatement<Long, PositionStatement>> ctx) {
+            return new PositionEffectiveStatementImpl(ctx);
         }
 
+        @Override
+        public void onFullDefinitionDeclared(
+                final StmtContext.Mutable<Long, PositionStatement, EffectiveStatement<Long, PositionStatement>> stmt) {
+            super.onFullDefinitionDeclared(stmt);
+            SUBSTATEMENT_VALIDATOR.validate(stmt);
+        }
     }
 
     @Override
-    public String getValue() {
+    public long getValue() {
         return argument();
     }
-
 }