blob: 6c98efb437f7000e8b99acaa2ef2f6cbc3a4b9f4 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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
Thomas Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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.
Thomas Vachuska781d18b2014-10-27 10:31:25 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.sdnip;
Pingping3855f312014-10-22 12:50:37 -070017
Jonathan Hart31582d12014-10-22 13:52:41 -070018import static org.easymock.EasyMock.anyObject;
Pingping3855f312014-10-22 12:50:37 -070019import static org.easymock.EasyMock.createMock;
20import static org.easymock.EasyMock.expect;
Jonathan Hart31582d12014-10-22 13:52:41 -070021import static org.easymock.EasyMock.expectLastCall;
Pingping3855f312014-10-22 12:50:37 -070022import static org.easymock.EasyMock.replay;
23import static org.easymock.EasyMock.reset;
24import static org.easymock.EasyMock.verify;
25import static org.junit.Assert.assertEquals;
26import static org.junit.Assert.assertTrue;
27
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -080028import java.util.Collections;
Pingping3855f312014-10-22 12:50:37 -070029import java.util.HashMap;
30import java.util.HashSet;
31import java.util.Map;
32import java.util.Set;
33
34import org.junit.Before;
35import org.junit.Test;
Pavlin Radoslavovd26f57a2014-10-23 17:19:45 -070036import org.onlab.junit.TestUtils;
37import org.onlab.junit.TestUtils.TestUtilsException;
Jonathan Hart51372182014-12-03 21:32:34 -080038import org.onlab.packet.Ethernet;
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -080039import org.onlab.packet.Ip4Address;
40import org.onlab.packet.Ip4Prefix;
Jonathan Hart6cd2f352015-01-13 17:44:45 -080041import org.onlab.packet.IpAddress;
42import org.onlab.packet.IpPrefix;
Jonathan Hart51372182014-12-03 21:32:34 -080043import org.onlab.packet.MacAddress;
44import org.onlab.packet.VlanId;
Brian O'Connorabafb502014-12-02 22:26:20 -080045import org.onosproject.core.ApplicationId;
46import org.onosproject.net.ConnectPoint;
47import org.onosproject.net.DefaultHost;
48import org.onosproject.net.DeviceId;
49import org.onosproject.net.Host;
50import org.onosproject.net.HostId;
51import org.onosproject.net.HostLocation;
52import org.onosproject.net.PortNumber;
53import org.onosproject.net.flow.DefaultTrafficSelector;
54import org.onosproject.net.flow.DefaultTrafficTreatment;
55import org.onosproject.net.flow.TrafficSelector;
56import org.onosproject.net.flow.TrafficTreatment;
57import org.onosproject.net.host.HostListener;
58import org.onosproject.net.host.HostService;
59import org.onosproject.net.host.InterfaceIpAddress;
Jonathan Hart51372182014-12-03 21:32:34 -080060import org.onosproject.net.intent.AbstractIntentTest;
Brian O'Connorabafb502014-12-02 22:26:20 -080061import org.onosproject.net.intent.Intent;
62import org.onosproject.net.intent.IntentOperations;
63import org.onosproject.net.intent.IntentService;
64import org.onosproject.net.intent.MultiPointToSinglePointIntent;
Brian O'Connorabafb502014-12-02 22:26:20 -080065import org.onosproject.net.provider.ProviderId;
66import org.onosproject.sdnip.IntentSynchronizer.IntentKey;
67import org.onosproject.sdnip.config.BgpPeer;
68import org.onosproject.sdnip.config.Interface;
69import org.onosproject.sdnip.config.SdnIpConfigurationService;
Pingping3855f312014-10-22 12:50:37 -070070
71import com.google.common.collect.Sets;
72
73/**
74 * This class tests adding a route, updating a route, deleting a route,
75 * and adding a route whose next hop is the local BGP speaker.
Pingpingfc584672014-10-23 10:51:19 -070076 * <p/>
77 * ARP module answers the MAC address synchronously.
Pingping3855f312014-10-22 12:50:37 -070078 */
Brian O'Connor520c0522014-11-23 23:50:47 -080079public class RouterTest extends AbstractIntentTest {
Pingping3855f312014-10-22 12:50:37 -070080
Jonathan Hart9965d772014-12-02 10:28:34 -080081 private SdnIpConfigurationService sdnIpConfigService;
Pingping3855f312014-10-22 12:50:37 -070082 private InterfaceService interfaceService;
83 private IntentService intentService;
84 private HostService hostService;
85
Jonathan Hart31582d12014-10-22 13:52:41 -070086 private static final ConnectPoint SW1_ETH1 = new ConnectPoint(
87 DeviceId.deviceId("of:0000000000000001"),
88 PortNumber.portNumber(1));
89
90 private static final ConnectPoint SW2_ETH1 = new ConnectPoint(
91 DeviceId.deviceId("of:0000000000000002"),
92 PortNumber.portNumber(1));
93
94 private static final ConnectPoint SW3_ETH1 = new ConnectPoint(
95 DeviceId.deviceId("of:0000000000000003"),
96 PortNumber.portNumber(1));
Pingping3855f312014-10-22 12:50:37 -070097
Jonathan Hart6cd2f352015-01-13 17:44:45 -080098 private static final ConnectPoint SW4_ETH1 = new ConnectPoint(
99 DeviceId.deviceId("of:0000000000000004"),
100 PortNumber.portNumber(1));
101
Pingping3855f312014-10-22 12:50:37 -0700102 private static final ApplicationId APPID = new ApplicationId() {
103 @Override
104 public short id() {
105 return 1;
106 }
107
108 @Override
109 public String name() {
110 return "SDNIP";
111 }
112 };
113
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800114 private IntentSynchronizer intentSynchronizer;
Pingping3855f312014-10-22 12:50:37 -0700115 private Router router;
116
117 @Before
118 public void setUp() throws Exception {
Brian O'Connor520c0522014-11-23 23:50:47 -0800119 super.setUp();
120
Jonathan Hart31582d12014-10-22 13:52:41 -0700121 setUpBgpPeers();
Pingping3855f312014-10-22 12:50:37 -0700122
Jonathan Hart31582d12014-10-22 13:52:41 -0700123 setUpInterfaceService();
124 setUpHostService();
Pingping3855f312014-10-22 12:50:37 -0700125
126 intentService = createMock(IntentService.class);
Pingping3855f312014-10-22 12:50:37 -0700127
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800128 intentSynchronizer = new IntentSynchronizer(APPID, intentService);
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800129 router = new Router(APPID, intentSynchronizer, sdnIpConfigService,
130 interfaceService, hostService);
Pingping3855f312014-10-22 12:50:37 -0700131 }
132
133 /**
134 * Sets up BGP peers in external networks.
Pingping3855f312014-10-22 12:50:37 -0700135 */
Jonathan Hart31582d12014-10-22 13:52:41 -0700136 private void setUpBgpPeers() {
Pingping3855f312014-10-22 12:50:37 -0700137
Jonathan Hart31582d12014-10-22 13:52:41 -0700138 Map<IpAddress, BgpPeer> peers = new HashMap<>();
Pingping3855f312014-10-22 12:50:37 -0700139
140 String peerSw1Eth1 = "192.168.10.1";
Jonathan Hart31582d12014-10-22 13:52:41 -0700141 peers.put(IpAddress.valueOf(peerSw1Eth1),
Pingping3855f312014-10-22 12:50:37 -0700142 new BgpPeer("00:00:00:00:00:00:00:01", 1, peerSw1Eth1));
143
144 // Two BGP peers are connected to switch 2 port 1.
145 String peer1Sw2Eth1 = "192.168.20.1";
Jonathan Hart31582d12014-10-22 13:52:41 -0700146 peers.put(IpAddress.valueOf(peer1Sw2Eth1),
Pingping3855f312014-10-22 12:50:37 -0700147 new BgpPeer("00:00:00:00:00:00:00:02", 1, peer1Sw2Eth1));
148
149 String peer2Sw2Eth1 = "192.168.20.2";
Jonathan Hart31582d12014-10-22 13:52:41 -0700150 peers.put(IpAddress.valueOf(peer2Sw2Eth1),
Pingping3855f312014-10-22 12:50:37 -0700151 new BgpPeer("00:00:00:00:00:00:00:02", 1, peer2Sw2Eth1));
152
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800153 String peer1Sw4Eth1 = "192.168.40.1";
154 peers.put(IpAddress.valueOf(peer1Sw4Eth1),
155 new BgpPeer("00:00:00:00:00:00:00:04", 1, peer1Sw4Eth1));
156
Jonathan Hart9965d772014-12-02 10:28:34 -0800157 sdnIpConfigService = createMock(SdnIpConfigurationService.class);
Jonathan Hart31582d12014-10-22 13:52:41 -0700158 expect(sdnIpConfigService.getBgpPeers()).andReturn(peers).anyTimes();
159 replay(sdnIpConfigService);
160
Pingping3855f312014-10-22 12:50:37 -0700161 }
162
163 /**
164 * Sets up logical interfaces, which emulate the configured interfaces
165 * in SDN-IP application.
Pingping3855f312014-10-22 12:50:37 -0700166 */
Jonathan Hart31582d12014-10-22 13:52:41 -0700167 private void setUpInterfaceService() {
168 interfaceService = createMock(InterfaceService.class);
Pingping3855f312014-10-22 12:50:37 -0700169
Jonathan Hart31582d12014-10-22 13:52:41 -0700170 Set<Interface> interfaces = Sets.newHashSet();
Pingping3855f312014-10-22 12:50:37 -0700171
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700172 InterfaceIpAddress ia1 =
173 new InterfaceIpAddress(IpAddress.valueOf("192.168.10.101"),
174 IpPrefix.valueOf("192.168.10.0/24"));
Jonathan Hart31582d12014-10-22 13:52:41 -0700175 Interface sw1Eth1 = new Interface(SW1_ETH1,
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700176 Sets.newHashSet(ia1),
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800177 MacAddress.valueOf("00:00:00:00:00:01"),
178 VlanId.NONE);
Pingping3855f312014-10-22 12:50:37 -0700179
Jonathan Hart31582d12014-10-22 13:52:41 -0700180 expect(interfaceService.getInterface(SW1_ETH1)).andReturn(sw1Eth1).anyTimes();
181 interfaces.add(sw1Eth1);
Pingping3855f312014-10-22 12:50:37 -0700182
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700183 InterfaceIpAddress ia2 =
184 new InterfaceIpAddress(IpAddress.valueOf("192.168.20.101"),
185 IpPrefix.valueOf("192.168.20.0/24"));
Jonathan Hart31582d12014-10-22 13:52:41 -0700186 Interface sw2Eth1 = new Interface(SW2_ETH1,
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700187 Sets.newHashSet(ia2),
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800188 MacAddress.valueOf("00:00:00:00:00:02"),
189 VlanId.NONE);
Pingping3855f312014-10-22 12:50:37 -0700190
Jonathan Hart31582d12014-10-22 13:52:41 -0700191 expect(interfaceService.getInterface(SW2_ETH1)).andReturn(sw2Eth1).anyTimes();
192 interfaces.add(sw2Eth1);
193
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700194 InterfaceIpAddress ia3 =
195 new InterfaceIpAddress(IpAddress.valueOf("192.168.30.101"),
196 IpPrefix.valueOf("192.168.30.0/24"));
Jonathan Hart31582d12014-10-22 13:52:41 -0700197 Interface sw3Eth1 = new Interface(SW3_ETH1,
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700198 Sets.newHashSet(ia3),
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800199 MacAddress.valueOf("00:00:00:00:00:03"),
200 VlanId.NONE);
Jonathan Hart31582d12014-10-22 13:52:41 -0700201
202 expect(interfaceService.getInterface(SW3_ETH1)).andReturn(sw3Eth1).anyTimes();
203 interfaces.add(sw3Eth1);
204
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800205 InterfaceIpAddress ia4 =
206 new InterfaceIpAddress(IpAddress.valueOf("192.168.40.101"),
207 IpPrefix.valueOf("192.168.40.0/24"));
208 Interface sw4Eth1 = new Interface(SW4_ETH1,
209 Sets.newHashSet(ia4),
210 MacAddress.valueOf("00:00:00:00:00:04"),
211 VlanId.vlanId((short) 1));
212
213 expect(interfaceService.getInterface(SW4_ETH1)).andReturn(sw4Eth1).anyTimes();
214 interfaces.add(sw4Eth1);
215
Jonathan Hart31582d12014-10-22 13:52:41 -0700216 expect(interfaceService.getInterfaces()).andReturn(interfaces).anyTimes();
217
218 replay(interfaceService);
219 }
220
221 /**
222 * Sets up the host service with details of some hosts.
223 */
224 private void setUpHostService() {
225 hostService = createMock(HostService.class);
226
227 hostService.addListener(anyObject(HostListener.class));
228 expectLastCall().anyTimes();
229
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -0700230 IpAddress host1Address = IpAddress.valueOf("192.168.10.1");
Jonathan Hart31582d12014-10-22 13:52:41 -0700231 Host host1 = new DefaultHost(ProviderId.NONE, HostId.NONE,
232 MacAddress.valueOf("00:00:00:00:00:01"), VlanId.NONE,
233 new HostLocation(SW1_ETH1, 1),
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -0700234 Sets.newHashSet(host1Address));
Jonathan Hart31582d12014-10-22 13:52:41 -0700235
236 expect(hostService.getHostsByIp(host1Address))
237 .andReturn(Sets.newHashSet(host1)).anyTimes();
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -0700238 hostService.startMonitoringIp(host1Address);
Jonathan Hart31582d12014-10-22 13:52:41 -0700239 expectLastCall().anyTimes();
240
241
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -0700242 IpAddress host2Address = IpAddress.valueOf("192.168.20.1");
Jonathan Hart31582d12014-10-22 13:52:41 -0700243 Host host2 = new DefaultHost(ProviderId.NONE, HostId.NONE,
244 MacAddress.valueOf("00:00:00:00:00:02"), VlanId.NONE,
245 new HostLocation(SW2_ETH1, 1),
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -0700246 Sets.newHashSet(host2Address));
Jonathan Hart31582d12014-10-22 13:52:41 -0700247
248 expect(hostService.getHostsByIp(host2Address))
249 .andReturn(Sets.newHashSet(host2)).anyTimes();
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -0700250 hostService.startMonitoringIp(host2Address);
Jonathan Hart31582d12014-10-22 13:52:41 -0700251 expectLastCall().anyTimes();
252
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800253 // Next hop on a VLAN
254 IpAddress host3Address = IpAddress.valueOf("192.168.40.1");
255 Host host3 = new DefaultHost(ProviderId.NONE, HostId.NONE,
256 MacAddress.valueOf("00:00:00:00:00:03"), VlanId.vlanId((short) 1),
257 new HostLocation(SW4_ETH1, 1),
258 Sets.newHashSet(host3Address));
259
260 expect(hostService.getHostsByIp(host3Address))
261 .andReturn(Sets.newHashSet(host3)).anyTimes();
262 hostService.startMonitoringIp(host3Address);
263 expectLastCall().anyTimes();
264
Jonathan Hart31582d12014-10-22 13:52:41 -0700265
266 replay(hostService);
Pingping3855f312014-10-22 12:50:37 -0700267 }
268
269 /**
270 * This method tests adding a route entry.
271 */
Pavlin Radoslavov97e8a8b2014-11-24 17:51:28 -0800272 @Test
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800273 public void testRouteAdd() throws TestUtilsException {
Pingping3855f312014-10-22 12:50:37 -0700274 // Construct a route entry
275 RouteEntry routeEntry = new RouteEntry(
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800276 Ip4Prefix.valueOf("1.1.1.0/24"),
277 Ip4Address.valueOf("192.168.10.1"));
Pingping3855f312014-10-22 12:50:37 -0700278
279 // Construct a MultiPointToSinglePointIntent intent
280 TrafficSelector.Builder selectorBuilder =
281 DefaultTrafficSelector.builder();
282 selectorBuilder.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(
283 routeEntry.prefix());
284
285 TrafficTreatment.Builder treatmentBuilder =
286 DefaultTrafficTreatment.builder();
287 treatmentBuilder.setEthDst(MacAddress.valueOf("00:00:00:00:00:01"));
288
289 Set<ConnectPoint> ingressPoints = new HashSet<ConnectPoint>();
Jonathan Hart31582d12014-10-22 13:52:41 -0700290 ingressPoints.add(SW2_ETH1);
291 ingressPoints.add(SW3_ETH1);
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800292 ingressPoints.add(SW4_ETH1);
Pingping3855f312014-10-22 12:50:37 -0700293
294 MultiPointToSinglePointIntent intent =
295 new MultiPointToSinglePointIntent(APPID,
296 selectorBuilder.build(), treatmentBuilder.build(),
Jonathan Hart31582d12014-10-22 13:52:41 -0700297 ingressPoints, SW1_ETH1);
Pingping3855f312014-10-22 12:50:37 -0700298
299 // Set up test expectation
300 reset(intentService);
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800301 // Setup the expected intents
302 IntentOperations.Builder builder = IntentOperations.builder(APPID);
303 builder.addSubmitOperation(intent);
304 intentService.execute(TestIntentServiceHelper.eqExceptId(
305 builder.build()));
Pingping3855f312014-10-22 12:50:37 -0700306 replay(intentService);
307
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800308 // Call the processRouteUpdates() method in Router class
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800309 intentSynchronizer.leaderChanged(true);
310 TestUtils.setField(intentSynchronizer, "isActivatedLeader", true);
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800311 RouteUpdate routeUpdate = new RouteUpdate(RouteUpdate.Type.UPDATE,
312 routeEntry);
313 router.processRouteUpdates(Collections.<RouteUpdate>singletonList(routeUpdate));
Pingping3855f312014-10-22 12:50:37 -0700314
315 // Verify
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800316 assertEquals(router.getRoutes4().size(), 1);
317 assertTrue(router.getRoutes4().contains(routeEntry));
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800318 assertEquals(intentSynchronizer.getRouteIntents().size(), 1);
Pavlin Radoslavov97e8a8b2014-11-24 17:51:28 -0800319 Intent firstIntent =
320 intentSynchronizer.getRouteIntents().iterator().next();
321 IntentKey firstIntentKey = new IntentKey(firstIntent);
322 IntentKey intentKey = new IntentKey(intent);
323 assertTrue(firstIntentKey.equals(intentKey));
Pingping3855f312014-10-22 12:50:37 -0700324 verify(intentService);
325 }
326
327 /**
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800328 * This method tests adding a route entry.
329 */
330 @Test
331 public void testRouteAddWithVlan() throws TestUtilsException {
332 // Construct a route entry
333 RouteEntry routeEntry = new RouteEntry(
334 Ip4Prefix.valueOf("3.3.3.0/24"),
335 Ip4Address.valueOf("192.168.40.1"));
336
337 // Construct a MultiPointToSinglePointIntent intent
338 TrafficSelector.Builder selectorBuilder =
339 DefaultTrafficSelector.builder();
340 selectorBuilder.matchEthType(Ethernet.TYPE_IPV4)
341 .matchIPDst(routeEntry.prefix())
342 .matchVlanId(VlanId.ANY);
343
344 TrafficTreatment.Builder treatmentBuilder =
345 DefaultTrafficTreatment.builder();
346 treatmentBuilder.setEthDst(MacAddress.valueOf("00:00:00:00:00:03"))
347 .setVlanId(VlanId.vlanId((short) 1));
348
349 Set<ConnectPoint> ingressPoints = new HashSet<ConnectPoint>();
350 ingressPoints.add(SW1_ETH1);
351 ingressPoints.add(SW2_ETH1);
352 ingressPoints.add(SW3_ETH1);
353
354 MultiPointToSinglePointIntent intent =
355 new MultiPointToSinglePointIntent(APPID,
356 selectorBuilder.build(), treatmentBuilder.build(),
357 ingressPoints, SW4_ETH1);
358
359 // Set up test expectation
360 reset(intentService);
361 // Setup the expected intents
362 IntentOperations.Builder builder = IntentOperations.builder(APPID);
363 builder.addSubmitOperation(intent);
364 intentService.execute(TestIntentServiceHelper.eqExceptId(
365 builder.build()));
366 replay(intentService);
367
368 // Call the processRouteUpdates() method in Router class
369 intentSynchronizer.leaderChanged(true);
370 TestUtils.setField(intentSynchronizer, "isActivatedLeader", true);
371 RouteUpdate routeUpdate = new RouteUpdate(RouteUpdate.Type.UPDATE,
372 routeEntry);
373 router.processRouteUpdates(Collections.<RouteUpdate>singletonList(routeUpdate));
374
375 // Verify
376 assertEquals(router.getRoutes4().size(), 1);
377 assertTrue(router.getRoutes4().contains(routeEntry));
378 assertEquals(intentSynchronizer.getRouteIntents().size(), 1);
379 Intent firstIntent =
380 intentSynchronizer.getRouteIntents().iterator().next();
381 IntentKey firstIntentKey = new IntentKey(firstIntent);
382 IntentKey intentKey = new IntentKey(intent);
383 assertTrue(firstIntentKey.equals(intentKey));
384 verify(intentService);
385 }
386
387 /**
Pingping3855f312014-10-22 12:50:37 -0700388 * This method tests updating a route entry.
389 *
390 * @throws TestUtilsException
391 */
Pavlin Radoslavov97e8a8b2014-11-24 17:51:28 -0800392 @Test
Pingping3855f312014-10-22 12:50:37 -0700393 public void testRouteUpdate() throws TestUtilsException {
Pingping3855f312014-10-22 12:50:37 -0700394 // Firstly add a route
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800395 testRouteAdd();
396
397 Intent addedIntent =
398 intentSynchronizer.getRouteIntents().iterator().next();
Pingping3855f312014-10-22 12:50:37 -0700399
Pingping3855f312014-10-22 12:50:37 -0700400 // Start to construct a new route entry and new intent
401 RouteEntry routeEntryUpdate = new RouteEntry(
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800402 Ip4Prefix.valueOf("1.1.1.0/24"),
403 Ip4Address.valueOf("192.168.20.1"));
Pingping3855f312014-10-22 12:50:37 -0700404
405 // Construct a new MultiPointToSinglePointIntent intent
406 TrafficSelector.Builder selectorBuilderNew =
407 DefaultTrafficSelector.builder();
408 selectorBuilderNew.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(
409 routeEntryUpdate.prefix());
410
411 TrafficTreatment.Builder treatmentBuilderNew =
412 DefaultTrafficTreatment.builder();
413 treatmentBuilderNew.setEthDst(MacAddress.valueOf("00:00:00:00:00:02"));
414
Pingping3855f312014-10-22 12:50:37 -0700415
416 Set<ConnectPoint> ingressPointsNew = new HashSet<ConnectPoint>();
Jonathan Hart31582d12014-10-22 13:52:41 -0700417 ingressPointsNew.add(SW1_ETH1);
418 ingressPointsNew.add(SW3_ETH1);
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800419 ingressPointsNew.add(SW4_ETH1);
Pingping3855f312014-10-22 12:50:37 -0700420
421 MultiPointToSinglePointIntent intentNew =
422 new MultiPointToSinglePointIntent(APPID,
423 selectorBuilderNew.build(),
424 treatmentBuilderNew.build(),
Jonathan Hart31582d12014-10-22 13:52:41 -0700425 ingressPointsNew, SW2_ETH1);
Pingping3855f312014-10-22 12:50:37 -0700426
427 // Set up test expectation
428 reset(intentService);
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800429 // Setup the expected intents
430 IntentOperations.Builder builder = IntentOperations.builder(APPID);
431 builder.addWithdrawOperation(addedIntent.id());
432 intentService.execute(TestIntentServiceHelper.eqExceptId(
433 builder.build()));
434 builder = IntentOperations.builder(APPID);
435 builder.addSubmitOperation(intentNew);
436 intentService.execute(TestIntentServiceHelper.eqExceptId(
437 builder.build()));
Pingping3855f312014-10-22 12:50:37 -0700438 replay(intentService);
439
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800440 // Call the processRouteUpdates() method in Router class
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800441 intentSynchronizer.leaderChanged(true);
442 TestUtils.setField(intentSynchronizer, "isActivatedLeader", true);
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800443 RouteUpdate routeUpdate = new RouteUpdate(RouteUpdate.Type.UPDATE,
444 routeEntryUpdate);
445 router.processRouteUpdates(Collections.<RouteUpdate>singletonList(routeUpdate));
Pingping3855f312014-10-22 12:50:37 -0700446
447 // Verify
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800448 assertEquals(router.getRoutes4().size(), 1);
449 assertTrue(router.getRoutes4().contains(routeEntryUpdate));
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800450 assertEquals(intentSynchronizer.getRouteIntents().size(), 1);
Pavlin Radoslavov97e8a8b2014-11-24 17:51:28 -0800451 Intent firstIntent =
452 intentSynchronizer.getRouteIntents().iterator().next();
453 IntentKey firstIntentKey = new IntentKey(firstIntent);
454 IntentKey intentNewKey = new IntentKey(intentNew);
455 assertTrue(firstIntentKey.equals(intentNewKey));
Pingping3855f312014-10-22 12:50:37 -0700456 verify(intentService);
457 }
458
459 /**
460 * This method tests deleting a route entry.
461 */
Pavlin Radoslavov97e8a8b2014-11-24 17:51:28 -0800462 @Test
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800463 public void testRouteDelete() throws TestUtilsException {
Pingping3855f312014-10-22 12:50:37 -0700464 // Firstly add a route
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800465 testRouteAdd();
466
467 Intent addedIntent =
468 intentSynchronizer.getRouteIntents().iterator().next();
Pingping3855f312014-10-22 12:50:37 -0700469
470 // Construct the existing route entry
471 RouteEntry routeEntry = new RouteEntry(
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800472 Ip4Prefix.valueOf("1.1.1.0/24"),
473 Ip4Address.valueOf("192.168.10.1"));
Pingping3855f312014-10-22 12:50:37 -0700474
Pingping3855f312014-10-22 12:50:37 -0700475 // Set up expectation
476 reset(intentService);
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800477 // Setup the expected intents
478 IntentOperations.Builder builder = IntentOperations.builder(APPID);
479 builder.addWithdrawOperation(addedIntent.id());
480 intentService.execute(TestIntentServiceHelper.eqExceptId(
481 builder.build()));
Pingping3855f312014-10-22 12:50:37 -0700482 replay(intentService);
483
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800484 // Call the processRouteUpdates() method in Router class
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800485 intentSynchronizer.leaderChanged(true);
486 TestUtils.setField(intentSynchronizer, "isActivatedLeader", true);
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800487 RouteUpdate routeUpdate = new RouteUpdate(RouteUpdate.Type.DELETE,
488 routeEntry);
489 router.processRouteUpdates(Collections.<RouteUpdate>singletonList(routeUpdate));
Pingping3855f312014-10-22 12:50:37 -0700490
491 // Verify
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800492 assertEquals(router.getRoutes4().size(), 0);
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800493 assertEquals(intentSynchronizer.getRouteIntents().size(), 0);
Pingping3855f312014-10-22 12:50:37 -0700494 verify(intentService);
495 }
496
497 /**
498 * This method tests when the next hop of a route is the local BGP speaker.
499 *
500 * @throws TestUtilsException
501 */
502 @Test
503 public void testLocalRouteAdd() throws TestUtilsException {
Pingping3855f312014-10-22 12:50:37 -0700504 // Construct a route entry, the next hop is the local BGP speaker
505 RouteEntry routeEntry = new RouteEntry(
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800506 Ip4Prefix.valueOf("1.1.1.0/24"),
507 Ip4Address.valueOf("0.0.0.0"));
Pingping3855f312014-10-22 12:50:37 -0700508
509 // Reset intentService to check whether the submit method is called
510 reset(intentService);
511 replay(intentService);
512
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800513 // Call the processRouteUpdates() method in Router class
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800514 intentSynchronizer.leaderChanged(true);
515 TestUtils.setField(intentSynchronizer, "isActivatedLeader", true);
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800516 RouteUpdate routeUpdate = new RouteUpdate(RouteUpdate.Type.UPDATE,
517 routeEntry);
518 router.processRouteUpdates(Collections.<RouteUpdate>singletonList(routeUpdate));
Pingping3855f312014-10-22 12:50:37 -0700519
520 // Verify
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800521 assertEquals(router.getRoutes4().size(), 1);
522 assertTrue(router.getRoutes4().contains(routeEntry));
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800523 assertEquals(intentSynchronizer.getRouteIntents().size(), 0);
Pingping3855f312014-10-22 12:50:37 -0700524 verify(intentService);
525 }
526}