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