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