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; |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 31 | import org.onosproject.incubator.net.virtual.VirtualNetworkService; |
Yoonseon Han | ffcc32f | 2017-05-03 14:42:17 -0700 | [diff] [blame] | 32 | import org.onosproject.incubator.net.virtual.VirtualNetworkServiceAdapter; |
yoonseon | fe72197 | 2017-01-10 17:18:49 -0800 | [diff] [blame] | 33 | |
| 34 | import java.util.ArrayList; |
| 35 | import java.util.List; |
| 36 | import java.util.Set; |
| 37 | import java.util.concurrent.CountDownLatch; |
| 38 | import java.util.concurrent.TimeUnit; |
| 39 | |
| 40 | import static org.junit.Assert.*; |
| 41 | |
| 42 | /** |
| 43 | * Test of the virtual event dispatcher mechanism. |
| 44 | */ |
| 45 | public class AbstractVirtualListenerManagerTest { |
| 46 | |
Yoonseon Han | b14461b | 2017-03-07 14:08:01 +0900 | [diff] [blame] | 47 | private TestEventDispatcher dispatcher = new TestEventDispatcher(); |
| 48 | private VirtualListenerRegistryManager listenerRegistryManager = |
yoonseon | fe72197 | 2017-01-10 17:18:49 -0800 | [diff] [blame] | 49 | VirtualListenerRegistryManager.getInstance(); |
| 50 | |
Yoonseon Han | b14461b | 2017-03-07 14:08:01 +0900 | [diff] [blame] | 51 | private PrickleManager prickleManager; |
| 52 | private PrickleListener prickleListener; |
yoonseon | fe72197 | 2017-01-10 17:18:49 -0800 | [diff] [blame] | 53 | |
Yoonseon Han | b14461b | 2017-03-07 14:08:01 +0900 | [diff] [blame] | 54 | private GooManager gooManager; |
| 55 | private GooListener gooListener; |
yoonseon | fe72197 | 2017-01-10 17:18:49 -0800 | [diff] [blame] | 56 | |
Yoonseon Han | b14461b | 2017-03-07 14:08:01 +0900 | [diff] [blame] | 57 | private BarManager barManager; |
| 58 | private BarListener barListener; |
yoonseon | fe72197 | 2017-01-10 17:18:49 -0800 | [diff] [blame] | 59 | |
| 60 | @Before |
| 61 | public void setUp() { |
Yoonseon Han | b14461b | 2017-03-07 14:08:01 +0900 | [diff] [blame] | 62 | VirtualNetworkService manager = new TestVirtualNetworkManager(); |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 63 | |
yoonseon | fe72197 | 2017-01-10 17:18:49 -0800 | [diff] [blame] | 64 | dispatcher.addSink(VirtualEvent.class, listenerRegistryManager); |
| 65 | |
| 66 | prickleListener = new PrickleListener(); |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 67 | prickleManager = new PrickleManager(manager, NetworkId.networkId(1)); |
yoonseon | fe72197 | 2017-01-10 17:18:49 -0800 | [diff] [blame] | 68 | prickleManager.addListener(prickleListener); |
| 69 | |
| 70 | gooListener = new GooListener(); |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 71 | gooManager = new GooManager(manager, NetworkId.networkId(1)); |
yoonseon | fe72197 | 2017-01-10 17:18:49 -0800 | [diff] [blame] | 72 | gooManager.addListener(gooListener); |
| 73 | |
| 74 | barListener = new BarListener(); |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 75 | barManager = new BarManager(manager, NetworkId.networkId(2)); |
yoonseon | fe72197 | 2017-01-10 17:18:49 -0800 | [diff] [blame] | 76 | 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 Moon | ffaeac3 | 2017-04-05 11:25:17 +0900 | [diff] [blame] | 146 | protected Thing(String subject) { |
yoonseon | fe72197 | 2017-01-10 17:18:49 -0800 | [diff] [blame] | 147 | super(Type.FOO, subject); |
| 148 | } |
| 149 | } |
| 150 | |
Hyunsun Moon | ffaeac3 | 2017-04-05 11:25:17 +0900 | [diff] [blame] | 151 | private static final class Prickle extends Thing { |
Yoonseon Han | b14461b | 2017-03-07 14:08:01 +0900 | [diff] [blame] | 152 | private Prickle(String subject) { |
yoonseon | fe72197 | 2017-01-10 17:18:49 -0800 | [diff] [blame] | 153 | super(subject); |
| 154 | } |
| 155 | } |
| 156 | |
Hyunsun Moon | ffaeac3 | 2017-04-05 11:25:17 +0900 | [diff] [blame] | 157 | private static final class Goo extends Thing { |
Yoonseon Han | b14461b | 2017-03-07 14:08:01 +0900 | [diff] [blame] | 158 | private Goo(String subject) { |
yoonseon | fe72197 | 2017-01-10 17:18:49 -0800 | [diff] [blame] | 159 | super(subject); |
| 160 | } |
| 161 | } |
| 162 | |
Hyunsun Moon | ffaeac3 | 2017-04-05 11:25:17 +0900 | [diff] [blame] | 163 | private static final class Bar extends Thing { |
Yoonseon Han | b14461b | 2017-03-07 14:08:01 +0900 | [diff] [blame] | 164 | private Bar(String subject) { |
yoonseon | fe72197 | 2017-01-10 17:18:49 -0800 | [diff] [blame] | 165 | 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) { |
yoonseon | fe72197 | 2017-01-10 17:18:49 -0800 | [diff] [blame] | 175 | 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> { |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 190 | public PrickleManager(VirtualNetworkService service, NetworkId networkId) { |
Yoonseon Han | b14461b | 2017-03-07 14:08:01 +0900 | [diff] [blame] | 191 | super(service, networkId, Prickle.class); |
yoonseon | fe72197 | 2017-01-10 17:18:49 -0800 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
| 195 | private class GooManager extends AbstractVirtualListenerManager<Goo, GooListener> { |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 196 | public GooManager(VirtualNetworkService service, NetworkId networkId) { |
Yoonseon Han | b14461b | 2017-03-07 14:08:01 +0900 | [diff] [blame] | 197 | super(service, networkId, Goo.class); |
yoonseon | fe72197 | 2017-01-10 17:18:49 -0800 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | |
| 201 | private class BarManager extends AbstractVirtualListenerManager<Bar, BarListener> { |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 202 | public BarManager(VirtualNetworkService service, NetworkId networkId) { |
Yoonseon Han | b14461b | 2017-03-07 14:08:01 +0900 | [diff] [blame] | 203 | super(service, networkId, Bar.class); |
yoonseon | fe72197 | 2017-01-10 17:18:49 -0800 | [diff] [blame] | 204 | } |
| 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 | } |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 248 | |
Yoonseon Han | ffcc32f | 2017-05-03 14:42:17 -0700 | [diff] [blame] | 249 | private class TestVirtualNetworkManager extends VirtualNetworkServiceAdapter { |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 250 | TestServiceDirectory serviceDirectory = new TestServiceDirectory(); |
| 251 | |
| 252 | public TestVirtualNetworkManager() { |
| 253 | serviceDirectory.add(EventDeliveryService.class, dispatcher); |
| 254 | } |
| 255 | |
| 256 | @Override |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 257 | 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 | } |
yoonseon | fe72197 | 2017-01-10 17:18:49 -0800 | [diff] [blame] | 284 | } |