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