blob: a7b4cced2876d6e19104939118768446755eb10d [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 Hart552e31f2015-02-06 11:11:59 -080018import com.google.common.collect.Sets;
Pingping3855f312014-10-22 12:50:37 -070019import org.junit.Before;
20import org.junit.Test;
Pavlin Radoslavovd26f57a2014-10-23 17:19:45 -070021import org.onlab.junit.TestUtils;
22import org.onlab.junit.TestUtils.TestUtilsException;
Jonathan Hart51372182014-12-03 21:32:34 -080023import org.onlab.packet.Ethernet;
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -080024import org.onlab.packet.Ip4Address;
25import org.onlab.packet.Ip4Prefix;
Jonathan Hart6cd2f352015-01-13 17:44:45 -080026import org.onlab.packet.IpAddress;
27import org.onlab.packet.IpPrefix;
Jonathan Hart51372182014-12-03 21:32:34 -080028import org.onlab.packet.MacAddress;
29import org.onlab.packet.VlanId;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.core.ApplicationId;
31import org.onosproject.net.ConnectPoint;
32import org.onosproject.net.DefaultHost;
33import org.onosproject.net.DeviceId;
34import org.onosproject.net.Host;
35import org.onosproject.net.HostId;
36import org.onosproject.net.HostLocation;
37import org.onosproject.net.PortNumber;
38import org.onosproject.net.flow.DefaultTrafficSelector;
39import org.onosproject.net.flow.DefaultTrafficTreatment;
40import org.onosproject.net.flow.TrafficSelector;
41import org.onosproject.net.flow.TrafficTreatment;
42import org.onosproject.net.host.HostListener;
43import org.onosproject.net.host.HostService;
44import org.onosproject.net.host.InterfaceIpAddress;
Jonathan Hart51372182014-12-03 21:32:34 -080045import org.onosproject.net.intent.AbstractIntentTest;
Brian O'Connorabafb502014-12-02 22:26:20 -080046import org.onosproject.net.intent.Intent;
47import org.onosproject.net.intent.IntentOperations;
48import org.onosproject.net.intent.IntentService;
49import org.onosproject.net.intent.MultiPointToSinglePointIntent;
Brian O'Connorabafb502014-12-02 22:26:20 -080050import org.onosproject.net.provider.ProviderId;
51import org.onosproject.sdnip.IntentSynchronizer.IntentKey;
52import org.onosproject.sdnip.config.BgpPeer;
53import org.onosproject.sdnip.config.Interface;
54import org.onosproject.sdnip.config.SdnIpConfigurationService;
Pingping3855f312014-10-22 12:50:37 -070055
Jonathan Hart552e31f2015-02-06 11:11:59 -080056import java.util.Collections;
57import java.util.HashMap;
58import java.util.HashSet;
59import java.util.Map;
60import java.util.Set;
61
62import static org.easymock.EasyMock.*;
63import static org.junit.Assert.assertEquals;
64import static org.junit.Assert.assertTrue;
Pingping3855f312014-10-22 12:50:37 -070065
66/**
67 * This class tests adding a route, updating a route, deleting a route,
68 * and adding a route whose next hop is the local BGP speaker.
Pingpingfc584672014-10-23 10:51:19 -070069 * <p/>
70 * ARP module answers the MAC address synchronously.
Pingping3855f312014-10-22 12:50:37 -070071 */
Brian O'Connor520c0522014-11-23 23:50:47 -080072public class RouterTest extends AbstractIntentTest {
Pingping3855f312014-10-22 12:50:37 -070073
Jonathan Hart9965d772014-12-02 10:28:34 -080074 private SdnIpConfigurationService sdnIpConfigService;
Pingping3855f312014-10-22 12:50:37 -070075 private InterfaceService interfaceService;
76 private IntentService intentService;
77 private HostService hostService;
78
Jonathan Hart31582d12014-10-22 13:52:41 -070079 private static final ConnectPoint SW1_ETH1 = new ConnectPoint(
80 DeviceId.deviceId("of:0000000000000001"),
81 PortNumber.portNumber(1));
82
83 private static final ConnectPoint SW2_ETH1 = new ConnectPoint(
84 DeviceId.deviceId("of:0000000000000002"),
85 PortNumber.portNumber(1));
86
87 private static final ConnectPoint SW3_ETH1 = new ConnectPoint(
88 DeviceId.deviceId("of:0000000000000003"),
89 PortNumber.portNumber(1));
Pingping3855f312014-10-22 12:50:37 -070090
Jonathan Hart6cd2f352015-01-13 17:44:45 -080091 private static final ConnectPoint SW4_ETH1 = new ConnectPoint(
92 DeviceId.deviceId("of:0000000000000004"),
93 PortNumber.portNumber(1));
94
Pingping3855f312014-10-22 12:50:37 -070095 private static final ApplicationId APPID = new ApplicationId() {
96 @Override
97 public short id() {
98 return 1;
99 }
100
101 @Override
102 public String name() {
103 return "SDNIP";
104 }
105 };
106
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800107 private IntentSynchronizer intentSynchronizer;
Pingping3855f312014-10-22 12:50:37 -0700108 private Router router;
109
110 @Before
111 public void setUp() throws Exception {
Brian O'Connor520c0522014-11-23 23:50:47 -0800112 super.setUp();
113
Jonathan Hart31582d12014-10-22 13:52:41 -0700114 setUpBgpPeers();
Pingping3855f312014-10-22 12:50:37 -0700115
Jonathan Hart31582d12014-10-22 13:52:41 -0700116 setUpInterfaceService();
117 setUpHostService();
Pingping3855f312014-10-22 12:50:37 -0700118
119 intentService = createMock(IntentService.class);
Pingping3855f312014-10-22 12:50:37 -0700120
Jonathan Hart552e31f2015-02-06 11:11:59 -0800121 intentSynchronizer = new IntentSynchronizer(APPID, intentService,
122 sdnIpConfigService,
123 interfaceService);
124 router = new Router(intentSynchronizer, hostService);
Pingping3855f312014-10-22 12:50:37 -0700125 }
126
127 /**
128 * Sets up BGP peers in external networks.
Pingping3855f312014-10-22 12:50:37 -0700129 */
Jonathan Hart31582d12014-10-22 13:52:41 -0700130 private void setUpBgpPeers() {
Pingping3855f312014-10-22 12:50:37 -0700131
Jonathan Hart31582d12014-10-22 13:52:41 -0700132 Map<IpAddress, BgpPeer> peers = new HashMap<>();
Pingping3855f312014-10-22 12:50:37 -0700133
134 String peerSw1Eth1 = "192.168.10.1";
Jonathan Hart31582d12014-10-22 13:52:41 -0700135 peers.put(IpAddress.valueOf(peerSw1Eth1),
Pingping3855f312014-10-22 12:50:37 -0700136 new BgpPeer("00:00:00:00:00:00:00:01", 1, peerSw1Eth1));
137
138 // Two BGP peers are connected to switch 2 port 1.
139 String peer1Sw2Eth1 = "192.168.20.1";
Jonathan Hart31582d12014-10-22 13:52:41 -0700140 peers.put(IpAddress.valueOf(peer1Sw2Eth1),
Pingping3855f312014-10-22 12:50:37 -0700141 new BgpPeer("00:00:00:00:00:00:00:02", 1, peer1Sw2Eth1));
142
143 String peer2Sw2Eth1 = "192.168.20.2";
Jonathan Hart31582d12014-10-22 13:52:41 -0700144 peers.put(IpAddress.valueOf(peer2Sw2Eth1),
Pingping3855f312014-10-22 12:50:37 -0700145 new BgpPeer("00:00:00:00:00:00:00:02", 1, peer2Sw2Eth1));
146
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800147 String peer1Sw4Eth1 = "192.168.40.1";
148 peers.put(IpAddress.valueOf(peer1Sw4Eth1),
149 new BgpPeer("00:00:00:00:00:00:00:04", 1, peer1Sw4Eth1));
150
Jonathan Hart9965d772014-12-02 10:28:34 -0800151 sdnIpConfigService = createMock(SdnIpConfigurationService.class);
Jonathan Hart31582d12014-10-22 13:52:41 -0700152 expect(sdnIpConfigService.getBgpPeers()).andReturn(peers).anyTimes();
153 replay(sdnIpConfigService);
154
Pingping3855f312014-10-22 12:50:37 -0700155 }
156
157 /**
158 * Sets up logical interfaces, which emulate the configured interfaces
159 * in SDN-IP application.
Pingping3855f312014-10-22 12:50:37 -0700160 */
Jonathan Hart31582d12014-10-22 13:52:41 -0700161 private void setUpInterfaceService() {
162 interfaceService = createMock(InterfaceService.class);
Pingping3855f312014-10-22 12:50:37 -0700163
Jonathan Hart31582d12014-10-22 13:52:41 -0700164 Set<Interface> interfaces = Sets.newHashSet();
Pingping3855f312014-10-22 12:50:37 -0700165
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700166 InterfaceIpAddress ia1 =
167 new InterfaceIpAddress(IpAddress.valueOf("192.168.10.101"),
168 IpPrefix.valueOf("192.168.10.0/24"));
Jonathan Hart31582d12014-10-22 13:52:41 -0700169 Interface sw1Eth1 = new Interface(SW1_ETH1,
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700170 Sets.newHashSet(ia1),
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800171 MacAddress.valueOf("00:00:00:00:00:01"),
172 VlanId.NONE);
Pingping3855f312014-10-22 12:50:37 -0700173
Jonathan Hart31582d12014-10-22 13:52:41 -0700174 expect(interfaceService.getInterface(SW1_ETH1)).andReturn(sw1Eth1).anyTimes();
175 interfaces.add(sw1Eth1);
Pingping3855f312014-10-22 12:50:37 -0700176
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700177 InterfaceIpAddress ia2 =
178 new InterfaceIpAddress(IpAddress.valueOf("192.168.20.101"),
179 IpPrefix.valueOf("192.168.20.0/24"));
Jonathan Hart31582d12014-10-22 13:52:41 -0700180 Interface sw2Eth1 = new Interface(SW2_ETH1,
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700181 Sets.newHashSet(ia2),
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800182 MacAddress.valueOf("00:00:00:00:00:02"),
183 VlanId.NONE);
Pingping3855f312014-10-22 12:50:37 -0700184
Jonathan Hart31582d12014-10-22 13:52:41 -0700185 expect(interfaceService.getInterface(SW2_ETH1)).andReturn(sw2Eth1).anyTimes();
186 interfaces.add(sw2Eth1);
187
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700188 InterfaceIpAddress ia3 =
189 new InterfaceIpAddress(IpAddress.valueOf("192.168.30.101"),
190 IpPrefix.valueOf("192.168.30.0/24"));
Jonathan Hart31582d12014-10-22 13:52:41 -0700191 Interface sw3Eth1 = new Interface(SW3_ETH1,
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700192 Sets.newHashSet(ia3),
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800193 MacAddress.valueOf("00:00:00:00:00:03"),
194 VlanId.NONE);
Jonathan Hart31582d12014-10-22 13:52:41 -0700195
196 expect(interfaceService.getInterface(SW3_ETH1)).andReturn(sw3Eth1).anyTimes();
197 interfaces.add(sw3Eth1);
198
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800199 InterfaceIpAddress ia4 =
200 new InterfaceIpAddress(IpAddress.valueOf("192.168.40.101"),
201 IpPrefix.valueOf("192.168.40.0/24"));
202 Interface sw4Eth1 = new Interface(SW4_ETH1,
203 Sets.newHashSet(ia4),
204 MacAddress.valueOf("00:00:00:00:00:04"),
205 VlanId.vlanId((short) 1));
206
207 expect(interfaceService.getInterface(SW4_ETH1)).andReturn(sw4Eth1).anyTimes();
208 interfaces.add(sw4Eth1);
209
Jonathan Hart31582d12014-10-22 13:52:41 -0700210 expect(interfaceService.getInterfaces()).andReturn(interfaces).anyTimes();
211
212 replay(interfaceService);
213 }
214
215 /**
216 * Sets up the host service with details of some hosts.
217 */
218 private void setUpHostService() {
219 hostService = createMock(HostService.class);
220
221 hostService.addListener(anyObject(HostListener.class));
222 expectLastCall().anyTimes();
223
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -0700224 IpAddress host1Address = IpAddress.valueOf("192.168.10.1");
Jonathan Hart31582d12014-10-22 13:52:41 -0700225 Host host1 = new DefaultHost(ProviderId.NONE, HostId.NONE,
226 MacAddress.valueOf("00:00:00:00:00:01"), VlanId.NONE,
227 new HostLocation(SW1_ETH1, 1),
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -0700228 Sets.newHashSet(host1Address));
Jonathan Hart31582d12014-10-22 13:52:41 -0700229
230 expect(hostService.getHostsByIp(host1Address))
231 .andReturn(Sets.newHashSet(host1)).anyTimes();
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -0700232 hostService.startMonitoringIp(host1Address);
Jonathan Hart31582d12014-10-22 13:52:41 -0700233 expectLastCall().anyTimes();
234
235
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -0700236 IpAddress host2Address = IpAddress.valueOf("192.168.20.1");
Jonathan Hart31582d12014-10-22 13:52:41 -0700237 Host host2 = new DefaultHost(ProviderId.NONE, HostId.NONE,
238 MacAddress.valueOf("00:00:00:00:00:02"), VlanId.NONE,
239 new HostLocation(SW2_ETH1, 1),
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -0700240 Sets.newHashSet(host2Address));
Jonathan Hart31582d12014-10-22 13:52:41 -0700241
242 expect(hostService.getHostsByIp(host2Address))
243 .andReturn(Sets.newHashSet(host2)).anyTimes();
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -0700244 hostService.startMonitoringIp(host2Address);
Jonathan Hart31582d12014-10-22 13:52:41 -0700245 expectLastCall().anyTimes();
246
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800247 // Next hop on a VLAN
248 IpAddress host3Address = IpAddress.valueOf("192.168.40.1");
249 Host host3 = new DefaultHost(ProviderId.NONE, HostId.NONE,
250 MacAddress.valueOf("00:00:00:00:00:03"), VlanId.vlanId((short) 1),
251 new HostLocation(SW4_ETH1, 1),
252 Sets.newHashSet(host3Address));
253
254 expect(hostService.getHostsByIp(host3Address))
255 .andReturn(Sets.newHashSet(host3)).anyTimes();
256 hostService.startMonitoringIp(host3Address);
257 expectLastCall().anyTimes();
258
Jonathan Hart31582d12014-10-22 13:52:41 -0700259
260 replay(hostService);
Pingping3855f312014-10-22 12:50:37 -0700261 }
262
263 /**
264 * This method tests adding a route entry.
265 */
Pavlin Radoslavov97e8a8b2014-11-24 17:51:28 -0800266 @Test
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800267 public void testRouteAdd() throws TestUtilsException {
Pingping3855f312014-10-22 12:50:37 -0700268 // Construct a route entry
269 RouteEntry routeEntry = new RouteEntry(
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800270 Ip4Prefix.valueOf("1.1.1.0/24"),
271 Ip4Address.valueOf("192.168.10.1"));
Pingping3855f312014-10-22 12:50:37 -0700272
273 // Construct a MultiPointToSinglePointIntent intent
274 TrafficSelector.Builder selectorBuilder =
275 DefaultTrafficSelector.builder();
276 selectorBuilder.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(
277 routeEntry.prefix());
278
279 TrafficTreatment.Builder treatmentBuilder =
280 DefaultTrafficTreatment.builder();
281 treatmentBuilder.setEthDst(MacAddress.valueOf("00:00:00:00:00:01"));
282
283 Set<ConnectPoint> ingressPoints = new HashSet<ConnectPoint>();
Jonathan Hart31582d12014-10-22 13:52:41 -0700284 ingressPoints.add(SW2_ETH1);
285 ingressPoints.add(SW3_ETH1);
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800286 ingressPoints.add(SW4_ETH1);
Pingping3855f312014-10-22 12:50:37 -0700287
288 MultiPointToSinglePointIntent intent =
289 new MultiPointToSinglePointIntent(APPID,
290 selectorBuilder.build(), treatmentBuilder.build(),
Jonathan Hart31582d12014-10-22 13:52:41 -0700291 ingressPoints, SW1_ETH1);
Pingping3855f312014-10-22 12:50:37 -0700292
293 // Set up test expectation
294 reset(intentService);
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800295 // Setup the expected intents
296 IntentOperations.Builder builder = IntentOperations.builder(APPID);
297 builder.addSubmitOperation(intent);
298 intentService.execute(TestIntentServiceHelper.eqExceptId(
299 builder.build()));
Pingping3855f312014-10-22 12:50:37 -0700300 replay(intentService);
301
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800302 // Call the processRouteUpdates() method in Router class
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800303 intentSynchronizer.leaderChanged(true);
304 TestUtils.setField(intentSynchronizer, "isActivatedLeader", true);
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800305 RouteUpdate routeUpdate = new RouteUpdate(RouteUpdate.Type.UPDATE,
306 routeEntry);
307 router.processRouteUpdates(Collections.<RouteUpdate>singletonList(routeUpdate));
Pingping3855f312014-10-22 12:50:37 -0700308
309 // Verify
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800310 assertEquals(router.getRoutes4().size(), 1);
311 assertTrue(router.getRoutes4().contains(routeEntry));
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800312 assertEquals(intentSynchronizer.getRouteIntents().size(), 1);
Pavlin Radoslavov97e8a8b2014-11-24 17:51:28 -0800313 Intent firstIntent =
314 intentSynchronizer.getRouteIntents().iterator().next();
315 IntentKey firstIntentKey = new IntentKey(firstIntent);
316 IntentKey intentKey = new IntentKey(intent);
317 assertTrue(firstIntentKey.equals(intentKey));
Pingping3855f312014-10-22 12:50:37 -0700318 verify(intentService);
319 }
320
321 /**
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800322 * This method tests adding a route entry.
323 */
324 @Test
325 public void testRouteAddWithVlan() throws TestUtilsException {
326 // Construct a route entry
327 RouteEntry routeEntry = new RouteEntry(
328 Ip4Prefix.valueOf("3.3.3.0/24"),
329 Ip4Address.valueOf("192.168.40.1"));
330
331 // Construct a MultiPointToSinglePointIntent intent
332 TrafficSelector.Builder selectorBuilder =
333 DefaultTrafficSelector.builder();
334 selectorBuilder.matchEthType(Ethernet.TYPE_IPV4)
335 .matchIPDst(routeEntry.prefix())
336 .matchVlanId(VlanId.ANY);
337
338 TrafficTreatment.Builder treatmentBuilder =
339 DefaultTrafficTreatment.builder();
340 treatmentBuilder.setEthDst(MacAddress.valueOf("00:00:00:00:00:03"))
341 .setVlanId(VlanId.vlanId((short) 1));
342
343 Set<ConnectPoint> ingressPoints = new HashSet<ConnectPoint>();
344 ingressPoints.add(SW1_ETH1);
345 ingressPoints.add(SW2_ETH1);
346 ingressPoints.add(SW3_ETH1);
347
348 MultiPointToSinglePointIntent intent =
349 new MultiPointToSinglePointIntent(APPID,
350 selectorBuilder.build(), treatmentBuilder.build(),
351 ingressPoints, SW4_ETH1);
352
353 // Set up test expectation
354 reset(intentService);
355 // Setup the expected intents
356 IntentOperations.Builder builder = IntentOperations.builder(APPID);
357 builder.addSubmitOperation(intent);
358 intentService.execute(TestIntentServiceHelper.eqExceptId(
359 builder.build()));
360 replay(intentService);
361
362 // Call the processRouteUpdates() method in Router class
363 intentSynchronizer.leaderChanged(true);
364 TestUtils.setField(intentSynchronizer, "isActivatedLeader", true);
365 RouteUpdate routeUpdate = new RouteUpdate(RouteUpdate.Type.UPDATE,
366 routeEntry);
367 router.processRouteUpdates(Collections.<RouteUpdate>singletonList(routeUpdate));
368
369 // Verify
370 assertEquals(router.getRoutes4().size(), 1);
371 assertTrue(router.getRoutes4().contains(routeEntry));
372 assertEquals(intentSynchronizer.getRouteIntents().size(), 1);
373 Intent firstIntent =
374 intentSynchronizer.getRouteIntents().iterator().next();
375 IntentKey firstIntentKey = new IntentKey(firstIntent);
376 IntentKey intentKey = new IntentKey(intent);
377 assertTrue(firstIntentKey.equals(intentKey));
378 verify(intentService);
379 }
380
381 /**
Pingping3855f312014-10-22 12:50:37 -0700382 * This method tests updating a route entry.
383 *
384 * @throws TestUtilsException
385 */
Pavlin Radoslavov97e8a8b2014-11-24 17:51:28 -0800386 @Test
Pingping3855f312014-10-22 12:50:37 -0700387 public void testRouteUpdate() throws TestUtilsException {
Pingping3855f312014-10-22 12:50:37 -0700388 // Firstly add a route
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800389 testRouteAdd();
390
391 Intent addedIntent =
392 intentSynchronizer.getRouteIntents().iterator().next();
Pingping3855f312014-10-22 12:50:37 -0700393
Pingping3855f312014-10-22 12:50:37 -0700394 // Start to construct a new route entry and new intent
395 RouteEntry routeEntryUpdate = new RouteEntry(
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800396 Ip4Prefix.valueOf("1.1.1.0/24"),
397 Ip4Address.valueOf("192.168.20.1"));
Pingping3855f312014-10-22 12:50:37 -0700398
399 // Construct a new MultiPointToSinglePointIntent intent
400 TrafficSelector.Builder selectorBuilderNew =
401 DefaultTrafficSelector.builder();
402 selectorBuilderNew.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(
403 routeEntryUpdate.prefix());
404
405 TrafficTreatment.Builder treatmentBuilderNew =
406 DefaultTrafficTreatment.builder();
407 treatmentBuilderNew.setEthDst(MacAddress.valueOf("00:00:00:00:00:02"));
408
Pingping3855f312014-10-22 12:50:37 -0700409
410 Set<ConnectPoint> ingressPointsNew = new HashSet<ConnectPoint>();
Jonathan Hart31582d12014-10-22 13:52:41 -0700411 ingressPointsNew.add(SW1_ETH1);
412 ingressPointsNew.add(SW3_ETH1);
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800413 ingressPointsNew.add(SW4_ETH1);
Pingping3855f312014-10-22 12:50:37 -0700414
415 MultiPointToSinglePointIntent intentNew =
416 new MultiPointToSinglePointIntent(APPID,
417 selectorBuilderNew.build(),
418 treatmentBuilderNew.build(),
Jonathan Hart31582d12014-10-22 13:52:41 -0700419 ingressPointsNew, SW2_ETH1);
Pingping3855f312014-10-22 12:50:37 -0700420
421 // Set up test expectation
422 reset(intentService);
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800423 // Setup the expected intents
424 IntentOperations.Builder builder = IntentOperations.builder(APPID);
425 builder.addWithdrawOperation(addedIntent.id());
426 intentService.execute(TestIntentServiceHelper.eqExceptId(
427 builder.build()));
428 builder = IntentOperations.builder(APPID);
429 builder.addSubmitOperation(intentNew);
430 intentService.execute(TestIntentServiceHelper.eqExceptId(
431 builder.build()));
Pingping3855f312014-10-22 12:50:37 -0700432 replay(intentService);
433
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800434 // Call the processRouteUpdates() method in Router class
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800435 intentSynchronizer.leaderChanged(true);
436 TestUtils.setField(intentSynchronizer, "isActivatedLeader", true);
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800437 RouteUpdate routeUpdate = new RouteUpdate(RouteUpdate.Type.UPDATE,
438 routeEntryUpdate);
439 router.processRouteUpdates(Collections.<RouteUpdate>singletonList(routeUpdate));
Pingping3855f312014-10-22 12:50:37 -0700440
441 // Verify
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800442 assertEquals(router.getRoutes4().size(), 1);
443 assertTrue(router.getRoutes4().contains(routeEntryUpdate));
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800444 assertEquals(intentSynchronizer.getRouteIntents().size(), 1);
Pavlin Radoslavov97e8a8b2014-11-24 17:51:28 -0800445 Intent firstIntent =
446 intentSynchronizer.getRouteIntents().iterator().next();
447 IntentKey firstIntentKey = new IntentKey(firstIntent);
448 IntentKey intentNewKey = new IntentKey(intentNew);
449 assertTrue(firstIntentKey.equals(intentNewKey));
Pingping3855f312014-10-22 12:50:37 -0700450 verify(intentService);
451 }
452
453 /**
454 * This method tests deleting a route entry.
455 */
Pavlin Radoslavov97e8a8b2014-11-24 17:51:28 -0800456 @Test
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800457 public void testRouteDelete() throws TestUtilsException {
Pingping3855f312014-10-22 12:50:37 -0700458 // Firstly add a route
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800459 testRouteAdd();
460
461 Intent addedIntent =
462 intentSynchronizer.getRouteIntents().iterator().next();
Pingping3855f312014-10-22 12:50:37 -0700463
464 // Construct the existing route entry
465 RouteEntry routeEntry = new RouteEntry(
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800466 Ip4Prefix.valueOf("1.1.1.0/24"),
467 Ip4Address.valueOf("192.168.10.1"));
Pingping3855f312014-10-22 12:50:37 -0700468
Pingping3855f312014-10-22 12:50:37 -0700469 // Set up expectation
470 reset(intentService);
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800471 // Setup the expected intents
472 IntentOperations.Builder builder = IntentOperations.builder(APPID);
473 builder.addWithdrawOperation(addedIntent.id());
474 intentService.execute(TestIntentServiceHelper.eqExceptId(
475 builder.build()));
Pingping3855f312014-10-22 12:50:37 -0700476 replay(intentService);
477
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800478 // Call the processRouteUpdates() method in Router class
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800479 intentSynchronizer.leaderChanged(true);
480 TestUtils.setField(intentSynchronizer, "isActivatedLeader", true);
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800481 RouteUpdate routeUpdate = new RouteUpdate(RouteUpdate.Type.DELETE,
482 routeEntry);
483 router.processRouteUpdates(Collections.<RouteUpdate>singletonList(routeUpdate));
Pingping3855f312014-10-22 12:50:37 -0700484
485 // Verify
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800486 assertEquals(router.getRoutes4().size(), 0);
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800487 assertEquals(intentSynchronizer.getRouteIntents().size(), 0);
Pingping3855f312014-10-22 12:50:37 -0700488 verify(intentService);
489 }
490
491 /**
492 * This method tests when the next hop of a route is the local BGP speaker.
493 *
494 * @throws TestUtilsException
495 */
496 @Test
497 public void testLocalRouteAdd() throws TestUtilsException {
Pingping3855f312014-10-22 12:50:37 -0700498 // Construct a route entry, the next hop is the local BGP speaker
499 RouteEntry routeEntry = new RouteEntry(
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800500 Ip4Prefix.valueOf("1.1.1.0/24"),
501 Ip4Address.valueOf("0.0.0.0"));
Pingping3855f312014-10-22 12:50:37 -0700502
503 // Reset intentService to check whether the submit method is called
504 reset(intentService);
505 replay(intentService);
506
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800507 // Call the processRouteUpdates() method in Router class
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800508 intentSynchronizer.leaderChanged(true);
509 TestUtils.setField(intentSynchronizer, "isActivatedLeader", true);
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800510 RouteUpdate routeUpdate = new RouteUpdate(RouteUpdate.Type.UPDATE,
511 routeEntry);
512 router.processRouteUpdates(Collections.<RouteUpdate>singletonList(routeUpdate));
Pingping3855f312014-10-22 12:50:37 -0700513
514 // Verify
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800515 assertEquals(router.getRoutes4().size(), 1);
516 assertTrue(router.getRoutes4().contains(routeEntry));
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800517 assertEquals(intentSynchronizer.getRouteIntents().size(), 0);
Pingping3855f312014-10-22 12:50:37 -0700518 verify(intentService);
519 }
520}