Hyunsun Moon | 2c3f0ee | 2017-04-06 16:47:21 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.opencord.cordvtn.impl; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableSet; |
| 19 | import com.google.common.collect.Lists; |
| 20 | import com.google.common.collect.Maps; |
| 21 | import com.google.common.collect.Sets; |
Hyunsun Moon | 41eef92 | 2017-04-26 14:00:01 +0900 | [diff] [blame] | 22 | import com.google.common.util.concurrent.MoreExecutors; |
Hyunsun Moon | 2c3f0ee | 2017-04-06 16:47:21 +0900 | [diff] [blame] | 23 | import com.jcraft.jsch.Session; |
| 24 | import org.junit.After; |
| 25 | import org.junit.Before; |
| 26 | import org.junit.Test; |
| 27 | import org.junit.runner.RunWith; |
Hyunsun Moon | 41eef92 | 2017-04-26 14:00:01 +0900 | [diff] [blame] | 28 | import org.onlab.junit.TestUtils; |
Hyunsun Moon | 2c3f0ee | 2017-04-06 16:47:21 +0900 | [diff] [blame] | 29 | import org.onlab.packet.ChassisId; |
| 30 | import org.onlab.packet.IpAddress; |
| 31 | import org.onosproject.cluster.ClusterServiceAdapter; |
| 32 | import org.onosproject.cluster.ControllerNode; |
| 33 | import org.onosproject.cluster.DefaultControllerNode; |
| 34 | import org.onosproject.cluster.LeadershipServiceAdapter; |
| 35 | import org.onosproject.cluster.NodeId; |
| 36 | import org.onosproject.core.ApplicationId; |
| 37 | import org.onosproject.core.CoreServiceAdapter; |
| 38 | import org.onosproject.core.DefaultApplicationId; |
| 39 | import org.onosproject.event.DefaultEventSinkRegistry; |
| 40 | import org.onosproject.event.Event; |
| 41 | import org.onosproject.event.EventDeliveryService; |
| 42 | import org.onosproject.event.EventSink; |
| 43 | import org.onosproject.net.Annotations; |
| 44 | import org.onosproject.net.ConnectPoint; |
| 45 | import org.onosproject.net.DefaultDevice; |
| 46 | import org.onosproject.net.Device; |
| 47 | import org.onosproject.net.DeviceId; |
| 48 | import org.onosproject.net.Host; |
| 49 | import org.onosproject.net.HostId; |
| 50 | import org.onosproject.net.Port; |
| 51 | import org.onosproject.net.behaviour.BridgeConfig; |
| 52 | import org.onosproject.net.behaviour.BridgeDescription; |
| 53 | import org.onosproject.net.behaviour.InterfaceConfig; |
| 54 | import org.onosproject.net.config.NetworkConfigServiceAdapter; |
| 55 | import org.onosproject.net.device.DeviceAdminService; |
| 56 | import org.onosproject.net.device.DeviceEvent; |
| 57 | import org.onosproject.net.device.DeviceListener; |
| 58 | import org.onosproject.net.device.DeviceServiceAdapter; |
| 59 | import org.onosproject.net.driver.Behaviour; |
| 60 | import org.onosproject.net.flow.FlowRule; |
| 61 | import org.onosproject.net.host.HostDescription; |
| 62 | import org.onosproject.net.host.HostServiceAdapter; |
| 63 | import org.onosproject.net.provider.ProviderId; |
| 64 | import org.onosproject.ovsdb.controller.OvsdbClientService; |
| 65 | import org.onosproject.ovsdb.controller.OvsdbController; |
| 66 | import org.opencord.cordvtn.api.core.CordVtnPipeline; |
| 67 | import org.opencord.cordvtn.api.core.InstanceService; |
| 68 | import org.opencord.cordvtn.api.node.CordVtnNode; |
| 69 | import org.opencord.cordvtn.api.node.CordVtnNodeAdminService; |
| 70 | import org.opencord.cordvtn.api.node.CordVtnNodeEvent; |
| 71 | import org.opencord.cordvtn.api.node.CordVtnNodeListener; |
| 72 | import org.opencord.cordvtn.api.node.CordVtnNodeService; |
| 73 | import org.powermock.api.easymock.PowerMock; |
| 74 | import org.powermock.core.classloader.annotations.PrepareForTest; |
| 75 | import org.powermock.modules.junit4.PowerMockRunner; |
| 76 | |
| 77 | import java.util.List; |
| 78 | import java.util.Map; |
| 79 | import java.util.Set; |
| 80 | import java.util.stream.Collectors; |
| 81 | |
| 82 | import static com.google.common.base.Preconditions.checkState; |
| 83 | import static org.easymock.EasyMock.*; |
| 84 | import static org.junit.Assert.assertEquals; |
| 85 | import static org.onosproject.net.Device.Type.CONTROLLER; |
| 86 | import static org.onosproject.net.NetTestTools.injectEventDispatcher; |
| 87 | import static org.onosproject.net.device.DeviceEvent.Type.*; |
| 88 | import static org.opencord.cordvtn.api.Constants.INTEGRATION_BRIDGE; |
Hyunsun Moon | 2c3f0ee | 2017-04-06 16:47:21 +0900 | [diff] [blame] | 89 | import static org.opencord.cordvtn.api.node.CordVtnNodeEvent.Type.NODE_UPDATED; |
| 90 | import static org.opencord.cordvtn.api.node.CordVtnNodeState.*; |
| 91 | import static org.opencord.cordvtn.impl.RemoteIpCommandUtil.*; |
| 92 | import static org.powermock.api.easymock.PowerMock.mockStatic; |
| 93 | |
| 94 | /** |
| 95 | * Unit test for CordVtnNodeHandler which provides cordvtn node bootstrap state machine. |
| 96 | */ |
| 97 | @RunWith(PowerMockRunner.class) |
| 98 | public class DefaultCordVtnNodeHandlerTest extends CordVtnNodeTest { |
| 99 | |
| 100 | private static final String ERR_STATE = "Node state did not match"; |
| 101 | |
| 102 | private static final ApplicationId TEST_APP_ID = new DefaultApplicationId(1, "test"); |
| 103 | private static final NodeId LOCAL_NODE_ID = new NodeId("local"); |
| 104 | private static final ControllerNode LOCAL_CTRL = |
| 105 | new DefaultControllerNode(LOCAL_NODE_ID, IpAddress.valueOf("127.0.0.1")); |
| 106 | |
| 107 | private static final Device OVSDB_DEVICE = new TestDevice( |
| 108 | new ProviderId("of", "foo"), |
| 109 | DeviceId.deviceId("ovsdb:" + TEST_CIDR_ADDR.ip().toString()), |
| 110 | CONTROLLER, |
| 111 | "manufacturer", |
| 112 | "hwVersion", |
| 113 | "swVersion", |
| 114 | "serialNumber", |
| 115 | new ChassisId(1)); |
| 116 | |
| 117 | private static final Device OF_DEVICE_1 = createDevice(1); |
| 118 | |
| 119 | private static final Device OF_DEVICE_2 = createDevice(2); |
| 120 | private static final Port OF_DEVICE_2_PORT_1 = createPort(OF_DEVICE_2, 1, TEST_DATA_IFACE); |
| 121 | private static final Port OF_DEVICE_2_PORT_2 = createPort(OF_DEVICE_2, 2, TEST_VXLAN_IFACE); |
| 122 | |
| 123 | private static final Device OF_DEVICE_3 = createDevice(3); |
| 124 | private static final Port OF_DEVICE_3_PORT_1 = createPort(OF_DEVICE_3, 1, TEST_DATA_IFACE); |
| 125 | private static final Port OF_DEVICE_3_PORT_2 = createPort(OF_DEVICE_3, 2, TEST_VXLAN_IFACE); |
| 126 | |
| 127 | private static final Device OF_DEVICE_4 = createDevice(4); |
| 128 | private static final Port OF_DEVICE_4_PORT_1 = createPort(OF_DEVICE_4, 1, TEST_DATA_IFACE); |
| 129 | private static final Port OF_DEVICE_4_PORT_2 = createPort(OF_DEVICE_4, 2, TEST_VXLAN_IFACE); |
| 130 | |
| 131 | private static final CordVtnNode NODE_1 = createNode("node-01", OF_DEVICE_1, INIT); |
| 132 | private static final CordVtnNode NODE_2 = createNode("node-02", OF_DEVICE_2, DEVICE_CREATED); |
| 133 | private static final CordVtnNode NODE_3 = createNode("node-03", OF_DEVICE_3, PORT_CREATED); |
| 134 | private static final CordVtnNode NODE_4 = createNode("node-04", OF_DEVICE_4, COMPLETE); |
| 135 | |
| 136 | private TestDeviceService deviceService; |
| 137 | private TestNodeManager nodeManager; |
| 138 | private DefaultCordVtnNodeHandler target; |
| 139 | |
| 140 | @Before |
| 141 | public void setUp() throws Exception { |
| 142 | this.deviceService = new TestDeviceService(); |
| 143 | this.nodeManager = new TestNodeManager(); |
| 144 | |
| 145 | // add fake ovsdb device |
| 146 | this.deviceService.devMap.put(OVSDB_DEVICE.id(), OVSDB_DEVICE); |
| 147 | |
| 148 | // add fake OF devices |
| 149 | this.deviceService.devMap.put(OF_DEVICE_1.id(), OF_DEVICE_1); |
| 150 | this.deviceService.devMap.put(OF_DEVICE_2.id(), OF_DEVICE_2); |
| 151 | this.deviceService.devMap.put(OF_DEVICE_3.id(), OF_DEVICE_3); |
| 152 | this.deviceService.devMap.put(OF_DEVICE_4.id(), OF_DEVICE_4); |
| 153 | |
| 154 | // add fake OF ports |
Hyunsun Moon | 2c3f0ee | 2017-04-06 16:47:21 +0900 | [diff] [blame] | 155 | this.deviceService.portList.add(OF_DEVICE_3_PORT_1); |
| 156 | this.deviceService.portList.add(OF_DEVICE_3_PORT_2); |
| 157 | this.deviceService.portList.add(OF_DEVICE_4_PORT_1); |
| 158 | this.deviceService.portList.add(OF_DEVICE_4_PORT_2); |
| 159 | |
| 160 | // add fake nodes |
| 161 | this.nodeManager.nodeMap.put(OF_DEVICE_1.id(), NODE_1); |
| 162 | this.nodeManager.nodeMap.put(OF_DEVICE_2.id(), NODE_2); |
| 163 | this.nodeManager.nodeMap.put(OF_DEVICE_3.id(), NODE_3); |
| 164 | this.nodeManager.nodeMap.put(OF_DEVICE_4.id(), NODE_4); |
| 165 | |
| 166 | OvsdbClientService mockOvsdbClient = createMock(OvsdbClientService.class); |
| 167 | expect(mockOvsdbClient.isConnected()) |
| 168 | .andReturn(true) |
| 169 | .anyTimes(); |
| 170 | replay(mockOvsdbClient); |
| 171 | |
| 172 | OvsdbController mockOvsdbController = createMock(OvsdbController.class); |
| 173 | expect(mockOvsdbController.getOvsdbClient(anyObject())) |
| 174 | .andReturn(mockOvsdbClient) |
| 175 | .anyTimes(); |
| 176 | replay(mockOvsdbController); |
| 177 | |
| 178 | DeviceAdminService mockDeviceAdminService = createMock(DeviceAdminService.class); |
| 179 | mockDeviceAdminService.removeDevice(anyObject()); |
| 180 | replay(mockDeviceAdminService); |
| 181 | |
| 182 | target = new DefaultCordVtnNodeHandler(); |
| 183 | target.coreService = new TestCoreService(); |
| 184 | target.leadershipService = new TestLeadershipService(); |
| 185 | target.clusterService = new TestClusterService(); |
| 186 | target.configService = new TestConfigService(); |
| 187 | target.deviceService = this.deviceService; |
| 188 | target.deviceAdminService = mockDeviceAdminService; |
| 189 | target.hostService = new TestHostService(); |
| 190 | target.ovsdbController = mockOvsdbController; |
| 191 | target.nodeService = this.nodeManager; |
| 192 | target.nodeAdminService = this.nodeManager; |
| 193 | target.instanceService = new TestInstanceService(); |
| 194 | target.pipelineService = new TestCordVtnPipeline(); |
Hyunsun Moon | 41eef92 | 2017-04-26 14:00:01 +0900 | [diff] [blame] | 195 | TestUtils.setField(target, "eventExecutor", MoreExecutors.newDirectExecutorService()); |
Hyunsun Moon | 2c3f0ee | 2017-04-06 16:47:21 +0900 | [diff] [blame] | 196 | injectEventDispatcher(target, new TestEventDispatcher()); |
| 197 | target.activate(); |
| 198 | } |
| 199 | |
| 200 | @After |
| 201 | public void tearDown() { |
| 202 | target.deactivate(); |
| 203 | deviceService = null; |
| 204 | nodeManager = null; |
| 205 | target = null; |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Checks if the node state changes from INIT to DEVICE_CREATED when |
| 210 | * the integration bridge created. |
| 211 | */ |
| 212 | @Test |
| 213 | public void testProcessInitState() { |
| 214 | deviceService.addDevice(OF_DEVICE_1); |
Hyunsun Moon | 41eef92 | 2017-04-26 14:00:01 +0900 | [diff] [blame] | 215 | CordVtnNode current = nodeManager.node(NODE_1.integrationBridgeId()); |
| 216 | assertEquals(ERR_STATE, DEVICE_CREATED, current.state()); |
Hyunsun Moon | 2c3f0ee | 2017-04-06 16:47:21 +0900 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Checks if the node state changes from DEVICE_CREATED to PORT_CREATED |
| 221 | * when the data interface and vxlan interface are added. |
| 222 | */ |
| 223 | @Test |
| 224 | public void testProcessDeviceCreatedState() { |
Hyunsun Moon | 41eef92 | 2017-04-26 14:00:01 +0900 | [diff] [blame] | 225 | CordVtnNode current = nodeManager.node(NODE_2.integrationBridgeId()); |
| 226 | assertEquals(ERR_STATE, DEVICE_CREATED, current.state()); |
| 227 | |
| 228 | // Add the data port and check if the state is still in DEVICE_CREATED |
Hyunsun Moon | 2c3f0ee | 2017-04-06 16:47:21 +0900 | [diff] [blame] | 229 | deviceService.addPort(OF_DEVICE_2, OF_DEVICE_2_PORT_1); |
Hyunsun Moon | 41eef92 | 2017-04-26 14:00:01 +0900 | [diff] [blame] | 230 | current = nodeManager.node(NODE_2.integrationBridgeId()); |
| 231 | assertEquals(ERR_STATE, DEVICE_CREATED, current.state()); |
Hyunsun Moon | 2c3f0ee | 2017-04-06 16:47:21 +0900 | [diff] [blame] | 232 | |
| 233 | // Add the vxlan port and check if the state changes to PORT_CREATED |
| 234 | deviceService.addPort(OF_DEVICE_2, OF_DEVICE_2_PORT_2); |
Hyunsun Moon | 41eef92 | 2017-04-26 14:00:01 +0900 | [diff] [blame] | 235 | current = nodeManager.node(NODE_2.integrationBridgeId()); |
| 236 | assertEquals(ERR_STATE, PORT_CREATED, current.state()); |
Hyunsun Moon | 2c3f0ee | 2017-04-06 16:47:21 +0900 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Checks if the node state changes to COMPLETE when the network interface |
| 241 | * configuration is done. |
| 242 | */ |
| 243 | @PrepareForTest(RemoteIpCommandUtil.class) |
| 244 | @Test |
| 245 | public void testProcessPortCreatedState() { |
| 246 | Session mockSession = createMock(Session.class); |
| 247 | mockStatic(RemoteIpCommandUtil.class); |
| 248 | expect(connect(anyObject())).andReturn(mockSession); |
| 249 | expect(getCurrentIps(mockSession, INTEGRATION_BRIDGE)).andReturn(Sets.newHashSet(TEST_CIDR_ADDR.ip())); |
| 250 | expect(getCurrentIps(mockSession, TEST_DATA_IFACE)).andReturn(Sets.newHashSet()); |
| 251 | expect(isInterfaceUp(anyObject(), anyObject())).andReturn(true).anyTimes(); |
| 252 | RemoteIpCommandUtil.disconnect(anyObject()); |
| 253 | PowerMock.replay(RemoteIpCommandUtil.class); |
| 254 | |
| 255 | // There's no events for IP address changes on the interfaces, so just |
| 256 | // Set node state updated to trigger node bootstrap |
Hyunsun Moon | 41eef92 | 2017-04-26 14:00:01 +0900 | [diff] [blame] | 257 | nodeManager.updateNodeAndMakeEvent(NODE_3); |
| 258 | CordVtnNode current = nodeManager.node(NODE_3.integrationBridgeId()); |
| 259 | assertEquals(ERR_STATE, COMPLETE, current.state()); |
Hyunsun Moon | 2c3f0ee | 2017-04-06 16:47:21 +0900 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Checks if the node state falls back to INIT when the integration bridge |
| 264 | * is removed. |
| 265 | */ |
| 266 | @Test |
| 267 | public void testBackToInitStateWhenDeviceRemoved() { |
| 268 | // Remove the device from DEVICE_CREATED state node |
| 269 | deviceService.removeDevice(OF_DEVICE_2); |
Hyunsun Moon | 41eef92 | 2017-04-26 14:00:01 +0900 | [diff] [blame] | 270 | CordVtnNode current = nodeManager.node(NODE_2.integrationBridgeId()); |
| 271 | assertEquals(ERR_STATE, INIT, current.state()); |
Hyunsun Moon | 2c3f0ee | 2017-04-06 16:47:21 +0900 | [diff] [blame] | 272 | |
| 273 | // Remove the device from PORT_CREATED state node |
| 274 | deviceService.removeDevice(OF_DEVICE_3); |
Hyunsun Moon | 41eef92 | 2017-04-26 14:00:01 +0900 | [diff] [blame] | 275 | current = nodeManager.node(NODE_3.integrationBridgeId()); |
| 276 | assertEquals(ERR_STATE, INIT, current.state()); |
Hyunsun Moon | 2c3f0ee | 2017-04-06 16:47:21 +0900 | [diff] [blame] | 277 | |
| 278 | // Remove the device from COMPLETE state node |
| 279 | deviceService.removeDevice(OF_DEVICE_4); |
Hyunsun Moon | 41eef92 | 2017-04-26 14:00:01 +0900 | [diff] [blame] | 280 | current = nodeManager.node(NODE_4.integrationBridgeId()); |
| 281 | assertEquals(ERR_STATE, INIT, current.state()); |
Hyunsun Moon | 2c3f0ee | 2017-04-06 16:47:21 +0900 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | /** |
Hyunsun Moon | 41eef92 | 2017-04-26 14:00:01 +0900 | [diff] [blame] | 285 | * Checks if the node state falls back to INIT state when the ports |
Hyunsun Moon | 2c3f0ee | 2017-04-06 16:47:21 +0900 | [diff] [blame] | 286 | * are removed. |
| 287 | */ |
| 288 | @Test |
| 289 | public void testBackToDeviceCreatedStateWhenPortRemoved() { |
| 290 | // Remove the device from PORT_CREATED state node |
| 291 | deviceService.removePort(OF_DEVICE_3, OF_DEVICE_3_PORT_1); |
Hyunsun Moon | 41eef92 | 2017-04-26 14:00:01 +0900 | [diff] [blame] | 292 | CordVtnNode current = nodeManager.node(NODE_3.integrationBridgeId()); |
| 293 | assertEquals(ERR_STATE, INIT, current.state()); |
Hyunsun Moon | 2c3f0ee | 2017-04-06 16:47:21 +0900 | [diff] [blame] | 294 | |
| 295 | // Remove the device from COMPLETE state node |
| 296 | deviceService.removePort(OF_DEVICE_4, OF_DEVICE_4_PORT_1); |
Hyunsun Moon | 41eef92 | 2017-04-26 14:00:01 +0900 | [diff] [blame] | 297 | current = nodeManager.node(NODE_4.integrationBridgeId()); |
| 298 | assertEquals(ERR_STATE, INIT, current.state()); |
Hyunsun Moon | 2c3f0ee | 2017-04-06 16:47:21 +0900 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Checks if the node state falls back to PORT_CREATED when the interface |
| 303 | * configurations are incomplete. |
| 304 | */ |
| 305 | @PrepareForTest(RemoteIpCommandUtil.class) |
| 306 | @Test |
| 307 | public void testBackToPortCreatedStateWhenIpRemoved() { |
| 308 | // Mocks SSH connection failure |
| 309 | mockStatic(RemoteIpCommandUtil.class); |
| 310 | expect(connect(anyObject())).andReturn(null); |
| 311 | PowerMock.replay(RemoteIpCommandUtil.class); |
| 312 | |
| 313 | // ONOS is not able to detect IP is removed from the interface |
| 314 | // Just triggers node update event for the PORT_CREATED node and |
| 315 | // check if it stays in PORT_CREATED state |
| 316 | nodeManager.updateNode(NODE_3); |
Hyunsun Moon | 41eef92 | 2017-04-26 14:00:01 +0900 | [diff] [blame] | 317 | CordVtnNode current = nodeManager.node(NODE_3.integrationBridgeId()); |
| 318 | assertEquals(ERR_STATE, PORT_CREATED, current.state()); |
Hyunsun Moon | 2c3f0ee | 2017-04-06 16:47:21 +0900 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | private static final class TestDevice extends DefaultDevice { |
| 322 | InterfaceConfig mockInterfaceConfig = createMock(InterfaceConfig.class); |
| 323 | BridgeConfig mockBridgeConfig = createMock(BridgeConfig.class); |
| 324 | |
| 325 | private TestDevice(ProviderId providerId, |
| 326 | DeviceId id, |
| 327 | Type type, |
| 328 | String manufacturer, |
| 329 | String hwVersion, |
| 330 | String swVersion, |
| 331 | String serialNumber, |
| 332 | ChassisId chassisId, |
| 333 | Annotations... annotations) { |
| 334 | super(providerId, |
| 335 | id, |
| 336 | type, |
| 337 | manufacturer, |
| 338 | hwVersion, |
| 339 | swVersion, |
| 340 | serialNumber, |
| 341 | chassisId, |
| 342 | annotations); |
| 343 | } |
| 344 | |
| 345 | @Override |
| 346 | @SuppressWarnings("unchecked") |
| 347 | public <B extends Behaviour> B as(Class<B> projectionClass) { |
| 348 | if (projectionClass.equals(BridgeConfig.class)) { |
| 349 | expect(this.mockBridgeConfig.addBridge((BridgeDescription) anyObject())) |
| 350 | .andReturn(true) |
| 351 | .anyTimes(); |
| 352 | this.mockBridgeConfig.addPort(anyObject(), anyString()); |
| 353 | replay(mockBridgeConfig); |
| 354 | return (B) mockBridgeConfig; |
| 355 | } else if (projectionClass.equals(InterfaceConfig.class)) { |
| 356 | expect(this.mockInterfaceConfig.addTunnelMode(anyString(), anyObject())) |
| 357 | .andReturn(true) |
| 358 | .anyTimes(); |
| 359 | replay(mockInterfaceConfig); |
| 360 | return (B) mockInterfaceConfig; |
| 361 | } else { |
| 362 | return null; |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | @Override |
| 367 | public <B extends Behaviour> boolean is(Class<B> projectionClass) { |
| 368 | return true; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | |
| 373 | private static class TestCoreService extends CoreServiceAdapter { |
| 374 | |
| 375 | @Override |
| 376 | public ApplicationId registerApplication(String name) { |
| 377 | return TEST_APP_ID; |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | private static class TestLeadershipService extends LeadershipServiceAdapter { |
| 382 | |
| 383 | @Override |
| 384 | public NodeId getLeader(String path) { |
| 385 | return LOCAL_NODE_ID; |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | private static class TestClusterService extends ClusterServiceAdapter { |
| 390 | |
| 391 | @Override |
| 392 | public ControllerNode getLocalNode() { |
| 393 | return LOCAL_CTRL; |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | private static class TestConfigService extends NetworkConfigServiceAdapter { |
| 398 | |
| 399 | } |
| 400 | |
| 401 | private static class TestNodeManager implements CordVtnNodeService, CordVtnNodeAdminService { |
| 402 | Map<DeviceId, CordVtnNode> nodeMap = Maps.newHashMap(); |
| 403 | List<CordVtnNodeListener> listeners = Lists.newArrayList(); |
| 404 | |
| 405 | @Override |
| 406 | public Set<CordVtnNode> nodes() { |
| 407 | return ImmutableSet.copyOf(nodeMap.values()); |
| 408 | } |
| 409 | |
| 410 | @Override |
| 411 | public Set<CordVtnNode> completeNodes() { |
| 412 | return null; |
| 413 | } |
| 414 | |
| 415 | @Override |
| 416 | public CordVtnNode node(String hostname) { |
| 417 | return null; |
| 418 | } |
| 419 | |
| 420 | @Override |
| 421 | public CordVtnNode node(DeviceId deviceId) { |
| 422 | return nodeMap.get(deviceId); |
| 423 | } |
| 424 | |
| 425 | @Override |
| 426 | public void addListener(CordVtnNodeListener listener) { |
| 427 | listeners.add(listener); |
| 428 | } |
| 429 | |
| 430 | @Override |
| 431 | public void removeListener(CordVtnNodeListener listener) { |
| 432 | listeners.remove(listener); |
| 433 | } |
| 434 | |
| 435 | @Override |
| 436 | public void createNode(CordVtnNode node) { |
| 437 | nodeMap.put(node.integrationBridgeId(), node); |
Hyunsun Moon | 2c3f0ee | 2017-04-06 16:47:21 +0900 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | @Override |
| 441 | public void updateNode(CordVtnNode node) { |
| 442 | nodeMap.put(node.integrationBridgeId(), node); |
Hyunsun Moon | 41eef92 | 2017-04-26 14:00:01 +0900 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | public void updateNodeAndMakeEvent(CordVtnNode node) { |
| 446 | nodeMap.put(node.integrationBridgeId(), node); |
Hyunsun Moon | 2c3f0ee | 2017-04-06 16:47:21 +0900 | [diff] [blame] | 447 | CordVtnNodeEvent event = new CordVtnNodeEvent(NODE_UPDATED, node); |
| 448 | listeners.forEach(l -> l.event(event)); |
| 449 | } |
| 450 | |
| 451 | @Override |
| 452 | public CordVtnNode removeNode(String hostname) { |
| 453 | return null; |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | private static class TestDeviceService extends DeviceServiceAdapter { |
| 458 | Map<DeviceId, Device> devMap = Maps.newHashMap(); |
| 459 | List<Port> portList = Lists.newArrayList(); |
| 460 | List<DeviceListener> listeners = Lists.newArrayList(); |
| 461 | |
| 462 | @Override |
| 463 | public void addListener(DeviceListener listener) { |
| 464 | listeners.add(listener); |
| 465 | } |
| 466 | |
| 467 | @Override |
| 468 | public void removeListener(DeviceListener listener) { |
| 469 | listeners.remove(listener); |
| 470 | } |
| 471 | |
| 472 | @Override |
| 473 | public Device getDevice(DeviceId deviceId) { |
| 474 | return devMap.get(deviceId); |
| 475 | } |
| 476 | |
| 477 | @Override |
| 478 | public List<Port> getPorts(DeviceId deviceId) { |
| 479 | return this.portList.stream() |
| 480 | .filter(p -> p.element().id().equals(deviceId)) |
| 481 | .collect(Collectors.toList()); |
| 482 | } |
| 483 | |
| 484 | @Override |
| 485 | public boolean isAvailable(DeviceId deviceId) { |
| 486 | return devMap.containsKey(deviceId); |
| 487 | } |
| 488 | |
| 489 | void addDevice(Device device) { |
| 490 | devMap.put(device.id(), device); |
| 491 | DeviceEvent event = new DeviceEvent(DEVICE_ADDED, device); |
| 492 | listeners.forEach(l -> l.event(event)); |
| 493 | } |
| 494 | |
| 495 | void removeDevice(Device device) { |
| 496 | devMap.remove(device.id()); |
| 497 | DeviceEvent event = new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, device); |
| 498 | listeners.forEach(l -> l.event(event)); |
| 499 | } |
| 500 | |
| 501 | void addPort(Device device, Port port) { |
| 502 | portList.add(port); |
| 503 | DeviceEvent event = new DeviceEvent(PORT_ADDED, device, port); |
| 504 | listeners.forEach(l -> l.event(event)); |
| 505 | } |
| 506 | |
| 507 | void removePort(Device device, Port port) { |
| 508 | portList.remove(port); |
| 509 | DeviceEvent event = new DeviceEvent(PORT_REMOVED, device, port); |
| 510 | listeners.forEach(l -> l.event(event)); |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | private static class TestHostService extends HostServiceAdapter { |
| 515 | |
| 516 | @Override |
| 517 | public Iterable<Host> getHosts() { |
| 518 | return Lists.newArrayList(); |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | private static class TestInstanceService implements InstanceService { |
| 523 | |
| 524 | @Override |
| 525 | public void addInstance(ConnectPoint connectPoint) { |
| 526 | |
| 527 | } |
| 528 | |
| 529 | @Override |
| 530 | public void addInstance(HostId hostId, HostDescription description) { |
| 531 | |
| 532 | } |
| 533 | |
| 534 | @Override |
| 535 | public void removeInstance(ConnectPoint connectPoint) { |
| 536 | |
| 537 | } |
| 538 | |
| 539 | @Override |
| 540 | public void removeInstance(HostId hostId) { |
| 541 | |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | private static class TestCordVtnPipeline implements CordVtnPipeline { |
| 546 | |
| 547 | @Override |
| 548 | public void initPipeline(CordVtnNode node) { |
| 549 | |
| 550 | } |
| 551 | |
| 552 | @Override |
| 553 | public void cleanupPipeline() { |
| 554 | |
| 555 | } |
| 556 | |
| 557 | @Override |
| 558 | public void processFlowRule(boolean install, FlowRule rule) { |
| 559 | |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | public class TestEventDispatcher extends DefaultEventSinkRegistry |
| 564 | implements EventDeliveryService { |
| 565 | |
| 566 | @Override |
| 567 | @SuppressWarnings("unchecked") |
| 568 | public synchronized void post(Event event) { |
| 569 | EventSink sink = getSink(event.getClass()); |
| 570 | checkState(sink != null, "No sink for event %s", event); |
| 571 | sink.process(event); |
| 572 | } |
| 573 | |
| 574 | @Override |
| 575 | public void setDispatchTimeLimit(long millis) { |
| 576 | } |
| 577 | |
| 578 | @Override |
| 579 | public long getDispatchTimeLimit() { |
| 580 | return 0; |
| 581 | } |
| 582 | } |
| 583 | } |