2 * Copyright (c) 2017 Pantheon Technologies s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.binding.javav2.dom.adapter.impl.operation.invoker;
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;
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;
29 abstract class AbstractMappedOperationInvoker<T> extends OperationServiceInvoker {
31 private static final Logger LOG = LoggerFactory.getLogger(AbstractMappedOperationInvoker.class);
32 private final Map<T, OperationMethodInvoker> map;
34 protected AbstractMappedOperationInvoker(final Map<T, Method> map) {
35 final Builder<T, OperationMethodInvoker> b = ImmutableMap.builder();
37 for (final Entry<T, Method> e : map.entrySet()) {
38 if (BindingReflections.isOperationMethod(e.getValue())) {
39 b.put(e.getKey(), OperationMethodInvoker.from(e.getValue()));
41 LOG.debug("Method {} is not an operation method, ignoring it", e.getValue());
48 protected abstract T qnameToKey(QName qname);
51 public final <I extends Operation> Future<RpcResult<?>> invoke(@Nonnull final I impl,
52 @Nonnull final QName operationName, @Nullable final Instantiable<?> input) {
54 Preconditions.checkNotNull(impl, "Implementation must be supplied");
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);