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