blob: 4b29ba8840554f9c63f4c710d0bd1a9ad76d4e9c [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;
8import static org.easymock.EasyMock.reset;
9import static org.easymock.EasyMock.verify;
10import static org.junit.Assert.assertEquals;
Jonathan Hartec2df012014-10-23 16:40:24 -070011import static org.junit.Assert.assertFalse;
Pingpingf5d90932014-10-27 10:50:04 -070012import static org.junit.Assert.assertTrue;
13
14import java.util.HashSet;
15import java.util.Set;
16import java.util.concurrent.ConcurrentHashMap;
17
18import org.junit.Before;
19import org.junit.Test;
20import org.onlab.junit.TestUtils;
21import org.onlab.junit.TestUtils.TestUtilsException;
22import org.onlab.onos.core.ApplicationId;
23import org.onlab.onos.net.ConnectPoint;
24import org.onlab.onos.net.DefaultHost;
25import org.onlab.onos.net.DeviceId;
26import org.onlab.onos.net.Host;
27import org.onlab.onos.net.HostId;
28import org.onlab.onos.net.HostLocation;
29import org.onlab.onos.net.PortNumber;
30import org.onlab.onos.net.flow.DefaultTrafficSelector;
31import org.onlab.onos.net.flow.DefaultTrafficTreatment;
32import org.onlab.onos.net.flow.TrafficSelector;
33import org.onlab.onos.net.flow.TrafficTreatment;
34import org.onlab.onos.net.host.HostListener;
35import org.onlab.onos.net.host.HostService;
36import org.onlab.onos.net.host.InterfaceIpAddress;
37import org.onlab.onos.net.intent.Intent;
38import org.onlab.onos.net.intent.IntentService;
Jonathan Hartec2df012014-10-23 16:40:24 -070039import org.onlab.onos.net.intent.IntentState;
Pingpingf5d90932014-10-27 10:50:04 -070040import org.onlab.onos.net.intent.MultiPointToSinglePointIntent;
41import org.onlab.onos.net.provider.ProviderId;
42import org.onlab.onos.sdnip.config.Interface;
43import org.onlab.packet.Ethernet;
44import org.onlab.packet.IpAddress;
Pavlin Radoslavov6b570732014-11-06 13:16:45 -080045import org.onlab.packet.Ip4Address;
Pingpingf5d90932014-10-27 10:50:04 -070046import org.onlab.packet.IpPrefix;
Pavlin Radoslavov6b570732014-11-06 13:16:45 -080047import org.onlab.packet.Ip4Prefix;
Pingpingf5d90932014-10-27 10:50:04 -070048import org.onlab.packet.MacAddress;
49import org.onlab.packet.VlanId;
50
51import com.google.common.collect.Sets;
52import com.googlecode.concurrenttrees.radix.node.concrete.DefaultByteArrayNodeFactory;
53import com.googlecode.concurrenttrees.radixinverted.ConcurrentInvertedRadixTree;
54import com.googlecode.concurrenttrees.radixinverted.InvertedRadixTree;
55
56/**
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -080057 * This class tests the intent synchronization function in the
58 * IntentSynchronizer class.
Pingpingf5d90932014-10-27 10:50:04 -070059 */
60public class IntentSyncTest {
61
62 private InterfaceService interfaceService;
63 private IntentService intentService;
64 private HostService hostService;
65
66 private static final ConnectPoint SW1_ETH1 = new ConnectPoint(
67 DeviceId.deviceId("of:0000000000000001"),
68 PortNumber.portNumber(1));
69
70 private static final ConnectPoint SW2_ETH1 = new ConnectPoint(
71 DeviceId.deviceId("of:0000000000000002"),
72 PortNumber.portNumber(1));
73
74 private static final ConnectPoint SW3_ETH1 = new ConnectPoint(
75 DeviceId.deviceId("of:0000000000000003"),
76 PortNumber.portNumber(1));
77
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -080078 private IntentSynchronizer intentSynchronizer;
Pingpingf5d90932014-10-27 10:50:04 -070079 private Router router;
80
81 private static final ApplicationId APPID = new ApplicationId() {
82 @Override
83 public short id() {
84 return 1;
85 }
86
87 @Override
88 public String name() {
89 return "SDNIP";
90 }
91 };
92
93 @Before
94 public void setUp() throws Exception {
95 setUpInterfaceService();
96 setUpHostService();
97 intentService = createMock(IntentService.class);
98
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -080099 intentSynchronizer = new IntentSynchronizer(APPID, intentService);
100 router = new Router(APPID, intentSynchronizer,
Pingpingf5d90932014-10-27 10:50:04 -0700101 hostService, null, interfaceService);
102 }
103
104 /**
105 * Sets up InterfaceService.
106 */
107 private void setUpInterfaceService() {
108
109 interfaceService = createMock(InterfaceService.class);
110
111 Set<Interface> interfaces = Sets.newHashSet();
112
113 Set<InterfaceIpAddress> interfaceIpAddresses1 = Sets.newHashSet();
114 interfaceIpAddresses1.add(new InterfaceIpAddress(
115 IpAddress.valueOf("192.168.10.101"),
116 IpPrefix.valueOf("192.168.10.0/24")));
117 Interface sw1Eth1 = new Interface(SW1_ETH1,
118 interfaceIpAddresses1, MacAddress.valueOf("00:00:00:00:00:01"));
119 interfaces.add(sw1Eth1);
120
121 Set<InterfaceIpAddress> interfaceIpAddresses2 = Sets.newHashSet();
122 interfaceIpAddresses2.add(new InterfaceIpAddress(
123 IpAddress.valueOf("192.168.20.101"),
124 IpPrefix.valueOf("192.168.20.0/24")));
125 Interface sw2Eth1 = new Interface(SW2_ETH1,
126 interfaceIpAddresses2, MacAddress.valueOf("00:00:00:00:00:02"));
127 interfaces.add(sw2Eth1);
128
129 Set<InterfaceIpAddress> interfaceIpAddresses3 = Sets.newHashSet();
130 interfaceIpAddresses3.add(new InterfaceIpAddress(
131 IpAddress.valueOf("192.168.30.101"),
132 IpPrefix.valueOf("192.168.30.0/24")));
133 Interface sw3Eth1 = new Interface(SW3_ETH1,
134 interfaceIpAddresses3, MacAddress.valueOf("00:00:00:00:00:03"));
135 interfaces.add(sw3Eth1);
136
137 expect(interfaceService.getInterface(SW1_ETH1)).andReturn(
138 sw1Eth1).anyTimes();
139 expect(interfaceService.getInterface(SW2_ETH1)).andReturn(
140 sw2Eth1).anyTimes();
141 expect(interfaceService.getInterface(SW3_ETH1)).andReturn(
142 sw3Eth1).anyTimes();
143 expect(interfaceService.getInterfaces()).andReturn(
144 interfaces).anyTimes();
145 replay(interfaceService);
146 }
147
148 /**
149 * Sets up the host service with details of hosts.
150 */
151 private void setUpHostService() {
152 hostService = createMock(HostService.class);
153
154 hostService.addListener(anyObject(HostListener.class));
155 expectLastCall().anyTimes();
156
157 IpAddress host1Address = IpAddress.valueOf("192.168.10.1");
158 Host host1 = new DefaultHost(ProviderId.NONE, HostId.NONE,
159 MacAddress.valueOf("00:00:00:00:00:01"), VlanId.NONE,
160 new HostLocation(SW1_ETH1, 1),
161 Sets.newHashSet(host1Address));
162
163 expect(hostService.getHostsByIp(host1Address))
164 .andReturn(Sets.newHashSet(host1)).anyTimes();
165 hostService.startMonitoringIp(host1Address);
166 expectLastCall().anyTimes();
167
168
169 IpAddress host2Address = IpAddress.valueOf("192.168.20.1");
170 Host host2 = new DefaultHost(ProviderId.NONE, HostId.NONE,
171 MacAddress.valueOf("00:00:00:00:00:02"), VlanId.NONE,
172 new HostLocation(SW2_ETH1, 1),
173 Sets.newHashSet(host2Address));
174
175 expect(hostService.getHostsByIp(host2Address))
176 .andReturn(Sets.newHashSet(host2)).anyTimes();
177 hostService.startMonitoringIp(host2Address);
178 expectLastCall().anyTimes();
179
180
181 IpAddress host3Address = IpAddress.valueOf("192.168.30.1");
182 Host host3 = new DefaultHost(ProviderId.NONE, HostId.NONE,
183 MacAddress.valueOf("00:00:00:00:00:03"), VlanId.NONE,
184 new HostLocation(SW3_ETH1, 1),
185 Sets.newHashSet(host3Address));
186
187 expect(hostService.getHostsByIp(host3Address))
188 .andReturn(Sets.newHashSet(host3)).anyTimes();
189 hostService.startMonitoringIp(host3Address);
190 expectLastCall().anyTimes();
191
192
193 replay(hostService);
194 }
195
196 /**
197 * This method tests the behavior of intent Synchronizer.
198 *
199 * @throws TestUtilsException
200 */
201 @Test
202 public void testIntentSync() throws TestUtilsException {
203
204 //
205 // Construct routes and intents.
206 // This test simulates the following cases during the master change
207 // time interval:
208 // 1. RouteEntry1 did not change and the intent also did not change.
209 // 2. RouteEntry2 was deleted, but the intent was not deleted.
210 // 3. RouteEntry3 was newly added, and the intent was also submitted.
211 // 4. RouteEntry4 was updated to RouteEntry4Update, and the intent was
212 // also updated to a new one.
213 // 5. RouteEntry5 did not change, but its intent id changed.
214 // 6. RouteEntry6 was newly added, but the intent was not submitted.
215 //
216 RouteEntry routeEntry1 = new RouteEntry(
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800217 Ip4Prefix.valueOf("1.1.1.0/24"),
218 Ip4Address.valueOf("192.168.10.1"));
Pingpingf5d90932014-10-27 10:50:04 -0700219
220 RouteEntry routeEntry2 = new RouteEntry(
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800221 Ip4Prefix.valueOf("2.2.2.0/24"),
222 Ip4Address.valueOf("192.168.20.1"));
Pingpingf5d90932014-10-27 10:50:04 -0700223
224 RouteEntry routeEntry3 = new RouteEntry(
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800225 Ip4Prefix.valueOf("3.3.3.0/24"),
226 Ip4Address.valueOf("192.168.30.1"));
Pingpingf5d90932014-10-27 10:50:04 -0700227
228 RouteEntry routeEntry4 = new RouteEntry(
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800229 Ip4Prefix.valueOf("4.4.4.0/24"),
230 Ip4Address.valueOf("192.168.30.1"));
Pingpingf5d90932014-10-27 10:50:04 -0700231
232 RouteEntry routeEntry4Update = new RouteEntry(
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800233 Ip4Prefix.valueOf("4.4.4.0/24"),
234 Ip4Address.valueOf("192.168.20.1"));
Pingpingf5d90932014-10-27 10:50:04 -0700235
236 RouteEntry routeEntry5 = new RouteEntry(
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800237 Ip4Prefix.valueOf("5.5.5.0/24"),
238 Ip4Address.valueOf("192.168.10.1"));
Pingpingf5d90932014-10-27 10:50:04 -0700239
240 RouteEntry routeEntry6 = new RouteEntry(
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800241 Ip4Prefix.valueOf("6.6.6.0/24"),
242 Ip4Address.valueOf("192.168.10.1"));
Pingpingf5d90932014-10-27 10:50:04 -0700243
Jonathan Hartec2df012014-10-23 16:40:24 -0700244 RouteEntry routeEntry7 = new RouteEntry(
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800245 Ip4Prefix.valueOf("7.7.7.0/24"),
246 Ip4Address.valueOf("192.168.10.1"));
Jonathan Hartec2df012014-10-23 16:40:24 -0700247
Pingpingf5d90932014-10-27 10:50:04 -0700248 MultiPointToSinglePointIntent intent1 = intentBuilder(
249 routeEntry1.prefix(), "00:00:00:00:00:01", SW1_ETH1);
250 MultiPointToSinglePointIntent intent2 = intentBuilder(
251 routeEntry2.prefix(), "00:00:00:00:00:02", SW2_ETH1);
252 MultiPointToSinglePointIntent intent3 = intentBuilder(
253 routeEntry3.prefix(), "00:00:00:00:00:03", SW3_ETH1);
254 MultiPointToSinglePointIntent intent4 = intentBuilder(
255 routeEntry4.prefix(), "00:00:00:00:00:03", SW3_ETH1);
256 MultiPointToSinglePointIntent intent4Update = intentBuilder(
257 routeEntry4Update.prefix(), "00:00:00:00:00:02", SW2_ETH1);
258 MultiPointToSinglePointIntent intent5 = intentBuilder(
259 routeEntry5.prefix(), "00:00:00:00:00:01", SW1_ETH1);
Jonathan Hartec2df012014-10-23 16:40:24 -0700260 MultiPointToSinglePointIntent intent7 = intentBuilder(
261 routeEntry7.prefix(), "00:00:00:00:00:01", SW1_ETH1);
Pingpingf5d90932014-10-27 10:50:04 -0700262
263 // Compose a intent, which is equal to intent5 but the id is different.
264 MultiPointToSinglePointIntent intent5New =
265 staticIntentBuilder(intent5, routeEntry5, "00:00:00:00:00:01");
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800266 assertTrue(TestUtils.callMethod(intentSynchronizer,
Pingpingf5d90932014-10-27 10:50:04 -0700267 "compareMultiPointToSinglePointIntents",
268 new Class<?>[] {MultiPointToSinglePointIntent.class,
269 MultiPointToSinglePointIntent.class},
270 intent5, intent5New).equals(true));
Jonathan Hartec2df012014-10-23 16:40:24 -0700271 assertFalse(intent5.equals(intent5New));
Pingpingf5d90932014-10-27 10:50:04 -0700272
273 MultiPointToSinglePointIntent intent6 = intentBuilder(
274 routeEntry6.prefix(), "00:00:00:00:00:01", SW1_ETH1);
275
276 // Set up the bgpRoutes and pushedRouteIntents fields in Router class
277 InvertedRadixTree<RouteEntry> bgpRoutes =
278 new ConcurrentInvertedRadixTree<>(
279 new DefaultByteArrayNodeFactory());
280 bgpRoutes.put(RouteEntry.createBinaryString(routeEntry1.prefix()),
281 routeEntry1);
282 bgpRoutes.put(RouteEntry.createBinaryString(routeEntry3.prefix()),
283 routeEntry3);
284 bgpRoutes.put(RouteEntry.createBinaryString(routeEntry4Update.prefix()),
285 routeEntry4Update);
286 bgpRoutes.put(RouteEntry.createBinaryString(routeEntry5.prefix()),
287 routeEntry5);
288 bgpRoutes.put(RouteEntry.createBinaryString(routeEntry6.prefix()),
289 routeEntry6);
Jonathan Hartec2df012014-10-23 16:40:24 -0700290 bgpRoutes.put(RouteEntry.createBinaryString(routeEntry7.prefix()),
291 routeEntry7);
Pingpingf5d90932014-10-27 10:50:04 -0700292 TestUtils.setField(router, "bgpRoutes", bgpRoutes);
293
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800294 ConcurrentHashMap<Ip4Prefix, MultiPointToSinglePointIntent>
Pingpingf5d90932014-10-27 10:50:04 -0700295 pushedRouteIntents = new ConcurrentHashMap<>();
296 pushedRouteIntents.put(routeEntry1.prefix(), intent1);
297 pushedRouteIntents.put(routeEntry3.prefix(), intent3);
298 pushedRouteIntents.put(routeEntry4Update.prefix(), intent4Update);
299 pushedRouteIntents.put(routeEntry5.prefix(), intent5New);
300 pushedRouteIntents.put(routeEntry6.prefix(), intent6);
Jonathan Hartec2df012014-10-23 16:40:24 -0700301 pushedRouteIntents.put(routeEntry7.prefix(), intent7);
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800302 TestUtils.setField(intentSynchronizer, "pushedRouteIntents",
303 pushedRouteIntents);
Pingpingf5d90932014-10-27 10:50:04 -0700304
305 // Set up expectation
306 reset(intentService);
307 Set<Intent> intents = new HashSet<Intent>();
308 intents.add(intent1);
Jonathan Hartec2df012014-10-23 16:40:24 -0700309 expect(intentService.getIntentState(intent1.id()))
310 .andReturn(IntentState.INSTALLED).anyTimes();
Pingpingf5d90932014-10-27 10:50:04 -0700311 intents.add(intent2);
Jonathan Hartec2df012014-10-23 16:40:24 -0700312 expect(intentService.getIntentState(intent2.id()))
313 .andReturn(IntentState.INSTALLED).anyTimes();
Pingpingf5d90932014-10-27 10:50:04 -0700314 intents.add(intent4);
Jonathan Hartec2df012014-10-23 16:40:24 -0700315 expect(intentService.getIntentState(intent4.id()))
316 .andReturn(IntentState.INSTALLED).anyTimes();
Pingpingf5d90932014-10-27 10:50:04 -0700317 intents.add(intent5);
Jonathan Hartec2df012014-10-23 16:40:24 -0700318 expect(intentService.getIntentState(intent5.id()))
319 .andReturn(IntentState.INSTALLED).anyTimes();
320 intents.add(intent7);
321 expect(intentService.getIntentState(intent7.id()))
322 .andReturn(IntentState.WITHDRAWING).anyTimes();
Pingpingf5d90932014-10-27 10:50:04 -0700323 expect(intentService.getIntents()).andReturn(intents).anyTimes();
324
325 intentService.withdraw(intent2);
326 intentService.submit(intent3);
327 intentService.withdraw(intent4);
328 intentService.submit(intent4Update);
329 intentService.submit(intent6);
Jonathan Hartec2df012014-10-23 16:40:24 -0700330 intentService.submit(intent7);
Pingpingf5d90932014-10-27 10:50:04 -0700331 replay(intentService);
332
333 // Start the test
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800334 intentSynchronizer.leaderChanged(true);
335 TestUtils.callMethod(intentSynchronizer, "syncIntents",
336 new Class<?>[] {});
Pingpingf5d90932014-10-27 10:50:04 -0700337
338 // Verify
Jonathan Hartec2df012014-10-23 16:40:24 -0700339 assertEquals(router.getRoutes().size(), 6);
Pingpingf5d90932014-10-27 10:50:04 -0700340 assertTrue(router.getRoutes().contains(routeEntry1));
341 assertTrue(router.getRoutes().contains(routeEntry3));
342 assertTrue(router.getRoutes().contains(routeEntry4Update));
343 assertTrue(router.getRoutes().contains(routeEntry5));
344 assertTrue(router.getRoutes().contains(routeEntry6));
345
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800346 assertEquals(intentSynchronizer.getPushedRouteIntents().size(), 6);
347 assertTrue(intentSynchronizer.getPushedRouteIntents().contains(intent1));
348 assertTrue(intentSynchronizer.getPushedRouteIntents().contains(intent3));
349 assertTrue(intentSynchronizer.getPushedRouteIntents().contains(intent4Update));
350 assertTrue(intentSynchronizer.getPushedRouteIntents().contains(intent5));
351 assertTrue(intentSynchronizer.getPushedRouteIntents().contains(intent6));
Pingpingf5d90932014-10-27 10:50:04 -0700352
353 verify(intentService);
354 }
355
356 /**
357 * MultiPointToSinglePointIntent builder.
358 *
359 * @param ipPrefix the ipPrefix to match
360 * @param nextHopMacAddress to which the destination MAC address in packet
361 * should be rewritten
362 * @param egressPoint to which packets should be sent
363 * @return the constructed MultiPointToSinglePointIntent
364 */
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800365 private MultiPointToSinglePointIntent intentBuilder(Ip4Prefix ipPrefix,
Pingpingf5d90932014-10-27 10:50:04 -0700366 String nextHopMacAddress, ConnectPoint egressPoint) {
367
368 TrafficSelector.Builder selectorBuilder =
369 DefaultTrafficSelector.builder();
370 selectorBuilder.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipPrefix);
371
372 TrafficTreatment.Builder treatmentBuilder =
373 DefaultTrafficTreatment.builder();
374 treatmentBuilder.setEthDst(MacAddress.valueOf(nextHopMacAddress));
375
376 Set<ConnectPoint> ingressPoints = new HashSet<ConnectPoint>();
377 for (Interface intf : interfaceService.getInterfaces()) {
378 if (!intf.equals(interfaceService.getInterface(egressPoint))) {
379 ConnectPoint srcPort = intf.connectPoint();
380 ingressPoints.add(srcPort);
381 }
382 }
383 MultiPointToSinglePointIntent intent =
384 new MultiPointToSinglePointIntent(APPID,
385 selectorBuilder.build(), treatmentBuilder.build(),
386 ingressPoints, egressPoint);
387 return intent;
388 }
389
390 /**
391 * A static MultiPointToSinglePointIntent builder, the returned intent is
392 * equal to the input intent except that the id is different.
393 *
394 *
395 * @param intent the intent to be used for building a new intent
396 * @param routeEntry the relative routeEntry of the intent
397 * @return the newly constructed MultiPointToSinglePointIntent
398 * @throws TestUtilsException
399 */
400 private MultiPointToSinglePointIntent staticIntentBuilder(
401 MultiPointToSinglePointIntent intent, RouteEntry routeEntry,
402 String nextHopMacAddress) throws TestUtilsException {
403
404 // Use a different egress ConnectPoint with that in intent
405 // to generate a different id
406 MultiPointToSinglePointIntent intentNew = intentBuilder(
407 routeEntry.prefix(), nextHopMacAddress, SW2_ETH1);
408 TestUtils.setField(intentNew, "egressPoint", intent.egressPoint());
409 TestUtils.setField(intentNew,
410 "ingressPoints", intent.ingressPoints());
411 return intentNew;
412 }
413}