2 * Copyright (c) 2015 Cisco Systems, Inc. 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 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;
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;
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);
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()));
40 rpcNames = rpcBuilder.build();
41 facade = (RpcService) Proxy.newProxyInstance(type.getClassLoader(), new Class[] {type}, this);
44 final @NonNull CurrentAdapterSerializer currentSerializer() {
45 return adapterContext.currentSerializer();
48 final @NonNull DOMRpcService delegate() {
52 final @NonNull RpcService facade() {
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.");
63 return strategy.invoke((DataObject) requireNonNull(args[0]));
66 switch (method.getName()) {
68 if (method.getReturnType().equals(String.class) && method.getParameterCount() == 0) {
69 return type.getName() + "$Adapter{delegate=" + delegate.toString() + "}";
73 if (method.getReturnType().equals(int.class) && method.getParameterCount() == 0) {
74 return System.identityHashCode(proxy);
78 if (method.getReturnType().equals(boolean.class) && method.getParameterCount() == 1
79 && method.getParameterTypes()[0] == Object.class) {
80 return proxy == args[0];
87 throw new UnsupportedOperationException("Method " + method.toString() + "is unsupported.");