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.Method;
13 import org.opendaylight.mdsal.dom.api.DOMRpcService;
14 import org.opendaylight.yangtools.yang.binding.Rpc;
15 import org.opendaylight.yangtools.yang.binding.RpcInput;
16 import org.opendaylight.yangtools.yang.binding.contract.Naming;
18 final class RpcAdapter<T extends Rpc<?, ?>> extends AbstractRpcAdapter {
19 private final RpcInvocationStrategy strategy;
20 private final Method invokeMethod;
22 RpcAdapter(final AdapterContext adapterContext, final DOMRpcService delegate, final Class<T> type) {
23 super(adapterContext, delegate, type);
25 final var serializer = adapterContext.currentSerializer();
26 final var rpcType = serializer.getRuntimeContext().getRpcDefinition(type);
27 if (rpcType == null) {
28 throw new IllegalStateException("Failed to find runtime type for " + type);
32 invokeMethod = type.getMethod(Naming.RPC_INVOKE_NAME, RpcInput.class);
33 } catch (NoSuchMethodException e) {
34 throw new IllegalStateException("Failed to find invoke method in " + type, e);
37 strategy = createStrategy(serializer, rpcType);
41 public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
42 return invokeMethod.equals(method) ? strategy.invoke((RpcInput) requireNonNull(args[0]))
43 : defaultInvoke(proxy, method, args);