Unify RPC input serialization
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / main / java / org / opendaylight / mdsal / binding / dom / adapter / invoke / AbstractMappedRpcInvoker.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.invoke;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.annotations.VisibleForTesting;
14 import com.google.common.collect.ImmutableMap;
15 import com.google.common.collect.ImmutableMap.Builder;
16 import com.google.common.util.concurrent.ListenableFuture;
17 import java.lang.reflect.Method;
18 import java.util.Map;
19 import java.util.Map.Entry;
20 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
21 import org.opendaylight.yangtools.yang.binding.DataObject;
22 import org.opendaylight.yangtools.yang.binding.RpcService;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.common.RpcResult;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 @Deprecated(since = "11.0.0", forRemoval = true)
29 abstract class AbstractMappedRpcInvoker<T> extends RpcServiceInvoker {
30     private static final Logger LOG = LoggerFactory.getLogger(AbstractMappedRpcInvoker.class);
31
32     @VisibleForTesting
33     final Map<T, RpcMethodInvoker> map;
34
35     protected AbstractMappedRpcInvoker(final Map<T, Method> map) {
36         final Builder<T, RpcMethodInvoker> b = ImmutableMap.builder();
37
38         for (Entry<T, Method> e : map.entrySet()) {
39             if (BindingReflections.isRpcMethod(e.getValue())) {
40                 b.put(e.getKey(), RpcMethodInvoker.from(e.getValue()));
41             } else {
42                 LOG.debug("Method {} is not an RPC method, ignoring it", e.getValue());
43             }
44         }
45
46         this.map = b.build();
47     }
48
49     protected abstract T qnameToKey(QName qname);
50
51     @Override
52     public final ListenableFuture<RpcResult<?>> invokeRpc(final RpcService impl, final QName rpcName,
53             final DataObject input) {
54         requireNonNull(impl, "Implementation must be supplied");
55
56         RpcMethodInvoker invoker = map.get(qnameToKey(rpcName));
57         checkArgument(invoker != null, "Supplied RPC is not valid for implementation %s", impl);
58         return invoker.invokeOn(impl, input);
59     }
60 }