fdae91670c74ccd4ac5498f5bc4cd45ce2582e17
[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.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5
6 import java.math.BigInteger;
7 import java.util.ArrayList;
8 import java.util.List;
9
10 import org.junit.Before;
11 import org.junit.Test;
12 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ConsumerContext;
13 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
14 import org.opendaylight.controller.sal.binding.api.*;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.*;
16 import org.opendaylight.yangtools.concepts.Registration;
17 import org.opendaylight.yangtools.yang.binding.NotificationListener;
18
19 public class NoficationTest extends AbstractTest {
20
21     private FlowListener listener1 = new FlowListener();
22     private FlowListener listener2 = new FlowListener();
23
24     private Registration<NotificationListener> listener1Reg;
25     private Registration<NotificationListener> listener2Reg;
26
27     private NotificationProviderService notifyProviderService;
28
29     @Before
30     public void setUp() throws Exception {
31     }
32
33     @Test
34     public void notificationTest() throws Exception {
35         /**
36          * 
37          * The registration of the Provider 1.
38          * 
39          */
40         AbstractTestProvider provider1 = new AbstractTestProvider() {
41             @Override
42             public void onSessionInitiated(ProviderContext session) {
43                 notifyProviderService = session.getSALService(NotificationProviderService.class);
44             }
45         };
46
47         // registerProvider method calls onSessionInitiated method above
48         broker.registerProvider(provider1, getBundleContext());
49         assertNotNull(notifyProviderService);
50
51         /**
52          * 
53          * The registration of the Consumer 1. It retrieves Notification Service
54          * from MD-SAL and registers SalFlowListener as notification listener
55          * 
56          */
57         BindingAwareConsumer consumer1 = new BindingAwareConsumer() {
58             @Override
59             public void onSessionInitialized(ConsumerContext session) {
60                 NotificationService notificationService = session.getSALService(NotificationService.class);
61                 assertNotNull(notificationService);
62                 listener1Reg = notificationService.registerNotificationListener(listener1);
63             }
64         };
65         // registerConsumer method calls onSessionInitialized method above
66         broker.registerConsumer(consumer1, getBundleContext());
67
68         assertNotNull(listener1Reg);
69
70         /**
71          * The notification of type FlowAdded with cookie ID 0 is created. The
72          * delay 100ms to make sure that the notification was delivered to
73          * listener.
74          */
75         notifyProviderService.publish(flowAdded(0));
76         Thread.sleep(100);
77
78         /**
79          * Check that one notification was delivered and has correct cookie.
80          * 
81          */
82         assertEquals(1, listener1.addedFlows.size());
83         assertEquals(0, listener1.addedFlows.get(0).getCookie().intValue());
84
85         /**
86          * The registration of the Consumer 2. SalFlowListener is registered
87          * registered as notification listener.
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         // registerConsumer method calls onSessionInitialized method above
98         broker.registerConsumer(consumer2, getBundleContext());
99
100         /**
101          * 3 notifications are published
102          */
103         notifyProviderService.publish(flowAdded(5));
104         notifyProviderService.publish(flowAdded(10));
105         notifyProviderService.publish(flowAdded(2));
106
107         /**
108          * The delay 100ms to make sure that the notifications were delivered to
109          * listeners.
110          */
111         Thread.sleep(100);
112
113         /**
114          * Check that 3 notification was delivered to both listeners (first one
115          * received 4 in total, second 3 in total).
116          * 
117          */
118         assertEquals(4, listener1.addedFlows.size());
119         assertEquals(3, listener2.addedFlows.size());
120
121         /**
122          * The second listener is closed (unregistered)
123          * 
124          */
125         listener2Reg.close();
126
127         /**
128          * 
129          * The notification 5 is published
130          */
131         notifyProviderService.publish(flowAdded(10));
132
133         /**
134          * The delay 100ms to make sure that the notification was delivered to
135          * listener.
136          */
137         Thread.sleep(100);
138
139         /**
140          * Check that first consumer received 5 notifications in total, second
141          * consumer received only three. Last notification was never received by
142          * second consumer because its listener was unregistered.
143          * 
144          */
145         assertEquals(5, listener1.addedFlows.size());
146         assertEquals(3, listener2.addedFlows.size());
147
148     }
149
150     /**
151      * Creates instance of the type FlowAdded. Only cookie value is set. It is
152      * used only for testing purpose.
153      * 
154      * @param i
155      *            cookie value
156      * @return instance of the type FlowAdded
157      */
158     public static FlowAdded flowAdded(int i) {
159         FlowAddedBuilder ret = new FlowAddedBuilder();
160         ret.setCookie(BigInteger.valueOf(i));
161         return ret.build();
162     }
163
164     /**
165      * 
166      * Implements
167      * {@link org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowListener
168      * SalFlowListener} and contains attributes which keep lists of objects of
169      * the type
170      * {@link org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819. NodeFlow
171      * NodeFlow}. The lists are defined for flows which were added, removed or
172      * updated.
173      */
174     private static class FlowListener implements SalFlowListener {
175
176         List<FlowAdded> addedFlows = new ArrayList<>();
177         List<FlowRemoved> removedFlows = new ArrayList<>();
178         List<FlowUpdated> updatedFlows = new ArrayList<>();
179
180         @Override
181         public void onFlowAdded(FlowAdded notification) {
182             addedFlows.add(notification);
183         }
184
185         @Override
186         public void onFlowRemoved(FlowRemoved notification) {
187             removedFlows.add(notification);
188         };
189
190         @Override
191         public void onFlowUpdated(FlowUpdated notification) {
192             updatedFlows.add(notification);
193         }
194
195     }
196 }