2 * Copyright (c) 2013 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.sal.binding.test.connect.dom;
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.After;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.opendaylight.controller.md.sal.dom.api.DOMRpcIdentifier;
30 import org.opendaylight.controller.md.sal.dom.api.DOMRpcProviderService;
31 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
32 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
33 import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
34 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
35 import org.opendaylight.controller.sal.binding.test.util.BindingBrokerTestFactory;
36 import org.opendaylight.controller.sal.binding.test.util.BindingTestContext;
37 import org.opendaylight.mdsal.binding.dom.adapter.BindingDOMRpcProviderServiceAdapter;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.of.migration.test.model.rev150210.KnockKnockInput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.of.migration.test.model.rev150210.KnockKnockInputBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.of.migration.test.model.rev150210.KnockKnockOutput;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.of.migration.test.model.rev150210.KnockKnockOutputBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.of.migration.test.model.rev150210.OpendaylightOfMigrationTestModelService;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.Top;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelList;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelListKey;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.rpc.routing.rev140701.TestContext;
47 import org.opendaylight.yangtools.yang.binding.DataObject;
48 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
49 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
50 import org.opendaylight.yangtools.yang.common.QName;
51 import org.opendaylight.yangtools.yang.common.RpcResult;
52 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
53 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
54 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
56 public class CrossBrokerRpcTest {
58 protected RpcProviderRegistry providerRegistry;
59 protected DOMRpcProviderService provisionRegistry;
60 private BindingTestContext testContext;
61 private DOMRpcService biRpcInvoker;
62 private MessageCapturingFlowService knockService;
64 public static final TopLevelListKey NODE_A = new TopLevelListKey("a");
65 public static final TopLevelListKey NODE_B = new TopLevelListKey("b");
66 public static final TopLevelListKey NODE_C = new TopLevelListKey("c");
68 private static final QName NODE_ID_QNAME = QName.create(TopLevelList.QNAME, "name");
69 private static final QName KNOCK_KNOCK_QNAME = QName.create(KnockKnockOutput.QNAME, "knock-knock");
70 private static final SchemaPath KNOCK_KNOCK_PATH = SchemaPath.create(true, KNOCK_KNOCK_QNAME);
72 public static final InstanceIdentifier<Top> NODES_PATH = InstanceIdentifier.builder(Top.class).build();
73 public static final InstanceIdentifier<TopLevelList> BA_NODE_A_ID = NODES_PATH.child(TopLevelList.class, NODE_A);
74 public static final InstanceIdentifier<TopLevelList> BA_NODE_B_ID = NODES_PATH.child(TopLevelList.class, NODE_B);
75 public static final InstanceIdentifier<TopLevelList> BA_NODE_C_ID = NODES_PATH.child(TopLevelList.class, NODE_C);
77 public static final org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier BI_NODE_C_ID =
78 createBINodeIdentifier(NODE_C);
82 public void setup() throws Exception {
83 BindingBrokerTestFactory testFactory = new BindingBrokerTestFactory();
84 testFactory.setExecutor(MoreExecutors.newDirectExecutorService());
85 testContext = testFactory.getTestContext();
87 testContext.setSchemaModuleInfos(ImmutableSet.of(
88 BindingReflections.getModuleInfo(OpendaylightOfMigrationTestModelService.class)));
90 providerRegistry = testContext.getBindingRpcRegistry();
91 provisionRegistry = testContext.getDomRpcRegistry();
92 biRpcInvoker = testContext.getDomRpcInvoker();
93 assertNotNull(providerRegistry);
94 assertNotNull(provisionRegistry);
96 knockService = MessageCapturingFlowService.create(providerRegistry);
101 public void teardown() throws Exception {
106 public void testBindingRpcShortcutRegisteredViaLegacyAPI()
107 throws InterruptedException, ExecutionException, TimeoutException {
108 final ListenableFuture<RpcResult<KnockKnockOutput>> knockResult = knockResult(true, "open");
109 knockService.registerPath(TestContext.class, BA_NODE_A_ID).setKnockKnockResult(knockResult);
111 OpendaylightOfMigrationTestModelService baKnockInvoker =
112 providerRegistry.getRpcService(OpendaylightOfMigrationTestModelService.class);
114 final KnockKnockInput knockInput = knockKnock(BA_NODE_A_ID).setQuestion("Who's there?").build();
115 ListenableFuture<RpcResult<KnockKnockOutput>> future = baKnockInvoker.knockKnock(knockInput);
117 final RpcResult<KnockKnockOutput> rpcResult = future.get(5, TimeUnit.SECONDS);
119 assertEquals(knockResult.get().getResult().getClass(), rpcResult.getResult().getClass());
120 assertSame(knockResult.get().getResult(), rpcResult.getResult());
121 assertSame(knockInput, knockService.getReceivedKnocks().get(BA_NODE_A_ID).iterator().next());
125 public void testBindingRpcShortcutRegisteredViaMdsalAPI()
126 throws InterruptedException, ExecutionException, TimeoutException {
127 final ListenableFuture<RpcResult<KnockKnockOutput>> knockResult = knockResult(true, "open");
129 BindingDOMRpcProviderServiceAdapter mdsalServiceRegistry = new BindingDOMRpcProviderServiceAdapter(
130 testContext.getDelegateDomRouter().getRpcProviderService(), testContext.getCodec());
132 final Multimap<InstanceIdentifier<?>, KnockKnockInput> receivedKnocks = HashMultimap.create();
133 mdsalServiceRegistry.registerRpcImplementation(OpendaylightOfMigrationTestModelService.class,
134 (OpendaylightOfMigrationTestModelService) input -> {
135 receivedKnocks.put(input.getKnockerId(), input);
137 }, ImmutableSet.of(BA_NODE_A_ID));
139 OpendaylightOfMigrationTestModelService baKnockInvoker =
140 providerRegistry.getRpcService(OpendaylightOfMigrationTestModelService.class);
142 final KnockKnockInput knockInput = knockKnock(BA_NODE_A_ID).setQuestion("Who's there?").build();
143 Future<RpcResult<KnockKnockOutput>> future = baKnockInvoker.knockKnock(knockInput);
145 final RpcResult<KnockKnockOutput> rpcResult = future.get(5, TimeUnit.SECONDS);
147 assertEquals(knockResult.get().getResult().getClass(), rpcResult.getResult().getClass());
148 assertSame(knockResult.get().getResult(), rpcResult.getResult());
149 assertSame(knockInput, receivedKnocks.get(BA_NODE_A_ID).iterator().next());
153 public void bindingRoutedRpcProvider_DomInvokerTest() throws Exception {
156 .registerPath(TestContext.class, BA_NODE_A_ID) //
157 .registerPath(TestContext.class, BA_NODE_B_ID) //
158 .setKnockKnockResult(knockResult(true, "open"));
160 OpendaylightOfMigrationTestModelService baKnockInvoker =
161 providerRegistry.getRpcService(OpendaylightOfMigrationTestModelService.class);
162 assertNotSame(knockService, baKnockInvoker);
164 KnockKnockInput knockKnockA = knockKnock(BA_NODE_A_ID) //
165 .setQuestion("who's there?").build();
167 ContainerNode knockKnockDom = toDomRpc(KNOCK_KNOCK_QNAME, knockKnockA);
168 assertNotNull(knockKnockDom);
169 DOMRpcResult domResult = biRpcInvoker.invokeRpc(KNOCK_KNOCK_PATH, knockKnockDom).get();
170 assertNotNull(domResult);
171 assertNotNull("DOM result is successful.", domResult.getResult());
172 assertTrue("Bidning Add Flow RPC was captured.", knockService.getReceivedKnocks().containsKey(BA_NODE_A_ID));
173 assertEquals(knockKnockA, knockService.getReceivedKnocks().get(BA_NODE_A_ID).iterator().next());
177 public void bindingRpcInvoker_DomRoutedProviderTest() throws Exception {
178 KnockKnockOutputBuilder builder = new KnockKnockOutputBuilder();
179 builder.setAnswer("open");
180 final KnockKnockOutput output = builder.build();
182 provisionRegistry.registerRpcImplementation((rpc, input) -> {
183 ContainerNode result = testContext.getCodec().getCodecFactory().toNormalizedNodeRpcData(output);
184 return Futures.immediateCheckedFuture(new DefaultDOMRpcResult(result));
185 }, DOMRpcIdentifier.create(KNOCK_KNOCK_PATH, BI_NODE_C_ID));
187 OpendaylightOfMigrationTestModelService baKnockInvoker =
188 providerRegistry.getRpcService(OpendaylightOfMigrationTestModelService.class);
189 Future<RpcResult<KnockKnockOutput>> baResult = baKnockInvoker.knockKnock(knockKnock(BA_NODE_C_ID)
190 .setQuestion("Who's there?").build());
191 assertNotNull(baResult);
192 assertEquals(output, baResult.get().getResult());
195 private ContainerNode toDomRpcInput(final DataObject addFlowA) {
196 return testContext.getCodec().getCodecFactory().toNormalizedNodeRpcData(addFlowA);
199 private static org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier createBINodeIdentifier(
200 final TopLevelListKey listKey) {
201 return org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.builder().node(Top.QNAME)
202 .node(TopLevelList.QNAME)
203 .nodeWithKey(TopLevelList.QNAME, NODE_ID_QNAME, listKey.getName()).build();
206 private static ListenableFuture<RpcResult<KnockKnockOutput>> knockResult(final boolean success,
207 final String answer) {
208 KnockKnockOutput output = new KnockKnockOutputBuilder().setAnswer(answer).build();
209 RpcResult<KnockKnockOutput> result = RpcResultBuilder.<KnockKnockOutput>status(success).withResult(output)
211 return Futures.immediateFuture(result);
214 private static KnockKnockInputBuilder knockKnock(final InstanceIdentifier<TopLevelList> listId) {
215 KnockKnockInputBuilder builder = new KnockKnockInputBuilder();
216 builder.setKnockerId(listId);
220 private ContainerNode toDomRpc(final QName rpcName, final KnockKnockInput knockInput) {
221 return toDomRpcInput(knockInput);