2 * Copyright (c) 2016 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.mdsal.binding.dom.adapter;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNotSame;
13 import static org.junit.Assert.assertSame;
14 import static org.junit.Assert.assertTrue;
16 import com.google.common.collect.HashMultimap;
17 import com.google.common.collect.ImmutableSet;
18 import com.google.common.collect.Multimap;
19 import com.google.common.util.concurrent.Futures;
20 import com.google.common.util.concurrent.ListenableFuture;
21 import com.google.common.util.concurrent.MoreExecutors;
22 import java.util.concurrent.ExecutionException;
23 import java.util.concurrent.Future;
24 import java.util.concurrent.TimeUnit;
25 import java.util.concurrent.TimeoutException;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.opendaylight.mdsal.binding.api.RpcConsumerRegistry;
29 import org.opendaylight.mdsal.binding.api.RpcProviderService;
30 import org.opendaylight.mdsal.binding.dom.adapter.test.util.BindingBrokerTestFactory;
31 import org.opendaylight.mdsal.binding.dom.adapter.test.util.BindingTestContext;
32 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
33 import org.opendaylight.mdsal.dom.api.DOMRpcIdentifier;
34 import org.opendaylight.mdsal.dom.api.DOMRpcProviderService;
35 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
36 import org.opendaylight.mdsal.dom.api.DOMRpcService;
37 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.md.sal.knock.knock.rev180723.KnockKnockInput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.md.sal.knock.knock.rev180723.KnockKnockInputBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.md.sal.knock.knock.rev180723.KnockKnockOutput;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.md.sal.knock.knock.rev180723.KnockKnockOutputBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.md.sal.knock.knock.rev180723.OpendaylightKnockKnockRpcService;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.Top;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelList;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelListKey;
46 import org.opendaylight.yangtools.concepts.ObjectRegistration;
47 import org.opendaylight.yangtools.util.concurrent.FluentFutures;
48 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
49 import org.opendaylight.yangtools.yang.common.QName;
50 import org.opendaylight.yangtools.yang.common.RpcResult;
51 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
52 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
53 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
55 public class BindingDOMRpcIntegrationTest {
56 private static final InstanceIdentifier<TopLevelList> BA_NODE_ID = InstanceIdentifier.create(Top.class)
57 .child(TopLevelList.class, new TopLevelListKey("a"));
59 private static final QName KNOCK_KNOCK_QNAME = QName.create(KnockKnockOutput.QNAME, "knock-knock");
60 private static final SchemaPath KNOCK_KNOCK_PATH = SchemaPath.create(true, KNOCK_KNOCK_QNAME);
62 private RpcProviderService baRpcProviderService;
63 private RpcConsumerRegistry baRpcConsumerService;
64 private DOMRpcProviderService biRpcProviderService;
65 private BindingTestContext testContext;
66 private DOMRpcService biRpcService;
67 private final OpendaylightKnockKnockRpcServiceImpl knockRpcImpl = new OpendaylightKnockKnockRpcServiceImpl();
70 public void setup() throws Exception {
71 BindingBrokerTestFactory testFactory = new BindingBrokerTestFactory();
72 testFactory.setExecutor(MoreExecutors.newDirectExecutorService());
73 testContext = testFactory.getTestContext();
75 testContext.setSchemaModuleInfos(ImmutableSet.of(
76 BindingReflections.getModuleInfo(OpendaylightKnockKnockRpcService.class),
77 BindingReflections.getModuleInfo(Top.class)));
79 baRpcProviderService = testContext.getBindingRpcProviderRegistry();
80 baRpcConsumerService = testContext.getBindingRpcConsumerRegistry();
81 biRpcProviderService = testContext.getDomRpcRegistry();
82 biRpcService = testContext.getDomRpcInvoker();
86 public void testBindingRegistrationWithDOMInvocation()
87 throws InterruptedException, ExecutionException, TimeoutException {
88 knockRpcImpl.registerTo(baRpcProviderService, BA_NODE_ID).setKnockKnockResult(knockResult(true, "open"));
90 final OpendaylightKnockKnockRpcService baKnockService =
91 baRpcConsumerService.getRpcService(OpendaylightKnockKnockRpcService.class);
92 assertNotSame(knockRpcImpl, baKnockService);
94 KnockKnockInput baKnockKnockInput = knockKnock(BA_NODE_ID).setQuestion("who's there?").build();
96 ContainerNode biKnockKnockInput = toDOMKnockKnockInput(baKnockKnockInput);
97 DOMRpcResult domResult = biRpcService.invokeRpc(KNOCK_KNOCK_PATH, biKnockKnockInput).get(5, TimeUnit.SECONDS);
98 assertNotNull(domResult);
99 assertNotNull(domResult.getResult());
100 assertTrue("Binding KnockKnock service was not invoked",
101 knockRpcImpl.getReceivedKnocks().containsKey(BA_NODE_ID));
102 assertEquals(baKnockKnockInput, knockRpcImpl.getReceivedKnocks().get(BA_NODE_ID).iterator().next());
106 public void testDOMRegistrationWithBindingInvocation()
107 throws InterruptedException, ExecutionException, TimeoutException {
108 KnockKnockOutput baKnockKnockOutput = new KnockKnockOutputBuilder().setAnswer("open").build();
110 biRpcProviderService.registerRpcImplementation((rpc, input) ->
111 FluentFutures.immediateFluentFuture(new DefaultDOMRpcResult(testContext.getCodec()
112 .getCodecFactory().toNormalizedNodeRpcData(baKnockKnockOutput))),
113 DOMRpcIdentifier.create(KNOCK_KNOCK_PATH, testContext.getCodec().toYangInstanceIdentifier(BA_NODE_ID)));
115 final OpendaylightKnockKnockRpcService baKnockService =
116 baRpcConsumerService.getRpcService(OpendaylightKnockKnockRpcService.class);
117 Future<RpcResult<KnockKnockOutput>> baResult = baKnockService.knockKnock(knockKnock(BA_NODE_ID)
118 .setQuestion("Who's there?").build());
119 assertNotNull(baResult);
120 assertEquals(baKnockKnockOutput, baResult.get(5, TimeUnit.SECONDS).getResult());
124 public void testBindingRpcShortcut() throws InterruptedException, ExecutionException, TimeoutException {
125 final ListenableFuture<RpcResult<KnockKnockOutput>> baKnockResult = knockResult(true, "open");
126 knockRpcImpl.registerTo(baRpcProviderService, BA_NODE_ID).setKnockKnockResult(baKnockResult);
128 final OpendaylightKnockKnockRpcService baKnockService =
129 baRpcConsumerService.getRpcService(OpendaylightKnockKnockRpcService.class);
131 KnockKnockInput baKnockKnockInput = knockKnock(BA_NODE_ID).setQuestion("who's there?").build();
132 ListenableFuture<RpcResult<KnockKnockOutput>> future = baKnockService.knockKnock(baKnockKnockInput);
134 final RpcResult<KnockKnockOutput> rpcResult = future.get(5, TimeUnit.SECONDS);
136 assertEquals(baKnockResult.get().getResult().getClass(), rpcResult.getResult().getClass());
137 assertSame(baKnockResult.get().getResult(), rpcResult.getResult());
138 assertSame(baKnockKnockInput, knockRpcImpl.getReceivedKnocks().get(BA_NODE_ID).iterator().next());
141 private static ListenableFuture<RpcResult<KnockKnockOutput>> knockResult(final boolean success,
142 final String answer) {
143 KnockKnockOutput output = new KnockKnockOutputBuilder().setAnswer(answer).build();
144 RpcResult<KnockKnockOutput> result = RpcResultBuilder.<KnockKnockOutput>status(success).withResult(output)
146 return Futures.immediateFuture(result);
149 private static KnockKnockInputBuilder knockKnock(final InstanceIdentifier<TopLevelList> listId) {
150 KnockKnockInputBuilder builder = new KnockKnockInputBuilder();
151 builder.setKnockerId(listId);
155 private ContainerNode toDOMKnockKnockInput(final KnockKnockInput from) {
156 return testContext.getCodec().getCodecFactory().toNormalizedNodeRpcData(from);
159 private static class OpendaylightKnockKnockRpcServiceImpl implements OpendaylightKnockKnockRpcService {
160 private ListenableFuture<RpcResult<KnockKnockOutput>> knockKnockResult;
161 private final Multimap<InstanceIdentifier<?>, KnockKnockInput> receivedKnocks = HashMultimap.create();
162 private ObjectRegistration<OpendaylightKnockKnockRpcServiceImpl> registration;
164 OpendaylightKnockKnockRpcServiceImpl setKnockKnockResult(
165 final ListenableFuture<RpcResult<KnockKnockOutput>> kkOutput) {
166 this.knockKnockResult = kkOutput;
170 Multimap<InstanceIdentifier<?>, KnockKnockInput> getReceivedKnocks() {
171 return receivedKnocks;
174 OpendaylightKnockKnockRpcServiceImpl registerTo(final RpcProviderService registry,
175 final InstanceIdentifier<?>... paths) {
176 registration = registry.registerRpcImplementation(OpendaylightKnockKnockRpcService.class, this,
177 ImmutableSet.copyOf(paths));
178 assertNotNull(registration);
183 public ListenableFuture<RpcResult<KnockKnockOutput>> knockKnock(final KnockKnockInput input) {
184 receivedKnocks.put(input.getKnockerId(), input);
185 return knockKnockResult;