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.assertTrue;
15 import com.google.common.util.concurrent.Futures;
16 import com.google.common.util.concurrent.ListenableFuture;
17 import com.google.common.util.concurrent.MoreExecutors;
18 import java.util.concurrent.Future;
19 import org.junit.After;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.opendaylight.controller.md.sal.dom.api.DOMRpcIdentifier;
23 import org.opendaylight.controller.md.sal.dom.api.DOMRpcProviderService;
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.spi.DefaultDOMRpcResult;
27 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
28 import org.opendaylight.controller.sal.binding.test.util.BindingBrokerTestFactory;
29 import org.opendaylight.controller.sal.binding.test.util.BindingTestContext;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.of.migration.test.model.rev150210.KnockKnockInput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.of.migration.test.model.rev150210.KnockKnockInputBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.of.migration.test.model.rev150210.KnockKnockOutput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.of.migration.test.model.rev150210.KnockKnockOutputBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.of.migration.test.model.rev150210.OpendaylightOfMigrationTestModelService;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.Top;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelList;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelListKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.rpc.routing.rev140701.TestContext;
39 import org.opendaylight.yangtools.yang.binding.DataObject;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41 import org.opendaylight.yangtools.yang.common.QName;
42 import org.opendaylight.yangtools.yang.common.RpcResult;
43 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
44 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
45 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
47 public class CrossBrokerRpcTest {
49 protected RpcProviderRegistry providerRegistry;
50 protected DOMRpcProviderService provisionRegistry;
51 private BindingTestContext testContext;
52 private DOMRpcService biRpcInvoker;
53 private MessageCapturingFlowService knockService;
55 public static final TopLevelListKey NODE_A = new TopLevelListKey("a");
56 public static final TopLevelListKey NODE_B = new TopLevelListKey("b");
57 public static final TopLevelListKey NODE_C = new TopLevelListKey("c");
59 private static final QName NODE_ID_QNAME = QName.create(TopLevelList.QNAME, "name");
60 private static final QName KNOCK_KNOCK_QNAME = QName.create(KnockKnockOutput.QNAME, "knock-knock");
61 private static final SchemaPath KNOCK_KNOCK_PATH = SchemaPath.create(true, KNOCK_KNOCK_QNAME);
63 public static final InstanceIdentifier<Top> NODES_PATH = InstanceIdentifier.builder(Top.class).build();
64 public static final InstanceIdentifier<TopLevelList> BA_NODE_A_ID = NODES_PATH.child(TopLevelList.class, NODE_A);
65 public static final InstanceIdentifier<TopLevelList> BA_NODE_B_ID = NODES_PATH.child(TopLevelList.class, NODE_B);
66 public static final InstanceIdentifier<TopLevelList> BA_NODE_C_ID = NODES_PATH.child(TopLevelList.class, NODE_C);
68 public static final org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier BI_NODE_C_ID =
69 createBINodeIdentifier(NODE_C);
74 BindingBrokerTestFactory testFactory = new BindingBrokerTestFactory();
75 testFactory.setExecutor(MoreExecutors.newDirectExecutorService());
76 testFactory.setStartWithParsedSchema(true);
77 testContext = testFactory.getTestContext();
80 providerRegistry = testContext.getBindingRpcRegistry();
81 provisionRegistry = testContext.getDomRpcRegistry();
82 biRpcInvoker = testContext.getDomRpcInvoker();
83 assertNotNull(providerRegistry);
84 assertNotNull(provisionRegistry);
86 knockService = MessageCapturingFlowService.create(providerRegistry);
91 public void bindingRoutedRpcProvider_DomInvokerTest() throws Exception {
94 .registerPath(TestContext.class, BA_NODE_A_ID) //
95 .registerPath(TestContext.class, BA_NODE_B_ID) //
96 .setKnockKnockResult(knockResult(true, "open"));
98 OpendaylightOfMigrationTestModelService baKnockInvoker =
99 providerRegistry.getRpcService(OpendaylightOfMigrationTestModelService.class);
100 assertNotSame(knockService, baKnockInvoker);
102 KnockKnockInput knockKnockA = knockKnock(BA_NODE_A_ID) //
103 .setQuestion("who's there?").build();
105 ContainerNode knockKnockDom = toDomRpc(KNOCK_KNOCK_QNAME, knockKnockA);
106 assertNotNull(knockKnockDom);
107 DOMRpcResult domResult = biRpcInvoker.invokeRpc(KNOCK_KNOCK_PATH, knockKnockDom).get();
108 assertNotNull(domResult);
109 assertNotNull("DOM result is successful.", domResult.getResult());
110 assertTrue("Bidning Add Flow RPC was captured.", knockService.getReceivedKnocks().containsKey(BA_NODE_A_ID));
111 assertEquals(knockKnockA, knockService.getReceivedKnocks().get(BA_NODE_A_ID).iterator().next());
115 public void bindingRpcInvoker_DomRoutedProviderTest() throws Exception {
116 KnockKnockOutputBuilder builder = new KnockKnockOutputBuilder();
117 builder.setAnswer("open");
118 final KnockKnockOutput output = builder.build();
120 provisionRegistry.registerRpcImplementation((rpc, input) -> {
121 ContainerNode result = testContext.getCodec().getCodecFactory().toNormalizedNodeRpcData(output);
122 return Futures.immediateCheckedFuture(new DefaultDOMRpcResult(result));
123 }, DOMRpcIdentifier.create(KNOCK_KNOCK_PATH, BI_NODE_C_ID));
125 OpendaylightOfMigrationTestModelService baKnockInvoker =
126 providerRegistry.getRpcService(OpendaylightOfMigrationTestModelService.class);
127 Future<RpcResult<KnockKnockOutput>> baResult = baKnockInvoker.knockKnock(knockKnock(BA_NODE_C_ID)
128 .setQuestion("Who's there?").build());
129 assertNotNull(baResult);
130 assertEquals(output, baResult.get().getResult());
133 private ContainerNode toDomRpcInput(final DataObject addFlowA) {
134 return testContext.getCodec().getCodecFactory().toNormalizedNodeRpcData(addFlowA);
138 public void teardown() throws Exception {
142 private static org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier createBINodeIdentifier(
143 final TopLevelListKey listKey) {
144 return org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.builder().node(Top.QNAME)
145 .node(TopLevelList.QNAME)
146 .nodeWithKey(TopLevelList.QNAME, NODE_ID_QNAME, listKey.getName()).build();
149 private static ListenableFuture<RpcResult<KnockKnockOutput>> knockResult(final boolean success,
150 final String answer) {
151 KnockKnockOutput output = new KnockKnockOutputBuilder().setAnswer(answer).build();
152 RpcResult<KnockKnockOutput> result = RpcResultBuilder.<KnockKnockOutput>status(success).withResult(output)
154 return Futures.immediateFuture(result);
157 private static KnockKnockInputBuilder knockKnock(final InstanceIdentifier<TopLevelList> listId) {
158 KnockKnockInputBuilder builder = new KnockKnockInputBuilder();
159 builder.setKnockerId(listId);
163 private ContainerNode toDomRpc(final QName rpcName, final KnockKnockInput knockInput) {
164 return toDomRpcInput(knockInput);