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;
8 import java.util.Collection;
11 import org.junit.Before;
12 import org.junit.Test;
13 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ConsumerContext;
14 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
15 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider.ProviderFunctionality;
16 import org.opendaylight.controller.sal.binding.api.BindingAwareConsumer;
17 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
18 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
19 import org.opendaylight.controller.sal.binding.api.NotificationService;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowAdded;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowAddedBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowRemoved;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowUpdated;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeErrorNotification;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeExperimenterErrorNotification;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowListener;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SwitchFlowRemoved;
28 import org.opendaylight.yangtools.concepts.Registration;
29 import org.opendaylight.yangtools.yang.binding.NotificationListener;
30 import org.opendaylight.yangtools.yang.binding.RpcService;
32 public class NoficationTest extends AbstractTest {
34 private FlowListener listener1 = new FlowListener();
35 private FlowListener listener2 = new FlowListener();
37 private Registration<NotificationListener> listener1Reg;
38 private Registration<NotificationListener> listener2Reg;
40 private NotificationProviderService notifyProviderService;
43 public void setUp() throws Exception {
47 public void notificationTest() throws Exception {
50 * The registration of the Provider 1.
53 AbstractTestProvider provider1 = new AbstractTestProvider() {
55 public void onSessionInitiated(ProviderContext session) {
56 notifyProviderService = session.getSALService(NotificationProviderService.class);
60 // registerProvider method calls onSessionInitiated method above
61 broker.registerProvider(provider1, getBundleContext());
62 assertNotNull(notifyProviderService);
66 * The registration of the Consumer 1. It retrieves Notification Service
67 * from MD-SAL and registers SalFlowListener as notification listener
70 BindingAwareConsumer consumer1 = new BindingAwareConsumer() {
72 public void onSessionInitialized(ConsumerContext session) {
73 NotificationService notificationService = session.getSALService(NotificationService.class);
74 assertNotNull(notificationService);
75 listener1Reg = notificationService.registerNotificationListener(listener1);
78 // registerConsumer method calls onSessionInitialized method above
79 broker.registerConsumer(consumer1, getBundleContext());
81 assertNotNull(listener1Reg);
84 * The notification of type FlowAdded with cookie ID 0 is created. The
85 * delay 100ms to make sure that the notification was delivered to
88 notifyProviderService.publish(flowAdded(0));
92 * Check that one notification was delivered and has correct cookie.
95 assertEquals(1, listener1.addedFlows.size());
96 assertEquals(0, listener1.addedFlows.get(0).getCookie().intValue());
99 * The registration of the Consumer 2. SalFlowListener is registered
100 * registered as notification listener.
102 BindingAwareProvider provider = new BindingAwareProvider() {
105 public void onSessionInitiated(ProviderContext session) {
106 listener2Reg = session.getSALService(NotificationProviderService.class).registerNotificationListener(
111 public void onSessionInitialized(ConsumerContext session) {
112 // TODO Auto-generated method stub
117 public Collection<? extends RpcService> getImplementations() {
118 // TODO Auto-generated method stub
123 public Collection<? extends ProviderFunctionality> getFunctionality() {
124 // TODO Auto-generated method stub
130 // registerConsumer method calls onSessionInitialized method above
131 broker.registerProvider(provider, getBundleContext());
134 * 3 notifications are published
136 notifyProviderService.publish(flowAdded(5));
137 notifyProviderService.publish(flowAdded(10));
138 notifyProviderService.publish(flowAdded(2));
141 * The delay 100ms to make sure that the notifications were delivered to
147 * Check that 3 notification was delivered to both listeners (first one
148 * received 4 in total, second 3 in total).
151 assertEquals(4, listener1.addedFlows.size());
152 assertEquals(3, listener2.addedFlows.size());
155 * The second listener is closed (unregistered)
158 listener2Reg.close();
162 * The notification 5 is published
164 notifyProviderService.publish(flowAdded(10));
167 * The delay 100ms to make sure that the notification was delivered to
173 * Check that first consumer received 5 notifications in total, second
174 * consumer received only three. Last notification was never received by
175 * second consumer because its listener was unregistered.
178 assertEquals(5, listener1.addedFlows.size());
179 assertEquals(3, listener2.addedFlows.size());
184 * Creates instance of the type FlowAdded. Only cookie value is set. It is
185 * used only for testing purpose.
189 * @return instance of the type FlowAdded
191 public static FlowAdded flowAdded(int i) {
192 FlowAddedBuilder ret = new FlowAddedBuilder();
193 ret.setCookie(BigInteger.valueOf(i));
200 * {@link org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowListener
201 * SalFlowListener} and contains attributes which keep lists of objects of
203 * {@link org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819. NodeFlow
204 * NodeFlow}. The lists are defined for flows which were added, removed or
207 private static class FlowListener implements SalFlowListener {
209 List<FlowAdded> addedFlows = new ArrayList<>();
210 List<FlowRemoved> removedFlows = new ArrayList<>();
211 List<FlowUpdated> updatedFlows = new ArrayList<>();
214 public void onFlowAdded(FlowAdded notification) {
215 addedFlows.add(notification);
219 public void onFlowRemoved(FlowRemoved notification) {
220 removedFlows.add(notification);
224 public void onFlowUpdated(FlowUpdated notification) {
225 updatedFlows.add(notification);
229 public void onSwitchFlowRemoved(SwitchFlowRemoved notification) {
230 // TODO Auto-generated method stub
235 public void onNodeErrorNotification(NodeErrorNotification notification) {
236 // TODO Auto-generated method stub
241 public void onNodeExperimenterErrorNotification(
242 NodeExperimenterErrorNotification notification) {
243 // TODO Auto-generated method stub