Merge "Fixed bug in cross-broker RPC routing"
[controller.git] / opendaylight / md-sal / sal-binding-dom-it / src / test / java / org / opendaylight / controller / sal / binding / test / connect / dom / CrossBrokerRpcTest.java
1 package org.opendaylight.controller.sal.binding.test.connect.dom;
2
3 import java.math.BigInteger;
4 import java.util.Collections;
5 import java.util.concurrent.Future;
6
7 import org.junit.After;
8 import org.junit.Before;
9 import org.junit.Test;
10 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration;
11 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
12 import org.opendaylight.controller.sal.binding.test.util.BindingBrokerTestFactory;
13 import org.opendaylight.controller.sal.binding.test.util.BindingTestContext;
14 import org.opendaylight.controller.sal.common.util.Rpcs;
15 import org.opendaylight.controller.sal.core.api.RpcImplementation;
16 import org.opendaylight.controller.sal.core.api.RpcProvisionRegistry;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutput;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutputBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeFlowRemoved;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowOutput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowOutput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev131103.TransactionId;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeContext;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
34 import org.opendaylight.yangtools.yang.binding.BaseIdentity;
35 import org.opendaylight.yangtools.yang.binding.DataObject;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.opendaylight.yangtools.yang.common.QName;
38 import org.opendaylight.yangtools.yang.common.RpcError;
39 import org.opendaylight.yangtools.yang.common.RpcResult;
40 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
41 import org.opendaylight.yangtools.yang.data.impl.CompositeNodeTOImpl;
42
43 import static junit.framework.Assert.*;
44
45 import com.google.common.collect.HashMultimap;
46 import com.google.common.collect.ImmutableList;
47 import com.google.common.collect.Multimap;
48 import com.google.common.util.concurrent.Futures;
49 import com.google.common.util.concurrent.MoreExecutors;
50
51 public class CrossBrokerRpcTest {
52
53     protected RpcProviderRegistry baRpcRegistry;
54     protected RpcProvisionRegistry biRpcRegistry;
55     private BindingTestContext testContext;
56     private RpcImplementation biRpcInvoker;
57     private MessageCapturingFlowService flowService;
58
59     public static final NodeId NODE_A = new NodeId("a");
60     public static final NodeId NODE_B = new NodeId("b");
61     public static final NodeId NODE_C = new NodeId("c");
62     public static final NodeId NODE_D = new NodeId("d");
63
64     public static final InstanceIdentifier<Node> BA_NODE_A_ID = createBANodeIdentifier(NODE_A);
65     public static final InstanceIdentifier<Node> BA_NODE_B_ID = createBANodeIdentifier(NODE_B);
66     public static final InstanceIdentifier<Node> BA_NODE_C_ID = createBANodeIdentifier(NODE_C);
67     public static final InstanceIdentifier<Node> BA_NODE_D_ID = createBANodeIdentifier(NODE_D);
68
69     public static final org.opendaylight.yangtools.yang.data.api.InstanceIdentifier BI_NODE_A_ID = createBINodeIdentifier(NODE_A);
70     public static final org.opendaylight.yangtools.yang.data.api.InstanceIdentifier BI_NODE_B_ID = createBINodeIdentifier(NODE_B);
71     public static final org.opendaylight.yangtools.yang.data.api.InstanceIdentifier BI_NODE_C_ID = createBINodeIdentifier(NODE_C);
72     public static final org.opendaylight.yangtools.yang.data.api.InstanceIdentifier BI_NODE_D_ID = createBINodeIdentifier(NODE_D);
73
74     private static final QName NODE_ID_QNAME = QName.create(Node.QNAME, "id");
75     private static final QName ADD_FLOW_QNAME = QName.create(NodeFlowRemoved.QNAME, "add-flow");
76     private static final QName REMOVE_FLOW_QNAME = QName.create(NodeFlowRemoved.QNAME, "remove-flow");
77     private static final QName UPDATE_FLOW_QNAME = QName.create(NodeFlowRemoved.QNAME, "update-flow");
78
79     @Before
80     public void setup() {
81         BindingBrokerTestFactory testFactory = new BindingBrokerTestFactory();
82         testFactory.setExecutor(MoreExecutors.sameThreadExecutor());
83         testFactory.setStartWithParsedSchema(true);
84         testContext = testFactory.getTestContext();
85
86         testContext.start();
87         baRpcRegistry = testContext.getBindingRpcRegistry();
88         biRpcRegistry = testContext.getDomRpcRegistry();
89         biRpcInvoker = testContext.getDomRpcInvoker();
90         assertNotNull(baRpcRegistry);
91         assertNotNull(biRpcRegistry);
92
93         flowService = MessageCapturingFlowService.create(baRpcRegistry);
94
95     }
96
97     @Test
98     public void bindingRoutedRpcProvider_DomInvokerTest() {
99
100         flowService//
101                 .registerPath(NodeContext.class, BA_NODE_A_ID) //
102                 .registerPath(NodeContext.class, BA_NODE_B_ID) //
103                 .setAddFlowResult(addFlowResult(true, 10));
104
105         SalFlowService baFlowInvoker = baRpcRegistry.getRpcService(SalFlowService.class);
106         assertNotSame(flowService, baFlowInvoker);
107
108         AddFlowInput addFlowA = addFlow(BA_NODE_A_ID) //
109                 .setPriority(100).setBarrier(true).build();
110
111         CompositeNode addFlowDom = toDomRpc(ADD_FLOW_QNAME, addFlowA);
112         assertNotNull(addFlowDom);
113         RpcResult<CompositeNode> domResult = biRpcInvoker.invokeRpc(ADD_FLOW_QNAME, addFlowDom);
114         assertNotNull(domResult);
115         assertTrue("DOM result is successful.", domResult.isSuccessful());
116         assertTrue("Bidning Add Flow RPC was captured.", flowService.getReceivedAddFlows().containsKey(BA_NODE_A_ID));
117         assertEquals(addFlowA, flowService.getReceivedAddFlows().get(BA_NODE_A_ID).iterator().next());
118     }
119
120     public void bindingRpcInvoker_DomRoutedProviderTest() {
121
122     }
123
124     private CompositeNode toDomRpcInput(DataObject addFlowA) {
125         return testContext.getBindingToDomMappingService().toDataDom(addFlowA);
126     }
127
128     @After
129     public void teardown() throws Exception {
130         testContext.close();
131     }
132
133     private static InstanceIdentifier<Node> createBANodeIdentifier(NodeId node) {
134         return InstanceIdentifier.builder(Nodes.class).child(Node.class, new NodeKey(node)).toInstance();
135     }
136
137     private static org.opendaylight.yangtools.yang.data.api.InstanceIdentifier createBINodeIdentifier(NodeId node) {
138         return org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.builder().node(Nodes.QNAME)
139                 .nodeWithKey(Node.QNAME, NODE_ID_QNAME, node.getValue()).toInstance();
140     }
141
142     private Future<RpcResult<AddFlowOutput>> addFlowResult(boolean success, long xid) {
143         AddFlowOutput output = new AddFlowOutputBuilder() //
144                 .setTransactionId(new TransactionId(BigInteger.valueOf(xid))).build();
145         RpcResult<AddFlowOutput> result = Rpcs.getRpcResult(success, output, ImmutableList.<RpcError> of());
146         return Futures.immediateFuture(result);
147     }
148
149     private static AddFlowInputBuilder addFlow(InstanceIdentifier<Node> nodeId) {
150         AddFlowInputBuilder builder = new AddFlowInputBuilder();
151         builder.setNode(new NodeRef(nodeId));
152         return builder;
153     }
154
155     private CompositeNode toDomRpc(QName rpcName, AddFlowInput addFlowA) {
156         return new CompositeNodeTOImpl(rpcName, null,
157                 Collections.<org.opendaylight.yangtools.yang.data.api.Node<?>> singletonList(toDomRpcInput(addFlowA)));
158     }
159 }

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