2 * Copyright (c) 2018 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.dom.adapter;
10 import static java.util.Objects.requireNonNull;
11 import static org.opendaylight.mdsal.binding.dom.adapter.StaticConfiguration.ENABLE_CODEC_SHORTCUT;
12 import static org.opendaylight.yangtools.yang.common.YangConstants.operationInputQName;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import com.google.common.util.concurrent.MoreExecutors;
17 import java.lang.reflect.InvocationHandler;
18 import java.lang.reflect.Method;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.opendaylight.mdsal.binding.api.ActionSpec;
21 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
22 import org.opendaylight.mdsal.dom.api.DOMActionResult;
23 import org.opendaylight.mdsal.dom.api.DOMActionService;
24 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 import org.opendaylight.yangtools.yang.binding.RpcInput;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
28 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
30 final class ActionAdapter extends AbstractBindingAdapter<DOMActionService> implements InvocationHandler {
31 private final @NonNull ActionSpec<?, ?> spec;
32 private final @NonNull NodeIdentifier inputName;
33 private final @NonNull Absolute actionPath;
35 ActionAdapter(final AdapterContext codec, final DOMActionService delegate, final ActionSpec<?, ?> spec) {
36 super(codec, delegate);
37 this.spec = requireNonNull(spec);
38 actionPath = currentSerializer().getActionPath(spec);
39 inputName = NodeIdentifier.create(operationInputQName(actionPath.lastNodeIdentifier().getModule()));
43 public Object invoke(final Object proxy, final Method method, final Object [] args) throws NoSuchMethodError {
44 switch (method.getName()) {
46 if (args.length == 1) {
47 return proxy == args[0];
51 if (args.length == 0) {
52 return System.identityHashCode(proxy);
56 if (args.length == 0) {
57 return spec.type().getName() + "$Adapter{delegate=" + getDelegate() + "}";
61 if (args.length == 2) {
62 final InstanceIdentifier<?> path = (InstanceIdentifier<?>) requireNonNull(args[0]);
63 final RpcInput input = (RpcInput) requireNonNull(args[1]);
64 final CurrentAdapterSerializer serializer = currentSerializer();
65 final ListenableFuture<? extends DOMActionResult> future = getDelegate().invokeAction(actionPath,
66 new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL,
67 serializer.toYangInstanceIdentifier(path)),
68 serializer.toLazyNormalizedNodeActionInput(spec.type(), inputName, input));
70 // Invocation returned a future we know about -- return that future instead
71 if (ENABLE_CODEC_SHORTCUT && future instanceof BindingRpcFutureAware) {
72 return ((BindingRpcFutureAware) future).getBindingFuture();
75 return Futures.transform(future,
76 dom -> RpcResultUtil.rpcResultFromDOM(dom.getErrors(), dom.getOutput()
77 .map(output -> serializer.fromNormalizedNodeActionOutput(spec.type(), output))
79 MoreExecutors.directExecutor());
86 throw new NoSuchMethodError("Method " + method.toString() + "is unsupported.");