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
43 public void notificationTest() throws Exception {
44 NotificationTestListener listener1 = new NotificationTestListener();
45 ListenerRegistration<NotificationListener> listener1Reg =
46 notificationService.registerNotificationListener(listener1);
48 LOG.info("The notification of type FlowAdded with cookie ID 0 is created. The "
49 + "delay 100ms to make sure that the notification was delivered to "
51 notificationService.publish(noDustNotification("rainy day", 42));
55 * Check that one notification was delivered and has correct cookie.
58 assertEquals(1, listener1.notificationBag.size());
59 assertEquals("rainy day", listener1.notificationBag.get(0).getReason());
60 assertEquals(42, listener1.notificationBag.get(0).getDaysTillNewDust().intValue());
62 LOG.info("The registration of the Consumer 2. SalFlowListener is registered "
63 + "registered as notification listener.");
65 NotificationTestListener listener2 = new NotificationTestListener();
66 final ListenerRegistration<NotificationListener> listener2Reg =
67 notificationService.registerNotificationListener(listener2);
69 LOG.info("3 notifications are published");
70 notificationService.publish(noDustNotification("rainy day", 5));
71 notificationService.publish(noDustNotification("rainy day", 10));
72 notificationService.publish(noDustNotification("tax collector", 2));
75 * The delay 100ms to make sure that the notifications were delivered to
81 * Check that 3 notification was delivered to both listeners (first one
82 * received 4 in total, second 3 in total).
85 assertEquals(4, listener1.notificationBag.size());
86 assertEquals(3, listener2.notificationBag.size());
89 * The second listener is closed (unregistered)
94 LOG.info("The notification 5 is published");
95 notificationService.publish(noDustNotification("entomologist hunt", 10));
98 * The delay 100ms to make sure that the notification was delivered to
104 * Check that first consumer received 5 notifications in total, second
105 * consumer received only three. Last notification was never received by
106 * second consumer because its listener was unregistered.
109 assertEquals(5, listener1.notificationBag.size());
110 assertEquals(3, listener2.notificationBag.size());
115 * Creates instance of the type OutOfPixieDustNotification. It is
116 * used only for testing purpose.
120 * @return instance of the type OutOfPixieDustNotification
122 public static OutOfPixieDustNotification noDustNotification(String reason, int days) {
123 OutOfPixieDustNotificationBuilder ret = new OutOfPixieDustNotificationBuilder();
124 ret.setReason(reason).setDaysTillNewDust(days);
130 * {@link OpendaylightTestNotificationListener} and contains attributes which keep lists of objects of
131 * the type {@link OutOfFairyDustNotification}.
133 public static class NotificationTestListener implements OpendaylightTestNotificationListener {
135 List<OutOfPixieDustNotification> notificationBag = new ArrayList<>();
138 public void onOutOfPixieDustNotification(OutOfPixieDustNotification arg0) {
139 notificationBag.add(arg0);