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.inventory.rev130819.NodeContext;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 public class RoutedServiceTest extends AbstractTest {
39 private SalFlowService salFlowService1;
40 private SalFlowService salFlowService2;
42 private SalFlowService consumerService;
44 private RoutedRpcRegistration<SalFlowService> firstReg;
45 private RoutedRpcRegistration<SalFlowService> secondReg;
48 public void setUp() throws Exception {
49 salFlowService1 = mock(SalFlowService.class, "First Flow Service");
50 salFlowService2 = mock(SalFlowService.class, "Second Flow Service");
54 public void testServiceRegistration() {
56 assertNotNull(getBroker());
58 BindingAwareProvider provider1 = new AbstractTestProvider() {
61 public void onSessionInitiated(ProviderContext session) {
62 assertNotNull(session);
63 firstReg = session.addRoutedRpcImplementation(SalFlowService.class, salFlowService1);
68 * Register provider 1 with first implementation of SalFlowService -
72 broker.registerProvider(provider1, getBundleContext());
73 assertNotNull("Registration should not be null", firstReg);
74 assertSame(salFlowService1, firstReg.getInstance());
76 BindingAwareProvider provider2 = new AbstractTestProvider() {
79 public void onSessionInitiated(ProviderContext session) {
80 assertNotNull(session);
81 secondReg = session.addRoutedRpcImplementation(SalFlowService.class, salFlowService2);
86 * Register provider 2 with first implementation of SalFlowService -
90 broker.registerProvider(provider2, getBundleContext());
91 assertNotNull("Registration should not be null", firstReg);
92 assertSame(salFlowService2, secondReg.getInstance());
93 assertNotSame(secondReg, firstReg);
95 BindingAwareConsumer consumer = new BindingAwareConsumer() {
97 public void onSessionInitialized(ConsumerContext session) {
98 consumerService = session.getRpcService(SalFlowService.class);
101 broker.registerConsumer(consumer, getBundleContext());
103 assertNotNull("MD-SAL instance of Flow Service should be returned", consumerService);
104 assertNotSame("Provider instance and consumer instance should not be same.", salFlowService1, consumerService);
106 NodeRef nodeOne = createNodeRef("foo:node:1");
109 * Provider 1 registers path of node 1
111 firstReg.registerPath(NodeContext.class, nodeOne.getValue());
114 * Consumer creates addFlow message for node one and sends it to the
118 AddFlowInput addFlowFirstMessage = createSampleAddFlow(nodeOne, 1);
119 consumerService.addFlow(addFlowFirstMessage);
122 * Verifies that implementation of the first provider received the same
123 * message from MD-SAL.
126 verify(salFlowService1).addFlow(addFlowFirstMessage);
129 * Verifies that second instance was not invoked with first message
132 verify(salFlowService2, times(0)).addFlow(addFlowFirstMessage);
135 * Provider 2 registers path of node 2
138 NodeRef nodeTwo = createNodeRef("foo:node:2");
139 secondReg.registerPath(NodeContext.class, nodeTwo.getValue());
142 * Consumer sends message to nodeTwo for three times. Should be
143 * processed by second instance.
145 AddFlowInput AddFlowSecondMessage = createSampleAddFlow(nodeTwo, 2);
146 consumerService.addFlow(AddFlowSecondMessage);
147 consumerService.addFlow(AddFlowSecondMessage);
148 consumerService.addFlow(AddFlowSecondMessage);
151 * Verifies that second instance was invoked 3 times with second message
152 * and first instance wasn't invoked.
155 verify(salFlowService2, times(3)).addFlow(AddFlowSecondMessage);
156 verify(salFlowService1, times(0)).addFlow(AddFlowSecondMessage);
159 * Unregisteration of the path for the node one in the first provider
162 firstReg.unregisterPath(NodeContext.class, nodeOne.getValue());
165 * Provider 2 registers path of node 1
168 secondReg.registerPath(NodeContext.class, nodeOne.getValue());
171 * A consumer sends third message to node 1
174 AddFlowInput AddFlowThirdMessage = createSampleAddFlow(nodeOne, 3);
175 consumerService.addFlow(AddFlowThirdMessage);
178 * Verifies that provider 1 wasn't invoked and provider 2 was invoked 1
181 verify(salFlowService1, times(0)).addFlow(AddFlowThirdMessage);
182 verify(salFlowService2).addFlow(AddFlowThirdMessage);
187 * Returns node reference from string which represents path
190 * string with key(path)
191 * @return instance of the type NodeRef
193 private static NodeRef createNodeRef(String string) {
194 NodeKey key = new NodeKey(new NodeId(string));
195 InstanceIdentifier<Node> path = InstanceIdentifier.builder().node(Nodes.class).node(Node.class, key)
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(BigInteger.valueOf(cookie));