Migrate users of deprecated yang.common methods
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / test / java / org / opendaylight / mdsal / binding / dom / adapter / BindingDOMRpcIntegrationTest.java
1 /*
2  * Copyright (c) 2016 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.mdsal.binding.dom.adapter;
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 org.junit.Before;
23 import org.junit.Test;
24 import org.opendaylight.mdsal.binding.api.RpcProviderService;
25 import org.opendaylight.mdsal.binding.api.RpcService;
26 import org.opendaylight.mdsal.binding.dom.adapter.test.util.BindingBrokerTestFactory;
27 import org.opendaylight.mdsal.binding.dom.adapter.test.util.BindingTestContext;
28 import org.opendaylight.mdsal.binding.runtime.spi.BindingRuntimeHelpers;
29 import org.opendaylight.mdsal.dom.api.DOMRpcIdentifier;
30 import org.opendaylight.mdsal.dom.api.DOMRpcProviderService;
31 import org.opendaylight.mdsal.dom.api.DOMRpcService;
32 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.md.sal.knock.knock.rev180723.KnockKnock;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.md.sal.knock.knock.rev180723.KnockKnockInput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.md.sal.knock.knock.rev180723.KnockKnockInputBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.md.sal.knock.knock.rev180723.KnockKnockOutput;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.md.sal.knock.knock.rev180723.KnockKnockOutputBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.Top;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelList;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelListKey;
41 import org.opendaylight.yangtools.concepts.Registration;
42 import org.opendaylight.yangtools.util.concurrent.FluentFutures;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44 import org.opendaylight.yangtools.yang.common.QName;
45 import org.opendaylight.yangtools.yang.common.RpcResult;
46 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
47 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
48
49 public class BindingDOMRpcIntegrationTest {
50     private static final InstanceIdentifier<TopLevelList> BA_NODE_ID = InstanceIdentifier.create(Top.class)
51             .child(TopLevelList.class, new TopLevelListKey("a"));
52
53     private static final QName KNOCK_KNOCK_QNAME = QName.create(KnockKnockOutput.QNAME, "knock-knock");
54
55     private RpcProviderService baRpcProviderService;
56     private RpcService baRpcService;
57     private DOMRpcProviderService biRpcProviderService;
58     private BindingTestContext testContext;
59     private DOMRpcService biRpcService;
60     private final KnockKnockImpl knockRpcImpl = new KnockKnockImpl();
61
62     @Before
63     public void setup() throws Exception {
64         BindingBrokerTestFactory testFactory = new BindingBrokerTestFactory();
65         testFactory.setExecutor(MoreExecutors.newDirectExecutorService());
66         testContext = testFactory.getTestContext();
67
68         testContext.setSchemaModuleInfos(ImmutableSet.of(
69             BindingRuntimeHelpers.getYangModuleInfo(KnockKnock.class),
70             BindingRuntimeHelpers.getYangModuleInfo(Top.class)));
71         testContext.start();
72         baRpcProviderService = testContext.getBindingRpcProviderRegistry();
73         baRpcService = testContext.getBindingRpcService();
74         biRpcProviderService = testContext.getDomRpcRegistry();
75         biRpcService = testContext.getDomRpcInvoker();
76     }
77
78     @Test
79     public void testBindingRegistrationWithDOMInvocation() throws Exception {
80         knockRpcImpl.registerTo(baRpcProviderService, BA_NODE_ID).setKnockKnockResult(knockResult(true, "open"));
81
82         final var baKnockService = baRpcService.getRpc(KnockKnock.class);
83         assertNotSame(knockRpcImpl, baKnockService);
84
85         final var baKnockKnockInput = knockKnock(BA_NODE_ID).setQuestion("who's there?").build();
86
87         final var biKnockKnockInput = toDOMKnockKnockInput(baKnockKnockInput);
88         final var domResult = Futures.getDone(biRpcService.invokeRpc(KNOCK_KNOCK_QNAME, biKnockKnockInput));
89         assertNotNull(domResult);
90         assertNotNull(domResult.value());
91         assertTrue("Binding KnockKnock service was not invoked",
92                 knockRpcImpl.getReceivedKnocks().containsKey(BA_NODE_ID));
93         assertEquals(baKnockKnockInput, knockRpcImpl.getReceivedKnocks().get(BA_NODE_ID).iterator().next());
94     }
95
96     @Test
97     public void testDOMRegistrationWithBindingInvocation() throws Exception {
98         final var baKnockKnockOutput = new KnockKnockOutputBuilder().setAnswer("open").build();
99
100         biRpcProviderService.registerRpcImplementation((rpc, input) ->
101             FluentFutures.immediateFluentFuture(new DefaultDOMRpcResult(testContext.getCodec()
102                     .currentSerializer().toNormalizedNodeRpcData(baKnockKnockOutput))),
103             DOMRpcIdentifier.create(KNOCK_KNOCK_QNAME, testContext.getCodec().currentSerializer()
104                 .toYangInstanceIdentifier(BA_NODE_ID)));
105
106         final var baKnockService = baRpcService.getRpc(KnockKnock.class);
107         final var baResult = baKnockService.invoke(knockKnock(BA_NODE_ID).setQuestion("Who's there?").build());
108         assertNotNull(baResult);
109         assertEquals(baKnockKnockOutput, Futures.getDone(baResult).getResult());
110     }
111
112     @Test
113     public void testBindingRpcShortcut() throws Exception {
114         final var baKnockResult = knockResult(true, "open");
115         knockRpcImpl.registerTo(baRpcProviderService, BA_NODE_ID).setKnockKnockResult(baKnockResult);
116
117         final var baKnockService = baRpcService.getRpc(KnockKnock.class);
118
119         final var baKnockKnockInput = knockKnock(BA_NODE_ID).setQuestion("who's there?").build();
120
121         final var rpcResult = Futures.getDone(baKnockService.invoke(baKnockKnockInput));
122
123         assertEquals(baKnockResult.get().getResult().getClass(), rpcResult.getResult().getClass());
124         assertSame(baKnockResult.get().getResult(), rpcResult.getResult());
125         assertSame(baKnockKnockInput, knockRpcImpl.getReceivedKnocks().get(BA_NODE_ID).iterator().next());
126     }
127
128     @Test
129     public void testSimpleRpc() throws Exception {
130         baRpcProviderService.registerRpcImplementation((KnockKnock) input -> knockResult(true, "open"));
131
132         final var baKnockService = baRpcService.getRpc(KnockKnock.class);
133         final var rpcResult = Futures.getDone(
134             baKnockService.invoke(knockKnock(BA_NODE_ID).setQuestion("who's there?").build()));
135
136         assertEquals(rpcResult.getResult().getClass(), rpcResult.getResult().getClass());
137         assertSame(rpcResult.getResult(), rpcResult.getResult());
138     }
139
140     private static ListenableFuture<RpcResult<KnockKnockOutput>> knockResult(final boolean success,
141             final String answer) {
142         return RpcResultBuilder.<KnockKnockOutput>status(success)
143             .withResult(new KnockKnockOutputBuilder().setAnswer(answer).build())
144             .buildFuture();
145     }
146
147     private static KnockKnockInputBuilder knockKnock(final InstanceIdentifier<TopLevelList> listId) {
148         KnockKnockInputBuilder builder = new KnockKnockInputBuilder();
149         builder.setKnockerId(listId);
150         return builder;
151     }
152
153     private ContainerNode toDOMKnockKnockInput(final KnockKnockInput from) {
154         return testContext.getCodec().currentSerializer().toNormalizedNodeRpcData(from);
155     }
156
157     private static final class KnockKnockImpl implements KnockKnock {
158         private final Multimap<InstanceIdentifier<?>, KnockKnockInput> receivedKnocks = HashMultimap.create();
159         private ListenableFuture<RpcResult<KnockKnockOutput>> knockKnockResult;
160         private Registration registration;
161
162         KnockKnockImpl setKnockKnockResult(
163                 final ListenableFuture<RpcResult<KnockKnockOutput>> kkOutput) {
164             knockKnockResult = kkOutput;
165             return this;
166         }
167
168         Multimap<InstanceIdentifier<?>, KnockKnockInput> getReceivedKnocks() {
169             return receivedKnocks;
170         }
171
172         KnockKnockImpl registerTo(final RpcProviderService registry, final InstanceIdentifier<?>... paths) {
173             registration = registry.registerRpcImplementation(this, ImmutableSet.copyOf(paths));
174             assertNotNull(registration);
175             return this;
176         }
177
178         @Override
179         public ListenableFuture<RpcResult<KnockKnockOutput>> invoke(final KnockKnockInput input) {
180             receivedKnocks.put(input.getKnockerId(), input);
181             return knockKnockResult;
182         }
183     }
184 }