71d64301915fd36f57f829307b5d430c56804881
[controller.git] / opendaylight / md-sal / sal-binding-it / src / test / java / org / opendaylight / controller / test / sal / binding / it / RoutedServiceTest.java
1 package org.opendaylight.controller.test.sal.binding.it;
2
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;
9
10 import java.math.BigInteger;
11 import java.util.concurrent.Future;
12
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;
32
33 public class RoutedServiceTest extends AbstractTest {
34
35     private SalFlowService salFlowService1;
36     private SalFlowService salFlowService2;
37
38     private SalFlowService consumerService;
39
40     private RoutedRpcRegistration<SalFlowService> firstReg;
41     private RoutedRpcRegistration<SalFlowService> secondReg;
42
43     @Before
44     public void setUp() throws Exception {
45         salFlowService1 = mock(SalFlowService.class, "First Flow Service");
46         salFlowService2 = mock(SalFlowService.class, "Second Flow Service");
47     }
48
49     @Test
50     public void testServiceRegistration() {
51
52         assertNotNull(getBroker());
53
54         BindingAwareProvider provider1 = new AbstractTestProvider() {
55
56             @Override
57             public void onSessionInitiated(ProviderContext session) {
58                 assertNotNull(session);
59                 firstReg = session.addRoutedRpcImplementation(SalFlowService.class, salFlowService1);
60             }
61         };
62
63         /**
64          * Register provider 1 with first implementation of SalFlowService -
65          * service1
66          * 
67          */
68         broker.registerProvider(provider1, getBundleContext());
69         assertNotNull("Registration should not be null", firstReg);
70         assertSame(salFlowService1, firstReg.getInstance());
71
72         BindingAwareProvider provider2 = new AbstractTestProvider() {
73
74             @Override
75             public void onSessionInitiated(ProviderContext session) {
76                 assertNotNull(session);
77                 secondReg = session.addRoutedRpcImplementation(SalFlowService.class, salFlowService2);
78             }
79         };
80
81         /**
82          * Register provider 2 with first implementation of SalFlowService -
83          * service2
84          * 
85          */
86         broker.registerProvider(provider2, getBundleContext());
87         assertNotNull("Registration should not be null", firstReg);
88         assertSame(salFlowService2, secondReg.getInstance());
89         assertNotSame(secondReg, firstReg);
90
91         BindingAwareConsumer consumer = new BindingAwareConsumer() {
92             @Override
93             public void onSessionInitialized(ConsumerContext session) {
94                 consumerService = session.getRpcService(SalFlowService.class);
95             }
96         };
97         broker.registerConsumer(consumer, getBundleContext());
98
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);
101
102         NodeRef nodeOne = createNodeRef("foo:node:1");
103
104         /**
105          * Provider 1 registers path of node 1
106          */
107         firstReg.registerPath(NodeContext.class, nodeOne.getValue());
108
109         /**
110          * Consumer creates addFlow message for node one and sends it to the
111          * MD-SAL
112          * 
113          */
114         AddFlowInput addFlowFirstMessage = createSampleAddFlow(nodeOne, 1);
115         consumerService.addFlow(addFlowFirstMessage);
116
117         /**
118          * Verifies that implementation of the first provider received the same
119          * message from MD-SAL.
120          * 
121          */
122         verify(salFlowService1).addFlow(addFlowFirstMessage);
123
124         /**
125          * Verifies that second instance was not invoked with first message
126          * 
127          */
128         verify(salFlowService2, times(0)).addFlow(addFlowFirstMessage);
129
130         /**
131          * Provider 2 registers path of node 2
132          * 
133          */
134         NodeRef nodeTwo = createNodeRef("foo:node:2");
135         secondReg.registerPath(NodeContext.class, nodeTwo.getValue());
136
137         /**
138          * Consumer sends message to nodeTwo for three times. Should be
139          * processed by second instance.
140          */
141         AddFlowInput AddFlowSecondMessage = createSampleAddFlow(nodeTwo, 2);
142         consumerService.addFlow(AddFlowSecondMessage);
143         consumerService.addFlow(AddFlowSecondMessage);
144         consumerService.addFlow(AddFlowSecondMessage);
145
146         /**
147          * Verifies that second instance was invoked 3 times with second message
148          * and first instance wasn't invoked.
149          * 
150          */
151         verify(salFlowService2, times(3)).addFlow(AddFlowSecondMessage);
152         verify(salFlowService1, times(0)).addFlow(AddFlowSecondMessage);
153
154         /**
155          * Unregisteration of the path for the node one in the first provider
156          * 
157          */
158         firstReg.unregisterPath(NodeContext.class, nodeOne.getValue());
159
160         /**
161          * Provider 2 registers path of node 1
162          * 
163          */
164         secondReg.registerPath(NodeContext.class, nodeOne.getValue());
165
166         /**
167          * A consumer sends third message to node 1
168          * 
169          */
170         AddFlowInput AddFlowThirdMessage = createSampleAddFlow(nodeOne, 3);
171         consumerService.addFlow(AddFlowThirdMessage);
172
173         /**
174          * Verifies that provider 1 wasn't invoked and provider 2 was invoked 1
175          * time.
176          */
177         verify(salFlowService1, times(0)).addFlow(AddFlowThirdMessage);
178         verify(salFlowService2).addFlow(AddFlowThirdMessage);
179
180     }
181
182     /**
183      * Returns node reference from string which represents path
184      * 
185      * @param string
186      *            string with key(path)
187      * @return instance of the type NodeRef
188      */
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)
192                 .toInstance();
193
194         return new NodeRef(path);
195     }
196
197     /**
198      * Creates flow AddFlowInput for which only node and cookie are set
199      * 
200      * @param node
201      *            NodeRef value
202      * @param cookie
203      *            integer with cookie value
204      * @return AddFlowInput instance
205      */
206     static AddFlowInput createSampleAddFlow(NodeRef node, int cookie) {
207         AddFlowInputBuilder ret = new AddFlowInputBuilder();
208         ret.setNode(node);
209         ret.setCookie(BigInteger.valueOf(cookie));
210         return ret.build();
211     }
212 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.