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