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

©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.