Adjust for Binding RPC codegen changes
[controller.git] / opendaylight / md-sal / sal-binding-dom-it / src / test / java / org / opendaylight / controller / sal / binding / test / connect / dom / CrossBrokerRpcTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.sal.binding.test.connect.dom;
9
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;
14
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;
46
47 public class CrossBrokerRpcTest {
48
49     protected RpcProviderRegistry providerRegistry;
50     protected DOMRpcProviderService provisionRegistry;
51     private BindingTestContext testContext;
52     private DOMRpcService biRpcInvoker;
53     private MessageCapturingFlowService knockService;
54
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");
58
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);
62
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);
67
68     public static final org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier BI_NODE_C_ID =
69             createBINodeIdentifier(NODE_C);
70
71
72     @Before
73     public void setup() {
74         BindingBrokerTestFactory testFactory = new BindingBrokerTestFactory();
75         testFactory.setExecutor(MoreExecutors.newDirectExecutorService());
76         testFactory.setStartWithParsedSchema(true);
77         testContext = testFactory.getTestContext();
78
79         testContext.start();
80         providerRegistry = testContext.getBindingRpcRegistry();
81         provisionRegistry = testContext.getDomRpcRegistry();
82         biRpcInvoker = testContext.getDomRpcInvoker();
83         assertNotNull(providerRegistry);
84         assertNotNull(provisionRegistry);
85
86         knockService = MessageCapturingFlowService.create(providerRegistry);
87
88     }
89
90     @Test
91     public void bindingRoutedRpcProvider_DomInvokerTest() throws Exception {
92
93         knockService//
94                 .registerPath(TestContext.class, BA_NODE_A_ID) //
95                 .registerPath(TestContext.class, BA_NODE_B_ID) //
96                 .setKnockKnockResult(knockResult(true, "open"));
97
98         OpendaylightOfMigrationTestModelService baKnockInvoker =
99                 providerRegistry.getRpcService(OpendaylightOfMigrationTestModelService.class);
100         assertNotSame(knockService, baKnockInvoker);
101
102         KnockKnockInput knockKnockA = knockKnock(BA_NODE_A_ID) //
103                 .setQuestion("who's there?").build();
104
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());
112     }
113
114     @Test
115     public void bindingRpcInvoker_DomRoutedProviderTest() throws Exception {
116         KnockKnockOutputBuilder builder = new KnockKnockOutputBuilder();
117         builder.setAnswer("open");
118         final KnockKnockOutput output = builder.build();
119
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));
124
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());
131     }
132
133     private ContainerNode toDomRpcInput(final DataObject addFlowA) {
134         return testContext.getCodec().getCodecFactory().toNormalizedNodeRpcData(addFlowA);
135     }
136
137     @After
138     public void teardown() throws Exception {
139         testContext.close();
140     }
141
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();
147     }
148
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)
153                 .build();
154         return Futures.immediateFuture(result);
155     }
156
157     private static KnockKnockInputBuilder knockKnock(final InstanceIdentifier<TopLevelList> listId) {
158         KnockKnockInputBuilder builder = new KnockKnockInputBuilder();
159         builder.setKnockerId(listId);
160         return builder;
161     }
162
163     private ContainerNode toDomRpc(final QName rpcName, final KnockKnockInput knockInput) {
164         return toDomRpcInput(knockInput);
165     }
166 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.