Allow refine to change 'default' in leaf-list 58/97258/2
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 19 Aug 2021 12:36:51 +0000 (14:36 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 19 Aug 2021 13:40:38 +0000 (15:40 +0200)
RFC7950 allows 'default' to be specified for 'leaf-list's, which we
recognize. Unfortunately the definition of refine statement does not
take this into account. Fix that and add an explicit test.

JIRA: YANGTOOLS-1312
Change-Id: I0c3ba7ca7cabba486c7226d520ce82a7ec2d194b
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit 0125f41acf8764d48c8a78d71a34647631811853)

yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/reactor/YangValidationBundles.java
yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/YT1312Test.java [new file with mode: 0644]
yang/yang-parser-rfc7950/src/test/resources/bugs/YT1312/foo.yang [new file with mode: 0644]

index bee9d0bf404170b042dd6092c1eb7f7e2948a4cc..97ebbd67deee3b6e3f1409018d685c140cffa0b5 100644 (file)
@@ -27,17 +27,18 @@ public final class YangValidationBundles {
         YangStmtMapping.MAX_ELEMENTS, YangStmtMapping.IF_FEATURE);
 
     public static final Map<StatementDefinition, Set<StatementDefinition>> SUPPORTED_REFINE_TARGETS =
-            ImmutableMap.<StatementDefinition, Set<StatementDefinition>>builder()
-            .put(YangStmtMapping.DEFAULT, ImmutableSet.of(YangStmtMapping.LEAF, YangStmtMapping.CHOICE))
+        ImmutableMap.<StatementDefinition, Set<StatementDefinition>>builder()
+            .put(YangStmtMapping.DEFAULT, ImmutableSet.of(
+                YangStmtMapping.LEAF, YangStmtMapping.CHOICE, YangStmtMapping.LEAF_LIST))
             .put(YangStmtMapping.MANDATORY, ImmutableSet.of(
-                YangStmtMapping.LEAF, YangStmtMapping.CHOICE, YangStmtMapping.ANYXML, YangStmtMapping.ANYDATA))
-        .put(YangStmtMapping.PRESENCE, ImmutableSet.of(YangStmtMapping.CONTAINER))
-        .put(YangStmtMapping.MUST, ImmutableSet.of(
-                YangStmtMapping.CONTAINER, YangStmtMapping.LIST, YangStmtMapping.LEAF,
-                YangStmtMapping.LEAF_LIST, YangStmtMapping.ANYXML, YangStmtMapping.ANYDATA))
-        .put(YangStmtMapping.MIN_ELEMENTS, ImmutableSet.of(YangStmtMapping.LIST, YangStmtMapping.LEAF_LIST))
-        .put(YangStmtMapping.MAX_ELEMENTS, ImmutableSet.of(YangStmtMapping.LIST, YangStmtMapping.LEAF_LIST))
-        .build();
+                YangStmtMapping.LEAF, YangStmtMapping.ANYDATA, YangStmtMapping.ANYXML, YangStmtMapping.CHOICE))
+            .put(YangStmtMapping.MAX_ELEMENTS, ImmutableSet.of(YangStmtMapping.LIST, YangStmtMapping.LEAF_LIST))
+            .put(YangStmtMapping.MIN_ELEMENTS, ImmutableSet.of(YangStmtMapping.LIST, YangStmtMapping.LEAF_LIST))
+            .put(YangStmtMapping.MUST, ImmutableSet.of(
+                YangStmtMapping.LEAF, YangStmtMapping.LEAF_LIST, YangStmtMapping.LIST, YangStmtMapping.CONTAINER,
+                YangStmtMapping.ANYDATA, YangStmtMapping.ANYXML))
+            .put(YangStmtMapping.PRESENCE, ImmutableSet.of(YangStmtMapping.CONTAINER))
+            .build();
 
     private YangValidationBundles() {
         // Hidden on purpose
diff --git a/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/YT1312Test.java b/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/YT1312Test.java
new file mode 100644 (file)
index 0000000..8723e27
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2021 PANTHEON.tech, s.r.o. 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.Assert.assertEquals;
+import static org.junit.Assert.assertNotSame;
+
+import java.net.URI;
+import java.util.Optional;
+import org.junit.Test;
+import org.opendaylight.yangtools.yang.common.QNameModule;
+import org.opendaylight.yangtools.yang.model.api.stmt.DefaultEffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.GroupingEffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.LeafListEffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
+
+public class YT1312Test {
+    @Test
+    public void testRefineDefault() throws Exception {
+        final ModuleEffectiveStatement module = StmtTestUtils.parseYangSource("/bugs/YT1312/foo.yang")
+            .getModuleStatements().get(QNameModule.create(URI.create("foo")));
+
+        final LeafListEffectiveStatement grpFoo = module
+            .findFirstEffectiveSubstatement(GroupingEffectiveStatement.class).orElseThrow()
+            .findFirstEffectiveSubstatement(LeafListEffectiveStatement.class).orElseThrow();
+        final LeafListEffectiveStatement foo = module
+            .findFirstEffectiveSubstatement(LeafListEffectiveStatement.class).orElseThrow();
+
+        assertNotSame(foo, grpFoo);
+        assertEquals(Optional.empty(), grpFoo.findFirstEffectiveSubstatementArgument(DefaultEffectiveStatement.class));
+        assertEquals(Optional.of("abc"), foo.findFirstEffectiveSubstatementArgument(DefaultEffectiveStatement.class));
+    }
+}
diff --git a/yang/yang-parser-rfc7950/src/test/resources/bugs/YT1312/foo.yang b/yang/yang-parser-rfc7950/src/test/resources/bugs/YT1312/foo.yang
new file mode 100644 (file)
index 0000000..00ff520
--- /dev/null
@@ -0,0 +1,26 @@
+module foo {
+  namespace foo;
+  prefix foo;
+  yang-version 1.1;
+
+  grouping grp {
+    leaf-list foo {
+      type string;
+    }
+    leaf-list bar {
+      type string;
+      default "";
+    }
+  }
+
+  uses grp {
+    refine foo {
+      default abc;
+    }
+
+    refine bar {
+      default def;
+    }
+  }
+}
+