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