*/
package org.opendaylight.mdsal.binding.javav2.dom.adapter.impl.operation;
+import static java.util.Objects.requireNonNull;
+
import com.google.common.annotations.Beta;
import com.google.common.base.Preconditions;
-import com.google.common.collect.ImmutableMap;
+import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.JdkFutureAdapters;
import com.google.common.util.concurrent.ListenableFuture;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Collection;
-import java.util.Map.Entry;
import java.util.Optional;
import org.opendaylight.mdsal.binding.javav2.dom.adapter.extractor.ContextReferenceExtractor;
import org.opendaylight.mdsal.binding.javav2.dom.codec.impl.BindingNormalizedNodeCodecRegistry;
import org.opendaylight.mdsal.binding.javav2.dom.codec.impl.BindingToNormalizedNodeCodec;
import org.opendaylight.mdsal.binding.javav2.dom.codec.serialized.LazySerializedContainerNode;
import org.opendaylight.mdsal.binding.javav2.runtime.reflection.BindingReflections;
+import org.opendaylight.mdsal.binding.javav2.spec.base.Input;
import org.opendaylight.mdsal.binding.javav2.spec.base.InstanceIdentifier;
import org.opendaylight.mdsal.binding.javav2.spec.base.Instantiable;
+import org.opendaylight.mdsal.binding.javav2.spec.base.Output;
import org.opendaylight.mdsal.binding.javav2.spec.base.Rpc;
+import org.opendaylight.mdsal.binding.javav2.spec.base.RpcCallback;
import org.opendaylight.mdsal.binding.javav2.spec.base.TreeNode;
import org.opendaylight.mdsal.dom.api.DOMRpcResult;
import org.opendaylight.mdsal.dom.api.DOMRpcService;
import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
-import org.opendaylight.yangtools.yang.model.api.OperationDefinition;
import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
import org.opendaylight.yangtools.yang.model.api.SchemaPath;
@Beta
class RpcServiceAdapter implements InvocationHandler {
- private final ImmutableMap<Method, RpcInvocationStrategy> rpcNames;
+ private final RpcInvocationStrategy strategy;
private final Class<? extends Rpc<?, ?>> type;
private final BindingToNormalizedNodeCodec codec;
private final DOMRpcService delegate;
this.type = Preconditions.checkNotNull(type);
this.codec = Preconditions.checkNotNull(codec);
this.delegate = Preconditions.checkNotNull(domService);
- final ImmutableMap.Builder<Method, RpcInvocationStrategy> rpcBuilder = ImmutableMap.builder();
- for (final Entry<Method, OperationDefinition> rpc : codec.getRPCMethodToSchema(type).entrySet()) {
- rpcBuilder.put(rpc.getKey(), createStrategy(rpc.getKey(), (RpcDefinition) rpc.getValue()));
- }
- rpcNames = rpcBuilder.build();
+ strategy = createStrategy(type);
proxy = (Rpc<?, ?>) Proxy.newProxyInstance(type.getClassLoader(), new Class[] { type }, this);
}
- private RpcInvocationStrategy createStrategy(final Method method, final RpcDefinition schema) {
- final RpcRoutingStrategy strategy = RpcRoutingStrategy.from(schema);
- if (strategy.isContextBasedRouted()) {
- return new RoutedStrategy(schema.getPath(), method, strategy.getLeaf());
+ private RpcInvocationStrategy createStrategy(final Class<? extends Rpc<?, ?>> rpcInterface) {
+ final RpcDefinition rpc = codec.getRpcDefinition(rpcInterface);
+ final RpcRoutingStrategy domStrategy = RpcRoutingStrategy.from(rpc);
+ if (domStrategy.isContextBasedRouted()) {
+ try {
+ return new RoutedStrategy(rpc.getPath(),
+ rpcInterface.getMethod("invoke", Input.class, RpcCallback.class), domStrategy.getLeaf());
+ } catch (final NoSuchMethodException e) {
+ throw new IllegalStateException("Can not find 'invoke' method", e);
+ }
}
- return new NonRoutedStrategy(schema.getPath());
+ return new NonRoutedStrategy(rpc.getPath());
}
Rpc<?, ?> getProxy() {
@Override
@SuppressWarnings("checkstyle:hiddenField")
public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
-
- final RpcInvocationStrategy rpc = rpcNames.get(method);
- if (rpc != null) {
- if (method.getParameterTypes().length == 0) {
- return rpc.invokeEmpty();
- }
- if (args.length != 1) {
- throw new IllegalArgumentException("Input must be provided.");
- }
- return rpc.invoke((TreeNode) args[0]);
- }
-
- if (isObjectMethod(method)) {
- return callObjectMethod(proxy, method, args);
- }
- throw new UnsupportedOperationException("Method " + method.toString() + "is unsupported.");
- }
-
- private static boolean isObjectMethod(final Method method) {
- switch (method.getName()) {
- case "toString":
- return method.getReturnType().equals(String.class) && method.getParameterTypes().length == 0;
- case "hashCode":
- return method.getReturnType().equals(int.class) && method.getParameterTypes().length == 0;
- case "equals":
- return method.getReturnType().equals(boolean.class) && method.getParameterTypes().length == 1
- && method.getParameterTypes()[0] == Object.class;
- default:
- return false;
- }
- }
-
- private Object callObjectMethod(final Object self, final Method method, final Object[] args) {
switch (method.getName()) {
case "toString":
return type.getName() + "$Adapter{delegate=" + delegate.toString() + "}";
case "hashCode":
- return System.identityHashCode(self);
+ return System.identityHashCode(proxy);
case "equals":
- return self == args[0];
+ return proxy == args[0];
+ case "invoke":
+ if (args.length == 2) {
+ final Input<?> input = (Input<?>) requireNonNull(args[0]);
+ final RpcCallback<Output> callback = (RpcCallback<Output>) requireNonNull(args[1]);
+ ListenableFuture<RpcResult<?>> future = strategy.invoke((TreeNode) input);
+ Futures.addCallback(future, new FutureCallback<RpcResult<?>>() {
+
+ @Override
+ public void onSuccess(final RpcResult<?> result) {
+ if (result.getErrors().isEmpty()) {
+ callback.onSuccess((Output) result.getResult());
+ } else {
+ result.getErrors().forEach(e -> callback.onFailure(e.getCause()));
+ }
+ }
+
+ @Override
+ public void onFailure(final Throwable throwable) {
+ callback.onFailure(throwable);
+ }
+ }, MoreExecutors.directExecutor());
+ }
+ return 0;
default:
- return null;
+ break;
}
+
+ throw new UnsupportedOperationException("Method " + method.toString() + "is unsupported.");
}
private abstract class RpcInvocationStrategy {
--- /dev/null
+/*
+ * Copyright (c) 2018 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * 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.mdsal.binding.javav2.dom.adapter.impl.operation;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import com.google.common.collect.ImmutableSet;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.mdsal.binding.javav2.api.DataTreeIdentifier;
+import org.opendaylight.mdsal.binding.javav2.dom.adapter.test.BindingBrokerTestFactory;
+import org.opendaylight.mdsal.binding.javav2.dom.adapter.test.BindingTestContext;
+import org.opendaylight.mdsal.binding.javav2.spec.base.InstanceIdentifier;
+import org.opendaylight.mdsal.binding.javav2.spec.base.RpcCallback;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
+import org.opendaylight.mdsal.dom.api.DOMActionNotAvailableException;
+import org.opendaylight.mdsal.dom.api.DOMRpcImplementationNotAvailableException;
+import org.opendaylight.mdsal.gen.javav2.urn.test.operation.rev170621.TestOperationServiceInContAction;
+import org.opendaylight.mdsal.gen.javav2.urn.test.operation.rev170621.TestOperationServiceMyRpcRpc;
+import org.opendaylight.mdsal.gen.javav2.urn.test.operation.rev170621.data.MyCont;
+import org.opendaylight.mdsal.gen.javav2.urn.test.operation.rev170621.data.my_cont.in_cont.InContInput;
+import org.opendaylight.mdsal.gen.javav2.urn.test.operation.rev170621.data.my_cont.in_cont.InContOutput;
+import org.opendaylight.mdsal.gen.javav2.urn.test.operation.rev170621.data.my_rpc.MyRpcInput;
+import org.opendaylight.mdsal.gen.javav2.urn.test.operation.rev170621.data.my_rpc.MyRpcOutput;
+import org.opendaylight.mdsal.gen.javav2.urn.test.operation.rev170621.dto.my_cont.in_cont.InContInputBuilder;
+import org.opendaylight.mdsal.gen.javav2.urn.test.operation.rev170621.dto.my_cont.in_cont.InContOutputBuilder;
+import org.opendaylight.mdsal.gen.javav2.urn.test.operation.rev170621.dto.my_rpc.MyRpcInputBuilder;
+import org.opendaylight.mdsal.gen.javav2.urn.test.operation.rev170621.dto.my_rpc.MyRpcOutputBuilder;
+import org.opendaylight.yangtools.concepts.ObjectRegistration;
+
+
+public class BindingDOMOperationAdapterTest {
+ private static final MyRpcOutput RPC_OUTPUT = new MyRpcOutputBuilder().setRpcOutputLeaf("rpc_output_leaf").build();
+ private static final InContOutput ACTION_OUTPUT =
+ new InContOutputBuilder().setOutputLeaf("action_output_leaf").build();
+ private BindingDOMOperationProviderServiceAdapter adapter;
+ private BindingDOMOperationServiceAdapter serviceAdapter;
+
+ @Before
+ public void setUp() throws Exception {
+ final BindingBrokerTestFactory testFactory = new BindingBrokerTestFactory();
+ testFactory.setExecutor(Executors.newCachedThreadPool());
+
+ final BindingTestContext testContext = testFactory.getTestContext();
+ testContext.start();
+
+ this.adapter = new BindingDOMOperationProviderServiceAdapter(testContext.getDomRpcRegistry(),
+ testContext.getDomActionRegistry(), testContext.getCodec());
+
+ this.serviceAdapter = new BindingDOMOperationServiceAdapter(testContext.getDomRpcInvoker(),
+ testContext.getDomActionService(), testContext.getCodec());
+ }
+
+ @Test
+ public void actionTest() throws Exception {
+ final CountDownLatch invokeLatch = new CountDownLatch(1);
+ final ObjectRegistration<?> registration = adapter.registerActionImplementation(
+ TestOperationServiceInContAction.class, new TestActionImpl(), LogicalDatastoreType.OPERATIONAL,
+ ImmutableSet.of(
+ DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(MyCont.class))));
+ assertNotNull(registration);
+
+ final TestOperationServiceInContAction action =
+ serviceAdapter.getActionService(TestOperationServiceInContAction.class);
+ assertNotNull(action);
+
+ action.invoke(new InContInputBuilder().setInputLeaf("in").build(),
+ InstanceIdentifier.create(MyCont.class),
+ new RpcCallback<InContOutput>() {
+ @Override
+ public void onSuccess(InContOutput output) {
+ assertEquals(ACTION_OUTPUT, output);
+ invokeLatch.countDown();
+ }
+
+ @Override
+ public void onFailure(Throwable cause) {
+ assertNotNull(cause);
+ }
+ });
+ assertEquals("Invoke complete", true, invokeLatch.await(5, TimeUnit.SECONDS));
+ }
+
+ @Test
+ public void actionFailedTest() throws Exception {
+
+ final CountDownLatch invokeLatch = new CountDownLatch(1);
+
+ final ObjectRegistration<?> registration = adapter.registerActionImplementation(
+ TestOperationServiceInContAction.class, new TestActionFailedImpl(), LogicalDatastoreType.OPERATIONAL,
+ ImmutableSet.of(DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL,
+ InstanceIdentifier.create(MyCont.class))));
+ assertNotNull(registration);
+
+ final TestOperationServiceInContAction action =
+ serviceAdapter.getActionService(TestOperationServiceInContAction.class);
+ assertNotNull(action);
+
+ action.invoke(new InContInputBuilder().setInputLeaf("in").build(),
+ InstanceIdentifier.create(MyCont.class),
+ new RpcCallback<InContOutput>() {
+ @Override
+ public void onSuccess(InContOutput output) {
+
+ }
+
+ @Override
+ public void onFailure(Throwable cause) {
+ assertTrue(cause instanceof RuntimeException);
+ assertEquals(cause.getMessage(), "TestActionFailedImpl invoke failed.");
+ invokeLatch.countDown();
+ }
+ });
+ assertEquals("Invoke complete", true, invokeLatch.await(5, TimeUnit.SECONDS));
+ }
+
+ @Test
+ public void actionNotAvaliableTest() throws Exception {
+
+ final CountDownLatch invokeLatch = new CountDownLatch(1);
+
+ final TestOperationServiceInContAction action =
+ serviceAdapter.getActionService(TestOperationServiceInContAction.class);
+ assertNotNull(action);
+
+ action.invoke(new InContInputBuilder().setInputLeaf("in").build(),
+ InstanceIdentifier.create(MyCont.class),
+ new RpcCallback<InContOutput>() {
+ @Override
+ public void onSuccess(InContOutput output) {
+
+ }
+
+ @Override
+ public void onFailure(Throwable cause) {
+ assertTrue(cause instanceof DOMActionNotAvailableException);
+ invokeLatch.countDown();
+ }
+ });
+ assertEquals("Invoke complete", true, invokeLatch.await(5, TimeUnit.SECONDS));
+ }
+
+ @Test
+ public void rpcTest() throws Exception {
+ final CountDownLatch invokeLatch = new CountDownLatch(1);
+
+ assertNotNull(adapter.registerRpcImplementation(TestOperationServiceMyRpcRpc.class,
+ new TestRpcImpl()));
+ final TestOperationServiceMyRpcRpc rpc =
+ serviceAdapter.getRpcService(TestOperationServiceMyRpcRpc.class);
+ assertNotNull(rpc);
+
+ rpc.invoke(new MyRpcInputBuilder().setRpcInputLeaf("123").build(),
+ new RpcCallback<MyRpcOutput>() {
+ @Override
+ public void onSuccess(MyRpcOutput output) {
+ assertEquals(RPC_OUTPUT, output);
+ invokeLatch.countDown();
+ }
+
+ @Override
+ public void onFailure(Throwable cause) {
+ }
+ });
+ assertEquals("Invoke complete", true, invokeLatch.await(5, TimeUnit.SECONDS));
+ }
+
+ @Test
+ public void rpcFailedTest() throws Exception {
+
+ final CountDownLatch invokeLatch = new CountDownLatch(1);
+
+ assertNotNull(adapter.registerRpcImplementation(TestOperationServiceMyRpcRpc.class,
+ new TestRpcFailedImpl()));
+ final TestOperationServiceMyRpcRpc rpc =
+ serviceAdapter.getRpcService(TestOperationServiceMyRpcRpc.class);
+ assertNotNull(rpc);
+
+ rpc.invoke(new MyRpcInputBuilder().setRpcInputLeaf("123").build(),
+ new RpcCallback<MyRpcOutput>() {
+ @Override
+ public void onSuccess(MyRpcOutput output) {
+
+ }
+
+ @Override
+ public void onFailure(Throwable cause) {
+ assertTrue(cause instanceof RuntimeException);
+ assertEquals(cause.getMessage(), "TestRpcFailedImpl invoke failed.");
+ invokeLatch.countDown();
+ }
+ });
+ assertEquals("Invoke complete", true, invokeLatch.await(5, TimeUnit.SECONDS));
+ }
+
+ @Test
+ public void rpcNotAvaliableTest() throws Exception {
+ final CountDownLatch invokeLatch = new CountDownLatch(1);
+
+ final TestOperationServiceMyRpcRpc rpc =
+ serviceAdapter.getRpcService(TestOperationServiceMyRpcRpc.class);
+ assertNotNull(rpc);
+
+ rpc.invoke(new MyRpcInputBuilder().setRpcInputLeaf("123").build(),
+ new RpcCallback<MyRpcOutput>() {
+ @Override
+ public void onSuccess(MyRpcOutput output) {
+
+ }
+
+ @Override
+ public void onFailure(Throwable cause) {
+ assertTrue(cause instanceof DOMRpcImplementationNotAvailableException);
+ invokeLatch.countDown();
+ }
+ });
+ assertEquals("Invoke complete", true, invokeLatch.await(5, TimeUnit.SECONDS));
+ }
+
+ private class TestRpcImpl implements TestOperationServiceMyRpcRpc {
+
+ @Override
+ public void invoke(MyRpcInput input, RpcCallback<MyRpcOutput> callback) {
+ callback.onSuccess(RPC_OUTPUT);
+ }
+ }
+
+ private class TestRpcFailedImpl implements TestOperationServiceMyRpcRpc {
+
+ @Override
+ public void invoke(MyRpcInput input, RpcCallback<MyRpcOutput> callback) {
+ callback.onFailure(new RuntimeException("TestRpcFailedImpl invoke failed."));
+ }
+ }
+
+ private class TestActionImpl implements TestOperationServiceInContAction {
+
+ @Override
+ public void invoke(InContInput input, InstanceIdentifier<MyCont> ii, RpcCallback<InContOutput> callback) {
+ callback.onSuccess(ACTION_OUTPUT);
+ }
+ }
+
+ private class TestActionFailedImpl implements TestOperationServiceInContAction {
+
+ @Override
+ public void invoke(InContInput input, InstanceIdentifier<MyCont> ii, RpcCallback<InContOutput> callback) {
+ callback.onFailure(new RuntimeException("TestActionFailedImpl invoke failed."));
+ }
+ }
+}
\ No newline at end of file