Fix runtime type search 73/105773/2
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 2 May 2023 11:19:54 +0000 (13:19 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 2 May 2023 13:07:34 +0000 (15:07 +0200)
When searching for children, we should be cognizant of the fact that we
might be looking at an inherited contract -- such as 'output' of an
'action' inherited through 'uses'. Extend the runtime search algorithm
to also look at previous() axis.

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

index 4c13f3d5bb04b399bd3f401e1552d8f06a751fec..2902087d31275db23766c92dcf548a98dbbb276f 100644 (file)
@@ -137,7 +137,7 @@ abstract class CompositeRuntimeTypeBuilder<S extends EffectiveStatement<?, ?>, R
             }
         }
 
-        // ... and groupings recursively last
+        // ... groupings recursively next ...
         for (var grouping : parent.groupings()) {
             final AbstractExplicitGenerator<S, ?> found = findChildGenerator(grouping, localName);
             if (found != null) {
@@ -145,6 +145,8 @@ abstract class CompositeRuntimeTypeBuilder<S extends EffectiveStatement<?, ?>, R
             }
         }
 
-        return null;
+        // ... and finally anything along instantiation axis ...
+        final var origParent = (AbstractCompositeGenerator<?, ?>) parent.previous();
+        return origParent == null ? null : findChildGenerator(origParent, localName);
     }
 }
diff --git a/binding/mdsal-binding-generator/src/test/java/org/opendaylight/mdsal/binding/generator/impl/Mdsal824Test.java b/binding/mdsal-binding-generator/src/test/java/org/opendaylight/mdsal/binding/generator/impl/Mdsal824Test.java
new file mode 100644 (file)
index 0000000..a078db3
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ * Copyright (c) 2023 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.hamcrest.CoreMatchers.instanceOf;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertEquals;
+
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
+import org.opendaylight.mdsal.binding.runtime.api.ActionRuntimeType;
+import org.opendaylight.mdsal.binding.runtime.api.ContainerRuntimeType;
+import org.opendaylight.mdsal.binding.runtime.api.InputRuntimeType;
+import org.opendaylight.mdsal.binding.runtime.api.ListRuntimeType;
+import org.opendaylight.mdsal.binding.runtime.api.OutputRuntimeType;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
+import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
+
+public class Mdsal824Test {
+    private static EffectiveModelContext CONTEXT;
+
+    @BeforeClass
+    public static void beforeClass() {
+        CONTEXT = YangParserTestUtils.parseYangResourceDirectory("/mdsal824");
+    }
+
+    @Test
+    public void testCompileTimeTypes() {
+        assertEquals(13, DefaultBindingGenerator.generateFor(CONTEXT).size());
+    }
+
+    @Test
+    public void testRunTimeTypes() {
+        final var types = BindingRuntimeTypesFactory.createTypes(CONTEXT);
+        final var barTop = types.schemaTreeChild(QName.create("bar", "bar-top"));
+        assertThat(barTop, instanceOf(ContainerRuntimeType.class));
+        final var barList = ((ContainerRuntimeType) barTop).schemaTreeChild(QName.create("bar", "bar-list"));
+        assertThat(barList, instanceOf(ListRuntimeType.class));
+        final var barAction = ((ListRuntimeType) barList).schemaTreeChild(QName.create("bar", "foo"));
+        assertThat(barAction, instanceOf(ActionRuntimeType.class));
+
+        final var barInput = ((ActionRuntimeType) barAction).schemaTreeChild(QName.create("bar", "input"));
+        assertThat(barInput, instanceOf(InputRuntimeType.class));
+        assertEquals(JavaTypeName.create("org.opendaylight.yang.gen.v1.foo.norev.act.grp", "FooInput"),
+            barInput.javaType().getIdentifier());
+
+        final var barOutput = ((ActionRuntimeType) barAction).schemaTreeChild(QName.create("bar", "output"));
+        assertThat(barOutput, instanceOf(OutputRuntimeType.class));
+        assertEquals(JavaTypeName.create("org.opendaylight.yang.gen.v1.foo.norev.act.grp", "FooOutput"),
+            barOutput.javaType().getIdentifier());
+    }
+}
diff --git a/binding/mdsal-binding-generator/src/test/resources/mdsal824/bar.yang b/binding/mdsal-binding-generator/src/test/resources/mdsal824/bar.yang
new file mode 100644 (file)
index 0000000..8c336d3
--- /dev/null
@@ -0,0 +1,23 @@
+module bar {
+  yang-version 1.1;
+  namespace bar;
+  prefix bar;
+
+  import foo { prefix foo; }
+
+  grouping bar-grp {
+    list bar-list {
+      key key;
+      leaf key {
+        type string;
+      }
+
+      uses foo:uses-grp;
+    }
+  }
+
+  container bar-top {
+    uses bar-grp;
+  }
+}
+
diff --git a/binding/mdsal-binding-generator/src/test/resources/mdsal824/foo.yang b/binding/mdsal-binding-generator/src/test/resources/mdsal824/foo.yang
new file mode 100644 (file)
index 0000000..1ca9977
--- /dev/null
@@ -0,0 +1,24 @@
+module foo {
+  yang-version 1.1;
+  namespace foo;
+  prefix foo;
+
+  grouping act-grp {
+    action foo {
+      output {
+        choice type {
+          mandatory true;
+          case some-type {
+            leaf some-type {
+              type string;
+            }
+          }
+        }
+      }
+    }
+  }
+
+  grouping uses-grp {
+    uses act-grp;
+  }
+}