Improve segmented journal actor metrics
[controller.git] / opendaylight / md-sal / sal-binding-it / src / test / java / org / opendaylight / controller / test / sal / binding / it / NotificationIT.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.test.sal.binding.it;
9
10 import static org.junit.Assert.assertEquals;
11
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;
23
24 /**
25  * covers registering of notification listener, publishing of notification and receiving of notification.
26  */
27 public class NotificationIT extends AbstractIT {
28     private static final Logger LOG = LoggerFactory.getLogger(NotificationIT.class);
29
30     @Inject
31     @Filter(timeout = 120 * 1000)
32     NotificationService notificationService;
33
34     @Inject
35     @Filter(timeout = 120 * 1000)
36     NotificationPublishService notificationPublishService;
37
38     /**
39      * Test of delivering of notification.
40      */
41     @Test
42     public void notificationTest() throws Exception {
43         final var bag1 = new ArrayList<OutOfPixieDustNotification>();
44         try (var reg1 = notificationService.registerListener(OutOfPixieDustNotification.class, bag1::add)) {
45             LOG.info("""
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\
48                 listener.""");
49             notificationPublishService.putNotification(noDustNotification("rainy day", 42));
50             Thread.sleep(100);
51
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());
56
57             LOG.info("The registration of the Consumer 2. SalFlowListener is registered "
58                 + "registered as notification listener.");
59
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));
66
67                 // The delay 100ms to make sure that the notifications were delivered to listeners.
68                 Thread.sleep(100);
69
70                 // Check that 3 notification was delivered to both listeners (first one  received 4 in total, second 3
71                 // in total).
72                 assertEquals(4, bag1.size());
73                 assertEquals(3, bag2.size());
74
75                 // The second listener is closed (unregistered)
76                 reg2.close();
77
78                 LOG.info("The notification 5 is published");
79                 notificationPublishService.putNotification(noDustNotification("entomologist hunt", 10));
80
81                 // The delay 100ms to make sure that the notification was delivered to listener.
82                 Thread.sleep(100);
83
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());
88             }
89         }
90     }
91
92     /**
93      * Creates instance of the type OutOfPixieDustNotification. It is used only for testing purpose.
94      *
95      * @return instance of the type OutOfPixieDustNotification
96      */
97     public static OutOfPixieDustNotification noDustNotification(final String reason, final int days) {
98         OutOfPixieDustNotificationBuilder ret = new OutOfPixieDustNotificationBuilder();
99         ret.setReason(reason).setDaysTillNewDust(Uint16.valueOf(days));
100         return ret.build();
101     }
102 }