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;
12 import java.util.ArrayList;
13 import java.util.List;
14 import javax.inject.Inject;
15 import org.junit.Test;
16 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.bi.ba.notification.rev150205.OpendaylightTestNotificationListener;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.bi.ba.notification.rev150205.OutOfPixieDustNotification;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.bi.ba.notification.rev150205.OutOfPixieDustNotificationBuilder;
20 import org.opendaylight.yangtools.concepts.ListenerRegistration;
21 import org.opendaylight.yangtools.yang.binding.NotificationListener;
22 import org.ops4j.pax.exam.util.Filter;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
27 * covers registering of notification listener, publishing of notification and receiving of notification.
29 public class NotificationIT extends AbstractIT {
31 private static final Logger LOG = LoggerFactory
32 .getLogger(NotificationIT.class);
35 @Filter(timeout = 120 * 1000)
36 NotificationProviderService notificationService;
39 * Test of delivering of notification.
42 public void notificationTest() throws Exception {
43 NotificationTestListener listener1 = new NotificationTestListener();
44 ListenerRegistration<NotificationListener> listener1Reg =
45 notificationService.registerNotificationListener(listener1);
47 LOG.info("The notification of type FlowAdded with cookie ID 0 is created. The "
48 + "delay 100ms to make sure that the notification was delivered to "
50 notificationService.publish(noDustNotification("rainy day", 42));
54 * Check that one notification was delivered and has correct cookie.
56 assertEquals(1, listener1.notificationBag.size());
57 assertEquals("rainy day", listener1.notificationBag.get(0).getReason());
58 assertEquals(42, listener1.notificationBag.get(0).getDaysTillNewDust().intValue());
60 LOG.info("The registration of the Consumer 2. SalFlowListener is registered "
61 + "registered as notification listener.");
63 NotificationTestListener listener2 = new NotificationTestListener();
64 final ListenerRegistration<NotificationListener> listener2Reg =
65 notificationService.registerNotificationListener(listener2);
67 LOG.info("3 notifications are published");
68 notificationService.publish(noDustNotification("rainy day", 5));
69 notificationService.publish(noDustNotification("rainy day", 10));
70 notificationService.publish(noDustNotification("tax collector", 2));
73 * The delay 100ms to make sure that the notifications were delivered to
79 * Check that 3 notification was delivered to both listeners (first one
80 * received 4 in total, second 3 in total).
82 assertEquals(4, listener1.notificationBag.size());
83 assertEquals(3, listener2.notificationBag.size());
86 * The second listener is closed (unregistered)
91 LOG.info("The notification 5 is published");
92 notificationService.publish(noDustNotification("entomologist hunt", 10));
95 * The delay 100ms to make sure that the notification was delivered to
101 * Check that first consumer received 5 notifications in total, second
102 * consumer received only three. Last notification was never received by
103 * second consumer because its listener was unregistered.
106 assertEquals(5, listener1.notificationBag.size());
107 assertEquals(3, listener2.notificationBag.size());
111 * Creates instance of the type OutOfPixieDustNotification. It is used only for testing purpose.
113 * @return instance of the type OutOfPixieDustNotification
115 public static OutOfPixieDustNotification noDustNotification(final String reason, final int days) {
116 OutOfPixieDustNotificationBuilder ret = new OutOfPixieDustNotificationBuilder();
117 ret.setReason(reason).setDaysTillNewDust(days);
123 * {@link OpendaylightTestNotificationListener} and contains attributes which keep lists of objects of
124 * the type {@link OutOfFairyDustNotification}.
126 public static class NotificationTestListener implements OpendaylightTestNotificationListener {
127 List<OutOfPixieDustNotification> notificationBag = new ArrayList<>();
130 public void onOutOfPixieDustNotification(final OutOfPixieDustNotification arg0) {
131 notificationBag.add(arg0);