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