Do not create effective copy for unsupported statements 87/107387/5
authorSangwook Ha <sangwook.ha@verizon.com>
Thu, 10 Aug 2023 23:15:25 +0000 (16:15 -0700)
committerRobert Varga <nite@hq.sk>
Tue, 17 Oct 2023 15:51:12 +0000 (15:51 +0000)
If we are encountering a statement which is not supported, we should not
create an effective copy of it for the purposes of comparison.

JIRA: YANGTOOLS-1532
Change-Id: I4407e38f2a62c911b653191bf7c6cfd82ba0225e
Signed-off-by: Sangwook Ha <sangwook.ha@verizon.com>
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
parser/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/StatementContextBase.java
parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/YT1532Test.java [new file with mode: 0644]
parser/yang-parser-rfc7950/src/test/resources/bugs/YT1532/augment-with-description-all-leafs-with-if-feature/foo.yang [new file with mode: 0644]
parser/yang-parser-rfc7950/src/test/resources/bugs/YT1532/augment-with-description-no-leaf-with-if-feature/foo.yang [new file with mode: 0644]
parser/yang-parser-rfc7950/src/test/resources/bugs/YT1532/augment-with-no-description-all-leafs-with-if-feature/foo.yang [new file with mode: 0644]
parser/yang-parser-rfc7950/src/test/resources/bugs/YT1532/augment-with-no-description-one-leaf-with-if-feature/foo.yang [new file with mode: 0644]

