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 org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
16 import org.opendaylight.controller.md.sal.dom.api.DOMRpcIdentifier;
17 import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementation;
18 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
19 import org.opendaylight.yangtools.binding.data.codec.impl.BindingNormalizedNodeCodecRegistry;
20 import org.opendaylight.yangtools.yang.binding.DataObject;
21 import org.opendaylight.yangtools.yang.binding.RpcService;
22 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
23 import org.opendaylight.yangtools.yang.binding.util.RpcServiceInvoker;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.common.QNameModule;
26 import org.opendaylight.yangtools.yang.common.RpcResult;
27 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
29 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
31 public class BindingRpcImplementationAdapter implements DOMRpcImplementation {
33 private static final Function<? super Exception, DOMRpcException> EXCEPTION_MAPPER = new Function<Exception, DOMRpcException>() {
36 public DOMRpcException apply(Exception input) {
37 // FIXME: Return correct exception
42 private final BindingNormalizedNodeCodecRegistry codec;
43 private final RpcServiceInvoker invoker;
44 private final RpcService delegate;
45 private final QNameModule module;
47 private Function<RpcResult<?>,DOMRpcResult> lazySerializedMapper = new Function<RpcResult<?>,DOMRpcResult>() {
50 public DOMRpcResult apply(final RpcResult<?> input) {
51 return LazySerializedDOMRpcResult.create(input, codec);
55 public <T extends RpcService> BindingRpcImplementationAdapter(BindingNormalizedNodeCodecRegistry codec, Class<T> type ,T delegate) {
57 this.delegate = delegate;
58 this.invoker = RpcServiceInvoker.from(type);
59 this.module = BindingReflections.getQNameModule(type);
62 public QNameModule getQNameModule() {
67 public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(DOMRpcIdentifier rpc, NormalizedNode<?, ?> input) {
68 SchemaPath schemaPath = rpc.getType();
69 DataObject bindingInput = deserilialize(rpc.getType(),input);
70 ListenableFuture<RpcResult<?>> bindingResult = invoke(schemaPath,bindingInput);
71 return transformResult(schemaPath,bindingResult);
74 private DataObject deserilialize(SchemaPath rpcPath, NormalizedNode<?, ?> input) {
75 if(input instanceof LazySerializedContainerNode) {
76 return ((LazySerializedContainerNode) input).bindingData();
78 SchemaPath inputSchemaPath = rpcPath.createChild(QName.create(module,"input"));
79 return codec.fromNormalizedNodeRpcData(inputSchemaPath, (ContainerNode) input);
83 private ListenableFuture<RpcResult<?>> invoke(SchemaPath schemaPath, DataObject input) {
84 return JdkFutureAdapters.listenInPoolThread(invoker.invokeRpc(delegate, schemaPath.getLastComponent(), input));
87 private CheckedFuture<DOMRpcResult, DOMRpcException> transformResult(SchemaPath schemaPath,
88 ListenableFuture<RpcResult<?>> bindingResult) {
89 ListenableFuture<DOMRpcResult> transformed = Futures.transform(bindingResult, lazySerializedMapper);
90 return Futures.makeChecked(transformed, EXCEPTION_MAPPER);