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