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 | fe72197 | 2017-01-10 17:18:49 -0800 | [diff] [blame] | 25 | import org.onosproject.event.AbstractEvent; |
| 26 | import org.onosproject.event.Event; |
| 27 | import org.onosproject.event.EventDeliveryService; |
| 28 | import org.onosproject.event.EventListener; |
| 29 | import org.onosproject.event.EventSink; |
| 30 | import org.onosproject.incubator.net.virtual.NetworkId; |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame^] | 31 | import org.onosproject.incubator.net.virtual.TenantId; |
| 32 | import org.onosproject.incubator.net.virtual.VirtualDevice; |
| 33 | import org.onosproject.incubator.net.virtual.VirtualHost; |
| 34 | import org.onosproject.incubator.net.virtual.VirtualLink; |
| 35 | import org.onosproject.incubator.net.virtual.VirtualNetwork; |
| 36 | import org.onosproject.incubator.net.virtual.VirtualNetworkService; |
| 37 | import org.onosproject.incubator.net.virtual.VirtualPort; |
| 38 | import org.onosproject.net.DeviceId; |
yoonseon | fe72197 | 2017-01-10 17:18:49 -0800 | [diff] [blame] | 39 | |
| 40 | import java.util.ArrayList; |
| 41 | import java.util.List; |
| 42 | import java.util.Set; |
| 43 | import java.util.concurrent.CountDownLatch; |
| 44 | import java.util.concurrent.TimeUnit; |
| 45 | |
| 46 | import static org.junit.Assert.*; |
| 47 | |
| 48 | /** |
| 49 | * Test of the virtual event dispatcher mechanism. |
| 50 | */ |
| 51 | public class AbstractVirtualListenerManagerTest { |
| 52 | |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame^] | 53 | private VirtualNetworkService manager; |
| 54 | |
yoonseon | fe72197 | 2017-01-10 17:18:49 -0800 | [diff] [blame] | 55 | 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() { |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame^] | 70 | manager = new TestVirtualNetworkManager(); |
| 71 | |
yoonseon | fe72197 | 2017-01-10 17:18:49 -0800 | [diff] [blame] | 72 | dispatcher.addSink(VirtualEvent.class, listenerRegistryManager); |
| 73 | |
| 74 | prickleListener = new PrickleListener(); |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame^] | 75 | prickleManager = new PrickleManager(manager, NetworkId.networkId(1)); |
yoonseon | fe72197 | 2017-01-10 17:18:49 -0800 | [diff] [blame] | 76 | prickleManager.addListener(prickleListener); |
| 77 | |
| 78 | gooListener = new GooListener(); |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame^] | 79 | gooManager = new GooManager(manager, NetworkId.networkId(1)); |
yoonseon | fe72197 | 2017-01-10 17:18:49 -0800 | [diff] [blame] | 80 | gooManager.addListener(gooListener); |
| 81 | |
| 82 | barListener = new BarListener(); |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame^] | 83 | barManager = new BarManager(manager, NetworkId.networkId(2)); |
yoonseon | fe72197 | 2017-01-10 17:18:49 -0800 | [diff] [blame] | 84 | 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) { |
yoonseon | fe72197 | 2017-01-10 17:18:49 -0800 | [diff] [blame] | 183 | 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> { |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame^] | 198 | public PrickleManager(VirtualNetworkService service, NetworkId networkId) { |
| 199 | super(service, networkId); |
yoonseon | fe72197 | 2017-01-10 17:18:49 -0800 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | |
| 203 | private class GooManager extends AbstractVirtualListenerManager<Goo, GooListener> { |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame^] | 204 | public GooManager(VirtualNetworkService service, NetworkId networkId) { |
| 205 | super(service, networkId); |
yoonseon | fe72197 | 2017-01-10 17:18:49 -0800 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | |
| 209 | private class BarManager extends AbstractVirtualListenerManager<Bar, BarListener> { |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame^] | 210 | public BarManager(VirtualNetworkService service, NetworkId networkId) { |
| 211 | super(service, networkId); |
yoonseon | fe72197 | 2017-01-10 17:18:49 -0800 | [diff] [blame] | 212 | } |
| 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 | } |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame^] | 256 | |
| 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 | } |
yoonseon | fe72197 | 2017-01-10 17:18:49 -0800 | [diff] [blame] | 322 | } |