Adjust for Binding RPC codegen changes
[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.Preconditions;
11 import com.google.common.cache.Cache;
12 import com.google.common.cache.CacheBuilder;
13 import com.google.common.util.concurrent.CheckedFuture;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import java.lang.reflect.Method;
16 import java.util.HashMap;
17 import java.util.Map;
18 import java.util.Map.Entry;
19 import java.util.concurrent.ExecutionException;
20 import javax.annotation.Nonnull;
21 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
22 import org.opendaylight.controller.md.sal.dom.api.DOMRpcIdentifier;
23 import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementation;
24 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
25 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
26 import org.opendaylight.yangtools.yang.binding.DataObject;
27 import org.opendaylight.yangtools.yang.binding.RpcService;
28 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
29 import org.opendaylight.yangtools.yang.binding.util.RpcServiceInvoker;
30 import org.opendaylight.yangtools.yang.common.QName;
31 import org.opendaylight.yangtools.yang.common.RpcResult;
32 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
34 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
35
36 public class BindingDOMRpcImplementationAdapter implements DOMRpcImplementation {
37
38     private static final Cache<Class<?>, RpcServiceInvoker> SERVICE_INVOKERS =
39             CacheBuilder.newBuilder().weakKeys().build();
40
41     private final BindingNormalizedNodeSerializer codec;
42     private final RpcServiceInvoker invoker;
43     private final RpcService delegate;
44     private final QName inputQname;
45
46     <T extends RpcService> BindingDOMRpcImplementationAdapter(final BindingNormalizedNodeSerializer codec,
47             final Class<T> type, final Map<SchemaPath, Method> localNameToMethod, final T delegate) {
48         try {
49             this.invoker = SERVICE_INVOKERS.get(type, () -> {
50                 final Map<QName, Method> map = new HashMap<>();
51                 for (Entry<SchemaPath, Method> e : localNameToMethod.entrySet()) {
52                     map.put(e.getKey().getLastComponent(), e.getValue());
53                 }
54
55                 return RpcServiceInvoker.from(map);
56             });
57         } catch (ExecutionException e) {
58             throw new IllegalArgumentException("Failed to create invokers for type " + type, e);
59         }
60
61         this.codec = Preconditions.checkNotNull(codec);
62         this.delegate = Preconditions.checkNotNull(delegate);
63         inputQname = QName.create(BindingReflections.getQNameModule(type), "input").intern();
64     }
65
66     @Nonnull
67     @Override
68     public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(@Nonnull final DOMRpcIdentifier rpc,
69             final NormalizedNode<?, ?> input) {
70         final SchemaPath schemaPath = rpc.getType();
71         final DataObject bindingInput = input != null ? deserialize(rpc.getType(), input) : null;
72         final ListenableFuture<RpcResult<?>> bindingResult = invoke(schemaPath, bindingInput);
73         return transformResult(bindingResult);
74     }
75
76     private DataObject deserialize(final SchemaPath rpcPath, final NormalizedNode<?, ?> input) {
77         if (input instanceof LazySerializedContainerNode) {
78             return ((LazySerializedContainerNode) input).bindingData();
79         }
80         final SchemaPath inputSchemaPath = rpcPath.createChild(inputQname);
81         return codec.fromNormalizedNodeRpcData(inputSchemaPath, (ContainerNode) input);
82     }
83
84     private ListenableFuture<RpcResult<?>> invoke(final SchemaPath schemaPath, final DataObject input) {
85         return invoker.invokeRpc(delegate, schemaPath.getLastComponent(), input);
86     }
87
88     private CheckedFuture<DOMRpcResult, DOMRpcException> transformResult(
89             final ListenableFuture<RpcResult<?>> bindingResult) {
90         return LazyDOMRpcResultFuture.create(codec, bindingResult);
91     }
92
93 }

©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.