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.yangtools.yang.binding.util;
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;
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;
26 abstract class AbstractMappedRpcInvoker<T> extends RpcServiceInvoker {
27 private static final Logger LOG = LoggerFactory.getLogger(AbstractMappedRpcInvoker.class);
28 private final Map<T, RpcMethodInvoker> map;
30 protected AbstractMappedRpcInvoker(final Map<T, Method> map) {
31 final Builder<T, RpcMethodInvoker> b = ImmutableMap.builder();
33 for (Entry<T, Method> e : map.entrySet()) {
34 if (BindingReflections.isRpcMethod(e.getValue())) {
35 b.put(e.getKey(), RpcMethodInvoker.from(e.getValue()));
37 LOG.debug("Method {} is not an RPC method, ignoring it", e.getValue());
44 protected abstract T qnameToKey(final QName qname);
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");
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);