Bug 5884: Augmenting a choice without a case results in no getter 58/40158/13
authorFilip Gregor <fgregor@cisco.com>
Fri, 10 Jun 2016 11:26:00 +0000 (13:26 +0200)
committerRobert Varga <nite@hq.sk>
Thu, 7 Jul 2016 08:27:10 +0000 (08:27 +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/AugmentTest.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/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 5f47b015adb7344ad1140d3390a2a76344ba7fde..a6553fc893683c9793a8e5ab008b79ca7d9df6fe 100644 (file)
@@ -11,6 +11,7 @@ import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 import com.google.common.base.Verify;
 import java.util.Collection;
+import java.util.Objects;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.QNameModule;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
@@ -29,6 +30,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.validation.ValidationBundlesNamespace;
 import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace.ValidationBundleType;
 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.GroupingUtils;
@@ -173,7 +175,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 97edbf247dbfd06824737354273f1cc982fb4110..d6adbe039a410c31677ad7613b66ec414391f799 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;
@@ -116,6 +118,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 907c22e58524649796d14348e2b6e2f65fe98121..8382e00bb67a65cc1e05d0d82901f090b3ba8a0f 100644 (file)
@@ -28,6 +28,7 @@ import org.opendaylight.yangtools.yang.parser.spi.meta.SemanticVersionModuleName
 import org.opendaylight.yangtools.yang.parser.spi.meta.SemanticVersionNamespace;
 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;
@@ -166,6 +167,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..bffbd5fbe4a154774564a022c173ef2d2bfb5f35 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())) &&
+                            Objects.equals(true, 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);
                 }
index febf7b747e3227a24ad3f870903543e54f3d57fc..a59c8abd3b1952de9b4ee432e61332e0a3198892 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/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..309b0e7
--- /dev/null
@@ -0,0 +1,66 @@
+/*
+ * 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;
+
+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
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