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 / IncludeStatementImpl.java
index 4b425c33e161b5c5653e9d53280ac4024bbc8adc..d5869d6cf95288a229659fa99e865ec802323d29 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
@@ -10,10 +10,8 @@ package org.opendaylight.yangtools.yang.parser.stmt.rfc6020;
 import static org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase.SOURCE_LINKAGE;
 import static org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils.firstAttributeOf;
 
-import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.IncludeEffectiveStatementImpl;
-
+import com.google.common.base.Optional;
 import java.net.URI;
-import java.text.ParseException;
 import java.util.Collection;
 import java.util.Date;
 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
@@ -25,6 +23,7 @@ import org.opendaylight.yangtools.yang.model.api.stmt.PrefixStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.RevisionDateStatement;
 import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleIdentifierImpl;
 import org.opendaylight.yangtools.yang.parser.spi.SubmoduleNamespace;
+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.InferenceException;
@@ -33,10 +32,15 @@ import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Infere
 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
 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.IncludedSubmoduleNameToIdentifier;
 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
-import com.google.common.base.Optional;
+import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.IncludeEffectiveStatementImpl;
 
 public class IncludeStatementImpl extends AbstractDeclaredStatement<String> implements IncludeStatement {
+    private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(Rfc6020Mapping
+            .INCLUDE)
+            .add(Rfc6020Mapping.REVISION_DATE, 0, 1)
+            .build();
 
     protected IncludeStatementImpl(StmtContext<String, IncludeStatement, ?> context) {
         super(context);
@@ -68,30 +72,29 @@ public class IncludeStatementImpl extends AbstractDeclaredStatement<String> impl
         @Override
         public void onLinkageDeclared(
                 final Mutable<String, IncludeStatement, EffectiveStatement<String, IncludeStatement>> stmt)
-                throws InferenceException, SourceException {
+                throws SourceException {
             final ModuleIdentifier includeSubmoduleIdentifier = getIncludeSubmoduleIdentifier(stmt);
 
             ModelActionBuilder includeAction = stmt.newInferenceAction(SOURCE_LINKAGE);
             final Prerequisite<StmtContext<?, ?, ?>> requiresCtxPrerequisite = includeAction.requiresCtx(stmt,
                     SubmoduleNamespace.class, includeSubmoduleIdentifier, SOURCE_LINKAGE);
-            final Prerequisite<Mutable<?, ?, ?>> mutatesCtxPrerequisite = includeAction.mutatesCtx(stmt.getRoot(),
-                    SOURCE_LINKAGE);
 
             includeAction.apply(new InferenceAction() {
 
                 @Override
                 public void apply() throws InferenceException {
-                    StmtContext<?, ?, ?> includedSubmoduleStmt = requiresCtxPrerequisite.get();
+                    StmtContext<?, ?, ?> includedSubModuleContext = requiresCtxPrerequisite.get();
 
-                    mutatesCtxPrerequisite.get().addToNs(IncludedModuleContext.class, includeSubmoduleIdentifier,
-                            includedSubmoduleStmt);
+                    stmt.addToNs(IncludedModuleContext.class, includeSubmoduleIdentifier,
+                            includedSubModuleContext);
+                    stmt.addToNs(IncludedSubmoduleNameToIdentifier.class,
+                            stmt.getStatementArgument(), includeSubmoduleIdentifier);
                 }
 
                 @Override
                 public void prerequisiteFailed(Collection<? extends Prerequisite<?>> failed) throws InferenceException {
-                    System.out.println("");
                     if (failed.contains(requiresCtxPrerequisite)) {
-                        throw new InferenceException("Included submodule was not found.", stmt
+                        throw new InferenceException("Included submodule was not found: "+stmt.getStatementArgument(), stmt
                                 .getStatementSourceReference());
                     }
                 }
@@ -101,19 +104,20 @@ public class IncludeStatementImpl extends AbstractDeclaredStatement<String> impl
         private static ModuleIdentifier getIncludeSubmoduleIdentifier(Mutable<String, IncludeStatement, ?> stmt) {
 
             String subModuleName = stmt.getStatementArgument();
-            String revisionArg = firstAttributeOf(stmt.declaredSubstatements(), RevisionDateStatement.class);
-            final Optional<Date> revision;
-            if (revisionArg != null) {
-                try {
-                    revision = Optional.of(SimpleDateFormatUtil.getRevisionFormat().parse(revisionArg));
-                } catch (ParseException e) {
-                    throw new IllegalArgumentException(e);
-                }
-            } else {
-                revision = Optional.of(SimpleDateFormatUtil.DEFAULT_DATE_IMP);
+
+            Date revisionDate = firstAttributeOf(stmt.declaredSubstatements(), RevisionDateStatement.class);
+            if (revisionDate == null) {
+                revisionDate = SimpleDateFormatUtil.DEFAULT_DATE_IMP;
             }
 
-            return new ModuleIdentifierImpl(subModuleName, Optional.<URI> absent(), revision);
+            return new ModuleIdentifierImpl(subModuleName, Optional.<URI> absent(), Optional.<Date> of(revisionDate));
+        }
+
+        @Override
+        public void onFullDefinitionDeclared(Mutable<String, IncludeStatement,
+                EffectiveStatement<String, IncludeStatement>> stmt) throws SourceException {
+            super.onFullDefinitionDeclared(stmt);
+            SUBSTATEMENT_VALIDATOR.validate(stmt);
         }
     }