Bug 6669: Mandatory nodes cannot be added to node from another module via augment
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / AugmentUtils.java
index 09e78b4b420405930664af7df6ba92c20eaf8ec6..159b473370854ab51939ed91799fd822636830af 100644 (file)
@@ -10,6 +10,7 @@ package org.opendaylight.yangtools.yang.parser.stmt.rfc6020;
 import com.google.common.base.Verify;
 import com.google.common.collect.ImmutableList.Builder;
 import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Iterables;
 import java.util.Collection;
 import java.util.List;
 import java.util.Objects;
@@ -17,7 +18,6 @@ import java.util.Set;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
 import org.opendaylight.yangtools.yang.model.api.stmt.DataDefinitionStatement;
-import org.opendaylight.yangtools.yang.model.api.stmt.MandatoryStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.UsesStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.WhenStatement;
 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
@@ -89,15 +89,7 @@ public final class AugmentUtils {
         }
 
         if (typeOfCopy == CopyType.ADDED_BY_AUGMENTATION && reguiredCheckOfMandatoryNodes(sourceCtx, targetCtx)) {
-            final List<StatementContextBase<?, ?, ?>> sourceSubStatements = new Builder<StatementContextBase<?, ?, ?>>()
-                    .addAll(sourceCtx.declaredSubstatements()).addAll(sourceCtx.effectiveSubstatements()).build();
-
-            for (final StatementContextBase<?, ?, ?> sourceSubStatement : sourceSubStatements) {
-                InferenceException.throwIf(MandatoryStatement.class.equals(sourceSubStatement.getPublicDefinition()
-                        .getDeclaredRepresentationClass()), sourceCtx.getStatementSourceReference(),
-                        "An augment cannot add node '%s' because it is mandatory and in module different from target",
-                        sourceCtx.rawStatementArgument());
-            }
+            checkForMandatoryNodes(sourceCtx);
         }
 
         final List<StatementContextBase<?, ?, ?>> targetSubStatements = new Builder<StatementContextBase<?, ?, ?>>()
@@ -118,6 +110,27 @@ public final class AugmentUtils {
         }
     }
 
+    private static void checkForMandatoryNodes(final StatementContextBase<?, ?, ?> sourceCtx) {
+        if (StmtContextUtils.isNonPresenceContainer(sourceCtx)) {
+            /*
+             * We need to iterate over both declared and effective sub-statements,
+             * because a mandatory node can be:
+             * a) declared in augment body
+             * b) added to augment body also via uses of a grouping and
+             * such sub-statements are stored in effective sub-statements collection.
+             */
+            for (final StatementContextBase<?, ?, ?> sourceSubStatement : Iterables.concat(
+                    sourceCtx.declaredSubstatements(), sourceCtx.declaredSubstatements())) {
+                checkForMandatoryNodes(sourceSubStatement);
+            }
+        }
+
+        InferenceException.throwIf(StmtContextUtils.isMandatoryNode(sourceCtx),
+                sourceCtx.getStatementSourceReference(),
+                "An augment cannot add node '%s' because it is mandatory and in module different than target",
+                sourceCtx.rawStatementArgument());
+    }
+
     private static boolean reguiredCheckOfMandatoryNodes(final StatementContextBase<?, ?, ?> sourceCtx,
             StatementContextBase<?, ?, ?> targetCtx) {
         /*
@@ -146,7 +159,7 @@ public final class AugmentUtils {
                  * the same module, return false and skip mandatory nodes
                  * validation
                  */
-                if (Utils.isPresenceContainer(targetCtx)) {
+                if (StmtContextUtils.isPresenceContainer(targetCtx)) {
                     return false;
                 }
             }