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

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