Propagate @Nonnull and @Nullable annotations
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / IncludeStatementImpl.java
index 929634c14cdbdc8a77c94cc936ebe4c3dc1fe42b..2742c051743e0af36d7dd7a1b2aa99aa4b0a1a45 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,12 +10,10 @@ 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.spi.source.IncludedSubmoduleNameToIdentifier;
-
-import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.IncludeEffectiveStatementImpl;
-import java.net.URI;
 import java.util.Collection;
 import java.util.Date;
+import java.util.Optional;
+import javax.annotation.Nonnull;
 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
@@ -23,8 +21,9 @@ import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.IncludeStatement;
 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.model.util.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,12 +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.SourceException;
-import com.google.common.base.Optional;
+import org.opendaylight.yangtools.yang.parser.spi.source.IncludedModuleContext;
+import org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToIdentifier;
+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).addOptional(Rfc6020Mapping.REVISION_DATE).build();
 
-    protected IncludeStatementImpl(StmtContext<String, IncludeStatement, ?> context) {
+    protected IncludeStatementImpl(final StmtContext<String, IncludeStatement, ?> context) {
         super(context);
     }
 
@@ -50,25 +52,24 @@ public class IncludeStatementImpl extends AbstractDeclaredStatement<String> impl
         }
 
         @Override
-        public String parseArgumentValue(StmtContext<?, ?, ?> ctx, String value) {
+        public String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
             return value;
         }
 
         @Override
-        public IncludeStatement createDeclared(StmtContext<String, IncludeStatement, ?> ctx) {
+        public IncludeStatement createDeclared(final StmtContext<String, IncludeStatement, ?> ctx) {
             return new IncludeStatementImpl(ctx);
         }
 
         @Override
         public EffectiveStatement<String, IncludeStatement> createEffective(
-                StmtContext<String, IncludeStatement, EffectiveStatement<String, IncludeStatement>> ctx) {
+                final StmtContext<String, IncludeStatement, EffectiveStatement<String, IncludeStatement>> ctx) {
             return new IncludeEffectiveStatementImpl(ctx);
         }
 
         @Override
         public void onLinkageDeclared(
-                final Mutable<String, IncludeStatement, EffectiveStatement<String, IncludeStatement>> stmt)
-                throws SourceException {
+                final Mutable<String, IncludeStatement, EffectiveStatement<String, IncludeStatement>> stmt) {
             final ModuleIdentifier includeSubmoduleIdentifier = getIncludeSubmoduleIdentifier(stmt);
 
             ModelActionBuilder includeAction = stmt.newInferenceAction(SOURCE_LINKAGE);
@@ -76,9 +77,8 @@ public class IncludeStatementImpl extends AbstractDeclaredStatement<String> impl
                     SubmoduleNamespace.class, includeSubmoduleIdentifier, SOURCE_LINKAGE);
 
             includeAction.apply(new InferenceAction() {
-
                 @Override
-                public void apply() throws InferenceException {
+                public void apply() {
                     StmtContext<?, ?, ?> includedSubModuleContext = requiresCtxPrerequisite.get();
 
                     stmt.addToNs(IncludedModuleContext.class, includeSubmoduleIdentifier,
@@ -88,16 +88,15 @@ public class IncludeStatementImpl extends AbstractDeclaredStatement<String> impl
                 }
 
                 @Override
-                public void prerequisiteFailed(Collection<? extends Prerequisite<?>> failed) throws InferenceException {
-                    if (failed.contains(requiresCtxPrerequisite)) {
-                        throw new InferenceException("Included submodule was not found: "+stmt.getStatementArgument(), stmt
-                                .getStatementSourceReference());
-                    }
+                public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
+                    InferenceException.throwIf(failed.contains(requiresCtxPrerequisite),
+                        stmt.getStatementSourceReference(),
+                        "Included submodule '%s' was not found: ", stmt.getStatementArgument());
                 }
             });
         }
 
-        private static ModuleIdentifier getIncludeSubmoduleIdentifier(Mutable<String, IncludeStatement, ?> stmt) {
+        private static ModuleIdentifier getIncludeSubmoduleIdentifier(final Mutable<String, IncludeStatement, ?> stmt) {
 
             String subModuleName = stmt.getStatementArgument();
 
@@ -106,15 +105,24 @@ public class IncludeStatementImpl extends AbstractDeclaredStatement<String> impl
                 revisionDate = SimpleDateFormatUtil.DEFAULT_DATE_IMP;
             }
 
-            return new ModuleIdentifierImpl(subModuleName, Optional.<URI> absent(), Optional.<Date> of(revisionDate));
+            return ModuleIdentifierImpl.create(subModuleName, Optional.empty(), Optional.of(revisionDate));
+        }
+
+        @Override
+        public void onFullDefinitionDeclared(final Mutable<String, IncludeStatement,
+                EffectiveStatement<String, IncludeStatement>> stmt) {
+            super.onFullDefinitionDeclared(stmt);
+            SUBSTATEMENT_VALIDATOR.validate(stmt);
         }
     }
 
+    @Nonnull
     @Override
     public String getModule() {
         return argument();
     }
 
+    @Nonnull
     @Override
     public PrefixStatement getPrefix() {
         return firstDeclared(PrefixStatement.class);
@@ -125,4 +133,4 @@ public class IncludeStatementImpl extends AbstractDeclaredStatement<String> impl
         return firstDeclared(RevisionDateStatement.class);
     }
 
-}
\ No newline at end of file
+}