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

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