From fdcf549686105e2baf76a06e94a2da5ba0f73544 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Sat, 16 Dec 2023 21:55:17 +0100 Subject: [PATCH] Use schemaTreeChild() to lookup input/output We have proper RpcRuntimeType, hence we can follow along the schema tree axis to obtain the runtime types for RPC input/output. Change-Id: Iea34601edc340b788fc3ee4e17c5da7e245734b7 Signed-off-by: Robert Varga --- .../dom/codec/impl/BindingCodecContext.java | 21 ++++++++++--------- .../api/AbstractBindingRuntimeContext.java | 15 +++++++------ 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/BindingCodecContext.java b/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/BindingCodecContext.java index 82ba6fac4d..08997deaf3 100644 --- a/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/BindingCodecContext.java +++ b/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/BindingCodecContext.java @@ -38,7 +38,7 @@ import java.util.Map.Entry; import java.util.Optional; import java.util.ServiceLoader; import java.util.concurrent.ExecutionException; -import java.util.function.BiFunction; +import java.util.function.Function; import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.Nullable; import org.kohsuke.MetaInfServices; @@ -59,13 +59,13 @@ import org.opendaylight.mdsal.binding.loader.BindingClassLoader; import org.opendaylight.mdsal.binding.model.api.JavaTypeName; import org.opendaylight.mdsal.binding.runtime.api.ActionRuntimeType; import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeContext; -import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeTypes; import org.opendaylight.mdsal.binding.runtime.api.ChoiceRuntimeType; import org.opendaylight.mdsal.binding.runtime.api.ContainerLikeRuntimeType; import org.opendaylight.mdsal.binding.runtime.api.ContainerRuntimeType; import org.opendaylight.mdsal.binding.runtime.api.DataRuntimeType; import org.opendaylight.mdsal.binding.runtime.api.ListRuntimeType; import org.opendaylight.mdsal.binding.runtime.api.NotificationRuntimeType; +import org.opendaylight.mdsal.binding.runtime.api.RpcRuntimeType; import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections; import org.opendaylight.yangtools.concepts.Immutable; import org.opendaylight.yangtools.util.ClassLoaderUtils; @@ -303,11 +303,11 @@ public final class BindingCodecContext extends AbstractBindingNormalizedNodeSeri CacheBuilder.newBuilder().build(new CacheLoader<>() { @Override public ContainerLikeCodecContext load(final Class key) { - final BiFunction>> lookup; + final Function> lookup; if (RpcInput.class.isAssignableFrom(key)) { - lookup = BindingRuntimeTypes::findRpcInput; + lookup = RpcRuntimeType::input; } else if (RpcOutput.class.isAssignableFrom(key)) { - lookup = BindingRuntimeTypes::findRpcOutput; + lookup = RpcRuntimeType::output; } else { throw new IllegalArgumentException(key + " does not represent an RPC container"); } @@ -331,11 +331,12 @@ public final class BindingCodecContext extends AbstractBindingNormalizedNodeSeri final ContainerLike schema = getRpcDataSchema(potential, qname); checkArgument(schema != null, "Schema for %s does not define input / output.", potentialQName); - final var type = lookup.apply(context.getTypes(), potentialQName) - .orElseThrow(() -> new IllegalArgumentException("Cannot find runtime type for " + key)); - - // FIXME: accurate type - return new ContainerLikeCodecContext(key, type, BindingCodecContext.this); + final var runtimeType = context.getTypes().schemaTreeChild(potentialQName); + if (runtimeType instanceof RpcRuntimeType rpcType) { + // FIXME: accurate type + return new ContainerLikeCodecContext(key, lookup.apply(rpcType), BindingCodecContext.this); + } + throw new IllegalArgumentException("Cannot find runtime type for " + key); } } diff --git a/binding/mdsal-binding-runtime-api/src/main/java/org/opendaylight/mdsal/binding/runtime/api/AbstractBindingRuntimeContext.java b/binding/mdsal-binding-runtime-api/src/main/java/org/opendaylight/mdsal/binding/runtime/api/AbstractBindingRuntimeContext.java index 6b22a84432..bf0dc18a1d 100644 --- a/binding/mdsal-binding-runtime-api/src/main/java/org/opendaylight/mdsal/binding/runtime/api/AbstractBindingRuntimeContext.java +++ b/binding/mdsal-binding-runtime-api/src/main/java/org/opendaylight/mdsal/binding/runtime/api/AbstractBindingRuntimeContext.java @@ -105,16 +105,19 @@ public abstract class AbstractBindingRuntimeContext implements BindingRuntimeCon @Override public final Class getRpcInput(final QName rpcName) { - return loadClass(getTypes().findRpcInput(rpcName) - .orElseThrow(() -> new IllegalArgumentException("Failed to find RpcInput for " + rpcName))) - .asSubclass(RpcInput.class); + return loadClass(getRpc(rpcName).input()).asSubclass(RpcInput.class); } @Override public final Class getRpcOutput(final QName rpcName) { - return loadClass(getTypes().findRpcOutput(rpcName) - .orElseThrow(() -> new IllegalArgumentException("Failed to find RpcOutput for " + rpcName))) - .asSubclass(RpcOutput.class); + return loadClass(getRpc(rpcName).output()).asSubclass(RpcOutput.class); + } + + private @NonNull RpcRuntimeType getRpc(final QName rpcName) { + if (getTypes().schemaTreeChild(rpcName) instanceof RpcRuntimeType rpc) { + return rpc; + } + throw new IllegalArgumentException("Failed to find RPC for " + rpcName); } @Override -- 2.36.6