blob: 9f29c1b61e239a76ae97385eafe710bf56c1d7bd [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;
Pingpingf5d90932014-10-27 10:50:04 -070019import org.junit.Before;
20import org.junit.Test;
21import org.onlab.junit.TestUtils;
22import org.onlab.junit.TestUtils.TestUtilsException;
Jonathan Hart6cd2f352015-01-13 17:44:45 -080023import org.onlab.packet.Ethernet;
24import org.onlab.packet.Ip4Address;
25import org.onlab.packet.Ip4Prefix;
26import org.onlab.packet.IpAddress;
27import org.onlab.packet.IpPrefix;
28import 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;
Brian O'Connorabafb502014-12-02 22:26:20 -080032import org.onosproject.net.DeviceId;
Brian O'Connorabafb502014-12-02 22:26:20 -080033import org.onosproject.net.PortNumber;
34import org.onosproject.net.flow.DefaultTrafficSelector;
35import org.onosproject.net.flow.DefaultTrafficTreatment;
36import org.onosproject.net.flow.TrafficSelector;
37import org.onosproject.net.flow.TrafficTreatment;
Brian O'Connorabafb502014-12-02 22:26:20 -080038import org.onosproject.net.host.InterfaceIpAddress;
39import org.onosproject.net.intent.AbstractIntentTest;
40import org.onosproject.net.intent.Intent;
Brian O'Connorabafb502014-12-02 22:26:20 -080041import org.onosproject.net.intent.IntentService;
42import org.onosproject.net.intent.IntentState;
43import org.onosproject.net.intent.MultiPointToSinglePointIntent;
Jonathan Hart2da1e602015-02-18 19:09:24 -080044import org.onosproject.routing.FibEntry;
45import org.onosproject.routing.FibUpdate;
46import org.onosproject.routing.RouteEntry;
47import org.onosproject.routing.config.BgpPeer;
48import org.onosproject.routing.config.Interface;
49import org.onosproject.routing.config.RoutingConfigurationService;
Jonathan Hart41349e92015-02-09 14:14:02 -080050import org.onosproject.sdnip.IntentSynchronizer.IntentKey;
Pingpingf5d90932014-10-27 10:50:04 -070051
Jonathan Hart41349e92015-02-09 14:14:02 -080052import java.util.Collections;
53import java.util.HashMap;
Jonathan Hart552e31f2015-02-06 11:11:59 -080054import java.util.HashSet;
Jonathan Hart41349e92015-02-09 14:14:02 -080055import java.util.Map;
Jonathan Hart552e31f2015-02-06 11:11:59 -080056import java.util.Set;
57import java.util.concurrent.ConcurrentHashMap;
58
59import static org.easymock.EasyMock.*;
60import static org.hamcrest.Matchers.is;
Jonathan Hartcb726fc2015-02-13 16:26:22 -080061import static org.junit.Assert.assertEquals;
62import static org.junit.Assert.assertFalse;
63import static org.junit.Assert.assertThat;
64import static org.junit.Assert.assertTrue;
65import static org.onosproject.sdnip.TestIntentServiceHelper.eqExceptId;
Pingpingf5d90932014-10-27 10:50:04 -070066
67/**
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -080068 * This class tests the intent synchronization function in the
69 * IntentSynchronizer class.
Pingpingf5d90932014-10-27 10:50:04 -070070 */
Brian O'Connor520c0522014-11-23 23:50:47 -080071public class IntentSyncTest extends AbstractIntentTest {
Pingpingf5d90932014-10-27 10:50:04 -070072
Jonathan Hart90a02c22015-02-13 11:52:07 -080073 private RoutingConfigurationService routingConfig;
Pingpingf5d90932014-10-27 10:50:04 -070074 private IntentService intentService;
Pingpingf5d90932014-10-27 10:50:04 -070075
76 private static final ConnectPoint SW1_ETH1 = new ConnectPoint(
77 DeviceId.deviceId("of:0000000000000001"),
78 PortNumber.portNumber(1));
79
80 private static final ConnectPoint SW2_ETH1 = new ConnectPoint(
81 DeviceId.deviceId("of:0000000000000002"),
82 PortNumber.portNumber(1));
83
84 private static final ConnectPoint SW3_ETH1 = new ConnectPoint(
85 DeviceId.deviceId("of:0000000000000003"),
86 PortNumber.portNumber(1));
87
Jonathan Hart41349e92015-02-09 14:14:02 -080088 private static final ConnectPoint SW4_ETH1 = new ConnectPoint(
89 DeviceId.deviceId("of:0000000000000004"),
90 PortNumber.portNumber(1));
91
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -080092 private IntentSynchronizer intentSynchronizer;
Pingpingf5d90932014-10-27 10:50:04 -070093
94 private static final ApplicationId APPID = new ApplicationId() {
95 @Override
96 public short id() {
97 return 1;
98 }
99
100 @Override
101 public String name() {
102 return "SDNIP";
103 }
104 };
105
106 @Before
107 public void setUp() throws Exception {
Brian O'Connor520c0522014-11-23 23:50:47 -0800108 super.setUp();
Jonathan Hart41349e92015-02-09 14:14:02 -0800109
Jonathan Hart90a02c22015-02-13 11:52:07 -0800110 routingConfig = createMock(RoutingConfigurationService.class);
111
112 // These will set expectations on routingConfig
113 setUpInterfaceService();
Jonathan Hart41349e92015-02-09 14:14:02 -0800114 setUpBgpPeers();
Jonathan Hart90a02c22015-02-13 11:52:07 -0800115
116 replay(routingConfig);
117
Pingpingf5d90932014-10-27 10:50:04 -0700118 intentService = createMock(IntentService.class);
119
Jonathan Hart552e31f2015-02-06 11:11:59 -0800120 intentSynchronizer = new IntentSynchronizer(APPID, intentService,
Jonathan Hart90a02c22015-02-13 11:52:07 -0800121 routingConfig);
Jonathan Hart41349e92015-02-09 14:14:02 -0800122 }
123
124 /**
125 * Sets up BGP peers in external networks.
126 */
127 private void setUpBgpPeers() {
128
129 Map<IpAddress, BgpPeer> peers = new HashMap<>();
130
131 String peerSw1Eth1 = "192.168.10.1";
132 peers.put(IpAddress.valueOf(peerSw1Eth1),
133 new BgpPeer("00:00:00:00:00:00:00:01", 1, peerSw1Eth1));
134
135 // Two BGP peers are connected to switch 2 port 1.
136 String peer1Sw2Eth1 = "192.168.20.1";
137 peers.put(IpAddress.valueOf(peer1Sw2Eth1),
138 new BgpPeer("00:00:00:00:00:00:00:02", 1, peer1Sw2Eth1));
139
140 String peer2Sw2Eth1 = "192.168.20.2";
141 peers.put(IpAddress.valueOf(peer2Sw2Eth1),
142 new BgpPeer("00:00:00:00:00:00:00:02", 1, peer2Sw2Eth1));
143
144 String peer1Sw4Eth1 = "192.168.40.1";
145 peers.put(IpAddress.valueOf(peer1Sw4Eth1),
146 new BgpPeer("00:00:00:00:00:00:00:04", 1, peer1Sw4Eth1));
147
Jonathan Hart90a02c22015-02-13 11:52:07 -0800148 expect(routingConfig.getBgpPeers()).andReturn(peers).anyTimes();
Pingpingf5d90932014-10-27 10:50:04 -0700149 }
150
151 /**
152 * Sets up InterfaceService.
153 */
154 private void setUpInterfaceService() {
155
Pingpingf5d90932014-10-27 10:50:04 -0700156 Set<Interface> interfaces = Sets.newHashSet();
157
158 Set<InterfaceIpAddress> interfaceIpAddresses1 = Sets.newHashSet();
159 interfaceIpAddresses1.add(new InterfaceIpAddress(
160 IpAddress.valueOf("192.168.10.101"),
161 IpPrefix.valueOf("192.168.10.0/24")));
162 Interface sw1Eth1 = new Interface(SW1_ETH1,
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800163 interfaceIpAddresses1, MacAddress.valueOf("00:00:00:00:00:01"),
164 VlanId.NONE);
Pingpingf5d90932014-10-27 10:50:04 -0700165 interfaces.add(sw1Eth1);
166
167 Set<InterfaceIpAddress> interfaceIpAddresses2 = Sets.newHashSet();
Jonathan Hart41349e92015-02-09 14:14:02 -0800168 interfaceIpAddresses2.add(
169 new InterfaceIpAddress(IpAddress.valueOf("192.168.20.101"),
170 IpPrefix.valueOf("192.168.20.0/24")));
Pingpingf5d90932014-10-27 10:50:04 -0700171 Interface sw2Eth1 = new Interface(SW2_ETH1,
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800172 interfaceIpAddresses2, MacAddress.valueOf("00:00:00:00:00:02"),
173 VlanId.NONE);
Pingpingf5d90932014-10-27 10:50:04 -0700174 interfaces.add(sw2Eth1);
175
176 Set<InterfaceIpAddress> interfaceIpAddresses3 = Sets.newHashSet();
Jonathan Hart41349e92015-02-09 14:14:02 -0800177 interfaceIpAddresses3.add(
178 new InterfaceIpAddress(IpAddress.valueOf("192.168.30.101"),
179 IpPrefix.valueOf("192.168.30.0/24")));
Pingpingf5d90932014-10-27 10:50:04 -0700180 Interface sw3Eth1 = new Interface(SW3_ETH1,
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800181 interfaceIpAddresses3, MacAddress.valueOf("00:00:00:00:00:03"),
182 VlanId.NONE);
Pingpingf5d90932014-10-27 10:50:04 -0700183 interfaces.add(sw3Eth1);
184
Jonathan Hart41349e92015-02-09 14:14:02 -0800185 InterfaceIpAddress interfaceIpAddress4 =
186 new InterfaceIpAddress(IpAddress.valueOf("192.168.40.101"),
187 IpPrefix.valueOf("192.168.40.0/24"));
188 Interface sw4Eth1 = new Interface(SW4_ETH1,
189 Sets.newHashSet(interfaceIpAddress4),
190 MacAddress.valueOf("00:00:00:00:00:04"),
191 VlanId.vlanId((short) 1));
192
Jonathan Hart90a02c22015-02-13 11:52:07 -0800193 expect(routingConfig.getInterface(SW4_ETH1)).andReturn(
194 sw4Eth1).anyTimes();
Jonathan Hart41349e92015-02-09 14:14:02 -0800195 interfaces.add(sw4Eth1);
196
Jonathan Hart90a02c22015-02-13 11:52:07 -0800197 expect(routingConfig.getInterface(SW1_ETH1)).andReturn(
Pingpingf5d90932014-10-27 10:50:04 -0700198 sw1Eth1).anyTimes();
Jonathan Hart90a02c22015-02-13 11:52:07 -0800199 expect(routingConfig.getInterface(SW2_ETH1)).andReturn(
Pingpingf5d90932014-10-27 10:50:04 -0700200 sw2Eth1).anyTimes();
Jonathan Hart90a02c22015-02-13 11:52:07 -0800201 expect(routingConfig.getInterface(SW3_ETH1)).andReturn(sw3Eth1).anyTimes();
202 expect(routingConfig.getInterfaces()).andReturn(interfaces).anyTimes();
Pingpingf5d90932014-10-27 10:50:04 -0700203 }
204
205 /**
Jonathan Hart41349e92015-02-09 14:14:02 -0800206 * Tests adding a FIB entry to the IntentSynchronizer.
207 *
208 * We verify that the synchronizer records the correct state and that the
209 * correct intent is submitted to the IntentService.
210 *
211 * @throws TestUtilsException
Pingpingf5d90932014-10-27 10:50:04 -0700212 */
Jonathan Hart41349e92015-02-09 14:14:02 -0800213 @Test
214 public void testFibAdd() throws TestUtilsException {
215 FibEntry fibEntry = new FibEntry(
216 Ip4Prefix.valueOf("1.1.1.0/24"),
217 Ip4Address.valueOf("192.168.10.1"),
218 MacAddress.valueOf("00:00:00:00:00:01"));
Pingpingf5d90932014-10-27 10:50:04 -0700219
Jonathan Hart41349e92015-02-09 14:14:02 -0800220 // Construct a MultiPointToSinglePointIntent intent
221 TrafficSelector.Builder selectorBuilder =
222 DefaultTrafficSelector.builder();
223 selectorBuilder.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(
224 fibEntry.prefix());
Pingpingf5d90932014-10-27 10:50:04 -0700225
Jonathan Hart41349e92015-02-09 14:14:02 -0800226 TrafficTreatment.Builder treatmentBuilder =
227 DefaultTrafficTreatment.builder();
228 treatmentBuilder.setEthDst(MacAddress.valueOf("00:00:00:00:00:01"));
Pingpingf5d90932014-10-27 10:50:04 -0700229
Jonathan Hart41349e92015-02-09 14:14:02 -0800230 Set<ConnectPoint> ingressPoints = new HashSet<>();
231 ingressPoints.add(SW2_ETH1);
232 ingressPoints.add(SW3_ETH1);
233 ingressPoints.add(SW4_ETH1);
234
235 MultiPointToSinglePointIntent intent =
236 new MultiPointToSinglePointIntent(APPID,
237 selectorBuilder.build(), treatmentBuilder.build(),
238 ingressPoints, SW1_ETH1);
239
240 // Setup the expected intents
Jonathan Hartcb726fc2015-02-13 16:26:22 -0800241 intentService.submit(eqExceptId(intent));
Jonathan Hart41349e92015-02-09 14:14:02 -0800242 replay(intentService);
243
244 intentSynchronizer.leaderChanged(true);
245 TestUtils.setField(intentSynchronizer, "isActivatedLeader", true);
246
247 FibUpdate fibUpdate = new FibUpdate(FibUpdate.Type.UPDATE,
248 fibEntry);
249 intentSynchronizer.update(Collections.singleton(fibUpdate),
250 Collections.emptyList());
251
Brian O'Connor03406a42015-02-03 17:28:57 -0800252 assertEquals(intentSynchronizer.getRouteIntents().size(), 1);
Jonathan Hart41349e92015-02-09 14:14:02 -0800253 Intent firstIntent =
254 intentSynchronizer.getRouteIntents().iterator().next();
255 IntentKey firstIntentKey = new IntentKey(firstIntent);
256 IntentKey intentKey = new IntentKey(intent);
257 assertTrue(firstIntentKey.equals(intentKey));
258 verify(intentService);
259 }
260
261 /**
262 * Tests adding a FIB entry with to a next hop in a VLAN.
263 *
264 * We verify that the synchronizer records the correct state and that the
265 * correct intent is submitted to the IntentService.
266 *
267 * @throws TestUtilsException
268 */
269 @Test
270 public void testFibAddWithVlan() throws TestUtilsException {
271 FibEntry fibEntry = new FibEntry(
272 Ip4Prefix.valueOf("3.3.3.0/24"),
273 Ip4Address.valueOf("192.168.40.1"),
274 MacAddress.valueOf("00:00:00:00:00:04"));
275
276 // Construct a MultiPointToSinglePointIntent intent
277 TrafficSelector.Builder selectorBuilder =
278 DefaultTrafficSelector.builder();
279 selectorBuilder.matchEthType(Ethernet.TYPE_IPV4)
280 .matchIPDst(fibEntry.prefix())
281 .matchVlanId(VlanId.ANY);
282
283 TrafficTreatment.Builder treatmentBuilder =
284 DefaultTrafficTreatment.builder();
285 treatmentBuilder.setEthDst(MacAddress.valueOf("00:00:00:00:00:04"))
286 .setVlanId(VlanId.vlanId((short) 1));
287
288 Set<ConnectPoint> ingressPoints = new HashSet<>();
289 ingressPoints.add(SW1_ETH1);
290 ingressPoints.add(SW2_ETH1);
291 ingressPoints.add(SW3_ETH1);
292
293 MultiPointToSinglePointIntent intent =
294 new MultiPointToSinglePointIntent(APPID,
295 selectorBuilder.build(), treatmentBuilder.build(),
296 ingressPoints, SW4_ETH1);
297
298 // Setup the expected intents
Jonathan Hartcb726fc2015-02-13 16:26:22 -0800299 intentService.submit(eqExceptId(intent));
300
Jonathan Hart41349e92015-02-09 14:14:02 -0800301 replay(intentService);
302
303 // Run the test
304 intentSynchronizer.leaderChanged(true);
305 TestUtils.setField(intentSynchronizer, "isActivatedLeader", true);
306 FibUpdate fibUpdate = new FibUpdate(FibUpdate.Type.UPDATE, fibEntry);
307
308 intentSynchronizer.update(Collections.singleton(fibUpdate),
309 Collections.emptyList());
310
311 // Verify
Brian O'Connor03406a42015-02-03 17:28:57 -0800312 assertEquals(intentSynchronizer.getRouteIntents().size(), 1);
Jonathan Hart41349e92015-02-09 14:14:02 -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));
318 verify(intentService);
319 }
320
321 /**
322 * Tests updating a FIB entry.
323 *
324 * We verify that the synchronizer records the correct state and that the
325 * correct intent is submitted to the IntentService.
326 *
327 * @throws TestUtilsException
328 */
329 @Test
330 public void testFibUpdate() throws TestUtilsException {
331 // Firstly add a route
332 testFibAdd();
333
334 Intent addedIntent =
335 intentSynchronizer.getRouteIntents().iterator().next();
336
337 // Start to construct a new route entry and new intent
338 FibEntry fibEntryUpdate = new FibEntry(
339 Ip4Prefix.valueOf("1.1.1.0/24"),
340 Ip4Address.valueOf("192.168.20.1"),
341 MacAddress.valueOf("00:00:00:00:00:02"));
342
343 // Construct a new MultiPointToSinglePointIntent intent
344 TrafficSelector.Builder selectorBuilderNew =
345 DefaultTrafficSelector.builder();
346 selectorBuilderNew.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(
347 fibEntryUpdate.prefix());
348
349 TrafficTreatment.Builder treatmentBuilderNew =
350 DefaultTrafficTreatment.builder();
351 treatmentBuilderNew.setEthDst(MacAddress.valueOf("00:00:00:00:00:02"));
Pingpingf5d90932014-10-27 10:50:04 -0700352
353
Jonathan Hart41349e92015-02-09 14:14:02 -0800354 Set<ConnectPoint> ingressPointsNew = new HashSet<>();
355 ingressPointsNew.add(SW1_ETH1);
356 ingressPointsNew.add(SW3_ETH1);
357 ingressPointsNew.add(SW4_ETH1);
Pingpingf5d90932014-10-27 10:50:04 -0700358
Jonathan Hart41349e92015-02-09 14:14:02 -0800359 MultiPointToSinglePointIntent intentNew =
360 new MultiPointToSinglePointIntent(APPID,
361 selectorBuilderNew.build(),
362 treatmentBuilderNew.build(),
363 ingressPointsNew, SW2_ETH1);
Pingpingf5d90932014-10-27 10:50:04 -0700364
Jonathan Hart41349e92015-02-09 14:14:02 -0800365 // Set up test expectation
366 reset(intentService);
367 // Setup the expected intents
Jonathan Hartcb726fc2015-02-13 16:26:22 -0800368 intentService.withdraw(eqExceptId(addedIntent));
369 intentService.submit(eqExceptId(intentNew));
Jonathan Hart41349e92015-02-09 14:14:02 -0800370 replay(intentService);
Pingpingf5d90932014-10-27 10:50:04 -0700371
Jonathan Hart41349e92015-02-09 14:14:02 -0800372 // Call the update() method in IntentSynchronizer class
373 intentSynchronizer.leaderChanged(true);
374 TestUtils.setField(intentSynchronizer, "isActivatedLeader", true);
375 FibUpdate fibUpdate = new FibUpdate(FibUpdate.Type.UPDATE,
376 fibEntryUpdate);
377 intentSynchronizer.update(Collections.singletonList(fibUpdate),
378 Collections.emptyList());
Pingpingf5d90932014-10-27 10:50:04 -0700379
Jonathan Hart41349e92015-02-09 14:14:02 -0800380 // Verify
Brian O'Connor03406a42015-02-03 17:28:57 -0800381 assertEquals(intentSynchronizer.getRouteIntents().size(), 1);
Jonathan Hart41349e92015-02-09 14:14:02 -0800382 Intent firstIntent =
383 intentSynchronizer.getRouteIntents().iterator().next();
384 IntentKey firstIntentKey = new IntentKey(firstIntent);
385 IntentKey intentNewKey = new IntentKey(intentNew);
386 assertTrue(firstIntentKey.equals(intentNewKey));
387 verify(intentService);
388 }
Pingpingf5d90932014-10-27 10:50:04 -0700389
Jonathan Hart41349e92015-02-09 14:14:02 -0800390 /**
391 * Tests deleting a FIB entry.
392 *
393 * We verify that the synchronizer records the correct state and that the
394 * correct intent is withdrawn from the IntentService.
395 *
396 * @throws TestUtilsException
397 */
398 @Test
399 public void testFibDelete() throws TestUtilsException {
400 // Firstly add a route
401 testFibAdd();
Pingpingf5d90932014-10-27 10:50:04 -0700402
Jonathan Hart41349e92015-02-09 14:14:02 -0800403 Intent addedIntent =
404 intentSynchronizer.getRouteIntents().iterator().next();
405
406 // Construct the existing route entry
407 FibEntry fibEntry = new FibEntry(
408 Ip4Prefix.valueOf("1.1.1.0/24"), null, null);
409
410 // Set up expectation
411 reset(intentService);
412 // Setup the expected intents
Jonathan Hartcb726fc2015-02-13 16:26:22 -0800413 intentService.withdraw(eqExceptId(addedIntent));
Jonathan Hart41349e92015-02-09 14:14:02 -0800414 replay(intentService);
415
416 // Call the update() method in IntentSynchronizer class
417 intentSynchronizer.leaderChanged(true);
418 TestUtils.setField(intentSynchronizer, "isActivatedLeader", true);
419 FibUpdate fibUpdate = new FibUpdate(FibUpdate.Type.DELETE, fibEntry);
420 intentSynchronizer.update(Collections.emptyList(),
421 Collections.singletonList(fibUpdate));
422
423 // Verify
Brian O'Connor03406a42015-02-03 17:28:57 -0800424 assertEquals(intentSynchronizer.getRouteIntents().size(), 0);
Jonathan Hart41349e92015-02-09 14:14:02 -0800425 verify(intentService);
Pingpingf5d90932014-10-27 10:50:04 -0700426 }
427
428 /**
429 * This method tests the behavior of intent Synchronizer.
430 *
431 * @throws TestUtilsException
432 */
433 @Test
434 public void testIntentSync() throws TestUtilsException {
435
436 //
437 // Construct routes and intents.
438 // This test simulates the following cases during the master change
439 // time interval:
440 // 1. RouteEntry1 did not change and the intent also did not change.
441 // 2. RouteEntry2 was deleted, but the intent was not deleted.
442 // 3. RouteEntry3 was newly added, and the intent was also submitted.
443 // 4. RouteEntry4 was updated to RouteEntry4Update, and the intent was
444 // also updated to a new one.
445 // 5. RouteEntry5 did not change, but its intent id changed.
446 // 6. RouteEntry6 was newly added, but the intent was not submitted.
447 //
448 RouteEntry routeEntry1 = new RouteEntry(
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800449 Ip4Prefix.valueOf("1.1.1.0/24"),
450 Ip4Address.valueOf("192.168.10.1"));
Pingpingf5d90932014-10-27 10:50:04 -0700451
452 RouteEntry routeEntry2 = new RouteEntry(
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800453 Ip4Prefix.valueOf("2.2.2.0/24"),
454 Ip4Address.valueOf("192.168.20.1"));
Pingpingf5d90932014-10-27 10:50:04 -0700455
456 RouteEntry routeEntry3 = new RouteEntry(
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800457 Ip4Prefix.valueOf("3.3.3.0/24"),
458 Ip4Address.valueOf("192.168.30.1"));
Pingpingf5d90932014-10-27 10:50:04 -0700459
460 RouteEntry routeEntry4 = new RouteEntry(
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800461 Ip4Prefix.valueOf("4.4.4.0/24"),
462 Ip4Address.valueOf("192.168.30.1"));
Pingpingf5d90932014-10-27 10:50:04 -0700463
464 RouteEntry routeEntry4Update = new RouteEntry(
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800465 Ip4Prefix.valueOf("4.4.4.0/24"),
466 Ip4Address.valueOf("192.168.20.1"));
Pingpingf5d90932014-10-27 10:50:04 -0700467
468 RouteEntry routeEntry5 = new RouteEntry(
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800469 Ip4Prefix.valueOf("5.5.5.0/24"),
470 Ip4Address.valueOf("192.168.10.1"));
Pingpingf5d90932014-10-27 10:50:04 -0700471
472 RouteEntry routeEntry6 = new RouteEntry(
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800473 Ip4Prefix.valueOf("6.6.6.0/24"),
474 Ip4Address.valueOf("192.168.10.1"));
Pingpingf5d90932014-10-27 10:50:04 -0700475
Jonathan Hartec2df012014-10-23 16:40:24 -0700476 RouteEntry routeEntry7 = new RouteEntry(
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800477 Ip4Prefix.valueOf("7.7.7.0/24"),
478 Ip4Address.valueOf("192.168.10.1"));
Jonathan Hartec2df012014-10-23 16:40:24 -0700479
Pingpingf5d90932014-10-27 10:50:04 -0700480 MultiPointToSinglePointIntent intent1 = intentBuilder(
481 routeEntry1.prefix(), "00:00:00:00:00:01", SW1_ETH1);
482 MultiPointToSinglePointIntent intent2 = intentBuilder(
483 routeEntry2.prefix(), "00:00:00:00:00:02", SW2_ETH1);
484 MultiPointToSinglePointIntent intent3 = intentBuilder(
485 routeEntry3.prefix(), "00:00:00:00:00:03", SW3_ETH1);
486 MultiPointToSinglePointIntent intent4 = intentBuilder(
487 routeEntry4.prefix(), "00:00:00:00:00:03", SW3_ETH1);
488 MultiPointToSinglePointIntent intent4Update = intentBuilder(
489 routeEntry4Update.prefix(), "00:00:00:00:00:02", SW2_ETH1);
490 MultiPointToSinglePointIntent intent5 = intentBuilder(
491 routeEntry5.prefix(), "00:00:00:00:00:01", SW1_ETH1);
Jonathan Hartec2df012014-10-23 16:40:24 -0700492 MultiPointToSinglePointIntent intent7 = intentBuilder(
493 routeEntry7.prefix(), "00:00:00:00:00:01", SW1_ETH1);
Pingpingf5d90932014-10-27 10:50:04 -0700494
495 // Compose a intent, which is equal to intent5 but the id is different.
496 MultiPointToSinglePointIntent intent5New =
497 staticIntentBuilder(intent5, routeEntry5, "00:00:00:00:00:01");
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800498 assertThat(IntentSynchronizer.IntentKey.equalIntents(
499 intent5, intent5New),
500 is(true));
Jonathan Hartec2df012014-10-23 16:40:24 -0700501 assertFalse(intent5.equals(intent5New));
Pingpingf5d90932014-10-27 10:50:04 -0700502
503 MultiPointToSinglePointIntent intent6 = intentBuilder(
504 routeEntry6.prefix(), "00:00:00:00:00:01", SW1_ETH1);
505
Jonathan Hart41349e92015-02-09 14:14:02 -0800506 // Set up the routeIntents field in IntentSynchronizer class
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800507 ConcurrentHashMap<IpPrefix, MultiPointToSinglePointIntent>
508 routeIntents = new ConcurrentHashMap<>();
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800509 routeIntents.put(routeEntry1.prefix(), intent1);
510 routeIntents.put(routeEntry3.prefix(), intent3);
511 routeIntents.put(routeEntry4Update.prefix(), intent4Update);
512 routeIntents.put(routeEntry5.prefix(), intent5New);
513 routeIntents.put(routeEntry6.prefix(), intent6);
514 routeIntents.put(routeEntry7.prefix(), intent7);
515 TestUtils.setField(intentSynchronizer, "routeIntents", routeIntents);
Pingpingf5d90932014-10-27 10:50:04 -0700516
517 // Set up expectation
518 reset(intentService);
Jonathan Hart90a02c22015-02-13 11:52:07 -0800519 Set<Intent> intents = new HashSet<>();
Pingpingf5d90932014-10-27 10:50:04 -0700520 intents.add(intent1);
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800521 expect(intentService.getIntentState(intent1.key()))
Jonathan Hartec2df012014-10-23 16:40:24 -0700522 .andReturn(IntentState.INSTALLED).anyTimes();
Pingpingf5d90932014-10-27 10:50:04 -0700523 intents.add(intent2);
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800524 expect(intentService.getIntentState(intent2.key()))
Jonathan Hartec2df012014-10-23 16:40:24 -0700525 .andReturn(IntentState.INSTALLED).anyTimes();
Pingpingf5d90932014-10-27 10:50:04 -0700526 intents.add(intent4);
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800527 expect(intentService.getIntentState(intent4.key()))
Jonathan Hartec2df012014-10-23 16:40:24 -0700528 .andReturn(IntentState.INSTALLED).anyTimes();
Pingpingf5d90932014-10-27 10:50:04 -0700529 intents.add(intent5);
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800530 expect(intentService.getIntentState(intent5.key()))
Jonathan Hartec2df012014-10-23 16:40:24 -0700531 .andReturn(IntentState.INSTALLED).anyTimes();
532 intents.add(intent7);
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800533 expect(intentService.getIntentState(intent7.key()))
Jonathan Hartec2df012014-10-23 16:40:24 -0700534 .andReturn(IntentState.WITHDRAWING).anyTimes();
Pingpingf5d90932014-10-27 10:50:04 -0700535 expect(intentService.getIntents()).andReturn(intents).anyTimes();
536
Brian O'Connor03406a42015-02-03 17:28:57 -0800537 intentService.withdraw(intent2);
538 intentService.withdraw(intent4);
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800539
Brian O'Connor03406a42015-02-03 17:28:57 -0800540 intentService.submit(intent3);
541 intentService.submit(intent4Update);
542 intentService.submit(intent6);
543 intentService.submit(intent7);
Pingpingf5d90932014-10-27 10:50:04 -0700544 replay(intentService);
545
546 // Start the test
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800547 intentSynchronizer.leaderChanged(true);
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800548 intentSynchronizer.synchronizeIntents();
Pingpingf5d90932014-10-27 10:50:04 -0700549
550 // Verify
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800551 assertEquals(intentSynchronizer.getRouteIntents().size(), 6);
552 assertTrue(intentSynchronizer.getRouteIntents().contains(intent1));
553 assertTrue(intentSynchronizer.getRouteIntents().contains(intent3));
554 assertTrue(intentSynchronizer.getRouteIntents().contains(intent4Update));
555 assertTrue(intentSynchronizer.getRouteIntents().contains(intent5));
556 assertTrue(intentSynchronizer.getRouteIntents().contains(intent6));
Pingpingf5d90932014-10-27 10:50:04 -0700557
558 verify(intentService);
559 }
560
561 /**
562 * MultiPointToSinglePointIntent builder.
563 *
564 * @param ipPrefix the ipPrefix to match
565 * @param nextHopMacAddress to which the destination MAC address in packet
566 * should be rewritten
567 * @param egressPoint to which packets should be sent
568 * @return the constructed MultiPointToSinglePointIntent
569 */
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800570 private MultiPointToSinglePointIntent intentBuilder(IpPrefix ipPrefix,
Pingpingf5d90932014-10-27 10:50:04 -0700571 String nextHopMacAddress, ConnectPoint egressPoint) {
572
573 TrafficSelector.Builder selectorBuilder =
574 DefaultTrafficSelector.builder();
Pavlin Radoslavov87dd9302015-03-10 13:53:24 -0700575 if (ipPrefix.isIp4()) {
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800576 selectorBuilder.matchEthType(Ethernet.TYPE_IPV4); // IPv4
Pavlin Radoslavova8537092015-02-23 10:15:20 -0800577 selectorBuilder.matchIPDst(ipPrefix);
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800578 } else {
579 selectorBuilder.matchEthType(Ethernet.TYPE_IPV6); // IPv6
Pavlin Radoslavova8537092015-02-23 10:15:20 -0800580 selectorBuilder.matchIPv6Dst(ipPrefix);
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800581 }
Pingpingf5d90932014-10-27 10:50:04 -0700582
583 TrafficTreatment.Builder treatmentBuilder =
584 DefaultTrafficTreatment.builder();
585 treatmentBuilder.setEthDst(MacAddress.valueOf(nextHopMacAddress));
586
Jonathan Hart90a02c22015-02-13 11:52:07 -0800587 Set<ConnectPoint> ingressPoints = new HashSet<>();
588 for (Interface intf : routingConfig.getInterfaces()) {
589 if (!intf.equals(routingConfig.getInterface(egressPoint))) {
Pingpingf5d90932014-10-27 10:50:04 -0700590 ConnectPoint srcPort = intf.connectPoint();
591 ingressPoints.add(srcPort);
592 }
593 }
594 MultiPointToSinglePointIntent intent =
595 new MultiPointToSinglePointIntent(APPID,
596 selectorBuilder.build(), treatmentBuilder.build(),
597 ingressPoints, egressPoint);
598 return intent;
599 }
600
601 /**
602 * A static MultiPointToSinglePointIntent builder, the returned intent is
603 * equal to the input intent except that the id is different.
604 *
605 *
606 * @param intent the intent to be used for building a new intent
607 * @param routeEntry the relative routeEntry of the intent
608 * @return the newly constructed MultiPointToSinglePointIntent
609 * @throws TestUtilsException
610 */
611 private MultiPointToSinglePointIntent staticIntentBuilder(
612 MultiPointToSinglePointIntent intent, RouteEntry routeEntry,
613 String nextHopMacAddress) throws TestUtilsException {
614
615 // Use a different egress ConnectPoint with that in intent
616 // to generate a different id
617 MultiPointToSinglePointIntent intentNew = intentBuilder(
618 routeEntry.prefix(), nextHopMacAddress, SW2_ETH1);
619 TestUtils.setField(intentNew, "egressPoint", intent.egressPoint());
620 TestUtils.setField(intentNew,
621 "ingressPoints", intent.ingressPoints());
622 return intentNew;
623 }
Pingpingf5d90932014-10-27 10:50:04 -0700624}