yoonseon | fe72197 | 2017-01-10 17:18:49 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.incubator.net.virtual.event; |
| 18 | |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 19 | import com.google.common.collect.ClassToInstanceMap; |
| 20 | import com.google.common.collect.MutableClassToInstanceMap; |
yoonseon | fe72197 | 2017-01-10 17:18:49 -0800 | [diff] [blame] | 21 | import org.junit.After; |
| 22 | import org.junit.Before; |
| 23 | import org.junit.Test; |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 24 | import org.onlab.osgi.ServiceDirectory; |
yoonseon | 322c9c3 | 2016-12-07 16:47:02 -0800 | [diff] [blame] | 25 | import org.onosproject.core.ApplicationId; |
yoonseon | fe72197 | 2017-01-10 17:18:49 -0800 | [diff] [blame] | 26 | import org.onosproject.event.AbstractEvent; |
| 27 | import org.onosproject.event.Event; |
| 28 | import org.onosproject.event.EventDeliveryService; |
| 29 | import org.onosproject.event.EventListener; |
| 30 | import org.onosproject.event.EventSink; |
| 31 | import org.onosproject.incubator.net.virtual.NetworkId; |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 32 | import org.onosproject.incubator.net.virtual.TenantId; |
| 33 | import org.onosproject.incubator.net.virtual.VirtualDevice; |
| 34 | import org.onosproject.incubator.net.virtual.VirtualHost; |
| 35 | import org.onosproject.incubator.net.virtual.VirtualLink; |
| 36 | import org.onosproject.incubator.net.virtual.VirtualNetwork; |
Yoonseon Han | 33821fb | 2017-02-24 13:40:44 +0900 | [diff] [blame^] | 37 | import org.onosproject.incubator.net.virtual.VirtualNetworkListener; |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 38 | import org.onosproject.incubator.net.virtual.VirtualNetworkService; |
| 39 | import org.onosproject.incubator.net.virtual.VirtualPort; |
| 40 | import org.onosproject.net.DeviceId; |
yoonseon | fe72197 | 2017-01-10 17:18:49 -0800 | [diff] [blame] | 41 | |
| 42 | import java.util.ArrayList; |
| 43 | import java.util.List; |
| 44 | import java.util.Set; |
| 45 | import java.util.concurrent.CountDownLatch; |
| 46 | import java.util.concurrent.TimeUnit; |
| 47 | |
| 48 | import static org.junit.Assert.*; |
| 49 | |
| 50 | /** |
| 51 | * Test of the virtual event dispatcher mechanism. |
| 52 | */ |
| 53 | public class AbstractVirtualListenerManagerTest { |
| 54 | |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 55 | private VirtualNetworkService manager; |
| 56 | |
yoonseon | fe72197 | 2017-01-10 17:18:49 -0800 | [diff] [blame] | 57 | 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() { |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 72 | manager = new TestVirtualNetworkManager(); |
| 73 | |
yoonseon | fe72197 | 2017-01-10 17:18:49 -0800 | [diff] [blame] | 74 | dispatcher.addSink(VirtualEvent.class, listenerRegistryManager); |
| 75 | |
| 76 | prickleListener = new PrickleListener(); |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 77 | prickleManager = new PrickleManager(manager, NetworkId.networkId(1)); |
yoonseon | fe72197 | 2017-01-10 17:18:49 -0800 | [diff] [blame] | 78 | prickleManager.addListener(prickleListener); |
| 79 | |
| 80 | gooListener = new GooListener(); |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 81 | gooManager = new GooManager(manager, NetworkId.networkId(1)); |
yoonseon | fe72197 | 2017-01-10 17:18:49 -0800 | [diff] [blame] | 82 | gooManager.addListener(gooListener); |
| 83 | |
| 84 | barListener = new BarListener(); |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 85 | barManager = new BarManager(manager, NetworkId.networkId(2)); |
yoonseon | fe72197 | 2017-01-10 17:18:49 -0800 | [diff] [blame] | 86 | 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) { |
yoonseon | fe72197 | 2017-01-10 17:18:49 -0800 | [diff] [blame] | 185 | 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> { |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 200 | public PrickleManager(VirtualNetworkService service, NetworkId networkId) { |
| 201 | super(service, networkId); |
yoonseon | fe72197 | 2017-01-10 17:18:49 -0800 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
| 205 | private class GooManager extends AbstractVirtualListenerManager<Goo, GooListener> { |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 206 | public GooManager(VirtualNetworkService service, NetworkId networkId) { |
| 207 | super(service, networkId); |
yoonseon | fe72197 | 2017-01-10 17:18:49 -0800 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | |
| 211 | private class BarManager extends AbstractVirtualListenerManager<Bar, BarListener> { |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 212 | public BarManager(VirtualNetworkService service, NetworkId networkId) { |
| 213 | super(service, networkId); |
yoonseon | fe72197 | 2017-01-10 17:18:49 -0800 | [diff] [blame] | 214 | } |
| 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 | } |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 258 | |
| 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 | } |
yoonseon | 322c9c3 | 2016-12-07 16:47:02 -0800 | [diff] [blame] | 300 | |
| 301 | @Override |
| 302 | public ApplicationId getVirtualNetworkApplicationId(NetworkId networkId) { |
| 303 | return null; |
| 304 | } |
Yoonseon Han | 33821fb | 2017-02-24 13:40:44 +0900 | [diff] [blame^] | 305 | |
| 306 | @Override |
| 307 | public void addListener(VirtualNetworkListener listener) { |
| 308 | |
| 309 | } |
| 310 | |
| 311 | @Override |
| 312 | public void removeListener(VirtualNetworkListener listener) { |
| 313 | |
| 314 | } |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 315 | } |
| 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 | } |
yoonseon | fe72197 | 2017-01-10 17:18:49 -0800 | [diff] [blame] | 339 | } |