blob: 1519758c8356ef4c60b763cdc48a60b008d42333 [file] [log] [blame]
Jonathan Hart41349e92015-02-09 14:14:02 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present 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.HostEvent;
38import org.onosproject.net.host.HostListener;
39import org.onosproject.net.host.HostService;
40import org.onosproject.net.provider.ProviderId;
Pingping Line28ae4c2015-03-13 11:37:03 -070041import org.onosproject.routing.config.RoutingConfigurationService;
Jonathan Hart2da1e602015-02-18 19:09:24 -080042import org.onosproject.routing.impl.Router.InternalHostListener;
Jonathan Hart3930f632015-10-19 12:12:51 -070043import org.onosproject.routing.RouteSourceService;
Jonathan Hart2da1e602015-02-18 19:09:24 -080044import org.onosproject.routing.FibEntry;
45import org.onosproject.routing.FibListener;
46import org.onosproject.routing.FibUpdate;
47import org.onosproject.routing.RouteEntry;
48import org.onosproject.routing.RouteListener;
49import org.onosproject.routing.RouteUpdate;
Jonathan Hart41349e92015-02-09 14:14:02 -080050
51import java.util.Collections;
52
53import 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*/
59public class RouterAsyncArpTest {
60
61 private HostService hostService;
62 private FibListener fibListener;
Pingping Line28ae4c2015-03-13 11:37:03 -070063 private RoutingConfigurationService routingConfigurationService;
Jonathan Hart41349e92015-02-09 14:14:02 -080064
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 Line28ae4c2015-03-13 11:37:03 -070083 routingConfigurationService =
84 createMock(RoutingConfigurationService.class);
Jonathan Hart41349e92015-02-09 14:14:02 -080085
Jonathan Hart3930f632015-10-19 12:12:51 -070086 RouteSourceService routeSourceService = createMock(RouteSourceService.class);
87 routeSourceService.start(anyObject(RouteListener.class));
88 routeSourceService.stop();
89 replay(routeSourceService);
Jonathan Hart41349e92015-02-09 14:14:02 -080090
91 fibListener = createMock(FibListener.class);
92
93 router = new Router();
Jonathan Hart66018992015-07-31 11:19:27 -070094 router.coreService = createNiceMock(CoreService.class);
Jonathan Hart41349e92015-02-09 14:14:02 -080095 router.hostService = hostService;
Pingping Line28ae4c2015-03-13 11:37:03 -070096 router.routingConfigurationService = routingConfigurationService;
Jonathan Hart3930f632015-10-19 12:12:51 -070097 router.routeSourceService = routeSourceService;
Jonathan Hart41349e92015-02-09 14:14:02 -080098 router.activate();
99
Pingping Line28ae4c2015-03-13 11:37:03 -0700100 router.addFibListener(fibListener);
101 router.start();
Jonathan Hart41349e92015-02-09 14:14:02 -0800102
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 Line28ae4c2015-03-13 11:37:03 -0700133 reset(routingConfigurationService);
134 expect(routingConfigurationService.isIpPrefixLocal(
135 anyObject(IpPrefix.class))).andReturn(false);
136 replay(routingConfigurationService);
Jonathan Hart41349e92015-02-09 14:14:02 -0800137
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 Line28ae4c2015-03-13 11:37:03 -0700191 reset(routingConfigurationService);
192 expect(routingConfigurationService.isIpPrefixLocal(
193 anyObject(IpPrefix.class))).andReturn(false);
194 replay(routingConfigurationService);
Jonathan Hart41349e92015-02-09 14:14:02 -0800195
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}