Fix checkstyle violations in yang-binding
[mdsal.git] / binding / 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  * <p>
28  * RPC Service invoker provides common invocation interface for any subtype of {@link RpcService}.
29  * via {@link #invokeRpc(RpcService, QName, DataObject)} method.
30  */
31 public abstract class RpcServiceInvoker {
32     private static final Logger LOG = LoggerFactory.getLogger(RpcServiceInvoker.class);
33
34     /**
35      * Creates RPCServiceInvoker for specified RpcService type.
36      *
37      * @param type RpcService interface, which was generated from model.
38      * @return Cached instance of {@link RpcServiceInvoker} for supplied RPC type.
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,
58                         qname.getModule());
59                     return QNameRpcServiceInvoker.instanceFor(qnameToMethod);
60                 }
61             } else {
62                 module = qname.getModule();
63             }
64         }
65
66         // All module are equal, which means we can use localName only
67         return LocalNameRpcServiceInvoker.instanceFor(module, qnameToMethod);
68     }
69
70     /**
71      * Invokes supplied RPC on provided implementation of RPC Service.
72      *
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.
77      */
78     public abstract Future<RpcResult<?>> invokeRpc(@Nonnull RpcService impl, @Nonnull QName rpcName,
79             @Nullable DataObject input);
80 }