YuanyouZhang | b5e0530 | 2015-07-21 20:14:35 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
YuanyouZhang | b5e0530 | 2015-07-21 20:14:35 +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 | package org.onosproject.ovsdb.providers.device; |
| 17 | |
Michal Mach | 958adf6 | 2017-06-05 16:29:13 +0200 | [diff] [blame^] | 18 | import com.google.common.collect.HashMultimap; |
| 19 | import com.google.common.collect.Multimap; |
YuanyouZhang | b5e0530 | 2015-07-21 20:14:35 +0800 | [diff] [blame] | 20 | import org.junit.After; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
| 23 | import org.onlab.packet.IpAddress; |
Hyunsun Moon | 5fb20a5 | 2015-09-25 17:02:33 -0700 | [diff] [blame] | 24 | import org.onlab.packet.TpPort; |
Michal Mach | 958adf6 | 2017-06-05 16:29:13 +0200 | [diff] [blame^] | 25 | import org.onosproject.net.Annotations; |
| 26 | import org.onosproject.net.DefaultAnnotations; |
| 27 | import org.onosproject.net.DefaultDevice; |
| 28 | import org.onosproject.net.Device; |
YuanyouZhang | b5e0530 | 2015-07-21 20:14:35 +0800 | [diff] [blame] | 29 | import org.onosproject.net.DeviceId; |
| 30 | import org.onosproject.net.MastershipRole; |
Michal Mach | 958adf6 | 2017-06-05 16:29:13 +0200 | [diff] [blame^] | 31 | import org.onosproject.net.PortNumber; |
| 32 | import org.onosproject.net.device.DefaultPortDescription; |
YuanyouZhang | b5e0530 | 2015-07-21 20:14:35 +0800 | [diff] [blame] | 33 | import org.onosproject.net.device.DeviceDescription; |
Michal Mach | 958adf6 | 2017-06-05 16:29:13 +0200 | [diff] [blame^] | 34 | import org.onosproject.net.device.DeviceDescriptionDiscovery; |
| 35 | import org.onosproject.net.device.DeviceEvent; |
| 36 | import org.onosproject.net.device.DeviceListener; |
YuanyouZhang | b5e0530 | 2015-07-21 20:14:35 +0800 | [diff] [blame] | 37 | import org.onosproject.net.device.DeviceProvider; |
| 38 | import org.onosproject.net.device.DeviceProviderRegistry; |
| 39 | import org.onosproject.net.device.DeviceProviderService; |
Michal Mach | 958adf6 | 2017-06-05 16:29:13 +0200 | [diff] [blame^] | 40 | import org.onosproject.net.device.DeviceServiceAdapter; |
YuanyouZhang | b5e0530 | 2015-07-21 20:14:35 +0800 | [diff] [blame] | 41 | import org.onosproject.net.device.PortDescription; |
| 42 | import org.onosproject.net.device.PortStatistics; |
Michal Mach | 958adf6 | 2017-06-05 16:29:13 +0200 | [diff] [blame^] | 43 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 44 | import org.onosproject.net.driver.Behaviour; |
YuanyouZhang | b5e0530 | 2015-07-21 20:14:35 +0800 | [diff] [blame] | 45 | import org.onosproject.net.provider.ProviderId; |
| 46 | import org.onosproject.ovsdb.controller.OvsdbClientService; |
| 47 | import org.onosproject.ovsdb.controller.OvsdbController; |
| 48 | import org.onosproject.ovsdb.controller.OvsdbEventListener; |
| 49 | import org.onosproject.ovsdb.controller.OvsdbNodeId; |
| 50 | import org.onosproject.ovsdb.controller.OvsdbNodeListener; |
| 51 | |
Michal Mach | 958adf6 | 2017-06-05 16:29:13 +0200 | [diff] [blame^] | 52 | import java.util.ArrayList; |
| 53 | import java.util.Collection; |
| 54 | import java.util.HashSet; |
| 55 | import java.util.List; |
| 56 | import java.util.Set; |
| 57 | import java.util.concurrent.Callable; |
| 58 | import java.util.concurrent.ExecutionException; |
| 59 | import java.util.concurrent.ExecutorService; |
| 60 | import java.util.concurrent.Future; |
| 61 | import java.util.concurrent.TimeUnit; |
| 62 | import java.util.concurrent.TimeoutException; |
| 63 | import java.util.function.Consumer; |
| 64 | |
| 65 | import static org.junit.Assert.assertEquals; |
| 66 | import static org.junit.Assert.assertNotNull; |
YuanyouZhang | b5e0530 | 2015-07-21 20:14:35 +0800 | [diff] [blame] | 67 | |
| 68 | /** |
| 69 | * Test for ovsdb device provider. |
| 70 | */ |
| 71 | public class OvsdbDeviceProviderTest { |
| 72 | private final OvsdbDeviceProvider provider = new OvsdbDeviceProvider(); |
| 73 | private final TestDeviceRegistry registry = new TestDeviceRegistry(); |
| 74 | private final TestController controller = new TestController(); |
Michal Mach | 958adf6 | 2017-06-05 16:29:13 +0200 | [diff] [blame^] | 75 | private final TestDeviceService deviceService = new TestDeviceService(); |
| 76 | |
| 77 | private final Device ovsdbDevice = new MockDevice( |
| 78 | DeviceId.deviceId("ovsdb:127.0.0.1"), |
| 79 | DefaultAnnotations.EMPTY); |
| 80 | |
| 81 | private final Device notOvsdbDevice = new MockDevice( |
| 82 | DeviceId.deviceId("other:127.0.0.1"), |
| 83 | DefaultAnnotations.EMPTY); |
| 84 | |
| 85 | private final TestDescription deviceDescription = new TestDescription(); |
| 86 | |
YuanyouZhang | b5e0530 | 2015-07-21 20:14:35 +0800 | [diff] [blame] | 87 | |
| 88 | @Before |
| 89 | public void startUp() { |
| 90 | provider.providerRegistry = registry; |
| 91 | provider.controller = controller; |
Michal Mach | 958adf6 | 2017-06-05 16:29:13 +0200 | [diff] [blame^] | 92 | provider.deviceService = deviceService; |
YuanyouZhang | b5e0530 | 2015-07-21 20:14:35 +0800 | [diff] [blame] | 93 | provider.activate(); |
| 94 | assertNotNull("provider should be registered", registry.provider); |
| 95 | } |
| 96 | |
| 97 | @After |
| 98 | public void tearDown() { |
| 99 | provider.deactivate(); |
| 100 | provider.controller = null; |
| 101 | provider.providerRegistry = null; |
Michal Mach | 958adf6 | 2017-06-05 16:29:13 +0200 | [diff] [blame^] | 102 | provider.deviceService = null; |
YuanyouZhang | b5e0530 | 2015-07-21 20:14:35 +0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | @Test |
| 106 | public void testNodeAdded() { |
| 107 | controller.listener.nodeAdded(new OvsdbNodeId(IpAddress |
| 108 | .valueOf("192.168.202.36"), 5000)); |
| 109 | assertEquals("ovsdb node added", 1, registry.connected.size()); |
| 110 | } |
| 111 | |
| 112 | @Test |
| 113 | public void testNodeRemoved() { |
| 114 | controller.listener.nodeAdded(new OvsdbNodeId(IpAddress |
| 115 | .valueOf("192.168.202.36"), 5000)); |
| 116 | controller.listener.nodeRemoved(new OvsdbNodeId(IpAddress |
| 117 | .valueOf("192.168.202.36"), 5000)); |
| 118 | assertEquals("ovsdb node removded", 0, registry.connected.size()); |
| 119 | } |
| 120 | |
Michal Mach | 958adf6 | 2017-06-05 16:29:13 +0200 | [diff] [blame^] | 121 | @Test |
| 122 | public void testDiscoverPortsAfterDeviceAdded() { |
| 123 | final int portCount = 5; |
| 124 | provider.executor = new SynchronousExecutor(); |
| 125 | prepareMocks(portCount); |
| 126 | |
| 127 | deviceService.listener.event(new DeviceEvent(DeviceEvent.Type.DEVICE_ADDED, ovsdbDevice)); |
| 128 | deviceService.listener.event(new DeviceEvent(DeviceEvent.Type.DEVICE_ADDED, notOvsdbDevice)); |
| 129 | |
| 130 | assertEquals(portCount, registry.ports.get(ovsdbDevice.id()).size()); |
| 131 | assertEquals(0, registry.ports.get(notOvsdbDevice.id()).size()); |
| 132 | } |
| 133 | |
| 134 | private void prepareMocks(int count) { |
| 135 | for(int i = 1; i <= count; i++) { |
| 136 | deviceDescription.portDescriptions.add(new DefaultPortDescription(PortNumber.portNumber(i), true)); |
| 137 | } |
| 138 | } |
| 139 | |
YuanyouZhang | b5e0530 | 2015-07-21 20:14:35 +0800 | [diff] [blame] | 140 | private class TestDeviceRegistry implements DeviceProviderRegistry { |
| 141 | DeviceProvider provider; |
| 142 | |
Michal Mach | 958adf6 | 2017-06-05 16:29:13 +0200 | [diff] [blame^] | 143 | final Set<DeviceId> connected = new HashSet<>(); |
| 144 | final Multimap<DeviceId, PortDescription> ports = HashMultimap.create(); |
YuanyouZhang | b5e0530 | 2015-07-21 20:14:35 +0800 | [diff] [blame] | 145 | PortDescription descr = null; |
| 146 | |
| 147 | @Override |
| 148 | public DeviceProviderService register(DeviceProvider provider) { |
| 149 | this.provider = provider; |
| 150 | return new TestProviderService(); |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public void unregister(DeviceProvider provider) { |
| 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public Set<ProviderId> getProviders() { |
| 159 | return null; |
| 160 | } |
| 161 | |
| 162 | private class TestProviderService implements DeviceProviderService { |
| 163 | |
| 164 | @Override |
| 165 | public DeviceProvider provider() { |
| 166 | return null; |
| 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public void deviceConnected(DeviceId deviceId, |
| 171 | DeviceDescription deviceDescription) { |
| 172 | connected.add(deviceId); |
| 173 | } |
| 174 | |
| 175 | @Override |
| 176 | public void deviceDisconnected(DeviceId deviceId) { |
| 177 | connected.remove(deviceId); |
| 178 | ports.removeAll(deviceId); |
| 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public void updatePorts(DeviceId deviceId, |
| 183 | List<PortDescription> portDescriptions) { |
| 184 | for (PortDescription p : portDescriptions) { |
| 185 | ports.put(deviceId, p); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | @Override |
| 190 | public void portStatusChanged(DeviceId deviceId, |
| 191 | PortDescription portDescription) { |
| 192 | ports.put(deviceId, portDescription); |
| 193 | descr = portDescription; |
| 194 | } |
| 195 | |
| 196 | @Override |
| 197 | public void receivedRoleReply(DeviceId deviceId, |
| 198 | MastershipRole requested, |
| 199 | MastershipRole response) { |
| 200 | } |
| 201 | |
| 202 | @Override |
| 203 | public void updatePortStatistics(DeviceId deviceId, |
| 204 | Collection<PortStatistics> portStatistics) { |
| 205 | |
| 206 | } |
| 207 | |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | private class TestController implements OvsdbController { |
| 212 | OvsdbNodeListener listener = null; |
| 213 | |
| 214 | @Override |
| 215 | public void addNodeListener(OvsdbNodeListener listener) { |
| 216 | this.listener = listener; |
| 217 | } |
| 218 | |
| 219 | @Override |
| 220 | public void removeNodeListener(OvsdbNodeListener listener) { |
| 221 | this.listener = null; |
| 222 | } |
| 223 | |
| 224 | @Override |
| 225 | public void addOvsdbEventListener(OvsdbEventListener listener) { |
| 226 | |
| 227 | } |
| 228 | |
| 229 | @Override |
| 230 | public void removeOvsdbEventListener(OvsdbEventListener listener) { |
| 231 | |
| 232 | } |
| 233 | |
| 234 | @Override |
| 235 | public List<OvsdbNodeId> getNodeIds() { |
| 236 | return null; |
| 237 | } |
| 238 | |
| 239 | @Override |
| 240 | public OvsdbClientService getOvsdbClient(OvsdbNodeId nodeId) { |
| 241 | return null; |
| 242 | } |
| 243 | |
Hyunsun Moon | 5fb20a5 | 2015-09-25 17:02:33 -0700 | [diff] [blame] | 244 | @Override |
| 245 | public void connect(IpAddress ip, TpPort port) { |
| 246 | |
| 247 | } |
jaegonkim | 1af0ae5 | 2017-01-01 10:46:55 +0900 | [diff] [blame] | 248 | |
| 249 | @Override |
| 250 | public void connect(IpAddress ip, TpPort port, Consumer<Exception> failhandler) { |
| 251 | |
| 252 | } |
| 253 | |
YuanyouZhang | b5e0530 | 2015-07-21 20:14:35 +0800 | [diff] [blame] | 254 | } |
| 255 | |
Michal Mach | 958adf6 | 2017-06-05 16:29:13 +0200 | [diff] [blame^] | 256 | private class TestDeviceService extends DeviceServiceAdapter { |
| 257 | DeviceListener listener = null; |
| 258 | |
| 259 | @Override |
| 260 | public Device getDevice(DeviceId deviceId) { |
| 261 | return ovsdbDevice; |
| 262 | } |
| 263 | |
| 264 | @Override |
| 265 | public void addListener(DeviceListener listener) { |
| 266 | this.listener = listener; |
| 267 | } |
| 268 | |
| 269 | @Override |
| 270 | public void removeListener(DeviceListener listener) { |
| 271 | this.listener = null; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | private class TestDescription extends AbstractHandlerBehaviour implements DeviceDescriptionDiscovery { |
| 276 | |
| 277 | final List<PortDescription> portDescriptions = new ArrayList<>(); |
| 278 | |
| 279 | @Override |
| 280 | public DeviceDescription discoverDeviceDetails() { |
| 281 | return null; |
| 282 | } |
| 283 | |
| 284 | @Override |
| 285 | public List<PortDescription> discoverPortDetails() { |
| 286 | return portDescriptions; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | private class MockDevice extends DefaultDevice { |
| 291 | |
| 292 | MockDevice(DeviceId id, |
| 293 | Annotations... annotations) { |
| 294 | super(null, id, Type.SWITCH, null, null, null, null, |
| 295 | null, annotations); |
| 296 | } |
| 297 | |
| 298 | @Override |
| 299 | public <B extends Behaviour> B as(Class<B> projectionClass) { |
| 300 | return (B) deviceDescription; |
| 301 | } |
| 302 | |
| 303 | @Override |
| 304 | public <B extends Behaviour> boolean is(Class<B> projectionClass) { |
| 305 | return true; |
| 306 | } |
| 307 | |
| 308 | } |
| 309 | |
| 310 | private class SynchronousExecutor implements ExecutorService { |
| 311 | @Override |
| 312 | public void execute(Runnable task) { |
| 313 | task.run(); |
| 314 | } |
| 315 | |
| 316 | @Override |
| 317 | public void shutdown() { |
| 318 | |
| 319 | } |
| 320 | |
| 321 | @Override |
| 322 | public List<Runnable> shutdownNow() { |
| 323 | return null; |
| 324 | } |
| 325 | |
| 326 | @Override |
| 327 | public boolean isShutdown() { |
| 328 | return true; |
| 329 | } |
| 330 | |
| 331 | @Override |
| 332 | public boolean isTerminated() { |
| 333 | return true; |
| 334 | } |
| 335 | |
| 336 | @Override |
| 337 | public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException { |
| 338 | return true; |
| 339 | } |
| 340 | |
| 341 | @Override |
| 342 | public <T> Future<T> submit(Callable<T> task) { |
| 343 | return null; |
| 344 | } |
| 345 | |
| 346 | @Override |
| 347 | public <T> Future<T> submit(Runnable task, T result) { |
| 348 | return null; |
| 349 | } |
| 350 | |
| 351 | @Override |
| 352 | public Future<?> submit(Runnable task) { |
| 353 | return null; |
| 354 | } |
| 355 | |
| 356 | @Override |
| 357 | public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException { |
| 358 | return null; |
| 359 | } |
| 360 | |
| 361 | @Override |
| 362 | public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException { |
| 363 | return null; |
| 364 | } |
| 365 | |
| 366 | @Override |
| 367 | public <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException { |
| 368 | return null; |
| 369 | } |
| 370 | |
| 371 | @Override |
| 372 | public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { |
| 373 | return null; |
| 374 | } |
| 375 | } |
| 376 | |
YuanyouZhang | b5e0530 | 2015-07-21 20:14:35 +0800 | [diff] [blame] | 377 | } |