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;
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.Preconditions;
12 import com.google.common.cache.Cache;
13 import com.google.common.cache.CacheBuilder;
14 import com.google.common.util.concurrent.CheckedFuture;
15 import com.google.common.util.concurrent.Futures;
16 import com.google.common.util.concurrent.JdkFutureAdapters;
17 import com.google.common.util.concurrent.ListenableFuture;
18 import java.lang.reflect.Method;
19 import java.util.HashMap;
21 import java.util.Map.Entry;
22 import java.util.concurrent.ExecutionException;
23 import javax.annotation.Nonnull;
24 import javax.annotation.Nullable;
25 import org.opendaylight.mdsal.binding.javav2.dom.adapter.impl.operation.invoker.OperationServiceInvoker;
26 import org.opendaylight.mdsal.binding.javav2.dom.codec.impl.BindingNormalizedNodeCodecRegistry;
27 import org.opendaylight.mdsal.binding.javav2.dom.codec.serialized.LazySerializedContainerNode;
28 import org.opendaylight.mdsal.binding.javav2.runtime.reflection.BindingReflections;
29 import org.opendaylight.mdsal.binding.javav2.spec.base.Operation;
30 import org.opendaylight.mdsal.binding.javav2.spec.base.TreeNode;
31 import org.opendaylight.mdsal.dom.api.DOMRpcException;
32 import org.opendaylight.mdsal.dom.api.DOMRpcIdentifier;
33 import org.opendaylight.mdsal.dom.api.DOMRpcImplementation;
34 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
35 import org.opendaylight.yangtools.util.concurrent.ExceptionMapper;
36 import org.opendaylight.yangtools.yang.common.QName;
37 import org.opendaylight.yangtools.yang.common.RpcResult;
38 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
39 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
40 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
43 * Operation implementation adapter.
46 public class BindingDOMOperationImplementationAdapter implements DOMRpcImplementation {
48 private static final Cache<Class<? extends Operation>, OperationServiceInvoker> SERVICE_INVOKERS =
49 CacheBuilder.newBuilder().weakKeys().build();
50 // Default implementations are 0, we need to perform some translation, hence we have a slightly higher
52 private static final int COST = 1;
54 private final BindingNormalizedNodeCodecRegistry codec;
55 private final OperationServiceInvoker invoker;
56 private final Operation delegate;
57 private final QName inputQname;
59 <T extends Operation> BindingDOMOperationImplementationAdapter(final BindingNormalizedNodeCodecRegistry codec,
60 final Class<T> type, final Map<SchemaPath, Method> localNameToMethod, final T delegate) {
62 this.invoker = SERVICE_INVOKERS.get(type, () -> {
63 final Map<QName, Method> map = new HashMap<>();
64 for (final Entry<SchemaPath, Method> e : localNameToMethod.entrySet()) {
65 map.put(e.getKey().getLastComponent(), e.getValue());
68 return OperationServiceInvoker.from(map);
70 } catch (final ExecutionException e) {
71 throw new IllegalArgumentException("Failed to create invokers for type " + type, e);
74 this.codec = Preconditions.checkNotNull(codec);
75 this.delegate = Preconditions.checkNotNull(delegate);
76 inputQname = QName.create(BindingReflections.getQNameModule(type), "input").intern();
79 @SuppressWarnings("deprecation")
82 public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(@Nonnull final DOMRpcIdentifier rpc,
83 @Nullable final NormalizedNode<?, ?> input) {
85 final SchemaPath schemaPath = rpc.getType();
86 final TreeNode bindingInput = input != null ? deserialize(rpc.getType(), input) : null;
87 final ListenableFuture<RpcResult<?>> bindingResult = invoke(schemaPath, bindingInput);
88 return Futures.makeChecked(transformResult(bindingResult),
89 new ExceptionMapper<DOMRpcException>("invokeRPc", DOMRpcException.class) {
92 protected DOMRpcException newWithCause(final String message, final Throwable cause) {
93 return new DOMRpcInvokeException(message, cause);
98 private class DOMRpcInvokeException extends DOMRpcException {
100 private static final long serialVersionUID = 1L;
102 protected DOMRpcInvokeException(final String message, final Throwable cause) {
103 super(message, cause);
108 public long invocationCost() {
112 private TreeNode deserialize(final SchemaPath rpcPath, final NormalizedNode<?, ?> input) {
113 if (input instanceof LazySerializedContainerNode) {
114 return ((LazySerializedContainerNode) input).bindingData();
116 final SchemaPath inputSchemaPath = rpcPath.createChild(inputQname);
117 return codec.fromNormalizedNodeOperationData(inputSchemaPath, (ContainerNode) input);
120 private ListenableFuture<RpcResult<?>> invoke(final SchemaPath schemaPath, final TreeNode input) {
121 return JdkFutureAdapters.listenInPoolThread(invoker.invoke(delegate, schemaPath.getLastComponent(), input));
124 private ListenableFuture<DOMRpcResult>
125 transformResult(final ListenableFuture<RpcResult<?>> bindingResult) {
126 return LazyDOMOperationResultFuture.create(codec, bindingResult);