Override Rpc/Action invoke method
[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 String name;
30
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);
35
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);
40         }
41
42         facade = type.cast(Proxy.newProxyInstance(type.getClassLoader(), new Class<?>[] { type }, this));
43         name = type.getName();
44
45         strategy = createStrategy(serializer, rpcType);
46     }
47
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);
56             }
57         }
58         return new RpcInvocationStrategy(this, rpc.argument());
59     }
60
61     @Override
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]));
67                 }
68                 break;
69             case "toString":
70                 if (method.getReturnType().equals(String.class) && method.getParameterCount() == 0) {
71                     return name + "$Adapter{delegate=" + delegate + "}";
72                 }
73                 break;
74             case "hashCode":
75                 if (method.getReturnType().equals(int.class) && method.getParameterCount() == 0) {
76                     return System.identityHashCode(proxy);
77                 }
78                 break;
79             case "equals":
80                 if (method.getReturnType().equals(boolean.class) && method.getParameterCount() == 1
81                         && method.getParameterTypes()[0] == Object.class) {
82                     return proxy == args[0];
83                 }
84                 break;
85             default:
86                 break;
87         }
88
89         if (method.isDefault()) {
90             return InvocationHandler.invokeDefault(proxy, method, args);
91         }
92         throw new UnsupportedOperationException("Method " + method.toString() + " is not supported");
93     }
94
95     @NonNull CurrentAdapterSerializer currentSerializer() {
96         return adapterContext.currentSerializer();
97     }
98
99     @NonNull DOMRpcService delegate() {
100         return delegate;
101     }
102
103     @NonNull Rpc<?, ?> facade() {
104         return facade;
105     }
106 }