blob: d13aa2a0564ac543a0767885bd9ab1bdabcaa780 [file] [log] [blame]
alshabibab984662014-12-04 18:56:18 -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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.sdnip;
Pingpingf5d90932014-10-27 10:50:04 -070017
Jonathan Hart552e31f2015-02-06 11:11:59 -080018import com.google.common.collect.Sets;
Jonathan Hart41349e92015-02-09 14:14:02 -080019import org.easymock.EasyMock;
20import org.junit.Assert;
Pingpingf5d90932014-10-27 10:50:04 -070021import org.junit.Before;
22import org.junit.Test;
23import org.onlab.junit.TestUtils;
24import org.onlab.junit.TestUtils.TestUtilsException;
Jonathan Hart6cd2f352015-01-13 17:44:45 -080025import org.onlab.packet.Ethernet;
26import org.onlab.packet.Ip4Address;
27import org.onlab.packet.Ip4Prefix;
28import org.onlab.packet.IpAddress;
29import org.onlab.packet.IpPrefix;
30import org.onlab.packet.MacAddress;
31import org.onlab.packet.VlanId;
Brian O'Connorabafb502014-12-02 22:26:20 -080032import org.onosproject.core.ApplicationId;
33import org.onosproject.net.ConnectPoint;
Brian O'Connorabafb502014-12-02 22:26:20 -080034import org.onosproject.net.DeviceId;
Brian O'Connorabafb502014-12-02 22:26:20 -080035import org.onosproject.net.PortNumber;
36import org.onosproject.net.flow.DefaultTrafficSelector;
37import org.onosproject.net.flow.DefaultTrafficTreatment;
38import org.onosproject.net.flow.TrafficSelector;
39import org.onosproject.net.flow.TrafficTreatment;
Brian O'Connorabafb502014-12-02 22:26:20 -080040import org.onosproject.net.host.InterfaceIpAddress;
41import org.onosproject.net.intent.AbstractIntentTest;
42import org.onosproject.net.intent.Intent;
43import org.onosproject.net.intent.IntentOperations;
44import org.onosproject.net.intent.IntentService;
45import org.onosproject.net.intent.IntentState;
46import org.onosproject.net.intent.MultiPointToSinglePointIntent;
Jonathan Hart41349e92015-02-09 14:14:02 -080047import org.onosproject.routingapi.FibEntry;
48import org.onosproject.routingapi.FibUpdate;
49import org.onosproject.routingapi.RouteEntry;
50import org.onosproject.sdnip.IntentSynchronizer.IntentKey;
51import org.onosproject.sdnip.config.BgpPeer;
Brian O'Connorabafb502014-12-02 22:26:20 -080052import org.onosproject.sdnip.config.Interface;
Jonathan Hart41349e92015-02-09 14:14:02 -080053import org.onosproject.sdnip.config.SdnIpConfigurationService;
Pingpingf5d90932014-10-27 10:50:04 -070054
Jonathan Hart41349e92015-02-09 14:14:02 -080055import java.util.Collections;
56import java.util.HashMap;
Jonathan Hart552e31f2015-02-06 11:11:59 -080057import java.util.HashSet;
Jonathan Hart41349e92015-02-09 14:14:02 -080058import java.util.Map;
Jonathan Hart552e31f2015-02-06 11:11:59 -080059import java.util.Set;
60import java.util.concurrent.ConcurrentHashMap;
61
62import static org.easymock.EasyMock.*;
63import static org.hamcrest.Matchers.is;
64import static org.junit.Assert.assertEquals;
65import static org.junit.Assert.assertFalse;
66import static org.junit.Assert.assertThat;
67import static org.junit.Assert.assertTrue;
Pingpingf5d90932014-10-27 10:50:04 -070068
69/**
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -080070 * This class tests the intent synchronization function in the
71 * IntentSynchronizer class.
Pingpingf5d90932014-10-27 10:50:04 -070072 */
Brian O'Connor520c0522014-11-23 23:50:47 -080073public class IntentSyncTest extends AbstractIntentTest {
Pingpingf5d90932014-10-27 10:50:04 -070074
Jonathan Hart41349e92015-02-09 14:14:02 -080075 private SdnIpConfigurationService sdnIpConfigService;
Pingpingf5d90932014-10-27 10:50:04 -070076 private InterfaceService interfaceService;
77 private IntentService intentService;
Pingpingf5d90932014-10-27 10:50:04 -070078
79 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));
90
Jonathan Hart41349e92015-02-09 14:14:02 -080091 private static final ConnectPoint SW4_ETH1 = new ConnectPoint(
92 DeviceId.deviceId("of:0000000000000004"),
93 PortNumber.portNumber(1));
94
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -080095 private IntentSynchronizer intentSynchronizer;
Pingpingf5d90932014-10-27 10:50:04 -070096
97 private static final ApplicationId APPID = new ApplicationId() {
98 @Override
99 public short id() {
100 return 1;
101 }
102
103 @Override
104 public String name() {
105 return "SDNIP";
106 }
107 };
108
109 @Before
110 public void setUp() throws Exception {
Brian O'Connor520c0522014-11-23 23:50:47 -0800111 super.setUp();
Pingpingf5d90932014-10-27 10:50:04 -0700112 setUpInterfaceService();
Jonathan Hart41349e92015-02-09 14:14:02 -0800113
114 setUpBgpPeers();
Pingpingf5d90932014-10-27 10:50:04 -0700115 intentService = createMock(IntentService.class);
116
Jonathan Hart552e31f2015-02-06 11:11:59 -0800117 intentSynchronizer = new IntentSynchronizer(APPID, intentService,
Jonathan Hart41349e92015-02-09 14:14:02 -0800118 sdnIpConfigService, interfaceService);
119 }
120
121 /**
122 * Sets up BGP peers in external networks.
123 */
124 private void setUpBgpPeers() {
125
126 Map<IpAddress, BgpPeer> peers = new HashMap<>();
127
128 String peerSw1Eth1 = "192.168.10.1";
129 peers.put(IpAddress.valueOf(peerSw1Eth1),
130 new BgpPeer("00:00:00:00:00:00:00:01", 1, peerSw1Eth1));
131
132 // Two BGP peers are connected to switch 2 port 1.
133 String peer1Sw2Eth1 = "192.168.20.1";
134 peers.put(IpAddress.valueOf(peer1Sw2Eth1),
135 new BgpPeer("00:00:00:00:00:00:00:02", 1, peer1Sw2Eth1));
136
137 String peer2Sw2Eth1 = "192.168.20.2";
138 peers.put(IpAddress.valueOf(peer2Sw2Eth1),
139 new BgpPeer("00:00:00:00:00:00:00:02", 1, peer2Sw2Eth1));
140
141 String peer1Sw4Eth1 = "192.168.40.1";
142 peers.put(IpAddress.valueOf(peer1Sw4Eth1),
143 new BgpPeer("00:00:00:00:00:00:00:04", 1, peer1Sw4Eth1));
144
145 sdnIpConfigService = createMock(SdnIpConfigurationService.class);
146 expect(sdnIpConfigService.getBgpPeers()).andReturn(peers).anyTimes();
147 EasyMock.replay(sdnIpConfigService);
148
Pingpingf5d90932014-10-27 10:50:04 -0700149 }
150
151 /**
152 * Sets up InterfaceService.
153 */
154 private void setUpInterfaceService() {
155
156 interfaceService = createMock(InterfaceService.class);
157
158 Set<Interface> interfaces = Sets.newHashSet();
159
160 Set<InterfaceIpAddress> interfaceIpAddresses1 = Sets.newHashSet();
161 interfaceIpAddresses1.add(new InterfaceIpAddress(
162 IpAddress.valueOf("192.168.10.101"),
163 IpPrefix.valueOf("192.168.10.0/24")));
164 Interface sw1Eth1 = new Interface(SW1_ETH1,
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800165 interfaceIpAddresses1, MacAddress.valueOf("00:00:00:00:00:01"),
166 VlanId.NONE);
Pingpingf5d90932014-10-27 10:50:04 -0700167 interfaces.add(sw1Eth1);
168
169 Set<InterfaceIpAddress> interfaceIpAddresses2 = Sets.newHashSet();
Jonathan Hart41349e92015-02-09 14:14:02 -0800170 interfaceIpAddresses2.add(
171 new InterfaceIpAddress(IpAddress.valueOf("192.168.20.101"),
172 IpPrefix.valueOf("192.168.20.0/24")));
Pingpingf5d90932014-10-27 10:50:04 -0700173 Interface sw2Eth1 = new Interface(SW2_ETH1,
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800174 interfaceIpAddresses2, MacAddress.valueOf("00:00:00:00:00:02"),
175 VlanId.NONE);
Pingpingf5d90932014-10-27 10:50:04 -0700176 interfaces.add(sw2Eth1);
177
178 Set<InterfaceIpAddress> interfaceIpAddresses3 = Sets.newHashSet();
Jonathan Hart41349e92015-02-09 14:14:02 -0800179 interfaceIpAddresses3.add(
180 new InterfaceIpAddress(IpAddress.valueOf("192.168.30.101"),
181 IpPrefix.valueOf("192.168.30.0/24")));
Pingpingf5d90932014-10-27 10:50:04 -0700182 Interface sw3Eth1 = new Interface(SW3_ETH1,
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800183 interfaceIpAddresses3, MacAddress.valueOf("00:00:00:00:00:03"),
184 VlanId.NONE);
Pingpingf5d90932014-10-27 10:50:04 -0700185 interfaces.add(sw3Eth1);
186
Jonathan Hart41349e92015-02-09 14:14:02 -0800187 InterfaceIpAddress interfaceIpAddress4 =
188 new InterfaceIpAddress(IpAddress.valueOf("192.168.40.101"),
189 IpPrefix.valueOf("192.168.40.0/24"));
190 Interface sw4Eth1 = new Interface(SW4_ETH1,
191 Sets.newHashSet(interfaceIpAddress4),
192 MacAddress.valueOf("00:00:00:00:00:04"),
193 VlanId.vlanId((short) 1));
194
195 expect(interfaceService.getInterface(SW4_ETH1)).andReturn(sw4Eth1).anyTimes();
196 interfaces.add(sw4Eth1);
197
Pingpingf5d90932014-10-27 10:50:04 -0700198 expect(interfaceService.getInterface(SW1_ETH1)).andReturn(
199 sw1Eth1).anyTimes();
200 expect(interfaceService.getInterface(SW2_ETH1)).andReturn(
201 sw2Eth1).anyTimes();
202 expect(interfaceService.getInterface(SW3_ETH1)).andReturn(
203 sw3Eth1).anyTimes();
Jonathan Hart41349e92015-02-09 14:14:02 -0800204 expect(interfaceService.getInterfaces()).andReturn(interfaces).anyTimes();
Pingpingf5d90932014-10-27 10:50:04 -0700205 replay(interfaceService);
206 }
207
208 /**
Jonathan Hart41349e92015-02-09 14:14:02 -0800209 * Tests adding a FIB entry to the IntentSynchronizer.
210 *
211 * We verify that the synchronizer records the correct state and that the
212 * correct intent is submitted to the IntentService.
213 *
214 * @throws TestUtilsException
Pingpingf5d90932014-10-27 10:50:04 -0700215 */
Jonathan Hart41349e92015-02-09 14:14:02 -0800216 @Test
217 public void testFibAdd() throws TestUtilsException {
218 FibEntry fibEntry = new FibEntry(
219 Ip4Prefix.valueOf("1.1.1.0/24"),
220 Ip4Address.valueOf("192.168.10.1"),
221 MacAddress.valueOf("00:00:00:00:00:01"));
Pingpingf5d90932014-10-27 10:50:04 -0700222
Jonathan Hart41349e92015-02-09 14:14:02 -0800223 // Construct a MultiPointToSinglePointIntent intent
224 TrafficSelector.Builder selectorBuilder =
225 DefaultTrafficSelector.builder();
226 selectorBuilder.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(
227 fibEntry.prefix());
Pingpingf5d90932014-10-27 10:50:04 -0700228
Jonathan Hart41349e92015-02-09 14:14:02 -0800229 TrafficTreatment.Builder treatmentBuilder =
230 DefaultTrafficTreatment.builder();
231 treatmentBuilder.setEthDst(MacAddress.valueOf("00:00:00:00:00:01"));
Pingpingf5d90932014-10-27 10:50:04 -0700232
Jonathan Hart41349e92015-02-09 14:14:02 -0800233 Set<ConnectPoint> ingressPoints = new HashSet<>();
234 ingressPoints.add(SW2_ETH1);
235 ingressPoints.add(SW3_ETH1);
236 ingressPoints.add(SW4_ETH1);
237
238 MultiPointToSinglePointIntent intent =
239 new MultiPointToSinglePointIntent(APPID,
240 selectorBuilder.build(), treatmentBuilder.build(),
241 ingressPoints, SW1_ETH1);
242
243 // Setup the expected intents
244 IntentOperations.Builder builder = IntentOperations.builder(APPID);
245 builder.addSubmitOperation(intent);
246 intentService.execute(TestIntentServiceHelper.eqExceptId(
247 builder.build()));
248 replay(intentService);
249
250 intentSynchronizer.leaderChanged(true);
251 TestUtils.setField(intentSynchronizer, "isActivatedLeader", true);
252
253 FibUpdate fibUpdate = new FibUpdate(FibUpdate.Type.UPDATE,
254 fibEntry);
255 intentSynchronizer.update(Collections.singleton(fibUpdate),
256 Collections.emptyList());
257
258 Assert.assertEquals(intentSynchronizer.getRouteIntents().size(), 1);
259 Intent firstIntent =
260 intentSynchronizer.getRouteIntents().iterator().next();
261 IntentKey firstIntentKey = new IntentKey(firstIntent);
262 IntentKey intentKey = new IntentKey(intent);
263 assertTrue(firstIntentKey.equals(intentKey));
264 verify(intentService);
265 }
266
267 /**
268 * Tests adding a FIB entry with to a next hop in a VLAN.
269 *
270 * We verify that the synchronizer records the correct state and that the
271 * correct intent is submitted to the IntentService.
272 *
273 * @throws TestUtilsException
274 */
275 @Test
276 public void testFibAddWithVlan() throws TestUtilsException {
277 FibEntry fibEntry = new FibEntry(
278 Ip4Prefix.valueOf("3.3.3.0/24"),
279 Ip4Address.valueOf("192.168.40.1"),
280 MacAddress.valueOf("00:00:00:00:00:04"));
281
282 // Construct a MultiPointToSinglePointIntent intent
283 TrafficSelector.Builder selectorBuilder =
284 DefaultTrafficSelector.builder();
285 selectorBuilder.matchEthType(Ethernet.TYPE_IPV4)
286 .matchIPDst(fibEntry.prefix())
287 .matchVlanId(VlanId.ANY);
288
289 TrafficTreatment.Builder treatmentBuilder =
290 DefaultTrafficTreatment.builder();
291 treatmentBuilder.setEthDst(MacAddress.valueOf("00:00:00:00:00:04"))
292 .setVlanId(VlanId.vlanId((short) 1));
293
294 Set<ConnectPoint> ingressPoints = new HashSet<>();
295 ingressPoints.add(SW1_ETH1);
296 ingressPoints.add(SW2_ETH1);
297 ingressPoints.add(SW3_ETH1);
298
299 MultiPointToSinglePointIntent intent =
300 new MultiPointToSinglePointIntent(APPID,
301 selectorBuilder.build(), treatmentBuilder.build(),
302 ingressPoints, SW4_ETH1);
303
304 // Setup the expected intents
305 IntentOperations.Builder builder = IntentOperations.builder(APPID);
306 builder.addSubmitOperation(intent);
307 intentService.execute(
308 TestIntentServiceHelper.eqExceptId(builder.build()));
309 replay(intentService);
310
311 // Run the test
312 intentSynchronizer.leaderChanged(true);
313 TestUtils.setField(intentSynchronizer, "isActivatedLeader", true);
314 FibUpdate fibUpdate = new FibUpdate(FibUpdate.Type.UPDATE, fibEntry);
315
316 intentSynchronizer.update(Collections.singleton(fibUpdate),
317 Collections.emptyList());
318
319 // Verify
320 Assert.assertEquals(intentSynchronizer.getRouteIntents().size(), 1);
321 Intent firstIntent =
322 intentSynchronizer.getRouteIntents().iterator().next();
323 IntentKey firstIntentKey = new IntentKey(firstIntent);
324 IntentKey intentKey = new IntentKey(intent);
325 assertTrue(firstIntentKey.equals(intentKey));
326 verify(intentService);
327 }
328
329 /**
330 * Tests updating a FIB entry.
331 *
332 * We verify that the synchronizer records the correct state and that the
333 * correct intent is submitted to the IntentService.
334 *
335 * @throws TestUtilsException
336 */
337 @Test
338 public void testFibUpdate() throws TestUtilsException {
339 // Firstly add a route
340 testFibAdd();
341
342 Intent addedIntent =
343 intentSynchronizer.getRouteIntents().iterator().next();
344
345 // Start to construct a new route entry and new intent
346 FibEntry fibEntryUpdate = new FibEntry(
347 Ip4Prefix.valueOf("1.1.1.0/24"),
348 Ip4Address.valueOf("192.168.20.1"),
349 MacAddress.valueOf("00:00:00:00:00:02"));
350
351 // Construct a new MultiPointToSinglePointIntent intent
352 TrafficSelector.Builder selectorBuilderNew =
353 DefaultTrafficSelector.builder();
354 selectorBuilderNew.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(
355 fibEntryUpdate.prefix());
356
357 TrafficTreatment.Builder treatmentBuilderNew =
358 DefaultTrafficTreatment.builder();
359 treatmentBuilderNew.setEthDst(MacAddress.valueOf("00:00:00:00:00:02"));
Pingpingf5d90932014-10-27 10:50:04 -0700360
361
Jonathan Hart41349e92015-02-09 14:14:02 -0800362 Set<ConnectPoint> ingressPointsNew = new HashSet<>();
363 ingressPointsNew.add(SW1_ETH1);
364 ingressPointsNew.add(SW3_ETH1);
365 ingressPointsNew.add(SW4_ETH1);
Pingpingf5d90932014-10-27 10:50:04 -0700366
Jonathan Hart41349e92015-02-09 14:14:02 -0800367 MultiPointToSinglePointIntent intentNew =
368 new MultiPointToSinglePointIntent(APPID,
369 selectorBuilderNew.build(),
370 treatmentBuilderNew.build(),
371 ingressPointsNew, SW2_ETH1);
Pingpingf5d90932014-10-27 10:50:04 -0700372
Jonathan Hart41349e92015-02-09 14:14:02 -0800373 // Set up test expectation
374 reset(intentService);
375 // Setup the expected intents
376 IntentOperations.Builder builder = IntentOperations.builder(APPID);
377 builder.addWithdrawOperation(addedIntent.id());
378 intentService.execute(TestIntentServiceHelper.eqExceptId(
379 builder.build()));
380 builder = IntentOperations.builder(APPID);
381 builder.addSubmitOperation(intentNew);
382 intentService.execute(TestIntentServiceHelper.eqExceptId(
383 builder.build()));
384 replay(intentService);
Pingpingf5d90932014-10-27 10:50:04 -0700385
Jonathan Hart41349e92015-02-09 14:14:02 -0800386 // Call the update() method in IntentSynchronizer class
387 intentSynchronizer.leaderChanged(true);
388 TestUtils.setField(intentSynchronizer, "isActivatedLeader", true);
389 FibUpdate fibUpdate = new FibUpdate(FibUpdate.Type.UPDATE,
390 fibEntryUpdate);
391 intentSynchronizer.update(Collections.singletonList(fibUpdate),
392 Collections.emptyList());
Pingpingf5d90932014-10-27 10:50:04 -0700393
Jonathan Hart41349e92015-02-09 14:14:02 -0800394 // Verify
395 Assert.assertEquals(intentSynchronizer.getRouteIntents().size(), 1);
396 Intent firstIntent =
397 intentSynchronizer.getRouteIntents().iterator().next();
398 IntentKey firstIntentKey = new IntentKey(firstIntent);
399 IntentKey intentNewKey = new IntentKey(intentNew);
400 assertTrue(firstIntentKey.equals(intentNewKey));
401 verify(intentService);
402 }
Pingpingf5d90932014-10-27 10:50:04 -0700403
Jonathan Hart41349e92015-02-09 14:14:02 -0800404 /**
405 * Tests deleting a FIB entry.
406 *
407 * We verify that the synchronizer records the correct state and that the
408 * correct intent is withdrawn from the IntentService.
409 *
410 * @throws TestUtilsException
411 */
412 @Test
413 public void testFibDelete() throws TestUtilsException {
414 // Firstly add a route
415 testFibAdd();
Pingpingf5d90932014-10-27 10:50:04 -0700416
Jonathan Hart41349e92015-02-09 14:14:02 -0800417 Intent addedIntent =
418 intentSynchronizer.getRouteIntents().iterator().next();
419
420 // Construct the existing route entry
421 FibEntry fibEntry = new FibEntry(
422 Ip4Prefix.valueOf("1.1.1.0/24"), null, null);
423
424 // Set up expectation
425 reset(intentService);
426 // Setup the expected intents
427 IntentOperations.Builder builder = IntentOperations.builder(APPID);
428 builder.addWithdrawOperation(addedIntent.id());
429 intentService.execute(TestIntentServiceHelper.eqExceptId(
430 builder.build()));
431 replay(intentService);
432
433 // Call the update() method in IntentSynchronizer class
434 intentSynchronizer.leaderChanged(true);
435 TestUtils.setField(intentSynchronizer, "isActivatedLeader", true);
436 FibUpdate fibUpdate = new FibUpdate(FibUpdate.Type.DELETE, fibEntry);
437 intentSynchronizer.update(Collections.emptyList(),
438 Collections.singletonList(fibUpdate));
439
440 // Verify
441 Assert.assertEquals(intentSynchronizer.getRouteIntents().size(), 0);
442 verify(intentService);
Pingpingf5d90932014-10-27 10:50:04 -0700443 }
444
445 /**
446 * This method tests the behavior of intent Synchronizer.
447 *
448 * @throws TestUtilsException
449 */
450 @Test
451 public void testIntentSync() throws TestUtilsException {
452
453 //
454 // Construct routes and intents.
455 // This test simulates the following cases during the master change
456 // time interval:
457 // 1. RouteEntry1 did not change and the intent also did not change.
458 // 2. RouteEntry2 was deleted, but the intent was not deleted.
459 // 3. RouteEntry3 was newly added, and the intent was also submitted.
460 // 4. RouteEntry4 was updated to RouteEntry4Update, and the intent was
461 // also updated to a new one.
462 // 5. RouteEntry5 did not change, but its intent id changed.
463 // 6. RouteEntry6 was newly added, but the intent was not submitted.
464 //
465 RouteEntry routeEntry1 = new RouteEntry(
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800466 Ip4Prefix.valueOf("1.1.1.0/24"),
467 Ip4Address.valueOf("192.168.10.1"));
Pingpingf5d90932014-10-27 10:50:04 -0700468
469 RouteEntry routeEntry2 = new RouteEntry(
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800470 Ip4Prefix.valueOf("2.2.2.0/24"),
471 Ip4Address.valueOf("192.168.20.1"));
Pingpingf5d90932014-10-27 10:50:04 -0700472
473 RouteEntry routeEntry3 = new RouteEntry(
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800474 Ip4Prefix.valueOf("3.3.3.0/24"),
475 Ip4Address.valueOf("192.168.30.1"));
Pingpingf5d90932014-10-27 10:50:04 -0700476
477 RouteEntry routeEntry4 = new RouteEntry(
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800478 Ip4Prefix.valueOf("4.4.4.0/24"),
479 Ip4Address.valueOf("192.168.30.1"));
Pingpingf5d90932014-10-27 10:50:04 -0700480
481 RouteEntry routeEntry4Update = new RouteEntry(
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800482 Ip4Prefix.valueOf("4.4.4.0/24"),
483 Ip4Address.valueOf("192.168.20.1"));
Pingpingf5d90932014-10-27 10:50:04 -0700484
485 RouteEntry routeEntry5 = new RouteEntry(
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800486 Ip4Prefix.valueOf("5.5.5.0/24"),
487 Ip4Address.valueOf("192.168.10.1"));
Pingpingf5d90932014-10-27 10:50:04 -0700488
489 RouteEntry routeEntry6 = new RouteEntry(
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800490 Ip4Prefix.valueOf("6.6.6.0/24"),
491 Ip4Address.valueOf("192.168.10.1"));
Pingpingf5d90932014-10-27 10:50:04 -0700492
Jonathan Hartec2df012014-10-23 16:40:24 -0700493 RouteEntry routeEntry7 = new RouteEntry(
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800494 Ip4Prefix.valueOf("7.7.7.0/24"),
495 Ip4Address.valueOf("192.168.10.1"));
Jonathan Hartec2df012014-10-23 16:40:24 -0700496
Pingpingf5d90932014-10-27 10:50:04 -0700497 MultiPointToSinglePointIntent intent1 = intentBuilder(
498 routeEntry1.prefix(), "00:00:00:00:00:01", SW1_ETH1);
499 MultiPointToSinglePointIntent intent2 = intentBuilder(
500 routeEntry2.prefix(), "00:00:00:00:00:02", SW2_ETH1);
501 MultiPointToSinglePointIntent intent3 = intentBuilder(
502 routeEntry3.prefix(), "00:00:00:00:00:03", SW3_ETH1);
503 MultiPointToSinglePointIntent intent4 = intentBuilder(
504 routeEntry4.prefix(), "00:00:00:00:00:03", SW3_ETH1);
505 MultiPointToSinglePointIntent intent4Update = intentBuilder(
506 routeEntry4Update.prefix(), "00:00:00:00:00:02", SW2_ETH1);
507 MultiPointToSinglePointIntent intent5 = intentBuilder(
508 routeEntry5.prefix(), "00:00:00:00:00:01", SW1_ETH1);
Jonathan Hartec2df012014-10-23 16:40:24 -0700509 MultiPointToSinglePointIntent intent7 = intentBuilder(
510 routeEntry7.prefix(), "00:00:00:00:00:01", SW1_ETH1);
Pingpingf5d90932014-10-27 10:50:04 -0700511
512 // Compose a intent, which is equal to intent5 but the id is different.
513 MultiPointToSinglePointIntent intent5New =
514 staticIntentBuilder(intent5, routeEntry5, "00:00:00:00:00:01");
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800515 assertThat(IntentSynchronizer.IntentKey.equalIntents(
516 intent5, intent5New),
517 is(true));
Jonathan Hartec2df012014-10-23 16:40:24 -0700518 assertFalse(intent5.equals(intent5New));
Pingpingf5d90932014-10-27 10:50:04 -0700519
520 MultiPointToSinglePointIntent intent6 = intentBuilder(
521 routeEntry6.prefix(), "00:00:00:00:00:01", SW1_ETH1);
522
Jonathan Hart41349e92015-02-09 14:14:02 -0800523 // Set up the routeIntents field in IntentSynchronizer class
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800524 ConcurrentHashMap<IpPrefix, MultiPointToSinglePointIntent>
525 routeIntents = new ConcurrentHashMap<>();
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800526 routeIntents.put(routeEntry1.prefix(), intent1);
527 routeIntents.put(routeEntry3.prefix(), intent3);
528 routeIntents.put(routeEntry4Update.prefix(), intent4Update);
529 routeIntents.put(routeEntry5.prefix(), intent5New);
530 routeIntents.put(routeEntry6.prefix(), intent6);
531 routeIntents.put(routeEntry7.prefix(), intent7);
532 TestUtils.setField(intentSynchronizer, "routeIntents", routeIntents);
Pingpingf5d90932014-10-27 10:50:04 -0700533
534 // Set up expectation
535 reset(intentService);
536 Set<Intent> intents = new HashSet<Intent>();
537 intents.add(intent1);
Jonathan Hartec2df012014-10-23 16:40:24 -0700538 expect(intentService.getIntentState(intent1.id()))
539 .andReturn(IntentState.INSTALLED).anyTimes();
Pingpingf5d90932014-10-27 10:50:04 -0700540 intents.add(intent2);
Jonathan Hartec2df012014-10-23 16:40:24 -0700541 expect(intentService.getIntentState(intent2.id()))
542 .andReturn(IntentState.INSTALLED).anyTimes();
Pingpingf5d90932014-10-27 10:50:04 -0700543 intents.add(intent4);
Jonathan Hartec2df012014-10-23 16:40:24 -0700544 expect(intentService.getIntentState(intent4.id()))
545 .andReturn(IntentState.INSTALLED).anyTimes();
Pingpingf5d90932014-10-27 10:50:04 -0700546 intents.add(intent5);
Jonathan Hartec2df012014-10-23 16:40:24 -0700547 expect(intentService.getIntentState(intent5.id()))
548 .andReturn(IntentState.INSTALLED).anyTimes();
549 intents.add(intent7);
550 expect(intentService.getIntentState(intent7.id()))
551 .andReturn(IntentState.WITHDRAWING).anyTimes();
Pingpingf5d90932014-10-27 10:50:04 -0700552 expect(intentService.getIntents()).andReturn(intents).anyTimes();
553
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800554 IntentOperations.Builder builder = IntentOperations.builder(APPID);
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800555 builder.addWithdrawOperation(intent2.id());
556 builder.addWithdrawOperation(intent4.id());
Pavlin Radoslavovdde22ae2014-11-24 11:47:17 -0800557 intentService.execute(TestIntentServiceHelper.eqExceptId(
558 builder.build()));
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800559
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800560 builder = IntentOperations.builder(APPID);
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800561 builder.addSubmitOperation(intent3);
562 builder.addSubmitOperation(intent4Update);
563 builder.addSubmitOperation(intent6);
564 builder.addSubmitOperation(intent7);
Pavlin Radoslavovdde22ae2014-11-24 11:47:17 -0800565 intentService.execute(TestIntentServiceHelper.eqExceptId(
566 builder.build()));
Pingpingf5d90932014-10-27 10:50:04 -0700567 replay(intentService);
568
569 // Start the test
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800570 intentSynchronizer.leaderChanged(true);
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800571 intentSynchronizer.synchronizeIntents();
Pingpingf5d90932014-10-27 10:50:04 -0700572
573 // Verify
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800574 assertEquals(intentSynchronizer.getRouteIntents().size(), 6);
575 assertTrue(intentSynchronizer.getRouteIntents().contains(intent1));
576 assertTrue(intentSynchronizer.getRouteIntents().contains(intent3));
577 assertTrue(intentSynchronizer.getRouteIntents().contains(intent4Update));
578 assertTrue(intentSynchronizer.getRouteIntents().contains(intent5));
579 assertTrue(intentSynchronizer.getRouteIntents().contains(intent6));
Pingpingf5d90932014-10-27 10:50:04 -0700580
581 verify(intentService);
582 }
583
584 /**
585 * MultiPointToSinglePointIntent builder.
586 *
587 * @param ipPrefix the ipPrefix to match
588 * @param nextHopMacAddress to which the destination MAC address in packet
589 * should be rewritten
590 * @param egressPoint to which packets should be sent
591 * @return the constructed MultiPointToSinglePointIntent
592 */
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800593 private MultiPointToSinglePointIntent intentBuilder(IpPrefix ipPrefix,
Pingpingf5d90932014-10-27 10:50:04 -0700594 String nextHopMacAddress, ConnectPoint egressPoint) {
595
596 TrafficSelector.Builder selectorBuilder =
597 DefaultTrafficSelector.builder();
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800598 if (ipPrefix.version() == Ip4Address.VERSION) {
599 selectorBuilder.matchEthType(Ethernet.TYPE_IPV4); // IPv4
600 } else {
601 selectorBuilder.matchEthType(Ethernet.TYPE_IPV6); // IPv6
602 }
603 selectorBuilder.matchIPDst(ipPrefix);
Pingpingf5d90932014-10-27 10:50:04 -0700604
605 TrafficTreatment.Builder treatmentBuilder =
606 DefaultTrafficTreatment.builder();
607 treatmentBuilder.setEthDst(MacAddress.valueOf(nextHopMacAddress));
608
609 Set<ConnectPoint> ingressPoints = new HashSet<ConnectPoint>();
610 for (Interface intf : interfaceService.getInterfaces()) {
611 if (!intf.equals(interfaceService.getInterface(egressPoint))) {
612 ConnectPoint srcPort = intf.connectPoint();
613 ingressPoints.add(srcPort);
614 }
615 }
616 MultiPointToSinglePointIntent intent =
617 new MultiPointToSinglePointIntent(APPID,
618 selectorBuilder.build(), treatmentBuilder.build(),
619 ingressPoints, egressPoint);
620 return intent;
621 }
622
623 /**
624 * A static MultiPointToSinglePointIntent builder, the returned intent is
625 * equal to the input intent except that the id is different.
626 *
627 *
628 * @param intent the intent to be used for building a new intent
629 * @param routeEntry the relative routeEntry of the intent
630 * @return the newly constructed MultiPointToSinglePointIntent
631 * @throws TestUtilsException
632 */
633 private MultiPointToSinglePointIntent staticIntentBuilder(
634 MultiPointToSinglePointIntent intent, RouteEntry routeEntry,
635 String nextHopMacAddress) throws TestUtilsException {
636
637 // Use a different egress ConnectPoint with that in intent
638 // to generate a different id
639 MultiPointToSinglePointIntent intentNew = intentBuilder(
640 routeEntry.prefix(), nextHopMacAddress, SW2_ETH1);
641 TestUtils.setField(intentNew, "egressPoint", intent.egressPoint());
642 TestUtils.setField(intentNew,
643 "ingressPoints", intent.ingressPoints());
644 return intentNew;
645 }
Pingpingf5d90932014-10-27 10:50:04 -0700646}