blob: 33fce56f6f343a2dfcdd04ac73a8108f89090d1d [file] [log] [blame]
yoonseonfe721972017-01-10 17:18:49 -08001/*
2 * Copyright 2017-present Open Networking Laboratory
3 *
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
17package org.onosproject.incubator.net.virtual.event;
18
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;
30import org.onosproject.incubator.net.virtual.NetworkId;
yoonseonc6a69272017-01-12 18:22:20 -080031import org.onosproject.incubator.net.virtual.TenantId;
32import org.onosproject.incubator.net.virtual.VirtualDevice;
33import org.onosproject.incubator.net.virtual.VirtualHost;
34import org.onosproject.incubator.net.virtual.VirtualLink;
35import org.onosproject.incubator.net.virtual.VirtualNetwork;
36import org.onosproject.incubator.net.virtual.VirtualNetworkService;
37import org.onosproject.incubator.net.virtual.VirtualPort;
38import org.onosproject.net.DeviceId;
yoonseonfe721972017-01-10 17:18:49 -080039
40import java.util.ArrayList;
41import java.util.List;
42import java.util.Set;
43import java.util.concurrent.CountDownLatch;
44import java.util.concurrent.TimeUnit;
45
46import static org.junit.Assert.*;
47
48/**
49 * Test of the virtual event dispatcher mechanism.
50 */
51public class AbstractVirtualListenerManagerTest {
52
yoonseonc6a69272017-01-12 18:22:20 -080053 private VirtualNetworkService manager;
54
yoonseonfe721972017-01-10 17:18:49 -080055 TestEventDispatcher dispatcher = new TestEventDispatcher();
56 VirtualListenerRegistryManager listenerRegistryManager =
57 VirtualListenerRegistryManager.getInstance();
58
59 PrickleManager prickleManager;
60 PrickleListener prickleListener;
61
62 GooManager gooManager;
63 GooListener gooListener;
64
65 BarManager barManager;
66 BarListener barListener;
67
68 @Before
69 public void setUp() {
yoonseonc6a69272017-01-12 18:22:20 -080070 manager = new TestVirtualNetworkManager();
71
yoonseonfe721972017-01-10 17:18:49 -080072 dispatcher.addSink(VirtualEvent.class, listenerRegistryManager);
73
74 prickleListener = new PrickleListener();
yoonseonc6a69272017-01-12 18:22:20 -080075 prickleManager = new PrickleManager(manager, NetworkId.networkId(1));
yoonseonfe721972017-01-10 17:18:49 -080076 prickleManager.addListener(prickleListener);
77
78 gooListener = new GooListener();
yoonseonc6a69272017-01-12 18:22:20 -080079 gooManager = new GooManager(manager, NetworkId.networkId(1));
yoonseonfe721972017-01-10 17:18:49 -080080 gooManager.addListener(gooListener);
81
82 barListener = new BarListener();
yoonseonc6a69272017-01-12 18:22:20 -080083 barManager = new BarManager(manager, NetworkId.networkId(2));
yoonseonfe721972017-01-10 17:18:49 -080084 barManager.addListener(barListener);
85 }
86
87 @After
88 public void tearDown() {
89 dispatcher.removeSink(VirtualEvent.class);
90
91 prickleListener.events.clear();
92 gooListener.events.clear();
93 barListener.events.clear();
94
95 prickleListener.latch = null;
96 gooListener.latch = null;
97 barListener.latch = null;
98 }
99
100 @Test
101 public void postPrickle() throws InterruptedException {
102 prickleListener.latch = new CountDownLatch(1);
103 prickleManager.post(new Prickle("prickle"));
104 prickleListener.latch.await(100, TimeUnit.MILLISECONDS);
105
106 validate(prickleListener, "prickle");
107 validate(gooListener);
108 validate(barListener);
109 }
110
111 @Test
112 public void postGoo() throws InterruptedException {
113 gooListener.latch = new CountDownLatch(1);
114 gooManager.post(new Goo("goo"));
115 gooListener.latch.await(100, TimeUnit.MILLISECONDS);
116
117 validate(prickleListener);
118 validate(gooListener, "goo");
119 validate(barListener);
120 }
121
122 @Test
123 public void postBar() throws InterruptedException {
124 barListener.latch = new CountDownLatch(1);
125 barManager.post(new Bar("bar"));
126 barListener.latch.await(100, TimeUnit.MILLISECONDS);
127
128 validate(prickleListener);
129 validate(gooListener);
130 validate(barListener, "bar");
131 }
132
133 @Test
134 public void postEventWithNoListener() throws Exception {
135 dispatcher.post(new Thing("boom"));
136
137 validate(prickleListener);
138 validate(gooListener);
139 validate(barListener);
140 }
141
142 private void validate(TestListener listener, String... strings) {
143 int i = 0;
144 assertEquals("incorrect event count", strings.length, listener.events.size());
145 for (String string : strings) {
146 Event event = (Event) listener.events.get(i++);
147 assertEquals("incorrect event", string, event.subject());
148 }
149 }
150
151 private enum Type { FOO }
152
153 private static class Thing extends AbstractEvent<Type, String> {
154 protected Thing(String subject) {
155 super(Type.FOO, subject);
156 }
157 }
158
159 private static class Prickle extends Thing {
160 protected Prickle(String subject) {
161 super(subject);
162 }
163 }
164
165 private static class Goo extends Thing {
166 protected Goo(String subject) {
167 super(subject);
168 }
169 }
170
171 private static class Bar extends Thing {
172 protected Bar(String subject) {
173 super(subject);
174 }
175 }
176
177 private class TestListener<E extends Event> implements EventListener<E> {
178 List<E> events = new ArrayList<>();
179 CountDownLatch latch;
180
181 @Override
182 public void event(E event) {
yoonseonfe721972017-01-10 17:18:49 -0800183 events.add(event);
184 latch.countDown();
185 }
186 }
187
188 private class PrickleListener extends TestListener<Prickle> {
189 }
190
191 private class GooListener extends TestListener<Goo> {
192 }
193
194 private class BarListener extends TestListener<Bar> {
195 }
196
197 private class PrickleManager extends AbstractVirtualListenerManager<Prickle, PrickleListener> {
yoonseonc6a69272017-01-12 18:22:20 -0800198 public PrickleManager(VirtualNetworkService service, NetworkId networkId) {
199 super(service, networkId);
yoonseonfe721972017-01-10 17:18:49 -0800200 }
201 }
202
203 private class GooManager extends AbstractVirtualListenerManager<Goo, GooListener> {
yoonseonc6a69272017-01-12 18:22:20 -0800204 public GooManager(VirtualNetworkService service, NetworkId networkId) {
205 super(service, networkId);
yoonseonfe721972017-01-10 17:18:49 -0800206 }
207 }
208
209 private class BarManager extends AbstractVirtualListenerManager<Bar, BarListener> {
yoonseonc6a69272017-01-12 18:22:20 -0800210 public BarManager(VirtualNetworkService service, NetworkId networkId) {
211 super(service, networkId);
yoonseonfe721972017-01-10 17:18:49 -0800212 }
213 }
214
215
216 private class TestEventDispatcher implements EventDeliveryService {
217 private EventSink sink;
218
219 @Override
220 public <E extends Event> void addSink(Class<E> eventClass, EventSink<E> sink) {
221 this.sink = sink;
222 }
223
224 @Override
225 public <E extends Event> void removeSink(Class<E> eventClass) {
226 this.sink = null;
227 }
228
229 @Override
230 public <E extends Event> EventSink<E> getSink(Class<E> eventClass) {
231 return null;
232 }
233
234 @Override
235 public Set<Class<? extends Event>> getSinks() {
236 return null;
237 }
238
239 @Override
240 public void setDispatchTimeLimit(long millis) {
241
242 }
243
244 @Override
245 public long getDispatchTimeLimit() {
246 return 0;
247 }
248
249 @Override
250 public void post(Event event) {
251 if (event instanceof VirtualEvent) {
252 sink.process(event);
253 }
254 }
255 }
yoonseonc6a69272017-01-12 18:22:20 -0800256
257 private class TestVirtualNetworkManager implements VirtualNetworkService {
258 TestServiceDirectory serviceDirectory = new TestServiceDirectory();
259
260 public TestVirtualNetworkManager() {
261 serviceDirectory.add(EventDeliveryService.class, dispatcher);
262 }
263
264 @Override
265 public Set<VirtualNetwork> getVirtualNetworks(TenantId tenantId) {
266 return null;
267 }
268
269 @Override
270 public Set<VirtualDevice> getVirtualDevices(NetworkId networkId) {
271 return null;
272 }
273
274 @Override
275 public Set<VirtualHost> getVirtualHosts(NetworkId networkId) {
276 return null;
277 }
278
279 @Override
280 public Set<VirtualLink> getVirtualLinks(NetworkId networkId) {
281 return null;
282 }
283
284 @Override
285 public Set<VirtualPort> getVirtualPorts(NetworkId networkId, DeviceId deviceId) {
286 return null;
287 }
288
289 @Override
290 public <T> T get(NetworkId networkId, Class<T> serviceClass) {
291 return null;
292 }
293
294 @Override
295 public ServiceDirectory getServiceDirectory() {
296 return serviceDirectory;
297 }
298 }
299
300 private class TestServiceDirectory implements ServiceDirectory {
301
302 private ClassToInstanceMap<Object> services = MutableClassToInstanceMap.create();
303
304 @Override
305 public <T> T get(Class<T> serviceClass) {
306 return services.getInstance(serviceClass);
307 }
308
309 /**
310 * Adds a new service to the directory.
311 *
312 * @param serviceClass service class
313 * @param service service instance
314 * @return self
315 */
316 public TestServiceDirectory add(Class serviceClass, Object service) {
317 services.putInstance(serviceClass, service);
318 return this;
319 }
320
321 }
yoonseonfe721972017-01-10 17:18:49 -0800322}