2 * Copyright (c) 2013 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 java.lang.reflect.Method;
13 import java.util.concurrent.Future;
14 import javax.annotation.Nonnull;
15 import javax.annotation.Nullable;
16 import org.opendaylight.yangtools.yang.binding.DataObject;
17 import org.opendaylight.yangtools.yang.binding.RpcService;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.common.QNameModule;
20 import org.opendaylight.yangtools.yang.common.RpcResult;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
25 * Provides single method invocation of RPCs on supplied instance.
27 * Rpc Service invoker provides common invocation interface for any subtype of {@link RpcService}.
28 * via {@link #invokeRpc(RpcService, QName, DataObject)} method.
30 public abstract class RpcServiceInvoker {
31 private static final Logger LOG = LoggerFactory.getLogger(RpcServiceInvoker.class);
34 * Creates RPCServiceInvoker for specified RpcService type
36 * @param type RpcService interface, which was generated from model.
37 * @return Cached instance of {@link RpcServiceInvoker} for supplied RPC type.
40 public static RpcServiceInvoker from(final Class<? extends RpcService> type) {
41 return ClassBasedRpcServiceInvoker.instanceFor(type);
45 * Creates an RPCServiceInvoker for specified QName-<Method mapping.
47 * @param qnameToMethod translation mapping, must not be null nor empty.
48 * @return An {@link RpcMethodInvoker} instance.
50 public static RpcServiceInvoker from(final Map<QName, Method> qnameToMethod) {
51 Preconditions.checkArgument(!qnameToMethod.isEmpty());
52 QNameModule module = null;
54 for (QName qname : qnameToMethod.keySet()) {
56 if (!module.equals(qname.getModule())) {
57 LOG.debug("QNames from different modules {} and {}, falling back to QName map", module, qname.getModule());
58 return QNameRpcServiceInvoker.instanceFor(qnameToMethod);
61 module = qname.getModule();
65 // All module are equal, which means we can use localName only
66 return LocalNameRpcServiceInvoker.instanceFor(module, qnameToMethod);
70 * Invokes supplied RPC on provided implementation of RPC Service.
72 * @param impl Imlementation on which RPC should be invoked.
73 * @param rpcName Name of RPC to be invoked.
74 * @param input Input data for RPC.
75 * @return Future which will complete once rpc procesing is finished.
77 public abstract Future<RpcResult<?>> invokeRpc(@Nonnull final RpcService impl, @Nonnull final QName rpcName, @Nullable final DataObject input);