Bug 5550 - Omit the check of mandatory nodes in uses-augment 24/36424/2
authorPeter Kajsa <pkajsa@cisco.com>
Fri, 18 Mar 2016 07:26:04 +0000 (08:26 +0100)
committerGerrit Code Review <gerrit@opendaylight.org>
Thu, 24 Mar 2016 10:00:41 +0000 (10:00 +0000)
Yang statement parser should omit check of mandatory nodes in uses-augment.

Change-Id: If6d02bf7013717dbe039b214cae69bf177aa073b
Signed-off-by: Peter Kajsa <pkajsa@cisco.com>
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/AugmentUtils.java
yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/test/Bug5550.java [new file with mode: 0644]
yang/yang-parser-impl/src/test/resources/bugs/bug5550/foo.yang [new file with mode: 0644]

index 9bcedcb22ba870bcc39e483e09da1346247017fb..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,
diff --git a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/test/Bug5550.java b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/test/Bug5550.java
new file mode 100644 (file)
index 0000000..828c7eb
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.stmt.test;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.io.FileNotFoundException;
+import java.net.URISyntaxException;
+import org.junit.Test;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.model.api.SchemaNode;
+import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
+import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
+import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
+
+public class Bug5550 {
+    private static final String NS = "foo";
+    private static final String REV = "2016-03-18";
+
+    @Test
+    public void test() throws SourceException, FileNotFoundException, ReactorException, URISyntaxException {
+        SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5550");
+        assertNotNull(context);
+
+        QName root = QName.create(NS, REV, "root");
+        QName containerInGrouping = QName.create(NS, REV, "container-in-grouping");
+        QName leaf1 = QName.create(NS, REV, "leaf-1");
+
+        SchemaNode findDataSchemaNode = SchemaContextUtil.findDataSchemaNode(context,
+                SchemaPath.create(true, root, containerInGrouping, leaf1));
+        assertTrue(findDataSchemaNode instanceof LeafSchemaNode);
+    }
+}
diff --git a/yang/yang-parser-impl/src/test/resources/bugs/bug5550/foo.yang b/yang/yang-parser-impl/src/test/resources/bugs/bug5550/foo.yang
new file mode 100644 (file)
index 0000000..3ad0916
--- /dev/null
@@ -0,0 +1,28 @@
+module foo {
+    namespace "foo";
+    prefix foo;
+    yang-version 1;
+
+    revision "2016-03-18" {
+        description "test";
+    }
+
+    grouping grouping-1 {
+        container container-in-grouping {
+        }
+    }
+
+    container root {
+    }
+
+    augment "/root" {
+        uses grouping-1 {
+            augment "container-in-grouping" {
+                leaf leaf-1 {
+                    type string;
+                    mandatory true;
+                }
+            }
+        }
+    }
+}