blob: 45bc309f6e43a8e3a8dfc670cce3c59b81da2409 [file] [log] [blame]
Jonathan Hart41349e92015-02-09 14:14:02 -08001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Jonathan Hart41349e92015-02-09 14:14:02 -08003 *
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 Hart2da1e602015-02-18 19:09:24 -080016package org.onosproject.routing.impl;
Jonathan Hart41349e92015-02-09 14:14:02 -080017
18import com.google.common.collect.Sets;
Pingping Line28ae4c2015-03-13 11:37:03 -070019
Jonathan Hart41349e92015-02-09 14:14:02 -080020import org.junit.After;
21import org.junit.Before;
22import org.junit.Test;
23import org.onlab.packet.Ip4Address;
24import org.onlab.packet.Ip4Prefix;
25import org.onlab.packet.IpAddress;
26import org.onlab.packet.IpPrefix;
27import org.onlab.packet.MacAddress;
28import org.onlab.packet.VlanId;
Jonathan Hart66018992015-07-31 11:19:27 -070029import org.onosproject.core.CoreService;
Jonathan Hart41349e92015-02-09 14:14:02 -080030import org.onosproject.net.ConnectPoint;
31import org.onosproject.net.DefaultHost;
32import org.onosproject.net.DeviceId;
33import org.onosproject.net.Host;
34import org.onosproject.net.HostId;
35import org.onosproject.net.HostLocation;
36import org.onosproject.net.PortNumber;
37import org.onosproject.net.host.HostListener;
38import org.onosproject.net.host.HostService;
39import org.onosproject.net.provider.ProviderId;
Jonathan Hart2da1e602015-02-18 19:09:24 -080040import org.onosproject.routing.BgpService;
41import org.onosproject.routing.FibEntry;
42import org.onosproject.routing.FibListener;
43import org.onosproject.routing.FibUpdate;
44import org.onosproject.routing.RouteEntry;
45import org.onosproject.routing.RouteListener;
46import org.onosproject.routing.RouteUpdate;
Pingping Line28ae4c2015-03-13 11:37:03 -070047import org.onosproject.routing.config.RoutingConfigurationService;
Jonathan Hart41349e92015-02-09 14:14:02 -080048
49import java.util.Collections;
50
51import static org.easymock.EasyMock.*;
52import static org.junit.Assert.assertEquals;
53import static org.junit.Assert.assertTrue;
54
55/**
56 * This class tests adding a route, updating a route, deleting a route,
57 * and adding a route whose next hop is the local BGP speaker.
58 * <p/>
59 * The HostService answers requests synchronously.
60 */
61public class RouterTest {
62
63 private HostService hostService;
Pingping Line28ae4c2015-03-13 11:37:03 -070064 private RoutingConfigurationService routingConfigurationService;
Jonathan Hart41349e92015-02-09 14:14:02 -080065
66 private FibListener fibListener;
67
68 private static final ConnectPoint SW1_ETH1 = new ConnectPoint(
69 DeviceId.deviceId("of:0000000000000001"),
70 PortNumber.portNumber(1));
71
72 private static final ConnectPoint SW2_ETH1 = new ConnectPoint(
73 DeviceId.deviceId("of:0000000000000002"),
74 PortNumber.portNumber(1));
75
76 private static final ConnectPoint SW3_ETH1 = new ConnectPoint(
77 DeviceId.deviceId("of:0000000000000003"),
78 PortNumber.portNumber(1));
79
80 private static final ConnectPoint SW4_ETH1 = new ConnectPoint(
81 DeviceId.deviceId("of:0000000000000004"),
82 PortNumber.portNumber(1));
83
84 private Router router;
85
86 @Before
87 public void setUp() throws Exception {
88 setUpHostService();
Pingping Line28ae4c2015-03-13 11:37:03 -070089 routingConfigurationService =
90 createMock(RoutingConfigurationService.class);
Jonathan Hart41349e92015-02-09 14:14:02 -080091
92 BgpService bgpService = createMock(BgpService.class);
Jonathan Hartd24fafb2015-02-09 17:55:32 -080093 bgpService.start(anyObject(RouteListener.class));
Jonathan Hart41349e92015-02-09 14:14:02 -080094 bgpService.stop();
95 replay(bgpService);
96
97 fibListener = createMock(FibListener.class);
98
99 router = new Router();
Jonathan Hart66018992015-07-31 11:19:27 -0700100 router.coreService = createNiceMock(CoreService.class);
Jonathan Hart41349e92015-02-09 14:14:02 -0800101 router.hostService = hostService;
Pingping Line28ae4c2015-03-13 11:37:03 -0700102 router.routingConfigurationService = routingConfigurationService;
Jonathan Hart41349e92015-02-09 14:14:02 -0800103 router.bgpService = bgpService;
104 router.activate();
105
Pingping Line28ae4c2015-03-13 11:37:03 -0700106 router.addFibListener(fibListener);
107 router.start();
Jonathan Hart41349e92015-02-09 14:14:02 -0800108 }
109
110 @After
111 public void tearDown() {
112 router.stop();
113 }
114
115 /**
116 * Sets up the host service with details of some hosts.
117 */
118 private void setUpHostService() {
119 hostService = createMock(HostService.class);
120
121 hostService.addListener(anyObject(HostListener.class));
122 expectLastCall().anyTimes();
123
124 IpAddress host1Address = IpAddress.valueOf("192.168.10.1");
125 Host host1 = new DefaultHost(ProviderId.NONE, HostId.NONE,
126 MacAddress.valueOf("00:00:00:00:00:01"), VlanId.NONE,
127 new HostLocation(SW1_ETH1, 1),
128 Sets.newHashSet(host1Address));
129
130 expect(hostService.getHostsByIp(host1Address))
131 .andReturn(Sets.newHashSet(host1)).anyTimes();
132 hostService.startMonitoringIp(host1Address);
133 expectLastCall().anyTimes();
134
135
136 IpAddress host2Address = IpAddress.valueOf("192.168.20.1");
137 Host host2 = new DefaultHost(ProviderId.NONE, HostId.NONE,
138 MacAddress.valueOf("00:00:00:00:00:02"), VlanId.NONE,
139 new HostLocation(SW2_ETH1, 1),
140 Sets.newHashSet(host2Address));
141
142 expect(hostService.getHostsByIp(host2Address))
143 .andReturn(Sets.newHashSet(host2)).anyTimes();
144 hostService.startMonitoringIp(host2Address);
145 expectLastCall().anyTimes();
146
147 // Next hop on a VLAN
148 IpAddress host3Address = IpAddress.valueOf("192.168.40.1");
149 Host host3 = new DefaultHost(ProviderId.NONE, HostId.NONE,
150 MacAddress.valueOf("00:00:00:00:00:03"), VlanId.vlanId((short) 1),
151 new HostLocation(SW4_ETH1, 1),
152 Sets.newHashSet(host3Address));
153
154 expect(hostService.getHostsByIp(host3Address))
155 .andReturn(Sets.newHashSet(host3)).anyTimes();
156 hostService.startMonitoringIp(host3Address);
157 expectLastCall().anyTimes();
158
159 // Called during shutdown
160 hostService.removeListener(anyObject(HostListener.class));
161
162 replay(hostService);
163 }
164
165 /**
166 * Tests adding a route entry.
167 */
168 @Test
169 public void testRouteAdd() {
170 // Construct a route entry
171 IpPrefix prefix = Ip4Prefix.valueOf("1.1.1.0/24");
172 IpAddress nextHopIp = Ip4Address.valueOf("192.168.10.1");
173
174 RouteEntry routeEntry = new RouteEntry(prefix, nextHopIp);
175
176 // Expected FIB entry
177 FibEntry fibEntry = new FibEntry(prefix, nextHopIp,
178 MacAddress.valueOf("00:00:00:00:00:01"));
179
180 fibListener.update(Collections.singletonList(new FibUpdate(
181 FibUpdate.Type.UPDATE, fibEntry)), Collections.emptyList());
182
183 replay(fibListener);
184
185 router.processRouteUpdates(Collections.singletonList(
186 new RouteUpdate(RouteUpdate.Type.UPDATE, routeEntry)));
187
188 verify(fibListener);
189 }
190
191 /**
192 * Tests updating a route entry.
193 */
194 @Test
195 public void testRouteUpdate() {
196 // Firstly add a route
197 testRouteAdd();
198
199 // Route entry with updated next hop for the original prefix
200 RouteEntry routeEntryUpdate = new RouteEntry(
201 Ip4Prefix.valueOf("1.1.1.0/24"),
202 Ip4Address.valueOf("192.168.20.1"));
203
204 // The old FIB entry will be withdrawn
205 FibEntry withdrawFibEntry = new FibEntry(
206 Ip4Prefix.valueOf("1.1.1.0/24"), null, null);
207
208 // A new FIB entry will be added
209 FibEntry updateFibEntry = new FibEntry(
210 Ip4Prefix.valueOf("1.1.1.0/24"),
211 Ip4Address.valueOf("192.168.20.1"),
212 MacAddress.valueOf("00:00:00:00:00:02"));
213
214 reset(fibListener);
215 fibListener.update(Collections.singletonList(new FibUpdate(
216 FibUpdate.Type.UPDATE, updateFibEntry)),
217 Collections.singletonList(new FibUpdate(
218 FibUpdate.Type.DELETE, withdrawFibEntry)));
Jonathan Hart41349e92015-02-09 14:14:02 -0800219 replay(fibListener);
220
Pingping Line28ae4c2015-03-13 11:37:03 -0700221 reset(routingConfigurationService);
222 expect(routingConfigurationService.isIpPrefixLocal(
223 anyObject(IpPrefix.class))).andReturn(false);
224 replay(routingConfigurationService);
225
Jonathan Hart41349e92015-02-09 14:14:02 -0800226 router.processRouteUpdates(Collections.singletonList(new RouteUpdate(
227 RouteUpdate.Type.UPDATE, routeEntryUpdate)));
228
229 verify(fibListener);
230 }
231
232 /**
233 * Tests deleting a route entry.
234 */
235 @Test
236 public void testRouteDelete() {
237 // Firstly add a route
238 testRouteAdd();
239
240 RouteEntry deleteRouteEntry = new RouteEntry(
241 Ip4Prefix.valueOf("1.1.1.0/24"),
242 Ip4Address.valueOf("192.168.10.1"));
243
244 FibEntry deleteFibEntry = new FibEntry(
245 Ip4Prefix.valueOf("1.1.1.0/24"), null, null);
246
247 reset(fibListener);
248 fibListener.update(Collections.emptyList(), Collections.singletonList(
249 new FibUpdate(FibUpdate.Type.DELETE, deleteFibEntry)));
250
251 replay(fibListener);
252
253 router.processRouteUpdates(Collections.singletonList(
254 new RouteUpdate(RouteUpdate.Type.DELETE, deleteRouteEntry)));
255
256 verify(fibListener);
257 }
258
259 /**
260 * Tests adding a route whose next hop is the local BGP speaker.
261 */
262 @Test
263 public void testLocalRouteAdd() {
264 // Construct a route entry, the next hop is the local BGP speaker
265 RouteEntry routeEntry = new RouteEntry(
266 Ip4Prefix.valueOf("1.1.1.0/24"),
267 Ip4Address.valueOf("0.0.0.0"));
268
269 // No methods on the FIB listener should be called
270 replay(fibListener);
271
Pingping Line28ae4c2015-03-13 11:37:03 -0700272 reset(routingConfigurationService);
273 expect(routingConfigurationService.isIpPrefixLocal(
274 anyObject(IpPrefix.class))).andReturn(true);
275 replay(routingConfigurationService);
276
Jonathan Hart41349e92015-02-09 14:14:02 -0800277 // Call the processRouteUpdates() method in Router class
278 RouteUpdate routeUpdate = new RouteUpdate(RouteUpdate.Type.UPDATE,
279 routeEntry);
280 router.processRouteUpdates(Collections.singletonList(routeUpdate));
281
282 // Verify
283 assertEquals(1, router.getRoutes4().size());
284 assertTrue(router.getRoutes4().contains(routeEntry));
285 verify(fibListener);
286 }
287}