2 * Copyright (c) 2015 Cisco Systems, Inc. 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.controller.md.sal.binding.impl;
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;
32 public class BindingRpcImplementationAdapter implements DOMRpcImplementation {
34 private static final Function<? super Exception, DOMRpcException> EXCEPTION_MAPPER = new Function<Exception, DOMRpcException>() {
37 public DOMRpcException apply(final Exception input) {
38 // FIXME: Return correct exception
43 private final BindingNormalizedNodeCodecRegistry codec;
44 private final RpcServiceInvoker invoker;
45 private final RpcService delegate;
46 private final QNameModule module;
48 private final Function<RpcResult<?>,DOMRpcResult> lazySerializedMapper = new Function<RpcResult<?>,DOMRpcResult>() {
51 public DOMRpcResult apply(final RpcResult<?> input) {
52 return LazySerializedDOMRpcResult.create(input, codec);
56 public <T extends RpcService> BindingRpcImplementationAdapter(final BindingNormalizedNodeCodecRegistry codec, final Class<T> type ,final T delegate) {
58 this.delegate = delegate;
59 invoker = RpcServiceInvoker.from(type);
60 module = BindingReflections.getQNameModule(type);
63 public QNameModule getQNameModule() {
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);
75 private DataObject deserilialize(final SchemaPath rpcPath, final NormalizedNode<?, ?> input) {
76 if(input instanceof LazySerializedContainerNode) {
77 return ((LazySerializedContainerNode) input).bindingData();
79 final SchemaPath inputSchemaPath = rpcPath.createChild(QName.create(module,"input"));
80 return codec.fromNormalizedNodeRpcData(inputSchemaPath, (ContainerNode) input);
84 private ListenableFuture<RpcResult<?>> invoke(final SchemaPath schemaPath, final DataObject input) {
85 return JdkFutureAdapters.listenInPoolThread(invoker.invokeRpc(delegate, schemaPath.getLastComponent(), input));
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);