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