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.Ignore;
20 import org.junit.Test;
21 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ConsumerContext;
22 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
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.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
36 import org.opendaylight.yangtools.concepts.Registration;
37 import org.opendaylight.yangtools.yang.binding.NotificationListener;
38 import org.opendaylight.yangtools.yang.binding.RpcService;
41 public class NotificationTest extends AbstractTest {
43 private final FlowListener listener1 = new FlowListener();
44 private final FlowListener listener2 = new FlowListener();
46 private Registration<NotificationListener> listener1Reg;
47 private Registration<NotificationListener> listener2Reg;
49 private NotificationProviderService notifyProviderService;
52 public void setUp() throws Exception {
56 public void notificationTest() throws Exception {
59 * The registration of the Provider 1.
62 AbstractTestProvider provider1 = new AbstractTestProvider() {
64 public void onSessionInitiated(ProviderContext session) {
65 notifyProviderService = session.getSALService(NotificationProviderService.class);
69 // registerProvider method calls onSessionInitiated method above
70 broker.registerProvider(provider1, getBundleContext());
71 assertNotNull(notifyProviderService);
75 * The registration of the Consumer 1. It retrieves Notification Service
76 * from MD-SAL and registers SalFlowListener as notification listener
79 BindingAwareConsumer consumer1 = new BindingAwareConsumer() {
81 public void onSessionInitialized(ConsumerContext session) {
82 NotificationService notificationService = session.getSALService(NotificationService.class);
83 assertNotNull(notificationService);
84 listener1Reg = notificationService.registerNotificationListener(listener1);
87 // registerConsumer method calls onSessionInitialized method above
88 broker.registerConsumer(consumer1, getBundleContext());
90 assertNotNull(listener1Reg);
93 * The notification of type FlowAdded with cookie ID 0 is created. The
94 * delay 100ms to make sure that the notification was delivered to
97 notifyProviderService.publish(flowAdded(0));
101 * Check that one notification was delivered and has correct cookie.
104 assertEquals(1, listener1.addedFlows.size());
105 assertEquals(0, listener1.addedFlows.get(0).getCookie().getValue().intValue());
108 * The registration of the Consumer 2. SalFlowListener is registered
109 * registered as notification listener.
111 BindingAwareProvider provider = new BindingAwareProvider() {
114 public void onSessionInitiated(ProviderContext session) {
115 listener2Reg = session.getSALService(NotificationProviderService.class).registerNotificationListener(
120 public void onSessionInitialized(ConsumerContext session) {
121 // TODO Auto-generated method stub
126 public Collection<? extends RpcService> getImplementations() {
127 // TODO Auto-generated method stub
132 public Collection<? extends ProviderFunctionality> getFunctionality() {
133 // TODO Auto-generated method stub
139 // registerConsumer method calls onSessionInitialized method above
140 broker.registerProvider(provider, getBundleContext());
143 * 3 notifications are published
145 notifyProviderService.publish(flowAdded(5));
146 notifyProviderService.publish(flowAdded(10));
147 notifyProviderService.publish(flowAdded(2));
150 * The delay 100ms to make sure that the notifications were delivered to
156 * Check that 3 notification was delivered to both listeners (first one
157 * received 4 in total, second 3 in total).
160 assertEquals(4, listener1.addedFlows.size());
161 assertEquals(3, listener2.addedFlows.size());
164 * The second listener is closed (unregistered)
167 listener2Reg.close();
171 * The notification 5 is published
173 notifyProviderService.publish(flowAdded(10));
176 * The delay 100ms to make sure that the notification was delivered to
182 * Check that first consumer received 5 notifications in total, second
183 * consumer received only three. Last notification was never received by
184 * second consumer because its listener was unregistered.
187 assertEquals(5, listener1.addedFlows.size());
188 assertEquals(3, listener2.addedFlows.size());
193 * Creates instance of the type FlowAdded. Only cookie value is set. It is
194 * used only for testing purpose.
198 * @return instance of the type FlowAdded
200 public static FlowAdded flowAdded(int i) {
201 FlowAddedBuilder ret = new FlowAddedBuilder();
202 ret.setCookie(new FlowCookie(BigInteger.valueOf(i)));
209 * {@link org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowListener
210 * SalFlowListener} and contains attributes which keep lists of objects of
212 * {@link org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819. NodeFlow
213 * NodeFlow}. The lists are defined for flows which were added, removed or
216 private static class FlowListener implements SalFlowListener {
218 List<FlowAdded> addedFlows = new ArrayList<>();
219 List<FlowRemoved> removedFlows = new ArrayList<>();
220 List<FlowUpdated> updatedFlows = new ArrayList<>();
223 public void onFlowAdded(FlowAdded notification) {
224 addedFlows.add(notification);
228 public void onFlowRemoved(FlowRemoved notification) {
229 removedFlows.add(notification);
233 public void onFlowUpdated(FlowUpdated notification) {
234 updatedFlows.add(notification);
238 public void onSwitchFlowRemoved(SwitchFlowRemoved notification) {
239 // TODO Auto-generated method stub
244 public void onNodeErrorNotification(NodeErrorNotification notification) {
245 // TODO Auto-generated method stub
250 public void onNodeExperimenterErrorNotification(
251 NodeExperimenterErrorNotification notification) {
252 // TODO Auto-generated method stub