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.
28 * RPC Service invoker provides common invocation interface for any subtype of {@link RpcService}.
29 * via {@link #invokeRpc(RpcService, QName, DataObject)} method.
31 public abstract class RpcServiceInvoker {
32 private static final Logger LOG = LoggerFactory.getLogger(RpcServiceInvoker.class);
35 * Creates RPCServiceInvoker for specified RpcService type.
37 * @param type RpcService interface, which was generated from model.
38 * @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,
59 return QNameRpcServiceInvoker.instanceFor(qnameToMethod);
62 module = qname.getModule();
66 // All module are equal, which means we can use localName only
67 return LocalNameRpcServiceInvoker.instanceFor(module, qnameToMethod);
71 * Invokes supplied RPC on provided implementation of RPC Service.
73 * @param impl Imlementation on which RPC should be invoked.
74 * @param rpcName Name of RPC to be invoked.
75 * @param input Input data for RPC.
76 * @return Future which will complete once rpc procesing is finished.
78 public abstract Future<RpcResult<?>> invokeRpc(@Nonnull RpcService impl, @Nonnull QName rpcName,
79 @Nullable DataObject input);