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.ProviderContext;
18 import org.opendaylight.controller.sal.binding.api.BindingAwareConsumer;
19 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
20 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
21 import org.opendaylight.controller.sal.binding.api.NotificationService;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.bi.ba.notification.rev150205.OpendaylightTestNotificationListener;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.bi.ba.notification.rev150205.OutOfPixieDustNotification;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.bi.ba.notification.rev150205.OutOfPixieDustNotificationBuilder;
25 import org.opendaylight.yangtools.concepts.ListenerRegistration;
26 import org.opendaylight.yangtools.yang.binding.NotificationListener;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * covers registering of notification listener, publishing of notification and receiving of notification.
33 public class NotificationIT extends AbstractIT {
35 private static final Logger LOG = LoggerFactory
36 .getLogger(NotificationIT.class);
38 protected final NotificationTestListener listener1 = new NotificationTestListener();
39 protected final NotificationTestListener listener2 = new NotificationTestListener();
41 protected ListenerRegistration<NotificationListener> listener1Reg;
42 protected ListenerRegistration<NotificationListener> listener2Reg;
44 protected NotificationProviderService notifyProviderService;
47 * test of delivering of notification
51 public void notificationTest() throws Exception {
52 LOG.info("The registration of the Provider 1.");
53 AbstractTestProvider provider1 = new AbstractTestProvider() {
55 public void onSessionInitiated(ProviderContext session) {
56 notifyProviderService = session.getSALService(NotificationProviderService.class);
60 // registerProvider method calls onSessionInitiated method above
61 broker.registerProvider(provider1);
62 assertNotNull(notifyProviderService);
64 LOG.info("The registration of the Consumer 1. It retrieves Notification Service "
65 + "from MD-SAL and registers OpendaylightTestNotificationListener as notification listener");
66 BindingAwareConsumer consumer1 = session -> {
67 NotificationService notificationService = session.getSALService(NotificationService.class);
68 assertNotNull(notificationService);
69 listener1Reg = notificationService.registerNotificationListener(listener1);
71 // registerConsumer method calls onSessionInitialized method above
72 broker.registerConsumer(consumer1);
74 assertNotNull(listener1Reg);
76 LOG.info("The notification of type FlowAdded with cookie ID 0 is created. The "
77 + "delay 100ms to make sure that the notification was delivered to "
79 notifyProviderService.publish(noDustNotification("rainy day", 42));
83 * Check that one notification was delivered and has correct cookie.
86 assertEquals(1, listener1.notificationBag.size());
87 assertEquals("rainy day", listener1.notificationBag.get(0).getReason());
88 assertEquals(42, listener1.notificationBag.get(0).getDaysTillNewDust().intValue());
90 LOG.info("The registration of the Consumer 2. SalFlowListener is registered "
91 + "registered as notification listener.");
92 BindingAwareProvider provider = session -> listener2Reg = session.getSALService(NotificationProviderService.class).registerNotificationListener(
95 // registerConsumer method calls onSessionInitialized method above
96 broker.registerProvider(provider);
98 LOG.info("3 notifications are published");
99 notifyProviderService.publish(noDustNotification("rainy day", 5));
100 notifyProviderService.publish(noDustNotification("rainy day", 10));
101 notifyProviderService.publish(noDustNotification("tax collector", 2));
104 * The delay 100ms to make sure that the notifications were delivered to
110 * Check that 3 notification was delivered to both listeners (first one
111 * received 4 in total, second 3 in total).
114 assertEquals(4, listener1.notificationBag.size());
115 assertEquals(3, listener2.notificationBag.size());
118 * The second listener is closed (unregistered)
121 listener2Reg.close();
123 LOG.info("The notification 5 is published");
124 notifyProviderService.publish(noDustNotification("entomologist hunt", 10));
127 * The delay 100ms to make sure that the notification was delivered to
133 * Check that first consumer received 5 notifications in total, second
134 * consumer received only three. Last notification was never received by
135 * second consumer because its listener was unregistered.
138 assertEquals(5, listener1.notificationBag.size());
139 assertEquals(3, listener2.notificationBag.size());
144 * Creates instance of the type OutOfPixieDustNotification. It is
145 * used only for testing purpose.
149 * @return instance of the type OutOfPixieDustNotification
151 public static OutOfPixieDustNotification noDustNotification(String reason, int days) {
152 OutOfPixieDustNotificationBuilder ret = new OutOfPixieDustNotificationBuilder();
153 ret.setReason(reason).setDaysTillNewDust(days);
159 * {@link OpendaylightTestNotificationListener} and contains attributes which keep lists of objects of
160 * the type {@link OutOfFairyDustNotification}.
162 public static class NotificationTestListener implements OpendaylightTestNotificationListener {
164 List<OutOfPixieDustNotification> notificationBag = new ArrayList<>();
167 public void onOutOfPixieDustNotification(OutOfPixieDustNotification arg0) {
168 notificationBag.add(arg0);