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 / DeviateStatementImpl.java
index 5186596ad14d263dfc0cb9bdca7863fe6912e19e..0e0886f729ee2bb3aaa9d92d24607cf8c1926227 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,27 +7,41 @@
  */
 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020;
 
-import org.opendaylight.yangtools.yang.model.api.Deviation;
-import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.DeviateEffectiveStatementImpl;
+import static org.opendaylight.yangtools.yang.parser.spi.SubstatementValidator.MAX;
 
+import javax.annotation.Nonnull;
+import org.opendaylight.yangtools.yang.model.api.Deviation;
 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.DeviateStatement;
+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.spi.source.SourceException;
-
-import javax.annotation.Nonnull;
+import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.DeviateEffectiveStatementImpl;
 
 public class DeviateStatementImpl extends AbstractDeclaredStatement<Deviation.Deviate> implements DeviateStatement {
+    private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(Rfc6020Mapping
+            .DEVIATE)
+            .add(Rfc6020Mapping.CONFIG, 0, 1)
+            .add(Rfc6020Mapping.DEFAULT, 0, 1)
+            .add(Rfc6020Mapping.MANDATORY, 0, 1)
+            .add(Rfc6020Mapping.MAX_ELEMENTS, 0, 1)
+            .add(Rfc6020Mapping.MIN_ELEMENTS, 0, 1)
+            .add(Rfc6020Mapping.MUST, 0, MAX)
+            .add(Rfc6020Mapping.TYPE, 0, 1)
+            .add(Rfc6020Mapping.UNIQUE, 0, MAX)
+            .add(Rfc6020Mapping.UNITS, 0, 1)
+            .build();
 
     protected DeviateStatementImpl(
             StmtContext<Deviation.Deviate, DeviateStatement, ?> context) {
         super(context);
     }
 
-    public static class Definition extends AbstractStatementSupport<Deviation.Deviate,DeviateStatement,EffectiveStatement<Deviation.Deviate,DeviateStatement>> {
+    public static class Definition extends AbstractStatementSupport<Deviation.Deviate, DeviateStatement,
+            EffectiveStatement<Deviation.Deviate, DeviateStatement>> {
 
         public Definition() {
             super(Rfc6020Mapping.DEVIATE);
@@ -35,7 +49,7 @@ public class DeviateStatementImpl extends AbstractDeclaredStatement<Deviation.De
 
         @Override public Deviation.Deviate parseArgumentValue(
                 StmtContext<?, ?, ?> ctx, String value) throws SourceException {
-            return Utils.parseDeviateFromString(value);
+            return Utils.parseDeviateFromString(ctx, value);
         }
 
         @Override public DeviateStatement createDeclared(
@@ -44,10 +58,17 @@ public class DeviateStatementImpl extends AbstractDeclaredStatement<Deviation.De
         }
 
         @Override public EffectiveStatement<Deviation.Deviate, DeviateStatement> createEffective(
-                StmtContext<Deviation.Deviate, DeviateStatement, EffectiveStatement<Deviation.Deviate, DeviateStatement>> ctx) {
+                StmtContext<Deviation.Deviate, DeviateStatement, EffectiveStatement<Deviation.Deviate,
+                        DeviateStatement>> ctx) {
             return new DeviateEffectiveStatementImpl(ctx);
         }
 
+        @Override
+        public void onFullDefinitionDeclared(StmtContext.Mutable<Deviation.Deviate, DeviateStatement,
+                EffectiveStatement<Deviation.Deviate, DeviateStatement>> stmt) throws SourceException {
+            super.onFullDefinitionDeclared(stmt);
+            SUBSTATEMENT_VALIDATOR.validate(stmt);
+        }
     }
 
     @Nonnull @Override