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