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

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