Bug 2364: Migrated Binding MD-SAL to not use composites nodes
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / md / sal / binding / impl / BindingDOMRpcImplementationAdapter.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 java.util.Collection;
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.DataContainer;
22 import org.opendaylight.yangtools.yang.binding.DataObject;
23 import org.opendaylight.yangtools.yang.binding.RpcService;
24 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
25 import org.opendaylight.yangtools.yang.binding.util.RpcServiceInvoker;
26 import org.opendaylight.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.common.QNameModule;
28 import org.opendaylight.yangtools.yang.common.RpcError;
29 import org.opendaylight.yangtools.yang.common.RpcResult;
30 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
32 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
33
34 public class BindingDOMRpcImplementationAdapter implements DOMRpcImplementation {
35
36     private static final Function<? super Exception, DOMRpcException> EXCEPTION_MAPPER = new Function<Exception, DOMRpcException>() {
37
38         @Override
39         public DOMRpcException apply(final Exception input) {
40             // FIXME: Return correct exception
41             return null;
42         }
43
44     };
45     private final BindingNormalizedNodeCodecRegistry codec;
46     private final RpcServiceInvoker invoker;
47     private final RpcService delegate;
48     private final QNameModule module;
49
50     public <T extends RpcService> BindingDOMRpcImplementationAdapter(final BindingNormalizedNodeCodecRegistry codec, final Class<T> type ,final T delegate) {
51         this.codec = codec;
52         this.delegate = delegate;
53         this.invoker = RpcServiceInvoker.from(type);
54         this.module = BindingReflections.getQNameModule(type);
55     }
56
57     public QNameModule getQNameModule() {
58         return module;
59     }
60
61     @Override
62     public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(final DOMRpcIdentifier rpc, final NormalizedNode<?, ?> input) {
63         final SchemaPath schemaPath = rpc.getType();
64         final DataObject bindingInput = deserilialize(rpc.getType(),input);
65         final ListenableFuture<RpcResult<?>> bindingResult = invoke(schemaPath,bindingInput);
66         return transformResult(schemaPath,bindingResult);
67     }
68
69     private DataObject deserilialize(final SchemaPath rpcPath, final NormalizedNode<?, ?> input) {
70         if(input instanceof LazySerializedContainerNode) {
71             return ((LazySerializedContainerNode) input).bindingData();
72         }
73         final SchemaPath inputSchemaPath = rpcPath.createChild(QName.create(module,"input"));
74         return codec.fromNormalizedNodeRpcData(inputSchemaPath, (ContainerNode) input);
75     }
76
77
78     private ListenableFuture<RpcResult<?>> invoke(final SchemaPath schemaPath, final DataObject input) {
79         return JdkFutureAdapters.listenInPoolThread(invoker.invokeRpc(delegate, schemaPath.getLastComponent(), input));
80     }
81
82     private CheckedFuture<DOMRpcResult, DOMRpcException> transformResult(final SchemaPath schemaPath,
83             final ListenableFuture<RpcResult<?>> bindingResult) {
84         final ListenableFuture<DOMRpcResult> transformed = Futures.transform(bindingResult, new Function<RpcResult<?>,DOMRpcResult>() {
85
86             @Override
87             public DOMRpcResult apply(final RpcResult<?> input) {
88                 return new DOMRpcResult() {
89
90                     @Override
91                     public NormalizedNode<?, ?> getResult() {
92
93                         if(input instanceof DataContainer) {
94                             return codec.toNormalizedNodeRpcData((DataContainer) input);
95                         }
96                         return null;
97                     }
98
99                     @Override
100                     public Collection<RpcError> getErrors() {
101                         return input.getErrors();
102                     }
103                 };
104             }
105
106         });
107         return Futures.makeChecked(transformed, EXCEPTION_MAPPER);
108     }
109
110
111
112 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.