Refactored implementation of Notification Invoker
[controller.git] / opendaylight / md-sal / sal-binding-it / src / test / java / org / opendaylight / controller / test / sal / binding / it / NoficationTest.java
1 package org.opendaylight.controller.test.sal.binding.it;
2
3 import static org.junit.Assert.*;
4
5 import java.math.BigInteger;
6 import java.util.ArrayList;
7 import java.util.List;
8
9 import org.junit.Before;
10 import org.junit.Test;
11 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ConsumerContext;
12 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
13 import org.opendaylight.controller.sal.binding.api.BindingAwareConsumer;
14 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
15 import org.opendaylight.controller.sal.binding.api.NotificationService;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowAdded;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowAddedBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowRemoved;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowUpdated;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowListener;
21 import org.opendaylight.yangtools.concepts.Registration;
22 import org.opendaylight.yangtools.yang.binding.NotificationListener;
23
24 public class NoficationTest extends AbstractTest {
25
26     private FlowListener listener1 = new FlowListener();
27     private FlowListener listener2 = new FlowListener();
28
29     private Registration<NotificationListener> listener1Reg;
30     private Registration<NotificationListener> listener2Reg;
31
32     private NotificationProviderService notifyService;
33
34     @Before
35     public void setUp() throws Exception {
36     }
37
38     @Test
39     public void notificationTest() throws Exception {
40         /**
41          * 
42          * We register Provider 1 which retrieves Notification Service from MD-SAL
43          * 
44          */
45         AbstractTestProvider provider = new AbstractTestProvider() {
46             @Override
47             public void onSessionInitiated(ProviderContext session) {
48                 notifyService = session.getSALService(NotificationProviderService.class);
49             }
50         };
51         broker.registerProvider(provider, getBundleContext());
52
53         /**
54          * 
55          * We register Consumer 1 which retrieves Notification Service from MD-SAL
56          * and registers SalFlowListener as notification listener
57          * 
58          */
59         BindingAwareConsumer consumer1 = new BindingAwareConsumer() {
60             @Override
61             public void onSessionInitialized(ConsumerContext session) {
62                 NotificationService notificationService = session.getSALService(NotificationService.class);
63                 assertNotNull(notificationService);
64                 listener1Reg = notificationService.registerNotificationListener(listener1);
65             }
66         };
67
68         broker.registerConsumer(consumer1, getBundleContext());
69
70         assertNotNull(listener1Reg);
71
72         /**
73          * We wait 100ms for to make sure broker threads delivered notifications
74          */
75         notifyService.publish(flowAdded(0));
76         Thread.sleep(100);
77         
78         /** 
79          * We verify one notification was delivered
80          * 
81          */
82         assertEquals(1, listener1.addedFlows.size());
83         assertEquals(0, listener1.addedFlows.get(0).getCookie().intValue());
84
85         
86         /**
87          * We also register second consumerm and it's SalFlowListener
88          */
89         BindingAwareConsumer consumer2 = new BindingAwareConsumer() {
90             @Override
91             public void onSessionInitialized(ConsumerContext session) {
92                 listener2Reg = session.getSALService(NotificationProviderService.class).registerNotificationListener(
93                         listener2);
94             }
95         };
96
97         broker.registerConsumer(consumer2, getBundleContext());
98
99         /**
100          * We publish 3 notifications
101          */
102         notifyService.publish(flowAdded(5));
103         notifyService.publish(flowAdded(10));
104         notifyService.publish(flowAdded(2));
105
106         /**
107          * We wait 100ms for to make sure broker threads delivered notifications
108          */
109         Thread.sleep(100);
110         
111         /** 
112          * We verify 3 notification was delivered to both listeners
113          * (first one received 4 total, second 3 in total).
114          * 
115          */
116
117         assertEquals(4, listener1.addedFlows.size());
118         assertEquals(3, listener2.addedFlows.size());
119
120         /**
121          * We close / unregister second listener
122          * 
123          */
124         listener2Reg.close();
125   
126         /**
127          * 
128          * We punblish 5th notification
129          */
130         notifyService.publish(flowAdded(10));
131         
132         /**
133          * We wait 100ms for to make sure broker threads delivered notifications
134          */
135         Thread.sleep(100);
136         
137         /**
138          * We verify that first consumer received 5 notifications in total,
139          * second consumer only three. Last notification was never received,
140          * because it already unregistered listener.
141          * 
142          */
143         assertEquals(5, listener1.addedFlows.size());
144         assertEquals(3, listener2.addedFlows.size());
145
146     }
147
148     public static FlowAdded flowAdded(int i) {
149         FlowAddedBuilder ret = new FlowAddedBuilder();
150         ret.setCookie(BigInteger.valueOf(i));
151         return ret.build();
152     }
153
154     private static class FlowListener implements SalFlowListener {
155
156         List<FlowAdded> addedFlows = new ArrayList<>();
157         List<FlowRemoved> removedFlows = new ArrayList<>();
158         List<FlowUpdated> updatedFlows = new ArrayList<>();
159
160         @Override
161         public void onFlowAdded(FlowAdded notification) {
162             addedFlows.add(notification);
163         }
164
165         @Override
166         public void onFlowRemoved(FlowRemoved notification) {
167             removedFlows.add(notification);
168         };
169
170         @Override
171         public void onFlowUpdated(FlowUpdated notification) {
172             updatedFlows.add(notification);
173         }
174
175     }
176 }