blob: 23934b58aed09024364b054f1d06f834cec39e40 [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;
yoonseon322c9c32016-12-07 16:47:02 -080025import org.onosproject.core.ApplicationId;
yoonseonfe721972017-01-10 17:18:49 -080026import org.onosproject.event.AbstractEvent;
27import org.onosproject.event.Event;
28import org.onosproject.event.EventDeliveryService;
29import org.onosproject.event.EventListener;
30import org.onosproject.event.EventSink;
31import org.onosproject.incubator.net.virtual.NetworkId;
yoonseonc6a69272017-01-12 18:22:20 -080032import org.onosproject.incubator.net.virtual.TenantId;
33import org.onosproject.incubator.net.virtual.VirtualDevice;
34import org.onosproject.incubator.net.virtual.VirtualHost;
35import org.onosproject.incubator.net.virtual.VirtualLink;
36import org.onosproject.incubator.net.virtual.VirtualNetwork;
Yoonseon Han33821fb2017-02-24 13:40:44 +090037import org.onosproject.incubator.net.virtual.VirtualNetworkListener;
yoonseonc6a69272017-01-12 18:22:20 -080038import org.onosproject.incubator.net.virtual.VirtualNetworkService;
39import org.onosproject.incubator.net.virtual.VirtualPort;
40import org.onosproject.net.DeviceId;
yoonseonfe721972017-01-10 17:18:49 -080041
42import java.util.ArrayList;
43import java.util.List;
44import java.util.Set;
45import java.util.concurrent.CountDownLatch;
46import java.util.concurrent.TimeUnit;
47
48import static org.junit.Assert.*;
49
50/**
51 * Test of the virtual event dispatcher mechanism.
52 */
53public class AbstractVirtualListenerManagerTest {
54
yoonseonc6a69272017-01-12 18:22:20 -080055 private VirtualNetworkService manager;
56
yoonseonfe721972017-01-10 17:18:49 -080057 TestEventDispatcher dispatcher = new TestEventDispatcher();
58 VirtualListenerRegistryManager listenerRegistryManager =
59 VirtualListenerRegistryManager.getInstance();
60
61 PrickleManager prickleManager;
62 PrickleListener prickleListener;
63
64 GooManager gooManager;
65 GooListener gooListener;
66
67 BarManager barManager;
68 BarListener barListener;
69
70 @Before
71 public void setUp() {
yoonseonc6a69272017-01-12 18:22:20 -080072 manager = new TestVirtualNetworkManager();
73
yoonseonfe721972017-01-10 17:18:49 -080074 dispatcher.addSink(VirtualEvent.class, listenerRegistryManager);
75
76 prickleListener = new PrickleListener();
yoonseonc6a69272017-01-12 18:22:20 -080077 prickleManager = new PrickleManager(manager, NetworkId.networkId(1));
yoonseonfe721972017-01-10 17:18:49 -080078 prickleManager.addListener(prickleListener);
79
80 gooListener = new GooListener();
yoonseonc6a69272017-01-12 18:22:20 -080081 gooManager = new GooManager(manager, NetworkId.networkId(1));
yoonseonfe721972017-01-10 17:18:49 -080082 gooManager.addListener(gooListener);
83
84 barListener = new BarListener();
yoonseonc6a69272017-01-12 18:22:20 -080085 barManager = new BarManager(manager, NetworkId.networkId(2));
yoonseonfe721972017-01-10 17:18:49 -080086 barManager.addListener(barListener);
87 }
88
89 @After
90 public void tearDown() {
91 dispatcher.removeSink(VirtualEvent.class);
92
93 prickleListener.events.clear();
94 gooListener.events.clear();
95 barListener.events.clear();
96
97 prickleListener.latch = null;
98 gooListener.latch = null;
99 barListener.latch = null;
100 }
101
102 @Test
103 public void postPrickle() throws InterruptedException {
104 prickleListener.latch = new CountDownLatch(1);
105 prickleManager.post(new Prickle("prickle"));
106 prickleListener.latch.await(100, TimeUnit.MILLISECONDS);
107
108 validate(prickleListener, "prickle");
109 validate(gooListener);
110 validate(barListener);
111 }
112
113 @Test
114 public void postGoo() throws InterruptedException {
115 gooListener.latch = new CountDownLatch(1);
116 gooManager.post(new Goo("goo"));
117 gooListener.latch.await(100, TimeUnit.MILLISECONDS);
118
119 validate(prickleListener);
120 validate(gooListener, "goo");
121 validate(barListener);
122 }
123
124 @Test
125 public void postBar() throws InterruptedException {
126 barListener.latch = new CountDownLatch(1);
127 barManager.post(new Bar("bar"));
128 barListener.latch.await(100, TimeUnit.MILLISECONDS);
129
130 validate(prickleListener);
131 validate(gooListener);
132 validate(barListener, "bar");
133 }
134
135 @Test
136 public void postEventWithNoListener() throws Exception {
137 dispatcher.post(new Thing("boom"));
138
139 validate(prickleListener);
140 validate(gooListener);
141 validate(barListener);
142 }
143
144 private void validate(TestListener listener, String... strings) {
145 int i = 0;
146 assertEquals("incorrect event count", strings.length, listener.events.size());
147 for (String string : strings) {
148 Event event = (Event) listener.events.get(i++);
149 assertEquals("incorrect event", string, event.subject());
150 }
151 }
152
153 private enum Type { FOO }
154
155 private static class Thing extends AbstractEvent<Type, String> {
156 protected Thing(String subject) {
157 super(Type.FOO, subject);
158 }
159 }
160
161 private static class Prickle extends Thing {
162 protected Prickle(String subject) {
163 super(subject);
164 }
165 }
166
167 private static class Goo extends Thing {
168 protected Goo(String subject) {
169 super(subject);
170 }
171 }
172
173 private static class Bar extends Thing {
174 protected Bar(String subject) {
175 super(subject);
176 }
177 }
178
179 private class TestListener<E extends Event> implements EventListener<E> {
180 List<E> events = new ArrayList<>();
181 CountDownLatch latch;
182
183 @Override
184 public void event(E event) {
yoonseonfe721972017-01-10 17:18:49 -0800185 events.add(event);
186 latch.countDown();
187 }
188 }
189
190 private class PrickleListener extends TestListener<Prickle> {
191 }
192
193 private class GooListener extends TestListener<Goo> {
194 }
195
196 private class BarListener extends TestListener<Bar> {
197 }
198
199 private class PrickleManager extends AbstractVirtualListenerManager<Prickle, PrickleListener> {
yoonseonc6a69272017-01-12 18:22:20 -0800200 public PrickleManager(VirtualNetworkService service, NetworkId networkId) {
201 super(service, networkId);
yoonseonfe721972017-01-10 17:18:49 -0800202 }
203 }
204
205 private class GooManager extends AbstractVirtualListenerManager<Goo, GooListener> {
yoonseonc6a69272017-01-12 18:22:20 -0800206 public GooManager(VirtualNetworkService service, NetworkId networkId) {
207 super(service, networkId);
yoonseonfe721972017-01-10 17:18:49 -0800208 }
209 }
210
211 private class BarManager extends AbstractVirtualListenerManager<Bar, BarListener> {
yoonseonc6a69272017-01-12 18:22:20 -0800212 public BarManager(VirtualNetworkService service, NetworkId networkId) {
213 super(service, networkId);
yoonseonfe721972017-01-10 17:18:49 -0800214 }
215 }
216
217
218 private class TestEventDispatcher implements EventDeliveryService {
219 private EventSink sink;
220
221 @Override
222 public <E extends Event> void addSink(Class<E> eventClass, EventSink<E> sink) {
223 this.sink = sink;
224 }
225
226 @Override
227 public <E extends Event> void removeSink(Class<E> eventClass) {
228 this.sink = null;
229 }
230
231 @Override
232 public <E extends Event> EventSink<E> getSink(Class<E> eventClass) {
233 return null;
234 }
235
236 @Override
237 public Set<Class<? extends Event>> getSinks() {
238 return null;
239 }
240
241 @Override
242 public void setDispatchTimeLimit(long millis) {
243
244 }
245
246 @Override
247 public long getDispatchTimeLimit() {
248 return 0;
249 }
250
251 @Override
252 public void post(Event event) {
253 if (event instanceof VirtualEvent) {
254 sink.process(event);
255 }
256 }
257 }
yoonseonc6a69272017-01-12 18:22:20 -0800258
259 private class TestVirtualNetworkManager implements VirtualNetworkService {
260 TestServiceDirectory serviceDirectory = new TestServiceDirectory();
261
262 public TestVirtualNetworkManager() {
263 serviceDirectory.add(EventDeliveryService.class, dispatcher);
264 }
265
266 @Override
267 public Set<VirtualNetwork> getVirtualNetworks(TenantId tenantId) {
268 return null;
269 }
270
271 @Override
272 public Set<VirtualDevice> getVirtualDevices(NetworkId networkId) {
273 return null;
274 }
275
276 @Override
277 public Set<VirtualHost> getVirtualHosts(NetworkId networkId) {
278 return null;
279 }
280
281 @Override
282 public Set<VirtualLink> getVirtualLinks(NetworkId networkId) {
283 return null;
284 }
285
286 @Override
287 public Set<VirtualPort> getVirtualPorts(NetworkId networkId, DeviceId deviceId) {
288 return null;
289 }
290
291 @Override
292 public <T> T get(NetworkId networkId, Class<T> serviceClass) {
293 return null;
294 }
295
296 @Override
297 public ServiceDirectory getServiceDirectory() {
298 return serviceDirectory;
299 }
yoonseon322c9c32016-12-07 16:47:02 -0800300
301 @Override
302 public ApplicationId getVirtualNetworkApplicationId(NetworkId networkId) {
303 return null;
304 }
Yoonseon Han33821fb2017-02-24 13:40:44 +0900305
306 @Override
307 public void addListener(VirtualNetworkListener listener) {
308
309 }
310
311 @Override
312 public void removeListener(VirtualNetworkListener listener) {
313
314 }
yoonseonc6a69272017-01-12 18:22:20 -0800315 }
316
317 private class TestServiceDirectory implements ServiceDirectory {
318
319 private ClassToInstanceMap<Object> services = MutableClassToInstanceMap.create();
320
321 @Override
322 public <T> T get(Class<T> serviceClass) {
323 return services.getInstance(serviceClass);
324 }
325
326 /**
327 * Adds a new service to the directory.
328 *
329 * @param serviceClass service class
330 * @param service service instance
331 * @return self
332 */
333 public TestServiceDirectory add(Class serviceClass, Object service) {
334 services.putInstance(serviceClass, service);
335 return this;
336 }
337
338 }
yoonseonfe721972017-01-10 17:18:49 -0800339}