2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.test.sal.binding.it;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
13 import java.util.ArrayList;
14 import java.util.List;
16 import org.junit.Test;
17 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ConsumerContext;
18 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
19 import org.opendaylight.controller.sal.binding.api.BindingAwareConsumer;
20 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
21 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
22 import org.opendaylight.controller.sal.binding.api.NotificationService;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.bi.ba.notification.rev150205.OpendaylightTestNotificationListener;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.bi.ba.notification.rev150205.OutOfPixieDustNotification;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.bi.ba.notification.rev150205.OutOfPixieDustNotificationBuilder;
26 import org.opendaylight.yangtools.concepts.ListenerRegistration;
27 import org.opendaylight.yangtools.yang.binding.NotificationListener;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * covers registering of notification listener, publishing of notification and receiving of notification.
34 public class NotificationIT extends AbstractIT {
36 private static final Logger LOG = LoggerFactory
37 .getLogger(NotificationIT.class);
39 protected final NotificationTestListener listener1 = new NotificationTestListener();
40 protected final NotificationTestListener listener2 = new NotificationTestListener();
42 protected ListenerRegistration<NotificationListener> listener1Reg;
43 protected ListenerRegistration<NotificationListener> listener2Reg;
45 protected NotificationProviderService notifyProviderService;
48 * test of delivering of notification
52 public void notificationTest() throws Exception {
53 LOG.info("The registration of the Provider 1.");
54 AbstractTestProvider provider1 = new AbstractTestProvider() {
56 public void onSessionInitiated(ProviderContext session) {
57 notifyProviderService = session.getSALService(NotificationProviderService.class);
61 // registerProvider method calls onSessionInitiated method above
62 broker.registerProvider(provider1);
63 assertNotNull(notifyProviderService);
65 LOG.info("The registration of the Consumer 1. It retrieves Notification Service "
66 + "from MD-SAL and registers OpendaylightTestNotificationListener as notification listener");
67 BindingAwareConsumer consumer1 = new BindingAwareConsumer() {
69 public void onSessionInitialized(ConsumerContext session) {
70 NotificationService notificationService = session.getSALService(NotificationService.class);
71 assertNotNull(notificationService);
72 listener1Reg = notificationService.registerNotificationListener(listener1);
75 // registerConsumer method calls onSessionInitialized method above
76 broker.registerConsumer(consumer1);
78 assertNotNull(listener1Reg);
80 LOG.info("The notification of type FlowAdded with cookie ID 0 is created. The "
81 + "delay 100ms to make sure that the notification was delivered to "
83 notifyProviderService.publish(noDustNotification("rainy day", 42));
87 * Check that one notification was delivered and has correct cookie.
90 assertEquals(1, listener1.notificationBag.size());
91 assertEquals("rainy day", listener1.notificationBag.get(0).getReason());
92 assertEquals(42, listener1.notificationBag.get(0).getDaysTillNewDust().intValue());
94 LOG.info("The registration of the Consumer 2. SalFlowListener is registered "
95 + "registered as notification listener.");
96 BindingAwareProvider provider = new BindingAwareProvider() {
99 public void onSessionInitiated(ProviderContext session) {
100 listener2Reg = session.getSALService(NotificationProviderService.class).registerNotificationListener(
105 // registerConsumer method calls onSessionInitialized method above
106 broker.registerProvider(provider);
108 LOG.info("3 notifications are published");
109 notifyProviderService.publish(noDustNotification("rainy day", 5));
110 notifyProviderService.publish(noDustNotification("rainy day", 10));
111 notifyProviderService.publish(noDustNotification("tax collector", 2));
114 * The delay 100ms to make sure that the notifications were delivered to
120 * Check that 3 notification was delivered to both listeners (first one
121 * received 4 in total, second 3 in total).
124 assertEquals(4, listener1.notificationBag.size());
125 assertEquals(3, listener2.notificationBag.size());
128 * The second listener is closed (unregistered)
131 listener2Reg.close();
133 LOG.info("The notification 5 is published");
134 notifyProviderService.publish(noDustNotification("entomologist hunt", 10));
137 * The delay 100ms to make sure that the notification was delivered to
143 * Check that first consumer received 5 notifications in total, second
144 * consumer received only three. Last notification was never received by
145 * second consumer because its listener was unregistered.
148 assertEquals(5, listener1.notificationBag.size());
149 assertEquals(3, listener2.notificationBag.size());
154 * Creates instance of the type OutOfPixieDustNotification. It is
155 * used only for testing purpose.
159 * @return instance of the type OutOfPixieDustNotification
161 public static OutOfPixieDustNotification noDustNotification(String reason, int days) {
162 OutOfPixieDustNotificationBuilder ret = new OutOfPixieDustNotificationBuilder();
163 ret.setReason(reason).setDaysTillNewDust(days);
169 * {@link OpendaylightTestNotificationListener} and contains attributes which keep lists of objects of
170 * the type {@link OutOfFairyDustNotification}.
172 public static class NotificationTestListener implements OpendaylightTestNotificationListener {
174 List<OutOfPixieDustNotification> notificationBag = new ArrayList<>();
177 public void onOutOfPixieDustNotification(OutOfPixieDustNotification arg0) {
178 notificationBag.add(arg0);