2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.test.sal.binding.it;
10 import static org.junit.Assert.assertNotNull;
11 import static org.junit.Assert.assertNotSame;
12 import static org.junit.Assert.assertSame;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.times;
15 import static org.mockito.Mockito.verify;
17 import java.math.BigInteger;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ConsumerContext;
22 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
23 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration;
24 import org.opendaylight.controller.sal.binding.api.BindingAwareConsumer;
25 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
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.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 public class RoutedServiceTest extends AbstractTest {
40 private SalFlowService salFlowService1;
41 private SalFlowService salFlowService2;
43 private SalFlowService consumerService;
45 private RoutedRpcRegistration<SalFlowService> firstReg;
46 private RoutedRpcRegistration<SalFlowService> secondReg;
49 public void setUp() throws Exception {
50 salFlowService1 = mock(SalFlowService.class, "First Flow Service");
51 salFlowService2 = mock(SalFlowService.class, "Second Flow Service");
55 public void testServiceRegistration() {
57 assertNotNull(getBroker());
59 BindingAwareProvider provider1 = new AbstractTestProvider() {
62 public void onSessionInitiated(ProviderContext session) {
63 assertNotNull(session);
64 firstReg = session.addRoutedRpcImplementation(SalFlowService.class, salFlowService1);
69 * Register provider 1 with first implementation of SalFlowService -
73 broker.registerProvider(provider1, getBundleContext());
74 assertNotNull("Registration should not be null", firstReg);
75 assertSame(salFlowService1, firstReg.getInstance());
77 BindingAwareProvider provider2 = new AbstractTestProvider() {
80 public void onSessionInitiated(ProviderContext session) {
81 assertNotNull(session);
82 secondReg = session.addRoutedRpcImplementation(SalFlowService.class, salFlowService2);
87 * Register provider 2 with first implementation of SalFlowService -
91 broker.registerProvider(provider2, getBundleContext());
92 assertNotNull("Registration should not be null", firstReg);
93 assertSame(salFlowService2, secondReg.getInstance());
94 assertNotSame(secondReg, firstReg);
96 BindingAwareConsumer consumer = new BindingAwareConsumer() {
98 public void onSessionInitialized(ConsumerContext session) {
99 consumerService = session.getRpcService(SalFlowService.class);
102 broker.registerConsumer(consumer, getBundleContext());
104 assertNotNull("MD-SAL instance of Flow Service should be returned", consumerService);
105 assertNotSame("Provider instance and consumer instance should not be same.", salFlowService1, consumerService);
107 NodeRef nodeOne = createNodeRef("foo:node:1");
110 * Provider 1 registers path of node 1
112 firstReg.registerPath(NodeContext.class, nodeOne.getValue());
115 * Consumer creates addFlow message for node one and sends it to the
119 AddFlowInput addFlowFirstMessage = createSampleAddFlow(nodeOne, 1);
120 consumerService.addFlow(addFlowFirstMessage);
123 * Verifies that implementation of the first provider received the same
124 * message from MD-SAL.
127 verify(salFlowService1).addFlow(addFlowFirstMessage);
130 * Verifies that second instance was not invoked with first message
133 verify(salFlowService2, times(0)).addFlow(addFlowFirstMessage);
136 * Provider 2 registers path of node 2
139 NodeRef nodeTwo = createNodeRef("foo:node:2");
140 secondReg.registerPath(NodeContext.class, nodeTwo.getValue());
143 * Consumer sends message to nodeTwo for three times. Should be
144 * processed by second instance.
146 AddFlowInput AddFlowSecondMessage = createSampleAddFlow(nodeTwo, 2);
147 consumerService.addFlow(AddFlowSecondMessage);
148 consumerService.addFlow(AddFlowSecondMessage);
149 consumerService.addFlow(AddFlowSecondMessage);
152 * Verifies that second instance was invoked 3 times with second message
153 * and first instance wasn't invoked.
156 verify(salFlowService2, times(3)).addFlow(AddFlowSecondMessage);
157 verify(salFlowService1, times(0)).addFlow(AddFlowSecondMessage);
160 * Unregisteration of the path for the node one in the first provider
163 firstReg.unregisterPath(NodeContext.class, nodeOne.getValue());
166 * Provider 2 registers path of node 1
169 secondReg.registerPath(NodeContext.class, nodeOne.getValue());
172 * A consumer sends third message to node 1
175 AddFlowInput AddFlowThirdMessage = createSampleAddFlow(nodeOne, 3);
176 consumerService.addFlow(AddFlowThirdMessage);
179 * Verifies that provider 1 wasn't invoked and provider 2 was invoked 1
182 verify(salFlowService1, times(0)).addFlow(AddFlowThirdMessage);
183 verify(salFlowService2).addFlow(AddFlowThirdMessage);
188 * Returns node reference from string which represents path
191 * string with key(path)
192 * @return instance of the type NodeRef
194 private static NodeRef createNodeRef(String string) {
195 NodeKey key = new NodeKey(new NodeId(string));
196 InstanceIdentifier<Node> path = InstanceIdentifier.builder(Nodes.class).child(Node.class, key).build();
198 return new NodeRef(path);
202 * Creates flow AddFlowInput for which only node and cookie are set
207 * integer with cookie value
208 * @return AddFlowInput instance
210 static AddFlowInput createSampleAddFlow(NodeRef node, int cookie) {
211 AddFlowInputBuilder ret = new AddFlowInputBuilder();
213 ret.setCookie(new FlowCookie(BigInteger.valueOf(cookie)));