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.*;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeContext;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
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.yangtools.yang.binding.InstanceIdentifier;
31 import org.opendaylight.yangtools.yang.common.RpcResult;
33 public class RoutedServiceTest extends AbstractTest {
35 private SalFlowService salFlowService1;
36 private SalFlowService salFlowService2;
38 private SalFlowService consumerService;
40 private RoutedRpcRegistration<SalFlowService> firstReg;
41 private RoutedRpcRegistration<SalFlowService> secondReg;
44 public void setUp() throws Exception {
45 salFlowService1 = mock(SalFlowService.class, "First Flow Service");
46 salFlowService2 = mock(SalFlowService.class, "Second Flow Service");
50 public void testServiceRegistration() {
52 assertNotNull(getBroker());
54 BindingAwareProvider provider1 = new AbstractTestProvider() {
57 public void onSessionInitiated(ProviderContext session) {
58 assertNotNull(session);
59 firstReg = session.addRoutedRpcImplementation(SalFlowService.class, salFlowService1);
64 * Register provider 1 with first implementation of SalFlowService -
68 broker.registerProvider(provider1, getBundleContext());
69 assertNotNull("Registration should not be null", firstReg);
70 assertSame(salFlowService1, firstReg.getInstance());
72 BindingAwareProvider provider2 = new AbstractTestProvider() {
75 public void onSessionInitiated(ProviderContext session) {
76 assertNotNull(session);
77 secondReg = session.addRoutedRpcImplementation(SalFlowService.class, salFlowService2);
82 * Register provider 2 with first implementation of SalFlowService -
86 broker.registerProvider(provider2, getBundleContext());
87 assertNotNull("Registration should not be null", firstReg);
88 assertSame(salFlowService2, secondReg.getInstance());
89 assertNotSame(secondReg, firstReg);
91 BindingAwareConsumer consumer = new BindingAwareConsumer() {
93 public void onSessionInitialized(ConsumerContext session) {
94 consumerService = session.getRpcService(SalFlowService.class);
97 broker.registerConsumer(consumer, getBundleContext());
99 assertNotNull("MD-SAL instance of Flow Service should be returned", consumerService);
100 assertNotSame("Provider instance and consumer instance should not be same.", salFlowService1, consumerService);
102 NodeRef nodeOne = createNodeRef("foo:node:1");
105 * Provider 1 registers path of node 1
107 firstReg.registerPath(NodeContext.class, nodeOne.getValue());
110 * Consumer creates addFlow message for node one and sends it to the
114 AddFlowInput addFlowFirstMessage = createSampleAddFlow(nodeOne, 1);
115 consumerService.addFlow(addFlowFirstMessage);
118 * Verifies that implementation of the first provider received the same
119 * message from MD-SAL.
122 verify(salFlowService1).addFlow(addFlowFirstMessage);
125 * Verifies that second instance was not invoked with first message
128 verify(salFlowService2, times(0)).addFlow(addFlowFirstMessage);
131 * Provider 2 registers path of node 2
134 NodeRef nodeTwo = createNodeRef("foo:node:2");
135 secondReg.registerPath(NodeContext.class, nodeTwo.getValue());
138 * Consumer sends message to nodeTwo for three times. Should be
139 * processed by second instance.
141 AddFlowInput AddFlowSecondMessage = createSampleAddFlow(nodeTwo, 2);
142 consumerService.addFlow(AddFlowSecondMessage);
143 consumerService.addFlow(AddFlowSecondMessage);
144 consumerService.addFlow(AddFlowSecondMessage);
147 * Verifies that second instance was invoked 3 times with second message
148 * and first instance wasn't invoked.
151 verify(salFlowService2, times(3)).addFlow(AddFlowSecondMessage);
152 verify(salFlowService1, times(0)).addFlow(AddFlowSecondMessage);
155 * Unregisteration of the path for the node one in the first provider
158 firstReg.unregisterPath(NodeContext.class, nodeOne.getValue());
161 * Provider 2 registers path of node 1
164 secondReg.registerPath(NodeContext.class, nodeOne.getValue());
167 * A consumer sends third message to node 1
170 AddFlowInput AddFlowThirdMessage = createSampleAddFlow(nodeOne, 3);
171 consumerService.addFlow(AddFlowThirdMessage);
174 * Verifies that provider 1 wasn't invoked and provider 2 was invoked 1
177 verify(salFlowService1, times(0)).addFlow(AddFlowThirdMessage);
178 verify(salFlowService2).addFlow(AddFlowThirdMessage);
183 * Returns node reference from string which represents path
186 * string with key(path)
187 * @return instance of the type NodeRef
189 private static NodeRef createNodeRef(String string) {
190 NodeKey key = new NodeKey(new NodeId(string));
191 InstanceIdentifier<Node> path = InstanceIdentifier.builder().node(Nodes.class).node(Node.class, key)
194 return new NodeRef(path);
198 * Creates flow AddFlowInput for which only node and cookie are set
203 * integer with cookie value
204 * @return AddFlowInput instance
206 static AddFlowInput createSampleAddFlow(NodeRef node, int cookie) {
207 AddFlowInputBuilder ret = new AddFlowInputBuilder();
209 ret.setCookie(BigInteger.valueOf(cookie));