Fix action/grouping lookup mechanics 93/88093/2
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 27 Feb 2020 09:38:33 +0000 (10:38 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 27 Feb 2020 14:24:03 +0000 (15:24 +0100)
When we are following references towards the original definitions
we need to mind the fact that 'uses' statement may point to a grouping
in a different module and hence we need to adjust namespace accordingly.

The same thing applies when we want to find generated types for such
actions' input/output -- here we need to acquire the correct
ModuleContext.

JIRA: MDSAL-516
Change-Id: I1931279beb8e663e6d8558d50c5d7d274e248cf3
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/mdsal/binding/generator/impl/AbstractTypeGenerator.java
binding/mdsal-binding-generator-impl/src/test/java/org/opendaylight/mdsal/binding/generator/impl/Mdsal516Test.java [new file with mode: 0644]
binding/mdsal-binding-generator-impl/src/test/resources/mdsal-516/bar.yang [new file with mode: 0644]
binding/mdsal-binding-generator-impl/src/test/resources/mdsal-516/foo.yang [new file with mode: 0644]

index 678200e2874059cf6b4bfad64b43fd5d5250923c..7db5d01ff7a789698382c84b37783044e3af61ed 100644 (file)
@@ -437,8 +437,11 @@ abstract class AbstractTypeGenerator {
             final GeneratedType output;
             if (action.isAddedByUses()) {
                 final ActionDefinition orig = findOrigAction(parentSchema, action).get();
-                input = context.getChildNode(orig.getInput().getPath()).build();
-                output = context.getChildNode(orig.getOutput().getPath()).build();
+                // Original definition may live in a different module, make sure we account for that
+                final ModuleContext origContext = moduleContext(
+                    orig.getPath().getPathFromRoot().iterator().next().getModule());
+                input = origContext.getChildNode(orig.getInput().getPath()).build();
+                output = origContext.getChildNode(orig.getOutput().getPath()).build();
             } else {
                 input = actionContainer(context, RPC_INPUT, action.getInput(), inGrouping);
                 output = actionContainer(context, RPC_OUTPUT, action.getOutput(), inGrouping);
@@ -466,9 +469,12 @@ abstract class AbstractTypeGenerator {
     }
 
     private Optional<ActionDefinition> findOrigAction(final DataNodeContainer parent, final ActionDefinition action) {
+        final QName qname = action.getQName();
         for (UsesNode uses : parent.getUses()) {
             final GroupingDefinition grp = findUsedGrouping(uses);
-            final Optional<ActionDefinition> found = grp.findAction(action.getQName());
+            // Target grouping may reside in a different module, hence we need to rebind the QName to match grouping's
+            // namespace
+            final Optional<ActionDefinition> found = grp.findAction(qname.withModule(grp.getQName().getModule()));
             if (found.isPresent()) {
                 final ActionDefinition result = found.get();
                 return result.isAddedByUses() ? findOrigAction(grp, result) : found;
diff --git a/binding/mdsal-binding-generator-impl/src/test/java/org/opendaylight/mdsal/binding/generator/impl/Mdsal516Test.java b/binding/mdsal-binding-generator-impl/src/test/java/org/opendaylight/mdsal/binding/generator/impl/Mdsal516Test.java
new file mode 100644 (file)
index 0000000..3142bfd
--- /dev/null
@@ -0,0 +1,26 @@
+/*
+ * Copyright (c) 2020 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.mdsal.binding.generator.impl;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import java.util.List;
+import org.junit.Test;
+import org.opendaylight.mdsal.binding.model.api.Type;
+import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
+
+public class Mdsal516Test {
+    @Test
+    public void testGroupingActionType() {
+        final List<Type> types = new BindingGeneratorImpl().generateTypes(
+            YangParserTestUtils.parseYangResourceDirectory("/mdsal-516"));
+        assertNotNull(types);
+        assertEquals(7, types.size());
+    }
+}
diff --git a/binding/mdsal-binding-generator-impl/src/test/resources/mdsal-516/bar.yang b/binding/mdsal-binding-generator-impl/src/test/resources/mdsal-516/bar.yang
new file mode 100644 (file)
index 0000000..abf384c
--- /dev/null
@@ -0,0 +1,10 @@
+module bar {
+  yang-version 1.1;
+  namespace "bar";
+  prefix bar;
+
+  grouping bar-grp {
+    action something;
+  }
+}
+  
diff --git a/binding/mdsal-binding-generator-impl/src/test/resources/mdsal-516/foo.yang b/binding/mdsal-binding-generator-impl/src/test/resources/mdsal-516/foo.yang
new file mode 100644 (file)
index 0000000..02e8bc5
--- /dev/null
@@ -0,0 +1,18 @@
+module foo {
+  yang-version 1.1;
+  namespace "foo";
+  prefix foo;
+
+  import bar {
+    prefix bar;
+  }
+
+  grouping foo-grp {
+    uses bar:bar-grp;
+  }
+
+  container foo {
+    uses foo-grp;
+  }
+}
+