Jonathan Hart | deda0ba | 2014-04-03 11:14:12 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.registry; |
Naoki Shiota | d00accf | 2013-06-25 14:40:37 -0700 | [diff] [blame] | 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.Collection; |
| 5 | import java.util.List; |
| 6 | import java.util.Map; |
| 7 | |
| 8 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 9 | import net.floodlightcontroller.test.FloodlightTestCase; |
Jonathan Hart | deda0ba | 2014-04-03 11:14:12 -0700 | [diff] [blame] | 10 | import net.onrc.onos.core.registry.StandaloneRegistryTest.LoggingCallback; |
| 11 | import net.onrc.onos.core.registry.ZookeeperRegistry.SwitchLeaderListener; |
Pavlin Radoslavov | 53b208a | 2014-07-28 13:16:11 -0700 | [diff] [blame] | 12 | import net.onrc.onos.core.util.OnosInstanceId; |
Naoki Shiota | d00accf | 2013-06-25 14:40:37 -0700 | [diff] [blame] | 13 | |
Jonathan Hart | 116b1fe | 2014-03-14 18:53:47 -0700 | [diff] [blame] | 14 | import org.apache.curator.RetryPolicy; |
| 15 | import org.apache.curator.framework.CuratorFramework; |
| 16 | import org.apache.curator.framework.CuratorFrameworkFactory; |
| 17 | import org.apache.curator.framework.listen.ListenerContainer; |
| 18 | import org.apache.curator.framework.recipes.atomic.AtomicValue; |
| 19 | import org.apache.curator.framework.recipes.atomic.DistributedAtomicLong; |
| 20 | import org.apache.curator.framework.recipes.cache.ChildData; |
| 21 | import org.apache.curator.framework.recipes.cache.PathChildrenCache; |
| 22 | import org.apache.curator.framework.recipes.cache.PathChildrenCache.StartMode; |
| 23 | import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent; |
| 24 | import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener; |
| 25 | import org.apache.curator.framework.recipes.leader.LeaderLatch; |
| 26 | import org.apache.curator.x.discovery.ServiceCache; |
| 27 | import org.apache.curator.x.discovery.ServiceCacheBuilder; |
| 28 | import org.apache.curator.x.discovery.ServiceDiscovery; |
| 29 | import org.apache.curator.x.discovery.ServiceDiscoveryBuilder; |
| 30 | import org.apache.curator.x.discovery.ServiceInstance; |
Naoki Shiota | d00accf | 2013-06-25 14:40:37 -0700 | [diff] [blame] | 31 | import org.easymock.EasyMock; |
| 32 | import org.easymock.IAnswer; |
| 33 | import org.junit.After; |
| 34 | import org.junit.Before; |
| 35 | import org.junit.Ignore; |
| 36 | import org.junit.Test; |
| 37 | import org.junit.runner.RunWith; |
| 38 | import org.openflow.util.HexString; |
| 39 | import org.powermock.api.easymock.PowerMock; |
| 40 | import org.powermock.core.classloader.annotations.PrepareForTest; |
| 41 | import org.powermock.modules.junit4.PowerMockRunner; |
| 42 | |
Naoki Shiota | d00accf | 2013-06-25 14:40:37 -0700 | [diff] [blame] | 43 | /** |
Naoki Shiota | a3b2dfa | 2013-06-27 13:52:24 -0700 | [diff] [blame] | 44 | * Unit test for {@link ZookeeperRegistry}. |
| 45 | * NOTE: {@link FloodlightTestCase} conflicts with PowerMock. If FloodLight-related methods need to be tested, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 46 | * implement another test class to test them. |
Naoki Shiota | d00accf | 2013-06-25 14:40:37 -0700 | [diff] [blame] | 47 | */ |
| 48 | @RunWith(PowerMockRunner.class) |
| 49 | @PrepareForTest({ZookeeperRegistry.class, CuratorFramework.class, CuratorFrameworkFactory.class, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 50 | ServiceDiscoveryBuilder.class, ServiceDiscovery.class, ServiceCache.class, PathChildrenCache.class, |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 51 | ZookeeperRegistry.SwitchPathCacheListener.class }) |
Naoki Shiota | d00accf | 2013-06-25 14:40:37 -0700 | [diff] [blame] | 52 | public class ZookeeperRegistryTest extends FloodlightTestCase { |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 53 | private static final Long ID_BLOCK_SIZE = 0x100000000L; |
Naoki Shiota | d00accf | 2013-06-25 14:40:37 -0700 | [diff] [blame] | 54 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 55 | protected ZookeeperRegistry registry; |
| 56 | protected CuratorFramework client; |
Naoki Shiota | d00accf | 2013-06-25 14:40:37 -0700 | [diff] [blame] | 57 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 58 | protected PathChildrenCacheListener pathChildrenCacheListener; |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 59 | protected static final String CONTROLLER_ID = "controller2013"; |
Naoki Shiota | d00accf | 2013-06-25 14:40:37 -0700 | [diff] [blame] | 60 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 61 | /** |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 62 | * Initialize {@link ZookeeperRegistry} Object and inject initial value with |
| 63 | * {@link ZookeeperRegistry#init(FloodlightModuleContext)} method. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 64 | * This setup code also tests {@link ZookeeperRegistry#init(FloodlightModuleContext)} method itself. |
| 65 | */ |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 66 | @Override |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 67 | @Before |
| 68 | public void setUp() throws Exception { |
| 69 | super.setUp(); |
Naoki Shiota | d00accf | 2013-06-25 14:40:37 -0700 | [diff] [blame] | 70 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 71 | pathChildrenCacheListener = null; |
Naoki Shiota | d00accf | 2013-06-25 14:40:37 -0700 | [diff] [blame] | 72 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 73 | // Mock of CuratorFramework |
| 74 | client = createCuratorFrameworkMock(); |
Naoki Shiota | d00accf | 2013-06-25 14:40:37 -0700 | [diff] [blame] | 75 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 76 | // Mock of CuratorFrameworkFactory |
| 77 | PowerMock.mockStatic(CuratorFrameworkFactory.class); |
| 78 | EasyMock.expect(CuratorFrameworkFactory.newClient((String) EasyMock.anyObject(), |
| 79 | EasyMock.anyInt(), EasyMock.anyInt(), (RetryPolicy) EasyMock.anyObject())).andReturn(client); |
| 80 | PowerMock.replay(CuratorFrameworkFactory.class); |
Naoki Shiota | d00accf | 2013-06-25 14:40:37 -0700 | [diff] [blame] | 81 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 82 | FloodlightModuleContext fmc = new FloodlightModuleContext(); |
| 83 | registry = new ZookeeperRegistry(); |
| 84 | fmc.addService(ZookeeperRegistry.class, registry); |
Naoki Shiota | d00accf | 2013-06-25 14:40:37 -0700 | [diff] [blame] | 85 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 86 | registry.init(fmc); |
Naoki Shiota | d00accf | 2013-06-25 14:40:37 -0700 | [diff] [blame] | 87 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 88 | PowerMock.verify(client, CuratorFrameworkFactory.class); |
| 89 | } |
Naoki Shiota | d00accf | 2013-06-25 14:40:37 -0700 | [diff] [blame] | 90 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 91 | /** |
| 92 | * Clean up member variables (empty for now). |
| 93 | */ |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 94 | @Override |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 95 | @After |
| 96 | public void tearDown() throws Exception { |
| 97 | super.tearDown(); |
| 98 | } |
Naoki Shiota | d00accf | 2013-06-25 14:40:37 -0700 | [diff] [blame] | 99 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 100 | /** |
| 101 | * Test if {@link ZookeeperRegistry#registerController(String)} method can go through without exception. |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 102 | * (Exceptions are usually out of test target, |
| 103 | * but {@link ZookeeperRegistry#registerController(String)} |
| 104 | * throws an exception in case of invalid registration.) |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 105 | */ |
| 106 | @Test |
| 107 | public void testRegisterController() { |
| 108 | String controllerIdToRegister = "controller2013"; |
Naoki Shiota | d00accf | 2013-06-25 14:40:37 -0700 | [diff] [blame] | 109 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 110 | try { |
| 111 | registry.registerController(controllerIdToRegister); |
| 112 | } catch (RegistryException e) { |
| 113 | e.printStackTrace(); |
| 114 | fail(e.getMessage()); |
| 115 | } |
| 116 | } |
Naoki Shiota | d00accf | 2013-06-25 14:40:37 -0700 | [diff] [blame] | 117 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 118 | /** |
Pavlin Radoslavov | 53b208a | 2014-07-28 13:16:11 -0700 | [diff] [blame] | 119 | * Test if {@link ZookeeperRegistry#getOnosInstanceId()} correctly returns |
| 120 | * registered ID. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 121 | * |
| 122 | * @throws Exception |
| 123 | */ |
| 124 | @Test |
Pavlin Radoslavov | 53b208a | 2014-07-28 13:16:11 -0700 | [diff] [blame] | 125 | public void testGetOnosInstanceId() throws Exception { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 126 | String controllerIdToRegister = "controller1"; |
Naoki Shiota | d00accf | 2013-06-25 14:40:37 -0700 | [diff] [blame] | 127 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 128 | // try before controller is registered |
Pavlin Radoslavov | 53b208a | 2014-07-28 13:16:11 -0700 | [diff] [blame] | 129 | OnosInstanceId onosInstanceId = registry.getOnosInstanceId(); |
| 130 | assertNull(onosInstanceId); |
Naoki Shiota | d00accf | 2013-06-25 14:40:37 -0700 | [diff] [blame] | 131 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 132 | // register |
| 133 | registry.registerController(controllerIdToRegister); |
Naoki Shiota | d00accf | 2013-06-25 14:40:37 -0700 | [diff] [blame] | 134 | |
Pavlin Radoslavov | 53b208a | 2014-07-28 13:16:11 -0700 | [diff] [blame] | 135 | // call getOnosInstanceId and verify |
| 136 | onosInstanceId = registry.getOnosInstanceId(); |
| 137 | assertNotNull(onosInstanceId); |
| 138 | assertEquals(controllerIdToRegister, onosInstanceId.toString()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 139 | } |
Naoki Shiota | d00accf | 2013-06-25 14:40:37 -0700 | [diff] [blame] | 140 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 141 | /** |
| 142 | * Test if {@link ZookeeperRegistry#getAllControllers()} returns all controllers. |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 143 | * Controllers to be returned are injected while setup. |
| 144 | * See {@link ZookeeperRegistryTest#createCuratorFrameworkMock()} |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 145 | * to what controllers are injected using mock {@link ServiceCache}. |
| 146 | * |
| 147 | * @throws Exception |
| 148 | */ |
| 149 | @Test |
| 150 | public void testGetAllControllers() throws Exception { |
| 151 | String controllerIdRegistered = "controller1"; |
| 152 | String controllerIdNotRegistered = "controller2013"; |
Naoki Shiota | d00accf | 2013-06-25 14:40:37 -0700 | [diff] [blame] | 153 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 154 | try { |
| 155 | Collection<String> ctrls = registry.getAllControllers(); |
| 156 | assertTrue(ctrls.contains(controllerIdRegistered)); |
| 157 | assertFalse(ctrls.contains(controllerIdNotRegistered)); |
| 158 | } catch (RegistryException e) { |
| 159 | e.printStackTrace(); |
| 160 | fail(e.getMessage()); |
| 161 | } |
| 162 | } |
Naoki Shiota | d00accf | 2013-06-25 14:40:37 -0700 | [diff] [blame] | 163 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 164 | /** |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 165 | * Test if {@link ZookeeperRegistry#requestControl(long, IControllerRegistryService.ControlChangeCallback)} |
| 166 | * correctly take control of specific switch. |
| 167 | * Because {@link ZookeeperRegistry#requestControl(long, IControllerRegistryService.ControlChangeCallback)} |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 168 | * doesn't return values, inject mock {@link LeaderLatch} object and verify latch is correctly set up. |
| 169 | * |
| 170 | * @throws Exception |
| 171 | */ |
| 172 | @Test |
| 173 | public void testRequestControl() throws Exception { |
| 174 | // Mock LeaderLatch |
| 175 | LeaderLatch latch = EasyMock.createMock(LeaderLatch.class); |
| 176 | latch.addListener(EasyMock.anyObject(SwitchLeaderListener.class)); |
| 177 | EasyMock.expectLastCall().once(); |
| 178 | latch.start(); |
| 179 | EasyMock.expectLastCall().once(); |
| 180 | EasyMock.replay(latch); |
Naoki Shiota | d00accf | 2013-06-25 14:40:37 -0700 | [diff] [blame] | 181 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 182 | PowerMock.expectNew(LeaderLatch.class, |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 183 | EasyMock.anyObject(CuratorFramework.class), |
| 184 | EasyMock.anyObject(String.class), |
| 185 | EasyMock.anyObject(String.class)) |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 186 | .andReturn(latch).once(); |
| 187 | PowerMock.replay(LeaderLatch.class); |
Naoki Shiota | d00accf | 2013-06-25 14:40:37 -0700 | [diff] [blame] | 188 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 189 | String controllerId = "controller2013"; |
| 190 | registry.registerController(controllerId); |
Naoki Shiota | d00accf | 2013-06-25 14:40:37 -0700 | [diff] [blame] | 191 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 192 | LoggingCallback callback = new LoggingCallback(1); |
| 193 | long dpidToRequest = 2000L; |
Naoki Shiota | d00accf | 2013-06-25 14:40:37 -0700 | [diff] [blame] | 194 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 195 | try { |
| 196 | registry.requestControl(dpidToRequest, callback); |
| 197 | } catch (RegistryException e) { |
| 198 | e.printStackTrace(); |
| 199 | fail(e.getMessage()); |
| 200 | } |
Naoki Shiota | d00accf | 2013-06-25 14:40:37 -0700 | [diff] [blame] | 201 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 202 | EasyMock.verify(latch); |
| 203 | } |
Naoki Shiota | d00accf | 2013-06-25 14:40:37 -0700 | [diff] [blame] | 204 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 205 | /** |
| 206 | * Test if {@link ZookeeperRegistry#releaseControl(long)} correctly release control of specific switch. |
| 207 | * Because {@link ZookeeperRegistry#releaseControl(long)} doesn't return values, inject mock |
| 208 | * {@link LeaderLatch} object and verify latch is correctly set up. |
| 209 | * |
| 210 | * @throws Exception |
| 211 | */ |
| 212 | @Test |
| 213 | public void testReleaseControl() throws Exception { |
| 214 | // Mock of LeaderLatch |
| 215 | LeaderLatch latch = EasyMock.createMock(LeaderLatch.class); |
| 216 | latch.addListener(EasyMock.anyObject(SwitchLeaderListener.class)); |
| 217 | EasyMock.expectLastCall().once(); |
| 218 | latch.start(); |
| 219 | EasyMock.expectLastCall().once(); |
| 220 | latch.removeListener(EasyMock.anyObject(SwitchLeaderListener.class)); |
| 221 | EasyMock.expectLastCall().once(); |
| 222 | latch.close(); |
| 223 | EasyMock.expectLastCall().once(); |
| 224 | EasyMock.replay(latch); |
Naoki Shiota | d00accf | 2013-06-25 14:40:37 -0700 | [diff] [blame] | 225 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 226 | PowerMock.expectNew(LeaderLatch.class, |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 227 | EasyMock.anyObject(CuratorFramework.class), |
| 228 | EasyMock.anyObject(String.class), |
| 229 | EasyMock.anyObject(String.class)) |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 230 | .andReturn(latch).once(); |
| 231 | PowerMock.replay(LeaderLatch.class); |
Naoki Shiota | d00accf | 2013-06-25 14:40:37 -0700 | [diff] [blame] | 232 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 233 | String controllerId = "controller2013"; |
| 234 | registry.registerController(controllerId); |
Naoki Shiota | d00accf | 2013-06-25 14:40:37 -0700 | [diff] [blame] | 235 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 236 | long dpidToRequest = 2000L; |
| 237 | LoggingCallback callback = new LoggingCallback(1); |
| 238 | |
| 239 | registry.requestControl(dpidToRequest, callback); |
| 240 | registry.releaseControl(dpidToRequest); |
| 241 | |
| 242 | EasyMock.verify(latch); |
| 243 | } |
| 244 | |
| 245 | /** |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 246 | * Test if {@link ZookeeperRegistry#hasControl(long)} returns |
| 247 | * correct status whether controller has control of specific switch. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 248 | * |
| 249 | * @throws Exception |
| 250 | */ |
| 251 | @Test |
| 252 | public void testHasControl() throws Exception { |
| 253 | // Mock of LeaderLatch |
| 254 | LeaderLatch latch = EasyMock.createMock(LeaderLatch.class); |
| 255 | latch.addListener(EasyMock.anyObject(SwitchLeaderListener.class)); |
| 256 | EasyMock.expectLastCall().once(); |
| 257 | latch.start(); |
| 258 | EasyMock.expectLastCall().once(); |
| 259 | EasyMock.expect(latch.hasLeadership()).andReturn(true).anyTimes(); |
| 260 | latch.removeListener(EasyMock.anyObject(SwitchLeaderListener.class)); |
| 261 | EasyMock.expectLastCall().once(); |
| 262 | latch.close(); |
| 263 | EasyMock.expectLastCall().once(); |
| 264 | EasyMock.replay(latch); |
| 265 | |
| 266 | PowerMock.expectNew(LeaderLatch.class, |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 267 | EasyMock.anyObject(CuratorFramework.class), |
| 268 | EasyMock.anyObject(String.class), |
| 269 | EasyMock.anyObject(String.class)) |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 270 | .andReturn(latch); |
| 271 | PowerMock.replay(LeaderLatch.class); |
| 272 | |
| 273 | String controllerId = "controller2013"; |
| 274 | registry.registerController(controllerId); |
| 275 | |
| 276 | long dpidToRequest = 2000L; |
| 277 | LoggingCallback callback = new LoggingCallback(2); |
| 278 | |
| 279 | // Test before request control |
| 280 | assertFalse(registry.hasControl(dpidToRequest)); |
| 281 | |
| 282 | registry.requestControl(dpidToRequest, callback); |
| 283 | |
| 284 | // Test after request control |
| 285 | assertTrue(registry.hasControl(dpidToRequest)); |
| 286 | |
| 287 | registry.releaseControl(dpidToRequest); |
| 288 | |
| 289 | // Test after release control |
| 290 | assertFalse(registry.hasControl(dpidToRequest)); |
| 291 | |
| 292 | EasyMock.verify(latch); |
| 293 | } |
| 294 | |
| 295 | /** |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 296 | * Test if {@link ZookeeperRegistry#getControllerForSwitch(long)} |
| 297 | * correctly returns controller ID of specific switch. |
| 298 | * Relation between controllers and switches are defined by |
| 299 | * {@link ZookeeperRegistryTest#setPathChildrenCache()} function. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 300 | * |
| 301 | * @throws Throwable |
| 302 | */ |
| 303 | @Test |
| 304 | public void testGetControllerForSwitch() throws Throwable { |
| 305 | long dpidRegistered = 1000L; |
| 306 | long dpidNotRegistered = 2000L; |
| 307 | |
| 308 | setPathChildrenCache(); |
| 309 | |
| 310 | String controllerForSw = registry.getControllerForSwitch(dpidRegistered); |
| 311 | assertEquals("controller1", controllerForSw); |
| 312 | |
| 313 | controllerForSw = registry.getControllerForSwitch(dpidNotRegistered); |
| 314 | assertEquals(null, controllerForSw); |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * Test if {@link ZookeeperRegistry#getSwitchesControlledByController(String)} returns correct list of |
| 319 | * switches controlled by a controller. |
| 320 | * |
| 321 | * @throws Exception |
| 322 | */ |
| 323 | // TODO: Test after getSwitchesControlledByController() is implemented. |
| 324 | @Ignore |
| 325 | @Test |
| 326 | public void testGetSwitchesControlledByController() throws Exception { |
| 327 | String controllerIdRegistered = "controller1"; |
| 328 | String dpidRegistered = HexString.toHexString(1000L); |
| 329 | String controllerIdNotRegistered = CONTROLLER_ID; |
| 330 | |
| 331 | Collection<Long> switches = registry.getSwitchesControlledByController(controllerIdRegistered); |
| 332 | assertNotNull(switches); |
| 333 | assertTrue(switches.contains(dpidRegistered)); |
| 334 | |
| 335 | switches = registry.getSwitchesControlledByController(controllerIdNotRegistered); |
| 336 | assertNotNull(switches); |
| 337 | assertEquals(0, switches.size()); |
| 338 | } |
| 339 | |
| 340 | /** |
| 341 | * Test if {@link ZookeeperRegistry#getAllSwitches()} returns correct list of all switches. |
| 342 | * Switches are injected in {@link ZookeeperRegistryTest#setPathChildrenCache()} function. |
| 343 | * |
| 344 | * @throws Exception |
| 345 | */ |
| 346 | @Test |
| 347 | public void testGetAllSwitches() throws Exception { |
| 348 | String[] dpids = { |
| 349 | HexString.toHexString(1000L), |
| 350 | HexString.toHexString(1001L), |
| 351 | HexString.toHexString(1002L), |
| 352 | }; |
| 353 | |
| 354 | setPathChildrenCache(); |
| 355 | |
| 356 | Map<String, List<ControllerRegistryEntry>> switches = registry.getAllSwitches(); |
| 357 | assertNotNull(switches); |
| 358 | assertEquals(dpids.length, switches.size()); |
| 359 | for (String dpid : dpids) { |
| 360 | assertTrue(switches.keySet().contains(dpid)); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | /** |
| 365 | * Test if {@link ZookeeperRegistry#allocateUniqueIdBlock()} can assign IdBlock without duplication. |
| 366 | */ |
| 367 | @Test |
| 368 | public void testAllocateUniqueIdBlock() { |
| 369 | // Number of blocks to be verified that any of them has unique block |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 370 | final int numBlocks = 100; |
| 371 | ArrayList<IdBlock> blocks = new ArrayList<IdBlock>(numBlocks); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 372 | |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 373 | for (int i = 0; i < numBlocks; ++i) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 374 | IdBlock block = registry.allocateUniqueIdBlock(); |
| 375 | assertNotNull(block); |
| 376 | blocks.add(block); |
| 377 | } |
| 378 | |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 379 | for (int i = 0; i < numBlocks; ++i) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 380 | IdBlock block1 = blocks.get(i); |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 381 | for (int j = i + 1; j < numBlocks; ++j) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 382 | IdBlock block2 = blocks.get(j); |
| 383 | IdBlock lower, higher; |
| 384 | |
| 385 | if (block1.getStart() < block2.getStart()) { |
| 386 | lower = block1; |
| 387 | higher = block2; |
| 388 | } else { |
| 389 | lower = block2; |
| 390 | higher = block1; |
| 391 | } |
| 392 | |
| 393 | assertTrue(lower.getSize() > 0L); |
| 394 | assertTrue(higher.getSize() > 0L); |
| 395 | assertTrue(lower.getEnd() <= higher.getStart()); |
| 396 | } |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | |
| 401 | //-------------------------- Creation of mock objects -------------------------- |
| 402 | |
| 403 | /** |
| 404 | * Create mock {@link CuratorFramework} object with initial value below.<br> |
| 405 | * [Ctrl ID] : [DPID]<br> |
| 406 | * controller1 : 1000<br> |
| 407 | * controller2 : 1001<br> |
| 408 | * controller2 : 1002<br> |
| 409 | * controller2013 : nothing |
| 410 | * |
| 411 | * @return Created mock object |
| 412 | * @throws Exception |
| 413 | */ |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 414 | @SuppressWarnings({"serial", "unchecked" }) |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 415 | private CuratorFramework createCuratorFrameworkMock() throws Exception { |
| 416 | // Mock of AtomicValue |
| 417 | AtomicValue<Long> atomicValue = EasyMock.createMock(AtomicValue.class); |
| 418 | EasyMock.expect(atomicValue.succeeded()).andReturn(true).anyTimes(); |
| 419 | EasyMock.expect(atomicValue.preValue()).andAnswer(new IAnswer<Long>() { |
| 420 | private long value = 0; |
| 421 | |
| 422 | @Override |
| 423 | public Long answer() throws Throwable { |
| 424 | value += ID_BLOCK_SIZE; |
| 425 | return value; |
| 426 | } |
| 427 | }).anyTimes(); |
| 428 | EasyMock.expect(atomicValue.postValue()).andAnswer(new IAnswer<Long>() { |
| 429 | private long value = ID_BLOCK_SIZE; |
| 430 | |
| 431 | @Override |
| 432 | public Long answer() throws Throwable { |
| 433 | value += ID_BLOCK_SIZE; |
| 434 | return value; |
| 435 | } |
| 436 | }).anyTimes(); |
| 437 | EasyMock.replay(atomicValue); |
| 438 | |
| 439 | // Mock DistributedAtomicLong |
| 440 | DistributedAtomicLong daLong = EasyMock.createMock(DistributedAtomicLong.class); |
| 441 | EasyMock.expect(daLong.add(EasyMock.anyLong())).andReturn(atomicValue).anyTimes(); |
| 442 | EasyMock.replay(daLong); |
| 443 | PowerMock.expectNew(DistributedAtomicLong.class, |
| 444 | new Class<?>[]{CuratorFramework.class, String.class, RetryPolicy.class}, |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 445 | EasyMock.anyObject(CuratorFramework.class), |
| 446 | EasyMock.anyObject(String.class), |
| 447 | EasyMock.anyObject(RetryPolicy.class)). |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 448 | andReturn(daLong).anyTimes(); |
| 449 | PowerMock.replay(DistributedAtomicLong.class); |
| 450 | |
| 451 | // Mock ListenerContainer |
| 452 | ListenerContainer<PathChildrenCacheListener> listenerContainer = EasyMock.createMock(ListenerContainer.class); |
| 453 | listenerContainer.addListener(EasyMock.anyObject(PathChildrenCacheListener.class)); |
| 454 | EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() { |
| 455 | @Override |
| 456 | public Object answer() throws Throwable { |
| 457 | pathChildrenCacheListener = (PathChildrenCacheListener) EasyMock.getCurrentArguments()[0]; |
| 458 | return null; |
| 459 | } |
| 460 | }).once(); |
| 461 | EasyMock.replay(listenerContainer); |
| 462 | |
| 463 | // Mock PathChildrenCache |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 464 | PathChildrenCache pathChildrenCacheMain = createPathChildrenCacheMock( |
| 465 | CONTROLLER_ID, new String[] {"/switches"}, listenerContainer); |
| 466 | PathChildrenCache pathChildrenCache1 = createPathChildrenCacheMock("controller1", |
| 467 | new String[] {HexString.toHexString(1000L)}, listenerContainer); |
| 468 | PathChildrenCache pathChildrenCache2 = createPathChildrenCacheMock("controller2", |
| 469 | new String[] { |
| 470 | HexString.toHexString(1001L), HexString.toHexString(1002L)}, |
| 471 | listenerContainer); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 472 | |
| 473 | // Mock PathChildrenCache constructor |
| 474 | PowerMock.expectNew(PathChildrenCache.class, |
| 475 | EasyMock.anyObject(CuratorFramework.class), EasyMock.anyObject(String.class), EasyMock.anyBoolean()). |
| 476 | andReturn(pathChildrenCacheMain).once(); |
| 477 | PowerMock.expectNew(PathChildrenCache.class, |
| 478 | EasyMock.anyObject(CuratorFramework.class), EasyMock.anyObject(String.class), EasyMock.anyBoolean()). |
| 479 | andReturn(pathChildrenCache1).once(); |
| 480 | PowerMock.expectNew(PathChildrenCache.class, |
| 481 | EasyMock.anyObject(CuratorFramework.class), EasyMock.anyObject(String.class), EasyMock.anyBoolean()). |
| 482 | andReturn(pathChildrenCache2).anyTimes(); |
| 483 | PowerMock.replay(PathChildrenCache.class); |
| 484 | |
| 485 | // Mock ServiceCache |
| 486 | ServiceCache<ControllerService> serviceCache = EasyMock.createMock(ServiceCache.class); |
| 487 | serviceCache.start(); |
| 488 | EasyMock.expectLastCall().once(); |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 489 | EasyMock.expect(serviceCache.getInstances()).andReturn(new ArrayList<ServiceInstance<ControllerService>>() { { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 490 | add(createServiceInstanceMock("controller1")); |
| 491 | add(createServiceInstanceMock("controller2")); |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 492 | } }).anyTimes(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 493 | EasyMock.replay(serviceCache); |
| 494 | |
| 495 | // Mock ServiceCacheBuilder |
| 496 | ServiceCacheBuilder<ControllerService> serviceCacheBuilder = EasyMock.createMock(ServiceCacheBuilder.class); |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 497 | EasyMock.expect(serviceCacheBuilder.name(EasyMock.anyObject(String.class))) |
| 498 | .andReturn(serviceCacheBuilder).once(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 499 | EasyMock.expect(serviceCacheBuilder.build()).andReturn(serviceCache).once(); |
| 500 | EasyMock.replay(serviceCacheBuilder); |
| 501 | |
| 502 | // Mock ServiceDiscovery |
| 503 | ServiceDiscovery<ControllerService> serviceDiscovery = EasyMock.createMock(ServiceDiscovery.class); |
| 504 | serviceDiscovery.start(); |
| 505 | EasyMock.expectLastCall().once(); |
| 506 | EasyMock.expect(serviceDiscovery.serviceCacheBuilder()).andReturn(serviceCacheBuilder).once(); |
| 507 | serviceDiscovery.registerService(EasyMock.anyObject(ServiceInstance.class)); |
| 508 | EasyMock.expectLastCall().once(); |
| 509 | EasyMock.replay(serviceDiscovery); |
| 510 | |
| 511 | // Mock CuratorFramework |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 512 | CuratorFramework mockClient = EasyMock.createMock(CuratorFramework.class); |
| 513 | mockClient.start(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 514 | EasyMock.expectLastCall().once(); |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 515 | EasyMock.expect(mockClient.usingNamespace(EasyMock.anyObject(String.class))).andReturn(mockClient); |
| 516 | EasyMock.replay(mockClient); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 517 | |
| 518 | // Mock ServiceDiscoveryBuilder |
| 519 | ServiceDiscoveryBuilder<ControllerService> builder = EasyMock.createMock(ServiceDiscoveryBuilder.class); |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 520 | EasyMock.expect(builder.client(mockClient)).andReturn(builder).once(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 521 | EasyMock.expect(builder.basePath(EasyMock.anyObject(String.class))).andReturn(builder); |
| 522 | EasyMock.expect(builder.build()).andReturn(serviceDiscovery); |
| 523 | EasyMock.replay(builder); |
| 524 | |
| 525 | PowerMock.mockStatic(ServiceDiscoveryBuilder.class); |
| 526 | EasyMock.expect(ServiceDiscoveryBuilder.builder(ControllerService.class)).andReturn(builder).once(); |
| 527 | PowerMock.replay(ServiceDiscoveryBuilder.class); |
| 528 | |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 529 | return mockClient; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | /** |
| 533 | * Create mock {@link ServiceInstance} object using given controller ID. |
| 534 | * |
| 535 | * @param controllerId Controller ID to represent instance's payload (ControllerService). |
| 536 | * @return Mock ServiceInstance object |
| 537 | */ |
| 538 | private ServiceInstance<ControllerService> createServiceInstanceMock(String controllerId) { |
| 539 | ControllerService controllerService = EasyMock.createMock(ControllerService.class); |
| 540 | EasyMock.expect(controllerService.getControllerId()).andReturn(controllerId).anyTimes(); |
| 541 | EasyMock.replay(controllerService); |
| 542 | |
| 543 | @SuppressWarnings("unchecked") |
| 544 | ServiceInstance<ControllerService> serviceInstance = EasyMock.createMock(ServiceInstance.class); |
| 545 | EasyMock.expect(serviceInstance.getPayload()).andReturn(controllerService).anyTimes(); |
| 546 | EasyMock.replay(serviceInstance); |
| 547 | |
| 548 | return serviceInstance; |
| 549 | } |
| 550 | |
| 551 | /** |
| 552 | * Create mock {@link PathChildrenCache} using given controller ID and DPIDs. |
| 553 | * |
| 554 | * @param controllerId Controller ID to represent current data. |
| 555 | * @param paths List of HexString indicating switch's DPID. |
| 556 | * @param listener Callback object to be set as Listenable. |
| 557 | * @return Mock PathChildrenCache object |
| 558 | * @throws Exception |
| 559 | */ |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 560 | private PathChildrenCache createPathChildrenCacheMock( |
| 561 | final String controllerId, |
| 562 | final String[] paths, |
| 563 | ListenerContainer<PathChildrenCacheListener> listener) |
| 564 | throws Exception { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 565 | PathChildrenCache pathChildrenCache = EasyMock.createMock(PathChildrenCache.class); |
| 566 | |
| 567 | EasyMock.expect(pathChildrenCache.getListenable()).andReturn(listener).anyTimes(); |
| 568 | |
| 569 | pathChildrenCache.start(EasyMock.anyObject(StartMode.class)); |
| 570 | EasyMock.expectLastCall().anyTimes(); |
| 571 | |
| 572 | List<ChildData> childs = new ArrayList<ChildData>(); |
| 573 | for (String path : paths) { |
| 574 | childs.add(createChildDataMockForCurrentData(controllerId, path)); |
| 575 | } |
| 576 | EasyMock.expect(pathChildrenCache.getCurrentData()).andReturn(childs).anyTimes(); |
| 577 | |
| 578 | pathChildrenCache.rebuild(); |
| 579 | EasyMock.expectLastCall().anyTimes(); |
| 580 | |
| 581 | EasyMock.replay(pathChildrenCache); |
| 582 | |
| 583 | return pathChildrenCache; |
| 584 | } |
| 585 | |
| 586 | /** |
| 587 | * Create mock {@link ChildData} for {@link PathChildrenCache#getCurrentData()} return value. |
| 588 | * This object need to include 'sequence number' in tail of path string. ("-0" means 0th sequence) |
| 589 | * |
| 590 | * @param controllerId Controller ID |
| 591 | * @param path HexString indicating switch's DPID |
| 592 | * @return Mock ChildData object |
| 593 | */ |
| 594 | private ChildData createChildDataMockForCurrentData(String controllerId, String path) { |
| 595 | ChildData data = EasyMock.createMock(ChildData.class); |
| 596 | EasyMock.expect(data.getPath()).andReturn(path + "-0").anyTimes(); |
| 597 | EasyMock.expect(data.getData()).andReturn(controllerId.getBytes()).anyTimes(); |
| 598 | EasyMock.replay(data); |
| 599 | |
| 600 | return data; |
| 601 | } |
| 602 | |
| 603 | /** |
| 604 | * Inject relations between controllers and switches using callback object. |
| 605 | * |
| 606 | * @throws Exception |
| 607 | */ |
| 608 | private void setPathChildrenCache() throws Exception { |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 609 | pathChildrenCacheListener.childEvent( |
| 610 | client, |
| 611 | createChildrenCacheEventMock("controller1", HexString.toHexString(1000L), |
| 612 | PathChildrenCacheEvent.Type.CHILD_ADDED)); |
| 613 | pathChildrenCacheListener.childEvent( |
| 614 | client, |
| 615 | createChildrenCacheEventMock("controller2", HexString.toHexString(1001L), |
| 616 | PathChildrenCacheEvent.Type.CHILD_ADDED)); |
| 617 | pathChildrenCacheListener.childEvent( |
| 618 | client, |
| 619 | createChildrenCacheEventMock("controller2", HexString.toHexString(1002L), |
| 620 | PathChildrenCacheEvent.Type.CHILD_ADDED)); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | /** |
| 624 | * Create mock {@link PathChildrenCacheEvent} object using given controller ID and DPID. |
| 625 | * |
| 626 | * @param controllerId Controller ID. |
| 627 | * @param path HexString of DPID. |
| 628 | * @param type Event type to be set to mock object. |
| 629 | * @return Mock PathChildrenCacheEvent object |
| 630 | */ |
| 631 | private PathChildrenCacheEvent createChildrenCacheEventMock(String controllerId, String path, |
| 632 | PathChildrenCacheEvent.Type type) { |
| 633 | PathChildrenCacheEvent event = EasyMock.createMock(PathChildrenCacheEvent.class); |
| 634 | ChildData data = EasyMock.createMock(ChildData.class); |
| 635 | |
| 636 | EasyMock.expect(data.getPath()).andReturn(path).anyTimes(); |
| 637 | EasyMock.expect(data.getData()).andReturn(controllerId.getBytes()).anyTimes(); |
| 638 | EasyMock.replay(data); |
| 639 | |
| 640 | EasyMock.expect(event.getType()).andReturn(type).anyTimes(); |
| 641 | EasyMock.expect(event.getData()).andReturn(data).anyTimes(); |
| 642 | EasyMock.replay(event); |
| 643 | |
| 644 | return event; |
| 645 | } |
Naoki Shiota | d00accf | 2013-06-25 14:40:37 -0700 | [diff] [blame] | 646 | } |