* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
-
package org.opendaylight.controller.md.sal.binding.impl;
-import com.google.common.base.Function;
-import com.google.common.base.Preconditions;
+import static java.util.Objects.requireNonNull;
+
import com.google.common.collect.ImmutableMap;
import com.google.common.util.concurrent.CheckedFuture;
import com.google.common.util.concurrent.Futures;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
+import java.util.Collection;
import java.util.Map.Entry;
+import java.util.concurrent.Future;
import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
import org.opendaylight.controller.md.sal.dom.broker.spi.rpc.RpcRoutingStrategy;
+import org.opendaylight.controller.sal.core.compat.LegacyDOMRpcResultFutureAdapter;
+import org.opendaylight.mdsal.binding.dom.adapter.BindingRpcFutureAware;
import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
+import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
import org.opendaylight.yangtools.yang.binding.DataContainer;
import org.opendaylight.yangtools.yang.binding.DataObject;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
import org.opendaylight.yangtools.yang.binding.RpcService;
-import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.common.RpcError;
+import org.opendaylight.yangtools.yang.common.RpcError.ErrorSeverity;
import org.opendaylight.yangtools.yang.common.RpcResult;
import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+@Deprecated
class RpcServiceAdapter implements InvocationHandler {
private final ImmutableMap<Method, RpcInvocationStrategy> rpcNames;
RpcServiceAdapter(final Class<? extends RpcService> type, final BindingToNormalizedNodeCodec codec,
final DOMRpcService domService) {
- this.type = Preconditions.checkNotNull(type);
- this.codec = Preconditions.checkNotNull(codec);
- this.delegate = Preconditions.checkNotNull(domService);
+ this.type = requireNonNull(type);
+ this.codec = requireNonNull(codec);
+ this.delegate = requireNonNull(domService);
final ImmutableMap.Builder<Method, RpcInvocationStrategy> rpcBuilder = ImmutableMap.builder();
for (final Entry<Method, RpcDefinition> rpc : codec.getRpcMethodToSchema(type).entrySet()) {
rpcBuilder.put(rpc.getKey(), createStrategy(rpc.getKey(), rpc.getValue()));
proxy = (RpcService) Proxy.newProxyInstance(type.getClassLoader(), new Class[] {type}, this);
}
- private ListenableFuture<RpcResult<?>> invoke0(final SchemaPath schemaPath, final NormalizedNode<?, ?> input) {
+ ListenableFuture<RpcResult<?>> invoke0(final SchemaPath schemaPath, final NormalizedNode<?, ?> input) {
final CheckedFuture<DOMRpcResult, DOMRpcException> result = delegate.invokeRpc(schemaPath, input);
- if (result instanceof LazyDOMRpcResultFuture) {
- return ((LazyDOMRpcResultFuture) result).getBindingFuture();
+ if (result instanceof BindingRpcFutureAware) {
+ return ((BindingRpcFutureAware) result).getBindingFuture();
+ } else if (result instanceof LegacyDOMRpcResultFutureAdapter) {
+ Future<org.opendaylight.mdsal.dom.api.DOMRpcResult> delegateFuture =
+ ((LegacyDOMRpcResultFutureAdapter)result).delegate();
+ if (delegateFuture instanceof BindingRpcFutureAware) {
+ return ((BindingRpcFutureAware) delegateFuture).getBindingFuture();
+ }
}
return transformFuture(schemaPath, result, codec.getCodecFactory());
}
@Override
- public Object invoke(final Object proxyObj, final Method method, final Object[] args) throws Throwable {
+ public Object invoke(final Object proxyObj, final Method method, final Object[] args) {
final RpcInvocationStrategy rpc = rpcNames.get(method);
if (rpc != null) {
- if (method.getParameterTypes().length == 0) {
+ if (method.getParameterCount() == 0) {
return rpc.invokeEmpty();
}
if (args.length != 1) {
private static boolean isObjectMethod(final Method method) {
switch (method.getName()) {
case "toString":
- return method.getReturnType().equals(String.class) && method.getParameterTypes().length == 0;
+ return method.getReturnType().equals(String.class) && method.getParameterCount() == 0;
case "hashCode":
- return method.getReturnType().equals(int.class) && method.getParameterTypes().length == 0;
+ return method.getReturnType().equals(int.class) && method.getParameterCount() == 0;
case "equals":
- return method.getReturnType().equals(boolean.class) && method.getParameterTypes().length == 1 && method
+ return method.getReturnType().equals(boolean.class) && method.getParameterCount() == 1 && method
.getParameterTypes()[0] == Object.class;
default:
return false;
private static ListenableFuture<RpcResult<?>> transformFuture(final SchemaPath rpc,
final ListenableFuture<DOMRpcResult> domFuture, final BindingNormalizedNodeSerializer codec) {
- return Futures.transform(domFuture, (Function<DOMRpcResult, RpcResult<?>>) input -> {
+ return Futures.transform(domFuture, input -> {
final NormalizedNode<?, ?> domData = input.getResult();
final DataObject bindingResult;
if (domData != null) {
} else {
bindingResult = null;
}
- return RpcResult.class.cast(RpcResultBuilder.success(bindingResult).build());
+
+ // DOMRpcResult does not have a notion of success, hence we have to reverse-engineer it by looking
+ // at reported errors and checking whether they are just warnings.
+ final Collection<? extends RpcError> errors = input.getErrors();
+ return RpcResult.class.cast(RpcResultBuilder.status(errors.stream()
+ .noneMatch(error -> error.getSeverity() == ErrorSeverity.ERROR))
+ .withResult(bindingResult).withRpcErrors(errors).build());
}, MoreExecutors.directExecutor());
}