1 package org.opendaylight.controller.test.sal.binding.it;
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
6 import java.math.BigInteger;
7 import java.util.ArrayList;
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;
19 public class NoficationTest extends AbstractTest {
21 private FlowListener listener1 = new FlowListener();
22 private FlowListener listener2 = new FlowListener();
24 private Registration<NotificationListener> listener1Reg;
25 private Registration<NotificationListener> listener2Reg;
27 private NotificationProviderService notifyProviderService;
30 public void setUp() throws Exception {
34 public void notificationTest() throws Exception {
37 * The registration of the Provider 1.
40 AbstractTestProvider provider1 = new AbstractTestProvider() {
42 public void onSessionInitiated(ProviderContext session) {
43 notifyProviderService = session.getSALService(NotificationProviderService.class);
47 // registerProvider method calls onSessionInitiated method above
48 broker.registerProvider(provider1, getBundleContext());
49 assertNotNull(notifyProviderService);
53 * The registration of the Consumer 1. It retrieves Notification Service
54 * from MD-SAL and registers SalFlowListener as notification listener
57 BindingAwareConsumer consumer1 = new BindingAwareConsumer() {
59 public void onSessionInitialized(ConsumerContext session) {
60 NotificationService notificationService = session.getSALService(NotificationService.class);
61 assertNotNull(notificationService);
62 listener1Reg = notificationService.registerNotificationListener(listener1);
65 // registerConsumer method calls onSessionInitialized method above
66 broker.registerConsumer(consumer1, getBundleContext());
68 assertNotNull(listener1Reg);
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
75 notifyProviderService.publish(flowAdded(0));
79 * Check that one notification was delivered and has correct cookie.
82 assertEquals(1, listener1.addedFlows.size());
83 assertEquals(0, listener1.addedFlows.get(0).getCookie().intValue());
86 * The registration of the Consumer 2. SalFlowListener is registered
87 * registered as notification listener.
89 BindingAwareConsumer consumer2 = new BindingAwareConsumer() {
91 public void onSessionInitialized(ConsumerContext session) {
92 listener2Reg = session.getSALService(NotificationProviderService.class).registerNotificationListener(
97 // registerConsumer method calls onSessionInitialized method above
98 broker.registerConsumer(consumer2, getBundleContext());
101 * 3 notifications are published
103 notifyProviderService.publish(flowAdded(5));
104 notifyProviderService.publish(flowAdded(10));
105 notifyProviderService.publish(flowAdded(2));
108 * The delay 100ms to make sure that the notifications were delivered to
114 * Check that 3 notification was delivered to both listeners (first one
115 * received 4 in total, second 3 in total).
118 assertEquals(4, listener1.addedFlows.size());
119 assertEquals(3, listener2.addedFlows.size());
122 * The second listener is closed (unregistered)
125 listener2Reg.close();
129 * The notification 5 is published
131 notifyProviderService.publish(flowAdded(10));
134 * The delay 100ms to make sure that the notification was delivered to
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.
145 assertEquals(5, listener1.addedFlows.size());
146 assertEquals(3, listener2.addedFlows.size());
151 * Creates instance of the type FlowAdded. Only cookie value is set. It is
152 * used only for testing purpose.
156 * @return instance of the type FlowAdded
158 public static FlowAdded flowAdded(int i) {
159 FlowAddedBuilder ret = new FlowAddedBuilder();
160 ret.setCookie(BigInteger.valueOf(i));
167 * {@link org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowListener
168 * SalFlowListener} and contains attributes which keep lists of objects of
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
174 private static class FlowListener implements SalFlowListener {
176 List<FlowAdded> addedFlows = new ArrayList<>();
177 List<FlowRemoved> removedFlows = new ArrayList<>();
178 List<FlowUpdated> updatedFlows = new ArrayList<>();
181 public void onFlowAdded(FlowAdded notification) {
182 addedFlows.add(notification);
186 public void onFlowRemoved(FlowRemoved notification) {
187 removedFlows.add(notification);
191 public void onFlowUpdated(FlowUpdated notification) {
192 updatedFlows.add(notification);