blob: 26ed60a0085e1774e1681ca1cf7cd868c990c697 [file] [log] [blame]
Jonathan Hart41349e92015-02-09 14:14:02 -08001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.routing;
17
18import com.google.common.collect.Sets;
19import org.junit.After;
20import org.junit.Before;
21import org.junit.Test;
22import org.onlab.packet.Ip4Address;
23import org.onlab.packet.Ip4Prefix;
24import org.onlab.packet.IpAddress;
25import org.onlab.packet.IpPrefix;
26import org.onlab.packet.MacAddress;
27import org.onlab.packet.VlanId;
28import org.onosproject.net.ConnectPoint;
29import org.onosproject.net.DefaultHost;
30import org.onosproject.net.DeviceId;
31import org.onosproject.net.Host;
32import org.onosproject.net.HostId;
33import org.onosproject.net.HostLocation;
34import org.onosproject.net.PortNumber;
35import org.onosproject.net.host.HostListener;
36import org.onosproject.net.host.HostService;
37import org.onosproject.net.provider.ProviderId;
38import org.onosproject.routingapi.BgpService;
39import org.onosproject.routingapi.FibEntry;
40import org.onosproject.routingapi.FibListener;
41import org.onosproject.routingapi.FibUpdate;
42import org.onosproject.routingapi.RouteEntry;
43import org.onosproject.routingapi.RouteListener;
44import org.onosproject.routingapi.RouteUpdate;
45
46import java.util.Collections;
47
48import static org.easymock.EasyMock.*;
49import static org.junit.Assert.assertEquals;
50import static org.junit.Assert.assertTrue;
51
52/**
53 * This class tests adding a route, updating a route, deleting a route,
54 * and adding a route whose next hop is the local BGP speaker.
55 * <p/>
56 * The HostService answers requests synchronously.
57 */
58public class RouterTest {
59
60 private HostService hostService;
61
62 private FibListener fibListener;
63
64 private static final ConnectPoint SW1_ETH1 = new ConnectPoint(
65 DeviceId.deviceId("of:0000000000000001"),
66 PortNumber.portNumber(1));
67
68 private static final ConnectPoint SW2_ETH1 = new ConnectPoint(
69 DeviceId.deviceId("of:0000000000000002"),
70 PortNumber.portNumber(1));
71
72 private static final ConnectPoint SW3_ETH1 = new ConnectPoint(
73 DeviceId.deviceId("of:0000000000000003"),
74 PortNumber.portNumber(1));
75
76 private static final ConnectPoint SW4_ETH1 = new ConnectPoint(
77 DeviceId.deviceId("of:0000000000000004"),
78 PortNumber.portNumber(1));
79
80 private Router router;
81
82 @Before
83 public void setUp() throws Exception {
84 setUpHostService();
85
86 BgpService bgpService = createMock(BgpService.class);
Jonathan Hartd24fafb2015-02-09 17:55:32 -080087 bgpService.start(anyObject(RouteListener.class));
Jonathan Hart41349e92015-02-09 14:14:02 -080088 bgpService.stop();
89 replay(bgpService);
90
91 fibListener = createMock(FibListener.class);
92
93 router = new Router();
94 router.hostService = hostService;
95 router.bgpService = bgpService;
96 router.activate();
97
98 router.start(fibListener);
99 }
100
101 @After
102 public void tearDown() {
103 router.stop();
104 }
105
106 /**
107 * Sets up the host service with details of some hosts.
108 */
109 private void setUpHostService() {
110 hostService = createMock(HostService.class);
111
112 hostService.addListener(anyObject(HostListener.class));
113 expectLastCall().anyTimes();
114
115 IpAddress host1Address = IpAddress.valueOf("192.168.10.1");
116 Host host1 = new DefaultHost(ProviderId.NONE, HostId.NONE,
117 MacAddress.valueOf("00:00:00:00:00:01"), VlanId.NONE,
118 new HostLocation(SW1_ETH1, 1),
119 Sets.newHashSet(host1Address));
120
121 expect(hostService.getHostsByIp(host1Address))
122 .andReturn(Sets.newHashSet(host1)).anyTimes();
123 hostService.startMonitoringIp(host1Address);
124 expectLastCall().anyTimes();
125
126
127 IpAddress host2Address = IpAddress.valueOf("192.168.20.1");
128 Host host2 = new DefaultHost(ProviderId.NONE, HostId.NONE,
129 MacAddress.valueOf("00:00:00:00:00:02"), VlanId.NONE,
130 new HostLocation(SW2_ETH1, 1),
131 Sets.newHashSet(host2Address));
132
133 expect(hostService.getHostsByIp(host2Address))
134 .andReturn(Sets.newHashSet(host2)).anyTimes();
135 hostService.startMonitoringIp(host2Address);
136 expectLastCall().anyTimes();
137
138 // Next hop on a VLAN
139 IpAddress host3Address = IpAddress.valueOf("192.168.40.1");
140 Host host3 = new DefaultHost(ProviderId.NONE, HostId.NONE,
141 MacAddress.valueOf("00:00:00:00:00:03"), VlanId.vlanId((short) 1),
142 new HostLocation(SW4_ETH1, 1),
143 Sets.newHashSet(host3Address));
144
145 expect(hostService.getHostsByIp(host3Address))
146 .andReturn(Sets.newHashSet(host3)).anyTimes();
147 hostService.startMonitoringIp(host3Address);
148 expectLastCall().anyTimes();
149
150 // Called during shutdown
151 hostService.removeListener(anyObject(HostListener.class));
152
153 replay(hostService);
154 }
155
156 /**
157 * Tests adding a route entry.
158 */
159 @Test
160 public void testRouteAdd() {
161 // Construct a route entry
162 IpPrefix prefix = Ip4Prefix.valueOf("1.1.1.0/24");
163 IpAddress nextHopIp = Ip4Address.valueOf("192.168.10.1");
164
165 RouteEntry routeEntry = new RouteEntry(prefix, nextHopIp);
166
167 // Expected FIB entry
168 FibEntry fibEntry = new FibEntry(prefix, nextHopIp,
169 MacAddress.valueOf("00:00:00:00:00:01"));
170
171 fibListener.update(Collections.singletonList(new FibUpdate(
172 FibUpdate.Type.UPDATE, fibEntry)), Collections.emptyList());
173
174 replay(fibListener);
175
176 router.processRouteUpdates(Collections.singletonList(
177 new RouteUpdate(RouteUpdate.Type.UPDATE, routeEntry)));
178
179 verify(fibListener);
180 }
181
182 /**
183 * Tests updating a route entry.
184 */
185 @Test
186 public void testRouteUpdate() {
187 // Firstly add a route
188 testRouteAdd();
189
190 // Route entry with updated next hop for the original prefix
191 RouteEntry routeEntryUpdate = new RouteEntry(
192 Ip4Prefix.valueOf("1.1.1.0/24"),
193 Ip4Address.valueOf("192.168.20.1"));
194
195 // The old FIB entry will be withdrawn
196 FibEntry withdrawFibEntry = new FibEntry(
197 Ip4Prefix.valueOf("1.1.1.0/24"), null, null);
198
199 // A new FIB entry will be added
200 FibEntry updateFibEntry = new FibEntry(
201 Ip4Prefix.valueOf("1.1.1.0/24"),
202 Ip4Address.valueOf("192.168.20.1"),
203 MacAddress.valueOf("00:00:00:00:00:02"));
204
205 reset(fibListener);
206 fibListener.update(Collections.singletonList(new FibUpdate(
207 FibUpdate.Type.UPDATE, updateFibEntry)),
208 Collections.singletonList(new FibUpdate(
209 FibUpdate.Type.DELETE, withdrawFibEntry)));
210
211 replay(fibListener);
212
213 router.processRouteUpdates(Collections.singletonList(new RouteUpdate(
214 RouteUpdate.Type.UPDATE, routeEntryUpdate)));
215
216 verify(fibListener);
217 }
218
219 /**
220 * Tests deleting a route entry.
221 */
222 @Test
223 public void testRouteDelete() {
224 // Firstly add a route
225 testRouteAdd();
226
227 RouteEntry deleteRouteEntry = new RouteEntry(
228 Ip4Prefix.valueOf("1.1.1.0/24"),
229 Ip4Address.valueOf("192.168.10.1"));
230
231 FibEntry deleteFibEntry = new FibEntry(
232 Ip4Prefix.valueOf("1.1.1.0/24"), null, null);
233
234 reset(fibListener);
235 fibListener.update(Collections.emptyList(), Collections.singletonList(
236 new FibUpdate(FibUpdate.Type.DELETE, deleteFibEntry)));
237
238 replay(fibListener);
239
240 router.processRouteUpdates(Collections.singletonList(
241 new RouteUpdate(RouteUpdate.Type.DELETE, deleteRouteEntry)));
242
243 verify(fibListener);
244 }
245
246 /**
247 * Tests adding a route whose next hop is the local BGP speaker.
248 */
249 @Test
250 public void testLocalRouteAdd() {
251 // Construct a route entry, the next hop is the local BGP speaker
252 RouteEntry routeEntry = new RouteEntry(
253 Ip4Prefix.valueOf("1.1.1.0/24"),
254 Ip4Address.valueOf("0.0.0.0"));
255
256 // No methods on the FIB listener should be called
257 replay(fibListener);
258
259 // Call the processRouteUpdates() method in Router class
260 RouteUpdate routeUpdate = new RouteUpdate(RouteUpdate.Type.UPDATE,
261 routeEntry);
262 router.processRouteUpdates(Collections.singletonList(routeUpdate));
263
264 // Verify
265 assertEquals(1, router.getRoutes4().size());
266 assertTrue(router.getRoutes4().contains(routeEntry));
267 verify(fibListener);
268 }
269}