blob: 3d9cd2334a43822f95bb4dab8e15ed8b78f00695 [file] [log] [blame]
Pingpingf5d90932014-10-27 10:50:04 -07001package org.onlab.onos.sdnip;
2
3import static org.easymock.EasyMock.anyObject;
4import static org.easymock.EasyMock.createMock;
5import static org.easymock.EasyMock.expect;
6import static org.easymock.EasyMock.expectLastCall;
7import static org.easymock.EasyMock.replay;
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -08008import static org.easymock.EasyMock.reportMatcher;
Pingpingf5d90932014-10-27 10:50:04 -07009import static org.easymock.EasyMock.reset;
10import static org.easymock.EasyMock.verify;
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -080011import static org.hamcrest.Matchers.is;
Pingpingf5d90932014-10-27 10:50:04 -070012import static org.junit.Assert.assertEquals;
Jonathan Hartec2df012014-10-23 16:40:24 -070013import static org.junit.Assert.assertFalse;
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -080014import static org.junit.Assert.assertThat;
Pingpingf5d90932014-10-27 10:50:04 -070015import static org.junit.Assert.assertTrue;
16
17import java.util.HashSet;
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -080018import java.util.LinkedList;
19import java.util.List;
Pingpingf5d90932014-10-27 10:50:04 -070020import java.util.Set;
21import java.util.concurrent.ConcurrentHashMap;
22
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -080023import org.apache.commons.collections4.CollectionUtils;
24import org.easymock.IArgumentMatcher;
Pingpingf5d90932014-10-27 10:50:04 -070025import org.junit.Before;
26import org.junit.Test;
27import org.onlab.junit.TestUtils;
28import org.onlab.junit.TestUtils.TestUtilsException;
29import org.onlab.onos.core.ApplicationId;
30import org.onlab.onos.net.ConnectPoint;
31import org.onlab.onos.net.DefaultHost;
32import org.onlab.onos.net.DeviceId;
33import org.onlab.onos.net.Host;
34import org.onlab.onos.net.HostId;
35import org.onlab.onos.net.HostLocation;
36import org.onlab.onos.net.PortNumber;
37import org.onlab.onos.net.flow.DefaultTrafficSelector;
38import org.onlab.onos.net.flow.DefaultTrafficTreatment;
39import org.onlab.onos.net.flow.TrafficSelector;
40import org.onlab.onos.net.flow.TrafficTreatment;
41import org.onlab.onos.net.host.HostListener;
42import org.onlab.onos.net.host.HostService;
43import org.onlab.onos.net.host.InterfaceIpAddress;
44import org.onlab.onos.net.intent.Intent;
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -080045import org.onlab.onos.net.intent.IntentId;
46import org.onlab.onos.net.intent.IntentOperation;
47import org.onlab.onos.net.intent.IntentOperations;
Pingpingf5d90932014-10-27 10:50:04 -070048import org.onlab.onos.net.intent.IntentService;
Jonathan Hartec2df012014-10-23 16:40:24 -070049import org.onlab.onos.net.intent.IntentState;
Pingpingf5d90932014-10-27 10:50:04 -070050import org.onlab.onos.net.intent.MultiPointToSinglePointIntent;
51import org.onlab.onos.net.provider.ProviderId;
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -080052import org.onlab.onos.sdnip.IntentSynchronizer.IntentKey;
Pingpingf5d90932014-10-27 10:50:04 -070053import org.onlab.onos.sdnip.config.Interface;
54import org.onlab.packet.Ethernet;
55import org.onlab.packet.IpAddress;
Pavlin Radoslavov6b570732014-11-06 13:16:45 -080056import org.onlab.packet.Ip4Address;
Pingpingf5d90932014-10-27 10:50:04 -070057import org.onlab.packet.IpPrefix;
Pavlin Radoslavov6b570732014-11-06 13:16:45 -080058import org.onlab.packet.Ip4Prefix;
Pingpingf5d90932014-10-27 10:50:04 -070059import org.onlab.packet.MacAddress;
60import org.onlab.packet.VlanId;
61
62import com.google.common.collect.Sets;
63import com.googlecode.concurrenttrees.radix.node.concrete.DefaultByteArrayNodeFactory;
64import com.googlecode.concurrenttrees.radixinverted.ConcurrentInvertedRadixTree;
65import com.googlecode.concurrenttrees.radixinverted.InvertedRadixTree;
66
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 */
71public class IntentSyncTest {
72
73 private InterfaceService interfaceService;
74 private IntentService intentService;
75 private HostService hostService;
76
77 private static final ConnectPoint SW1_ETH1 = new ConnectPoint(
78 DeviceId.deviceId("of:0000000000000001"),
79 PortNumber.portNumber(1));
80
81 private static final ConnectPoint SW2_ETH1 = new ConnectPoint(
82 DeviceId.deviceId("of:0000000000000002"),
83 PortNumber.portNumber(1));
84
85 private static final ConnectPoint SW3_ETH1 = new ConnectPoint(
86 DeviceId.deviceId("of:0000000000000003"),
87 PortNumber.portNumber(1));
88
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -080089 private IntentSynchronizer intentSynchronizer;
Pingpingf5d90932014-10-27 10:50:04 -070090 private Router router;
91
92 private static final ApplicationId APPID = new ApplicationId() {
93 @Override
94 public short id() {
95 return 1;
96 }
97
98 @Override
99 public String name() {
100 return "SDNIP";
101 }
102 };
103
104 @Before
105 public void setUp() throws Exception {
106 setUpInterfaceService();
107 setUpHostService();
108 intentService = createMock(IntentService.class);
109
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800110 intentSynchronizer = new IntentSynchronizer(APPID, intentService);
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800111 router = new Router(APPID, intentSynchronizer, null, interfaceService,
112 hostService);
Pingpingf5d90932014-10-27 10:50:04 -0700113 }
114
115 /**
116 * Sets up InterfaceService.
117 */
118 private void setUpInterfaceService() {
119
120 interfaceService = createMock(InterfaceService.class);
121
122 Set<Interface> interfaces = Sets.newHashSet();
123
124 Set<InterfaceIpAddress> interfaceIpAddresses1 = Sets.newHashSet();
125 interfaceIpAddresses1.add(new InterfaceIpAddress(
126 IpAddress.valueOf("192.168.10.101"),
127 IpPrefix.valueOf("192.168.10.0/24")));
128 Interface sw1Eth1 = new Interface(SW1_ETH1,
129 interfaceIpAddresses1, MacAddress.valueOf("00:00:00:00:00:01"));
130 interfaces.add(sw1Eth1);
131
132 Set<InterfaceIpAddress> interfaceIpAddresses2 = Sets.newHashSet();
133 interfaceIpAddresses2.add(new InterfaceIpAddress(
134 IpAddress.valueOf("192.168.20.101"),
135 IpPrefix.valueOf("192.168.20.0/24")));
136 Interface sw2Eth1 = new Interface(SW2_ETH1,
137 interfaceIpAddresses2, MacAddress.valueOf("00:00:00:00:00:02"));
138 interfaces.add(sw2Eth1);
139
140 Set<InterfaceIpAddress> interfaceIpAddresses3 = Sets.newHashSet();
141 interfaceIpAddresses3.add(new InterfaceIpAddress(
142 IpAddress.valueOf("192.168.30.101"),
143 IpPrefix.valueOf("192.168.30.0/24")));
144 Interface sw3Eth1 = new Interface(SW3_ETH1,
145 interfaceIpAddresses3, MacAddress.valueOf("00:00:00:00:00:03"));
146 interfaces.add(sw3Eth1);
147
148 expect(interfaceService.getInterface(SW1_ETH1)).andReturn(
149 sw1Eth1).anyTimes();
150 expect(interfaceService.getInterface(SW2_ETH1)).andReturn(
151 sw2Eth1).anyTimes();
152 expect(interfaceService.getInterface(SW3_ETH1)).andReturn(
153 sw3Eth1).anyTimes();
154 expect(interfaceService.getInterfaces()).andReturn(
155 interfaces).anyTimes();
156 replay(interfaceService);
157 }
158
159 /**
160 * Sets up the host service with details of hosts.
161 */
162 private void setUpHostService() {
163 hostService = createMock(HostService.class);
164
165 hostService.addListener(anyObject(HostListener.class));
166 expectLastCall().anyTimes();
167
168 IpAddress host1Address = IpAddress.valueOf("192.168.10.1");
169 Host host1 = new DefaultHost(ProviderId.NONE, HostId.NONE,
170 MacAddress.valueOf("00:00:00:00:00:01"), VlanId.NONE,
171 new HostLocation(SW1_ETH1, 1),
172 Sets.newHashSet(host1Address));
173
174 expect(hostService.getHostsByIp(host1Address))
175 .andReturn(Sets.newHashSet(host1)).anyTimes();
176 hostService.startMonitoringIp(host1Address);
177 expectLastCall().anyTimes();
178
179
180 IpAddress host2Address = IpAddress.valueOf("192.168.20.1");
181 Host host2 = new DefaultHost(ProviderId.NONE, HostId.NONE,
182 MacAddress.valueOf("00:00:00:00:00:02"), VlanId.NONE,
183 new HostLocation(SW2_ETH1, 1),
184 Sets.newHashSet(host2Address));
185
186 expect(hostService.getHostsByIp(host2Address))
187 .andReturn(Sets.newHashSet(host2)).anyTimes();
188 hostService.startMonitoringIp(host2Address);
189 expectLastCall().anyTimes();
190
191
192 IpAddress host3Address = IpAddress.valueOf("192.168.30.1");
193 Host host3 = new DefaultHost(ProviderId.NONE, HostId.NONE,
194 MacAddress.valueOf("00:00:00:00:00:03"), VlanId.NONE,
195 new HostLocation(SW3_ETH1, 1),
196 Sets.newHashSet(host3Address));
197
198 expect(hostService.getHostsByIp(host3Address))
199 .andReturn(Sets.newHashSet(host3)).anyTimes();
200 hostService.startMonitoringIp(host3Address);
201 expectLastCall().anyTimes();
202
203
204 replay(hostService);
205 }
206
207 /**
208 * This method tests the behavior of intent Synchronizer.
209 *
210 * @throws TestUtilsException
211 */
212 @Test
213 public void testIntentSync() throws TestUtilsException {
214
215 //
216 // Construct routes and intents.
217 // This test simulates the following cases during the master change
218 // time interval:
219 // 1. RouteEntry1 did not change and the intent also did not change.
220 // 2. RouteEntry2 was deleted, but the intent was not deleted.
221 // 3. RouteEntry3 was newly added, and the intent was also submitted.
222 // 4. RouteEntry4 was updated to RouteEntry4Update, and the intent was
223 // also updated to a new one.
224 // 5. RouteEntry5 did not change, but its intent id changed.
225 // 6. RouteEntry6 was newly added, but the intent was not submitted.
226 //
227 RouteEntry routeEntry1 = new RouteEntry(
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800228 Ip4Prefix.valueOf("1.1.1.0/24"),
229 Ip4Address.valueOf("192.168.10.1"));
Pingpingf5d90932014-10-27 10:50:04 -0700230
231 RouteEntry routeEntry2 = new RouteEntry(
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800232 Ip4Prefix.valueOf("2.2.2.0/24"),
233 Ip4Address.valueOf("192.168.20.1"));
Pingpingf5d90932014-10-27 10:50:04 -0700234
235 RouteEntry routeEntry3 = new RouteEntry(
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800236 Ip4Prefix.valueOf("3.3.3.0/24"),
237 Ip4Address.valueOf("192.168.30.1"));
Pingpingf5d90932014-10-27 10:50:04 -0700238
239 RouteEntry routeEntry4 = new RouteEntry(
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800240 Ip4Prefix.valueOf("4.4.4.0/24"),
241 Ip4Address.valueOf("192.168.30.1"));
Pingpingf5d90932014-10-27 10:50:04 -0700242
243 RouteEntry routeEntry4Update = new RouteEntry(
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800244 Ip4Prefix.valueOf("4.4.4.0/24"),
245 Ip4Address.valueOf("192.168.20.1"));
Pingpingf5d90932014-10-27 10:50:04 -0700246
247 RouteEntry routeEntry5 = new RouteEntry(
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800248 Ip4Prefix.valueOf("5.5.5.0/24"),
249 Ip4Address.valueOf("192.168.10.1"));
Pingpingf5d90932014-10-27 10:50:04 -0700250
251 RouteEntry routeEntry6 = new RouteEntry(
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800252 Ip4Prefix.valueOf("6.6.6.0/24"),
253 Ip4Address.valueOf("192.168.10.1"));
Pingpingf5d90932014-10-27 10:50:04 -0700254
Jonathan Hartec2df012014-10-23 16:40:24 -0700255 RouteEntry routeEntry7 = new RouteEntry(
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800256 Ip4Prefix.valueOf("7.7.7.0/24"),
257 Ip4Address.valueOf("192.168.10.1"));
Jonathan Hartec2df012014-10-23 16:40:24 -0700258
Pingpingf5d90932014-10-27 10:50:04 -0700259 MultiPointToSinglePointIntent intent1 = intentBuilder(
260 routeEntry1.prefix(), "00:00:00:00:00:01", SW1_ETH1);
261 MultiPointToSinglePointIntent intent2 = intentBuilder(
262 routeEntry2.prefix(), "00:00:00:00:00:02", SW2_ETH1);
263 MultiPointToSinglePointIntent intent3 = intentBuilder(
264 routeEntry3.prefix(), "00:00:00:00:00:03", SW3_ETH1);
265 MultiPointToSinglePointIntent intent4 = intentBuilder(
266 routeEntry4.prefix(), "00:00:00:00:00:03", SW3_ETH1);
267 MultiPointToSinglePointIntent intent4Update = intentBuilder(
268 routeEntry4Update.prefix(), "00:00:00:00:00:02", SW2_ETH1);
269 MultiPointToSinglePointIntent intent5 = intentBuilder(
270 routeEntry5.prefix(), "00:00:00:00:00:01", SW1_ETH1);
Jonathan Hartec2df012014-10-23 16:40:24 -0700271 MultiPointToSinglePointIntent intent7 = intentBuilder(
272 routeEntry7.prefix(), "00:00:00:00:00:01", SW1_ETH1);
Pingpingf5d90932014-10-27 10:50:04 -0700273
274 // Compose a intent, which is equal to intent5 but the id is different.
275 MultiPointToSinglePointIntent intent5New =
276 staticIntentBuilder(intent5, routeEntry5, "00:00:00:00:00:01");
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800277 assertThat(IntentSynchronizer.IntentKey.equalIntents(
278 intent5, intent5New),
279 is(true));
Jonathan Hartec2df012014-10-23 16:40:24 -0700280 assertFalse(intent5.equals(intent5New));
Pingpingf5d90932014-10-27 10:50:04 -0700281
282 MultiPointToSinglePointIntent intent6 = intentBuilder(
283 routeEntry6.prefix(), "00:00:00:00:00:01", SW1_ETH1);
284
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800285 // Set up the bgpRoutes field in Router class and routeIntents fields
286 // in IntentSynchronizer class
Pingpingf5d90932014-10-27 10:50:04 -0700287 InvertedRadixTree<RouteEntry> bgpRoutes =
288 new ConcurrentInvertedRadixTree<>(
289 new DefaultByteArrayNodeFactory());
290 bgpRoutes.put(RouteEntry.createBinaryString(routeEntry1.prefix()),
291 routeEntry1);
292 bgpRoutes.put(RouteEntry.createBinaryString(routeEntry3.prefix()),
293 routeEntry3);
294 bgpRoutes.put(RouteEntry.createBinaryString(routeEntry4Update.prefix()),
295 routeEntry4Update);
296 bgpRoutes.put(RouteEntry.createBinaryString(routeEntry5.prefix()),
297 routeEntry5);
298 bgpRoutes.put(RouteEntry.createBinaryString(routeEntry6.prefix()),
299 routeEntry6);
Jonathan Hartec2df012014-10-23 16:40:24 -0700300 bgpRoutes.put(RouteEntry.createBinaryString(routeEntry7.prefix()),
301 routeEntry7);
Pingpingf5d90932014-10-27 10:50:04 -0700302 TestUtils.setField(router, "bgpRoutes", bgpRoutes);
303
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800304 ConcurrentHashMap<Ip4Prefix, MultiPointToSinglePointIntent>
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800305 routeIntents = new ConcurrentHashMap<>();
306 routeIntents.put(routeEntry1.prefix(), intent1);
307 routeIntents.put(routeEntry3.prefix(), intent3);
308 routeIntents.put(routeEntry4Update.prefix(), intent4Update);
309 routeIntents.put(routeEntry5.prefix(), intent5New);
310 routeIntents.put(routeEntry6.prefix(), intent6);
311 routeIntents.put(routeEntry7.prefix(), intent7);
312 TestUtils.setField(intentSynchronizer, "routeIntents", routeIntents);
Pingpingf5d90932014-10-27 10:50:04 -0700313
314 // Set up expectation
315 reset(intentService);
316 Set<Intent> intents = new HashSet<Intent>();
317 intents.add(intent1);
Jonathan Hartec2df012014-10-23 16:40:24 -0700318 expect(intentService.getIntentState(intent1.id()))
319 .andReturn(IntentState.INSTALLED).anyTimes();
Pingpingf5d90932014-10-27 10:50:04 -0700320 intents.add(intent2);
Jonathan Hartec2df012014-10-23 16:40:24 -0700321 expect(intentService.getIntentState(intent2.id()))
322 .andReturn(IntentState.INSTALLED).anyTimes();
Pingpingf5d90932014-10-27 10:50:04 -0700323 intents.add(intent4);
Jonathan Hartec2df012014-10-23 16:40:24 -0700324 expect(intentService.getIntentState(intent4.id()))
325 .andReturn(IntentState.INSTALLED).anyTimes();
Pingpingf5d90932014-10-27 10:50:04 -0700326 intents.add(intent5);
Jonathan Hartec2df012014-10-23 16:40:24 -0700327 expect(intentService.getIntentState(intent5.id()))
328 .andReturn(IntentState.INSTALLED).anyTimes();
329 intents.add(intent7);
330 expect(intentService.getIntentState(intent7.id()))
331 .andReturn(IntentState.WITHDRAWING).anyTimes();
Pingpingf5d90932014-10-27 10:50:04 -0700332 expect(intentService.getIntents()).andReturn(intents).anyTimes();
333
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800334 IntentOperations.Builder builder = IntentOperations.builder();
335 builder.addWithdrawOperation(intent2.id());
336 builder.addWithdrawOperation(intent4.id());
337 intentService.execute(eqExceptId(builder.build()));
338
339 builder = IntentOperations.builder();
340 builder.addSubmitOperation(intent3);
341 builder.addSubmitOperation(intent4Update);
342 builder.addSubmitOperation(intent6);
343 builder.addSubmitOperation(intent7);
344 intentService.execute(eqExceptId(builder.build()));
Pingpingf5d90932014-10-27 10:50:04 -0700345 replay(intentService);
346
347 // Start the test
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800348 intentSynchronizer.leaderChanged(true);
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800349 /*
350 TestUtils.callMethod(intentSynchronizer, "synchronizeIntents",
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800351 new Class<?>[] {});
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800352 */
353 intentSynchronizer.synchronizeIntents();
Pingpingf5d90932014-10-27 10:50:04 -0700354
355 // Verify
Jonathan Hartec2df012014-10-23 16:40:24 -0700356 assertEquals(router.getRoutes().size(), 6);
Pingpingf5d90932014-10-27 10:50:04 -0700357 assertTrue(router.getRoutes().contains(routeEntry1));
358 assertTrue(router.getRoutes().contains(routeEntry3));
359 assertTrue(router.getRoutes().contains(routeEntry4Update));
360 assertTrue(router.getRoutes().contains(routeEntry5));
361 assertTrue(router.getRoutes().contains(routeEntry6));
362
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800363 assertEquals(intentSynchronizer.getRouteIntents().size(), 6);
364 assertTrue(intentSynchronizer.getRouteIntents().contains(intent1));
365 assertTrue(intentSynchronizer.getRouteIntents().contains(intent3));
366 assertTrue(intentSynchronizer.getRouteIntents().contains(intent4Update));
367 assertTrue(intentSynchronizer.getRouteIntents().contains(intent5));
368 assertTrue(intentSynchronizer.getRouteIntents().contains(intent6));
Pingpingf5d90932014-10-27 10:50:04 -0700369
370 verify(intentService);
371 }
372
373 /**
374 * MultiPointToSinglePointIntent builder.
375 *
376 * @param ipPrefix the ipPrefix to match
377 * @param nextHopMacAddress to which the destination MAC address in packet
378 * should be rewritten
379 * @param egressPoint to which packets should be sent
380 * @return the constructed MultiPointToSinglePointIntent
381 */
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800382 private MultiPointToSinglePointIntent intentBuilder(Ip4Prefix ipPrefix,
Pingpingf5d90932014-10-27 10:50:04 -0700383 String nextHopMacAddress, ConnectPoint egressPoint) {
384
385 TrafficSelector.Builder selectorBuilder =
386 DefaultTrafficSelector.builder();
387 selectorBuilder.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipPrefix);
388
389 TrafficTreatment.Builder treatmentBuilder =
390 DefaultTrafficTreatment.builder();
391 treatmentBuilder.setEthDst(MacAddress.valueOf(nextHopMacAddress));
392
393 Set<ConnectPoint> ingressPoints = new HashSet<ConnectPoint>();
394 for (Interface intf : interfaceService.getInterfaces()) {
395 if (!intf.equals(interfaceService.getInterface(egressPoint))) {
396 ConnectPoint srcPort = intf.connectPoint();
397 ingressPoints.add(srcPort);
398 }
399 }
400 MultiPointToSinglePointIntent intent =
401 new MultiPointToSinglePointIntent(APPID,
402 selectorBuilder.build(), treatmentBuilder.build(),
403 ingressPoints, egressPoint);
404 return intent;
405 }
406
407 /**
408 * A static MultiPointToSinglePointIntent builder, the returned intent is
409 * equal to the input intent except that the id is different.
410 *
411 *
412 * @param intent the intent to be used for building a new intent
413 * @param routeEntry the relative routeEntry of the intent
414 * @return the newly constructed MultiPointToSinglePointIntent
415 * @throws TestUtilsException
416 */
417 private MultiPointToSinglePointIntent staticIntentBuilder(
418 MultiPointToSinglePointIntent intent, RouteEntry routeEntry,
419 String nextHopMacAddress) throws TestUtilsException {
420
421 // Use a different egress ConnectPoint with that in intent
422 // to generate a different id
423 MultiPointToSinglePointIntent intentNew = intentBuilder(
424 routeEntry.prefix(), nextHopMacAddress, SW2_ETH1);
425 TestUtils.setField(intentNew, "egressPoint", intent.egressPoint());
426 TestUtils.setField(intentNew,
427 "ingressPoints", intent.ingressPoints());
428 return intentNew;
429 }
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800430
431 /*
432 * EasyMock matcher that matches {@link IntenOperations} but
433 * ignores the {@link IntentId} when matching.
434 * <p/>
435 * The normal intent equals method tests that the intent IDs are equal,
436 * however in these tests we can't know what the intent IDs will be in
437 * advance, so we can't set up expected intents with the correct IDs. Thus,
438 * the solution is to use an EasyMock matcher that verifies that all the
439 * value properties of the provided intent match the expected values, but
440 * ignores the intent ID when testing equality.
441 */
442 private static final class IdAgnosticIntentOperationsMatcher implements
443 IArgumentMatcher {
444
445 private final IntentOperations intentOperations;
446 private String providedString;
447
448 /**
449 * Constructor taking the expected intent operations to match against.
450 *
451 * @param intentOperations the expected intent operations
452 */
453 public IdAgnosticIntentOperationsMatcher(
454 IntentOperations intentOperations) {
455 this.intentOperations = intentOperations;
456 }
457
458 @Override
459 public void appendTo(StringBuffer strBuffer) {
460 strBuffer.append("IntentOperationsMatcher unable to match: "
461 + providedString);
462 }
463
464 @Override
465 public boolean matches(Object object) {
466 if (!(object instanceof IntentOperations)) {
467 return false;
468 }
469
470 IntentOperations providedIntentOperations =
471 (IntentOperations) object;
472 providedString = providedIntentOperations.toString();
473
474 List<IntentKey> thisSubmitIntents = new LinkedList<>();
475 List<IntentId> thisWithdrawIntentIds = new LinkedList<>();
476 List<IntentKey> thisReplaceIntents = new LinkedList<>();
477 List<IntentKey> thisUpdateIntents = new LinkedList<>();
478 List<IntentKey> providedSubmitIntents = new LinkedList<>();
479 List<IntentId> providedWithdrawIntentIds = new LinkedList<>();
480 List<IntentKey> providedReplaceIntents = new LinkedList<>();
481 List<IntentKey> providedUpdateIntents = new LinkedList<>();
482
483 extractIntents(intentOperations, thisSubmitIntents,
484 thisWithdrawIntentIds, thisReplaceIntents,
485 thisUpdateIntents);
486 extractIntents(providedIntentOperations, providedSubmitIntents,
487 providedWithdrawIntentIds, providedReplaceIntents,
488 providedUpdateIntents);
489
490 return CollectionUtils.isEqualCollection(thisSubmitIntents,
491 providedSubmitIntents) &&
492 CollectionUtils.isEqualCollection(thisWithdrawIntentIds,
493 providedWithdrawIntentIds) &&
494 CollectionUtils.isEqualCollection(thisUpdateIntents,
495 providedUpdateIntents) &&
496 CollectionUtils.isEqualCollection(thisReplaceIntents,
497 providedReplaceIntents);
498 }
499
500 /**
501 * Extracts the intents per operation type. Each intent is encapsulated
502 * in IntentKey so it can be compared by excluding the Intent ID.
503 *
504 * @param intentOperations the container with the intent operations
505 * to extract the intents from
506 * @param submitIntents the SUBMIT intents
507 * @param withdrawIntentIds the WITHDRAW intents IDs
508 * @param replaceIntents the REPLACE intents
509 * @param updateIntents the UPDATE intens
510 */
511 private void extractIntents(IntentOperations intentOperations,
512 List<IntentKey> submitIntents,
513 List<IntentId> withdrawIntentIds,
514 List<IntentKey> replaceIntents,
515 List<IntentKey> updateIntents) {
516 for (IntentOperation oper : intentOperations.operations()) {
517 IntentId intentId;
518 IntentKey intentKey;
519 switch (oper.type()) {
520 case SUBMIT:
521 intentKey = new IntentKey(oper.intent());
522 submitIntents.add(intentKey);
523 break;
524 case WITHDRAW:
525 intentId = oper.intentId();
526 withdrawIntentIds.add(intentId);
527 break;
528 case REPLACE:
529 intentKey = new IntentKey(oper.intent());
530 replaceIntents.add(intentKey);
531 break;
532 case UPDATE:
533 intentKey = new IntentKey(oper.intent());
534 updateIntents.add(intentKey);
535 break;
536 default:
537 break;
538 }
539 }
540 }
541 }
542
543 /**
544 * Matcher method to set an expected intent to match against (ignoring the
545 * the intent ID).
546 *
547 * @param intent the expected intent
548 * @return something of type IntentOperations
549 */
550 private static IntentOperations eqExceptId(
551 IntentOperations intentOperations) {
552 reportMatcher(new IdAgnosticIntentOperationsMatcher(intentOperations));
553 return intentOperations;
554 }
Pingpingf5d90932014-10-27 10:50:04 -0700555}