Merge "Increase timeout for waiting for broker service in sal-binding-it."
[controller.git] / opendaylight / md-sal / sal-binding-it / src / test / java / org / opendaylight / controller / test / sal / binding / it / NoficationTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.test.sal.binding.it;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12
13 import java.math.BigInteger;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.List;
17
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ConsumerContext;
21 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
22 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider.ProviderFunctionality;
23 import org.opendaylight.controller.sal.binding.api.BindingAwareConsumer;
24 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
25 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
26 import org.opendaylight.controller.sal.binding.api.NotificationService;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowAdded;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowAddedBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowRemoved;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowUpdated;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeErrorNotification;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeExperimenterErrorNotification;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowListener;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SwitchFlowRemoved;
35 import org.opendaylight.yangtools.concepts.Registration;
36 import org.opendaylight.yangtools.yang.binding.NotificationListener;
37 import org.opendaylight.yangtools.yang.binding.RpcService;
38
39 public class NoficationTest extends AbstractTest {
40
41     private FlowListener listener1 = new FlowListener();
42     private FlowListener listener2 = new FlowListener();
43
44     private Registration<NotificationListener> listener1Reg;
45     private Registration<NotificationListener> listener2Reg;
46
47     private NotificationProviderService notifyProviderService;
48
49     @Before
50     public void setUp() throws Exception {
51     }
52
53     @Test
54     public void notificationTest() throws Exception {
55         /**
56          * 
57          * The registration of the Provider 1.
58          * 
59          */
60         AbstractTestProvider provider1 = new AbstractTestProvider() {
61             @Override
62             public void onSessionInitiated(ProviderContext session) {
63                 notifyProviderService = session.getSALService(NotificationProviderService.class);
64             }
65         };
66
67         // registerProvider method calls onSessionInitiated method above
68         broker.registerProvider(provider1, getBundleContext());
69         assertNotNull(notifyProviderService);
70
71         /**
72          * 
73          * The registration of the Consumer 1. It retrieves Notification Service
74          * from MD-SAL and registers SalFlowListener as notification listener
75          * 
76          */
77         BindingAwareConsumer consumer1 = new BindingAwareConsumer() {
78             @Override
79             public void onSessionInitialized(ConsumerContext session) {
80                 NotificationService notificationService = session.getSALService(NotificationService.class);
81                 assertNotNull(notificationService);
82                 listener1Reg = notificationService.registerNotificationListener(listener1);
83             }
84         };
85         // registerConsumer method calls onSessionInitialized method above
86         broker.registerConsumer(consumer1, getBundleContext());
87
88         assertNotNull(listener1Reg);
89
90         /**
91          * The notification of type FlowAdded with cookie ID 0 is created. The
92          * delay 100ms to make sure that the notification was delivered to
93          * listener.
94          */
95         notifyProviderService.publish(flowAdded(0));
96         Thread.sleep(100);
97
98         /**
99          * Check that one notification was delivered and has correct cookie.
100          * 
101          */
102         assertEquals(1, listener1.addedFlows.size());
103         assertEquals(0, listener1.addedFlows.get(0).getCookie().intValue());
104
105         /**
106          * The registration of the Consumer 2. SalFlowListener is registered
107          * registered as notification listener.
108          */
109         BindingAwareProvider provider = new BindingAwareProvider() {
110             
111             @Override
112             public void onSessionInitiated(ProviderContext session) {
113                 listener2Reg = session.getSALService(NotificationProviderService.class).registerNotificationListener(
114                         listener2);
115             }
116             
117             @Override
118             public void onSessionInitialized(ConsumerContext session) {
119                 // TODO Auto-generated method stub
120                 
121             }
122             
123             @Override
124             public Collection<? extends RpcService> getImplementations() {
125                 // TODO Auto-generated method stub
126                 return null;
127             }
128             
129             @Override
130             public Collection<? extends ProviderFunctionality> getFunctionality() {
131                 // TODO Auto-generated method stub
132                 return null;
133             }
134             
135         };
136
137         // registerConsumer method calls onSessionInitialized method above
138         broker.registerProvider(provider, getBundleContext());
139
140         /**
141          * 3 notifications are published
142          */
143         notifyProviderService.publish(flowAdded(5));
144         notifyProviderService.publish(flowAdded(10));
145         notifyProviderService.publish(flowAdded(2));
146
147         /**
148          * The delay 100ms to make sure that the notifications were delivered to
149          * listeners.
150          */
151         Thread.sleep(100);
152
153         /**
154          * Check that 3 notification was delivered to both listeners (first one
155          * received 4 in total, second 3 in total).
156          * 
157          */
158         assertEquals(4, listener1.addedFlows.size());
159         assertEquals(3, listener2.addedFlows.size());
160
161         /**
162          * The second listener is closed (unregistered)
163          * 
164          */
165         listener2Reg.close();
166
167         /**
168          * 
169          * The notification 5 is published
170          */
171         notifyProviderService.publish(flowAdded(10));
172
173         /**
174          * The delay 100ms to make sure that the notification was delivered to
175          * listener.
176          */
177         Thread.sleep(100);
178
179         /**
180          * Check that first consumer received 5 notifications in total, second
181          * consumer received only three. Last notification was never received by
182          * second consumer because its listener was unregistered.
183          * 
184          */
185         assertEquals(5, listener1.addedFlows.size());
186         assertEquals(3, listener2.addedFlows.size());
187
188     }
189
190     /**
191      * Creates instance of the type FlowAdded. Only cookie value is set. It is
192      * used only for testing purpose.
193      * 
194      * @param i
195      *            cookie value
196      * @return instance of the type FlowAdded
197      */
198     public static FlowAdded flowAdded(int i) {
199         FlowAddedBuilder ret = new FlowAddedBuilder();
200         ret.setCookie(BigInteger.valueOf(i));
201         return ret.build();
202     }
203
204     /**
205      * 
206      * Implements
207      * {@link org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowListener
208      * SalFlowListener} and contains attributes which keep lists of objects of
209      * the type
210      * {@link org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819. NodeFlow
211      * NodeFlow}. The lists are defined for flows which were added, removed or
212      * updated.
213      */
214     private static class FlowListener implements SalFlowListener {
215
216         List<FlowAdded> addedFlows = new ArrayList<>();
217         List<FlowRemoved> removedFlows = new ArrayList<>();
218         List<FlowUpdated> updatedFlows = new ArrayList<>();
219
220         @Override
221         public void onFlowAdded(FlowAdded notification) {
222             addedFlows.add(notification);
223         }
224
225         @Override
226         public void onFlowRemoved(FlowRemoved notification) {
227             removedFlows.add(notification);
228         }; 
229
230         @Override
231         public void onFlowUpdated(FlowUpdated notification) {
232             updatedFlows.add(notification);
233         }
234
235         @Override
236         public void onSwitchFlowRemoved(SwitchFlowRemoved notification) {
237             // TODO Auto-generated method stub
238             
239         }
240
241         @Override
242         public void onNodeErrorNotification(NodeErrorNotification notification) {
243             // TODO Auto-generated method stub
244             
245         }
246
247         @Override
248         public void onNodeExperimenterErrorNotification(
249                 NodeExperimenterErrorNotification notification) {
250             // TODO Auto-generated method stub
251             
252         }
253
254     }
255 }