2f86ee4cc2e9e34c4db55b803b565f090c7962b5
[controller.git] / opendaylight / md-sal / sal-binding-it / src / test / java / org / opendaylight / controller / test / sal / binding / it / NoficationTest.java
1 package org.opendaylight.controller.test.sal.binding.it;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5
6 import java.math.BigInteger;
7 import java.util.ArrayList;
8 import java.util.Collection;
9 import java.util.List;
10
11 import org.junit.Before;
12 import org.junit.Test;
13 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ConsumerContext;
14 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
15 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider.ProviderFunctionality;
16 import org.opendaylight.controller.sal.binding.api.BindingAwareConsumer;
17 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
18 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
19 import org.opendaylight.controller.sal.binding.api.NotificationService;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowAdded;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowAddedBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowRemoved;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowUpdated;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeErrorNotification;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeExperimenterErrorNotification;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowListener;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SwitchFlowRemoved;
28 import org.opendaylight.yangtools.concepts.Registration;
29 import org.opendaylight.yangtools.yang.binding.NotificationListener;
30 import org.opendaylight.yangtools.yang.binding.RpcService;
31
32 public class NoficationTest extends AbstractTest {
33
34     private FlowListener listener1 = new FlowListener();
35     private FlowListener listener2 = new FlowListener();
36
37     private Registration<NotificationListener> listener1Reg;
38     private Registration<NotificationListener> listener2Reg;
39
40     private NotificationProviderService notifyProviderService;
41
42     @Before
43     public void setUp() throws Exception {
44     }
45
46     @Test
47     public void notificationTest() throws Exception {
48         /**
49          * 
50          * The registration of the Provider 1.
51          * 
52          */
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, getBundleContext());
62         assertNotNull(notifyProviderService);
63
64         /**
65          * 
66          * The registration of the Consumer 1. It retrieves Notification Service
67          * from MD-SAL and registers SalFlowListener as notification listener
68          * 
69          */
70         BindingAwareConsumer consumer1 = new BindingAwareConsumer() {
71             @Override
72             public void onSessionInitialized(ConsumerContext session) {
73                 NotificationService notificationService = session.getSALService(NotificationService.class);
74                 assertNotNull(notificationService);
75                 listener1Reg = notificationService.registerNotificationListener(listener1);
76             }
77         };
78         // registerConsumer method calls onSessionInitialized method above
79         broker.registerConsumer(consumer1, getBundleContext());
80
81         assertNotNull(listener1Reg);
82
83         /**
84          * The notification of type FlowAdded with cookie ID 0 is created. The
85          * delay 100ms to make sure that the notification was delivered to
86          * listener.
87          */
88         notifyProviderService.publish(flowAdded(0));
89         Thread.sleep(100);
90
91         /**
92          * Check that one notification was delivered and has correct cookie.
93          * 
94          */
95         assertEquals(1, listener1.addedFlows.size());
96         assertEquals(0, listener1.addedFlows.get(0).getCookie().intValue());
97
98         /**
99          * The registration of the Consumer 2. SalFlowListener is registered
100          * registered as notification listener.
101          */
102         BindingAwareProvider provider = new BindingAwareProvider() {
103             
104             @Override
105             public void onSessionInitiated(ProviderContext session) {
106                 listener2Reg = session.getSALService(NotificationProviderService.class).registerNotificationListener(
107                         listener2);
108             }
109             
110             @Override
111             public void onSessionInitialized(ConsumerContext session) {
112                 // TODO Auto-generated method stub
113                 
114             }
115             
116             @Override
117             public Collection<? extends RpcService> getImplementations() {
118                 // TODO Auto-generated method stub
119                 return null;
120             }
121             
122             @Override
123             public Collection<? extends ProviderFunctionality> getFunctionality() {
124                 // TODO Auto-generated method stub
125                 return null;
126             }
127             
128         };
129
130         // registerConsumer method calls onSessionInitialized method above
131         broker.registerProvider(provider, getBundleContext());
132
133         /**
134          * 3 notifications are published
135          */
136         notifyProviderService.publish(flowAdded(5));
137         notifyProviderService.publish(flowAdded(10));
138         notifyProviderService.publish(flowAdded(2));
139
140         /**
141          * The delay 100ms to make sure that the notifications were delivered to
142          * listeners.
143          */
144         Thread.sleep(100);
145
146         /**
147          * Check that 3 notification was delivered to both listeners (first one
148          * received 4 in total, second 3 in total).
149          * 
150          */
151         assertEquals(4, listener1.addedFlows.size());
152         assertEquals(3, listener2.addedFlows.size());
153
154         /**
155          * The second listener is closed (unregistered)
156          * 
157          */
158         listener2Reg.close();
159
160         /**
161          * 
162          * The notification 5 is published
163          */
164         notifyProviderService.publish(flowAdded(10));
165
166         /**
167          * The delay 100ms to make sure that the notification was delivered to
168          * listener.
169          */
170         Thread.sleep(100);
171
172         /**
173          * Check that first consumer received 5 notifications in total, second
174          * consumer received only three. Last notification was never received by
175          * second consumer because its listener was unregistered.
176          * 
177          */
178         assertEquals(5, listener1.addedFlows.size());
179         assertEquals(3, listener2.addedFlows.size());
180
181     }
182
183     /**
184      * Creates instance of the type FlowAdded. Only cookie value is set. It is
185      * used only for testing purpose.
186      * 
187      * @param i
188      *            cookie value
189      * @return instance of the type FlowAdded
190      */
191     public static FlowAdded flowAdded(int i) {
192         FlowAddedBuilder ret = new FlowAddedBuilder();
193         ret.setCookie(BigInteger.valueOf(i));
194         return ret.build();
195     }
196
197     /**
198      * 
199      * Implements
200      * {@link org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowListener
201      * SalFlowListener} and contains attributes which keep lists of objects of
202      * the type
203      * {@link org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819. NodeFlow
204      * NodeFlow}. The lists are defined for flows which were added, removed or
205      * updated.
206      */
207     private static class FlowListener implements SalFlowListener {
208
209         List<FlowAdded> addedFlows = new ArrayList<>();
210         List<FlowRemoved> removedFlows = new ArrayList<>();
211         List<FlowUpdated> updatedFlows = new ArrayList<>();
212
213         @Override
214         public void onFlowAdded(FlowAdded notification) {
215             addedFlows.add(notification);
216         }
217
218         @Override
219         public void onFlowRemoved(FlowRemoved notification) {
220             removedFlows.add(notification);
221         }; 
222
223         @Override
224         public void onFlowUpdated(FlowUpdated notification) {
225             updatedFlows.add(notification);
226         }
227
228         @Override
229         public void onSwitchFlowRemoved(SwitchFlowRemoved notification) {
230             // TODO Auto-generated method stub
231             
232         }
233
234         @Override
235         public void onNodeErrorNotification(NodeErrorNotification notification) {
236             // TODO Auto-generated method stub
237             
238         }
239
240         @Override
241         public void onNodeExperimenterErrorNotification(
242                 NodeExperimenterErrorNotification notification) {
243             // TODO Auto-generated method stub
244             
245         }
246
247     }
248 }