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 javax.inject.Inject;
14 import org.junit.Test;
15 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
16 import org.opendaylight.mdsal.binding.api.NotificationService;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.bi.ba.notification.rev150205.OutOfPixieDustNotification;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.bi.ba.notification.rev150205.OutOfPixieDustNotificationBuilder;
19 import org.opendaylight.yangtools.yang.common.Uint16;
20 import org.ops4j.pax.exam.util.Filter;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
25 * covers registering of notification listener, publishing of notification and receiving of notification.
27 public class NotificationIT extends AbstractIT {
28 private static final Logger LOG = LoggerFactory.getLogger(NotificationIT.class);
31 @Filter(timeout = 120 * 1000)
32 NotificationService notificationService;
35 @Filter(timeout = 120 * 1000)
36 NotificationPublishService notificationPublishService;
39 * Test of delivering of notification.
42 public void notificationTest() throws Exception {
43 final var bag1 = new ArrayList<OutOfPixieDustNotification>();
44 try (var reg1 = notificationService.registerListener(OutOfPixieDustNotification.class, bag1::add)) {
46 The notification of type FlowAdded with cookie ID 0 is created. The\s\
47 delay 100ms to make sure that the notification was delivered to\s\
49 notificationPublishService.putNotification(noDustNotification("rainy day", 42));
52 // Check that one notification was delivered and has correct cookie.
53 assertEquals(1, bag1.size());
54 assertEquals("rainy day", bag1.get(0).getReason());
55 assertEquals(42, bag1.get(0).getDaysTillNewDust().intValue());
57 LOG.info("The registration of the Consumer 2. SalFlowListener is registered "
58 + "registered as notification listener.");
60 final var bag2 = new ArrayList<OutOfPixieDustNotification>();
61 try (var reg2 = notificationService.registerListener(OutOfPixieDustNotification.class, bag2::add)) {
62 LOG.info("3 notifications are published");
63 notificationPublishService.putNotification(noDustNotification("rainy day", 5));
64 notificationPublishService.putNotification(noDustNotification("rainy day", 10));
65 notificationPublishService.putNotification(noDustNotification("tax collector", 2));
67 // The delay 100ms to make sure that the notifications were delivered to listeners.
70 // Check that 3 notification was delivered to both listeners (first one received 4 in total, second 3
72 assertEquals(4, bag1.size());
73 assertEquals(3, bag2.size());
75 // The second listener is closed (unregistered)
78 LOG.info("The notification 5 is published");
79 notificationPublishService.putNotification(noDustNotification("entomologist hunt", 10));
81 // The delay 100ms to make sure that the notification was delivered to listener.
84 // Check that first consumer received 5 notifications in total, second consumer received only three.
85 // Last notification was never received by second consumer because its listener was unregistered.
86 assertEquals(5, bag1.size());
87 assertEquals(3, bag2.size());
93 * Creates instance of the type OutOfPixieDustNotification. It is used only for testing purpose.
95 * @return instance of the type OutOfPixieDustNotification
97 public static OutOfPixieDustNotification noDustNotification(final String reason, final int days) {
98 OutOfPixieDustNotificationBuilder ret = new OutOfPixieDustNotificationBuilder();
99 ret.setReason(reason).setDaysTillNewDust(Uint16.valueOf(days));