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.List;
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;
39 public class NotificationTest extends AbstractTest {
41 private final FlowListener listener1 = new FlowListener();
42 private final FlowListener listener2 = new FlowListener();
44 private ListenerRegistration<NotificationListener> listener1Reg;
45 private ListenerRegistration<NotificationListener> listener2Reg;
47 private NotificationProviderService notifyProviderService;
50 public void setUp() throws Exception {
54 public void notificationTest() throws Exception {
57 * The registration of the Provider 1.
60 AbstractTestProvider provider1 = new AbstractTestProvider() {
62 public void onSessionInitiated(ProviderContext session) {
63 notifyProviderService = session.getSALService(NotificationProviderService.class);
67 // registerProvider method calls onSessionInitiated method above
68 broker.registerProvider(provider1, getBundleContext());
69 assertNotNull(notifyProviderService);
73 * The registration of the Consumer 1. It retrieves Notification Service
74 * from MD-SAL and registers SalFlowListener as notification listener
77 BindingAwareConsumer consumer1 = new BindingAwareConsumer() {
79 public void onSessionInitialized(ConsumerContext session) {
80 NotificationService notificationService = session.getSALService(NotificationService.class);
81 assertNotNull(notificationService);
82 listener1Reg = notificationService.registerNotificationListener(listener1);
85 // registerConsumer method calls onSessionInitialized method above
86 broker.registerConsumer(consumer1, getBundleContext());
88 assertNotNull(listener1Reg);
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
95 notifyProviderService.publish(flowAdded(0));
99 * Check that one notification was delivered and has correct cookie.
102 assertEquals(1, listener1.addedFlows.size());
103 assertEquals(0, listener1.addedFlows.get(0).getCookie().getValue().intValue());
106 * The registration of the Consumer 2. SalFlowListener is registered
107 * registered as notification listener.
109 BindingAwareProvider provider = new BindingAwareProvider() {
112 public void onSessionInitiated(ProviderContext session) {
113 listener2Reg = session.getSALService(NotificationProviderService.class).registerNotificationListener(
118 // registerConsumer method calls onSessionInitialized method above
119 broker.registerProvider(provider, getBundleContext());
122 * 3 notifications are published
124 notifyProviderService.publish(flowAdded(5));
125 notifyProviderService.publish(flowAdded(10));
126 notifyProviderService.publish(flowAdded(2));
129 * The delay 100ms to make sure that the notifications were delivered to
135 * Check that 3 notification was delivered to both listeners (first one
136 * received 4 in total, second 3 in total).
139 assertEquals(4, listener1.addedFlows.size());
140 assertEquals(3, listener2.addedFlows.size());
143 * The second listener is closed (unregistered)
146 listener2Reg.close();
150 * The notification 5 is published
152 notifyProviderService.publish(flowAdded(10));
155 * The delay 100ms to make sure that the notification was delivered to
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.
166 assertEquals(5, listener1.addedFlows.size());
167 assertEquals(3, listener2.addedFlows.size());
172 * Creates instance of the type FlowAdded. Only cookie value is set. It is
173 * used only for testing purpose.
177 * @return instance of the type FlowAdded
179 public static FlowAdded flowAdded(int i) {
180 FlowAddedBuilder ret = new FlowAddedBuilder();
181 ret.setCookie(new FlowCookie(BigInteger.valueOf(i)));
188 * {@link org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowListener
189 * SalFlowListener} and contains attributes which keep lists of objects of
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
195 private static class FlowListener implements SalFlowListener {
197 List<FlowAdded> addedFlows = new ArrayList<>();
198 List<FlowRemoved> removedFlows = new ArrayList<>();
199 List<FlowUpdated> updatedFlows = new ArrayList<>();
202 public void onFlowAdded(FlowAdded notification) {
203 addedFlows.add(notification);
207 public void onFlowRemoved(FlowRemoved notification) {
208 removedFlows.add(notification);
212 public void onFlowUpdated(FlowUpdated notification) {
213 updatedFlows.add(notification);
217 public void onSwitchFlowRemoved(SwitchFlowRemoved notification) {
218 // TODO Auto-generated method stub
223 public void onNodeErrorNotification(NodeErrorNotification notification) {
224 // TODO Auto-generated method stub
229 public void onNodeExperimenterErrorNotification(
230 NodeExperimenterErrorNotification notification) {
231 // TODO Auto-generated method stub