Bug 5550 - Omit the check of mandatory nodes in uses-augment
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / AugmentUtils.java
index 7c83a5f0281fb72e599ae5c56e8b010b778c7486..7843953b83353fae4dbecd45ec1dcab36d892610 100644 (file)
@@ -44,12 +44,12 @@ public final class AugmentUtils {
     private static void copyDeclaredStmts(final StatementContextBase<?, ?, ?> sourceCtx,
             final StatementContextBase<?, ?, ?> targetCtx) throws SourceException {
 
-        TypeOfCopy typeOfCopy = sourceCtx.getParentContext().getPublicDefinition().getDeclaredRepresentationClass()
+        final TypeOfCopy typeOfCopy = sourceCtx.getParentContext().getPublicDefinition().getDeclaredRepresentationClass()
                 .equals(UsesStatement.class) ? TypeOfCopy.ADDED_BY_USES_AUGMENTATION : TypeOfCopy.ADDED_BY_AUGMENTATION;
 
         for (StatementContextBase<?, ?, ?> originalStmtCtx : sourceCtx.declaredSubstatements()) {
             if (needToCopyByAugment(originalStmtCtx)) {
-                validateNodeCanBeCopiedByAugment(originalStmtCtx, targetCtx);
+                validateNodeCanBeCopiedByAugment(originalStmtCtx, targetCtx, typeOfCopy);
 
                 StatementContextBase<?, ?, ?> copy = originalStmtCtx.createCopy(targetCtx, typeOfCopy);
                 targetCtx.addEffectiveSubstatement(copy);
@@ -61,12 +61,12 @@ public final class AugmentUtils {
 
     private static void copyEffectiveStmts(final StatementContextBase<?, ?, ?> sourceCtx,
             final StatementContextBase<?, ?, ?> targetCtx) throws SourceException {
-        TypeOfCopy typeOfCopy = sourceCtx.getParentContext().getPublicDefinition().getDeclaredRepresentationClass()
+        final TypeOfCopy typeOfCopy = sourceCtx.getParentContext().getPublicDefinition().getDeclaredRepresentationClass()
                 .equals(UsesStatement.class) ? TypeOfCopy.ADDED_BY_USES_AUGMENTATION : TypeOfCopy.ADDED_BY_AUGMENTATION;
 
         for (StatementContextBase<?, ?, ?> originalStmtCtx : sourceCtx.effectiveSubstatements()) {
             if (needToCopyByAugment(originalStmtCtx)) {
-                validateNodeCanBeCopiedByAugment(originalStmtCtx, targetCtx);
+                validateNodeCanBeCopiedByAugment(originalStmtCtx, targetCtx, typeOfCopy);
 
                 StatementContextBase<?, ?, ?> copy = originalStmtCtx.createCopy(targetCtx, typeOfCopy);
                 targetCtx.addEffectiveSubstatement(copy);
@@ -77,22 +77,21 @@ public final class AugmentUtils {
     }
 
     private static void validateNodeCanBeCopiedByAugment(final StatementContextBase<?, ?, ?> sourceCtx,
-            final StatementContextBase<?, ?, ?> targetCtx) {
+            final StatementContextBase<?, ?, ?> targetCtx, final TypeOfCopy typeOfCopy) {
 
         if (sourceCtx.getPublicDefinition().getDeclaredRepresentationClass().equals(WhenStatement.class)) {
             return;
         }
 
-        if (reguiredCheckOfMandatoryNodes(sourceCtx, targetCtx)) {
+        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());
+                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());
             }
         }
 
@@ -114,7 +113,7 @@ public final class AugmentUtils {
         }
     }
 
-    private static boolean reguiredCheckOfMandatoryNodes(StatementContextBase<?, ?, ?> sourceCtx,
+    private static boolean reguiredCheckOfMandatoryNodes(final StatementContextBase<?, ?, ?> sourceCtx,
             StatementContextBase<?, ?, ?> targetCtx) {
         /*
          * If the statement argument is not QName, it cannot be mandatory statement,
@@ -155,10 +154,11 @@ public final class AugmentUtils {
         return false;
     }
 
-    private static final Set<Rfc6020Mapping> NOCOPY_DEV_SET = ImmutableSet.of(Rfc6020Mapping.USES);
+    private static final Set<Rfc6020Mapping> NOCOPY_DEF_SET = ImmutableSet.of(Rfc6020Mapping.USES, Rfc6020Mapping.WHEN,
+            Rfc6020Mapping.DESCRIPTION, Rfc6020Mapping.REFERENCE, Rfc6020Mapping.STATUS);
 
     public static boolean needToCopyByAugment(final StmtContext<?, ?, ?> stmtContext) {
-        return !NOCOPY_DEV_SET.contains(stmtContext.getPublicDefinition());
+        return !NOCOPY_DEF_SET.contains(stmtContext.getPublicDefinition());
     }
 
     private static final Set<Rfc6020Mapping> REUSED_DEF_SET = ImmutableSet.of(Rfc6020Mapping.TYPEDEF);