2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.test.sal.binding.it;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
13 import java.math.BigInteger;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.List;
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;
38 public class NoficationTest extends AbstractTest {
40 private final FlowListener listener1 = new FlowListener();
41 private final FlowListener listener2 = new FlowListener();
43 private Registration<NotificationListener> listener1Reg;
44 private Registration<NotificationListener> listener2Reg;
46 private NotificationProviderService notifyProviderService;
49 public void setUp() throws Exception {
53 public void notificationTest() throws Exception {
56 * The registration of the Provider 1.
59 AbstractTestProvider provider1 = new AbstractTestProvider() {
61 public void onSessionInitiated(ProviderContext session) {
62 notifyProviderService = session.getSALService(NotificationProviderService.class);
66 // registerProvider method calls onSessionInitiated method above
67 broker.registerProvider(provider1, getBundleContext());
68 assertNotNull(notifyProviderService);
72 * The registration of the Consumer 1. It retrieves Notification Service
73 * from MD-SAL and registers SalFlowListener as notification listener
76 BindingAwareConsumer consumer1 = new BindingAwareConsumer() {
78 public void onSessionInitialized(ConsumerContext session) {
79 NotificationService notificationService = session.getSALService(NotificationService.class);
80 assertNotNull(notificationService);
81 listener1Reg = notificationService.registerNotificationListener(listener1);
84 // registerConsumer method calls onSessionInitialized method above
85 broker.registerConsumer(consumer1, getBundleContext());
87 assertNotNull(listener1Reg);
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
94 notifyProviderService.publish(flowAdded(0));
98 * Check that one notification was delivered and has correct cookie.
101 assertEquals(1, listener1.addedFlows.size());
102 assertEquals(0, listener1.addedFlows.get(0).getCookie().intValue());
105 * The registration of the Consumer 2. SalFlowListener is registered
106 * registered as notification listener.
108 BindingAwareProvider provider = new BindingAwareProvider() {
111 public void onSessionInitiated(ProviderContext session) {
112 listener2Reg = session.getSALService(NotificationProviderService.class).registerNotificationListener(
117 public void onSessionInitialized(ConsumerContext session) {
118 // TODO Auto-generated method stub
123 public Collection<? extends RpcService> getImplementations() {
124 // TODO Auto-generated method stub
129 public Collection<? extends ProviderFunctionality> getFunctionality() {
130 // TODO Auto-generated method stub
136 // registerConsumer method calls onSessionInitialized method above
137 broker.registerProvider(provider, getBundleContext());
140 * 3 notifications are published
142 notifyProviderService.publish(flowAdded(5));
143 notifyProviderService.publish(flowAdded(10));
144 notifyProviderService.publish(flowAdded(2));
147 * The delay 100ms to make sure that the notifications were delivered to
153 * Check that 3 notification was delivered to both listeners (first one
154 * received 4 in total, second 3 in total).
157 assertEquals(4, listener1.addedFlows.size());
158 assertEquals(3, listener2.addedFlows.size());
161 * The second listener is closed (unregistered)
164 listener2Reg.close();
168 * The notification 5 is published
170 notifyProviderService.publish(flowAdded(10));
173 * The delay 100ms to make sure that the notification was delivered to
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.
184 assertEquals(5, listener1.addedFlows.size());
185 assertEquals(3, listener2.addedFlows.size());
190 * Creates instance of the type FlowAdded. Only cookie value is set. It is
191 * used only for testing purpose.
195 * @return instance of the type FlowAdded
197 public static FlowAdded flowAdded(int i) {
198 FlowAddedBuilder ret = new FlowAddedBuilder();
199 ret.setCookie(BigInteger.valueOf(i));
206 * {@link org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowListener
207 * SalFlowListener} and contains attributes which keep lists of objects of
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
213 private static class FlowListener implements SalFlowListener {
215 List<FlowAdded> addedFlows = new ArrayList<>();
216 List<FlowRemoved> removedFlows = new ArrayList<>();
217 List<FlowUpdated> updatedFlows = new ArrayList<>();
220 public void onFlowAdded(FlowAdded notification) {
221 addedFlows.add(notification);
225 public void onFlowRemoved(FlowRemoved notification) {
226 removedFlows.add(notification);
230 public void onFlowUpdated(FlowUpdated notification) {
231 updatedFlows.add(notification);
235 public void onSwitchFlowRemoved(SwitchFlowRemoved notification) {
236 // TODO Auto-generated method stub
241 public void onNodeErrorNotification(NodeErrorNotification notification) {
242 // TODO Auto-generated method stub
247 public void onNodeExperimenterErrorNotification(
248 NodeExperimenterErrorNotification notification) {
249 // TODO Auto-generated method stub