Binding v2 runtime - adapters - impl - operations invoker
[mdsal.git] / binding2 / mdsal-binding2-dom-adapter / src / main / java / org / opendaylight / mdsal / binding / javav2 / dom / adapter / impl / operation / invoker / OperationServiceInvoker.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.javav2.dom.adapter.impl.operation.invoker;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.Preconditions;
12 import java.lang.reflect.Method;
13 import java.util.Map;
14 import java.util.concurrent.Future;
15 import javax.annotation.Nonnull;
16 import javax.annotation.Nullable;
17 import org.opendaylight.mdsal.binding.javav2.spec.base.Instantiable;
18 import org.opendaylight.mdsal.binding.javav2.spec.base.Operation;
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 operations on supplied instance.
27  *
28  * <p>
29  * Operation Service invoker provides common invocation interface for any subtype of operation. via
30  * {@link #invoke(Operation, QName, Instantiable)} method.
31  */
32 @Beta
33 public abstract class OperationServiceInvoker {
34
35     private static final Logger LOG = LoggerFactory.getLogger(OperationServiceInvoker.class);
36
37     /**
38      * Creates OperationServiceInvoker for specified operation type.
39      *
40      * @param type
41      *            operation interface, which was generated from model.
42      * @return Cached instance of {@link OperationServiceInvoker} for supplied operation type.
43      *
44      */
45     public static OperationServiceInvoker from(final Class<? extends Operation> type) {
46         return ClassBasedOperationServiceInvoker.instanceFor(type);
47     }
48
49     /**
50      * Creates an OperationServiceInvoker for specified QName-&lt;Method mapping.
51      *
52      * @param qnameToMethod
53      *            translation mapping, must not be null nor empty.
54      * @return An {@link OperationMethodInvoker} instance.
55      */
56     public static OperationServiceInvoker from(final Map<QName, Method> qnameToMethod) {
57         Preconditions.checkArgument(!qnameToMethod.isEmpty());
58         QNameModule module = null;
59
60         for (final QName qname : qnameToMethod.keySet()) {
61             if (module != null) {
62                 if (!module.equals(qname.getModule())) {
63                     LOG.debug("QNames from different modules {} and {}, falling back to QName map", module,
64                             qname.getModule());
65                     return QNameOperationServiceInvoker.instanceFor(qnameToMethod);
66                 }
67             } else {
68                 module = qname.getModule();
69             }
70         }
71
72         // All module are equal, which means we can use localName only
73         return LocalNameOperationServiceInvoker.instanceFor(module, qnameToMethod);
74     }
75
76     /**
77      * Invokes supplied operation on provided implementation of Operation Service.
78      *
79      * @param <T>
80      *            - operation type
81      * @param impl
82      *            Implementation on which operation should be invoked.
83      * @param operationName
84      *            Name of operation to be invoked.
85      * @param input
86      *            Input data for operation.
87      * @return Future which will complete once operation processing is finished.
88      */
89     public abstract <T extends Operation> Future<RpcResult<?>> invoke(@Nonnull T impl, @Nonnull QName operationName,
90             @Nullable Instantiable<?> input);
91 }