blob: bc0d32efcd7d5362dcfaf63edc6be501dfca2abf [file] [log] [blame]
yoonseonfe721972017-01-10 17:18:49 -08001/*
Thomas Vachuska52f2cd12018-11-08 21:20:04 -08002 * Copyright 2018-present Open Networking Foundation
yoonseonfe721972017-01-10 17:18:49 -08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Thomas Vachuska52f2cd12018-11-08 21:20:04 -080017package org.onosproject.incubator.net.virtual;
yoonseonfe721972017-01-10 17:18:49 -080018
yoonseonc6a69272017-01-12 18:22:20 -080019import com.google.common.collect.ClassToInstanceMap;
20import com.google.common.collect.MutableClassToInstanceMap;
yoonseonfe721972017-01-10 17:18:49 -080021import org.junit.After;
22import org.junit.Before;
23import org.junit.Test;
yoonseonc6a69272017-01-12 18:22:20 -080024import org.onlab.osgi.ServiceDirectory;
yoonseonfe721972017-01-10 17:18:49 -080025import org.onosproject.event.AbstractEvent;
26import org.onosproject.event.Event;
27import org.onosproject.event.EventDeliveryService;
28import org.onosproject.event.EventListener;
29import org.onosproject.event.EventSink;
Thomas Vachuska52f2cd12018-11-08 21:20:04 -080030import org.onosproject.incubator.net.virtual.event.AbstractVirtualListenerManager;
31import org.onosproject.incubator.net.virtual.event.VirtualEvent;
32import org.onosproject.incubator.net.virtual.event.VirtualListenerRegistryManager;
33import org.onosproject.net.TenantId;
yoonseonfe721972017-01-10 17:18:49 -080034
35import java.util.ArrayList;
36import java.util.List;
37import java.util.Set;
38import java.util.concurrent.CountDownLatch;
39import java.util.concurrent.TimeUnit;
40
41import static org.junit.Assert.*;
42
43/**
44 * Test of the virtual event dispatcher mechanism.
45 */
46public class AbstractVirtualListenerManagerTest {
47
Yoonseon Hanb14461b2017-03-07 14:08:01 +090048 private TestEventDispatcher dispatcher = new TestEventDispatcher();
49 private VirtualListenerRegistryManager listenerRegistryManager =
yoonseonfe721972017-01-10 17:18:49 -080050 VirtualListenerRegistryManager.getInstance();
51
Yoonseon Hanb14461b2017-03-07 14:08:01 +090052 private PrickleManager prickleManager;
53 private PrickleListener prickleListener;
yoonseonfe721972017-01-10 17:18:49 -080054
Yoonseon Hanb14461b2017-03-07 14:08:01 +090055 private GooManager gooManager;
56 private GooListener gooListener;
yoonseonfe721972017-01-10 17:18:49 -080057
Yoonseon Hanb14461b2017-03-07 14:08:01 +090058 private BarManager barManager;
59 private BarListener barListener;
yoonseonfe721972017-01-10 17:18:49 -080060
61 @Before
62 public void setUp() {
Yoonseon Hanb14461b2017-03-07 14:08:01 +090063 VirtualNetworkService manager = new TestVirtualNetworkManager();
yoonseonc6a69272017-01-12 18:22:20 -080064
yoonseonfe721972017-01-10 17:18:49 -080065 dispatcher.addSink(VirtualEvent.class, listenerRegistryManager);
66
67 prickleListener = new PrickleListener();
yoonseonc6a69272017-01-12 18:22:20 -080068 prickleManager = new PrickleManager(manager, NetworkId.networkId(1));
yoonseonfe721972017-01-10 17:18:49 -080069 prickleManager.addListener(prickleListener);
70
71 gooListener = new GooListener();
yoonseonc6a69272017-01-12 18:22:20 -080072 gooManager = new GooManager(manager, NetworkId.networkId(1));
yoonseonfe721972017-01-10 17:18:49 -080073 gooManager.addListener(gooListener);
74
75 barListener = new BarListener();
yoonseonc6a69272017-01-12 18:22:20 -080076 barManager = new BarManager(manager, NetworkId.networkId(2));
yoonseonfe721972017-01-10 17:18:49 -080077 barManager.addListener(barListener);
78 }
79
80 @After
81 public void tearDown() {
82 dispatcher.removeSink(VirtualEvent.class);
83
84 prickleListener.events.clear();
85 gooListener.events.clear();
86 barListener.events.clear();
87
88 prickleListener.latch = null;
89 gooListener.latch = null;
90 barListener.latch = null;
91 }
92
93 @Test
94 public void postPrickle() throws InterruptedException {
95 prickleListener.latch = new CountDownLatch(1);
96 prickleManager.post(new Prickle("prickle"));
97 prickleListener.latch.await(100, TimeUnit.MILLISECONDS);
98
99 validate(prickleListener, "prickle");
100 validate(gooListener);
101 validate(barListener);
102 }
103
104 @Test
105 public void postGoo() throws InterruptedException {
106 gooListener.latch = new CountDownLatch(1);
107 gooManager.post(new Goo("goo"));
108 gooListener.latch.await(100, TimeUnit.MILLISECONDS);
109
110 validate(prickleListener);
111 validate(gooListener, "goo");
112 validate(barListener);
113 }
114
115 @Test
116 public void postBar() throws InterruptedException {
117 barListener.latch = new CountDownLatch(1);
118 barManager.post(new Bar("bar"));
119 barListener.latch.await(100, TimeUnit.MILLISECONDS);
120
121 validate(prickleListener);
122 validate(gooListener);
123 validate(barListener, "bar");
124 }
125
126 @Test
127 public void postEventWithNoListener() throws Exception {
128 dispatcher.post(new Thing("boom"));
129
130 validate(prickleListener);
131 validate(gooListener);
132 validate(barListener);
133 }
134
135 private void validate(TestListener listener, String... strings) {
136 int i = 0;
137 assertEquals("incorrect event count", strings.length, listener.events.size());
138 for (String string : strings) {
139 Event event = (Event) listener.events.get(i++);
140 assertEquals("incorrect event", string, event.subject());
141 }
142 }
143
144 private enum Type { FOO }
145
146 private static class Thing extends AbstractEvent<Type, String> {
Hyunsun Moonffaeac32017-04-05 11:25:17 +0900147 protected Thing(String subject) {
yoonseonfe721972017-01-10 17:18:49 -0800148 super(Type.FOO, subject);
149 }
150 }
151
Hyunsun Moonffaeac32017-04-05 11:25:17 +0900152 private static final class Prickle extends Thing {
Yoonseon Hanb14461b2017-03-07 14:08:01 +0900153 private Prickle(String subject) {
yoonseonfe721972017-01-10 17:18:49 -0800154 super(subject);
155 }
156 }
157
Hyunsun Moonffaeac32017-04-05 11:25:17 +0900158 private static final class Goo extends Thing {
Yoonseon Hanb14461b2017-03-07 14:08:01 +0900159 private Goo(String subject) {
yoonseonfe721972017-01-10 17:18:49 -0800160 super(subject);
161 }
162 }
163
Hyunsun Moonffaeac32017-04-05 11:25:17 +0900164 private static final class Bar extends Thing {
Yoonseon Hanb14461b2017-03-07 14:08:01 +0900165 private Bar(String subject) {
yoonseonfe721972017-01-10 17:18:49 -0800166 super(subject);
167 }
168 }
169
170 private class TestListener<E extends Event> implements EventListener<E> {
171 List<E> events = new ArrayList<>();
172 CountDownLatch latch;
173
174 @Override
175 public void event(E event) {
yoonseonfe721972017-01-10 17:18:49 -0800176 events.add(event);
177 latch.countDown();
178 }
179 }
180
181 private class PrickleListener extends TestListener<Prickle> {
182 }
183
184 private class GooListener extends TestListener<Goo> {
185 }
186
187 private class BarListener extends TestListener<Bar> {
188 }
189
190 private class PrickleManager extends AbstractVirtualListenerManager<Prickle, PrickleListener> {
yoonseonc6a69272017-01-12 18:22:20 -0800191 public PrickleManager(VirtualNetworkService service, NetworkId networkId) {
Yoonseon Hanb14461b2017-03-07 14:08:01 +0900192 super(service, networkId, Prickle.class);
yoonseonfe721972017-01-10 17:18:49 -0800193 }
Thomas Vachuska52f2cd12018-11-08 21:20:04 -0800194
195 @Override
196 protected void post(Prickle event) {
197 super.post(event);
198 }
yoonseonfe721972017-01-10 17:18:49 -0800199 }
200
201 private class GooManager extends AbstractVirtualListenerManager<Goo, GooListener> {
yoonseonc6a69272017-01-12 18:22:20 -0800202 public GooManager(VirtualNetworkService service, NetworkId networkId) {
Yoonseon Hanb14461b2017-03-07 14:08:01 +0900203 super(service, networkId, Goo.class);
yoonseonfe721972017-01-10 17:18:49 -0800204 }
Thomas Vachuska52f2cd12018-11-08 21:20:04 -0800205
206 @Override
207 protected void post(Goo event) {
208 super.post(event);
209 }
yoonseonfe721972017-01-10 17:18:49 -0800210 }
211
212 private class BarManager extends AbstractVirtualListenerManager<Bar, BarListener> {
yoonseonc6a69272017-01-12 18:22:20 -0800213 public BarManager(VirtualNetworkService service, NetworkId networkId) {
Yoonseon Hanb14461b2017-03-07 14:08:01 +0900214 super(service, networkId, Bar.class);
yoonseonfe721972017-01-10 17:18:49 -0800215 }
Thomas Vachuska52f2cd12018-11-08 21:20:04 -0800216
217 @Override
218 protected void post(Bar event) {
219 super.post(event);
220 }
yoonseonfe721972017-01-10 17:18:49 -0800221 }
222
223
224 private class TestEventDispatcher implements EventDeliveryService {
225 private EventSink sink;
226
227 @Override
228 public <E extends Event> void addSink(Class<E> eventClass, EventSink<E> sink) {
229 this.sink = sink;
230 }
231
232 @Override
233 public <E extends Event> void removeSink(Class<E> eventClass) {
234 this.sink = null;
235 }
236
237 @Override
238 public <E extends Event> EventSink<E> getSink(Class<E> eventClass) {
239 return null;
240 }
241
242 @Override
243 public Set<Class<? extends Event>> getSinks() {
244 return null;
245 }
246
247 @Override
248 public void setDispatchTimeLimit(long millis) {
249
250 }
251
252 @Override
253 public long getDispatchTimeLimit() {
254 return 0;
255 }
256
257 @Override
258 public void post(Event event) {
259 if (event instanceof VirtualEvent) {
260 sink.process(event);
261 }
262 }
263 }
yoonseonc6a69272017-01-12 18:22:20 -0800264
Yoonseon Hanffcc32f2017-05-03 14:42:17 -0700265 private class TestVirtualNetworkManager extends VirtualNetworkServiceAdapter {
yoonseonc6a69272017-01-12 18:22:20 -0800266 TestServiceDirectory serviceDirectory = new TestServiceDirectory();
267
268 public TestVirtualNetworkManager() {
269 serviceDirectory.add(EventDeliveryService.class, dispatcher);
270 }
271
272 @Override
Jovana Vuletac884b692017-11-28 16:52:35 +0100273 public VirtualNetwork getVirtualNetwork(NetworkId networkId) {
274 return null;
275 }
276
277 @Override
278 public TenantId getTenantId(NetworkId networkId) {
279 return null;
280 }
281
282 @Override
yoonseonc6a69272017-01-12 18:22:20 -0800283 public ServiceDirectory getServiceDirectory() {
284 return serviceDirectory;
285 }
286 }
287
288 private class TestServiceDirectory implements ServiceDirectory {
289
290 private ClassToInstanceMap<Object> services = MutableClassToInstanceMap.create();
291
292 @Override
293 public <T> T get(Class<T> serviceClass) {
294 return services.getInstance(serviceClass);
295 }
296
297 /**
298 * Adds a new service to the directory.
299 *
300 * @param serviceClass service class
301 * @param service service instance
302 * @return self
303 */
304 public TestServiceDirectory add(Class serviceClass, Object service) {
305 services.putInstance(serviceClass, service);
306 return this;
307 }
308
309 }
yoonseonfe721972017-01-10 17:18:49 -0800310}