Charles Chan | 114aec7 | 2017-06-19 14:00:53 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0947d7e | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Charles Chan | 114aec7 | 2017-06-19 14:00:53 -0700 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.segmentrouting; |
| 18 | |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.JsonNode; |
| 20 | import com.fasterxml.jackson.databind.ObjectMapper; |
Charles Chan | 114aec7 | 2017-06-19 14:00:53 -0700 | [diff] [blame] | 21 | import com.google.common.collect.Lists; |
| 22 | import com.google.common.collect.Maps; |
| 23 | import com.google.common.collect.Sets; |
| 24 | import org.junit.Before; |
| 25 | import org.junit.Test; |
| 26 | import org.onlab.packet.IpAddress; |
| 27 | import org.onlab.packet.IpPrefix; |
| 28 | import org.onlab.packet.MacAddress; |
| 29 | import org.onlab.packet.VlanId; |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 30 | import org.onosproject.net.config.ConfigApplyDelegate; |
Ray Milkey | c2d43be | 2017-08-03 11:58:29 -0700 | [diff] [blame] | 31 | import org.onosproject.net.intf.Interface; |
Charles Chan | 114aec7 | 2017-06-19 14:00:53 -0700 | [diff] [blame] | 32 | import org.onosproject.net.ConnectPoint; |
| 33 | import org.onosproject.net.DefaultHost; |
| 34 | import org.onosproject.net.DeviceId; |
| 35 | import org.onosproject.net.Host; |
| 36 | import org.onosproject.net.HostId; |
| 37 | import org.onosproject.net.HostLocation; |
| 38 | import org.onosproject.net.PortNumber; |
| 39 | import org.onosproject.net.config.NetworkConfigRegistryAdapter; |
Charles Chan | 114aec7 | 2017-06-19 14:00:53 -0700 | [diff] [blame] | 40 | import org.onosproject.net.flow.TrafficTreatment; |
Charles Chan | 114aec7 | 2017-06-19 14:00:53 -0700 | [diff] [blame] | 41 | import org.onosproject.net.host.HostEvent; |
| 42 | import org.onosproject.net.host.InterfaceIpAddress; |
| 43 | import org.onosproject.net.provider.ProviderId; |
| 44 | import org.onosproject.segmentrouting.config.DeviceConfiguration; |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 45 | import org.onosproject.segmentrouting.config.SegmentRoutingDeviceConfig; |
Charles Chan | 114aec7 | 2017-06-19 14:00:53 -0700 | [diff] [blame] | 46 | |
| 47 | import java.util.Map; |
Charles Chan | 114aec7 | 2017-06-19 14:00:53 -0700 | [diff] [blame] | 48 | import java.util.Set; |
Charles Chan | 114aec7 | 2017-06-19 14:00:53 -0700 | [diff] [blame] | 49 | |
| 50 | import static org.junit.Assert.*; |
| 51 | |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 52 | /**r |
Charles Chan | 114aec7 | 2017-06-19 14:00:53 -0700 | [diff] [blame] | 53 | * Unit test for {@link HostHandler}. |
| 54 | */ |
| 55 | public class HostHandlerTest { |
Charles Chan | 114aec7 | 2017-06-19 14:00:53 -0700 | [diff] [blame] | 56 | private HostHandler hostHandler; |
| 57 | |
| 58 | // Mocked routing and bridging tables |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 59 | private static final Map<MockBridgingTableKey, MockBridgingTableValue> BRIDGING_TABLE = |
| 60 | Maps.newConcurrentMap(); |
| 61 | private static final Map<MockRoutingTableKey, MockRoutingTableValue> ROUTING_TABLE = |
| 62 | Maps.newConcurrentMap(); |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 63 | private static final Map<ConnectPoint, Set<IpPrefix>> SUBNET_TABLE = Maps.newConcurrentMap(); |
Charles Chan | 114aec7 | 2017-06-19 14:00:53 -0700 | [diff] [blame] | 64 | // Mocked Next Id |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 65 | private static final Map<Integer, TrafficTreatment> NEXT_TABLE = Maps.newConcurrentMap(); |
Charles Chan | 114aec7 | 2017-06-19 14:00:53 -0700 | [diff] [blame] | 66 | |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 67 | // Host Mac, VLAN |
Charles Chan | 114aec7 | 2017-06-19 14:00:53 -0700 | [diff] [blame] | 68 | private static final ProviderId PROVIDER_ID = ProviderId.NONE; |
| 69 | private static final MacAddress HOST_MAC = MacAddress.valueOf("00:00:00:00:00:01"); |
| 70 | private static final VlanId HOST_VLAN_UNTAGGED = VlanId.NONE; |
| 71 | private static final HostId HOST_ID_UNTAGGED = HostId.hostId(HOST_MAC, HOST_VLAN_UNTAGGED); |
| 72 | private static final VlanId HOST_VLAN_TAGGED = VlanId.vlanId((short) 20); |
| 73 | private static final HostId HOST_ID_TAGGED = HostId.hostId(HOST_MAC, HOST_VLAN_TAGGED); |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 74 | // Host IP |
| 75 | private static final IpAddress HOST_IP11 = IpAddress.valueOf("10.0.1.1"); |
| 76 | private static final IpAddress HOST_IP21 = IpAddress.valueOf("10.0.2.1"); |
| 77 | private static final IpAddress HOST_IP12 = IpAddress.valueOf("10.0.1.2"); |
| 78 | private static final IpAddress HOST_IP13 = IpAddress.valueOf("10.0.1.3"); |
| 79 | private static final IpAddress HOST_IP14 = IpAddress.valueOf("10.0.1.4"); |
Charles Chan | ceb2a2e | 2017-09-12 18:57:47 -0700 | [diff] [blame] | 80 | private static final IpAddress HOST_IP32 = IpAddress.valueOf("10.0.3.2"); |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 81 | // Device |
| 82 | private static final DeviceId DEV1 = DeviceId.deviceId("of:0000000000000001"); |
| 83 | private static final DeviceId DEV2 = DeviceId.deviceId("of:0000000000000002"); |
Charles Chan | abfe7e0 | 2017-08-09 16:50:15 -0700 | [diff] [blame] | 84 | private static final DeviceId DEV3 = DeviceId.deviceId("of:0000000000000003"); |
| 85 | private static final DeviceId DEV4 = DeviceId.deviceId("of:0000000000000004"); |
| 86 | private static final DeviceId DEV5 = DeviceId.deviceId("of:0000000000000005"); |
| 87 | private static final DeviceId DEV6 = DeviceId.deviceId("of:0000000000000006"); |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 88 | // Port |
| 89 | private static final PortNumber P1 = PortNumber.portNumber(1); |
| 90 | private static final PortNumber P2 = PortNumber.portNumber(2); |
| 91 | private static final PortNumber P3 = PortNumber.portNumber(3); |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 92 | private static final PortNumber P9 = PortNumber.portNumber(9); |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 93 | // Connect Point |
| 94 | private static final ConnectPoint CP11 = new ConnectPoint(DEV1, P1); |
| 95 | private static final HostLocation HOST_LOC11 = new HostLocation(CP11, 0); |
| 96 | private static final ConnectPoint CP12 = new ConnectPoint(DEV1, P2); |
| 97 | private static final HostLocation HOST_LOC12 = new HostLocation(CP12, 0); |
| 98 | private static final ConnectPoint CP13 = new ConnectPoint(DEV1, P3); |
| 99 | private static final HostLocation HOST_LOC13 = new HostLocation(CP13, 0); |
| 100 | private static final ConnectPoint CP21 = new ConnectPoint(DEV2, P1); |
| 101 | private static final HostLocation HOST_LOC21 = new HostLocation(CP21, 0); |
| 102 | private static final ConnectPoint CP22 = new ConnectPoint(DEV2, P2); |
| 103 | private static final HostLocation HOST_LOC22 = new HostLocation(CP22, 0); |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 104 | // Connect Point for dual-homed host failover |
| 105 | private static final ConnectPoint CP31 = new ConnectPoint(DEV3, P1); |
| 106 | private static final HostLocation HOST_LOC31 = new HostLocation(CP31, 0); |
Charles Chan | ceb2a2e | 2017-09-12 18:57:47 -0700 | [diff] [blame] | 107 | private static final ConnectPoint CP32 = new ConnectPoint(DEV3, P2); |
| 108 | private static final HostLocation HOST_LOC32 = new HostLocation(CP32, 0); |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 109 | private static final ConnectPoint CP41 = new ConnectPoint(DEV4, P1); |
| 110 | private static final HostLocation HOST_LOC41 = new HostLocation(CP41, 0); |
| 111 | private static final ConnectPoint CP39 = new ConnectPoint(DEV3, P9); |
| 112 | private static final ConnectPoint CP49 = new ConnectPoint(DEV4, P9); |
Charles Chan | abfe7e0 | 2017-08-09 16:50:15 -0700 | [diff] [blame] | 113 | // Conenct Point for mastership test |
| 114 | private static final ConnectPoint CP51 = new ConnectPoint(DEV5, P1); |
| 115 | private static final HostLocation HOST_LOC51 = new HostLocation(CP51, 0); |
| 116 | private static final ConnectPoint CP61 = new ConnectPoint(DEV6, P1); |
| 117 | private static final HostLocation HOST_LOC61 = new HostLocation(CP61, 0); |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 118 | // Interface VLAN |
Charles Chan | 114aec7 | 2017-06-19 14:00:53 -0700 | [diff] [blame] | 119 | private static final VlanId INTF_VLAN_UNTAGGED = VlanId.vlanId((short) 10); |
Charles Chan | 114aec7 | 2017-06-19 14:00:53 -0700 | [diff] [blame] | 120 | private static final Set<VlanId> INTF_VLAN_TAGGED = Sets.newHashSet(VlanId.vlanId((short) 20)); |
| 121 | private static final VlanId INTF_VLAN_NATIVE = VlanId.vlanId((short) 30); |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 122 | private static final Set<VlanId> INTF_VLAN_PAIR = Sets.newHashSet(VlanId.vlanId((short) 10), |
| 123 | VlanId.vlanId((short) 20), VlanId.vlanId((short) 30)); |
Charles Chan | ceb2a2e | 2017-09-12 18:57:47 -0700 | [diff] [blame] | 124 | private static final VlanId INTF_VLAN_OTHER = VlanId.vlanId((short) 40); |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 125 | // Interface subnet |
| 126 | private static final IpPrefix INTF_PREFIX1 = IpPrefix.valueOf("10.0.1.254/24"); |
| 127 | private static final IpPrefix INTF_PREFIX2 = IpPrefix.valueOf("10.0.2.254/24"); |
Charles Chan | ceb2a2e | 2017-09-12 18:57:47 -0700 | [diff] [blame] | 128 | private static final IpPrefix INTF_PREFIX3 = IpPrefix.valueOf("10.0.3.254/24"); |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 129 | private static final InterfaceIpAddress INTF_IP1 = |
| 130 | new InterfaceIpAddress(INTF_PREFIX1.address(), INTF_PREFIX1); |
| 131 | private static final InterfaceIpAddress INTF_IP2 = |
| 132 | new InterfaceIpAddress(INTF_PREFIX2.address(), INTF_PREFIX2); |
Charles Chan | ceb2a2e | 2017-09-12 18:57:47 -0700 | [diff] [blame] | 133 | private static final InterfaceIpAddress INTF_IP3 = |
| 134 | new InterfaceIpAddress(INTF_PREFIX3.address(), INTF_PREFIX3); |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 135 | // Interfaces |
| 136 | private static final Interface INTF11 = |
| 137 | new Interface(null, CP11, Lists.newArrayList(INTF_IP1), MacAddress.NONE, null, |
| 138 | INTF_VLAN_UNTAGGED, null, null); |
| 139 | private static final Interface INTF12 = |
| 140 | new Interface(null, CP12, Lists.newArrayList(INTF_IP1), MacAddress.NONE, null, |
| 141 | INTF_VLAN_UNTAGGED, null, null); |
| 142 | private static final Interface INTF13 = |
| 143 | new Interface(null, CP13, Lists.newArrayList(INTF_IP2), MacAddress.NONE, null, |
| 144 | null, INTF_VLAN_TAGGED, INTF_VLAN_NATIVE); |
| 145 | private static final Interface INTF21 = |
| 146 | new Interface(null, CP21, Lists.newArrayList(INTF_IP1), MacAddress.NONE, null, |
| 147 | INTF_VLAN_UNTAGGED, null, null); |
| 148 | private static final Interface INTF22 = |
| 149 | new Interface(null, CP22, Lists.newArrayList(INTF_IP1), MacAddress.NONE, null, |
| 150 | INTF_VLAN_UNTAGGED, null, null); |
| 151 | private static final Interface INTF31 = |
| 152 | new Interface(null, CP31, Lists.newArrayList(INTF_IP1), MacAddress.NONE, null, |
| 153 | INTF_VLAN_UNTAGGED, null, null); |
Charles Chan | ceb2a2e | 2017-09-12 18:57:47 -0700 | [diff] [blame] | 154 | private static final Interface INTF32 = |
| 155 | new Interface(null, CP32, Lists.newArrayList(INTF_IP3), MacAddress.NONE, null, |
| 156 | INTF_VLAN_OTHER, null, null); |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 157 | private static final Interface INTF39 = |
| 158 | new Interface(null, CP39, Lists.newArrayList(INTF_IP1), MacAddress.NONE, null, |
| 159 | null, INTF_VLAN_PAIR, null); |
| 160 | private static final Interface INTF41 = |
| 161 | new Interface(null, CP41, Lists.newArrayList(INTF_IP1), MacAddress.NONE, null, |
| 162 | INTF_VLAN_UNTAGGED, null, null); |
| 163 | private static final Interface INTF49 = |
| 164 | new Interface(null, CP49, Lists.newArrayList(INTF_IP1), MacAddress.NONE, null, |
| 165 | null, INTF_VLAN_PAIR, null); |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 166 | // Host |
| 167 | private static final Host HOST1 = new DefaultHost(PROVIDER_ID, HOST_ID_UNTAGGED, HOST_MAC, |
| 168 | HOST_VLAN_UNTAGGED, Sets.newHashSet(HOST_LOC11, HOST_LOC21), Sets.newHashSet(HOST_IP11), |
| 169 | false); |
Charles Chan | 114aec7 | 2017-06-19 14:00:53 -0700 | [diff] [blame] | 170 | |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 171 | // A set of hosts |
| 172 | private static final Set<Host> HOSTS = Sets.newHashSet(HOST1); |
| 173 | // A set of devices of which we have mastership |
| 174 | private static final Set<DeviceId> LOCAL_DEVICES = Sets.newHashSet(DEV1, DEV2, DEV3, DEV4); |
| 175 | // A set of interfaces |
| 176 | private static final Set<Interface> INTERFACES = Sets.newHashSet(INTF11, INTF12, INTF13, INTF21, |
Charles Chan | ceb2a2e | 2017-09-12 18:57:47 -0700 | [diff] [blame] | 177 | INTF22, INTF31, INTF32, INTF39, INTF41, INTF49); |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 178 | |
Charles Chan | 114aec7 | 2017-06-19 14:00:53 -0700 | [diff] [blame] | 179 | @Before |
| 180 | public void setUp() throws Exception { |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 181 | // Initialize pairDevice and pairLocalPort config |
| 182 | ObjectMapper mapper = new ObjectMapper(); |
| 183 | ConfigApplyDelegate delegate = config -> { }; |
| 184 | |
| 185 | SegmentRoutingDeviceConfig dev3Config = new SegmentRoutingDeviceConfig(); |
| 186 | JsonNode dev3Tree = mapper.createObjectNode(); |
| 187 | dev3Config.init(DEV3, "host-handler-test", dev3Tree, mapper, delegate); |
| 188 | dev3Config.setPairDeviceId(DEV4).setPairLocalPort(P9); |
| 189 | |
| 190 | SegmentRoutingDeviceConfig dev4Config = new SegmentRoutingDeviceConfig(); |
| 191 | JsonNode dev4Tree = mapper.createObjectNode(); |
| 192 | dev4Config.init(DEV4, "host-handler-test", dev4Tree, mapper, delegate); |
| 193 | dev4Config.setPairDeviceId(DEV3).setPairLocalPort(P9); |
| 194 | |
| 195 | MockNetworkConfigRegistry mockNetworkConfigRegistry = new MockNetworkConfigRegistry(); |
| 196 | mockNetworkConfigRegistry.applyConfig(dev3Config); |
| 197 | mockNetworkConfigRegistry.applyConfig(dev4Config); |
| 198 | |
| 199 | // Initialize Segment Routing Manager |
| 200 | SegmentRoutingManager srManager = new MockSegmentRoutingManager(NEXT_TABLE); |
Charles Chan | 114aec7 | 2017-06-19 14:00:53 -0700 | [diff] [blame] | 201 | srManager.cfgService = new NetworkConfigRegistryAdapter(); |
| 202 | srManager.deviceConfiguration = new DeviceConfiguration(srManager); |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 203 | srManager.flowObjectiveService = new MockFlowObjectiveService(BRIDGING_TABLE, NEXT_TABLE); |
| 204 | srManager.routingRulePopulator = new MockRoutingRulePopulator(srManager, ROUTING_TABLE); |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 205 | srManager.defaultRoutingHandler = new MockDefaultRoutingHandler(srManager, SUBNET_TABLE); |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 206 | srManager.interfaceService = new MockInterfaceService(INTERFACES); |
| 207 | srManager.mastershipService = new MockMastershipService(LOCAL_DEVICES); |
| 208 | srManager.hostService = new MockHostService(HOSTS); |
| 209 | srManager.cfgService = mockNetworkConfigRegistry; |
Charles Chan | 114aec7 | 2017-06-19 14:00:53 -0700 | [diff] [blame] | 210 | |
| 211 | hostHandler = new HostHandler(srManager); |
| 212 | |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 213 | ROUTING_TABLE.clear(); |
| 214 | BRIDGING_TABLE.clear(); |
Charles Chan | 114aec7 | 2017-06-19 14:00:53 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | @Test |
| 218 | public void init() throws Exception { |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 219 | hostHandler.init(DEV1); |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 220 | assertEquals(1, ROUTING_TABLE.size()); |
| 221 | assertNotNull(ROUTING_TABLE.get(new MockRoutingTableKey(DEV1, HOST_IP11.toIpPrefix()))); |
| 222 | assertEquals(1, BRIDGING_TABLE.size()); |
| 223 | assertNotNull(BRIDGING_TABLE.get(new MockBridgingTableKey(DEV1, HOST_MAC, INTF_VLAN_UNTAGGED))); |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 224 | |
| 225 | hostHandler.init(DEV2); |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 226 | assertEquals(2, ROUTING_TABLE.size()); |
| 227 | assertNotNull(ROUTING_TABLE.get(new MockRoutingTableKey(DEV1, HOST_IP11.toIpPrefix()))); |
| 228 | assertNotNull(ROUTING_TABLE.get(new MockRoutingTableKey(DEV2, HOST_IP11.toIpPrefix()))); |
| 229 | assertEquals(2, BRIDGING_TABLE.size()); |
| 230 | assertNotNull(BRIDGING_TABLE.get(new MockBridgingTableKey(DEV1, HOST_MAC, INTF_VLAN_UNTAGGED))); |
| 231 | assertNotNull(BRIDGING_TABLE.get(new MockBridgingTableKey(DEV2, HOST_MAC, INTF_VLAN_UNTAGGED))); |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | @Test(expected = IllegalArgumentException.class) |
| 235 | public void testHostAddedAtWrongLocation() throws Exception { |
| 236 | hostHandler.processHostAddedAtLocation(HOST1, HOST_LOC13); |
| 237 | } |
| 238 | |
| 239 | |
| 240 | @Test() |
| 241 | public void testHostAddedAtCorrectLocation() throws Exception { |
| 242 | hostHandler.processHostAddedAtLocation(HOST1, HOST_LOC11); |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 243 | assertEquals(1, ROUTING_TABLE.size()); |
| 244 | assertNotNull(ROUTING_TABLE.get(new MockRoutingTableKey(DEV1, HOST_IP11.toIpPrefix()))); |
| 245 | assertEquals(1, BRIDGING_TABLE.size()); |
| 246 | assertNotNull(BRIDGING_TABLE.get(new MockBridgingTableKey(DEV1, HOST_MAC, INTF_VLAN_UNTAGGED))); |
Charles Chan | 114aec7 | 2017-06-19 14:00:53 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | @Test |
| 250 | public void testHostAdded() throws Exception { |
| 251 | Host subject; |
| 252 | |
| 253 | // Untagged host discovered on untagged port |
| 254 | // Expect: add one routing rule and one bridging rule |
| 255 | subject = new DefaultHost(PROVIDER_ID, HOST_ID_UNTAGGED, HOST_MAC, HOST_VLAN_UNTAGGED, |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 256 | Sets.newHashSet(HOST_LOC11), Sets.newHashSet(HOST_IP11), false); |
Charles Chan | 114aec7 | 2017-06-19 14:00:53 -0700 | [diff] [blame] | 257 | hostHandler.processHostAddedEvent(new HostEvent(HostEvent.Type.HOST_ADDED, subject)); |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 258 | assertEquals(1, ROUTING_TABLE.size()); |
| 259 | assertNotNull(ROUTING_TABLE.get(new MockRoutingTableKey(DEV1, HOST_IP11.toIpPrefix()))); |
| 260 | assertEquals(1, BRIDGING_TABLE.size()); |
| 261 | assertNotNull(BRIDGING_TABLE.get(new MockBridgingTableKey(DEV1, HOST_MAC, INTF_VLAN_UNTAGGED))); |
Charles Chan | 114aec7 | 2017-06-19 14:00:53 -0700 | [diff] [blame] | 262 | |
| 263 | // Untagged host discovered on tagged/native port |
| 264 | // Expect: add one routing rule and one bridging rule |
| 265 | subject = new DefaultHost(PROVIDER_ID, HOST_ID_UNTAGGED, HOST_MAC, HOST_VLAN_UNTAGGED, |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 266 | Sets.newHashSet(HOST_LOC13), Sets.newHashSet(HOST_IP21), false); |
Charles Chan | 114aec7 | 2017-06-19 14:00:53 -0700 | [diff] [blame] | 267 | hostHandler.processHostAddedEvent(new HostEvent(HostEvent.Type.HOST_ADDED, subject)); |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 268 | assertEquals(2, ROUTING_TABLE.size()); |
| 269 | assertNotNull(ROUTING_TABLE.get(new MockRoutingTableKey(DEV1, HOST_IP21.toIpPrefix()))); |
| 270 | assertEquals(2, BRIDGING_TABLE.size()); |
| 271 | assertNotNull(BRIDGING_TABLE.get(new MockBridgingTableKey(DEV1, HOST_MAC, INTF_VLAN_NATIVE))); |
Charles Chan | 114aec7 | 2017-06-19 14:00:53 -0700 | [diff] [blame] | 272 | |
| 273 | // Tagged host discovered on untagged port |
| 274 | // Expect: ignore the host. No rule is added. |
| 275 | subject = new DefaultHost(PROVIDER_ID, HOST_ID_TAGGED, HOST_MAC, HOST_VLAN_TAGGED, |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 276 | Sets.newHashSet(HOST_LOC11), Sets.newHashSet(HOST_IP11), false); |
Charles Chan | 114aec7 | 2017-06-19 14:00:53 -0700 | [diff] [blame] | 277 | hostHandler.processHostAddedEvent(new HostEvent(HostEvent.Type.HOST_ADDED, subject)); |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 278 | assertEquals(2, ROUTING_TABLE.size()); |
| 279 | assertEquals(2, BRIDGING_TABLE.size()); |
Charles Chan | 114aec7 | 2017-06-19 14:00:53 -0700 | [diff] [blame] | 280 | |
| 281 | // Tagged host discovered on tagged port with the same IP |
| 282 | // Expect: update existing route, add one bridging rule |
| 283 | subject = new DefaultHost(PROVIDER_ID, HOST_ID_TAGGED, HOST_MAC, HOST_VLAN_TAGGED, |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 284 | Sets.newHashSet(HOST_LOC13), Sets.newHashSet(HOST_IP21), false); |
Charles Chan | 114aec7 | 2017-06-19 14:00:53 -0700 | [diff] [blame] | 285 | hostHandler.processHostAddedEvent(new HostEvent(HostEvent.Type.HOST_ADDED, subject)); |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 286 | assertEquals(2, ROUTING_TABLE.size()); |
| 287 | assertNotNull(ROUTING_TABLE.get(new MockRoutingTableKey(DEV1, HOST_IP21.toIpPrefix()))); |
| 288 | assertEquals(HOST_VLAN_TAGGED, ROUTING_TABLE.get(new MockRoutingTableKey(HOST_LOC13.deviceId(), |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 289 | HOST_IP21.toIpPrefix())).vlanId); |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 290 | assertEquals(3, BRIDGING_TABLE.size()); |
| 291 | assertNotNull(BRIDGING_TABLE.get(new MockBridgingTableKey(DEV1, HOST_MAC, HOST_VLAN_TAGGED))); |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | @Test |
| 295 | public void testDualHomedHostAdded() throws Exception { |
| 296 | // Add a dual-homed host that has 2 locations |
| 297 | // Expect: add two routing rules and two bridging rules |
| 298 | Host subject = new DefaultHost(PROVIDER_ID, HOST_ID_UNTAGGED, HOST_MAC, HOST_VLAN_UNTAGGED, |
| 299 | Sets.newHashSet(HOST_LOC11, HOST_LOC21), Sets.newHashSet(HOST_IP11), false); |
| 300 | hostHandler.processHostAddedEvent(new HostEvent(HostEvent.Type.HOST_ADDED, subject)); |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 301 | assertEquals(2, ROUTING_TABLE.size()); |
| 302 | assertNotNull(ROUTING_TABLE.get(new MockRoutingTableKey(DEV1, HOST_IP11.toIpPrefix()))); |
| 303 | assertNotNull(ROUTING_TABLE.get(new MockRoutingTableKey(DEV2, HOST_IP11.toIpPrefix()))); |
| 304 | assertEquals(2, BRIDGING_TABLE.size()); |
| 305 | assertNotNull(BRIDGING_TABLE.get(new MockBridgingTableKey(DEV1, HOST_MAC, INTF_VLAN_UNTAGGED))); |
| 306 | assertNotNull(BRIDGING_TABLE.get(new MockBridgingTableKey(DEV2, HOST_MAC, INTF_VLAN_UNTAGGED))); |
Charles Chan | 114aec7 | 2017-06-19 14:00:53 -0700 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | @Test |
Charles Chan | ceb2a2e | 2017-09-12 18:57:47 -0700 | [diff] [blame] | 310 | public void testSingleHomedHostAddedOnPairLeaf() throws Exception { |
| 311 | Host host1 = new DefaultHost(PROVIDER_ID, HOST_ID_UNTAGGED, HOST_MAC, HOST_VLAN_UNTAGGED, |
| 312 | Sets.newHashSet(HOST_LOC32), Sets.newHashSet(HOST_IP32), false); |
| 313 | |
| 314 | // Add a single-homed host with one location |
| 315 | // Expect: the pair link should not be utilized |
| 316 | hostHandler.processHostAddedEvent(new HostEvent(HostEvent.Type.HOST_ADDED, host1)); |
| 317 | assertEquals(1, ROUTING_TABLE.size()); |
| 318 | assertEquals(P2, ROUTING_TABLE.get(new MockRoutingTableKey(DEV3, HOST_IP32.toIpPrefix())).portNumber); |
| 319 | assertEquals(1, BRIDGING_TABLE.size()); |
| 320 | assertEquals(P2, BRIDGING_TABLE.get(new MockBridgingTableKey(DEV3, HOST_MAC, INTF_VLAN_OTHER)).portNumber); |
| 321 | } |
| 322 | |
| 323 | @Test |
| 324 | public void testDualHomedHostAddedOneByOne() throws Exception { |
| 325 | Host host1 = new DefaultHost(PROVIDER_ID, HOST_ID_UNTAGGED, HOST_MAC, HOST_VLAN_UNTAGGED, |
| 326 | Sets.newHashSet(HOST_LOC31), Sets.newHashSet(HOST_IP11), false); |
| 327 | Host host2 = new DefaultHost(PROVIDER_ID, HOST_ID_UNTAGGED, HOST_MAC, HOST_VLAN_UNTAGGED, |
| 328 | Sets.newHashSet(HOST_LOC31, HOST_LOC41), Sets.newHashSet(HOST_IP11), false); |
| 329 | |
| 330 | // Add a dual-homed host with one location |
| 331 | // Expect: the pair link is utilized temporarily before the second location is discovered |
| 332 | hostHandler.processHostAddedEvent(new HostEvent(HostEvent.Type.HOST_ADDED, host1)); |
| 333 | assertEquals(2, ROUTING_TABLE.size()); |
| 334 | assertEquals(P1, ROUTING_TABLE.get(new MockRoutingTableKey(DEV3, HOST_IP11.toIpPrefix())).portNumber); |
| 335 | assertEquals(P9, ROUTING_TABLE.get(new MockRoutingTableKey(DEV4, HOST_IP11.toIpPrefix())).portNumber); |
| 336 | assertEquals(2, BRIDGING_TABLE.size()); |
| 337 | assertEquals(P1, BRIDGING_TABLE.get(new MockBridgingTableKey(DEV3, HOST_MAC, INTF_VLAN_UNTAGGED)).portNumber); |
| 338 | assertEquals(P9, BRIDGING_TABLE.get(new MockBridgingTableKey(DEV4, HOST_MAC, INTF_VLAN_UNTAGGED)).portNumber); |
| 339 | |
| 340 | // Add the second location of dual-homed host |
| 341 | // Expect: no longer use the pair link |
| 342 | hostHandler.processHostMovedEvent(new HostEvent(HostEvent.Type.HOST_MOVED, host2, host1)); |
| 343 | assertEquals(2, ROUTING_TABLE.size()); |
| 344 | assertEquals(P1, ROUTING_TABLE.get(new MockRoutingTableKey(DEV3, HOST_IP11.toIpPrefix())).portNumber); |
| 345 | assertEquals(P1, ROUTING_TABLE.get(new MockRoutingTableKey(DEV4, HOST_IP11.toIpPrefix())).portNumber); |
| 346 | assertEquals(2, BRIDGING_TABLE.size()); |
| 347 | assertEquals(P1, BRIDGING_TABLE.get(new MockBridgingTableKey(DEV3, HOST_MAC, INTF_VLAN_UNTAGGED)).portNumber); |
| 348 | assertEquals(P1, BRIDGING_TABLE.get(new MockBridgingTableKey(DEV4, HOST_MAC, INTF_VLAN_UNTAGGED)).portNumber); |
| 349 | } |
| 350 | |
| 351 | @Test |
Charles Chan | 114aec7 | 2017-06-19 14:00:53 -0700 | [diff] [blame] | 352 | public void testHostRemoved() throws Exception { |
| 353 | Host subject = new DefaultHost(PROVIDER_ID, HOST_ID_UNTAGGED, HOST_MAC, HOST_VLAN_UNTAGGED, |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 354 | Sets.newHashSet(HOST_LOC11), Sets.newHashSet(HOST_IP11), false); |
Charles Chan | 114aec7 | 2017-06-19 14:00:53 -0700 | [diff] [blame] | 355 | |
| 356 | // Add a host |
| 357 | // Expect: add one routing rule and one bridging rule |
| 358 | hostHandler.processHostAddedEvent(new HostEvent(HostEvent.Type.HOST_ADDED, subject)); |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 359 | assertEquals(1, ROUTING_TABLE.size()); |
| 360 | assertNotNull(ROUTING_TABLE.get(new MockRoutingTableKey(DEV1, HOST_IP11.toIpPrefix()))); |
| 361 | assertEquals(1, BRIDGING_TABLE.size()); |
| 362 | assertNotNull(BRIDGING_TABLE.get(new MockBridgingTableKey(DEV1, HOST_MAC, INTF_VLAN_UNTAGGED))); |
Charles Chan | 114aec7 | 2017-06-19 14:00:53 -0700 | [diff] [blame] | 363 | |
| 364 | // Remove the host |
| 365 | // Expect: add the routing rule and the bridging rule |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 366 | hostHandler.processHostRemovedEvent(new HostEvent(HostEvent.Type.HOST_REMOVED, subject)); |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 367 | assertEquals(0, ROUTING_TABLE.size()); |
| 368 | assertEquals(0, BRIDGING_TABLE.size()); |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | @Test |
| 372 | public void testDualHomedHostRemoved() throws Exception { |
| 373 | // Add a dual-homed host that has 2 locations |
| 374 | // Expect: add two routing rules and two bridging rules |
| 375 | Host subject = new DefaultHost(PROVIDER_ID, HOST_ID_UNTAGGED, HOST_MAC, HOST_VLAN_UNTAGGED, |
| 376 | Sets.newHashSet(HOST_LOC11, HOST_LOC21), Sets.newHashSet(HOST_IP11), false); |
| 377 | hostHandler.processHostAddedEvent(new HostEvent(HostEvent.Type.HOST_ADDED, subject)); |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 378 | assertEquals(2, ROUTING_TABLE.size()); |
| 379 | assertNotNull(ROUTING_TABLE.get(new MockRoutingTableKey(DEV1, HOST_IP11.toIpPrefix()))); |
| 380 | assertNotNull(ROUTING_TABLE.get(new MockRoutingTableKey(DEV2, HOST_IP11.toIpPrefix()))); |
| 381 | assertEquals(2, BRIDGING_TABLE.size()); |
| 382 | assertNotNull(BRIDGING_TABLE.get(new MockBridgingTableKey(DEV1, HOST_MAC, INTF_VLAN_UNTAGGED))); |
| 383 | assertNotNull(BRIDGING_TABLE.get(new MockBridgingTableKey(DEV2, HOST_MAC, INTF_VLAN_UNTAGGED))); |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 384 | |
| 385 | // Remove a dual-homed host that has 2 locations |
| 386 | // Expect: all routing and bridging rules are removed |
| 387 | hostHandler.processHostRemovedEvent(new HostEvent(HostEvent.Type.HOST_REMOVED, subject)); |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 388 | assertEquals(0, ROUTING_TABLE.size()); |
| 389 | assertEquals(0, BRIDGING_TABLE.size()); |
Charles Chan | 114aec7 | 2017-06-19 14:00:53 -0700 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | @Test |
| 393 | public void testHostMoved() throws Exception { |
| 394 | Host host1 = new DefaultHost(PROVIDER_ID, HOST_ID_UNTAGGED, HOST_MAC, HOST_VLAN_UNTAGGED, |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 395 | Sets.newHashSet(HOST_LOC11), Sets.newHashSet(HOST_IP11), false); |
Charles Chan | 114aec7 | 2017-06-19 14:00:53 -0700 | [diff] [blame] | 396 | Host host2 = new DefaultHost(PROVIDER_ID, HOST_ID_UNTAGGED, HOST_MAC, HOST_VLAN_UNTAGGED, |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 397 | Sets.newHashSet(HOST_LOC21), Sets.newHashSet(HOST_IP11), false); |
Charles Chan | 114aec7 | 2017-06-19 14:00:53 -0700 | [diff] [blame] | 398 | Host host3 = new DefaultHost(PROVIDER_ID, HOST_ID_UNTAGGED, HOST_MAC, HOST_VLAN_UNTAGGED, |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 399 | Sets.newHashSet(HOST_LOC13), Sets.newHashSet(HOST_IP11), false); |
Charles Chan | 114aec7 | 2017-06-19 14:00:53 -0700 | [diff] [blame] | 400 | |
| 401 | // Add a host |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 402 | // Expect: add one new routing rule, one new bridging rule |
Charles Chan | 114aec7 | 2017-06-19 14:00:53 -0700 | [diff] [blame] | 403 | hostHandler.processHostAddedEvent(new HostEvent(HostEvent.Type.HOST_ADDED, host1)); |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 404 | assertEquals(1, ROUTING_TABLE.size()); |
| 405 | assertNotNull(ROUTING_TABLE.get(new MockRoutingTableKey(DEV1, HOST_IP11.toIpPrefix()))); |
| 406 | assertNull(ROUTING_TABLE.get(new MockRoutingTableKey(DEV1, HOST_IP21.toIpPrefix()))); |
| 407 | assertNull(ROUTING_TABLE.get(new MockRoutingTableKey(DEV1, HOST_IP13.toIpPrefix()))); |
| 408 | assertEquals(1, BRIDGING_TABLE.size()); |
| 409 | assertNotNull(BRIDGING_TABLE.get(new MockBridgingTableKey(DEV1, HOST_MAC, INTF_VLAN_UNTAGGED))); |
| 410 | assertNull(BRIDGING_TABLE.get(new MockBridgingTableKey(DEV2, HOST_MAC, INTF_VLAN_UNTAGGED))); |
Charles Chan | 114aec7 | 2017-06-19 14:00:53 -0700 | [diff] [blame] | 411 | |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 412 | // Move the host to CP13, which has different subnet setting |
Charles Chan | 114aec7 | 2017-06-19 14:00:53 -0700 | [diff] [blame] | 413 | // Expect: remove routing rule. Change vlan in bridging rule. |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 414 | hostHandler.processHostMovedEvent(new HostEvent(HostEvent.Type.HOST_MOVED, host3, host1)); |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 415 | assertEquals(0, ROUTING_TABLE.size()); |
| 416 | assertEquals(1, BRIDGING_TABLE.size()); |
| 417 | assertNotNull(BRIDGING_TABLE.get(new MockBridgingTableKey(DEV1, HOST_MAC, INTF_VLAN_NATIVE))); |
| 418 | assertNull(BRIDGING_TABLE.get(new MockBridgingTableKey(DEV1, HOST_MAC, INTF_VLAN_UNTAGGED))); |
Charles Chan | 114aec7 | 2017-06-19 14:00:53 -0700 | [diff] [blame] | 419 | |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 420 | // Move the host to CP21, which has same subnet setting |
Charles Chan | 114aec7 | 2017-06-19 14:00:53 -0700 | [diff] [blame] | 421 | // Expect: add a new routing rule. Change vlan in bridging rule. |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 422 | hostHandler.processHostMovedEvent(new HostEvent(HostEvent.Type.HOST_MOVED, host2, host3)); |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 423 | assertEquals(1, ROUTING_TABLE.size()); |
| 424 | assertNull(ROUTING_TABLE.get(new MockRoutingTableKey(DEV1, HOST_IP11.toIpPrefix()))); |
| 425 | assertNull(ROUTING_TABLE.get(new MockRoutingTableKey(DEV1, HOST_IP11.toIpPrefix()))); |
| 426 | assertNotNull(ROUTING_TABLE.get(new MockRoutingTableKey(DEV2, HOST_IP11.toIpPrefix()))); |
| 427 | assertEquals(1, BRIDGING_TABLE.size()); |
| 428 | assertNull(BRIDGING_TABLE.get(new MockBridgingTableKey(DEV1, HOST_MAC, INTF_VLAN_UNTAGGED))); |
| 429 | assertNotNull(BRIDGING_TABLE.get(new MockBridgingTableKey(DEV2, HOST_MAC, INTF_VLAN_UNTAGGED))); |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | @Test |
| 433 | public void testDualHomedHostMoved() throws Exception { |
| 434 | Host host1 = new DefaultHost(PROVIDER_ID, HOST_ID_UNTAGGED, HOST_MAC, HOST_VLAN_UNTAGGED, |
| 435 | Sets.newHashSet(HOST_LOC11, HOST_LOC21), Sets.newHashSet(HOST_IP11, HOST_IP12), false); |
| 436 | Host host2 = new DefaultHost(PROVIDER_ID, HOST_ID_UNTAGGED, HOST_MAC, HOST_VLAN_UNTAGGED, |
| 437 | Sets.newHashSet(HOST_LOC12, HOST_LOC22), Sets.newHashSet(HOST_IP11, HOST_IP12), false); |
| 438 | Host host3 = new DefaultHost(PROVIDER_ID, HOST_ID_UNTAGGED, HOST_MAC, HOST_VLAN_UNTAGGED, |
| 439 | Sets.newHashSet(HOST_LOC11, HOST_LOC21), Sets.newHashSet(HOST_IP13, HOST_IP14), false); |
| 440 | Host host4 = new DefaultHost(PROVIDER_ID, HOST_ID_UNTAGGED, HOST_MAC, HOST_VLAN_UNTAGGED, |
| 441 | Sets.newHashSet(HOST_LOC11, HOST_LOC22), Sets.newHashSet(HOST_IP12, HOST_IP13), false); |
| 442 | |
| 443 | // Add a host with IP11, IP12 and LOC11, LOC21 |
| 444 | // Expect: 4 routing rules and 2 bridging rules |
| 445 | hostHandler.processHostAddedEvent(new HostEvent(HostEvent.Type.HOST_ADDED, host1)); |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 446 | assertEquals(4, ROUTING_TABLE.size()); |
| 447 | assertEquals(P1, ROUTING_TABLE.get(new MockRoutingTableKey(DEV1, HOST_IP11.toIpPrefix())).portNumber); |
| 448 | assertEquals(P1, ROUTING_TABLE.get(new MockRoutingTableKey(DEV1, HOST_IP12.toIpPrefix())).portNumber); |
| 449 | assertEquals(P1, ROUTING_TABLE.get(new MockRoutingTableKey(DEV2, HOST_IP11.toIpPrefix())).portNumber); |
| 450 | assertEquals(P1, ROUTING_TABLE.get(new MockRoutingTableKey(DEV2, HOST_IP12.toIpPrefix())).portNumber); |
| 451 | assertEquals(2, BRIDGING_TABLE.size()); |
| 452 | assertEquals(P1, BRIDGING_TABLE.get(new MockBridgingTableKey(DEV1, HOST_MAC, INTF_VLAN_UNTAGGED)).portNumber); |
| 453 | assertEquals(P1, BRIDGING_TABLE.get(new MockBridgingTableKey(DEV2, HOST_MAC, INTF_VLAN_UNTAGGED)).portNumber); |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 454 | |
| 455 | // Move the host to LOC12, LOC22 and keep the IP |
| 456 | // Expect: 4 routing rules and 2 bridging rules all at the new location |
| 457 | hostHandler.processHostMovedEvent(new HostEvent(HostEvent.Type.HOST_MOVED, host2, host1)); |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 458 | assertEquals(4, ROUTING_TABLE.size()); |
| 459 | assertEquals(P2, ROUTING_TABLE.get(new MockRoutingTableKey(DEV1, HOST_IP11.toIpPrefix())).portNumber); |
| 460 | assertEquals(P2, ROUTING_TABLE.get(new MockRoutingTableKey(DEV1, HOST_IP12.toIpPrefix())).portNumber); |
| 461 | assertEquals(P2, ROUTING_TABLE.get(new MockRoutingTableKey(DEV2, HOST_IP11.toIpPrefix())).portNumber); |
| 462 | assertEquals(P2, ROUTING_TABLE.get(new MockRoutingTableKey(DEV2, HOST_IP12.toIpPrefix())).portNumber); |
| 463 | assertEquals(2, BRIDGING_TABLE.size()); |
| 464 | assertEquals(P2, BRIDGING_TABLE.get(new MockBridgingTableKey(DEV1, HOST_MAC, INTF_VLAN_UNTAGGED)).portNumber); |
| 465 | assertEquals(P2, BRIDGING_TABLE.get(new MockBridgingTableKey(DEV2, HOST_MAC, INTF_VLAN_UNTAGGED)).portNumber); |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 466 | |
| 467 | // Move the host to LOC11, LOC21 and change the IP to IP13, IP14 at the same time |
| 468 | // Expect: 4 routing rules and 2 bridging rules all at the new location |
| 469 | hostHandler.processHostMovedEvent(new HostEvent(HostEvent.Type.HOST_MOVED, host3, host2)); |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 470 | assertEquals(4, ROUTING_TABLE.size()); |
| 471 | assertEquals(P1, ROUTING_TABLE.get(new MockRoutingTableKey(DEV1, HOST_IP13.toIpPrefix())).portNumber); |
| 472 | assertEquals(P1, ROUTING_TABLE.get(new MockRoutingTableKey(DEV1, HOST_IP14.toIpPrefix())).portNumber); |
| 473 | assertEquals(P1, ROUTING_TABLE.get(new MockRoutingTableKey(DEV2, HOST_IP13.toIpPrefix())).portNumber); |
| 474 | assertEquals(P1, ROUTING_TABLE.get(new MockRoutingTableKey(DEV2, HOST_IP14.toIpPrefix())).portNumber); |
| 475 | assertEquals(2, BRIDGING_TABLE.size()); |
| 476 | assertEquals(P1, BRIDGING_TABLE.get(new MockBridgingTableKey(DEV1, HOST_MAC, INTF_VLAN_UNTAGGED)).portNumber); |
| 477 | assertEquals(P1, BRIDGING_TABLE.get(new MockBridgingTableKey(DEV2, HOST_MAC, INTF_VLAN_UNTAGGED)).portNumber); |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 478 | |
| 479 | // Move the host to LOC11, LOC22 and change the IP to IP12, IP13 at the same time |
| 480 | // Expect: 4 routing rules and 2 bridging rules all at the new location |
| 481 | hostHandler.processHostMovedEvent(new HostEvent(HostEvent.Type.HOST_MOVED, host4, host3)); |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 482 | assertEquals(4, ROUTING_TABLE.size()); |
| 483 | assertEquals(P1, ROUTING_TABLE.get(new MockRoutingTableKey(DEV1, HOST_IP12.toIpPrefix())).portNumber); |
| 484 | assertEquals(P1, ROUTING_TABLE.get(new MockRoutingTableKey(DEV1, HOST_IP13.toIpPrefix())).portNumber); |
| 485 | assertEquals(P2, ROUTING_TABLE.get(new MockRoutingTableKey(DEV2, HOST_IP12.toIpPrefix())).portNumber); |
| 486 | assertEquals(P2, ROUTING_TABLE.get(new MockRoutingTableKey(DEV2, HOST_IP13.toIpPrefix())).portNumber); |
| 487 | assertEquals(2, BRIDGING_TABLE.size()); |
| 488 | assertEquals(P1, BRIDGING_TABLE.get(new MockBridgingTableKey(DEV1, HOST_MAC, INTF_VLAN_UNTAGGED)).portNumber); |
| 489 | assertEquals(P2, BRIDGING_TABLE.get(new MockBridgingTableKey(DEV2, HOST_MAC, INTF_VLAN_UNTAGGED)).portNumber); |
Charles Chan | 114aec7 | 2017-06-19 14:00:53 -0700 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | @Test |
Charles Chan | abfe7e0 | 2017-08-09 16:50:15 -0700 | [diff] [blame] | 493 | public void testHostMoveToInvalidLocation() throws Exception { |
| 494 | Host host1 = new DefaultHost(PROVIDER_ID, HOST_ID_UNTAGGED, HOST_MAC, HOST_VLAN_UNTAGGED, |
| 495 | Sets.newHashSet(HOST_LOC11), Sets.newHashSet(HOST_IP11), false); |
| 496 | Host host2 = new DefaultHost(PROVIDER_ID, HOST_ID_UNTAGGED, HOST_MAC, HOST_VLAN_UNTAGGED, |
| 497 | Sets.newHashSet(HOST_LOC51), Sets.newHashSet(HOST_IP11), false); |
| 498 | |
| 499 | // Add a host |
| 500 | // Expect: add one new routing rule, one new bridging rule |
| 501 | hostHandler.processHostAddedEvent(new HostEvent(HostEvent.Type.HOST_ADDED, host1)); |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 502 | assertEquals(1, ROUTING_TABLE.size()); |
| 503 | assertNotNull(ROUTING_TABLE.get(new MockRoutingTableKey(DEV1, HOST_IP11.toIpPrefix()))); |
| 504 | assertNull(ROUTING_TABLE.get(new MockRoutingTableKey(DEV1, HOST_IP21.toIpPrefix()))); |
| 505 | assertEquals(1, BRIDGING_TABLE.size()); |
| 506 | assertNotNull(BRIDGING_TABLE.get(new MockBridgingTableKey(DEV1, HOST_MAC, INTF_VLAN_UNTAGGED))); |
| 507 | assertNull(BRIDGING_TABLE.get(new MockBridgingTableKey(DEV2, HOST_MAC, INTF_VLAN_UNTAGGED))); |
Charles Chan | abfe7e0 | 2017-08-09 16:50:15 -0700 | [diff] [blame] | 508 | |
| 509 | // Move the host to an invalid location |
| 510 | // Expect: Old flow is removed. New flow is not created |
| 511 | hostHandler.processHostMovedEvent(new HostEvent(HostEvent.Type.HOST_MOVED, host2, host1)); |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 512 | assertEquals(0, ROUTING_TABLE.size()); |
| 513 | assertEquals(0, BRIDGING_TABLE.size()); |
Charles Chan | abfe7e0 | 2017-08-09 16:50:15 -0700 | [diff] [blame] | 514 | |
| 515 | // Move the host to a valid location |
| 516 | // Expect: add one new routing rule, one new bridging rule |
| 517 | hostHandler.processHostMovedEvent(new HostEvent(HostEvent.Type.HOST_MOVED, host1, host2)); |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 518 | assertEquals(1, ROUTING_TABLE.size()); |
| 519 | assertNotNull(ROUTING_TABLE.get(new MockRoutingTableKey(DEV1, HOST_IP11.toIpPrefix()))); |
| 520 | assertNull(ROUTING_TABLE.get(new MockRoutingTableKey(DEV1, HOST_IP21.toIpPrefix()))); |
| 521 | assertEquals(1, BRIDGING_TABLE.size()); |
| 522 | assertNotNull(BRIDGING_TABLE.get(new MockBridgingTableKey(DEV1, HOST_MAC, INTF_VLAN_UNTAGGED))); |
| 523 | assertNull(BRIDGING_TABLE.get(new MockBridgingTableKey(DEV2, HOST_MAC, INTF_VLAN_UNTAGGED))); |
Charles Chan | abfe7e0 | 2017-08-09 16:50:15 -0700 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | @Test |
| 527 | public void testDualHomedHostMoveToInvalidLocation() throws Exception { |
| 528 | Host host1 = new DefaultHost(PROVIDER_ID, HOST_ID_UNTAGGED, HOST_MAC, HOST_VLAN_UNTAGGED, |
| 529 | Sets.newHashSet(HOST_LOC11, HOST_LOC21), Sets.newHashSet(HOST_IP11), false); |
| 530 | Host host2 = new DefaultHost(PROVIDER_ID, HOST_ID_UNTAGGED, HOST_MAC, HOST_VLAN_UNTAGGED, |
| 531 | Sets.newHashSet(HOST_LOC11, HOST_LOC51), Sets.newHashSet(HOST_IP11), false); |
| 532 | Host host3 = new DefaultHost(PROVIDER_ID, HOST_ID_UNTAGGED, HOST_MAC, HOST_VLAN_UNTAGGED, |
| 533 | Sets.newHashSet(HOST_LOC61, HOST_LOC51), Sets.newHashSet(HOST_IP11), false); |
| 534 | |
| 535 | // Add a host |
| 536 | // Expect: add two new routing rules, two new bridging rules |
| 537 | hostHandler.processHostAddedEvent(new HostEvent(HostEvent.Type.HOST_ADDED, host1)); |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 538 | assertEquals(2, ROUTING_TABLE.size()); |
| 539 | assertEquals(P1, ROUTING_TABLE.get(new MockRoutingTableKey(DEV1, HOST_IP11.toIpPrefix())).portNumber); |
| 540 | assertEquals(P1, ROUTING_TABLE.get(new MockRoutingTableKey(DEV2, HOST_IP11.toIpPrefix())).portNumber); |
| 541 | assertEquals(2, BRIDGING_TABLE.size()); |
| 542 | assertEquals(P1, BRIDGING_TABLE.get(new MockBridgingTableKey(DEV1, HOST_MAC, INTF_VLAN_UNTAGGED)).portNumber); |
| 543 | assertEquals(P1, BRIDGING_TABLE.get(new MockBridgingTableKey(DEV2, HOST_MAC, INTF_VLAN_UNTAGGED)).portNumber); |
Charles Chan | abfe7e0 | 2017-08-09 16:50:15 -0700 | [diff] [blame] | 544 | |
| 545 | // Move first host location to an invalid location |
| 546 | // Expect: One routing and one bridging flow |
| 547 | hostHandler.processHostMovedEvent(new HostEvent(HostEvent.Type.HOST_MOVED, host2, host1)); |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 548 | assertEquals(1, ROUTING_TABLE.size()); |
| 549 | assertEquals(P1, ROUTING_TABLE.get(new MockRoutingTableKey(DEV1, HOST_IP11.toIpPrefix())).portNumber); |
| 550 | assertEquals(1, BRIDGING_TABLE.size()); |
| 551 | assertEquals(P1, BRIDGING_TABLE.get(new MockBridgingTableKey(DEV1, HOST_MAC, INTF_VLAN_UNTAGGED)).portNumber); |
Charles Chan | abfe7e0 | 2017-08-09 16:50:15 -0700 | [diff] [blame] | 552 | |
| 553 | // Move second host location to an invalid location |
| 554 | // Expect: No routing or bridging rule |
| 555 | hostHandler.processHostMovedEvent(new HostEvent(HostEvent.Type.HOST_MOVED, host3, host2)); |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 556 | assertEquals(0, ROUTING_TABLE.size()); |
| 557 | assertEquals(0, BRIDGING_TABLE.size()); |
Charles Chan | abfe7e0 | 2017-08-09 16:50:15 -0700 | [diff] [blame] | 558 | |
| 559 | // Move second host location back to a valid location |
| 560 | // Expect: One routing and one bridging flow |
| 561 | hostHandler.processHostMovedEvent(new HostEvent(HostEvent.Type.HOST_MOVED, host2, host3)); |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 562 | assertEquals(1, ROUTING_TABLE.size()); |
| 563 | assertEquals(P1, ROUTING_TABLE.get(new MockRoutingTableKey(DEV1, HOST_IP11.toIpPrefix())).portNumber); |
| 564 | assertEquals(1, BRIDGING_TABLE.size()); |
| 565 | assertEquals(P1, BRIDGING_TABLE.get(new MockBridgingTableKey(DEV1, HOST_MAC, INTF_VLAN_UNTAGGED)).portNumber); |
Charles Chan | abfe7e0 | 2017-08-09 16:50:15 -0700 | [diff] [blame] | 566 | |
| 567 | // Move first host location back to a valid location |
| 568 | // Expect: Two routing and two bridging flow |
| 569 | hostHandler.processHostMovedEvent(new HostEvent(HostEvent.Type.HOST_MOVED, host1, host2)); |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 570 | assertEquals(2, ROUTING_TABLE.size()); |
| 571 | assertEquals(P1, ROUTING_TABLE.get(new MockRoutingTableKey(DEV1, HOST_IP11.toIpPrefix())).portNumber); |
| 572 | assertEquals(P1, ROUTING_TABLE.get(new MockRoutingTableKey(DEV2, HOST_IP11.toIpPrefix())).portNumber); |
| 573 | assertEquals(2, BRIDGING_TABLE.size()); |
| 574 | assertEquals(P1, BRIDGING_TABLE.get(new MockBridgingTableKey(DEV1, HOST_MAC, INTF_VLAN_UNTAGGED)).portNumber); |
| 575 | assertEquals(P1, BRIDGING_TABLE.get(new MockBridgingTableKey(DEV2, HOST_MAC, INTF_VLAN_UNTAGGED)).portNumber); |
Charles Chan | abfe7e0 | 2017-08-09 16:50:15 -0700 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | @Test |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 579 | public void testDualHomingSingleLocationFail() throws Exception { |
| 580 | Host host1 = new DefaultHost(PROVIDER_ID, HOST_ID_UNTAGGED, HOST_MAC, HOST_VLAN_UNTAGGED, |
| 581 | Sets.newHashSet(HOST_LOC31, HOST_LOC41), Sets.newHashSet(HOST_IP11, HOST_IP12), false); |
| 582 | Host host2 = new DefaultHost(PROVIDER_ID, HOST_ID_UNTAGGED, HOST_MAC, HOST_VLAN_UNTAGGED, |
| 583 | Sets.newHashSet(HOST_LOC31), Sets.newHashSet(HOST_IP11, HOST_IP12), false); |
| 584 | |
| 585 | // Add a host |
| 586 | // Expect: add four new routing rules, two new bridging rules |
| 587 | hostHandler.processHostAddedEvent(new HostEvent(HostEvent.Type.HOST_ADDED, host1)); |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 588 | assertEquals(4, ROUTING_TABLE.size()); |
| 589 | assertEquals(P1, ROUTING_TABLE.get(new MockRoutingTableKey(DEV3, HOST_IP11.toIpPrefix())).portNumber); |
| 590 | assertEquals(P1, ROUTING_TABLE.get(new MockRoutingTableKey(DEV3, HOST_IP12.toIpPrefix())).portNumber); |
| 591 | assertEquals(P1, ROUTING_TABLE.get(new MockRoutingTableKey(DEV4, HOST_IP11.toIpPrefix())).portNumber); |
| 592 | assertEquals(P1, ROUTING_TABLE.get(new MockRoutingTableKey(DEV4, HOST_IP12.toIpPrefix())).portNumber); |
| 593 | assertEquals(2, BRIDGING_TABLE.size()); |
| 594 | assertEquals(P1, BRIDGING_TABLE.get(new MockBridgingTableKey(DEV3, HOST_MAC, INTF_VLAN_UNTAGGED)).portNumber); |
| 595 | assertEquals(P1, BRIDGING_TABLE.get(new MockBridgingTableKey(DEV4, HOST_MAC, INTF_VLAN_UNTAGGED)).portNumber); |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 596 | |
| 597 | // Host becomes single-homed |
| 598 | // Expect: redirect flows from host location to pair link |
| 599 | hostHandler.processHostMovedEvent(new HostEvent(HostEvent.Type.HOST_MOVED, host2, host1)); |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 600 | assertEquals(4, ROUTING_TABLE.size()); |
| 601 | assertEquals(P1, ROUTING_TABLE.get(new MockRoutingTableKey(DEV3, HOST_IP11.toIpPrefix())).portNumber); |
| 602 | assertEquals(P1, ROUTING_TABLE.get(new MockRoutingTableKey(DEV3, HOST_IP12.toIpPrefix())).portNumber); |
| 603 | assertEquals(P9, ROUTING_TABLE.get(new MockRoutingTableKey(DEV4, HOST_IP11.toIpPrefix())).portNumber); |
| 604 | assertEquals(P9, ROUTING_TABLE.get(new MockRoutingTableKey(DEV4, HOST_IP12.toIpPrefix())).portNumber); |
| 605 | assertEquals(2, BRIDGING_TABLE.size()); |
| 606 | assertEquals(P1, BRIDGING_TABLE.get(new MockBridgingTableKey(DEV3, HOST_MAC, INTF_VLAN_UNTAGGED)).portNumber); |
| 607 | assertEquals(P9, BRIDGING_TABLE.get(new MockBridgingTableKey(DEV4, HOST_MAC, INTF_VLAN_UNTAGGED)).portNumber); |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 608 | |
| 609 | // Host becomes dual-homed again |
| 610 | // Expect: Redirect flows from pair link back to host location |
| 611 | hostHandler.processHostMovedEvent(new HostEvent(HostEvent.Type.HOST_MOVED, host1, host2)); |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 612 | assertEquals(4, ROUTING_TABLE.size()); |
| 613 | assertEquals(P1, ROUTING_TABLE.get(new MockRoutingTableKey(DEV3, HOST_IP11.toIpPrefix())).portNumber); |
| 614 | assertEquals(P1, ROUTING_TABLE.get(new MockRoutingTableKey(DEV3, HOST_IP12.toIpPrefix())).portNumber); |
| 615 | assertEquals(P1, ROUTING_TABLE.get(new MockRoutingTableKey(DEV4, HOST_IP11.toIpPrefix())).portNumber); |
| 616 | assertEquals(P1, ROUTING_TABLE.get(new MockRoutingTableKey(DEV4, HOST_IP12.toIpPrefix())).portNumber); |
| 617 | assertEquals(2, BRIDGING_TABLE.size()); |
| 618 | assertEquals(P1, BRIDGING_TABLE.get(new MockBridgingTableKey(DEV3, HOST_MAC, INTF_VLAN_UNTAGGED)).portNumber); |
| 619 | assertEquals(P1, BRIDGING_TABLE.get(new MockBridgingTableKey(DEV4, HOST_MAC, INTF_VLAN_UNTAGGED)).portNumber); |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | @Test |
| 623 | public void testDualHomingBothLocationFail() throws Exception { |
| 624 | Host host1 = new DefaultHost(PROVIDER_ID, HOST_ID_UNTAGGED, HOST_MAC, HOST_VLAN_UNTAGGED, |
| 625 | Sets.newHashSet(HOST_LOC31, HOST_LOC41), Sets.newHashSet(HOST_IP11, HOST_IP12), false); |
| 626 | Host host2 = new DefaultHost(PROVIDER_ID, HOST_ID_UNTAGGED, HOST_MAC, HOST_VLAN_UNTAGGED, |
| 627 | Sets.newHashSet(HOST_LOC31), Sets.newHashSet(HOST_IP11, HOST_IP12), false); |
| 628 | |
| 629 | // Add a host |
| 630 | // Expect: add four new routing rules, two new bridging rules |
| 631 | hostHandler.processHostAddedEvent(new HostEvent(HostEvent.Type.HOST_ADDED, host1)); |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 632 | assertEquals(4, ROUTING_TABLE.size()); |
| 633 | assertEquals(P1, ROUTING_TABLE.get(new MockRoutingTableKey(DEV3, HOST_IP11.toIpPrefix())).portNumber); |
| 634 | assertEquals(P1, ROUTING_TABLE.get(new MockRoutingTableKey(DEV3, HOST_IP12.toIpPrefix())).portNumber); |
| 635 | assertEquals(P1, ROUTING_TABLE.get(new MockRoutingTableKey(DEV4, HOST_IP11.toIpPrefix())).portNumber); |
| 636 | assertEquals(P1, ROUTING_TABLE.get(new MockRoutingTableKey(DEV4, HOST_IP12.toIpPrefix())).portNumber); |
| 637 | assertEquals(2, BRIDGING_TABLE.size()); |
| 638 | assertEquals(P1, BRIDGING_TABLE.get(new MockBridgingTableKey(DEV3, HOST_MAC, INTF_VLAN_UNTAGGED)).portNumber); |
| 639 | assertEquals(P1, BRIDGING_TABLE.get(new MockBridgingTableKey(DEV4, HOST_MAC, INTF_VLAN_UNTAGGED)).portNumber); |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 640 | |
| 641 | // Host becomes single-homed |
| 642 | // Expect: redirect flows from host location to pair link |
| 643 | hostHandler.processHostMovedEvent(new HostEvent(HostEvent.Type.HOST_MOVED, host2, host1)); |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 644 | assertEquals(4, ROUTING_TABLE.size()); |
| 645 | assertEquals(P1, ROUTING_TABLE.get(new MockRoutingTableKey(DEV3, HOST_IP11.toIpPrefix())).portNumber); |
| 646 | assertEquals(P1, ROUTING_TABLE.get(new MockRoutingTableKey(DEV3, HOST_IP12.toIpPrefix())).portNumber); |
| 647 | assertEquals(P9, ROUTING_TABLE.get(new MockRoutingTableKey(DEV4, HOST_IP11.toIpPrefix())).portNumber); |
| 648 | assertEquals(P9, ROUTING_TABLE.get(new MockRoutingTableKey(DEV4, HOST_IP12.toIpPrefix())).portNumber); |
| 649 | assertEquals(2, BRIDGING_TABLE.size()); |
| 650 | assertEquals(P1, BRIDGING_TABLE.get(new MockBridgingTableKey(DEV3, HOST_MAC, INTF_VLAN_UNTAGGED)).portNumber); |
| 651 | assertEquals(P9, BRIDGING_TABLE.get(new MockBridgingTableKey(DEV4, HOST_MAC, INTF_VLAN_UNTAGGED)).portNumber); |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 652 | |
| 653 | // Host loses both locations |
| 654 | // Expect: Remove last location and all previous redirection flows |
| 655 | hostHandler.processHostRemovedEvent(new HostEvent(HostEvent.Type.HOST_REMOVED, host2)); |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 656 | assertEquals(0, ROUTING_TABLE.size()); |
| 657 | assertEquals(0, BRIDGING_TABLE.size()); |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | @Test |
Charles Chan | 114aec7 | 2017-06-19 14:00:53 -0700 | [diff] [blame] | 661 | public void testHostUpdated() throws Exception { |
| 662 | Host host1 = new DefaultHost(PROVIDER_ID, HOST_ID_UNTAGGED, HOST_MAC, HOST_VLAN_UNTAGGED, |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 663 | Sets.newHashSet(HOST_LOC11), Sets.newHashSet(HOST_IP11), false); |
Charles Chan | 114aec7 | 2017-06-19 14:00:53 -0700 | [diff] [blame] | 664 | Host host2 = new DefaultHost(PROVIDER_ID, HOST_ID_UNTAGGED, HOST_MAC, HOST_VLAN_UNTAGGED, |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 665 | Sets.newHashSet(HOST_LOC11), Sets.newHashSet(HOST_IP21), false); |
Charles Chan | 114aec7 | 2017-06-19 14:00:53 -0700 | [diff] [blame] | 666 | Host host3 = new DefaultHost(PROVIDER_ID, HOST_ID_UNTAGGED, HOST_MAC, HOST_VLAN_UNTAGGED, |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 667 | Sets.newHashSet(HOST_LOC11), Sets.newHashSet(HOST_IP12), false); |
Charles Chan | 114aec7 | 2017-06-19 14:00:53 -0700 | [diff] [blame] | 668 | |
| 669 | // Add a host |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 670 | // Expect: add one new routing rule. Add one new bridging rule. |
Charles Chan | 114aec7 | 2017-06-19 14:00:53 -0700 | [diff] [blame] | 671 | hostHandler.processHostAddedEvent(new HostEvent(HostEvent.Type.HOST_ADDED, host1)); |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 672 | assertEquals(1, ROUTING_TABLE.size()); |
| 673 | assertNotNull(ROUTING_TABLE.get(new MockRoutingTableKey(DEV1, HOST_IP11.toIpPrefix()))); |
| 674 | assertNull(ROUTING_TABLE.get(new MockRoutingTableKey(DEV1, HOST_IP21.toIpPrefix()))); |
| 675 | assertNull(ROUTING_TABLE.get(new MockRoutingTableKey(DEV1, HOST_IP12.toIpPrefix()))); |
| 676 | assertEquals(1, BRIDGING_TABLE.size()); |
| 677 | assertNotNull(BRIDGING_TABLE.get(new MockBridgingTableKey(HOST_LOC11.deviceId(), HOST_MAC, |
| 678 | INTF_VLAN_UNTAGGED))); |
Charles Chan | 114aec7 | 2017-06-19 14:00:53 -0700 | [diff] [blame] | 679 | |
| 680 | // Update the host IP to same subnet |
| 681 | // Expect: update routing rule with new IP. No change to bridging rule. |
| 682 | hostHandler.processHostUpdatedEvent(new HostEvent(HostEvent.Type.HOST_UPDATED, host3, host1)); |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 683 | assertEquals(1, ROUTING_TABLE.size()); |
| 684 | assertNull(ROUTING_TABLE.get(new MockRoutingTableKey(DEV1, HOST_IP11.toIpPrefix()))); |
| 685 | assertNull(ROUTING_TABLE.get(new MockRoutingTableKey(DEV1, HOST_IP21.toIpPrefix()))); |
| 686 | assertNotNull(ROUTING_TABLE.get(new MockRoutingTableKey(DEV1, HOST_IP12.toIpPrefix()))); |
| 687 | assertEquals(1, BRIDGING_TABLE.size()); |
| 688 | assertNotNull(BRIDGING_TABLE.get(new MockBridgingTableKey(DEV1, HOST_MAC, INTF_VLAN_UNTAGGED))); |
Charles Chan | 114aec7 | 2017-06-19 14:00:53 -0700 | [diff] [blame] | 689 | |
| 690 | // Update the host IP to different subnet |
| 691 | // Expect: Remove routing rule. No change to bridging rule. |
| 692 | hostHandler.processHostUpdatedEvent(new HostEvent(HostEvent.Type.HOST_UPDATED, host2, host3)); |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 693 | assertEquals(0, ROUTING_TABLE.size()); |
| 694 | assertEquals(1, BRIDGING_TABLE.size()); |
| 695 | assertNotNull(BRIDGING_TABLE.get(new MockBridgingTableKey(DEV1, HOST_MAC, INTF_VLAN_UNTAGGED))); |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | @Test |
Charles Chan | b3c1faf | 2017-11-20 08:46:24 -0800 | [diff] [blame^] | 699 | public void testDelayedIpAndLocation() throws Exception { |
| 700 | Host host1 = new DefaultHost(PROVIDER_ID, HOST_ID_UNTAGGED, HOST_MAC, HOST_VLAN_UNTAGGED, |
| 701 | Sets.newHashSet(HOST_LOC31), Sets.newHashSet(), false); |
| 702 | Host host2 = new DefaultHost(PROVIDER_ID, HOST_ID_UNTAGGED, HOST_MAC, HOST_VLAN_UNTAGGED, |
| 703 | Sets.newHashSet(HOST_LOC31), Sets.newHashSet(HOST_IP11), false); |
| 704 | Host host3 = new DefaultHost(PROVIDER_ID, HOST_ID_UNTAGGED, HOST_MAC, HOST_VLAN_UNTAGGED, |
| 705 | Sets.newHashSet(HOST_LOC31, HOST_LOC41), Sets.newHashSet(HOST_IP11), false); |
| 706 | |
| 707 | // Add a dual-home host with only one location and no IP |
| 708 | // Expect: only bridging redirection works |
| 709 | hostHandler.processHostAddedEvent(new HostEvent(HostEvent.Type.HOST_ADDED, host1)); |
| 710 | assertEquals(0, ROUTING_TABLE.size()); |
| 711 | assertEquals(2, BRIDGING_TABLE.size()); |
| 712 | assertEquals(P1, BRIDGING_TABLE.get(new MockBridgingTableKey(DEV3, HOST_MAC, INTF_VLAN_UNTAGGED)).portNumber); |
| 713 | assertEquals(P9, BRIDGING_TABLE.get(new MockBridgingTableKey(DEV4, HOST_MAC, INTF_VLAN_UNTAGGED)).portNumber); |
| 714 | |
| 715 | // Discover IP |
| 716 | // Expect: routing redirection should also work |
| 717 | hostHandler.processHostAddedEvent(new HostEvent(HostEvent.Type.HOST_UPDATED, host2, host1)); |
| 718 | assertEquals(2, ROUTING_TABLE.size()); |
| 719 | assertEquals(P1, ROUTING_TABLE.get(new MockRoutingTableKey(DEV3, HOST_IP11.toIpPrefix())).portNumber); |
| 720 | assertEquals(P9, ROUTING_TABLE.get(new MockRoutingTableKey(DEV4, HOST_IP11.toIpPrefix())).portNumber); |
| 721 | assertEquals(2, BRIDGING_TABLE.size()); |
| 722 | assertEquals(P1, BRIDGING_TABLE.get(new MockBridgingTableKey(DEV3, HOST_MAC, INTF_VLAN_UNTAGGED)).portNumber); |
| 723 | assertEquals(P9, BRIDGING_TABLE.get(new MockBridgingTableKey(DEV4, HOST_MAC, INTF_VLAN_UNTAGGED)).portNumber); |
| 724 | |
| 725 | // Discover location |
| 726 | // Expect: cancel all redirections |
| 727 | hostHandler.processHostAddedEvent(new HostEvent(HostEvent.Type.HOST_MOVED, host3, host2)); |
| 728 | assertEquals(2, ROUTING_TABLE.size()); |
| 729 | assertEquals(P1, ROUTING_TABLE.get(new MockRoutingTableKey(DEV3, HOST_IP11.toIpPrefix())).portNumber); |
| 730 | assertEquals(P1, ROUTING_TABLE.get(new MockRoutingTableKey(DEV4, HOST_IP11.toIpPrefix())).portNumber); |
| 731 | assertEquals(2, BRIDGING_TABLE.size()); |
| 732 | assertEquals(P1, BRIDGING_TABLE.get(new MockBridgingTableKey(DEV3, HOST_MAC, INTF_VLAN_UNTAGGED)).portNumber); |
| 733 | assertEquals(P1, BRIDGING_TABLE.get(new MockBridgingTableKey(DEV4, HOST_MAC, INTF_VLAN_UNTAGGED)).portNumber); |
| 734 | } |
| 735 | |
| 736 | @Test |
| 737 | public void testDelayedIpAndLocation2() throws Exception { |
| 738 | Host host1 = new DefaultHost(PROVIDER_ID, HOST_ID_UNTAGGED, HOST_MAC, HOST_VLAN_UNTAGGED, |
| 739 | Sets.newHashSet(HOST_LOC31), Sets.newHashSet(), false); |
| 740 | Host host2 = new DefaultHost(PROVIDER_ID, HOST_ID_UNTAGGED, HOST_MAC, HOST_VLAN_UNTAGGED, |
| 741 | Sets.newHashSet(HOST_LOC31, HOST_LOC41), Sets.newHashSet(), false); |
| 742 | Host host3 = new DefaultHost(PROVIDER_ID, HOST_ID_UNTAGGED, HOST_MAC, HOST_VLAN_UNTAGGED, |
| 743 | Sets.newHashSet(HOST_LOC31, HOST_LOC41), Sets.newHashSet(HOST_IP11), false); |
| 744 | |
| 745 | // Add a dual-home host with only one location and no IP |
| 746 | // Expect: only bridging redirection works |
| 747 | hostHandler.processHostAddedEvent(new HostEvent(HostEvent.Type.HOST_ADDED, host1)); |
| 748 | assertEquals(0, ROUTING_TABLE.size()); |
| 749 | assertEquals(2, BRIDGING_TABLE.size()); |
| 750 | assertEquals(P1, BRIDGING_TABLE.get(new MockBridgingTableKey(DEV3, HOST_MAC, INTF_VLAN_UNTAGGED)).portNumber); |
| 751 | assertEquals(P9, BRIDGING_TABLE.get(new MockBridgingTableKey(DEV4, HOST_MAC, INTF_VLAN_UNTAGGED)).portNumber); |
| 752 | |
| 753 | // Discover Location |
| 754 | // Expect: cancel bridging redirections |
| 755 | hostHandler.processHostAddedEvent(new HostEvent(HostEvent.Type.HOST_MOVED, host2, host1)); |
| 756 | assertEquals(0, ROUTING_TABLE.size()); |
| 757 | assertEquals(2, BRIDGING_TABLE.size()); |
| 758 | assertEquals(P1, BRIDGING_TABLE.get(new MockBridgingTableKey(DEV3, HOST_MAC, INTF_VLAN_UNTAGGED)).portNumber); |
| 759 | assertEquals(P1, BRIDGING_TABLE.get(new MockBridgingTableKey(DEV4, HOST_MAC, INTF_VLAN_UNTAGGED)).portNumber); |
| 760 | |
| 761 | // Discover IP |
| 762 | // Expect: add IP rules to both location |
| 763 | hostHandler.processHostAddedEvent(new HostEvent(HostEvent.Type.HOST_UPDATED, host3, host2)); |
| 764 | assertEquals(2, ROUTING_TABLE.size()); |
| 765 | assertEquals(P1, ROUTING_TABLE.get(new MockRoutingTableKey(DEV3, HOST_IP11.toIpPrefix())).portNumber); |
| 766 | assertEquals(P1, ROUTING_TABLE.get(new MockRoutingTableKey(DEV4, HOST_IP11.toIpPrefix())).portNumber); |
| 767 | assertEquals(2, BRIDGING_TABLE.size()); |
| 768 | assertEquals(P1, BRIDGING_TABLE.get(new MockBridgingTableKey(DEV3, HOST_MAC, INTF_VLAN_UNTAGGED)).portNumber); |
| 769 | assertEquals(P1, BRIDGING_TABLE.get(new MockBridgingTableKey(DEV4, HOST_MAC, INTF_VLAN_UNTAGGED)).portNumber); |
| 770 | } |
| 771 | |
| 772 | @Test |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 773 | public void testDualHomedHostUpdated() throws Exception { |
| 774 | Host host1 = new DefaultHost(PROVIDER_ID, HOST_ID_UNTAGGED, HOST_MAC, HOST_VLAN_UNTAGGED, |
| 775 | Sets.newHashSet(HOST_LOC11, HOST_LOC21), Sets.newHashSet(HOST_IP11, HOST_IP12), false); |
| 776 | Host host2 = new DefaultHost(PROVIDER_ID, HOST_ID_UNTAGGED, HOST_MAC, HOST_VLAN_UNTAGGED, |
| 777 | Sets.newHashSet(HOST_LOC11, HOST_LOC21), Sets.newHashSet(HOST_IP11, HOST_IP21), false); |
| 778 | Host host3 = new DefaultHost(PROVIDER_ID, HOST_ID_UNTAGGED, HOST_MAC, HOST_VLAN_UNTAGGED, |
| 779 | Sets.newHashSet(HOST_LOC11, HOST_LOC21), Sets.newHashSet(HOST_IP13, HOST_IP14), false); |
| 780 | |
| 781 | // Add a dual-homed host with two locations and two IPs |
| 782 | // Expect: add four new routing rules. Add two new bridging rules |
| 783 | hostHandler.processHostAddedEvent(new HostEvent(HostEvent.Type.HOST_ADDED, host1)); |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 784 | assertEquals(4, ROUTING_TABLE.size()); |
| 785 | assertNotNull(ROUTING_TABLE.get(new MockRoutingTableKey(DEV1, HOST_IP11.toIpPrefix()))); |
| 786 | assertNotNull(ROUTING_TABLE.get(new MockRoutingTableKey(DEV1, HOST_IP12.toIpPrefix()))); |
| 787 | assertNotNull(ROUTING_TABLE.get(new MockRoutingTableKey(DEV2, HOST_IP11.toIpPrefix()))); |
| 788 | assertNotNull(ROUTING_TABLE.get(new MockRoutingTableKey(DEV2, HOST_IP12.toIpPrefix()))); |
| 789 | assertEquals(2, BRIDGING_TABLE.size()); |
| 790 | assertNotNull(BRIDGING_TABLE.get(new MockBridgingTableKey(DEV1, HOST_MAC, INTF_VLAN_UNTAGGED))); |
| 791 | assertNotNull(BRIDGING_TABLE.get(new MockBridgingTableKey(DEV2, HOST_MAC, INTF_VLAN_UNTAGGED))); |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 792 | |
| 793 | // Update both host IPs |
| 794 | // Expect: update routing rules with new IP. No change to bridging rule. |
| 795 | hostHandler.processHostUpdatedEvent(new HostEvent(HostEvent.Type.HOST_UPDATED, host3, host1)); |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 796 | assertEquals(4, ROUTING_TABLE.size()); |
| 797 | assertNull(ROUTING_TABLE.get(new MockRoutingTableKey(DEV1, HOST_IP11.toIpPrefix()))); |
| 798 | assertNull(ROUTING_TABLE.get(new MockRoutingTableKey(DEV1, HOST_IP12.toIpPrefix()))); |
| 799 | assertNotNull(ROUTING_TABLE.get(new MockRoutingTableKey(DEV1, HOST_IP13.toIpPrefix()))); |
| 800 | assertNotNull(ROUTING_TABLE.get(new MockRoutingTableKey(DEV1, HOST_IP14.toIpPrefix()))); |
| 801 | assertNull(ROUTING_TABLE.get(new MockRoutingTableKey(DEV2, HOST_IP11.toIpPrefix()))); |
| 802 | assertNull(ROUTING_TABLE.get(new MockRoutingTableKey(DEV2, HOST_IP12.toIpPrefix()))); |
| 803 | assertNotNull(ROUTING_TABLE.get(new MockRoutingTableKey(DEV2, HOST_IP13.toIpPrefix()))); |
| 804 | assertNotNull(ROUTING_TABLE.get(new MockRoutingTableKey(DEV2, HOST_IP14.toIpPrefix()))); |
| 805 | assertEquals(2, BRIDGING_TABLE.size()); |
| 806 | assertNotNull(BRIDGING_TABLE.get(new MockBridgingTableKey(DEV1, HOST_MAC, INTF_VLAN_UNTAGGED))); |
| 807 | assertNotNull(BRIDGING_TABLE.get(new MockBridgingTableKey(DEV2, HOST_MAC, INTF_VLAN_UNTAGGED))); |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 808 | |
| 809 | // Update one of the host IP to different subnet |
| 810 | // Expect: update routing rule with new IP. No change to bridging rule. |
| 811 | hostHandler.processHostUpdatedEvent(new HostEvent(HostEvent.Type.HOST_UPDATED, host2, host3)); |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 812 | assertEquals(2, ROUTING_TABLE.size()); |
| 813 | assertNotNull(ROUTING_TABLE.get(new MockRoutingTableKey(DEV1, HOST_IP11.toIpPrefix()))); |
| 814 | assertNull(ROUTING_TABLE.get(new MockRoutingTableKey(DEV1, HOST_IP21.toIpPrefix()))); |
| 815 | assertNull(ROUTING_TABLE.get(new MockRoutingTableKey(DEV1, HOST_IP12.toIpPrefix()))); |
| 816 | assertNotNull(ROUTING_TABLE.get(new MockRoutingTableKey(DEV2, HOST_IP11.toIpPrefix()))); |
| 817 | assertNull(ROUTING_TABLE.get(new MockRoutingTableKey(DEV2, HOST_IP21.toIpPrefix()))); |
| 818 | assertNull(ROUTING_TABLE.get(new MockRoutingTableKey(DEV2, HOST_IP12.toIpPrefix()))); |
| 819 | assertEquals(2, BRIDGING_TABLE.size()); |
| 820 | assertNotNull(BRIDGING_TABLE.get(new MockBridgingTableKey(DEV1, HOST_MAC, INTF_VLAN_UNTAGGED))); |
| 821 | assertNotNull(BRIDGING_TABLE.get(new MockBridgingTableKey(DEV2, HOST_MAC, INTF_VLAN_UNTAGGED))); |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | @Test |
| 825 | public void testBridgingFwdObjBuilder() throws Exception { |
| 826 | assertNotNull(hostHandler.bridgingFwdObjBuilder(DEV2, HOST_MAC, HOST_VLAN_UNTAGGED, P1, false)); |
| 827 | assertNull(hostHandler.bridgingFwdObjBuilder(DEV2, HOST_MAC, HOST_VLAN_UNTAGGED, P3, false)); |
Charles Chan | 114aec7 | 2017-06-19 14:00:53 -0700 | [diff] [blame] | 828 | } |
Ray Milkey | c2d43be | 2017-08-03 11:58:29 -0700 | [diff] [blame] | 829 | } |