tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net.device.impl; |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 2 | |
| 3 | import org.junit.After; |
| 4 | import org.junit.Before; |
| 5 | import org.junit.Test; |
Ayaka Koshibe | a7f044e | 2014-09-23 16:56:20 -0700 | [diff] [blame] | 6 | import org.onlab.onos.cluster.MastershipListener; |
| 7 | import org.onlab.onos.cluster.MastershipService; |
| 8 | import org.onlab.onos.cluster.NodeId; |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 9 | import org.onlab.onos.event.Event; |
| 10 | import org.onlab.onos.net.Device; |
| 11 | import org.onlab.onos.net.DeviceId; |
| 12 | import org.onlab.onos.net.MastershipRole; |
tom | 24c55cd | 2014-09-06 10:47:25 -0700 | [diff] [blame] | 13 | import org.onlab.onos.net.Port; |
| 14 | import org.onlab.onos.net.PortNumber; |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 15 | import org.onlab.onos.net.device.DefaultDeviceDescription; |
tom | 24c55cd | 2014-09-06 10:47:25 -0700 | [diff] [blame] | 16 | import org.onlab.onos.net.device.DefaultPortDescription; |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 17 | import org.onlab.onos.net.device.DeviceAdminService; |
| 18 | import org.onlab.onos.net.device.DeviceDescription; |
| 19 | import org.onlab.onos.net.device.DeviceEvent; |
| 20 | import org.onlab.onos.net.device.DeviceListener; |
| 21 | import org.onlab.onos.net.device.DeviceProvider; |
| 22 | import org.onlab.onos.net.device.DeviceProviderRegistry; |
| 23 | import org.onlab.onos.net.device.DeviceProviderService; |
| 24 | import org.onlab.onos.net.device.DeviceService; |
tom | 24c55cd | 2014-09-06 10:47:25 -0700 | [diff] [blame] | 25 | import org.onlab.onos.net.device.PortDescription; |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 26 | import org.onlab.onos.net.provider.AbstractProvider; |
| 27 | import org.onlab.onos.net.provider.ProviderId; |
tom | 8bf2e6b | 2014-09-10 20:53:54 -0700 | [diff] [blame] | 28 | import org.onlab.onos.event.impl.TestEventDispatcher; |
tom | 202175a | 2014-09-19 19:00:11 -0700 | [diff] [blame] | 29 | import org.onlab.onos.net.trivial.impl.SimpleDeviceStore; |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 30 | |
Ayaka Koshibe | a7f044e | 2014-09-23 16:56:20 -0700 | [diff] [blame] | 31 | import com.google.common.collect.Sets; |
| 32 | |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 33 | import java.util.ArrayList; |
| 34 | import java.util.Iterator; |
| 35 | import java.util.List; |
Ayaka Koshibe | a7f044e | 2014-09-23 16:56:20 -0700 | [diff] [blame] | 36 | import java.util.Set; |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 37 | |
| 38 | import static org.junit.Assert.*; |
| 39 | import static org.onlab.onos.net.Device.Type.SWITCH; |
| 40 | import static org.onlab.onos.net.DeviceId.deviceId; |
| 41 | import static org.onlab.onos.net.device.DeviceEvent.Type.*; |
| 42 | |
| 43 | /** |
| 44 | * Test codifying the device service & device provider service contracts. |
| 45 | */ |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 46 | public class DeviceManagerTest { |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 47 | |
tom | 7e02cda | 2014-09-18 12:05:46 -0700 | [diff] [blame] | 48 | private static final ProviderId PID = new ProviderId("of", "foo"); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 49 | private static final DeviceId DID1 = deviceId("of:foo"); |
| 50 | private static final DeviceId DID2 = deviceId("of:bar"); |
| 51 | private static final String MFR = "whitebox"; |
| 52 | private static final String HW = "1.1.x"; |
| 53 | private static final String SW1 = "3.8.1"; |
| 54 | private static final String SW2 = "3.9.5"; |
| 55 | private static final String SN = "43311-12345"; |
| 56 | |
tom | 24c55cd | 2014-09-06 10:47:25 -0700 | [diff] [blame] | 57 | private static final PortNumber P1 = PortNumber.portNumber(1); |
| 58 | private static final PortNumber P2 = PortNumber.portNumber(2); |
| 59 | private static final PortNumber P3 = PortNumber.portNumber(3); |
| 60 | |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 61 | private DeviceManager mgr; |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 62 | |
| 63 | protected DeviceService service; |
| 64 | protected DeviceAdminService admin; |
| 65 | protected DeviceProviderRegistry registry; |
| 66 | protected DeviceProviderService providerService; |
| 67 | protected TestProvider provider; |
| 68 | protected TestListener listener = new TestListener(); |
| 69 | |
| 70 | @Before |
| 71 | public void setUp() { |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 72 | mgr = new DeviceManager(); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 73 | service = mgr; |
| 74 | admin = mgr; |
| 75 | registry = mgr; |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 76 | mgr.store = new SimpleDeviceStore(); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 77 | mgr.eventDispatcher = new TestEventDispatcher(); |
Ayaka Koshibe | a7f044e | 2014-09-23 16:56:20 -0700 | [diff] [blame] | 78 | mgr.mastershipService = new TestMastershipService(); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 79 | mgr.activate(); |
| 80 | |
| 81 | service.addListener(listener); |
| 82 | |
| 83 | provider = new TestProvider(); |
| 84 | providerService = registry.register(provider); |
| 85 | assertTrue("provider should be registered", |
| 86 | registry.getProviders().contains(provider.id())); |
| 87 | } |
| 88 | |
| 89 | @After |
| 90 | public void tearDown() { |
| 91 | registry.unregister(provider); |
| 92 | assertFalse("provider should not be registered", |
| 93 | registry.getProviders().contains(provider.id())); |
| 94 | service.removeListener(listener); |
| 95 | mgr.deactivate(); |
| 96 | } |
| 97 | |
| 98 | private void connectDevice(DeviceId deviceId, String swVersion) { |
| 99 | DeviceDescription description = |
| 100 | new DefaultDeviceDescription(deviceId.uri(), SWITCH, MFR, |
| 101 | HW, swVersion, SN); |
| 102 | providerService.deviceConnected(deviceId, description); |
| 103 | assertNotNull("device should be found", service.getDevice(DID1)); |
| 104 | } |
| 105 | |
| 106 | @Test |
| 107 | public void deviceConnected() { |
| 108 | assertNull("device should not be found", service.getDevice(DID1)); |
| 109 | connectDevice(DID1, SW1); |
| 110 | validateEvents(DEVICE_ADDED); |
| 111 | |
| 112 | Iterator<Device> it = service.getDevices().iterator(); |
| 113 | assertNotNull("one device expected", it.next()); |
| 114 | assertFalse("only one device expected", it.hasNext()); |
tom | ad2d209 | 2014-09-06 23:24:20 -0700 | [diff] [blame] | 115 | assertEquals("incorrect device count", 1, service.getDeviceCount()); |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 116 | assertTrue("device should be available", service.isAvailable(DID1)); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | @Test |
| 120 | public void deviceDisconnected() { |
| 121 | connectDevice(DID1, SW1); |
| 122 | connectDevice(DID2, SW1); |
| 123 | validateEvents(DEVICE_ADDED, DEVICE_ADDED); |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 124 | assertTrue("device should be available", service.isAvailable(DID1)); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 125 | |
| 126 | // Disconnect |
| 127 | providerService.deviceDisconnected(DID1); |
| 128 | assertNotNull("device should not be found", service.getDevice(DID1)); |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 129 | assertFalse("device should not be available", service.isAvailable(DID1)); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 130 | validateEvents(DEVICE_AVAILABILITY_CHANGED); |
| 131 | |
| 132 | // Reconnect |
| 133 | connectDevice(DID1, SW1); |
| 134 | validateEvents(DEVICE_AVAILABILITY_CHANGED); |
tom | ad2d209 | 2014-09-06 23:24:20 -0700 | [diff] [blame] | 135 | |
| 136 | assertEquals("incorrect device count", 2, service.getDeviceCount()); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | @Test |
| 140 | public void deviceUpdated() { |
| 141 | connectDevice(DID1, SW1); |
| 142 | validateEvents(DEVICE_ADDED); |
| 143 | |
| 144 | connectDevice(DID1, SW2); |
| 145 | validateEvents(DEVICE_UPDATED); |
| 146 | } |
| 147 | |
| 148 | @Test |
| 149 | public void getRole() { |
| 150 | connectDevice(DID1, SW1); |
tom | 80c0e5e | 2014-09-08 18:08:58 -0700 | [diff] [blame] | 151 | assertEquals("incorrect role", MastershipRole.MASTER, service.getRole(DID1)); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | @Test |
tom | 5f35f7c | 2014-09-08 18:38:19 -0700 | [diff] [blame] | 155 | public void setRole() throws InterruptedException { |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 156 | connectDevice(DID1, SW1); |
tom | 80c0e5e | 2014-09-08 18:08:58 -0700 | [diff] [blame] | 157 | admin.setRole(DID1, MastershipRole.STANDBY); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 158 | validateEvents(DEVICE_ADDED, DEVICE_MASTERSHIP_CHANGED); |
tom | 80c0e5e | 2014-09-08 18:08:58 -0700 | [diff] [blame] | 159 | assertEquals("incorrect role", MastershipRole.STANDBY, service.getRole(DID1)); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 160 | assertEquals("incorrect device", DID1, provider.deviceReceived.id()); |
tom | 80c0e5e | 2014-09-08 18:08:58 -0700 | [diff] [blame] | 161 | assertEquals("incorrect role", MastershipRole.STANDBY, provider.roleReceived); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 162 | } |
| 163 | |
tom | 24c55cd | 2014-09-06 10:47:25 -0700 | [diff] [blame] | 164 | @Test |
| 165 | public void updatePorts() { |
| 166 | connectDevice(DID1, SW1); |
| 167 | List<PortDescription> pds = new ArrayList<>(); |
| 168 | pds.add(new DefaultPortDescription(P1, true)); |
| 169 | pds.add(new DefaultPortDescription(P2, true)); |
| 170 | pds.add(new DefaultPortDescription(P3, true)); |
| 171 | providerService.updatePorts(DID1, pds); |
| 172 | validateEvents(DEVICE_ADDED, PORT_ADDED, PORT_ADDED, PORT_ADDED); |
| 173 | pds.clear(); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 174 | |
tom | 24c55cd | 2014-09-06 10:47:25 -0700 | [diff] [blame] | 175 | pds.add(new DefaultPortDescription(P1, false)); |
| 176 | pds.add(new DefaultPortDescription(P3, true)); |
| 177 | providerService.updatePorts(DID1, pds); |
| 178 | validateEvents(PORT_UPDATED, PORT_REMOVED); |
| 179 | } |
| 180 | |
| 181 | @Test |
| 182 | public void updatePortStatus() { |
| 183 | connectDevice(DID1, SW1); |
| 184 | List<PortDescription> pds = new ArrayList<>(); |
| 185 | pds.add(new DefaultPortDescription(P1, true)); |
| 186 | pds.add(new DefaultPortDescription(P2, true)); |
| 187 | providerService.updatePorts(DID1, pds); |
| 188 | validateEvents(DEVICE_ADDED, PORT_ADDED, PORT_ADDED); |
| 189 | |
| 190 | providerService.portStatusChanged(DID1, new DefaultPortDescription(P1, false)); |
| 191 | validateEvents(PORT_UPDATED); |
| 192 | providerService.portStatusChanged(DID1, new DefaultPortDescription(P1, false)); |
| 193 | assertTrue("no events expected", listener.events.isEmpty()); |
| 194 | } |
| 195 | |
| 196 | @Test |
| 197 | public void getPorts() { |
| 198 | connectDevice(DID1, SW1); |
| 199 | List<PortDescription> pds = new ArrayList<>(); |
| 200 | pds.add(new DefaultPortDescription(P1, true)); |
| 201 | pds.add(new DefaultPortDescription(P2, true)); |
| 202 | providerService.updatePorts(DID1, pds); |
| 203 | validateEvents(DEVICE_ADDED, PORT_ADDED, PORT_ADDED); |
| 204 | assertEquals("wrong port count", 2, service.getPorts(DID1).size()); |
| 205 | |
| 206 | Port port = service.getPort(DID1, P1); |
| 207 | assertEquals("incorrect port", P1, service.getPort(DID1, P1).number()); |
| 208 | assertEquals("incorrect state", true, service.getPort(DID1, P1).isEnabled()); |
| 209 | } |
| 210 | |
| 211 | @Test |
| 212 | public void removeDevice() { |
| 213 | connectDevice(DID1, SW1); |
| 214 | connectDevice(DID2, SW2); |
tom | ad2d209 | 2014-09-06 23:24:20 -0700 | [diff] [blame] | 215 | assertEquals("incorrect device count", 2, service.getDeviceCount()); |
tom | 24c55cd | 2014-09-06 10:47:25 -0700 | [diff] [blame] | 216 | admin.removeDevice(DID1); |
| 217 | assertNull("device should not be found", service.getDevice(DID1)); |
| 218 | assertNotNull("device should be found", service.getDevice(DID2)); |
tom | ad2d209 | 2014-09-06 23:24:20 -0700 | [diff] [blame] | 219 | assertEquals("incorrect device count", 1, service.getDeviceCount()); |
| 220 | |
tom | 24c55cd | 2014-09-06 10:47:25 -0700 | [diff] [blame] | 221 | } |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 222 | |
| 223 | protected void validateEvents(Enum... types) { |
| 224 | int i = 0; |
| 225 | assertEquals("wrong events received", types.length, listener.events.size()); |
| 226 | for (Event event : listener.events) { |
| 227 | assertEquals("incorrect event type", types[i], event.type()); |
| 228 | i++; |
| 229 | } |
| 230 | listener.events.clear(); |
| 231 | } |
| 232 | |
| 233 | |
| 234 | private class TestProvider extends AbstractProvider implements DeviceProvider { |
| 235 | private Device deviceReceived; |
| 236 | private MastershipRole roleReceived; |
| 237 | |
| 238 | public TestProvider() { |
| 239 | super(PID); |
| 240 | } |
| 241 | |
| 242 | @Override |
| 243 | public void triggerProbe(Device device) { |
| 244 | } |
| 245 | |
| 246 | @Override |
| 247 | public void roleChanged(Device device, MastershipRole newRole) { |
| 248 | deviceReceived = device; |
| 249 | roleReceived = newRole; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | private static class TestListener implements DeviceListener { |
| 254 | final List<DeviceEvent> events = new ArrayList<>(); |
| 255 | |
| 256 | @Override |
| 257 | public void event(DeviceEvent event) { |
| 258 | events.add(event); |
| 259 | } |
| 260 | } |
| 261 | |
Ayaka Koshibe | a7f044e | 2014-09-23 16:56:20 -0700 | [diff] [blame] | 262 | private static class TestMastershipService implements MastershipService { |
| 263 | |
| 264 | @Override |
| 265 | public NodeId getMasterFor(DeviceId deviceId) { |
| 266 | return null; |
| 267 | } |
| 268 | |
| 269 | @Override |
| 270 | public Set<DeviceId> getDevicesOf(NodeId nodeId) { |
| 271 | return Sets.newHashSet(DID1, DID2); |
| 272 | } |
| 273 | |
| 274 | @Override |
| 275 | public MastershipRole requestRoleFor(DeviceId deviceId) { |
| 276 | return MastershipRole.MASTER; |
| 277 | } |
| 278 | |
| 279 | @Override |
| 280 | public void addListener(MastershipListener listener) { |
| 281 | } |
| 282 | |
| 283 | @Override |
| 284 | public void removeListener(MastershipListener listener) { |
| 285 | } |
| 286 | |
| 287 | } |
| 288 | |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 289 | } |