Binding v2 - remove checked future
[mdsal.git] / binding2 / mdsal-binding2-dom-adapter / src / main / java / org / opendaylight / mdsal / binding / javav2 / dom / adapter / impl / operation / BindingDOMOperationImplementationAdapter.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;
9
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;
20 import java.util.Map;
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;
41
42 /**
43  * Operation implementation adapter.
44  */
45 @Beta
46 public class BindingDOMOperationImplementationAdapter implements DOMRpcImplementation {
47
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
51     // cost
52     private static final int COST = 1;
53
54     private final BindingNormalizedNodeCodecRegistry codec;
55     private final OperationServiceInvoker invoker;
56     private final Operation delegate;
57     private final QName inputQname;
58
59     <T extends Operation> BindingDOMOperationImplementationAdapter(final BindingNormalizedNodeCodecRegistry codec,
60             final Class<T> type, final Map<SchemaPath, Method> localNameToMethod, final T delegate) {
61         try {
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());
66                 }
67
68                 return OperationServiceInvoker.from(map);
69             });
70         } catch (final ExecutionException e) {
71             throw new IllegalArgumentException("Failed to create invokers for type " + type, e);
72         }
73
74         this.codec = Preconditions.checkNotNull(codec);
75         this.delegate = Preconditions.checkNotNull(delegate);
76         inputQname = QName.create(BindingReflections.getQNameModule(type), "input").intern();
77     }
78
79     @SuppressWarnings("deprecation")
80     @Nonnull
81     @Override
82     public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(@Nonnull final DOMRpcIdentifier rpc,
83             @Nullable final NormalizedNode<?, ?> input) {
84
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) {
90
91                     @Override
92                     protected DOMRpcException newWithCause(final String message, final Throwable cause) {
93                         return new DOMRpcInvokeException(message, cause);
94                     }
95                 });
96     }
97
98     private class DOMRpcInvokeException extends DOMRpcException {
99
100         private static final long serialVersionUID = 1L;
101
102         protected DOMRpcInvokeException(final String message, final Throwable cause) {
103             super(message, cause);
104         }
105     }
106
107     @Override
108     public long invocationCost() {
109         return COST;
110     }
111
112     private TreeNode deserialize(final SchemaPath rpcPath, final NormalizedNode<?, ?> input) {
113         if (input instanceof LazySerializedContainerNode) {
114             return ((LazySerializedContainerNode) input).bindingData();
115         }
116         final SchemaPath inputSchemaPath = rpcPath.createChild(inputQname);
117         return codec.fromNormalizedNodeOperationData(inputSchemaPath, (ContainerNode) input);
118     }
119
120     private ListenableFuture<RpcResult<?>> invoke(final SchemaPath schemaPath, final TreeNode input) {
121         return JdkFutureAdapters.listenInPoolThread(invoker.invoke(delegate, schemaPath.getLastComponent(), input));
122     }
123
124     private ListenableFuture<DOMRpcResult>
125             transformResult(final ListenableFuture<RpcResult<?>> bindingResult) {
126         return LazyDOMOperationResultFuture.create(codec, bindingResult);
127     }
128 }