index 04bb3d6cdb353629fbc3abddda3c61da4224d9bc..c7dadcae9f9bc4265dd60da6bbc7f45e6cd1f80e 100644 (file)
@@ -727,7 +727,12 @@ abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E extends
     @Override
     final ReactorStmtCtx<?, ?, ?> asEffectiveChildOf(final StatementContextBase<?, ?, ?> parent, final CopyType type,
             final QNameModule targetModule) {
-        final ReactorStmtCtx<A, D, E> copy = copyAsChildOfImpl(parent, type, targetModule);
+        if (!isSupportedToBuildEffective() || !isSupportedByFeatures()) {
+            // Do not create effective copies, as they cannot be built anyway
+            return null;
+        }
+
+        final var copy = copyAsChildOfImpl(parent, type, targetModule);
         if (copy == null) {
             // The statement fizzled, this should never happen, perhaps a verify()?
             return null;
diff --git a/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/YT1532Test.java b/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/YT1532Test.java
new file mode 100644 (file)
index 0000000..407d66b
--- /dev/null
@@ -0,0 +1,65 @@
+/*
+ * Copyright (c) 2023 Verizon 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;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.util.Set;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.model.api.stmt.ContainerEffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.LeafEffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
+
+class YT1532Test extends AbstractYangTest {
+    private static final QName FOO = QName.create("urn:foo", "foo");
+    private static final QName BAR1 = QName.create("urn:foo", "bar1");
+    private static final QName BAR2 = QName.create("urn:foo", "bar2");
+    private static final QName BAZ = QName.create("urn:foo", "baz");
+
+    @ParameterizedTest(name = "Augmentation of grouping with {0}")
+    @MethodSource("generateArgs")
+    void assertFoo(final String testDesc, final String dirName, final Set<QName> expectedLeafs) {
+        final var foo = assertFooModule(dirName)
+            .findFirstEffectiveSubstatement(ContainerEffectiveStatement.class).orElseThrow();
+        assertEquals(FOO, foo.argument());
+        assertEquals(expectedLeafs, findLeafs(foo));
+    }
+
+    private static Stream<Arguments> generateArgs() {
+        return Stream.of(
+            Arguments.of("description and no leaf with if-feature",
+                "augment-with-description-no-leaf-with-if-feature",
+                Set.of(BAR1, BAR2, BAZ)),
+            Arguments.of("no description and all leafs with if-feature",
+                "augment-with-no-description-all-leafs-with-if-feature",
+                Set.of(BAZ)),
+            Arguments.of("description and all leafs with if-feature",
+                "augment-with-description-all-leafs-with-if-feature",
+                Set.of(BAZ)),
+            Arguments.of("no description and one leaf with if-feature",
+                "augment-with-no-description-one-leaf-with-if-feature",
+                Set.of(BAR2, BAZ)));
+    }
+
+    private static ModuleEffectiveStatement assertFooModule(final String dirName) {
+        return assertEffectiveModelDir("/bugs/YT1532/" + dirName, Set.of())
+            .findModuleStatement(FOO).orElseThrow();
+    }
+
+    private static Set<Object> findLeafs(final ContainerEffectiveStatement foo) {
+        return foo.effectiveSubstatements().stream().filter(LeafEffectiveStatement.class::isInstance)
+            .map(LeafEffectiveStatement.class::cast)
+            .map(LeafEffectiveStatement::argument)
+            .collect(Collectors.toSet());
+    }
+}
diff --git a/parser/yang-parser-rfc7950/src/test/resources/bugs/YT1532/augment-with-description-all-leafs-with-if-feature/foo.yang b/parser/yang-parser-rfc7950/src/test/resources/bugs/YT1532/augment-with-description-all-leafs-with-if-feature/foo.yang
new file mode 100644 (file)
index 0000000..d522586
--- /dev/null
@@ -0,0 +1,34 @@
+module foo {
+  namespace "urn:foo";
+  prefix "foo";
+
+  feature alpha;
+
+  grouping foo-group {
+    container foo;
+  }
+
+  grouping bar-group {
+    uses foo-group {
+      augment foo {
+        description "augmentation to foo";
+        leaf bar1 {
+          if-feature alpha;
+          type string;
+        }
+        leaf bar2 {
+          if-feature alpha;
+          type string;
+        }
+      }
+    }
+  }
+
+  uses bar-group {
+    augment foo {
+      leaf baz {
+        type string;
+      }
+    }
+  }
+}
diff --git a/parser/yang-parser-rfc7950/src/test/resources/bugs/YT1532/augment-with-description-no-leaf-with-if-feature/foo.yang b/parser/yang-parser-rfc7950/src/test/resources/bugs/YT1532/augment-with-description-no-leaf-with-if-feature/foo.yang
new file mode 100644 (file)
index 0000000..bae3948
--- /dev/null
@@ -0,0 +1,32 @@
+module foo {
+  namespace "urn:foo";
+  prefix "foo";
+
+  feature alpha;
+
+  grouping foo-group {
+    container foo;
+  }
+
+  grouping bar-group {
+    uses foo-group {
+      augment foo {
+        description "augmentation to foo";
+        leaf bar1 {
+          type string;
+        }
+        leaf bar2 {
+          type string;
+        }
+      }
+    }
+  }
+
+  uses bar-group {
+    augment foo {
+      leaf baz {
+        type string;
+      }
+    }
+  }
+}
diff --git a/parser/yang-parser-rfc7950/src/test/resources/bugs/YT1532/augment-with-no-description-all-leafs-with-if-feature/foo.yang b/parser/yang-parser-rfc7950/src/test/resources/bugs/YT1532/augment-with-no-description-all-leafs-with-if-feature/foo.yang
new file mode 100644 (file)
index 0000000..07ae048
--- /dev/null
@@ -0,0 +1,33 @@
+module foo {
+  namespace "urn:foo";
+  prefix "foo";
+
+  feature alpha;
+
+  grouping foo-group {
+    container foo;
+  }
+
+  grouping bar-group {
+    uses foo-group {
+      augment foo {
+        leaf bar1 {
+          if-feature alpha;
+          type string;
+        }
+        leaf bar2 {
+          if-feature alpha;
+          type string;
+        }
+      }
+    }
+  }
+
+  uses bar-group {
+    augment foo {
+      leaf baz {
+        type string;
+      }
+    }
+  }
+}
diff --git a/parser/yang-parser-rfc7950/src/test/resources/bugs/YT1532/augment-with-no-description-one-leaf-with-if-feature/foo.yang b/parser/yang-parser-rfc7950/src/test/resources/bugs/YT1532/augment-with-no-description-one-leaf-with-if-feature/foo.yang
new file mode 100644 (file)
index 0000000..b98703d
--- /dev/null
@@ -0,0 +1,32 @@
+module foo {
+  namespace "urn:foo";
+  prefix "foo";
+
+  feature alpha;
+
+  grouping foo-group {
+    container foo;
+  }
+
+  grouping bar-group {
+    uses foo-group {
+      augment foo {
+        leaf bar1 {
+          if-feature alpha;
+          type string;
+        }
+        leaf bar2 {
+          type string;
+        }
+      }
+    }
+  }
+
+  uses bar-group {
+    augment foo {
+      leaf baz {
+        type string;
+      }
+    }
+  }
+}