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