83c2f88376239a29bd0bf09060dc981fff2a1f65
[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.collect.ImmutableSet;
16 import com.google.common.util.concurrent.Futures;
17 import com.google.common.util.concurrent.ListenableFuture;
18 import com.google.common.util.concurrent.MoreExecutors;
19 import org.junit.After;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
23 import org.opendaylight.controller.sal.binding.test.util.BindingBrokerTestFactory;
24 import org.opendaylight.controller.sal.binding.test.util.BindingTestContext;
25 import org.opendaylight.controller.sal.core.api.RpcImplementation;
26 import org.opendaylight.controller.sal.core.api.RpcProvisionRegistry;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.of.migration.test.model.rev150210.KnockKnockInput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.of.migration.test.model.rev150210.KnockKnockInputBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.of.migration.test.model.rev150210.KnockKnockOutput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.of.migration.test.model.rev150210.KnockKnockOutputBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.of.migration.test.model.rev150210.OpendaylightOfMigrationTestModelService;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.Top;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelList;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelListKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.rpc.routing.rev140701.TestContext;
36 import org.opendaylight.yangtools.yang.binding.DataObject;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import org.opendaylight.yangtools.yang.common.QName;
39 import org.opendaylight.yangtools.yang.common.RpcResult;
40 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
41 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
42 import org.opendaylight.yangtools.yang.data.impl.CompositeNodeTOImpl;
43 import java.util.Collections;
44 import java.util.Set;
45 import java.util.concurrent.Future;
46
47 public class CrossBrokerRpcTest {
48
49     protected RpcProviderRegistry providerRegistry;
50     protected RpcProvisionRegistry provisionRegistry;
51     private BindingTestContext testContext;
52     private RpcImplementation biRpcInvoker;
53     private MessageCapturingFlowService knockService;
54
55     public static final TopLevelListKey NODE_A = new TopLevelListKey("a");
56     public static final TopLevelListKey NODE_B = new TopLevelListKey("b");
57     public static final TopLevelListKey NODE_C = new TopLevelListKey("c");
58
59     private static final QName NODE_ID_QNAME = QName.create(TopLevelList.QNAME, "name");
60     private static final QName KNOCK_KNOCK_QNAME = QName.create(KnockKnockOutput.QNAME, "knock-knock");
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.sameThreadExecutor());
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         CompositeNode knockKnockDom = toDomRpc(KNOCK_KNOCK_QNAME, knockKnockA);
104         assertNotNull(knockKnockDom);
105         RpcResult<CompositeNode> domResult = biRpcInvoker.invokeRpc(KNOCK_KNOCK_QNAME, knockKnockDom).get();
106         assertNotNull(domResult);
107         assertTrue("DOM result is successful.", domResult.isSuccessful());
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         org.opendaylight.controller.sal.core.api.Broker.RoutedRpcRegistration registration = provisionRegistry.addRoutedRpcImplementation(KNOCK_KNOCK_QNAME, new RpcImplementation() {
118             @Override
119             public Set<QName> getSupportedRpcs() {
120                 return ImmutableSet.of(KNOCK_KNOCK_QNAME);
121             }
122
123             @Override
124             public ListenableFuture<RpcResult<CompositeNode>> invokeRpc(QName rpc, CompositeNode input) {
125                 CompositeNode result = testContext.getBindingToDomMappingService().toDataDom(output);
126                 return Futures.immediateFuture(RpcResultBuilder.<CompositeNode>success(result).build());
127             }
128         });
129         registration.registerPath(TestContext.QNAME, BI_NODE_C_ID);
130
131
132         OpendaylightOfMigrationTestModelService baKnockInvoker =
133                 providerRegistry.getRpcService(OpendaylightOfMigrationTestModelService.class);
134         Future<RpcResult<KnockKnockOutput>> baResult = baKnockInvoker.knockKnock((knockKnock(BA_NODE_C_ID).setQuestion("Who's there?").build()));
135         assertNotNull(baResult);
136         assertEquals(output, baResult.get().getResult());
137     }
138
139     private CompositeNode toDomRpcInput(DataObject addFlowA) {
140         return testContext.getBindingToDomMappingService().toDataDom(addFlowA);
141     }
142
143     @After
144     public void teardown() throws Exception {
145         testContext.close();
146     }
147
148     private static org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier createBINodeIdentifier(TopLevelListKey listKey) {
149         return org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.builder().node(Top.QNAME)
150                 .nodeWithKey(TopLevelList.QNAME, NODE_ID_QNAME, listKey.getName()).toInstance();
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 CompositeNode toDomRpc(QName rpcName, KnockKnockInput knockInput) {
167         return new CompositeNodeTOImpl(rpcName, null,
168                 Collections.<org.opendaylight.yangtools.yang.data.api.Node<?>>singletonList(toDomRpcInput(knockInput)));
169     }
170 }

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