Deprecate old MD-SAL APIs for removal
[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 static java.util.Objects.requireNonNull;
11
12 import com.google.common.cache.Cache;
13 import com.google.common.cache.CacheBuilder;
14 import com.google.common.util.concurrent.CheckedFuture;
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 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.adapter.BindingDataAware;
26 import org.opendaylight.mdsal.binding.dom.adapter.invoke.RpcServiceInvoker;
27 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
28 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
29 import org.opendaylight.yangtools.yang.binding.DataObject;
30 import org.opendaylight.yangtools.yang.binding.RpcService;
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 @Deprecated(forRemoval = true)
38 public class BindingDOMRpcImplementationAdapter implements DOMRpcImplementation {
39
40     private static final Cache<Class<?>, RpcServiceInvoker> SERVICE_INVOKERS =
41             CacheBuilder.newBuilder().weakKeys().build();
42
43     private final BindingNormalizedNodeSerializer codec;
44     private final RpcServiceInvoker invoker;
45     private final RpcService delegate;
46     private final QName inputQname;
47
48     <T extends RpcService> BindingDOMRpcImplementationAdapter(final BindingNormalizedNodeSerializer codec,
49             final Class<T> type, final Map<SchemaPath, Method> localNameToMethod, final T delegate) {
50         try {
51             this.invoker = SERVICE_INVOKERS.get(type, () -> {
52                 final Map<QName, Method> map = new HashMap<>();
53                 for (Entry<SchemaPath, Method> e : localNameToMethod.entrySet()) {
54                     map.put(e.getKey().getLastComponent(), e.getValue());
55                 }
56
57                 return RpcServiceInvoker.from(map);
58             });
59         } catch (ExecutionException e) {
60             throw new IllegalArgumentException("Failed to create invokers for type " + type, e);
61         }
62
63         this.codec = requireNonNull(codec);
64         this.delegate = requireNonNull(delegate);
65         inputQname = QName.create(BindingReflections.getQNameModule(type), "input").intern();
66     }
67
68     @Override
69     public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(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 BindingDataAware) {
79             return ((BindingDataAware) 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 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.