2 * Copyright (c) 2022 PANTHEON.tech, s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.mdsal.binding.dom.adapter;
10 import static java.util.Objects.requireNonNull;
12 import java.lang.reflect.InvocationHandler;
13 import java.lang.reflect.Method;
14 import java.lang.reflect.Proxy;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.mdsal.binding.dom.adapter.RpcInvocationStrategy.ContentRouted;
17 import org.opendaylight.mdsal.binding.runtime.api.RpcRuntimeType;
18 import org.opendaylight.mdsal.dom.api.DOMRpcService;
19 import org.opendaylight.mdsal.dom.spi.ContentRoutedRpcContext;
20 import org.opendaylight.yangtools.yang.binding.Rpc;
21 import org.opendaylight.yangtools.yang.binding.RpcInput;
22 import org.opendaylight.yangtools.yang.binding.contract.Naming;
24 final class RpcAdapter implements InvocationHandler {
25 private final @NonNull AdapterContext adapterContext;
26 private final @NonNull DOMRpcService delegate;
27 private final RpcInvocationStrategy strategy;
28 private final @NonNull Rpc<?, ?> facade;
29 private final String name;
31 <T extends Rpc<?, ?>> RpcAdapter(final AdapterContext adapterContext, final DOMRpcService delegate,
32 final Class<T> type) {
33 this.adapterContext = requireNonNull(adapterContext);
34 this.delegate = requireNonNull(delegate);
36 final var serializer = adapterContext.currentSerializer();
37 final var rpcType = serializer.getRuntimeContext().getRpcDefinition(type);
38 if (rpcType == null) {
39 throw new IllegalStateException("Failed to find runtime type for " + type);
42 facade = type.cast(Proxy.newProxyInstance(type.getClassLoader(), new Class<?>[] { type }, this));
43 name = type.getName();
45 strategy = createStrategy(serializer, rpcType);
48 private @NonNull RpcInvocationStrategy createStrategy(final CurrentAdapterSerializer serializer,
49 final RpcRuntimeType rpcType) {
50 final var rpc = rpcType.statement();
51 final var contentContext = ContentRoutedRpcContext.forRpc(rpc);
52 if (contentContext != null) {
53 final var extractor = serializer.findExtractor(rpcType.input());
54 if (extractor != null) {
55 return new ContentRouted(this, rpc.argument(), contentContext.leaf(), extractor);
58 return new RpcInvocationStrategy(this, rpc.argument());
62 public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
63 switch (method.getName()) {
64 case Naming.RPC_INVOKE_NAME:
65 if (method.getParameterCount() == 1) {
66 return strategy.invoke((RpcInput) requireNonNull(args[0]));
70 if (method.getReturnType().equals(String.class) && method.getParameterCount() == 0) {
71 return name + "$Adapter{delegate=" + delegate + "}";
75 if (method.getReturnType().equals(int.class) && method.getParameterCount() == 0) {
76 return System.identityHashCode(proxy);
80 if (method.getReturnType().equals(boolean.class) && method.getParameterCount() == 1
81 && method.getParameterTypes()[0] == Object.class) {
82 return proxy == args[0];
89 if (method.isDefault()) {
90 return InvocationHandler.invokeDefault(proxy, method, args);
92 throw new UnsupportedOperationException("Method " + method.toString() + " is not supported");
95 @NonNull CurrentAdapterSerializer currentSerializer() {
96 return adapterContext.currentSerializer();
99 @NonNull DOMRpcService delegate() {
103 @NonNull Rpc<?, ?> facade() {