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