BUG-2681: switch sal-binding-it to sal test models
[controller.git] / opendaylight / md-sal / sal-binding-it / src / test / java / org / opendaylight / controller / test / sal / binding / it / NotificationTest.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.ConsumerContext;
18 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
19 import org.opendaylight.controller.sal.binding.api.BindingAwareConsumer;
20 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
21 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
22 import org.opendaylight.controller.sal.binding.api.NotificationService;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.bi.ba.notification.rev150205.OpendaylightTestNotificationListener;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.bi.ba.notification.rev150205.OutOfPixieDustNotification;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.bi.ba.notification.rev150205.OutOfPixieDustNotificationBuilder;
26 import org.opendaylight.yangtools.concepts.ListenerRegistration;
27 import org.opendaylight.yangtools.yang.binding.NotificationListener;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * covers registering of notification listener, publishing of notification and receiving of notification.
33  */
34 public class NotificationTest extends AbstractTest {
35
36     private static final Logger LOG = LoggerFactory
37             .getLogger(NotificationTest.class);
38
39     protected final NotificationTestListener listener1 = new NotificationTestListener();
40     protected final NotificationTestListener listener2 = new NotificationTestListener();
41
42     protected ListenerRegistration<NotificationListener> listener1Reg;
43     protected ListenerRegistration<NotificationListener> listener2Reg;
44
45     protected NotificationProviderService notifyProviderService;
46
47     /**
48      * test of delivering of notification
49      * @throws Exception
50      */
51     @Test
52     public void notificationTest() throws Exception {
53         LOG.info("The registration of the Provider 1.");
54         AbstractTestProvider provider1 = new AbstractTestProvider() {
55             @Override
56             public void onSessionInitiated(ProviderContext session) {
57                 notifyProviderService = session.getSALService(NotificationProviderService.class);
58             }
59         };
60
61         // registerProvider method calls onSessionInitiated method above
62         broker.registerProvider(provider1);
63         assertNotNull(notifyProviderService);
64
65         LOG.info("The registration of the Consumer 1. It retrieves Notification Service "
66                 + "from MD-SAL and registers OpendaylightTestNotificationListener as notification listener");
67         BindingAwareConsumer consumer1 = new BindingAwareConsumer() {
68             @Override
69             public void onSessionInitialized(ConsumerContext session) {
70                 NotificationService notificationService = session.getSALService(NotificationService.class);
71                 assertNotNull(notificationService);
72                 listener1Reg = notificationService.registerNotificationListener(listener1);
73             }
74         };
75         // registerConsumer method calls onSessionInitialized method above
76         broker.registerConsumer(consumer1);
77
78         assertNotNull(listener1Reg);
79
80         LOG.info("The notification of type FlowAdded with cookie ID 0 is created. The "
81                 + "delay 100ms to make sure that the notification was delivered to "
82                 + "listener.");
83         notifyProviderService.publish(noDustNotification("rainy day", 42));
84         Thread.sleep(100);
85
86         /**
87          * Check that one notification was delivered and has correct cookie.
88          *
89          */
90         assertEquals(1, listener1.notificationBag.size());
91         assertEquals("rainy day", listener1.notificationBag.get(0).getReason());
92         assertEquals(42, listener1.notificationBag.get(0).getDaysTillNewDust().intValue());
93
94         LOG.info("The registration of the Consumer 2. SalFlowListener is registered "
95                 + "registered as notification listener.");
96         BindingAwareProvider provider = new BindingAwareProvider() {
97
98             @Override
99             public void onSessionInitiated(ProviderContext session) {
100                 listener2Reg = session.getSALService(NotificationProviderService.class).registerNotificationListener(
101                         listener2);
102             }
103         };
104
105         // registerConsumer method calls onSessionInitialized method above
106         broker.registerProvider(provider);
107
108         LOG.info("3 notifications are published");
109         notifyProviderService.publish(noDustNotification("rainy day", 5));
110         notifyProviderService.publish(noDustNotification("rainy day", 10));
111         notifyProviderService.publish(noDustNotification("tax collector", 2));
112
113         /**
114          * The delay 100ms to make sure that the notifications were delivered to
115          * listeners.
116          */
117         Thread.sleep(100);
118
119         /**
120          * Check that 3 notification was delivered to both listeners (first one
121          * received 4 in total, second 3 in total).
122          *
123          */
124         assertEquals(4, listener1.notificationBag.size());
125         assertEquals(3, listener2.notificationBag.size());
126
127         /**
128          * The second listener is closed (unregistered)
129          *
130          */
131         listener2Reg.close();
132
133         LOG.info("The notification 5 is published");
134         notifyProviderService.publish(noDustNotification("entomologist hunt", 10));
135
136         /**
137          * The delay 100ms to make sure that the notification was delivered to
138          * listener.
139          */
140         Thread.sleep(100);
141
142         /**
143          * Check that first consumer received 5 notifications in total, second
144          * consumer received only three. Last notification was never received by
145          * second consumer because its listener was unregistered.
146          *
147          */
148         assertEquals(5, listener1.notificationBag.size());
149         assertEquals(3, listener2.notificationBag.size());
150
151     }
152
153     /**
154      * Creates instance of the type OutOfPixieDustNotification. It is
155      * used only for testing purpose.
156      *
157      * @param reason
158      * @param days
159      * @return instance of the type OutOfPixieDustNotification
160      */
161     public static OutOfPixieDustNotification noDustNotification(String reason, int days) {
162         OutOfPixieDustNotificationBuilder ret = new OutOfPixieDustNotificationBuilder();
163         ret.setReason(reason).setDaysTillNewDust(days);
164         return ret.build();
165     }
166
167     /**
168      *
169      * Implements
170      * {@link OpendaylightTestNotificationListener} and contains attributes which keep lists of objects of
171      * the type {@link OutOfFairyDustNotification}.
172      */
173     public static class NotificationTestListener implements OpendaylightTestNotificationListener {
174
175         List<OutOfPixieDustNotification> notificationBag = new ArrayList<>();
176
177         @Override
178         public void onOutOfPixieDustNotification(OutOfPixieDustNotification arg0) {
179             notificationBag.add(arg0);
180         }
181
182     }
183 }