.map(context::resolveIdentity)
.collect(Collectors.toUnmodifiableList()));
} else if (TypeDefinitions.LEAFREF.equals(arg)) {
- final AbstractTypeObjectGenerator<?, ?> targetGenerator = context.resolveLeafref(
- type.findFirstEffectiveSubstatementArgument(PathEffectiveStatement.class).orElseThrow());
- checkArgument(targetGenerator != this, "Effective model contains self-referencing leaf %s",
- statement().argument());
- refType = TypeReference.leafRef(targetGenerator);
+ refType = resolveLeafref(context);
} else if (TypeDefinitions.UNION.equals(arg)) {
unionDependencies = new UnionDependencies(type, context);
LOG.trace("Resolved union {} to dependencies {}", type, unionDependencies);
LOG.trace("Resolved derived {} to generator {}", type, refType);
}
+ private @NonNull TypeReference resolveLeafref(final GeneratorContext context) {
+ final AbstractTypeObjectGenerator<?, ?> targetGenerator;
+ try {
+ targetGenerator = context.resolveLeafref(
+ type.findFirstEffectiveSubstatementArgument(PathEffectiveStatement.class).orElseThrow());
+ } catch (IllegalArgumentException e) {
+ return TypeReference.leafRef(e);
+ }
+
+ checkArgument(targetGenerator != this, "Effective model contains self-referencing leaf %s",
+ statement().argument());
+ return TypeReference.leafRef(targetGenerator);
+ }
+
private static boolean isBuiltinName(final QName typeName) {
return YangConstants.RFC6020_YANG_MODULE.equals(typeName.getModule());
}
import org.opendaylight.mdsal.binding.model.api.Type;
import org.opendaylight.mdsal.binding.model.ri.Types;
-abstract class TypeReference {
+abstract sealed class TypeReference {
private static final class Identityref extends TypeReference {
private final List<IdentityGenerator> referencedGenerators;
}
// Note: this is exposed only for legacy naming handling
- abstract static class Leafref extends TypeReference {
+ abstract static sealed class Leafref extends TypeReference {
private Leafref() {
// Hidden on purpose
}
}
}
+ private static final class FailedLeafref extends Leafref {
+ private final IllegalArgumentException cause;
+
+ FailedLeafref(final IllegalArgumentException cause) {
+ this.cause = requireNonNull(cause);
+ }
+
+ @Override
+ Type methodReturnType(final TypeBuilderFactory builderFactory) {
+ throw new UnsupportedOperationException("Cannot ascertain type", cause);
+ }
+ }
+
static @NonNull TypeReference leafRef(final @Nullable AbstractTypeObjectGenerator<?, ?> referencedGenerator) {
return referencedGenerator == null ? UnresolvedLeafref.INSTANCE : new ResolvedLeafref(referencedGenerator);
}
+ static @NonNull TypeReference leafRef(final @NonNull IllegalArgumentException cause) {
+ return new FailedLeafref(cause);
+ }
+
static @NonNull TypeReference identityRef(final List<IdentityGenerator> referencedGenerators) {
return new Identityref(referencedGenerators);
}
*/
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 static org.junit.Assert.assertThrows;
import org.junit.Test;
-import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
public class Bug4621Test {
@Test
public void testMissingLeafrefTarget() {
- final EffectiveModelContext context = YangParserTestUtils.parseYangResource("/bug4621.yang");
- final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
+ final var context = YangParserTestUtils.parseYangResource("/bug4621.yang");
+ final var uoe = assertThrows(UnsupportedOperationException.class,
() -> DefaultBindingGenerator.generateFor(context));
- assertEquals("Failed to find leafref target /foo:neighbor/foo:mystring1", ex.getMessage());
+ assertEquals("Cannot ascertain type", uoe.getMessage());
+ final var cause = uoe.getCause();
+ assertThat(cause, instanceOf(IllegalArgumentException.class));
+ assertEquals("Failed to find leafref target /foo:neighbor/foo:mystring1", cause.getMessage());
}
}
\ No newline at end of file
@Test
public void javaTypeForSchemaDefinitionInvalidLeafrefPathTest() {
final var ctx = YangParserTestUtils.parseYangResource("/unresolvable-leafref.yang");
- final var ex = assertThrows(IllegalArgumentException.class, () -> DefaultBindingGenerator.generateFor(ctx));
+
+ final var uoe = assertThrows(UnsupportedOperationException.class,
+ () -> DefaultBindingGenerator.generateFor(ctx));
+ assertEquals("Cannot ascertain type", uoe.getMessage());
+ final var ex = uoe.getCause();
+ assertThat(ex, instanceOf(IllegalArgumentException.class));
assertEquals("Failed to find leafref target /somewhere/i/belong", ex.getMessage());
final var cause = ex.getCause();
assertThat(cause, instanceOf(IllegalArgumentException.class));
package org.opendaylight.mdsal.binding.generator.impl;
import static org.hamcrest.CoreMatchers.containsString;
+import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.opendaylight.mdsal.binding.model.api.MethodSignature;
import org.opendaylight.mdsal.binding.model.api.Type;
import org.opendaylight.yangtools.yang.binding.contract.Naming;
-import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
public class GeneratedTypesLeafrefTest {
@Test
public void testLeafrefInvalidPathResolving() {
- final EffectiveModelContext context = YangParserTestUtils.parseYangResource(
+ final var context = YangParserTestUtils.parseYangResource(
"/leafref-test-invalid-model/foo.yang");
assertEquals(1, context.getModules().size());
- IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
+ final var uoe = assertThrows(UnsupportedOperationException.class,
() -> DefaultBindingGenerator.generateFor(context));
- assertThat(ex.getMessage(), containsString("Failed to find leafref target"));
+ assertEquals("Cannot ascertain type", uoe.getMessage());
+ final var cause = uoe.getCause();
+ assertThat(cause, instanceOf(IllegalArgumentException.class));
+ assertThat(cause.getMessage(), containsString("Failed to find leafref target"));
}
}
\ No newline at end of file
--- /dev/null
+/*
+ * 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.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertSame;
+
+import java.util.Set;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
+import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
+
+public class Mdsal829Test {
+ private static EffectiveModelContext CONTEXT;
+
+ @BeforeClass
+ public static void beforeClass() {
+ CONTEXT = YangParserTestUtils.parseYangResource("/mdsal829.yang", Set.of());
+ }
+
+ @Test
+ public void testCompileTimeTypes() {
+ assertEquals(1, DefaultBindingGenerator.generateFor(CONTEXT).size());
+ }
+
+ @Test
+ public void testRunTimeTypes() {
+ final var types = BindingRuntimeTypesFactory.createTypes(CONTEXT);
+ assertSame(CONTEXT, types.getEffectiveModelContext());
+ final var schema = types.findSchema(
+ JavaTypeName.create("org.opendaylight.yang.gen.v1.mdsal829.norev", "Mdsal829Data")).orElseThrow();
+ assertNotNull(schema);
+ }
+}
--- /dev/null
+module mdsal829 {
+ namespace mdsal829;
+ prefix mdsal829;
+
+ feature feat;
+
+ typedef foo {
+ type leafref {
+ path /mdsal829:bar;
+ }
+ }
+
+ leaf bar {
+ if-feature feat;
+ type string;
+ }
+}