From e82d91a6c79d0f210c74e459b4363d5e1112128a Mon Sep 17 00:00:00 2001 From: Sangwook Ha Date: Thu, 10 Aug 2023 16:15:25 -0700 Subject: [PATCH] Do not create effective copy for unsupported statements 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 Signed-off-by: Robert Varga --- .../stmt/reactor/StatementContextBase.java | 7 +- .../yangtools/yang/stmt/YT1532Test.java | 65 +++++++++++++++++++ .../foo.yang | 34 ++++++++++ .../foo.yang | 32 +++++++++ .../foo.yang | 33 ++++++++++ .../foo.yang | 32 +++++++++ 6 files changed, 202 insertions(+), 1 deletion(-) create mode 100644 parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/YT1532Test.java create mode 100644 parser/yang-parser-rfc7950/src/test/resources/bugs/YT1532/augment-with-description-all-leafs-with-if-feature/foo.yang create mode 100644 parser/yang-parser-rfc7950/src/test/resources/bugs/YT1532/augment-with-description-no-leaf-with-if-feature/foo.yang create mode 100644 parser/yang-parser-rfc7950/src/test/resources/bugs/YT1532/augment-with-no-description-all-leafs-with-if-feature/foo.yang create mode 100644 parser/yang-parser-rfc7950/src/test/resources/bugs/YT1532/augment-with-no-description-one-leaf-with-if-feature/foo.yang diff --git a/parser/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/StatementContextBase.java b/parser/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/StatementContextBase.java index 04bb3d6cdb..c7dadcae9f 100644 --- a/parser/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/StatementContextBase.java +++ b/parser/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/StatementContextBase.java @@ -727,7 +727,12 @@ abstract class StatementContextBase, E extends @Override final ReactorStmtCtx asEffectiveChildOf(final StatementContextBase parent, final CopyType type, final QNameModule targetModule) { - final ReactorStmtCtx 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 index 0000000000..407d66bdc3 --- /dev/null +++ b/parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/YT1532Test.java @@ -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 expectedLeafs) { + final var foo = assertFooModule(dirName) + .findFirstEffectiveSubstatement(ContainerEffectiveStatement.class).orElseThrow(); + assertEquals(FOO, foo.argument()); + assertEquals(expectedLeafs, findLeafs(foo)); + } + + private static Stream 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 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 index 0000000000..d522586c67 --- /dev/null +++ b/parser/yang-parser-rfc7950/src/test/resources/bugs/YT1532/augment-with-description-all-leafs-with-if-feature/foo.yang @@ -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 index 0000000000..bae3948a37 --- /dev/null +++ b/parser/yang-parser-rfc7950/src/test/resources/bugs/YT1532/augment-with-description-no-leaf-with-if-feature/foo.yang @@ -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 index 0000000000..07ae048a86 --- /dev/null +++ b/parser/yang-parser-rfc7950/src/test/resources/bugs/YT1532/augment-with-no-description-all-leafs-with-if-feature/foo.yang @@ -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 index 0000000000..b98703d42b --- /dev/null +++ b/parser/yang-parser-rfc7950/src/test/resources/bugs/YT1532/augment-with-no-description-one-leaf-with-if-feature/foo.yang @@ -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; + } + } + } +} -- 2.36.6