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 static java.util.Objects.requireNonNull;
12 import com.google.common.collect.ImmutableMap;
13 import com.google.common.util.concurrent.CheckedFuture;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import com.google.common.util.concurrent.MoreExecutors;
17 import java.lang.reflect.InvocationHandler;
18 import java.lang.reflect.Method;
19 import java.lang.reflect.Proxy;
20 import java.util.Collection;
21 import java.util.Map.Entry;
22 import java.util.concurrent.Future;
23 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
24 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
25 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
26 import org.opendaylight.controller.md.sal.dom.broker.spi.rpc.RpcRoutingStrategy;
27 import org.opendaylight.controller.sal.core.compat.LegacyDOMRpcResultFutureAdapter;
28 import org.opendaylight.mdsal.binding.dom.adapter.BindingRpcFutureAware;
29 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
30 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
31 import org.opendaylight.yangtools.yang.binding.DataContainer;
32 import org.opendaylight.yangtools.yang.binding.DataObject;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.opendaylight.yangtools.yang.binding.RpcService;
35 import org.opendaylight.yangtools.yang.common.QName;
36 import org.opendaylight.yangtools.yang.common.RpcError;
37 import org.opendaylight.yangtools.yang.common.RpcError.ErrorSeverity;
38 import org.opendaylight.yangtools.yang.common.RpcResult;
39 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
40 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
41 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
42 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
43 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
44 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
45 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
46 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
47 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
49 @Deprecated(forRemoval = true)
50 class RpcServiceAdapter implements InvocationHandler {
52 private final ImmutableMap<Method, RpcInvocationStrategy> rpcNames;
53 private final Class<? extends RpcService> type;
54 private final BindingToNormalizedNodeCodec codec;
55 private final DOMRpcService delegate;
56 private final RpcService proxy;
58 RpcServiceAdapter(final Class<? extends RpcService> type, final BindingToNormalizedNodeCodec codec,
59 final DOMRpcService domService) {
60 this.type = requireNonNull(type);
61 this.codec = requireNonNull(codec);
62 this.delegate = requireNonNull(domService);
63 final ImmutableMap.Builder<Method, RpcInvocationStrategy> rpcBuilder = ImmutableMap.builder();
64 for (final Entry<Method, RpcDefinition> rpc : codec.getRpcMethodToSchema(type).entrySet()) {
65 rpcBuilder.put(rpc.getKey(), createStrategy(rpc.getKey(), rpc.getValue()));
67 rpcNames = rpcBuilder.build();
68 proxy = (RpcService) Proxy.newProxyInstance(type.getClassLoader(), new Class[] {type}, this);
71 ListenableFuture<RpcResult<?>> invoke0(final SchemaPath schemaPath, final NormalizedNode<?, ?> input) {
72 final CheckedFuture<DOMRpcResult, DOMRpcException> result = delegate.invokeRpc(schemaPath, input);
73 if (result instanceof BindingRpcFutureAware) {
74 return ((BindingRpcFutureAware) result).getBindingFuture();
75 } else if (result instanceof LegacyDOMRpcResultFutureAdapter) {
76 Future<org.opendaylight.mdsal.dom.api.DOMRpcResult> delegateFuture =
77 ((LegacyDOMRpcResultFutureAdapter)result).delegate();
78 if (delegateFuture instanceof BindingRpcFutureAware) {
79 return ((BindingRpcFutureAware) delegateFuture).getBindingFuture();
83 return transformFuture(schemaPath, result, codec.getCodecFactory());
86 private RpcInvocationStrategy createStrategy(final Method method, final RpcDefinition schema) {
87 final RpcRoutingStrategy strategy = RpcRoutingStrategy.from(schema);
88 if (strategy.isContextBasedRouted()) {
89 return new RoutedStrategy(schema.getPath(), method, strategy.getLeaf());
91 return new NonRoutedStrategy(schema.getPath());
94 RpcService getProxy() {
99 public Object invoke(final Object proxyObj, final Method method, final Object[] args) {
101 final RpcInvocationStrategy rpc = rpcNames.get(method);
103 if (method.getParameterCount() == 0) {
104 return rpc.invokeEmpty();
106 if (args.length != 1) {
107 throw new IllegalArgumentException("Input must be provided.");
109 return rpc.invoke((DataObject) args[0]);
112 if (isObjectMethod(method)) {
113 return callObjectMethod(proxyObj, method, args);
115 throw new UnsupportedOperationException("Method " + method.toString() + "is unsupported.");
118 private static boolean isObjectMethod(final Method method) {
119 switch (method.getName()) {
121 return method.getReturnType().equals(String.class) && method.getParameterCount() == 0;
123 return method.getReturnType().equals(int.class) && method.getParameterCount() == 0;
125 return method.getReturnType().equals(boolean.class) && method.getParameterCount() == 1 && method
126 .getParameterTypes()[0] == Object.class;
132 private Object callObjectMethod(final Object self, final Method method, final Object[] args) {
133 switch (method.getName()) {
135 return type.getName() + "$Adapter{delegate=" + delegate.toString() + "}";
137 return System.identityHashCode(self);
139 return self == args[0];
145 private static ListenableFuture<RpcResult<?>> transformFuture(final SchemaPath rpc,
146 final ListenableFuture<DOMRpcResult> domFuture, final BindingNormalizedNodeSerializer codec) {
147 return Futures.transform(domFuture, input -> {
148 final NormalizedNode<?, ?> domData = input.getResult();
149 final DataObject bindingResult;
150 if (domData != null) {
151 final SchemaPath rpcOutput = rpc.createChild(QName.create(rpc.getLastComponent(), "output"));
152 bindingResult = codec.fromNormalizedNodeRpcData(rpcOutput, (ContainerNode) domData);
154 bindingResult = null;
157 // DOMRpcResult does not have a notion of success, hence we have to reverse-engineer it by looking
158 // at reported errors and checking whether they are just warnings.
159 final Collection<? extends RpcError> errors = input.getErrors();
160 return RpcResult.class.cast(RpcResultBuilder.status(errors.stream()
161 .noneMatch(error -> error.getSeverity() == ErrorSeverity.ERROR))
162 .withResult(bindingResult).withRpcErrors(errors).build());
163 }, MoreExecutors.directExecutor());
166 private abstract class RpcInvocationStrategy {
168 private final SchemaPath rpcName;
170 protected RpcInvocationStrategy(final SchemaPath path) {
174 final ListenableFuture<RpcResult<?>> invoke(final DataObject input) {
175 return invoke0(rpcName, serialize(input));
178 abstract NormalizedNode<?, ?> serialize(DataObject input);
180 final ListenableFuture<RpcResult<?>> invokeEmpty() {
181 return invoke0(rpcName, null);
184 final SchemaPath getRpcName() {
189 private final class NonRoutedStrategy extends RpcInvocationStrategy {
191 protected NonRoutedStrategy(final SchemaPath path) {
196 NormalizedNode<?, ?> serialize(final DataObject input) {
197 return LazySerializedContainerNode.create(getRpcName(), input, codec.getCodecRegistry());
201 private final class RoutedStrategy extends RpcInvocationStrategy {
203 private final ContextReferenceExtractor refExtractor;
204 private final NodeIdentifier contextName;
206 protected RoutedStrategy(final SchemaPath path, final Method rpcMethod, final QName leafName) {
208 final Class<? extends DataContainer> inputType = BindingReflections.resolveRpcInputClass(rpcMethod).get();
209 refExtractor = ContextReferenceExtractor.from(inputType);
210 this.contextName = new NodeIdentifier(leafName);
214 NormalizedNode<?, ?> serialize(final DataObject input) {
215 final InstanceIdentifier<?> bindingII = refExtractor.extract(input);
216 if (bindingII != null) {
217 final YangInstanceIdentifier yangII = codec.toYangInstanceIdentifierCached(bindingII);
218 final LeafNode<?> contextRef = ImmutableNodes.leafNode(contextName, yangII);
219 return LazySerializedContainerNode.withContextRef(getRpcName(), input, contextRef,
220 codec.getCodecRegistry());
222 return LazySerializedContainerNode.create(getRpcName(), input, codec.getCodecRegistry());