Bug 5884: Augmenting a choice without a case results in no getter 38/42238/3
authorFilip Gregor <fgregor@cisco.com>
Fri, 10 Jun 2016 11:26:00 +0000 (13:26 +0200)
committerFilip Gregor <fgregor@cisco.com>
Fri, 29 Jul 2016 08:34:51 +0000 (08:34 +0000)
binding generator can not generate correctly getters for
augmented case shortHand.
added new namespace, added short hand case for augments
shortHand is created when augmenting choice with
case shortHand

Change-Id: I8543834bf3e3cf5e222d60617f116aca3639bc82
Signed-off-by: Filip Gregor <fgregor@cisco.com>
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/spi/source/AugmentToChoiceNamespace.java [new file with mode: 0644]
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/SubstatementContext.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/AugmentStatementImpl.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/YangInferencePipeline.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/AbstractEffectiveDocumentedDataNodeContainer.java
yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug5884Test.java [new file with mode: 0644]
yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/retest/AugmentTest.java
yang/yang-parser-impl/src/test/resources/bugs/bug5884/foo.yang [new file with mode: 0644]

diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/spi/source/AugmentToChoiceNamespace.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/spi/source/AugmentToChoiceNamespace.java
new file mode 100644 (file)
index 0000000..964063f
--- /dev/null
@@ -0,0 +1,19 @@
+/*
+ * Copyright (c) 2016 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.parser.spi.source;
+
+import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
+import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
+
+/**
+ * namespace key class for storing augment nodes which are going to be augmented as
+ * shortHand case nodes into choice node
+ */
+public interface AugmentToChoiceNamespace extends IdentifierNamespace<StmtContext<?, ?, ?>, Boolean> {
+
+}
\ No newline at end of file
index 24cb033571b9192bdcf28b1b1e47f017c24d7e6f..738592e4ad00f8b373dee3be2e0340e918014d38 100644 (file)
@@ -29,6 +29,7 @@ import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.Regist
 import org.opendaylight.yangtools.yang.parser.spi.meta.QNameCacheNamespace;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
+import org.opendaylight.yangtools.yang.parser.spi.source.AugmentToChoiceNamespace;
 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
 import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace;
 import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace.ValidationBundleType;
@@ -179,7 +180,8 @@ final class SubstatementContext<A, D extends DeclaredStatement<A>, E extends Eff
             }
 
             final SchemaPath path;
