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