db978dbeec8b8861441800713939bb0f6be3c531
[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 import static org.junit.Assert.assertNotNull;
12
13 import java.util.ArrayList;
14 import java.util.List;
15
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;
29
30 /**
31  * covers registering of notification listener, publishing of notification and receiving of notification.
32  */
33 public class NotificationIT extends AbstractIT {
34
35     private static final Logger LOG = LoggerFactory
36             .getLogger(NotificationIT.class);
37
38     protected final NotificationTestListener listener1 = new NotificationTestListener();
39     protected final NotificationTestListener listener2 = new NotificationTestListener();
40
41     protected ListenerRegistration<NotificationListener> listener1Reg;
42     protected ListenerRegistration<NotificationListener> listener2Reg;
43
44     protected NotificationProviderService notifyProviderService;
45
46     /**
47      * test of delivering of notification
48      * @throws Exception
49      */
50     @Test
51     public void notificationTest() throws Exception {
52         LOG.info("The registration of the Provider 1.");
53         AbstractTestProvider provider1 = new AbstractTestProvider() {
54             @Override
55             public void onSessionInitiated(ProviderContext session) {
56                 notifyProviderService = session.getSALService(NotificationProviderService.class);
57             }
58         };
59
60         // registerProvider method calls onSessionInitiated method above
61         broker.registerProvider(provider1);
62         assertNotNull(notifyProviderService);
63
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);
70         };
71         // registerConsumer method calls onSessionInitialized method above
72         broker.registerConsumer(consumer1);
73
74         assertNotNull(listener1Reg);
75
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 "
78                 + "listener.");
79         notifyProviderService.publish(noDustNotification("rainy day", 42));
80         Thread.sleep(100);
81
82         /**
83          * Check that one notification was delivered and has correct cookie.
84          *
85          */
86         assertEquals(1, listener1.notificationBag.size());
87         assertEquals("rainy day", listener1.notificationBag.get(0).getReason());
88         assertEquals(42, listener1.notificationBag.get(0).getDaysTillNewDust().intValue());
89
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(
93                 listener2);
94
95         // registerConsumer method calls onSessionInitialized method above
96         broker.registerProvider(provider);
97
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));
102
103         /**
104          * The delay 100ms to make sure that the notifications were delivered to
105          * listeners.
106          */
107         Thread.sleep(100);
108
109         /**
110          * Check that 3 notification was delivered to both listeners (first one
111          * received 4 in total, second 3 in total).
112          *
113          */
114         assertEquals(4, listener1.notificationBag.size());
115         assertEquals(3, listener2.notificationBag.size());
116
117         /**
118          * The second listener is closed (unregistered)
119          *
120          */
121         listener2Reg.close();
122
123         LOG.info("The notification 5 is published");
124         notifyProviderService.publish(noDustNotification("entomologist hunt", 10));
125
126         /**
127          * The delay 100ms to make sure that the notification was delivered to
128          * listener.
129          */
130         Thread.sleep(100);
131
132         /**
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.
136          *
137          */
138         assertEquals(5, listener1.notificationBag.size());
139         assertEquals(3, listener2.notificationBag.size());
140
141     }
142
143     /**
144      * Creates instance of the type OutOfPixieDustNotification. It is
145      * used only for testing purpose.
146      *
147      * @param reason
148      * @param days
149      * @return instance of the type OutOfPixieDustNotification
150      */
151     public static OutOfPixieDustNotification noDustNotification(String reason, int days) {
152         OutOfPixieDustNotificationBuilder ret = new OutOfPixieDustNotificationBuilder();
153         ret.setReason(reason).setDaysTillNewDust(days);
154         return ret.build();
155     }
156
157     /**
158      * Implements
159      * {@link OpendaylightTestNotificationListener} and contains attributes which keep lists of objects of
160      * the type {@link OutOfFairyDustNotification}.
161      */
162     public static class NotificationTestListener implements OpendaylightTestNotificationListener {
163
164         List<OutOfPixieDustNotification> notificationBag = new ArrayList<>();
165
166         @Override
167         public void onOutOfPixieDustNotification(OutOfPixieDustNotification arg0) {
168             notificationBag.add(arg0);
169         }
170
171     }
172 }