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