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;
18 import java.util.concurrent.Future;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ConsumerContext;
23 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
24 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration;
25 import org.opendaylight.controller.sal.binding.api.BindingAwareConsumer;
26 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeContext;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.opendaylight.yangtools.yang.common.RpcResult;
41 public class RoutedServiceTest extends AbstractTest {
43 private SalFlowService salFlowService1;
44 private SalFlowService salFlowService2;
46 private SalFlowService consumerService;
48 private RoutedRpcRegistration<SalFlowService> firstReg;
49 private RoutedRpcRegistration<SalFlowService> secondReg;
52 public void setUp() throws Exception {
53 salFlowService1 = mock(SalFlowService.class, "First Flow Service");
54 salFlowService2 = mock(SalFlowService.class, "Second Flow Service");
58 public void testServiceRegistration() {
60 assertNotNull(getBroker());
62 BindingAwareProvider provider1 = new AbstractTestProvider() {
65 public void onSessionInitiated(ProviderContext session) {
66 assertNotNull(session);
67 firstReg = session.addRoutedRpcImplementation(SalFlowService.class, salFlowService1);
72 * Register provider 1 with first implementation of SalFlowService -
76 broker.registerProvider(provider1, getBundleContext());
77 assertNotNull("Registration should not be null", firstReg);
78 assertSame(salFlowService1, firstReg.getInstance());
80 BindingAwareProvider provider2 = new AbstractTestProvider() {
83 public void onSessionInitiated(ProviderContext session) {
84 assertNotNull(session);
85 secondReg = session.addRoutedRpcImplementation(SalFlowService.class, salFlowService2);
90 * Register provider 2 with first implementation of SalFlowService -
94 broker.registerProvider(provider2, getBundleContext());
95 assertNotNull("Registration should not be null", firstReg);
96 assertSame(salFlowService2, secondReg.getInstance());
97 assertNotSame(secondReg, firstReg);
99 BindingAwareConsumer consumer = new BindingAwareConsumer() {
101 public void onSessionInitialized(ConsumerContext session) {
102 consumerService = session.getRpcService(SalFlowService.class);
105 broker.registerConsumer(consumer, getBundleContext());
107 assertNotNull("MD-SAL instance of Flow Service should be returned", consumerService);
108 assertNotSame("Provider instance and consumer instance should not be same.", salFlowService1, consumerService);
110 NodeRef nodeOne = createNodeRef("foo:node:1");
113 * Provider 1 registers path of node 1
115 firstReg.registerPath(NodeContext.class, nodeOne.getValue());
118 * Consumer creates addFlow message for node one and sends it to the
122 AddFlowInput addFlowFirstMessage = createSampleAddFlow(nodeOne, 1);
123 consumerService.addFlow(addFlowFirstMessage);
126 * Verifies that implementation of the first provider received the same
127 * message from MD-SAL.
130 verify(salFlowService1).addFlow(addFlowFirstMessage);
133 * Verifies that second instance was not invoked with first message
136 verify(salFlowService2, times(0)).addFlow(addFlowFirstMessage);
139 * Provider 2 registers path of node 2
142 NodeRef nodeTwo = createNodeRef("foo:node:2");
143 secondReg.registerPath(NodeContext.class, nodeTwo.getValue());
146 * Consumer sends message to nodeTwo for three times. Should be
147 * processed by second instance.
149 AddFlowInput AddFlowSecondMessage = createSampleAddFlow(nodeTwo, 2);
150 consumerService.addFlow(AddFlowSecondMessage);
151 consumerService.addFlow(AddFlowSecondMessage);
152 consumerService.addFlow(AddFlowSecondMessage);
155 * Verifies that second instance was invoked 3 times with second message
156 * and first instance wasn't invoked.
159 verify(salFlowService2, times(3)).addFlow(AddFlowSecondMessage);
160 verify(salFlowService1, times(0)).addFlow(AddFlowSecondMessage);
163 * Unregisteration of the path for the node one in the first provider
166 firstReg.unregisterPath(NodeContext.class, nodeOne.getValue());
169 * Provider 2 registers path of node 1
172 secondReg.registerPath(NodeContext.class, nodeOne.getValue());
175 * A consumer sends third message to node 1
178 AddFlowInput AddFlowThirdMessage = createSampleAddFlow(nodeOne, 3);
179 consumerService.addFlow(AddFlowThirdMessage);
182 * Verifies that provider 1 wasn't invoked and provider 2 was invoked 1
185 verify(salFlowService1, times(0)).addFlow(AddFlowThirdMessage);
186 verify(salFlowService2).addFlow(AddFlowThirdMessage);
191 * Returns node reference from string which represents path
194 * string with key(path)
195 * @return instance of the type NodeRef
197 private static NodeRef createNodeRef(String string) {
198 NodeKey key = new NodeKey(new NodeId(string));
199 InstanceIdentifier<Node> path = InstanceIdentifier.builder().node(Nodes.class).node(Node.class, key)
202 return new NodeRef(path);
206 * Creates flow AddFlowInput for which only node and cookie are set
211 * integer with cookie value
212 * @return AddFlowInput instance
214 static AddFlowInput createSampleAddFlow(NodeRef node, int cookie) {
215 AddFlowInputBuilder ret = new AddFlowInputBuilder();
217 ret.setCookie(BigInteger.valueOf(cookie));