9654ebf27a881dbe81c79b29e6358a0887101d31
[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 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;
25
26 /**
27  * covers registering of notification listener, publishing of notification and receiving of notification.
28  */
29 public class NotificationIT extends AbstractIT {
30
31     private static final Logger LOG = LoggerFactory
32             .getLogger(NotificationIT.class);
33
34     @Inject
35     @Filter(timeout = 120 * 1000)
36     NotificationProviderService notificationService;
37
38     /**
39      * Test of delivering of notification.
40      */
41     @Test
42     public void notificationTest() throws Exception {
43         NotificationTestListener listener1 = new NotificationTestListener();
44         ListenerRegistration<NotificationListener> listener1Reg =
45                 notificationService.registerNotificationListener(listener1);
46
47         LOG.info("The notification of type FlowAdded with cookie ID 0 is created. The "
48                 + "delay 100ms to make sure that the notification was delivered to "
49                 + "listener.");
50         notificationService.publish(noDustNotification("rainy day", 42));
51         Thread.sleep(100);
52
53         /**
54          * Check that one notification was delivered and has correct cookie.
55          */
56         assertEquals(1, listener1.notificationBag.size());
57         assertEquals("rainy day", listener1.notificationBag.get(0).getReason());
58         assertEquals(42, listener1.notificationBag.get(0).getDaysTillNewDust().intValue());
59
60         LOG.info("The registration of the Consumer 2. SalFlowListener is registered "
61                 + "registered as notification listener.");
62
63         NotificationTestListener listener2 = new NotificationTestListener();
64         final ListenerRegistration<NotificationListener> listener2Reg =
65                 notificationService.registerNotificationListener(listener2);
66
67         LOG.info("3 notifications are published");
68         notificationService.publish(noDustNotification("rainy day", 5));
69         notificationService.publish(noDustNotification("rainy day", 10));
70         notificationService.publish(noDustNotification("tax collector", 2));
71
72         /**
73          * The delay 100ms to make sure that the notifications were delivered to
74          * listeners.
75          */
76         Thread.sleep(100);
77
78         /**
79          * Check that 3 notification was delivered to both listeners (first one
80          * received 4 in total, second 3 in total).
81          */
82         assertEquals(4, listener1.notificationBag.size());
83         assertEquals(3, listener2.notificationBag.size());
84
85         /**
86          * The second listener is closed (unregistered)
87          *
88          */
89         listener2Reg.close();
90
91         LOG.info("The notification 5 is published");
92         notificationService.publish(noDustNotification("entomologist hunt", 10));
93
94         /**
95          * The delay 100ms to make sure that the notification was delivered to
96          * listener.
97          */
98         Thread.sleep(100);
99
100         /**
101          * Check that first consumer received 5 notifications in total, second
102          * consumer received only three. Last notification was never received by
103          * second consumer because its listener was unregistered.
104          *
105          */
106         assertEquals(5, listener1.notificationBag.size());
107         assertEquals(3, listener2.notificationBag.size());
108     }
109
110     /**
111      * Creates instance of the type OutOfPixieDustNotification. It is used only for testing purpose.
112      *
113      * @return instance of the type OutOfPixieDustNotification
114      */
115     public static OutOfPixieDustNotification noDustNotification(final String reason, final int days) {
116         OutOfPixieDustNotificationBuilder ret = new OutOfPixieDustNotificationBuilder();
117         ret.setReason(reason).setDaysTillNewDust(days);
118         return ret.build();
119     }
120
121     /**
122      * Implements
123      * {@link OpendaylightTestNotificationListener} and contains attributes which keep lists of objects of
124      * the type {@link OutOfFairyDustNotification}.
125      */
126     public static class NotificationTestListener implements OpendaylightTestNotificationListener {
127         List<OutOfPixieDustNotification> notificationBag = new ArrayList<>();
128
129         @Override
130         public void onOutOfPixieDustNotification(final OutOfPixieDustNotification arg0) {
131             notificationBag.add(arg0);
132         }
133     }
134 }