Fix DOMRpcRouter breakage from mdsal
[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.assertSame;
14 import static org.junit.Assert.assertTrue;
15
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;
55
56 public class CrossBrokerRpcTest {
57
58     protected RpcProviderRegistry providerRegistry;
59     protected DOMRpcProviderService provisionRegistry;
60     private BindingTestContext testContext;
61     private DOMRpcService biRpcInvoker;
62     private MessageCapturingFlowService knockService;
63
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");
67
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);
71
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);
76
77     public static final org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier BI_NODE_C_ID =
78             createBINodeIdentifier(NODE_C);
79
80
81     @Before
82     public void setup() throws Exception {
83         BindingBrokerTestFactory testFactory = new BindingBrokerTestFactory();
84         testFactory.setExecutor(MoreExecutors.newDirectExecutorService());
85         testContext = testFactory.getTestContext();
86
87         testContext.setSchemaModuleInfos(ImmutableSet.of(
88                 BindingReflections.getModuleInfo(OpendaylightOfMigrationTestModelService.class)));
89         testContext.start();
90         providerRegistry = testContext.getBindingRpcRegistry();
91         provisionRegistry = testContext.getDomRpcRegistry();
92         biRpcInvoker = testContext.getDomRpcInvoker();
93         assertNotNull(providerRegistry);
94         assertNotNull(provisionRegistry);
95
96         knockService = MessageCapturingFlowService.create(providerRegistry);
97
98     }
99
100     @After
101     public void teardown() throws Exception {
102         testContext.close();
103     }
104
105     @Test
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);
110
111         OpendaylightOfMigrationTestModelService baKnockInvoker =
112                 providerRegistry.getRpcService(OpendaylightOfMigrationTestModelService.class);
113
114         final KnockKnockInput knockInput = knockKnock(BA_NODE_A_ID).setQuestion("Who's there?").build();
115         ListenableFuture<RpcResult<KnockKnockOutput>> future = baKnockInvoker.knockKnock(knockInput);
116
117         final RpcResult<KnockKnockOutput> rpcResult = future.get(5, TimeUnit.SECONDS);
118
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());
122     }
123
124     @Test
125     public void testBindingRpcShortcutRegisteredViaMdsalAPI()
126             throws InterruptedException, ExecutionException, TimeoutException {
127         final ListenableFuture<RpcResult<KnockKnockOutput>> knockResult = knockResult(true, "open");
128
129         BindingDOMRpcProviderServiceAdapter mdsalServiceRegistry = new BindingDOMRpcProviderServiceAdapter(
130                 testContext.getDelegateDomRouter().getRpcProviderService(), testContext.getCodec());
131
132         final Multimap<InstanceIdentifier<?>, KnockKnockInput> receivedKnocks = HashMultimap.create();
133         mdsalServiceRegistry.registerRpcImplementation(OpendaylightOfMigrationTestModelService.class,
134             (OpendaylightOfMigrationTestModelService) input -> {
135                 receivedKnocks.put(input.getKnockerId(), input);
136                 return knockResult;
137             }, ImmutableSet.of(BA_NODE_A_ID));
138
139         OpendaylightOfMigrationTestModelService baKnockInvoker =
140                 providerRegistry.getRpcService(OpendaylightOfMigrationTestModelService.class);
141
142         final KnockKnockInput knockInput = knockKnock(BA_NODE_A_ID).setQuestion("Who's there?").build();
143         Future<RpcResult<KnockKnockOutput>> future = baKnockInvoker.knockKnock(knockInput);
144
145         final RpcResult<KnockKnockOutput> rpcResult = future.get(5, TimeUnit.SECONDS);
146
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());
150     }
151
152     @Test
153     public void bindingRoutedRpcProvider_DomInvokerTest() throws Exception {
154
155         knockService//
156                 .registerPath(TestContext.class, BA_NODE_A_ID) //
157                 .registerPath(TestContext.class, BA_NODE_B_ID) //
158                 .setKnockKnockResult(knockResult(true, "open"));
159
160         OpendaylightOfMigrationTestModelService baKnockInvoker =
161                 providerRegistry.getRpcService(OpendaylightOfMigrationTestModelService.class);
162         assertNotSame(knockService, baKnockInvoker);
163
164         KnockKnockInput knockKnockA = knockKnock(BA_NODE_A_ID) //
165                 .setQuestion("who's there?").build();
166
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());
174     }
175
176     @Test
177     public void bindingRpcInvoker_DomRoutedProviderTest() throws Exception {
178         KnockKnockOutputBuilder builder = new KnockKnockOutputBuilder();
179         builder.setAnswer("open");
180         final KnockKnockOutput output = builder.build();
181
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));
186
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());
193     }
194
195     private ContainerNode toDomRpcInput(final DataObject addFlowA) {
196         return testContext.getCodec().getCodecFactory().toNormalizedNodeRpcData(addFlowA);
197     }
198
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();
204     }
205
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)
210                 .build();
211         return Futures.immediateFuture(result);
212     }
213
214     private static KnockKnockInputBuilder knockKnock(final InstanceIdentifier<TopLevelList> listId) {
215         KnockKnockInputBuilder builder = new KnockKnockInputBuilder();
216         builder.setKnockerId(listId);
217         return builder;
218     }
219
220     private ContainerNode toDomRpc(final QName rpcName, final KnockKnockInput knockInput) {
221         return toDomRpcInput(knockInput);
222     }
223 }