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.BindingAwareProvider.ProviderFunctionality;
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.yangtools.concepts.Registration;
36 import org.opendaylight.yangtools.yang.binding.NotificationListener;
37 import org.opendaylight.yangtools.yang.binding.RpcService;
39 public class NoficationTest extends AbstractTest {
41 private FlowListener listener1 = new FlowListener();
42 private FlowListener listener2 = new FlowListener();
44 private Registration<NotificationListener> listener1Reg;
45 private Registration<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().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 public void onSessionInitialized(ConsumerContext session) {
119 // TODO Auto-generated method stub
124 public Collection<? extends RpcService> getImplementations() {
125 // TODO Auto-generated method stub
130 public Collection<? extends ProviderFunctionality> getFunctionality() {
131 // TODO Auto-generated method stub
137 // registerConsumer method calls onSessionInitialized method above
138 broker.registerProvider(provider, getBundleContext());
141 * 3 notifications are published
143 notifyProviderService.publish(flowAdded(5));
144 notifyProviderService.publish(flowAdded(10));
145 notifyProviderService.publish(flowAdded(2));
148 * The delay 100ms to make sure that the notifications were delivered to
154 * Check that 3 notification was delivered to both listeners (first one
155 * received 4 in total, second 3 in total).
158 assertEquals(4, listener1.addedFlows.size());
159 assertEquals(3, listener2.addedFlows.size());
162 * The second listener is closed (unregistered)
165 listener2Reg.close();
169 * The notification 5 is published
171 notifyProviderService.publish(flowAdded(10));
174 * The delay 100ms to make sure that the notification was delivered to
180 * Check that first consumer received 5 notifications in total, second
181 * consumer received only three. Last notification was never received by
182 * second consumer because its listener was unregistered.
185 assertEquals(5, listener1.addedFlows.size());
186 assertEquals(3, listener2.addedFlows.size());
191 * Creates instance of the type FlowAdded. Only cookie value is set. It is
192 * used only for testing purpose.
196 * @return instance of the type FlowAdded
198 public static FlowAdded flowAdded(int i) {
199 FlowAddedBuilder ret = new FlowAddedBuilder();
200 ret.setCookie(BigInteger.valueOf(i));
207 * {@link org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowListener
208 * SalFlowListener} and contains attributes which keep lists of objects of
210 * {@link org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819. NodeFlow
211 * NodeFlow}. The lists are defined for flows which were added, removed or
214 private static class FlowListener implements SalFlowListener {
216 List<FlowAdded> addedFlows = new ArrayList<>();
217 List<FlowRemoved> removedFlows = new ArrayList<>();
218 List<FlowUpdated> updatedFlows = new ArrayList<>();
221 public void onFlowAdded(FlowAdded notification) {
222 addedFlows.add(notification);
226 public void onFlowRemoved(FlowRemoved notification) {
227 removedFlows.add(notification);
231 public void onFlowUpdated(FlowUpdated notification) {
232 updatedFlows.add(notification);
236 public void onSwitchFlowRemoved(SwitchFlowRemoved notification) {
237 // TODO Auto-generated method stub
242 public void onNodeErrorNotification(NodeErrorNotification notification) {
243 // TODO Auto-generated method stub
248 public void onNodeExperimenterErrorNotification(
249 NodeExperimenterErrorNotification notification) {
250 // TODO Auto-generated method stub