Charles Chan | 1a3b02a | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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 | 2fde6d4 | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.JsonNode; |
| 20 | import com.fasterxml.jackson.databind.ObjectMapper; |
Charles Chan | 1a3b02a | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 21 | import com.google.common.collect.Maps; |
| 22 | import com.google.common.collect.Sets; |
| 23 | import org.junit.Before; |
| 24 | import org.junit.Test; |
| 25 | import org.onlab.packet.IpAddress; |
| 26 | import org.onlab.packet.IpPrefix; |
| 27 | import org.onlab.packet.MacAddress; |
| 28 | import org.onlab.packet.VlanId; |
Charles Chan | 2fde6d4 | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 29 | import org.onosproject.net.config.ConfigApplyDelegate; |
Charles Chan | 1a3b02a | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 30 | import org.onosproject.net.ConnectPoint; |
Charles Chan | 9640c81 | 2017-08-23 13:55:39 -0700 | [diff] [blame] | 31 | import org.onosproject.net.DefaultHost; |
Charles Chan | 1a3b02a | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 32 | import org.onosproject.net.DeviceId; |
| 33 | import org.onosproject.net.Host; |
Charles Chan | 9640c81 | 2017-08-23 13:55:39 -0700 | [diff] [blame] | 34 | import org.onosproject.net.HostId; |
| 35 | import org.onosproject.net.HostLocation; |
Charles Chan | 2fde6d4 | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 36 | import org.onosproject.net.PortNumber; |
Charles Chan | 1a3b02a | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 37 | import org.onosproject.net.config.NetworkConfigRegistryAdapter; |
| 38 | import org.onosproject.net.flow.TrafficTreatment; |
Charles Chan | 2fde6d4 | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 39 | import org.onosproject.net.host.HostEvent; |
| 40 | import org.onosproject.net.host.HostService; |
Charles Chan | 1a3b02a | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 41 | import org.onosproject.net.intf.Interface; |
Charles Chan | 9640c81 | 2017-08-23 13:55:39 -0700 | [diff] [blame] | 42 | import org.onosproject.net.provider.ProviderId; |
Charles Chan | 1a3b02a | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 43 | import org.onosproject.routeservice.ResolvedRoute; |
| 44 | import org.onosproject.routeservice.Route; |
| 45 | import org.onosproject.routeservice.RouteEvent; |
Charles Chan | 1a3b02a | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 46 | import org.onosproject.segmentrouting.config.DeviceConfiguration; |
Charles Chan | 2fde6d4 | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 47 | import org.onosproject.segmentrouting.config.SegmentRoutingDeviceConfig; |
Charles Chan | 2ff1bac | 2018-03-29 16:03:41 -0700 | [diff] [blame^] | 48 | import org.onosproject.store.service.StorageService; |
| 49 | import org.onosproject.store.service.TestConsistentMap; |
Charles Chan | 1a3b02a | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 50 | |
| 51 | import java.util.Map; |
| 52 | import java.util.Set; |
| 53 | |
Charles Chan | 2ff1bac | 2018-03-29 16:03:41 -0700 | [diff] [blame^] | 54 | import static org.easymock.EasyMock.expect; |
Charles Chan | 08d9132 | 2018-02-05 17:20:05 -0800 | [diff] [blame] | 55 | import static org.easymock.EasyMock.reset; |
Charles Chan | 1a3b02a | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 56 | import static org.junit.Assert.*; |
Charles Chan | 08d9132 | 2018-02-05 17:20:05 -0800 | [diff] [blame] | 57 | import static org.easymock.EasyMock.createMock; |
| 58 | import static org.easymock.EasyMock.expectLastCall; |
| 59 | import static org.easymock.EasyMock.replay; |
| 60 | import static org.easymock.EasyMock.verify; |
Charles Chan | 1a3b02a | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 61 | |
| 62 | /** |
| 63 | * Unit test for {@link RouteHandler}. |
| 64 | */ |
| 65 | public class RouteHandlerTest { |
Charles Chan | 08d9132 | 2018-02-05 17:20:05 -0800 | [diff] [blame] | 66 | private SegmentRoutingManager srManager; |
Charles Chan | 1a3b02a | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 67 | private RouteHandler routeHandler; |
Charles Chan | 2fde6d4 | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 68 | private HostService hostService; |
Charles Chan | 1a3b02a | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 69 | |
| 70 | // Mocked routing and bridging tables |
| 71 | private static final Map<MockBridgingTableKey, MockBridgingTableValue> BRIDGING_TABLE = |
| 72 | Maps.newConcurrentMap(); |
| 73 | private static final Map<MockRoutingTableKey, MockRoutingTableValue> ROUTING_TABLE = |
| 74 | Maps.newConcurrentMap(); |
| 75 | private static final Map<ConnectPoint, Set<IpPrefix>> SUBNET_TABLE = Maps.newConcurrentMap(); |
| 76 | // Mocked Next Id |
| 77 | private static final Map<Integer, TrafficTreatment> NEXT_TABLE = Maps.newConcurrentMap(); |
| 78 | |
| 79 | private static final IpPrefix P1 = IpPrefix.valueOf("10.0.0.0/24"); |
Charles Chan | 2fde6d4 | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 80 | |
| 81 | // Single homed router 1 |
Charles Chan | 1a3b02a | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 82 | private static final IpAddress N1 = IpAddress.valueOf("10.0.1.254"); |
| 83 | private static final MacAddress M1 = MacAddress.valueOf("00:00:00:00:00:01"); |
| 84 | private static final VlanId V1 = VlanId.vlanId((short) 1); |
| 85 | private static final ConnectPoint CP1 = ConnectPoint.deviceConnectPoint("of:0000000000000001/1"); |
| 86 | private static final Route R1 = new Route(Route.Source.STATIC, P1, N1); |
Charles Chan | 9640c81 | 2017-08-23 13:55:39 -0700 | [diff] [blame] | 87 | private static final ResolvedRoute RR1 = new ResolvedRoute(R1, M1, V1); |
Charles Chan | 1a3b02a | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 88 | |
Charles Chan | 2fde6d4 | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 89 | // Single homed router 2 |
Charles Chan | 1a3b02a | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 90 | private static final IpAddress N2 = IpAddress.valueOf("10.0.2.254"); |
| 91 | private static final MacAddress M2 = MacAddress.valueOf("00:00:00:00:00:02"); |
| 92 | private static final VlanId V2 = VlanId.vlanId((short) 2); |
Charles Chan | 2fde6d4 | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 93 | private static final ConnectPoint CP2 = ConnectPoint.deviceConnectPoint("of:0000000000000002/2"); |
Charles Chan | 1a3b02a | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 94 | private static final Route R2 = new Route(Route.Source.STATIC, P1, N2); |
Charles Chan | 9640c81 | 2017-08-23 13:55:39 -0700 | [diff] [blame] | 95 | private static final ResolvedRoute RR2 = new ResolvedRoute(R2, M2, V2); |
Charles Chan | 1a3b02a | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 96 | |
Charles Chan | 2fde6d4 | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 97 | // Dual homed router 1 |
| 98 | private static final IpAddress N3 = IpAddress.valueOf("10.0.3.254"); |
| 99 | private static final MacAddress M3 = MacAddress.valueOf("00:00:00:00:00:03"); |
| 100 | private static final VlanId V3 = VlanId.vlanId((short) 3); |
| 101 | private static final Route R3 = new Route(Route.Source.STATIC, P1, N3); |
| 102 | private static final ResolvedRoute RR3 = new ResolvedRoute(R3, M3, V3); |
Charles Chan | 1a3b02a | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 103 | |
Charles Chan | 2fde6d4 | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 104 | // Hosts |
Charles Chan | 9640c81 | 2017-08-23 13:55:39 -0700 | [diff] [blame] | 105 | private static final Host H1 = new DefaultHost(ProviderId.NONE, HostId.hostId(M1, V1), M1, V1, |
| 106 | Sets.newHashSet(new HostLocation(CP1, 0)), Sets.newHashSet(N1), false); |
| 107 | private static final Host H2 = new DefaultHost(ProviderId.NONE, HostId.hostId(M2, V2), M2, V2, |
| 108 | Sets.newHashSet(new HostLocation(CP2, 0)), Sets.newHashSet(N2), false); |
Charles Chan | 2fde6d4 | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 109 | private static final Host H3D = new DefaultHost(ProviderId.NONE, HostId.hostId(M3, V3), M3, V3, |
| 110 | Sets.newHashSet(new HostLocation(CP1, 0), new HostLocation(CP2, 0)), Sets.newHashSet(N3), false); |
| 111 | private static final Host H3S = new DefaultHost(ProviderId.NONE, HostId.hostId(M3, V3), M3, V3, |
| 112 | Sets.newHashSet(new HostLocation(CP1, 0)), Sets.newHashSet(N3), false); |
| 113 | |
| 114 | // Pair Local Port |
| 115 | private static final PortNumber P9 = PortNumber.portNumber(9); |
Charles Chan | 9640c81 | 2017-08-23 13:55:39 -0700 | [diff] [blame] | 116 | |
Charles Chan | 1a3b02a | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 117 | // A set of hosts |
Charles Chan | 2fde6d4 | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 118 | private static final Set<Host> HOSTS = Sets.newHashSet(H1, H2, H3D); |
| 119 | private static final Set<Host> HOSTS_ONE_FAIL = Sets.newHashSet(H1, H2, H3S); |
| 120 | private static final Set<Host> HOSTS_BOTH_FAIL = Sets.newHashSet(H1, H2); |
Charles Chan | 1a3b02a | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 121 | // A set of devices of which we have mastership |
Charles Chan | 2fde6d4 | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 122 | private static final Set<DeviceId> LOCAL_DEVICES = Sets.newHashSet(CP1.deviceId(), CP2.deviceId()); |
Charles Chan | 1a3b02a | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 123 | // A set of interfaces |
| 124 | private static final Set<Interface> INTERFACES = Sets.newHashSet(); |
Charles Chan | 1a3b02a | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 125 | |
| 126 | @Before |
| 127 | public void setUp() throws Exception { |
Charles Chan | 2fde6d4 | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 128 | ObjectMapper mapper = new ObjectMapper(); |
| 129 | ConfigApplyDelegate delegate = config -> { }; |
| 130 | |
| 131 | SegmentRoutingDeviceConfig dev1Config = new SegmentRoutingDeviceConfig(); |
| 132 | JsonNode dev1Tree = mapper.createObjectNode(); |
| 133 | dev1Config.init(CP1.deviceId(), "host-handler-test", dev1Tree, mapper, delegate); |
| 134 | dev1Config.setPairDeviceId(CP2.deviceId()).setPairLocalPort(P9); |
| 135 | |
| 136 | SegmentRoutingDeviceConfig dev2Config = new SegmentRoutingDeviceConfig(); |
| 137 | JsonNode dev2Tree = mapper.createObjectNode(); |
| 138 | dev2Config.init(CP2.deviceId(), "host-handler-test", dev2Tree, mapper, delegate); |
| 139 | dev2Config.setPairDeviceId(CP1.deviceId()).setPairLocalPort(P9); |
Charles Chan | 1a3b02a | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 140 | |
| 141 | MockNetworkConfigRegistry mockNetworkConfigRegistry = new MockNetworkConfigRegistry(); |
Charles Chan | 2fde6d4 | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 142 | mockNetworkConfigRegistry.applyConfig(dev1Config); |
| 143 | mockNetworkConfigRegistry.applyConfig(dev2Config); |
Charles Chan | 1a3b02a | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 144 | |
| 145 | // Initialize Segment Routing Manager |
Charles Chan | 08d9132 | 2018-02-05 17:20:05 -0800 | [diff] [blame] | 146 | srManager = new MockSegmentRoutingManager(NEXT_TABLE); |
Charles Chan | 2ff1bac | 2018-03-29 16:03:41 -0700 | [diff] [blame^] | 147 | srManager.storageService = createMock(StorageService.class); |
| 148 | expect(srManager.storageService.consistentMapBuilder()).andReturn(new TestConsistentMap.Builder<>()).anyTimes(); |
| 149 | replay(srManager.storageService); |
Charles Chan | 1a3b02a | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 150 | srManager.cfgService = new NetworkConfigRegistryAdapter(); |
Charles Chan | 08d9132 | 2018-02-05 17:20:05 -0800 | [diff] [blame] | 151 | srManager.deviceConfiguration = createMock(DeviceConfiguration.class); |
Charles Chan | 1a3b02a | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 152 | srManager.flowObjectiveService = new MockFlowObjectiveService(BRIDGING_TABLE, NEXT_TABLE); |
| 153 | srManager.routingRulePopulator = new MockRoutingRulePopulator(srManager, ROUTING_TABLE); |
Charles Chan | f433f95 | 2018-03-15 16:41:10 -0700 | [diff] [blame] | 154 | srManager.defaultRoutingHandler = new MockDefaultRoutingHandler(srManager, SUBNET_TABLE, ROUTING_TABLE); |
Charles Chan | 1a3b02a | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 155 | srManager.interfaceService = new MockInterfaceService(INTERFACES); |
| 156 | srManager.mastershipService = new MockMastershipService(LOCAL_DEVICES); |
Charles Chan | 2fde6d4 | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 157 | hostService = new MockHostService(HOSTS); |
| 158 | srManager.hostService = hostService; |
Charles Chan | 1a3b02a | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 159 | srManager.cfgService = mockNetworkConfigRegistry; |
Charles Chan | 2fde6d4 | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 160 | srManager.routeService = new MockRouteService(ROUTING_TABLE); |
Charles Chan | 1a3b02a | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 161 | |
Charles Chan | 2fde6d4 | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 162 | routeHandler = new RouteHandler(srManager) { |
| 163 | // routeEventCache is not necessary for unit tests |
| 164 | @Override |
| 165 | void enqueueRouteEvent(RouteEvent routeEvent) { |
| 166 | dequeueRouteEvent(routeEvent); |
| 167 | } |
| 168 | }; |
Charles Chan | 1a3b02a | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 169 | |
| 170 | ROUTING_TABLE.clear(); |
| 171 | BRIDGING_TABLE.clear(); |
| 172 | SUBNET_TABLE.clear(); |
| 173 | } |
| 174 | |
| 175 | @Test |
| 176 | public void init() throws Exception { |
Charles Chan | 2fde6d4 | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 177 | MockRoutingTableKey rtk = new MockRoutingTableKey(CP1.deviceId(), P1); |
| 178 | MockRoutingTableValue rtv = new MockRoutingTableValue(CP1.port(), M1, V1); |
| 179 | ROUTING_TABLE.put(rtk, rtv); |
| 180 | |
Charles Chan | 1a3b02a | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 181 | routeHandler.init(CP1.deviceId()); |
| 182 | |
| 183 | assertEquals(1, ROUTING_TABLE.size()); |
Charles Chan | 9640c81 | 2017-08-23 13:55:39 -0700 | [diff] [blame] | 184 | MockRoutingTableValue rtv1 = ROUTING_TABLE.get(new MockRoutingTableKey(CP1.deviceId(), P1)); |
| 185 | assertEquals(M1, rtv1.macAddress); |
| 186 | assertEquals(V1, rtv1.vlanId); |
| 187 | assertEquals(CP1.port(), rtv1.portNumber); |
| 188 | |
Charles Chan | 1a3b02a | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 189 | assertEquals(1, SUBNET_TABLE.size()); |
| 190 | assertTrue(SUBNET_TABLE.get(CP1).contains(P1)); |
| 191 | } |
| 192 | |
| 193 | @Test |
| 194 | public void processRouteAdded() throws Exception { |
Charles Chan | 08d9132 | 2018-02-05 17:20:05 -0800 | [diff] [blame] | 195 | reset(srManager.deviceConfiguration); |
| 196 | srManager.deviceConfiguration.addSubnet(CP1, P1); |
| 197 | expectLastCall().once(); |
| 198 | replay(srManager.deviceConfiguration); |
| 199 | |
Charles Chan | 2fde6d4 | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 200 | RouteEvent re = new RouteEvent(RouteEvent.Type.ROUTE_ADDED, RR1, Sets.newHashSet(RR1)); |
Charles Chan | 1a3b02a | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 201 | routeHandler.processRouteAdded(re); |
| 202 | |
| 203 | assertEquals(1, ROUTING_TABLE.size()); |
Charles Chan | 9640c81 | 2017-08-23 13:55:39 -0700 | [diff] [blame] | 204 | MockRoutingTableValue rtv1 = ROUTING_TABLE.get(new MockRoutingTableKey(CP1.deviceId(), P1)); |
| 205 | assertEquals(M1, rtv1.macAddress); |
| 206 | assertEquals(V1, rtv1.vlanId); |
| 207 | assertEquals(CP1.port(), rtv1.portNumber); |
| 208 | |
Charles Chan | 1a3b02a | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 209 | assertEquals(1, SUBNET_TABLE.size()); |
| 210 | assertTrue(SUBNET_TABLE.get(CP1).contains(P1)); |
Charles Chan | 08d9132 | 2018-02-05 17:20:05 -0800 | [diff] [blame] | 211 | |
| 212 | verify(srManager.deviceConfiguration); |
Charles Chan | 1a3b02a | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | @Test |
| 216 | public void processRouteUpdated() throws Exception { |
| 217 | processRouteAdded(); |
| 218 | |
Charles Chan | 08d9132 | 2018-02-05 17:20:05 -0800 | [diff] [blame] | 219 | reset(srManager.deviceConfiguration); |
| 220 | srManager.deviceConfiguration.removeSubnet(CP1, P1); |
| 221 | expectLastCall().once(); |
| 222 | srManager.deviceConfiguration.addSubnet(CP2, P1); |
| 223 | expectLastCall().once(); |
| 224 | replay(srManager.deviceConfiguration); |
| 225 | |
Charles Chan | 2fde6d4 | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 226 | RouteEvent re = new RouteEvent(RouteEvent.Type.ROUTE_UPDATED, RR2, RR1, Sets.newHashSet(RR2), |
| 227 | Sets.newHashSet(RR1)); |
Charles Chan | 1a3b02a | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 228 | routeHandler.processRouteUpdated(re); |
| 229 | |
Charles Chan | 08d9132 | 2018-02-05 17:20:05 -0800 | [diff] [blame] | 230 | // Note: We shouldn't remove the old nexthop during the occasion of route update |
| 231 | // since the populate subnet will take care of it and point it to an ECMP group |
| 232 | assertEquals(2, ROUTING_TABLE.size()); |
Charles Chan | 2fde6d4 | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 233 | MockRoutingTableValue rtv2 = ROUTING_TABLE.get(new MockRoutingTableKey(CP2.deviceId(), P1)); |
Charles Chan | 9640c81 | 2017-08-23 13:55:39 -0700 | [diff] [blame] | 234 | assertEquals(M2, rtv2.macAddress); |
| 235 | assertEquals(V2, rtv2.vlanId); |
| 236 | assertEquals(CP2.port(), rtv2.portNumber); |
| 237 | |
Charles Chan | 1a3b02a | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 238 | assertEquals(1, SUBNET_TABLE.size()); |
| 239 | assertTrue(SUBNET_TABLE.get(CP2).contains(P1)); |
Charles Chan | 08d9132 | 2018-02-05 17:20:05 -0800 | [diff] [blame] | 240 | |
| 241 | verify(srManager.deviceConfiguration); |
Charles Chan | 1a3b02a | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | @Test |
| 245 | public void processRouteRemoved() throws Exception { |
| 246 | processRouteAdded(); |
| 247 | |
Charles Chan | 08d9132 | 2018-02-05 17:20:05 -0800 | [diff] [blame] | 248 | reset(srManager.deviceConfiguration); |
| 249 | srManager.deviceConfiguration.removeSubnet(CP1, P1); |
| 250 | expectLastCall().once(); |
| 251 | replay(srManager.deviceConfiguration); |
| 252 | |
Charles Chan | 2fde6d4 | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 253 | RouteEvent re = new RouteEvent(RouteEvent.Type.ROUTE_REMOVED, RR1, Sets.newHashSet(RR1)); |
| 254 | routeHandler.processRouteRemoved(re); |
| 255 | |
| 256 | assertEquals(0, ROUTING_TABLE.size()); |
| 257 | assertEquals(0, SUBNET_TABLE.size()); |
Charles Chan | 08d9132 | 2018-02-05 17:20:05 -0800 | [diff] [blame] | 258 | |
| 259 | verify(srManager.deviceConfiguration); |
Charles Chan | 2fde6d4 | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | @Test |
| 263 | public void testTwoSingleHomedAdded() throws Exception { |
Charles Chan | 08d9132 | 2018-02-05 17:20:05 -0800 | [diff] [blame] | 264 | reset(srManager.deviceConfiguration); |
| 265 | srManager.deviceConfiguration.addSubnet(CP1, P1); |
| 266 | expectLastCall().once(); |
| 267 | srManager.deviceConfiguration.addSubnet(CP2, P1); |
| 268 | expectLastCall().once(); |
| 269 | replay(srManager.deviceConfiguration); |
| 270 | |
Charles Chan | 2fde6d4 | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 271 | RouteEvent re = new RouteEvent(RouteEvent.Type.ROUTE_ADDED, RR1, Sets.newHashSet(RR1, RR2)); |
| 272 | routeHandler.processRouteAdded(re); |
| 273 | |
| 274 | assertEquals(2, ROUTING_TABLE.size()); |
| 275 | MockRoutingTableValue rtv1 = ROUTING_TABLE.get(new MockRoutingTableKey(CP1.deviceId(), P1)); |
| 276 | MockRoutingTableValue rtv2 = ROUTING_TABLE.get(new MockRoutingTableKey(CP2.deviceId(), P1)); |
| 277 | assertEquals(M1, rtv1.macAddress); |
| 278 | assertEquals(M2, rtv2.macAddress); |
| 279 | assertEquals(V1, rtv1.vlanId); |
| 280 | assertEquals(V2, rtv2.vlanId); |
| 281 | assertEquals(CP1.port(), rtv1.portNumber); |
| 282 | assertEquals(CP2.port(), rtv2.portNumber); |
| 283 | |
| 284 | assertEquals(2, SUBNET_TABLE.size()); |
| 285 | assertTrue(SUBNET_TABLE.get(CP1).contains(P1)); |
| 286 | assertTrue(SUBNET_TABLE.get(CP2).contains(P1)); |
Charles Chan | 08d9132 | 2018-02-05 17:20:05 -0800 | [diff] [blame] | 287 | |
| 288 | verify(srManager.deviceConfiguration); |
Charles Chan | 2fde6d4 | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | @Test |
| 292 | public void testOneDualHomedAdded() throws Exception { |
Charles Chan | 08d9132 | 2018-02-05 17:20:05 -0800 | [diff] [blame] | 293 | reset(srManager.deviceConfiguration); |
| 294 | srManager.deviceConfiguration.addSubnet(CP1, P1); |
| 295 | expectLastCall().once(); |
| 296 | srManager.deviceConfiguration.addSubnet(CP2, P1); |
| 297 | expectLastCall().once(); |
| 298 | replay(srManager.deviceConfiguration); |
| 299 | |
Charles Chan | 2fde6d4 | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 300 | RouteEvent re = new RouteEvent(RouteEvent.Type.ROUTE_ADDED, RR3, Sets.newHashSet(RR3)); |
| 301 | routeHandler.processRouteAdded(re); |
| 302 | |
| 303 | assertEquals(2, ROUTING_TABLE.size()); |
| 304 | MockRoutingTableValue rtv1 = ROUTING_TABLE.get(new MockRoutingTableKey(CP1.deviceId(), P1)); |
| 305 | MockRoutingTableValue rtv2 = ROUTING_TABLE.get(new MockRoutingTableKey(CP2.deviceId(), P1)); |
| 306 | assertEquals(M3, rtv1.macAddress); |
| 307 | assertEquals(M3, rtv2.macAddress); |
| 308 | assertEquals(V3, rtv1.vlanId); |
| 309 | assertEquals(V3, rtv2.vlanId); |
| 310 | assertEquals(CP1.port(), rtv1.portNumber); |
| 311 | assertEquals(CP2.port(), rtv2.portNumber); |
| 312 | |
| 313 | assertEquals(2, SUBNET_TABLE.size()); |
| 314 | assertTrue(SUBNET_TABLE.get(CP1).contains(P1)); |
| 315 | assertTrue(SUBNET_TABLE.get(CP2).contains(P1)); |
Charles Chan | 08d9132 | 2018-02-05 17:20:05 -0800 | [diff] [blame] | 316 | |
| 317 | verify(srManager.deviceConfiguration); |
Charles Chan | 2fde6d4 | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | @Test |
| 321 | public void testOneSingleHomedToTwoSingleHomed() throws Exception { |
| 322 | processRouteAdded(); |
| 323 | |
Charles Chan | 08d9132 | 2018-02-05 17:20:05 -0800 | [diff] [blame] | 324 | reset(srManager.deviceConfiguration); |
| 325 | srManager.deviceConfiguration.addSubnet(CP2, P1); |
| 326 | expectLastCall().once(); |
| 327 | replay(srManager.deviceConfiguration); |
| 328 | |
Charles Chan | 2fde6d4 | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 329 | RouteEvent re = new RouteEvent(RouteEvent.Type.ALTERNATIVE_ROUTES_CHANGED, RR1, null, |
| 330 | Sets.newHashSet(RR1, RR2), Sets.newHashSet(RR1)); |
| 331 | routeHandler.processAlternativeRoutesChanged(re); |
| 332 | |
| 333 | assertEquals(2, ROUTING_TABLE.size()); |
| 334 | MockRoutingTableValue rtv1 = ROUTING_TABLE.get(new MockRoutingTableKey(CP1.deviceId(), P1)); |
| 335 | MockRoutingTableValue rtv2 = ROUTING_TABLE.get(new MockRoutingTableKey(CP2.deviceId(), P1)); |
| 336 | assertEquals(M1, rtv1.macAddress); |
| 337 | assertEquals(M2, rtv2.macAddress); |
| 338 | assertEquals(V1, rtv1.vlanId); |
| 339 | assertEquals(V2, rtv2.vlanId); |
| 340 | assertEquals(CP1.port(), rtv1.portNumber); |
| 341 | assertEquals(CP2.port(), rtv2.portNumber); |
| 342 | |
| 343 | assertEquals(2, SUBNET_TABLE.size()); |
| 344 | assertTrue(SUBNET_TABLE.get(CP1).contains(P1)); |
| 345 | assertTrue(SUBNET_TABLE.get(CP2).contains(P1)); |
Charles Chan | 08d9132 | 2018-02-05 17:20:05 -0800 | [diff] [blame] | 346 | |
| 347 | verify(srManager.deviceConfiguration); |
Charles Chan | 2fde6d4 | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | @Test |
| 351 | public void testTwoSingleHomedToOneSingleHomed() throws Exception { |
| 352 | testTwoSingleHomedAdded(); |
| 353 | |
Charles Chan | 08d9132 | 2018-02-05 17:20:05 -0800 | [diff] [blame] | 354 | reset(srManager.deviceConfiguration); |
| 355 | srManager.deviceConfiguration.removeSubnet(CP2, P1); |
| 356 | expectLastCall().once(); |
| 357 | replay(srManager.deviceConfiguration); |
| 358 | |
Charles Chan | 2fde6d4 | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 359 | RouteEvent re = new RouteEvent(RouteEvent.Type.ALTERNATIVE_ROUTES_CHANGED, RR1, null, |
| 360 | Sets.newHashSet(RR1), Sets.newHashSet(RR1, RR2)); |
| 361 | routeHandler.processAlternativeRoutesChanged(re); |
| 362 | |
Charles Chan | 08d9132 | 2018-02-05 17:20:05 -0800 | [diff] [blame] | 363 | // Note: We shouldn't remove the old nexthop during the occasion of route update |
| 364 | // since the populate subnet will take care of it and point it to an ECMP group |
| 365 | assertEquals(2, ROUTING_TABLE.size()); |
Charles Chan | 2fde6d4 | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 366 | MockRoutingTableValue rtv1 = ROUTING_TABLE.get(new MockRoutingTableKey(CP1.deviceId(), P1)); |
| 367 | assertEquals(M1, rtv1.macAddress); |
| 368 | assertEquals(V1, rtv1.vlanId); |
| 369 | assertEquals(CP1.port(), rtv1.portNumber); |
| 370 | |
| 371 | assertEquals(1, SUBNET_TABLE.size()); |
| 372 | assertTrue(SUBNET_TABLE.get(CP1).contains(P1)); |
Charles Chan | 08d9132 | 2018-02-05 17:20:05 -0800 | [diff] [blame] | 373 | |
| 374 | verify(srManager.deviceConfiguration); |
Charles Chan | 2fde6d4 | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 375 | } |
| 376 | |
Charles Chan | 08d9132 | 2018-02-05 17:20:05 -0800 | [diff] [blame] | 377 | // TODO Add test cases for two single homed next hop at same location |
| 378 | |
Charles Chan | 2fde6d4 | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 379 | @Test |
| 380 | public void testDualHomedSingleLocationFail() throws Exception { |
| 381 | testOneDualHomedAdded(); |
| 382 | |
| 383 | HostEvent he = new HostEvent(HostEvent.Type.HOST_MOVED, H3S, H3D); |
| 384 | routeHandler.processHostMovedEvent(he); |
| 385 | |
| 386 | assertEquals(2, ROUTING_TABLE.size()); |
| 387 | MockRoutingTableValue rtv1 = ROUTING_TABLE.get(new MockRoutingTableKey(CP1.deviceId(), P1)); |
| 388 | MockRoutingTableValue rtv2 = ROUTING_TABLE.get(new MockRoutingTableKey(CP2.deviceId(), P1)); |
| 389 | assertEquals(M3, rtv1.macAddress); |
| 390 | assertEquals(M3, rtv2.macAddress); |
| 391 | assertEquals(V3, rtv1.vlanId); |
| 392 | assertEquals(V3, rtv2.vlanId); |
| 393 | assertEquals(CP1.port(), rtv1.portNumber); |
| 394 | assertEquals(P9, rtv2.portNumber); |
| 395 | |
| 396 | // ECMP route table hasn't changed |
| 397 | assertEquals(2, SUBNET_TABLE.size()); |
| 398 | assertTrue(SUBNET_TABLE.get(CP1).contains(P1)); |
| 399 | assertTrue(SUBNET_TABLE.get(CP2).contains(P1)); |
| 400 | } |
| 401 | |
| 402 | @Test |
| 403 | public void testDualHomedBothLocationFail() throws Exception { |
| 404 | testDualHomedSingleLocationFail(); |
| 405 | |
| 406 | hostService = new MockHostService(HOSTS_ONE_FAIL); |
| 407 | |
Charles Chan | 08d9132 | 2018-02-05 17:20:05 -0800 | [diff] [blame] | 408 | reset(srManager.deviceConfiguration); |
| 409 | srManager.deviceConfiguration.removeSubnet(CP1, P1); |
| 410 | expectLastCall().once(); |
| 411 | srManager.deviceConfiguration.removeSubnet(CP2, P1); |
| 412 | expectLastCall().once(); |
| 413 | replay(srManager.deviceConfiguration); |
| 414 | |
Charles Chan | 2fde6d4 | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 415 | RouteEvent re = new RouteEvent(RouteEvent.Type.ROUTE_REMOVED, RR3, Sets.newHashSet(RR3)); |
| 416 | routeHandler.processRouteRemoved(re); |
| 417 | |
| 418 | assertEquals(0, ROUTING_TABLE.size()); |
| 419 | assertEquals(0, SUBNET_TABLE.size()); |
Charles Chan | 08d9132 | 2018-02-05 17:20:05 -0800 | [diff] [blame] | 420 | |
| 421 | verify(srManager.deviceConfiguration); |
Charles Chan | 2fde6d4 | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | @Test |
| 425 | public void testTwoSingleHomedRemoved() throws Exception { |
| 426 | testTwoSingleHomedAdded(); |
| 427 | |
| 428 | hostService = new MockHostService(HOSTS_BOTH_FAIL); |
| 429 | |
Charles Chan | 08d9132 | 2018-02-05 17:20:05 -0800 | [diff] [blame] | 430 | reset(srManager.deviceConfiguration); |
| 431 | srManager.deviceConfiguration.removeSubnet(CP1, P1); |
| 432 | expectLastCall().once(); |
| 433 | srManager.deviceConfiguration.removeSubnet(CP2, P1); |
| 434 | expectLastCall().once(); |
| 435 | replay(srManager.deviceConfiguration); |
| 436 | |
Charles Chan | 2fde6d4 | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 437 | RouteEvent re = new RouteEvent(RouteEvent.Type.ROUTE_REMOVED, RR1, Sets.newHashSet(RR1, RR2)); |
| 438 | routeHandler.processRouteRemoved(re); |
| 439 | |
| 440 | assertEquals(0, ROUTING_TABLE.size()); |
| 441 | assertEquals(0, SUBNET_TABLE.size()); |
Charles Chan | 08d9132 | 2018-02-05 17:20:05 -0800 | [diff] [blame] | 442 | |
| 443 | verify(srManager.deviceConfiguration); |
Charles Chan | 2fde6d4 | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | @Test |
| 447 | public void testOneDualHomeRemoved() throws Exception { |
| 448 | testOneDualHomedAdded(); |
| 449 | |
Charles Chan | 08d9132 | 2018-02-05 17:20:05 -0800 | [diff] [blame] | 450 | reset(srManager.deviceConfiguration); |
| 451 | srManager.deviceConfiguration.removeSubnet(CP1, P1); |
| 452 | expectLastCall().once(); |
| 453 | srManager.deviceConfiguration.removeSubnet(CP2, P1); |
| 454 | expectLastCall().once(); |
| 455 | replay(srManager.deviceConfiguration); |
| 456 | |
Charles Chan | 2fde6d4 | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 457 | RouteEvent re = new RouteEvent(RouteEvent.Type.ROUTE_REMOVED, RR3, Sets.newHashSet(RR3)); |
Charles Chan | 1a3b02a | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 458 | routeHandler.processRouteRemoved(re); |
| 459 | |
| 460 | assertEquals(0, ROUTING_TABLE.size()); |
| 461 | assertEquals(0, SUBNET_TABLE.size()); |
Charles Chan | 08d9132 | 2018-02-05 17:20:05 -0800 | [diff] [blame] | 462 | |
| 463 | verify(srManager.deviceConfiguration); |
Charles Chan | 1a3b02a | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 464 | } |
Charles Chan | 1a3b02a | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 465 | } |