1 package org.opendaylight.controller.test.sal.binding.it;
3 import static org.junit.Assert.assertNotNull;
4 import static org.junit.Assert.assertNotSame;
5 import static org.junit.Assert.assertSame;
6 import static org.mockito.Mockito.mock;
7 import static org.mockito.Mockito.times;
8 import static org.mockito.Mockito.verify;
10 import java.math.BigInteger;
11 import java.util.concurrent.Future;
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ConsumerContext;
16 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
17 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration;
18 import org.opendaylight.controller.sal.binding.api.BindingAwareConsumer;
19 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
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.SalFlowService;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeContext;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.opendaylight.yangtools.yang.common.RpcResult;
34 public class RoutedServiceTest extends AbstractTest {
36 private SalFlowService salFlowService1;
37 private SalFlowService salFlowService2;
39 private SalFlowService consumerService;
41 private RoutedRpcRegistration<SalFlowService> firstReg;
42 private RoutedRpcRegistration<SalFlowService> secondReg;
45 public void setUp() throws Exception {
46 salFlowService1 = mock(SalFlowService.class, "First Flow Service");
47 salFlowService2 = mock(SalFlowService.class, "Second Flow Service");
51 public void testServiceRegistration() {
53 assertNotNull(getBroker());
55 BindingAwareProvider provider1 = new AbstractTestProvider() {
58 public void onSessionInitiated(ProviderContext session) {
59 assertNotNull(session);
60 firstReg = session.addRoutedRpcImplementation(SalFlowService.class, salFlowService1);
65 * Register provider 1 with first implementation of SalFlowService -
69 broker.registerProvider(provider1, getBundleContext());
70 assertNotNull("Registration should not be null", firstReg);
71 assertSame(salFlowService1, firstReg.getInstance());
73 BindingAwareProvider provider2 = new AbstractTestProvider() {
76 public void onSessionInitiated(ProviderContext session) {
77 assertNotNull(session);
78 secondReg = session.addRoutedRpcImplementation(SalFlowService.class, salFlowService2);
83 * Register provider 2 with first implementation of SalFlowService -
87 broker.registerProvider(provider2, getBundleContext());
88 assertNotNull("Registration should not be null", firstReg);
89 assertSame(salFlowService2, secondReg.getInstance());
90 assertNotSame(secondReg, firstReg);
92 BindingAwareConsumer consumer = new BindingAwareConsumer() {
94 public void onSessionInitialized(ConsumerContext session) {
95 consumerService = session.getRpcService(SalFlowService.class);
98 broker.registerConsumer(consumer, getBundleContext());
100 assertNotNull("MD-SAL instance of Flow Service should be returned", consumerService);
101 assertNotSame("Provider instance and consumer instance should not be same.", salFlowService1, consumerService);
103 NodeRef nodeOne = createNodeRef("foo:node:1");
106 * Provider 1 registers path of node 1
108 firstReg.registerPath(NodeContext.class, nodeOne.getValue());
111 * Consumer creates addFlow message for node one and sends it to the
115 AddFlowInput addFlowFirstMessage = createSampleAddFlow(nodeOne, 1);
116 consumerService.addFlow(addFlowFirstMessage);
119 * Verifies that implementation of the first provider received the same
120 * message from MD-SAL.
123 verify(salFlowService1).addFlow(addFlowFirstMessage);
126 * Verifies that second instance was not invoked with first message
129 verify(salFlowService2, times(0)).addFlow(addFlowFirstMessage);
132 * Provider 2 registers path of node 2
135 NodeRef nodeTwo = createNodeRef("foo:node:2");
136 secondReg.registerPath(NodeContext.class, nodeTwo.getValue());
139 * Consumer sends message to nodeTwo for three times. Should be
140 * processed by second instance.
142 AddFlowInput AddFlowSecondMessage = createSampleAddFlow(nodeTwo, 2);
143 consumerService.addFlow(AddFlowSecondMessage);
144 consumerService.addFlow(AddFlowSecondMessage);
145 consumerService.addFlow(AddFlowSecondMessage);
148 * Verifies that second instance was invoked 3 times with second message
149 * and first instance wasn't invoked.
152 verify(salFlowService2, times(3)).addFlow(AddFlowSecondMessage);
153 verify(salFlowService1, times(0)).addFlow(AddFlowSecondMessage);
156 * Unregisteration of the path for the node one in the first provider
159 firstReg.unregisterPath(NodeContext.class, nodeOne.getValue());
162 * Provider 2 registers path of node 1
165 secondReg.registerPath(NodeContext.class, nodeOne.getValue());
168 * A consumer sends third message to node 1
171 AddFlowInput AddFlowThirdMessage = createSampleAddFlow(nodeOne, 3);
172 consumerService.addFlow(AddFlowThirdMessage);
175 * Verifies that provider 1 wasn't invoked and provider 2 was invoked 1
178 verify(salFlowService1, times(0)).addFlow(AddFlowThirdMessage);
179 verify(salFlowService2).addFlow(AddFlowThirdMessage);
184 * Returns node reference from string which represents path
187 * string with key(path)
188 * @return instance of the type NodeRef
190 private static NodeRef createNodeRef(String string) {
191 NodeKey key = new NodeKey(new NodeId(string));
192 InstanceIdentifier<Node> path = InstanceIdentifier.builder().node(Nodes.class).node(Node.class, key)
195 return new NodeRef(path);
199 * Creates flow AddFlowInput for which only node and cookie are set
204 * integer with cookie value
205 * @return AddFlowInput instance
207 static AddFlowInput createSampleAddFlow(NodeRef node, int cookie) {
208 AddFlowInputBuilder ret = new AddFlowInputBuilder();
210 ret.setCookie(BigInteger.valueOf(cookie));