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

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