Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [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 | */ |
Jonathan Hart | 2da1e60 | 2015-02-18 19:09:24 -0800 | [diff] [blame] | 16 | package org.onosproject.routing.impl; |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 17 | |
| 18 | import com.google.common.collect.Sets; |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 19 | |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 20 | import org.junit.After; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
| 23 | import org.onlab.packet.Ip4Address; |
| 24 | import org.onlab.packet.Ip4Prefix; |
| 25 | import org.onlab.packet.IpAddress; |
| 26 | import org.onlab.packet.IpPrefix; |
| 27 | import org.onlab.packet.MacAddress; |
| 28 | import org.onlab.packet.VlanId; |
Jonathan Hart | 6601899 | 2015-07-31 11:19:27 -0700 | [diff] [blame] | 29 | import org.onosproject.core.CoreService; |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 30 | import org.onosproject.net.ConnectPoint; |
| 31 | import org.onosproject.net.DefaultHost; |
| 32 | import org.onosproject.net.DeviceId; |
| 33 | import org.onosproject.net.Host; |
| 34 | import org.onosproject.net.HostId; |
| 35 | import org.onosproject.net.HostLocation; |
| 36 | import org.onosproject.net.PortNumber; |
| 37 | import org.onosproject.net.host.HostEvent; |
| 38 | import org.onosproject.net.host.HostListener; |
| 39 | import org.onosproject.net.host.HostService; |
| 40 | import org.onosproject.net.provider.ProviderId; |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 41 | import org.onosproject.routing.config.RoutingConfigurationService; |
Jonathan Hart | 2da1e60 | 2015-02-18 19:09:24 -0800 | [diff] [blame] | 42 | import org.onosproject.routing.impl.Router.InternalHostListener; |
Jonathan Hart | 3930f63 | 2015-10-19 12:12:51 -0700 | [diff] [blame] | 43 | import org.onosproject.routing.RouteSourceService; |
Jonathan Hart | 2da1e60 | 2015-02-18 19:09:24 -0800 | [diff] [blame] | 44 | import org.onosproject.routing.FibEntry; |
| 45 | import org.onosproject.routing.FibListener; |
| 46 | import org.onosproject.routing.FibUpdate; |
| 47 | import org.onosproject.routing.RouteEntry; |
| 48 | import org.onosproject.routing.RouteListener; |
| 49 | import org.onosproject.routing.RouteUpdate; |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 50 | |
| 51 | import java.util.Collections; |
| 52 | |
| 53 | import static org.easymock.EasyMock.*; |
| 54 | |
| 55 | /** |
| 56 | * This class tests adding a route and updating a route. |
| 57 | * The HostService module answers the MAC address asynchronously. |
| 58 | */ |
| 59 | public class RouterAsyncArpTest { |
| 60 | |
| 61 | private HostService hostService; |
| 62 | private FibListener fibListener; |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 63 | private RoutingConfigurationService routingConfigurationService; |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 64 | |
| 65 | private static final ConnectPoint SW1_ETH1 = new ConnectPoint( |
| 66 | DeviceId.deviceId("of:0000000000000001"), |
| 67 | PortNumber.portNumber(1)); |
| 68 | |
| 69 | private static final ConnectPoint SW2_ETH1 = new ConnectPoint( |
| 70 | DeviceId.deviceId("of:0000000000000002"), |
| 71 | PortNumber.portNumber(1)); |
| 72 | |
| 73 | private static final ConnectPoint SW3_ETH1 = new ConnectPoint( |
| 74 | DeviceId.deviceId("of:0000000000000003"), |
| 75 | PortNumber.portNumber(1)); |
| 76 | |
| 77 | private Router router; |
| 78 | private InternalHostListener internalHostListener; |
| 79 | |
| 80 | @Before |
| 81 | public void setUp() throws Exception { |
| 82 | hostService = createMock(HostService.class); |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 83 | routingConfigurationService = |
| 84 | createMock(RoutingConfigurationService.class); |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 85 | |
Jonathan Hart | 3930f63 | 2015-10-19 12:12:51 -0700 | [diff] [blame] | 86 | RouteSourceService routeSourceService = createMock(RouteSourceService.class); |
| 87 | routeSourceService.start(anyObject(RouteListener.class)); |
| 88 | routeSourceService.stop(); |
| 89 | replay(routeSourceService); |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 90 | |
| 91 | fibListener = createMock(FibListener.class); |
| 92 | |
| 93 | router = new Router(); |
Jonathan Hart | 6601899 | 2015-07-31 11:19:27 -0700 | [diff] [blame] | 94 | router.coreService = createNiceMock(CoreService.class); |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 95 | router.hostService = hostService; |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 96 | router.routingConfigurationService = routingConfigurationService; |
Jonathan Hart | 3930f63 | 2015-10-19 12:12:51 -0700 | [diff] [blame] | 97 | router.routeSourceService = routeSourceService; |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 98 | router.activate(); |
| 99 | |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 100 | router.addFibListener(fibListener); |
| 101 | router.start(); |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 102 | |
| 103 | internalHostListener = router.new InternalHostListener(); |
| 104 | } |
| 105 | |
| 106 | @After |
| 107 | public void tearDown() { |
| 108 | // Called during shutdown |
| 109 | reset(hostService); |
| 110 | hostService.removeListener(anyObject(HostListener.class)); |
| 111 | |
| 112 | router.stop(); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Tests adding a route entry with asynchronous HostService replies. |
| 117 | */ |
| 118 | @Test |
| 119 | public void testRouteAdd() { |
| 120 | // Construct a route entry |
| 121 | IpPrefix prefix = Ip4Prefix.valueOf("1.1.1.0/24"); |
| 122 | IpAddress nextHopIp = Ip4Address.valueOf("192.168.10.1"); |
| 123 | |
| 124 | RouteEntry routeEntry = new RouteEntry(prefix, nextHopIp); |
| 125 | |
| 126 | // Host service will reply with no hosts when asked |
| 127 | reset(hostService); |
| 128 | expect(hostService.getHostsByIp(anyObject(IpAddress.class))).andReturn( |
| 129 | Collections.emptySet()).anyTimes(); |
| 130 | hostService.startMonitoringIp(IpAddress.valueOf("192.168.10.1")); |
| 131 | replay(hostService); |
| 132 | |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 133 | reset(routingConfigurationService); |
| 134 | expect(routingConfigurationService.isIpPrefixLocal( |
| 135 | anyObject(IpPrefix.class))).andReturn(false); |
| 136 | replay(routingConfigurationService); |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 137 | |
| 138 | // Initially when we add the route, no FIB update will be sent |
| 139 | replay(fibListener); |
| 140 | |
| 141 | router.processRouteUpdates(Collections.singletonList( |
| 142 | new RouteUpdate(RouteUpdate.Type.UPDATE, routeEntry))); |
| 143 | |
| 144 | verify(fibListener); |
| 145 | |
| 146 | |
| 147 | // Now when we send the event, we expect the FIB update to be sent |
| 148 | reset(fibListener); |
| 149 | FibEntry fibEntry = new FibEntry(prefix, nextHopIp, |
| 150 | MacAddress.valueOf("00:00:00:00:00:01")); |
| 151 | |
| 152 | fibListener.update(Collections.singletonList(new FibUpdate( |
| 153 | FibUpdate.Type.UPDATE, fibEntry)), Collections.emptyList()); |
| 154 | replay(fibListener); |
| 155 | |
| 156 | Host host = new DefaultHost(ProviderId.NONE, HostId.NONE, |
| 157 | MacAddress.valueOf("00:00:00:00:00:01"), VlanId.NONE, |
| 158 | new HostLocation( |
| 159 | SW1_ETH1.deviceId(), |
| 160 | SW1_ETH1.port(), 1), |
| 161 | Sets.newHashSet(IpAddress.valueOf("192.168.10.1"))); |
| 162 | |
| 163 | // Send in the host event |
| 164 | internalHostListener.event( |
| 165 | new HostEvent(HostEvent.Type.HOST_ADDED, host)); |
| 166 | |
| 167 | verify(fibListener); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Tests updating a route entry with asynchronous HostService replies. |
| 172 | */ |
| 173 | @Test |
| 174 | public void testRouteUpdate() { |
| 175 | // Add a route |
| 176 | testRouteAdd(); |
| 177 | |
| 178 | // Construct a route entry |
| 179 | IpPrefix prefix = Ip4Prefix.valueOf("1.1.1.0/24"); |
| 180 | IpAddress nextHopIp = Ip4Address.valueOf("192.168.20.1"); |
| 181 | |
| 182 | RouteEntry routeEntry = new RouteEntry(prefix, nextHopIp); |
| 183 | |
| 184 | // Host service will reply with no hosts when asked |
| 185 | reset(hostService); |
| 186 | expect(hostService.getHostsByIp(anyObject(IpAddress.class))).andReturn( |
| 187 | Collections.emptySet()).anyTimes(); |
| 188 | hostService.startMonitoringIp(IpAddress.valueOf("192.168.20.1")); |
| 189 | replay(hostService); |
| 190 | |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 191 | reset(routingConfigurationService); |
| 192 | expect(routingConfigurationService.isIpPrefixLocal( |
| 193 | anyObject(IpPrefix.class))).andReturn(false); |
| 194 | replay(routingConfigurationService); |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 195 | |
| 196 | // Initially when we add the route, the DELETE FIB update will be sent |
| 197 | // but the UPDATE FIB update will come later when the MAC is resolved |
| 198 | reset(fibListener); |
| 199 | |
| 200 | fibListener.update(Collections.emptyList(), Collections.singletonList(new FibUpdate( |
| 201 | FibUpdate.Type.DELETE, new FibEntry(prefix, null, null)))); |
| 202 | replay(fibListener); |
| 203 | |
| 204 | router.processRouteUpdates(Collections.singletonList( |
| 205 | new RouteUpdate(RouteUpdate.Type.UPDATE, routeEntry))); |
| 206 | |
| 207 | verify(fibListener); |
| 208 | |
| 209 | |
| 210 | // Now when we send the event, we expect the FIB update to be sent |
| 211 | reset(fibListener); |
| 212 | FibEntry fibEntry = new FibEntry(prefix, nextHopIp, |
| 213 | MacAddress.valueOf("00:00:00:00:00:02")); |
| 214 | |
| 215 | fibListener.update(Collections.singletonList(new FibUpdate( |
| 216 | FibUpdate.Type.UPDATE, fibEntry)), Collections.emptyList()); |
| 217 | replay(fibListener); |
| 218 | |
| 219 | Host host = new DefaultHost(ProviderId.NONE, HostId.NONE, |
| 220 | MacAddress.valueOf("00:00:00:00:00:02"), VlanId.NONE, |
| 221 | new HostLocation( |
| 222 | SW1_ETH1.deviceId(), |
| 223 | SW1_ETH1.port(), 1), |
| 224 | Sets.newHashSet(IpAddress.valueOf("192.168.20.1"))); |
| 225 | |
| 226 | // Send in the host event |
| 227 | internalHostListener.event( |
| 228 | new HostEvent(HostEvent.Type.HOST_ADDED, host)); |
| 229 | |
| 230 | verify(fibListener); |
| 231 | } |
| 232 | } |