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 2ae751d4aad45127afb44068ba2f78eb20fa1170..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,13 +18,12 @@ 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;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
-import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.TypeOfCopy;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
+import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
 import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace;
 import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace.ValidationBundleType;
 import org.opendaylight.yangtools.yang.parser.stmt.reactor.RootStatementContext;
@@ -44,9 +44,9 @@ public final class AugmentUtils {
     private static void copyDeclaredStmts(final StatementContextBase<?, ?, ?> sourceCtx,
             final StatementContextBase<?, ?, ?> targetCtx) {
 
-        final TypeOfCopy typeOfCopy = sourceCtx.getParentContext().getPublicDefinition()
-                .getDeclaredRepresentationClass().equals(UsesStatement.class) ? TypeOfCopy.ADDED_BY_USES_AUGMENTATION
-                : TypeOfCopy.ADDED_BY_AUGMENTATION;
+        final CopyType typeOfCopy = sourceCtx.getParentContext().getPublicDefinition()
+                .getDeclaredRepresentationClass().equals(UsesStatement.class) ? CopyType.ADDED_BY_USES_AUGMENTATION
+                : CopyType.ADDED_BY_AUGMENTATION;
 
         for (final StatementContextBase<?, ?, ?> originalStmtCtx : sourceCtx.declaredSubstatements()) {
             if (!StmtContextUtils.areFeaturesSupported(originalStmtCtx)) {
@@ -65,9 +65,9 @@ public final class AugmentUtils {
 
     private static void copyEffectiveStmts(final StatementContextBase<?, ?, ?> sourceCtx,
             final StatementContextBase<?, ?, ?> targetCtx) {
-        final TypeOfCopy typeOfCopy = sourceCtx.getParentContext().getPublicDefinition()
-                .getDeclaredRepresentationClass().equals(UsesStatement.class) ? TypeOfCopy.ADDED_BY_USES_AUGMENTATION
-                : TypeOfCopy.ADDED_BY_AUGMENTATION;
+        final CopyType typeOfCopy = sourceCtx.getParentContext().getPublicDefinition()
+                .getDeclaredRepresentationClass().equals(UsesStatement.class) ? CopyType.ADDED_BY_USES_AUGMENTATION
+                : CopyType.ADDED_BY_AUGMENTATION;
 
         for (final StatementContextBase<?, ?, ?> originalStmtCtx : sourceCtx.effectiveSubstatements()) {
             if (needToCopyByAugment(originalStmtCtx)) {
@@ -82,22 +82,14 @@ public final class AugmentUtils {
     }
 
     private static void validateNodeCanBeCopiedByAugment(final StatementContextBase<?, ?, ?> sourceCtx,
-            final StatementContextBase<?, ?, ?> targetCtx, final TypeOfCopy typeOfCopy) {
+            final StatementContextBase<?, ?, ?> targetCtx, final CopyType typeOfCopy) {
 
         if (sourceCtx.getPublicDefinition().getDeclaredRepresentationClass().equals(WhenStatement.class)) {
             return;
         }
 
-        if (typeOfCopy == TypeOfCopy.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());
-            }
+        if (typeOfCopy == CopyType.ADDED_BY_AUGMENTATION && reguiredCheckOfMandatoryNodes(sourceCtx, targetCtx)) {
+            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;
                 }
             }