Register multiple LegacyDOMRpcImplementationAdapters
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / main / java / org / opendaylight / mdsal / binding / dom / adapter / invoke / RpcMethodInvoker.java
1 /*
2  * Copyright (c) 2013 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.invoke;
9
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.base.Throwables;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.lang.invoke.MethodHandle;
14 import java.lang.invoke.MethodHandles;
15 import java.lang.invoke.MethodType;
16 import java.lang.reflect.Method;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.opendaylight.yangtools.yang.binding.DataObject;
19 import org.opendaylight.yangtools.yang.binding.RpcService;
20 import org.opendaylight.yangtools.yang.common.RpcResult;
21
22 @Deprecated(since = "11.0.0", forRemoval = true)
23 public final class RpcMethodInvoker {
24     private static final MethodType INVOCATION_SIGNATURE = MethodType.methodType(ListenableFuture.class,
25         RpcService.class, DataObject.class);
26
27     private final MethodHandle handle;
28
29     @VisibleForTesting
30     RpcMethodInvoker(final MethodHandle handle) {
31         this.handle = handle.asType(INVOCATION_SIGNATURE);
32     }
33
34     public static @NonNull RpcMethodInvoker from(final Method method) {
35         try {
36             return new RpcMethodInvoker(MethodHandles.publicLookup().unreflect(method));
37         } catch (IllegalAccessException e) {
38             throw new IllegalStateException("Lookup on public method failed.", e);
39         }
40     }
41
42     @SuppressWarnings("checkstyle:illegalCatch")
43     public @NonNull ListenableFuture<RpcResult<?>> invokeOn(final RpcService impl, final DataObject input) {
44         try {
45             return (ListenableFuture<RpcResult<?>>) handle.invokeExact(impl, input);
46         } catch (Throwable e) {
47             Throwables.throwIfUnchecked(e);
48             throw new IllegalStateException(e);
49         }
50     }
51 }