Introduce top-level pom file.
[mdsal.git] / yang / yang-binding / src / main / java / org / opendaylight / yangtools / yang / binding / util / RpcServiceInvoker.java
1 /*
2  * Copyright (c) 2013 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 java.lang.reflect.Method;
12 import java.util.Map;
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;
23
24 /**
25  * Provides single method invocation of RPCs on supplied instance.
26  *
27  * Rpc Service invoker provides common invocation interface for any subtype of {@link RpcService}.
28  * via {@link #invokeRpc(RpcService, QName, DataObject)} method.
29  */
30 public abstract class RpcServiceInvoker {
31     private static final Logger LOG = LoggerFactory.getLogger(RpcServiceInvoker.class);
32
33     /**
34      * Creates RPCServiceInvoker for specified RpcService type
35      *
36      * @param type RpcService interface, which was generated from model.
37      * @return Cached instance of {@link RpcServiceInvoker} for supplied RPC type.
38      *
39      */
40     public static RpcServiceInvoker from(final Class<? extends RpcService> type) {
41         return ClassBasedRpcServiceInvoker.instanceFor(type);
42     }
43
44     /**
45      * Creates an RPCServiceInvoker for specified QName-&lt;Method mapping.
46      *
47      * @param qnameToMethod translation mapping, must not be null nor empty.
48      * @return An {@link RpcMethodInvoker} instance.
49      */
50     public static RpcServiceInvoker from(final Map<QName, Method> qnameToMethod) {
51         Preconditions.checkArgument(!qnameToMethod.isEmpty());
52         QNameModule module = null;
53
54         for (QName qname : qnameToMethod.keySet()) {
55             if (module != null) {
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);
59                 }
60             } else {
61                 module = qname.getModule();
62             }
63         }
64
65         // All module are equal, which means we can use localName only
66         return LocalNameRpcServiceInvoker.instanceFor(module, qnameToMethod);
67     }
68
69     /**
70      * Invokes supplied RPC on provided implementation of RPC Service.
71      *
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.
76      */
77     public abstract Future<RpcResult<?>> invokeRpc(@Nonnull final RpcService impl, @Nonnull final QName rpcName, @Nullable final DataObject input);
78 }