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