blob: 299d8cb529e3c00b0fbad6353f78209724bcca91 [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.VirtualNetworkService;
Yoonseon Hanffcc32f2017-05-03 14:42:17 -070032import org.onosproject.incubator.net.virtual.VirtualNetworkServiceAdapter;
yoonseonfe721972017-01-10 17:18:49 -080033
34import java.util.ArrayList;
35import java.util.List;
36import java.util.Set;
37import java.util.concurrent.CountDownLatch;
38import java.util.concurrent.TimeUnit;
39
40import static org.junit.Assert.*;
41
42/**
43 * Test of the virtual event dispatcher mechanism.
44 */
45public class AbstractVirtualListenerManagerTest {
46
Yoonseon Hanb14461b2017-03-07 14:08:01 +090047 private TestEventDispatcher dispatcher = new TestEventDispatcher();
48 private VirtualListenerRegistryManager listenerRegistryManager =
yoonseonfe721972017-01-10 17:18:49 -080049 VirtualListenerRegistryManager.getInstance();
50
Yoonseon Hanb14461b2017-03-07 14:08:01 +090051 private PrickleManager prickleManager;
52 private PrickleListener prickleListener;
yoonseonfe721972017-01-10 17:18:49 -080053
Yoonseon Hanb14461b2017-03-07 14:08:01 +090054 private GooManager gooManager;
55 private GooListener gooListener;
yoonseonfe721972017-01-10 17:18:49 -080056
Yoonseon Hanb14461b2017-03-07 14:08:01 +090057 private BarManager barManager;
58 private BarListener barListener;
yoonseonfe721972017-01-10 17:18:49 -080059
60 @Before
61 public void setUp() {
Yoonseon Hanb14461b2017-03-07 14:08:01 +090062 VirtualNetworkService manager = new TestVirtualNetworkManager();
yoonseonc6a69272017-01-12 18:22:20 -080063
yoonseonfe721972017-01-10 17:18:49 -080064 dispatcher.addSink(VirtualEvent.class, listenerRegistryManager);
65
66 prickleListener = new PrickleListener();
yoonseonc6a69272017-01-12 18:22:20 -080067 prickleManager = new PrickleManager(manager, NetworkId.networkId(1));
yoonseonfe721972017-01-10 17:18:49 -080068 prickleManager.addListener(prickleListener);
69
70 gooListener = new GooListener();
yoonseonc6a69272017-01-12 18:22:20 -080071 gooManager = new GooManager(manager, NetworkId.networkId(1));
yoonseonfe721972017-01-10 17:18:49 -080072 gooManager.addListener(gooListener);
73
74 barListener = new BarListener();
yoonseonc6a69272017-01-12 18:22:20 -080075 barManager = new BarManager(manager, NetworkId.networkId(2));
yoonseonfe721972017-01-10 17:18:49 -080076 barManager.addListener(barListener);
77 }
78
79 @After
80 public void tearDown() {
81 dispatcher.removeSink(VirtualEvent.class);
82
83 prickleListener.events.clear();
84 gooListener.events.clear();
85 barListener.events.clear();
86
87 prickleListener.latch = null;
88 gooListener.latch = null;
89 barListener.latch = null;
90 }
91
92 @Test
93 public void postPrickle() throws InterruptedException {
94 prickleListener.latch = new CountDownLatch(1);
95 prickleManager.post(new Prickle("prickle"));
96 prickleListener.latch.await(100, TimeUnit.MILLISECONDS);
97
98 validate(prickleListener, "prickle");
99 validate(gooListener);
100 validate(barListener);
101 }
102
103 @Test
104 public void postGoo() throws InterruptedException {
105 gooListener.latch = new CountDownLatch(1);
106 gooManager.post(new Goo("goo"));
107 gooListener.latch.await(100, TimeUnit.MILLISECONDS);
108
109 validate(prickleListener);
110 validate(gooListener, "goo");
111 validate(barListener);
112 }
113
114 @Test
115 public void postBar() throws InterruptedException {
116 barListener.latch = new CountDownLatch(1);
117 barManager.post(new Bar("bar"));
118 barListener.latch.await(100, TimeUnit.MILLISECONDS);
119
120 validate(prickleListener);
121 validate(gooListener);
122 validate(barListener, "bar");
123 }
124
125 @Test
126 public void postEventWithNoListener() throws Exception {
127 dispatcher.post(new Thing("boom"));
128
129 validate(prickleListener);
130 validate(gooListener);
131 validate(barListener);
132 }
133
134 private void validate(TestListener listener, String... strings) {
135 int i = 0;
136 assertEquals("incorrect event count", strings.length, listener.events.size());
137 for (String string : strings) {
138 Event event = (Event) listener.events.get(i++);
139 assertEquals("incorrect event", string, event.subject());
140 }
141 }
142
143 private enum Type { FOO }
144
145 private static class Thing extends AbstractEvent<Type, String> {
Hyunsun Moonffaeac32017-04-05 11:25:17 +0900146 protected Thing(String subject) {
yoonseonfe721972017-01-10 17:18:49 -0800147 super(Type.FOO, subject);
148 }
149 }
150
Hyunsun Moonffaeac32017-04-05 11:25:17 +0900151 private static final class Prickle extends Thing {
Yoonseon Hanb14461b2017-03-07 14:08:01 +0900152 private Prickle(String subject) {
yoonseonfe721972017-01-10 17:18:49 -0800153 super(subject);
154 }
155 }
156
Hyunsun Moonffaeac32017-04-05 11:25:17 +0900157 private static final class Goo extends Thing {
Yoonseon Hanb14461b2017-03-07 14:08:01 +0900158 private Goo(String subject) {
yoonseonfe721972017-01-10 17:18:49 -0800159 super(subject);
160 }
161 }
162
Hyunsun Moonffaeac32017-04-05 11:25:17 +0900163 private static final class Bar extends Thing {
Yoonseon Hanb14461b2017-03-07 14:08:01 +0900164 private Bar(String subject) {
yoonseonfe721972017-01-10 17:18:49 -0800165 super(subject);
166 }
167 }
168
169 private class TestListener<E extends Event> implements EventListener<E> {
170 List<E> events = new ArrayList<>();
171 CountDownLatch latch;
172
173 @Override
174 public void event(E event) {
yoonseonfe721972017-01-10 17:18:49 -0800175 events.add(event);
176 latch.countDown();
177 }
178 }
179
180 private class PrickleListener extends TestListener<Prickle> {
181 }
182
183 private class GooListener extends TestListener<Goo> {
184 }
185
186 private class BarListener extends TestListener<Bar> {
187 }
188
189 private class PrickleManager extends AbstractVirtualListenerManager<Prickle, PrickleListener> {
yoonseonc6a69272017-01-12 18:22:20 -0800190 public PrickleManager(VirtualNetworkService service, NetworkId networkId) {
Yoonseon Hanb14461b2017-03-07 14:08:01 +0900191 super(service, networkId, Prickle.class);
yoonseonfe721972017-01-10 17:18:49 -0800192 }
193 }
194
195 private class GooManager extends AbstractVirtualListenerManager<Goo, GooListener> {
yoonseonc6a69272017-01-12 18:22:20 -0800196 public GooManager(VirtualNetworkService service, NetworkId networkId) {
Yoonseon Hanb14461b2017-03-07 14:08:01 +0900197 super(service, networkId, Goo.class);
yoonseonfe721972017-01-10 17:18:49 -0800198 }
199 }
200
201 private class BarManager extends AbstractVirtualListenerManager<Bar, BarListener> {
yoonseonc6a69272017-01-12 18:22:20 -0800202 public BarManager(VirtualNetworkService service, NetworkId networkId) {
Yoonseon Hanb14461b2017-03-07 14:08:01 +0900203 super(service, networkId, Bar.class);
yoonseonfe721972017-01-10 17:18:49 -0800204 }
205 }
206
207
208 private class TestEventDispatcher implements EventDeliveryService {
209 private EventSink sink;
210
211 @Override
212 public <E extends Event> void addSink(Class<E> eventClass, EventSink<E> sink) {
213 this.sink = sink;
214 }
215
216 @Override
217 public <E extends Event> void removeSink(Class<E> eventClass) {
218 this.sink = null;
219 }
220
221 @Override
222 public <E extends Event> EventSink<E> getSink(Class<E> eventClass) {
223 return null;
224 }
225
226 @Override
227 public Set<Class<? extends Event>> getSinks() {
228 return null;
229 }
230
231 @Override
232 public void setDispatchTimeLimit(long millis) {
233
234 }
235
236 @Override
237 public long getDispatchTimeLimit() {
238 return 0;
239 }
240
241 @Override
242 public void post(Event event) {
243 if (event instanceof VirtualEvent) {
244 sink.process(event);
245 }
246 }
247 }
yoonseonc6a69272017-01-12 18:22:20 -0800248
Yoonseon Hanffcc32f2017-05-03 14:42:17 -0700249 private class TestVirtualNetworkManager extends VirtualNetworkServiceAdapter {
yoonseonc6a69272017-01-12 18:22:20 -0800250 TestServiceDirectory serviceDirectory = new TestServiceDirectory();
251
252 public TestVirtualNetworkManager() {
253 serviceDirectory.add(EventDeliveryService.class, dispatcher);
254 }
255
256 @Override
yoonseonc6a69272017-01-12 18:22:20 -0800257 public ServiceDirectory getServiceDirectory() {
258 return serviceDirectory;
259 }
260 }
261
262 private class TestServiceDirectory implements ServiceDirectory {
263
264 private ClassToInstanceMap<Object> services = MutableClassToInstanceMap.create();
265
266 @Override
267 public <T> T get(Class<T> serviceClass) {
268 return services.getInstance(serviceClass);
269 }
270
271 /**
272 * Adds a new service to the directory.
273 *
274 * @param serviceClass service class
275 * @param service service instance
276 * @return self
277 */
278 public TestServiceDirectory add(Class serviceClass, Object service) {
279 services.putInstance(serviceClass, service);
280 return this;
281 }
282
283 }
yoonseonfe721972017-01-10 17:18:49 -0800284}