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.mdsal.binding.api.NotificationPublishService;
17 import org.opendaylight.mdsal.binding.api.NotificationService;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.bi.ba.notification.rev150205.OpendaylightTestNotificationListener;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.bi.ba.notification.rev150205.OutOfPixieDustNotification;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.bi.ba.notification.rev150205.OutOfPixieDustNotificationBuilder;
21 import org.opendaylight.yangtools.concepts.ListenerRegistration;
22 import org.opendaylight.yangtools.yang.binding.NotificationListener;
23 import org.opendaylight.yangtools.yang.common.Uint16;
24 import org.ops4j.pax.exam.util.Filter;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 * covers registering of notification listener, publishing of notification and receiving of notification.
31 public class NotificationIT extends AbstractIT {
32 private static final Logger LOG = LoggerFactory.getLogger(NotificationIT.class);
35 @Filter(timeout = 120 * 1000)
36 NotificationService notificationService;
39 @Filter(timeout = 120 * 1000)
40 NotificationPublishService notificationPublishService;
43 * Test of delivering of notification.
46 public void notificationTest() throws Exception {
47 NotificationTestListener listener1 = new NotificationTestListener();
48 ListenerRegistration<NotificationListener> listener1Reg =
49 notificationService.registerNotificationListener(listener1);
51 LOG.info("The notification of type FlowAdded with cookie ID 0 is created. The "
52 + "delay 100ms to make sure that the notification was delivered to "
54 notificationPublishService.putNotification(noDustNotification("rainy day", 42));
58 * Check that one notification was delivered and has correct cookie.
60 assertEquals(1, listener1.notificationBag.size());
61 assertEquals("rainy day", listener1.notificationBag.get(0).getReason());
62 assertEquals(42, listener1.notificationBag.get(0).getDaysTillNewDust().intValue());
64 LOG.info("The registration of the Consumer 2. SalFlowListener is registered "
65 + "registered as notification listener.");
67 NotificationTestListener listener2 = new NotificationTestListener();
68 final ListenerRegistration<NotificationListener> listener2Reg =
69 notificationService.registerNotificationListener(listener2);
71 LOG.info("3 notifications are published");
72 notificationPublishService.putNotification(noDustNotification("rainy day", 5));
73 notificationPublishService.putNotification(noDustNotification("rainy day", 10));
74 notificationPublishService.putNotification(noDustNotification("tax collector", 2));
77 * The delay 100ms to make sure that the notifications were delivered to
83 * Check that 3 notification was delivered to both listeners (first one
84 * received 4 in total, second 3 in total).
86 assertEquals(4, listener1.notificationBag.size());
87 assertEquals(3, listener2.notificationBag.size());
90 * The second listener is closed (unregistered)
95 LOG.info("The notification 5 is published");
96 notificationPublishService.putNotification(noDustNotification("entomologist hunt", 10));
99 * The delay 100ms to make sure that the notification was delivered to
105 * Check that first consumer received 5 notifications in total, second
106 * consumer received only three. Last notification was never received by
107 * second consumer because its listener was unregistered.
110 assertEquals(5, listener1.notificationBag.size());
111 assertEquals(3, listener2.notificationBag.size());
115 * Creates instance of the type OutOfPixieDustNotification. It is used only for testing purpose.
117 * @return instance of the type OutOfPixieDustNotification
119 public static OutOfPixieDustNotification noDustNotification(final String reason, final int days) {
120 OutOfPixieDustNotificationBuilder ret = new OutOfPixieDustNotificationBuilder();
121 ret.setReason(reason).setDaysTillNewDust(Uint16.valueOf(days));
126 * Implements {@link OpendaylightTestNotificationListener} and contains attributes which keep lists of objects of
127 * the type {@link OutOfPixieDustNotification}.
129 public static class NotificationTestListener implements OpendaylightTestNotificationListener {
130 List<OutOfPixieDustNotification> notificationBag = new ArrayList<>();
133 public void onOutOfPixieDustNotification(final OutOfPixieDustNotification arg0) {
134 notificationBag.add(arg0);