1 package org.opendaylight.controller.test.sal.binding.it;
3 import static org.junit.Assert.*;
5 import java.math.BigInteger;
6 import java.util.ArrayList;
9 import org.junit.Before;
10 import org.junit.Test;
11 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ConsumerContext;
12 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
13 import org.opendaylight.controller.sal.binding.api.BindingAwareConsumer;
14 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
15 import org.opendaylight.controller.sal.binding.api.NotificationService;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowAdded;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowAddedBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowRemoved;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowUpdated;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowListener;
21 import org.opendaylight.yangtools.concepts.Registration;
22 import org.opendaylight.yangtools.yang.binding.NotificationListener;
24 public class NoficationTest extends AbstractTest {
26 private FlowListener listener1 = new FlowListener();
27 private FlowListener listener2 = new FlowListener();
29 private Registration<NotificationListener> listener1Reg;
30 private Registration<NotificationListener> listener2Reg;
32 private NotificationProviderService notifyService;
35 public void setUp() throws Exception {
39 public void notificationTest() throws Exception {
42 * We register Provider 1 which retrieves Notification Service from MD-SAL
45 AbstractTestProvider provider = new AbstractTestProvider() {
47 public void onSessionInitiated(ProviderContext session) {
48 notifyService = session.getSALService(NotificationProviderService.class);
51 broker.registerProvider(provider, getBundleContext());
55 * We register Consumer 1 which retrieves Notification Service from MD-SAL
56 * and registers SalFlowListener as notification listener
59 BindingAwareConsumer consumer1 = new BindingAwareConsumer() {
61 public void onSessionInitialized(ConsumerContext session) {
62 NotificationService notificationService = session.getSALService(NotificationService.class);
63 assertNotNull(notificationService);
64 listener1Reg = notificationService.registerNotificationListener(listener1);
68 broker.registerConsumer(consumer1, getBundleContext());
70 assertNotNull(listener1Reg);
73 * We wait 100ms for to make sure broker threads delivered notifications
75 notifyService.publish(flowAdded(0));
79 * We verify one notification was delivered
82 assertEquals(1, listener1.addedFlows.size());
83 assertEquals(0, listener1.addedFlows.get(0).getCookie().intValue());
87 * We also register second consumerm and it's SalFlowListener
89 BindingAwareConsumer consumer2 = new BindingAwareConsumer() {
91 public void onSessionInitialized(ConsumerContext session) {
92 listener2Reg = session.getSALService(NotificationProviderService.class).registerNotificationListener(
97 broker.registerConsumer(consumer2, getBundleContext());
100 * We publish 3 notifications
102 notifyService.publish(flowAdded(5));
103 notifyService.publish(flowAdded(10));
104 notifyService.publish(flowAdded(2));
107 * We wait 100ms for to make sure broker threads delivered notifications
112 * We verify 3 notification was delivered to both listeners
113 * (first one received 4 total, second 3 in total).
117 assertEquals(4, listener1.addedFlows.size());
118 assertEquals(3, listener2.addedFlows.size());
121 * We close / unregister second listener
124 listener2Reg.close();
128 * We punblish 5th notification
130 notifyService.publish(flowAdded(10));
133 * We wait 100ms for to make sure broker threads delivered notifications
138 * We verify that first consumer received 5 notifications in total,
139 * second consumer only three. Last notification was never received,
140 * because it already unregistered listener.
143 assertEquals(5, listener1.addedFlows.size());
144 assertEquals(3, listener2.addedFlows.size());
148 public static FlowAdded flowAdded(int i) {
149 FlowAddedBuilder ret = new FlowAddedBuilder();
150 ret.setCookie(BigInteger.valueOf(i));
154 private static class FlowListener implements SalFlowListener {
156 List<FlowAdded> addedFlows = new ArrayList<>();
157 List<FlowRemoved> removedFlows = new ArrayList<>();
158 List<FlowUpdated> updatedFlows = new ArrayList<>();
161 public void onFlowAdded(FlowAdded notification) {
162 addedFlows.add(notification);
166 public void onFlowRemoved(FlowRemoved notification) {
167 removedFlows.add(notification);
171 public void onFlowUpdated(FlowUpdated notification) {
172 updatedFlows.add(notification);