-            if (StmtContextUtils.producesDeclared(getParentContext(), ChoiceStatement.class)
+            if ((StmtContextUtils.producesDeclared(getParentContext(), ChoiceStatement.class)
+                    || Boolean.TRUE.equals(parent.getFromNamespace(AugmentToChoiceNamespace.class, parent)))
                     && isSupportedAsShorthandCase()) {
                 path = parentPath.createChild(qname);
             } else {
index 86b7533357b8ef01a04167fc23f2ff967f75f52c..c1e6b96565933f2be6d23af525bb085f48037421 100644 (file)
@@ -8,6 +8,7 @@
 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020;
 
 import static org.opendaylight.yangtools.yang.parser.spi.SubstatementValidator.MAX;
+
 import com.google.common.base.Preconditions;
 import java.util.Collection;
 import java.util.regex.Pattern;
@@ -27,6 +28,7 @@ import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
+import org.opendaylight.yangtools.yang.parser.spi.source.AugmentToChoiceNamespace;
 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
 import org.opendaylight.yangtools.yang.parser.spi.source.StmtOrderingNamespace;
 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
@@ -112,6 +114,12 @@ public class AugmentStatementImpl extends AbstractDeclaredStatement<SchemaNodeId
                         augmentNode.setIsSupportedToBuildEffective(false);
                         return;
                     }
+                    /**
+                     * Marks case short hand in augment
+                     */
+                    if (augmentTargetCtx.getPublicDefinition() == Rfc6020Mapping.CHOICE) {
+                        augmentNode.addToNs(AugmentToChoiceNamespace.class, augmentNode, true);
+                    }
 
                     // FIXME: this is a workaround for models which augment a node which is added via an extension
                     //        which we do not handle. This needs to be reworked in terms of unknown schema nodes.
index 4d3889680341b395dd4afad053a469a4ee38f287..66ff05cf1c6393a3db371782e31934c97537f38c 100644 (file)
@@ -25,6 +25,7 @@ import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
 import org.opendaylight.yangtools.yang.parser.spi.meta.QNameCacheNamespace;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupportBundle;
 import org.opendaylight.yangtools.yang.parser.spi.source.AnyxmlSchemaLocationNamespace;
+import org.opendaylight.yangtools.yang.parser.spi.source.AugmentToChoiceNamespace;
 import org.opendaylight.yangtools.yang.parser.spi.source.BelongsToModuleContext;
 import org.opendaylight.yangtools.yang.parser.spi.source.BelongsToPrefixToModuleIdentifier;
 import org.opendaylight.yangtools.yang.parser.spi.source.BelongsToPrefixToModuleName;
@@ -145,6 +146,7 @@ public final class YangInferencePipeline {
             .addSupport(new OrderedByStatementImpl.Definition())
             .addSupport(new WhenStatementImpl.Definition())
             .addSupport(new AugmentStatementImpl.Definition())
+            .addSupport(treeScoped(AugmentToChoiceNamespace.class))
             .addSupport(new RefineStatementImpl.Definition())
             .addSupport(new FeatureStatementImpl.Definition())
             .addSupport(new PositionStatementImpl.Definition())
index de640644cd12bd69efca2c3df6e8b17f41d9a3b4..c796e105b73c133300d606a28cdd4416fad37686 100644 (file)
@@ -14,8 +14,12 @@ import java.util.HashSet;
 import java.util.LinkedHashMap;
 import java.util.LinkedHashSet;
 import java.util.Map;
+import java.util.Objects;
 import java.util.Set;
 import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
+import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
+import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
@@ -24,6 +28,8 @@ import org.opendaylight.yangtools.yang.model.api.UsesNode;
 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
+import org.opendaylight.yangtools.yang.parser.spi.source.AugmentToChoiceNamespace;
+import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangValidationBundles;
 
 abstract class AbstractEffectiveDocumentedDataNodeContainer<A, D extends DeclaredStatement<A>>
         extends AbstractEffectiveDocumentedNode<A, D> implements
@@ -51,8 +57,20 @@ abstract class AbstractEffectiveDocumentedDataNodeContainer<A, D extends Declare
             if (effectiveStatement instanceof DataSchemaNode) {
                 final DataSchemaNode dataSchemaNode = (DataSchemaNode) effectiveStatement;
                 if (!mutableChildNodes.containsKey(dataSchemaNode.getQName())) {
-                    mutableChildNodes.put(dataSchemaNode.getQName(), dataSchemaNode);
-                    mutablePublicChildNodes.add(dataSchemaNode);
+                    /**
+                     * Add case short hand when augmenting choice with short hand
+                     **/
+                    if (this instanceof AugmentationSchema && !(effectiveStatement instanceof ChoiceCaseNode ||
+                            effectiveStatement instanceof ChoiceSchemaNode) &&
+                            (YangValidationBundles.SUPPORTED_CASE_SHORTHANDS.contains(effectiveStatement.statementDefinition())) &&
+                            Boolean.TRUE.equals(ctx.getFromNamespace(AugmentToChoiceNamespace.class, ctx))) {
+                        final CaseShorthandImpl caseShorthand = new CaseShorthandImpl(dataSchemaNode);
+                        mutableChildNodes.put(caseShorthand.getQName(), caseShorthand);
+                        mutablePublicChildNodes.add(caseShorthand);
+                    } else {
+                        mutableChildNodes.put(dataSchemaNode.getQName(), dataSchemaNode);
+                        mutablePublicChildNodes.add(dataSchemaNode);
+                    }
                 } else {
                     throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
                 }
diff --git a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug5884Test.java b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug5884Test.java
new file mode 100644 (file)
index 0000000..4825173
--- /dev/null
@@ -0,0 +1,67 @@
+/*
+ * Copyright (c) 2016 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;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.io.FileNotFoundException;
+import java.net.URISyntaxException;
+import java.text.ParseException;
+import java.util.Date;
+import java.util.Iterator;
+import java.util.Set;
+import org.junit.Test;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
+import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
+import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
+import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.Module;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
+import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
+import org.opendaylight.yangtools.yang.stmt.test.StmtTestUtils;
+
+public class Bug5884Test {
+    private static final String NS = "urn:yang.foo";
+    private static final String REV = "2016-01-01";
+
+    @Test
+    public void testBug5884() throws SourceException, FileNotFoundException, ReactorException, URISyntaxException, ParseException {
+        final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5884");
+        assertNotNull(context);
+
+        final QName root = QName.create(NS, REV, "main-container");
+        final QName choice = QName.create(NS, REV, "test-choice");
+        final QName testContainerQname = QName.create(NS, REV, "test");
+        final Date date = SimpleDateFormatUtil.getRevisionFormat().parse("2016-01-01");
+        final Module foo = context.findModuleByName("foo", date);
+        final ContainerSchemaNode rootContainer = (ContainerSchemaNode) context.getDataChildByName(root);
+        final ContainerSchemaNode testContainer = (ContainerSchemaNode) rootContainer.getDataChildByName(testContainerQname);
+        final ChoiceSchemaNode dataChildByName = (ChoiceSchemaNode) testContainer.getDataChildByName(choice);
+        final Set<AugmentationSchema> augmentations = foo.getAugmentations();
+        final Set<AugmentationSchema> availableAugmentations = dataChildByName.getAvailableAugmentations();
+        final Iterator<AugmentationSchema> iterator = augmentations.iterator();
+        final Iterator<AugmentationSchema> availableIterator = availableAugmentations.iterator();
+
+        testIterator(iterator);
+        testIterator(availableIterator);
+    }
+
+    private void testIterator(final Iterator<AugmentationSchema> iterator) {
+        AugmentationSchema allAugments;
+        while (iterator.hasNext()) {
+            allAugments = iterator.next();
+            final DataSchemaNode currentChoice = allAugments.getChildNodes().iterator().next();
+            assertTrue(currentChoice instanceof ChoiceCaseNode);
+        }
+    }
+}
\ No newline at end of file
index 066ece47f84c7eb505d7234c7d5ea465ba333a40..9bbff42a05ef2c4099c378be4656a7b6e83c0c53 100644 (file)
@@ -166,7 +166,7 @@ public class AugmentTest {
         assertNotNull(augmentHolder2);
 
         assertEquals(1, augment3.getChildNodes().size());
-        LeafSchemaNode pause = (LeafSchemaNode) augment3.getDataChildByName("pause");
+        ChoiceCaseNode pause = (ChoiceCaseNode) augment3.getDataChildByName("pause");
         assertNotNull(pause);
     }
 
diff --git a/yang/yang-parser-impl/src/test/resources/bugs/bug5884/foo.yang b/yang/yang-parser-impl/src/test/resources/bugs/bug5884/foo.yang
new file mode 100644 (file)
index 0000000..5ae1bd6
--- /dev/null
@@ -0,0 +1,78 @@
+module foo {
+    namespace "urn:yang.foo";
+    prefix "foo";
+
+    revision "2016-01-01";
+
+    container main-container {
+        uses test-grouping {
+            augment "test/test-choice" {
+                leaf short-aug-uses-inside {
+                    type string;
+                }
+            }
+
+            augment "test/test-choice" {
+                case normal-aug-uses-inside {
+                    leaf normal-aug-uses-inside {
+                        type string;
+                    }
+                }
+            }
+        }
+    }
+
+    grouping test-grouping {
+        container test {
+            choice test-choice {
+                leaf short {
+                    type string;
+                }
+                case normal {
+                    leaf normal {
+                        type string;
+                    }
+                }
+            }
+        }
+    }
+
+    choice test-choice {
+        leaf short {
+            type string;
+        }
+        case normal {
+            leaf normal {
+                type string;
+            }
+        }
+    }
+
+    augment "/test-choice" {
+        leaf short-aug-uses {
+            type string;
+        }
+    }
+
+    augment "/test-choice" {
+        case normal-aug-uses {
+            leaf normal-aug-uses {
+                type string;
+            }
+        }
+    }
+
+    augment "/main-container/test/test-choice" {
+        leaf short-aug-uses-outside {
+            type string;
+        }
+    }
+
+    augment "/main-container/test/test-choice" {
+        case normal-aug-uses-outside {
+            leaf normal-aug-uses-outside {
+                type string;
+            }
+        }
+    }
+}
\ No newline at end of file