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