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