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