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 / ModuleStatementSupport.java
index bd7175879b81ca25cd6e12677ce4ec52ace86730..ea6d565b895288b891582df87ec952ac5d7e319f 100644 (file)
@@ -7,10 +7,9 @@
  */
 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020;
 
+import static org.opendaylight.yangtools.yang.parser.spi.SubstatementValidator.MAX;
 import static org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils.firstAttributeOf;
 
-import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName;
-
 import com.google.common.base.Optional;
 import java.net.URI;
 import java.util.Date;
@@ -25,10 +24,12 @@ import org.opendaylight.yangtools.yang.model.api.stmt.PrefixStatement;
 import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleIdentifierImpl;
 import org.opendaylight.yangtools.yang.parser.spi.ModuleNamespace;
 import org.opendaylight.yangtools.yang.parser.spi.NamespaceToModule;
+import org.opendaylight.yangtools.yang.parser.spi.SubstatementValidator;
 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.meta.StmtContext.Mutable;
 import org.opendaylight.yangtools.yang.parser.spi.source.ImpPrefixToModuleIdentifier;
+import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName;
 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleIdentifierToModuleQName;
 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNameToModuleQName;
 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNamespaceForBelongsTo;
@@ -39,8 +40,35 @@ import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.ModuleEffec
 
 public class ModuleStatementSupport extends
         AbstractStatementSupport<String, ModuleStatement, EffectiveStatement<String, ModuleStatement>> {
-
-    private QNameModule qNameModule;
+    private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(Rfc6020Mapping
+            .MODULE)
+            .add(Rfc6020Mapping.ANYXML, 0, MAX)
+            .add(Rfc6020Mapping.AUGMENT, 0, MAX)
+            .add(Rfc6020Mapping.CHOICE, 0, MAX)
+            .add(Rfc6020Mapping.CONTACT, 0, 1)
+            .add(Rfc6020Mapping.CONTAINER, 0, MAX)
+            .add(Rfc6020Mapping.DESCRIPTION, 0, 1)
+            .add(Rfc6020Mapping.DEVIATION, 0, MAX)
+            .add(Rfc6020Mapping.EXTENSION, 0, MAX)
+            .add(Rfc6020Mapping.FEATURE, 0, MAX)
+            .add(Rfc6020Mapping.GROUPING, 0, MAX)
+            .add(Rfc6020Mapping.IDENTITY, 0, MAX)
+            .add(Rfc6020Mapping.IMPORT, 0, MAX)
+            .add(Rfc6020Mapping.INCLUDE, 0, MAX)
+            .add(Rfc6020Mapping.LEAF, 0, MAX)
+            .add(Rfc6020Mapping.LEAF_LIST, 0, MAX)
+            .add(Rfc6020Mapping.LIST, 0, MAX)
+            .add(Rfc6020Mapping.NAMESPACE, 1, 1)
+            .add(Rfc6020Mapping.NOTIFICATION, 0, MAX)
+            .add(Rfc6020Mapping.ORGANIZATION, 0, 1)
+            .add(Rfc6020Mapping.PREFIX, 1, 1)
+            .add(Rfc6020Mapping.REFERENCE, 0, 1)
+            .add(Rfc6020Mapping.REVISION, 0, MAX)
+            .add(Rfc6020Mapping.RPC, 0, MAX)
+            .add(Rfc6020Mapping.TYPEDEF, 0, MAX)
+            .add(Rfc6020Mapping.USES, 0, MAX)
+            .add(Rfc6020Mapping.YANG_VERSION, 0, 1)
+            .build();
 
     public ModuleStatementSupport() {
         super(Rfc6020Mapping.MODULE);
@@ -69,8 +97,8 @@ public class ModuleStatementSupport extends
         Optional<URI> moduleNs = Optional.fromNullable(firstAttributeOf(stmt.declaredSubstatements(),
                 NamespaceStatement.class));
         if (!moduleNs.isPresent()) {
-            throw new IllegalArgumentException("Namespace of the module [" + stmt.getStatementArgument()
-                    + "] is missing.");
+            throw new SourceException(String.format("Namespace of the module [%s] is missing",
+                    stmt.getStatementArgument()), stmt.getStatementSourceReference());
         }
 
         Optional<Date> revisionDate = Optional.fromNullable(Utils.getLatestRevision(stmt.declaredSubstatements()));
@@ -78,7 +106,7 @@ public class ModuleStatementSupport extends
             revisionDate = Optional.of(SimpleDateFormatUtil.DEFAULT_DATE_REV);
         }
 
-        qNameModule = QNameModule.cachedReference(QNameModule.create(moduleNs.get(), revisionDate.orNull()));
+        QNameModule qNameModule = QNameModule.create(moduleNs.get(), revisionDate.orNull()).intern();
         ModuleIdentifier moduleIdentifier = new ModuleIdentifierImpl(stmt.getStatementArgument(),
                 Optional.<URI> absent(), revisionDate);
 
@@ -88,7 +116,8 @@ public class ModuleStatementSupport extends
 
         String modulePrefix = firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class);
         if (modulePrefix == null) {
-            throw new IllegalArgumentException("Prefix of the module [" + stmt.getStatementArgument() + "] is missing.");
+            throw new SourceException(String.format("Prefix of the module [%s] is missing",
+                    stmt.getStatementArgument()), stmt.getStatementSourceReference());
         }
 
         stmt.addToNs(PrefixToModule.class, modulePrefix, qNameModule);
@@ -101,10 +130,9 @@ public class ModuleStatementSupport extends
     }
 
     @Override
-    public void onFullDefinitionDeclared(
-            final Mutable<String, ModuleStatement, EffectiveStatement<String, ModuleStatement>> stmt)
-            throws SourceException {
-
-        stmt.addContext(NamespaceToModule.class, qNameModule, stmt);
+    public void onFullDefinitionDeclared(Mutable<String, ModuleStatement,
+            EffectiveStatement<String, ModuleStatement>> stmt) throws SourceException {
+        super.onFullDefinitionDeclared(stmt);
+        SUBSTATEMENT_VALIDATOR.validate(stmt);
     }
-}
\ No newline at end of file
+}