Merge "Reduce/enhance logging in AbstractLeader"
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / md / sal / binding / impl / BindingRpcImplementationAdapter.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. 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.controller.md.sal.binding.impl;
9
10 import com.google.common.base.Function;
11 import com.google.common.util.concurrent.CheckedFuture;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.JdkFutureAdapters;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import javax.annotation.Nullable;
16 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
17 import org.opendaylight.controller.md.sal.dom.api.DOMRpcIdentifier;
18 import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementation;
19 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
20 import org.opendaylight.yangtools.binding.data.codec.impl.BindingNormalizedNodeCodecRegistry;
21 import org.opendaylight.yangtools.yang.binding.DataObject;
22 import org.opendaylight.yangtools.yang.binding.RpcService;
23 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
24 import org.opendaylight.yangtools.yang.binding.util.RpcServiceInvoker;
25 import org.opendaylight.yangtools.yang.common.QName;
26 import org.opendaylight.yangtools.yang.common.QNameModule;
27 import org.opendaylight.yangtools.yang.common.RpcResult;
28 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
30 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
31
32 public class BindingRpcImplementationAdapter implements DOMRpcImplementation {
33
34     private static final Function<? super Exception, DOMRpcException> EXCEPTION_MAPPER = new Function<Exception, DOMRpcException>() {
35
36         @Override
37         public DOMRpcException apply(final Exception input) {
38             // FIXME: Return correct exception
39             return null;
40         }
41
42     };
43     private final BindingNormalizedNodeCodecRegistry codec;
44     private final RpcServiceInvoker invoker;
45     private final RpcService delegate;
46     private final QNameModule module;
47
48     private final Function<RpcResult<?>,DOMRpcResult> lazySerializedMapper = new Function<RpcResult<?>,DOMRpcResult>() {
49
50         @Override
51         public DOMRpcResult apply(final RpcResult<?> input) {
52             return LazySerializedDOMRpcResult.create(input, codec);
53         }
54     };
55
56     public <T extends RpcService> BindingRpcImplementationAdapter(final BindingNormalizedNodeCodecRegistry codec, final Class<T> type ,final T delegate) {
57         this.codec = codec;
58         this.delegate = delegate;
59         invoker = RpcServiceInvoker.from(type);
60         module = BindingReflections.getQNameModule(type);
61     }
62
63     public QNameModule getQNameModule() {
64         return module;
65     }
66
67     @Override
68     public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(final DOMRpcIdentifier rpc, @Nullable final NormalizedNode<?, ?> input) {
69         final SchemaPath schemaPath = rpc.getType();
70         final DataObject bindingInput = input != null ? deserilialize(rpc.getType(),input) : null;
71         final ListenableFuture<RpcResult<?>> bindingResult = invoke(schemaPath, bindingInput);
72         return transformResult(schemaPath,bindingResult);
73     }
74
75     private DataObject deserilialize(final SchemaPath rpcPath, final NormalizedNode<?, ?> input) {
76         if(input instanceof LazySerializedContainerNode) {
77             return ((LazySerializedContainerNode) input).bindingData();
78         }
79         final SchemaPath inputSchemaPath = rpcPath.createChild(QName.create(module,"input"));
80         return codec.fromNormalizedNodeRpcData(inputSchemaPath, (ContainerNode) input);
81     }
82
83
84     private ListenableFuture<RpcResult<?>> invoke(final SchemaPath schemaPath, final DataObject input) {
85         return JdkFutureAdapters.listenInPoolThread(invoker.invokeRpc(delegate, schemaPath.getLastComponent(), input));
86     }
87
88     private CheckedFuture<DOMRpcResult, DOMRpcException> transformResult(final SchemaPath schemaPath,
89             final ListenableFuture<RpcResult<?>> bindingResult) {
90         final ListenableFuture<DOMRpcResult> transformed = Futures.transform(bindingResult, lazySerializedMapper);
91         return Futures.makeChecked(transformed, EXCEPTION_MAPPER);
92     }
93
94 }