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 / AbstractMappedOperationInvoker.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 com.google.common.collect.ImmutableMap;
13 import com.google.common.collect.ImmutableMap.Builder;
14 import java.lang.reflect.Method;
15 import java.util.Map;
16 import java.util.Map.Entry;
17 import java.util.concurrent.Future;
18 import javax.annotation.Nonnull;
19 import javax.annotation.Nullable;
20 import org.opendaylight.mdsal.binding.javav2.runtime.reflection.BindingReflections;
21 import org.opendaylight.mdsal.binding.javav2.spec.base.Instantiable;
22 import org.opendaylight.mdsal.binding.javav2.spec.base.Operation;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.common.RpcResult;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 @Beta
29 abstract class AbstractMappedOperationInvoker<T> extends OperationServiceInvoker {
30
31     private static final Logger LOG = LoggerFactory.getLogger(AbstractMappedOperationInvoker.class);
32     private final Map<T, OperationMethodInvoker> map;
33
34     protected AbstractMappedOperationInvoker(final Map<T, Method> map) {
35         final Builder<T, OperationMethodInvoker> b = ImmutableMap.builder();
36
37         for (final Entry<T, Method> e : map.entrySet()) {
38             if (BindingReflections.isOperationMethod(e.getValue())) {
39                 b.put(e.getKey(), OperationMethodInvoker.from(e.getValue()));
40             } else {
41                 LOG.debug("Method {} is not an operation method, ignoring it", e.getValue());
42             }
43         }
44
45         this.map = b.build();
46     }
47
48     protected abstract T qnameToKey(QName qname);
49
50     @Override
51     public final <I extends Operation> Future<RpcResult<?>> invoke(@Nonnull final I impl,
52             @Nonnull final QName operationName, @Nullable final Instantiable<?> input) {
53
54         Preconditions.checkNotNull(impl, "Implementation must be supplied");
55
56         final OperationMethodInvoker invoker = map.get(qnameToKey(operationName));
57         Preconditions.checkArgument(invoker != null, "Supplied operation is not valid for implementation %s", impl);
58         return invoker.invokeOn(impl, input);
59     }
60 }