Add a proper grouping statement policy 37/94837/7
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 27 Jan 2021 10:54:58 +0000 (11:54 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 27 Jan 2021 13:13:09 +0000 (14:13 +0100)
A grouping is very similar to an uninstantiated statement, yet it
is a bit more loose. Add a proper StatementPolicy to cover its
mechanics.

JIRA: YANGTOOLS-1208
Change-Id: I79eb18766be252fd4b163e8a51653621585127a3
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/grouping/GroupingStatementSupport.java
yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/YT1208Test.java
yang/yang-parser-rfc7950/src/test/resources/bugs/YT1208/grouping.yang [new file with mode: 0644]

index 68ab5afcd3986fe3e2037eae9a794b8c7f822a49..5613c953d8f0a251e7886c5e4a986bcd788ce90a 100644 (file)
@@ -75,7 +75,11 @@ public final class GroupingStatementSupport
     private final SubstatementValidator validator;
 
     GroupingStatementSupport(final SubstatementValidator validator) {
-        super(YangStmtMapping.GROUPING, StatementPolicy.legacyDeclaredCopy());
+        super(YangStmtMapping.GROUPING, StatementPolicy.copyDeclared(
+            (copy, current, substatements) ->
+                copy.history().isAddedByUses() == current.history().isAddedByUses()
+                && copy.getArgument().equals(current.getArgument())
+                && copy.equalParentPath(current)));
         this.validator = requireNonNull(validator);
     }
 
index 040b5725e2dd2b341e8cd76a64ca1b18a7df4973..2792dde483775f254c999de7eacff7054aa7471e 100644 (file)
@@ -20,6 +20,28 @@ import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.NotificationEffectiveStatement;
 
 public class YT1208Test {
+    @Test
+    public void testGroupingStatementReuse() throws Exception {
+        final ModuleEffectiveStatement module = StmtTestUtils.parseYangSource("/bugs/YT1208/grouping.yang")
+            .getModuleStatements()
+            .get(QNameModule.create(URI.create("foo")));
+        assertNotNull(module);
+
+        final NotificationEffectiveStatement notif = module
+            .findFirstEffectiveSubstatement(NotificationEffectiveStatement.class).orElseThrow();
+
+        final GroupingEffectiveStatement grpBar = notif
+            .findFirstEffectiveSubstatement(GroupingEffectiveStatement.class).orElseThrow()
+            .findFirstEffectiveSubstatement(ContainerEffectiveStatement.class).orElseThrow()
+            .findFirstEffectiveSubstatement(GroupingEffectiveStatement.class).orElseThrow();
+        final GroupingEffectiveStatement contBar = notif
+            .findFirstEffectiveSubstatement(ContainerEffectiveStatement.class).orElseThrow()
+            .findFirstEffectiveSubstatement(ContainerEffectiveStatement.class).orElseThrow()
+            .findFirstEffectiveSubstatement(GroupingEffectiveStatement.class).orElseThrow();
+
+        assertSame(contBar, grpBar);
+    }
+
     @Test
     public void testLeafStatementReuse() throws Exception {
         final ModuleEffectiveStatement module = StmtTestUtils.parseYangSource("/bugs/YT1208/leaf.yang")
diff --git a/yang/yang-parser-rfc7950/src/test/resources/bugs/YT1208/grouping.yang b/yang/yang-parser-rfc7950/src/test/resources/bugs/YT1208/grouping.yang
new file mode 100644 (file)
index 0000000..28104d7
--- /dev/null
@@ -0,0 +1,23 @@
+module foo {
+  namespace foo;
+  prefix foo;
+
+  grouping grp {
+    container foo {
+      grouping bar {
+        container bar;
+        description "desc";
+      }
+    }
+  }
+
+  notification foo {
+    grouping baz {
+      uses grp;
+    }
+
+    container baz {
+      uses baz;
+    }
+  }
+}