From 53450b03cee1ad3eb8e065c17729bdd16decd574 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Tue, 2 May 2023 13:19:54 +0200 Subject: [PATCH] Fix runtime type search 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 --- .../reactor/CompositeRuntimeTypeBuilder.java | 6 +- .../binding/generator/impl/Mdsal824Test.java | 59 +++++++++++++++++++ .../src/test/resources/mdsal824/bar.yang | 23 ++++++++ .../src/test/resources/mdsal824/foo.yang | 24 ++++++++ 4 files changed, 110 insertions(+), 2 deletions(-) create mode 100644 binding/mdsal-binding-generator/src/test/java/org/opendaylight/mdsal/binding/generator/impl/Mdsal824Test.java create mode 100644 binding/mdsal-binding-generator/src/test/resources/mdsal824/bar.yang create mode 100644 binding/mdsal-binding-generator/src/test/resources/mdsal824/foo.yang diff --git a/binding/mdsal-binding-generator/src/main/java/org/opendaylight/mdsal/binding/generator/impl/reactor/CompositeRuntimeTypeBuilder.java b/binding/mdsal-binding-generator/src/main/java/org/opendaylight/mdsal/binding/generator/impl/reactor/CompositeRuntimeTypeBuilder.java index 4c13f3d5bb0..2902087d312 100644 --- a/binding/mdsal-binding-generator/src/main/java/org/opendaylight/mdsal/binding/generator/impl/reactor/CompositeRuntimeTypeBuilder.java +++ b/binding/mdsal-binding-generator/src/main/java/org/opendaylight/mdsal/binding/generator/impl/reactor/CompositeRuntimeTypeBuilder.java @@ -137,7 +137,7 @@ private void processGenerator(final AugmentResolver resolver, final AbstractComp } } - // ... and groupings recursively last + // ... groupings recursively next ... for (var grouping : parent.groupings()) { final AbstractExplicitGenerator found = findChildGenerator(grouping, localName); if (found != null) { @@ -145,6 +145,8 @@ private void processGenerator(final AugmentResolver resolver, final AbstractComp } } - 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 index 00000000000..a078db3303e --- /dev/null +++ b/binding/mdsal-binding-generator/src/test/java/org/opendaylight/mdsal/binding/generator/impl/Mdsal824Test.java @@ -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 index 00000000000..8c336d36fbd --- /dev/null +++ b/binding/mdsal-binding-generator/src/test/resources/mdsal824/bar.yang @@ -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 index 00000000000..1ca9977ac21 --- /dev/null +++ b/binding/mdsal-binding-generator/src/test/resources/mdsal824/foo.yang @@ -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; + } +}