bd1f68071e2c237e52273cbaf8502fe2452ad9dd
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / main / java / org / opendaylight / mdsal / binding / dom / adapter / RpcServiceAdapter.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. 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 com.google.common.collect.ImmutableMap;
13 import java.lang.reflect.InvocationHandler;
14 import java.lang.reflect.Method;
15 import java.lang.reflect.Proxy;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.mdsal.dom.api.DOMRpcService;
18 import org.opendaylight.yangtools.yang.binding.DataObject;
19 import org.opendaylight.yangtools.yang.binding.RpcService;
20
21 class RpcServiceAdapter implements InvocationHandler {
22     private final ImmutableMap<Method, RpcInvocationStrategy> rpcNames;
23     private final @NonNull Class<? extends RpcService> type;
24     private final @NonNull AdapterContext adapterContext;
25     private final @NonNull DOMRpcService delegate;
26     private final @NonNull RpcService facade;
27
28     RpcServiceAdapter(final Class<? extends RpcService> type, final AdapterContext adapterContext,
29             final DOMRpcService domService) {
30         this.type = requireNonNull(type);
31         this.adapterContext = requireNonNull(adapterContext);
32         delegate = requireNonNull(domService);
33
34         final var methods = adapterContext.currentSerializer().getRpcMethodToSchema(type);
35         final var rpcBuilder = ImmutableMap.<Method, RpcInvocationStrategy>builderWithExpectedSize(methods.size());
36         for (var entry : methods.entrySet()) {
37             final var method = entry.getKey();
38             rpcBuilder.put(method, RpcInvocationStrategy.of(this, method, entry.getValue()));
39         }
40         rpcNames = rpcBuilder.build();
41         facade = (RpcService) Proxy.newProxyInstance(type.getClassLoader(), new Class[] {type}, this);
42     }
43
44     final @NonNull CurrentAdapterSerializer currentSerializer() {
45         return adapterContext.currentSerializer();
46     }
47
48     final @NonNull DOMRpcService delegate() {
49         return delegate;
50     }
51
52     final @NonNull RpcService facade() {
53         return facade;
54     }
55
56     @Override
57     public Object invoke(final Object proxy, final Method method, final Object[] args) {
58         final var strategy = rpcNames.get(method);
59         if (strategy != null) {
60             if (args.length != 1) {
61                 throw new IllegalArgumentException("Input must be provided.");
62             }
63             return strategy.invoke((DataObject) requireNonNull(args[0]));
64         }
65
66         switch (method.getName()) {
67             case "toString":
68                 if (method.getReturnType().equals(String.class) && method.getParameterCount() == 0) {
69                     return type.getName() + "$Adapter{delegate=" + delegate.toString() + "}";
70                 }
71                 break;
72             case "hashCode":
73                 if (method.getReturnType().equals(int.class) && method.getParameterCount() == 0) {
74                     return System.identityHashCode(proxy);
75                 }
76                 break;
77             case "equals":
78                 if (method.getReturnType().equals(boolean.class) && method.getParameterCount() == 1
79                         && method.getParameterTypes()[0] == Object.class) {
80                     return proxy == args[0];
81                 }
82                 break;
83             default:
84                 break;
85         }
86
87         throw new UnsupportedOperationException("Method " + method.toString() + "is unsupported.");
88     }
89 }