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