624d8ffe9a9eb8b7fbc4ddce7bc17025b25dbba9
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / main / java / org / opendaylight / mdsal / binding / dom / adapter / RpcAdapter.java
1 /*
2  * Copyright (c) 2022 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.binding.dom.adapter;
9
10 import static java.util.Objects.requireNonNull;
11
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;
23
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 Method invokeMethod;
30     private final String name;
31
32     <T extends Rpc<?, ?>> RpcAdapter(final AdapterContext adapterContext, final DOMRpcService delegate,
33             final Class<T> type) {
34         this.adapterContext = requireNonNull(adapterContext);
35         this.delegate = requireNonNull(delegate);
36
37         final var serializer = adapterContext.currentSerializer();
38         final var rpcType = serializer.getRuntimeContext().getRpcDefinition(type);
39         if (rpcType == null) {
40             throw new IllegalStateException("Failed to find runtime type for " + type);
41         }
42
43         try {
44             invokeMethod = type.getMethod(Naming.RPC_INVOKE_NAME, RpcInput.class);
45         } catch (NoSuchMethodException e) {
46             throw new IllegalStateException("Failed to find invoke method in " + type, e);
47         }
48
49         facade = type.cast(Proxy.newProxyInstance(type.getClassLoader(), new Class<?>[] { type }, this));
50         name = type.getName();
51
52         strategy = createStrategy(serializer, rpcType);
53     }
54
55     private @NonNull RpcInvocationStrategy createStrategy(final CurrentAdapterSerializer serializer,
56             final RpcRuntimeType rpcType) {
57         final var rpc = rpcType.statement();
58         final var contentContext = ContentRoutedRpcContext.forRpc(rpc);
59         if (contentContext != null) {
60             final var extractor = serializer.findExtractor(rpcType.input());
61             if (extractor != null) {
62                 return new ContentRouted(this, rpc.argument(), contentContext.leaf(), extractor);
63             }
64         }
65         return new RpcInvocationStrategy(this, rpc.argument());
66     }
67
68     @Override
69     public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
70         if (invokeMethod.equals(method)) {
71             return strategy.invoke((RpcInput) requireNonNull(args[0]));
72         }
73
74         switch (method.getName()) {
75             case "toString":
76                 if (method.getReturnType().equals(String.class) && method.getParameterCount() == 0) {
77                     return name + "$Adapter{delegate=" + delegate + "}";
78                 }
79                 break;
80             case "hashCode":
81                 if (method.getReturnType().equals(int.class) && method.getParameterCount() == 0) {
82                     return System.identityHashCode(proxy);
83                 }
84                 break;
85             case "equals":
86                 if (method.getReturnType().equals(boolean.class) && method.getParameterCount() == 1
87                         && method.getParameterTypes()[0] == Object.class) {
88                     return proxy == args[0];
89                 }
90                 break;
91             default:
92                 break;
93         }
94
95         if (method.isDefault()) {
96             return InvocationHandler.invokeDefault(proxy, method, args);
97         }
98         throw new UnsupportedOperationException("Method " + method.toString() + " is not supported");
99     }
100
101     @NonNull CurrentAdapterSerializer currentSerializer() {
102         return adapterContext.currentSerializer();
103     }
104
105     @NonNull DOMRpcService delegate() {
106         return delegate;
107     }
108
109     @NonNull Rpc<?, ?> facade() {
110         return facade;
111     }
112 }