blob: 351a099b1ea7afd8f6747022ca8de32bdbdb885e [file] [log] [blame]
alshabibab984662014-12-04 18:56:18 -08001/*
Jonathan Hart6af92be2016-01-05 20:52:25 -08002 * Copyright 2016 Open Networking Laboratory
alshabibab984662014-12-04 18:56:18 -08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Jonathan Hart6af92be2016-01-05 20:52:25 -080016package org.onosproject.routing.impl;
Pingpingf5d90932014-10-27 10:50:04 -070017
Jonathan Hart552e31f2015-02-06 11:11:59 -080018import com.google.common.collect.Sets;
Jonathan Hart9a426f82015-09-03 15:43:13 +020019import com.google.common.util.concurrent.MoreExecutors;
Madan Jampani620f70d2016-01-30 22:22:47 -080020
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;
Jonathan Hart9a426f82015-09-03 15:43:13 +020032import org.onosproject.TestApplicationId;
Madan Jampani620f70d2016-01-30 22:22:47 -080033import org.onosproject.cluster.ClusterServiceAdapter;
34import org.onosproject.cluster.ControllerNode;
35import org.onosproject.cluster.DefaultControllerNode;
Jonathan Hart365335e2015-12-10 11:09:53 -080036import org.onosproject.cluster.LeadershipServiceAdapter;
Madan Jampani620f70d2016-01-30 22:22:47 -080037import org.onosproject.cluster.NodeId;
Brian O'Connorabafb502014-12-02 22:26:20 -080038import org.onosproject.core.ApplicationId;
Jonathan Hart365335e2015-12-10 11:09:53 -080039import org.onosproject.core.CoreServiceAdapter;
Jonathan Hart4cb39882015-08-12 23:50:55 -040040import org.onosproject.incubator.net.intf.Interface;
Brian O'Connorabafb502014-12-02 22:26:20 -080041import org.onosproject.net.ConnectPoint;
Brian O'Connorabafb502014-12-02 22:26:20 -080042import org.onosproject.net.DeviceId;
Brian O'Connorabafb502014-12-02 22:26:20 -080043import org.onosproject.net.PortNumber;
44import org.onosproject.net.flow.DefaultTrafficSelector;
45import org.onosproject.net.flow.DefaultTrafficTreatment;
46import org.onosproject.net.flow.TrafficSelector;
47import org.onosproject.net.flow.TrafficTreatment;
Brian O'Connorabafb502014-12-02 22:26:20 -080048import org.onosproject.net.host.InterfaceIpAddress;
49import org.onosproject.net.intent.AbstractIntentTest;
50import org.onosproject.net.intent.Intent;
Brian O'Connorabafb502014-12-02 22:26:20 -080051import org.onosproject.net.intent.IntentService;
52import org.onosproject.net.intent.IntentState;
Jonathan Hart365335e2015-12-10 11:09:53 -080053import org.onosproject.net.intent.IntentUtils;
Jonathan Hart9a426f82015-09-03 15:43:13 +020054import org.onosproject.net.intent.Key;
Brian O'Connorabafb502014-12-02 22:26:20 -080055import org.onosproject.net.intent.MultiPointToSinglePointIntent;
Jonathan Hart2da1e602015-02-18 19:09:24 -080056import org.onosproject.routing.RouteEntry;
Pingpingf5d90932014-10-27 10:50:04 -070057
Jonathan Hart41349e92015-02-09 14:14:02 -080058import java.util.Collections;
Jonathan Hart552e31f2015-02-06 11:11:59 -080059import java.util.HashSet;
60import java.util.Set;
Jonathan Hart365335e2015-12-10 11:09:53 -080061import java.util.concurrent.ExecutorService;
Jonathan Hart552e31f2015-02-06 11:11:59 -080062
Jonathan Hart4cb39882015-08-12 23:50:55 -040063import static org.easymock.EasyMock.createMock;
64import static org.easymock.EasyMock.expect;
65import static org.easymock.EasyMock.replay;
66import static org.easymock.EasyMock.reset;
67import static org.easymock.EasyMock.verify;
Jonathan Hart552e31f2015-02-06 11:11:59 -080068import static org.hamcrest.Matchers.is;
Jonathan Hartcb726fc2015-02-13 16:26:22 -080069import static org.junit.Assert.assertFalse;
70import static org.junit.Assert.assertThat;
Pingpingf5d90932014-10-27 10:50:04 -070071
72/**
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -080073 * This class tests the intent synchronization function in the
74 * IntentSynchronizer class.
Pingpingf5d90932014-10-27 10:50:04 -070075 */
Jonathan Hart6af92be2016-01-05 20:52:25 -080076public class IntentSynchronizerTest extends AbstractIntentTest {
Pingpingf5d90932014-10-27 10:50:04 -070077
Pingpingf5d90932014-10-27 10:50:04 -070078 private IntentService intentService;
Pingpingf5d90932014-10-27 10:50:04 -070079
80 private static final ConnectPoint SW1_ETH1 = new ConnectPoint(
81 DeviceId.deviceId("of:0000000000000001"),
82 PortNumber.portNumber(1));
83
84 private static final ConnectPoint SW2_ETH1 = new ConnectPoint(
85 DeviceId.deviceId("of:0000000000000002"),
86 PortNumber.portNumber(1));
87
88 private static final ConnectPoint SW3_ETH1 = new ConnectPoint(
89 DeviceId.deviceId("of:0000000000000003"),
90 PortNumber.portNumber(1));
91
Jonathan Hart41349e92015-02-09 14:14:02 -080092 private static final ConnectPoint SW4_ETH1 = new ConnectPoint(
93 DeviceId.deviceId("of:0000000000000004"),
94 PortNumber.portNumber(1));
95
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -080096 private IntentSynchronizer intentSynchronizer;
Jonathan Hart4cb39882015-08-12 23:50:55 -040097 private final Set<Interface> interfaces = Sets.newHashSet();
Pingpingf5d90932014-10-27 10:50:04 -070098
Jonathan Hart365335e2015-12-10 11:09:53 -080099 private static final ApplicationId APPID =
100 TestApplicationId.create("intent-sync-test");
Pingpingf5d90932014-10-27 10:50:04 -0700101
Madan Jampani620f70d2016-01-30 22:22:47 -0800102 private static final ControllerNode LOCAL_NODE =
103 new DefaultControllerNode(new NodeId("foo"), IpAddress.valueOf("127.0.0.1"));
104
Pingpingf5d90932014-10-27 10:50:04 -0700105 @Before
106 public void setUp() throws Exception {
Brian O'Connor520c0522014-11-23 23:50:47 -0800107 super.setUp();
Jonathan Hart41349e92015-02-09 14:14:02 -0800108
Jonathan Hart90a02c22015-02-13 11:52:07 -0800109 setUpInterfaceService();
Jonathan Hart90a02c22015-02-13 11:52:07 -0800110
Pingpingf5d90932014-10-27 10:50:04 -0700111 intentService = createMock(IntentService.class);
112
Jonathan Hart365335e2015-12-10 11:09:53 -0800113 intentSynchronizer = new TestIntentSynchronizer();
114
115 intentSynchronizer.coreService = new TestCoreService();
Madan Jampani620f70d2016-01-30 22:22:47 -0800116 intentSynchronizer.clusterService = new TestClusterService();
Jonathan Hart365335e2015-12-10 11:09:53 -0800117 intentSynchronizer.leadershipService = new TestLeadershipService();
118 intentSynchronizer.intentService = intentService;
119
120 intentSynchronizer.activate();
Pingpingf5d90932014-10-27 10:50:04 -0700121 }
122
123 /**
124 * Sets up InterfaceService.
125 */
126 private void setUpInterfaceService() {
Pingpingf5d90932014-10-27 10:50:04 -0700127 Set<InterfaceIpAddress> interfaceIpAddresses1 = Sets.newHashSet();
128 interfaceIpAddresses1.add(new InterfaceIpAddress(
129 IpAddress.valueOf("192.168.10.101"),
130 IpPrefix.valueOf("192.168.10.0/24")));
131 Interface sw1Eth1 = new Interface(SW1_ETH1,
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800132 interfaceIpAddresses1, MacAddress.valueOf("00:00:00:00:00:01"),
133 VlanId.NONE);
Pingpingf5d90932014-10-27 10:50:04 -0700134 interfaces.add(sw1Eth1);
135
136 Set<InterfaceIpAddress> interfaceIpAddresses2 = Sets.newHashSet();
Jonathan Hart41349e92015-02-09 14:14:02 -0800137 interfaceIpAddresses2.add(
138 new InterfaceIpAddress(IpAddress.valueOf("192.168.20.101"),
139 IpPrefix.valueOf("192.168.20.0/24")));
Pingpingf5d90932014-10-27 10:50:04 -0700140 Interface sw2Eth1 = new Interface(SW2_ETH1,
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800141 interfaceIpAddresses2, MacAddress.valueOf("00:00:00:00:00:02"),
142 VlanId.NONE);
Pingpingf5d90932014-10-27 10:50:04 -0700143 interfaces.add(sw2Eth1);
144
145 Set<InterfaceIpAddress> interfaceIpAddresses3 = Sets.newHashSet();
Jonathan Hart41349e92015-02-09 14:14:02 -0800146 interfaceIpAddresses3.add(
147 new InterfaceIpAddress(IpAddress.valueOf("192.168.30.101"),
148 IpPrefix.valueOf("192.168.30.0/24")));
Pingpingf5d90932014-10-27 10:50:04 -0700149 Interface sw3Eth1 = new Interface(SW3_ETH1,
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800150 interfaceIpAddresses3, MacAddress.valueOf("00:00:00:00:00:03"),
151 VlanId.NONE);
Pingpingf5d90932014-10-27 10:50:04 -0700152 interfaces.add(sw3Eth1);
153
Jonathan Hart41349e92015-02-09 14:14:02 -0800154 InterfaceIpAddress interfaceIpAddress4 =
155 new InterfaceIpAddress(IpAddress.valueOf("192.168.40.101"),
156 IpPrefix.valueOf("192.168.40.0/24"));
157 Interface sw4Eth1 = new Interface(SW4_ETH1,
158 Sets.newHashSet(interfaceIpAddress4),
159 MacAddress.valueOf("00:00:00:00:00:04"),
160 VlanId.vlanId((short) 1));
161
Jonathan Hart41349e92015-02-09 14:14:02 -0800162 interfaces.add(sw4Eth1);
Pingpingf5d90932014-10-27 10:50:04 -0700163 }
164
165 /**
Jonathan Hart9a426f82015-09-03 15:43:13 +0200166 * Tests the synchronization behavior of intent synchronizer. We set up
167 * a discrepancy between the intent service state and the intent
168 * synchronizer's state and ensure that this is reconciled correctly.
Pingpingf5d90932014-10-27 10:50:04 -0700169 *
170 * @throws TestUtilsException
171 */
172 @Test
173 public void testIntentSync() throws TestUtilsException {
174
175 //
176 // Construct routes and intents.
177 // This test simulates the following cases during the master change
178 // time interval:
179 // 1. RouteEntry1 did not change and the intent also did not change.
180 // 2. RouteEntry2 was deleted, but the intent was not deleted.
181 // 3. RouteEntry3 was newly added, and the intent was also submitted.
182 // 4. RouteEntry4 was updated to RouteEntry4Update, and the intent was
183 // also updated to a new one.
184 // 5. RouteEntry5 did not change, but its intent id changed.
185 // 6. RouteEntry6 was newly added, but the intent was not submitted.
186 //
187 RouteEntry routeEntry1 = new RouteEntry(
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800188 Ip4Prefix.valueOf("1.1.1.0/24"),
189 Ip4Address.valueOf("192.168.10.1"));
Pingpingf5d90932014-10-27 10:50:04 -0700190
191 RouteEntry routeEntry2 = new RouteEntry(
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800192 Ip4Prefix.valueOf("2.2.2.0/24"),
193 Ip4Address.valueOf("192.168.20.1"));
Pingpingf5d90932014-10-27 10:50:04 -0700194
195 RouteEntry routeEntry3 = new RouteEntry(
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800196 Ip4Prefix.valueOf("3.3.3.0/24"),
197 Ip4Address.valueOf("192.168.30.1"));
Pingpingf5d90932014-10-27 10:50:04 -0700198
199 RouteEntry routeEntry4 = new RouteEntry(
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800200 Ip4Prefix.valueOf("4.4.4.0/24"),
201 Ip4Address.valueOf("192.168.30.1"));
Pingpingf5d90932014-10-27 10:50:04 -0700202
203 RouteEntry routeEntry4Update = new RouteEntry(
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800204 Ip4Prefix.valueOf("4.4.4.0/24"),
205 Ip4Address.valueOf("192.168.20.1"));
Pingpingf5d90932014-10-27 10:50:04 -0700206
207 RouteEntry routeEntry5 = new RouteEntry(
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800208 Ip4Prefix.valueOf("5.5.5.0/24"),
209 Ip4Address.valueOf("192.168.10.1"));
Pingpingf5d90932014-10-27 10:50:04 -0700210
211 RouteEntry routeEntry6 = new RouteEntry(
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800212 Ip4Prefix.valueOf("6.6.6.0/24"),
213 Ip4Address.valueOf("192.168.10.1"));
Pingpingf5d90932014-10-27 10:50:04 -0700214
Jonathan Hartec2df012014-10-23 16:40:24 -0700215 RouteEntry routeEntry7 = new RouteEntry(
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800216 Ip4Prefix.valueOf("7.7.7.0/24"),
217 Ip4Address.valueOf("192.168.10.1"));
Jonathan Hartec2df012014-10-23 16:40:24 -0700218
Pingpingf5d90932014-10-27 10:50:04 -0700219 MultiPointToSinglePointIntent intent1 = intentBuilder(
220 routeEntry1.prefix(), "00:00:00:00:00:01", SW1_ETH1);
221 MultiPointToSinglePointIntent intent2 = intentBuilder(
222 routeEntry2.prefix(), "00:00:00:00:00:02", SW2_ETH1);
223 MultiPointToSinglePointIntent intent3 = intentBuilder(
224 routeEntry3.prefix(), "00:00:00:00:00:03", SW3_ETH1);
225 MultiPointToSinglePointIntent intent4 = intentBuilder(
226 routeEntry4.prefix(), "00:00:00:00:00:03", SW3_ETH1);
227 MultiPointToSinglePointIntent intent4Update = intentBuilder(
228 routeEntry4Update.prefix(), "00:00:00:00:00:02", SW2_ETH1);
229 MultiPointToSinglePointIntent intent5 = intentBuilder(
230 routeEntry5.prefix(), "00:00:00:00:00:01", SW1_ETH1);
Jonathan Hartec2df012014-10-23 16:40:24 -0700231 MultiPointToSinglePointIntent intent7 = intentBuilder(
232 routeEntry7.prefix(), "00:00:00:00:00:01", SW1_ETH1);
Pingpingf5d90932014-10-27 10:50:04 -0700233
234 // Compose a intent, which is equal to intent5 but the id is different.
235 MultiPointToSinglePointIntent intent5New =
236 staticIntentBuilder(intent5, routeEntry5, "00:00:00:00:00:01");
Ray Milkey4fd3ceb2015-12-10 14:43:08 -0800237 assertThat(IntentUtils.intentsAreEqual(intent5, intent5New), is(true));
Jonathan Hartec2df012014-10-23 16:40:24 -0700238 assertFalse(intent5.equals(intent5New));
Pingpingf5d90932014-10-27 10:50:04 -0700239
240 MultiPointToSinglePointIntent intent6 = intentBuilder(
241 routeEntry6.prefix(), "00:00:00:00:00:01", SW1_ETH1);
242
Pingpingf5d90932014-10-27 10:50:04 -0700243 // Set up expectation
Jonathan Hart90a02c22015-02-13 11:52:07 -0800244 Set<Intent> intents = new HashSet<>();
Pingpingf5d90932014-10-27 10:50:04 -0700245 intents.add(intent1);
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800246 expect(intentService.getIntentState(intent1.key()))
Jonathan Hartec2df012014-10-23 16:40:24 -0700247 .andReturn(IntentState.INSTALLED).anyTimes();
Pingpingf5d90932014-10-27 10:50:04 -0700248 intents.add(intent2);
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800249 expect(intentService.getIntentState(intent2.key()))
Jonathan Hartec2df012014-10-23 16:40:24 -0700250 .andReturn(IntentState.INSTALLED).anyTimes();
Pingpingf5d90932014-10-27 10:50:04 -0700251 intents.add(intent4);
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800252 expect(intentService.getIntentState(intent4.key()))
Jonathan Hartec2df012014-10-23 16:40:24 -0700253 .andReturn(IntentState.INSTALLED).anyTimes();
Pingpingf5d90932014-10-27 10:50:04 -0700254 intents.add(intent5);
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800255 expect(intentService.getIntentState(intent5.key()))
Jonathan Hartec2df012014-10-23 16:40:24 -0700256 .andReturn(IntentState.INSTALLED).anyTimes();
257 intents.add(intent7);
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800258 expect(intentService.getIntentState(intent7.key()))
Jonathan Hartec2df012014-10-23 16:40:24 -0700259 .andReturn(IntentState.WITHDRAWING).anyTimes();
Pingpingf5d90932014-10-27 10:50:04 -0700260 expect(intentService.getIntents()).andReturn(intents).anyTimes();
261
Jonathan Hart9a426f82015-09-03 15:43:13 +0200262 // These are the operations that should be done to the intentService
263 // during synchronization
Brian O'Connor03406a42015-02-03 17:28:57 -0800264 intentService.withdraw(intent2);
Brian O'Connor03406a42015-02-03 17:28:57 -0800265 intentService.submit(intent3);
266 intentService.submit(intent4Update);
267 intentService.submit(intent6);
268 intentService.submit(intent7);
Pingpingf5d90932014-10-27 10:50:04 -0700269 replay(intentService);
270
271 // Start the test
Pingpingf5d90932014-10-27 10:50:04 -0700272
Jonathan Hart9a426f82015-09-03 15:43:13 +0200273 // Simulate some input from the clients. The intent synchronizer has not
274 // gained the global leadership yet, but it will remember this input for
275 // when it does.
276 intentSynchronizer.submit(intent1);
277 intentSynchronizer.submit(intent2);
278 intentSynchronizer.withdraw(intent2);
279 intentSynchronizer.submit(intent3);
280 intentSynchronizer.submit(intent4);
281 intentSynchronizer.submit(intent4Update);
282 intentSynchronizer.submit(intent5);
283 intentSynchronizer.submit(intent6);
284 intentSynchronizer.submit(intent7);
285
286 // Give the leadership to the intent synchronizer. It will now attempt
287 // to synchronize the intents in the store with the intents it has
288 // recorded based on the earlier user input.
Jonathan Hart365335e2015-12-10 11:09:53 -0800289 intentSynchronizer.modifyPrimary(true);
Jonathan Hart9a426f82015-09-03 15:43:13 +0200290
291 verify(intentService);
292 }
293
294 /**
295 * Tests the behavior of the submit API, both when the synchronizer has
296 * leadership and when it does not.
297 */
298 @Test
299 public void testSubmit() {
300 IpPrefix prefix = Ip4Prefix.valueOf("1.1.1.0/24");
301 Intent intent = intentBuilder(prefix, "00:00:00:00:00:01", SW1_ETH1);
302
303 // Set up expectations
304 intentService.submit(intent);
305 expect(intentService.getIntents()).andReturn(Collections.emptyList())
306 .anyTimes();
307 replay(intentService);
308
309 // Give the intent synchronizer leadership so it will submit intents
310 // to the intent service
Jonathan Hart365335e2015-12-10 11:09:53 -0800311 intentSynchronizer.modifyPrimary(true);
Jonathan Hart9a426f82015-09-03 15:43:13 +0200312
313 // Test the submit
314 intentSynchronizer.submit(intent);
315
316 verify(intentService);
317
318 // Now we'll remove leadership from the intent synchronizer and verify
319 // that it does not submit any intents to the intent service when we
320 // call the submit API
321 reset(intentService);
322 replay(intentService);
323
Jonathan Hart365335e2015-12-10 11:09:53 -0800324 intentSynchronizer.modifyPrimary(false);
Jonathan Hart9a426f82015-09-03 15:43:13 +0200325
326 intentSynchronizer.submit(intent);
327
328 verify(intentService);
329 }
330
331 /**
332 * Tests the behavior of the withdraw API, both when the synchronizer has
333 * leadership and when it does not.
334 */
335 @Test
336 public void testWithdraw() {
337 IpPrefix prefix = Ip4Prefix.valueOf("1.1.1.0/24");
338 Intent intent = intentBuilder(prefix, "00:00:00:00:00:01", SW1_ETH1);
339
340 // Submit an intent first so we can withdraw it later
341 intentService.submit(intent);
342 intentService.withdraw(intent);
343 expect(intentService.getIntents()).andReturn(Collections.emptyList())
344 .anyTimes();
345 replay(intentService);
346
347 // Give the intent synchronizer leadership so it will submit intents
348 // to the intent service
Jonathan Hart365335e2015-12-10 11:09:53 -0800349 intentSynchronizer.modifyPrimary(true);
Jonathan Hart9a426f82015-09-03 15:43:13 +0200350
351 // Test the submit then withdraw
352 intentSynchronizer.submit(intent);
353 intentSynchronizer.withdraw(intent);
354
355 verify(intentService);
356
357 // Now we'll remove leadership from the intent synchronizer and verify
358 // that it does not withdraw any intents to the intent service when we
359 // call the withdraw API
360 reset(intentService);
361 replay(intentService);
362
Jonathan Hart365335e2015-12-10 11:09:53 -0800363 intentSynchronizer.modifyPrimary(false);
Jonathan Hart9a426f82015-09-03 15:43:13 +0200364
365 intentSynchronizer.submit(intent);
366 intentSynchronizer.withdraw(intent);
Pingpingf5d90932014-10-27 10:50:04 -0700367
368 verify(intentService);
369 }
370
371 /**
372 * MultiPointToSinglePointIntent builder.
373 *
374 * @param ipPrefix the ipPrefix to match
375 * @param nextHopMacAddress to which the destination MAC address in packet
376 * should be rewritten
377 * @param egressPoint to which packets should be sent
378 * @return the constructed MultiPointToSinglePointIntent
379 */
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800380 private MultiPointToSinglePointIntent intentBuilder(IpPrefix ipPrefix,
Pingpingf5d90932014-10-27 10:50:04 -0700381 String nextHopMacAddress, ConnectPoint egressPoint) {
382
383 TrafficSelector.Builder selectorBuilder =
384 DefaultTrafficSelector.builder();
Pavlin Radoslavov87dd9302015-03-10 13:53:24 -0700385 if (ipPrefix.isIp4()) {
Jonathan Hart9a426f82015-09-03 15:43:13 +0200386 selectorBuilder.matchEthType(Ethernet.TYPE_IPV4);
Pavlin Radoslavova8537092015-02-23 10:15:20 -0800387 selectorBuilder.matchIPDst(ipPrefix);
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800388 } else {
Jonathan Hart9a426f82015-09-03 15:43:13 +0200389 selectorBuilder.matchEthType(Ethernet.TYPE_IPV6);
Pavlin Radoslavova8537092015-02-23 10:15:20 -0800390 selectorBuilder.matchIPv6Dst(ipPrefix);
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800391 }
Pingpingf5d90932014-10-27 10:50:04 -0700392
393 TrafficTreatment.Builder treatmentBuilder =
394 DefaultTrafficTreatment.builder();
395 treatmentBuilder.setEthDst(MacAddress.valueOf(nextHopMacAddress));
396
Jonathan Hart90a02c22015-02-13 11:52:07 -0800397 Set<ConnectPoint> ingressPoints = new HashSet<>();
Jonathan Hart4cb39882015-08-12 23:50:55 -0400398 for (Interface intf : interfaces) {
399 if (!intf.connectPoint().equals(egressPoint)) {
Pingpingf5d90932014-10-27 10:50:04 -0700400 ConnectPoint srcPort = intf.connectPoint();
401 ingressPoints.add(srcPort);
402 }
403 }
404 MultiPointToSinglePointIntent intent =
Ray Milkeyebc5d222015-03-18 15:45:36 -0700405 MultiPointToSinglePointIntent.builder()
406 .appId(APPID)
Jonathan Hart9a426f82015-09-03 15:43:13 +0200407 .key(Key.of(ipPrefix.toString(), APPID))
Ray Milkeyebc5d222015-03-18 15:45:36 -0700408 .selector(selectorBuilder.build())
409 .treatment(treatmentBuilder.build())
410 .ingressPoints(ingressPoints)
411 .egressPoint(egressPoint)
412 .build();
Pingpingf5d90932014-10-27 10:50:04 -0700413 return intent;
414 }
415
416 /**
417 * A static MultiPointToSinglePointIntent builder, the returned intent is
418 * equal to the input intent except that the id is different.
419 *
Pingpingf5d90932014-10-27 10:50:04 -0700420 * @param intent the intent to be used for building a new intent
421 * @param routeEntry the relative routeEntry of the intent
422 * @return the newly constructed MultiPointToSinglePointIntent
423 * @throws TestUtilsException
424 */
Jonathan Hart9a426f82015-09-03 15:43:13 +0200425 private MultiPointToSinglePointIntent staticIntentBuilder(
Pingpingf5d90932014-10-27 10:50:04 -0700426 MultiPointToSinglePointIntent intent, RouteEntry routeEntry,
427 String nextHopMacAddress) throws TestUtilsException {
428
429 // Use a different egress ConnectPoint with that in intent
430 // to generate a different id
431 MultiPointToSinglePointIntent intentNew = intentBuilder(
432 routeEntry.prefix(), nextHopMacAddress, SW2_ETH1);
433 TestUtils.setField(intentNew, "egressPoint", intent.egressPoint());
434 TestUtils.setField(intentNew,
435 "ingressPoints", intent.ingressPoints());
436 return intentNew;
437 }
Jonathan Hart365335e2015-12-10 11:09:53 -0800438
439 private class TestIntentSynchronizer extends IntentSynchronizer {
440 @Override
441 protected ExecutorService createExecutor() {
442 return MoreExecutors.newDirectExecutorService();
443 }
444 }
445
446 private class TestCoreService extends CoreServiceAdapter {
447 @Override
448 public ApplicationId registerApplication(String name) {
449 return APPID;
450 }
451 }
452
Madan Jampani620f70d2016-01-30 22:22:47 -0800453 private class TestClusterService extends ClusterServiceAdapter {
454 @Override
455 public ControllerNode getLocalNode() {
456 return LOCAL_NODE;
457 }
458 }
459
Jonathan Hart365335e2015-12-10 11:09:53 -0800460 private class TestLeadershipService extends LeadershipServiceAdapter {
461
462 }
Pingpingf5d90932014-10-27 10:50:04 -0700463}