0b8016dbffe66d207fc7d22f959983793065a974
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / main / java / org / opendaylight / mdsal / binding / dom / adapter / invoke / 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.mdsal.binding.dom.adapter.invoke;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.lang.reflect.Method;
14 import java.util.Map;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.opendaylight.yangtools.yang.binding.DataObject;
18 import org.opendaylight.yangtools.yang.binding.RpcService;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.common.QNameModule;
21 import org.opendaylight.yangtools.yang.common.RpcResult;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * Provides single method invocation of RPCs on supplied instance.
27  *
28  * <p>
29  * RPC Service invoker provides common invocation interface for any subtype of {@link RpcService} via
30  * {@link #invokeRpc(RpcService, QName, DataObject)} method.
31  */
32 public abstract class RpcServiceInvoker {
33     private static final Logger LOG = LoggerFactory.getLogger(RpcServiceInvoker.class);
34
35     /**
36      * Creates RPCServiceInvoker for specified RpcService type.
37      *
38      * @param type RpcService interface, which was generated from model.
39      * @return Cached instance of {@link RpcServiceInvoker} for supplied RPC type.
40      * @deprecated This method is not used by the adapter and is scheduled for removal.
41      */
42     @Deprecated(forRemoval = true, since = "9.0.3")
43     public static RpcServiceInvoker from(final Class<? extends RpcService> type) {
44         return ClassBasedRpcServiceInvoker.instanceFor(type);
45     }
46
47     /**
48      * Creates an RPCServiceInvoker for specified QName-&lt;Method mapping.
49      *
50      * @param qnameToMethod translation mapping, must not be null nor empty.
51      * @return An {@link RpcServiceInvoker} instance.
52      */
53     public static RpcServiceInvoker from(final Map<QName, Method> qnameToMethod) {
54         checkArgument(!qnameToMethod.isEmpty());
55         QNameModule module = null;
56
57         for (QName qname : qnameToMethod.keySet()) {
58             if (module != null) {
59                 if (!module.equals(qname.getModule())) {
60                     LOG.debug("QNames from different modules {} and {}, falling back to QName map", module,
61                         qname.getModule());
62                     return QNameRpcServiceInvoker.instanceFor(qnameToMethod);
63                 }
64             } else {
65                 module = qname.getModule();
66             }
67         }
68
69         // All module are equal, which means we can use localName only
70         return LocalNameRpcServiceInvoker.instanceFor(module, qnameToMethod);
71     }
72
73     /**
74      * Invokes supplied RPC on provided implementation of RPC Service.
75      *
76      * @param impl Imlementation on which RPC should be invoked.
77      * @param rpcName Name of RPC to be invoked.
78      * @param input Input data for RPC.
79      * @return Future which will complete once rpc procesing is finished.
80      */
81     public abstract ListenableFuture<RpcResult<?>> invokeRpc(@NonNull RpcService impl, @NonNull QName rpcName,
82             @Nullable DataObject input);
83 }