blob: 45b0c903aa3675b33d1e818cdb6caf5638bb7cad [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;
45import org.onlab.packet.IpPrefix;
46import org.onlab.packet.MacAddress;
47import org.onlab.packet.VlanId;
48
49import com.google.common.collect.Sets;
50import com.googlecode.concurrenttrees.radix.node.concrete.DefaultByteArrayNodeFactory;
51import com.googlecode.concurrenttrees.radixinverted.ConcurrentInvertedRadixTree;
52import com.googlecode.concurrenttrees.radixinverted.InvertedRadixTree;
53
54/**
55 * This class tests the intent synchronization function in Router class.
56 */
57public class IntentSyncTest {
58
59 private InterfaceService interfaceService;
60 private IntentService intentService;
61 private HostService hostService;
62
63 private static final ConnectPoint SW1_ETH1 = new ConnectPoint(
64 DeviceId.deviceId("of:0000000000000001"),
65 PortNumber.portNumber(1));
66
67 private static final ConnectPoint SW2_ETH1 = new ConnectPoint(
68 DeviceId.deviceId("of:0000000000000002"),
69 PortNumber.portNumber(1));
70
71 private static final ConnectPoint SW3_ETH1 = new ConnectPoint(
72 DeviceId.deviceId("of:0000000000000003"),
73 PortNumber.portNumber(1));
74
75 private Router router;
76
77 private static final ApplicationId APPID = new ApplicationId() {
78 @Override
79 public short id() {
80 return 1;
81 }
82
83 @Override
84 public String name() {
85 return "SDNIP";
86 }
87 };
88
89 @Before
90 public void setUp() throws Exception {
91 setUpInterfaceService();
92 setUpHostService();
93 intentService = createMock(IntentService.class);
94
95 router = new Router(APPID, intentService,
96 hostService, null, interfaceService);
97 }
98
99 /**
100 * Sets up InterfaceService.
101 */
102 private void setUpInterfaceService() {
103
104 interfaceService = createMock(InterfaceService.class);
105
106 Set<Interface> interfaces = Sets.newHashSet();
107
108 Set<InterfaceIpAddress> interfaceIpAddresses1 = Sets.newHashSet();
109 interfaceIpAddresses1.add(new InterfaceIpAddress(
110 IpAddress.valueOf("192.168.10.101"),
111 IpPrefix.valueOf("192.168.10.0/24")));
112 Interface sw1Eth1 = new Interface(SW1_ETH1,
113 interfaceIpAddresses1, MacAddress.valueOf("00:00:00:00:00:01"));
114 interfaces.add(sw1Eth1);
115
116 Set<InterfaceIpAddress> interfaceIpAddresses2 = Sets.newHashSet();
117 interfaceIpAddresses2.add(new InterfaceIpAddress(
118 IpAddress.valueOf("192.168.20.101"),
119 IpPrefix.valueOf("192.168.20.0/24")));
120 Interface sw2Eth1 = new Interface(SW2_ETH1,
121 interfaceIpAddresses2, MacAddress.valueOf("00:00:00:00:00:02"));
122 interfaces.add(sw2Eth1);
123
124 Set<InterfaceIpAddress> interfaceIpAddresses3 = Sets.newHashSet();
125 interfaceIpAddresses3.add(new InterfaceIpAddress(
126 IpAddress.valueOf("192.168.30.101"),
127 IpPrefix.valueOf("192.168.30.0/24")));
128 Interface sw3Eth1 = new Interface(SW3_ETH1,
129 interfaceIpAddresses3, MacAddress.valueOf("00:00:00:00:00:03"));
130 interfaces.add(sw3Eth1);
131
132 expect(interfaceService.getInterface(SW1_ETH1)).andReturn(
133 sw1Eth1).anyTimes();
134 expect(interfaceService.getInterface(SW2_ETH1)).andReturn(
135 sw2Eth1).anyTimes();
136 expect(interfaceService.getInterface(SW3_ETH1)).andReturn(
137 sw3Eth1).anyTimes();
138 expect(interfaceService.getInterfaces()).andReturn(
139 interfaces).anyTimes();
140 replay(interfaceService);
141 }
142
143 /**
144 * Sets up the host service with details of hosts.
145 */
146 private void setUpHostService() {
147 hostService = createMock(HostService.class);
148
149 hostService.addListener(anyObject(HostListener.class));
150 expectLastCall().anyTimes();
151
152 IpAddress host1Address = IpAddress.valueOf("192.168.10.1");
153 Host host1 = new DefaultHost(ProviderId.NONE, HostId.NONE,
154 MacAddress.valueOf("00:00:00:00:00:01"), VlanId.NONE,
155 new HostLocation(SW1_ETH1, 1),
156 Sets.newHashSet(host1Address));
157
158 expect(hostService.getHostsByIp(host1Address))
159 .andReturn(Sets.newHashSet(host1)).anyTimes();
160 hostService.startMonitoringIp(host1Address);
161 expectLastCall().anyTimes();
162
163
164 IpAddress host2Address = IpAddress.valueOf("192.168.20.1");
165 Host host2 = new DefaultHost(ProviderId.NONE, HostId.NONE,
166 MacAddress.valueOf("00:00:00:00:00:02"), VlanId.NONE,
167 new HostLocation(SW2_ETH1, 1),
168 Sets.newHashSet(host2Address));
169
170 expect(hostService.getHostsByIp(host2Address))
171 .andReturn(Sets.newHashSet(host2)).anyTimes();
172 hostService.startMonitoringIp(host2Address);
173 expectLastCall().anyTimes();
174
175
176 IpAddress host3Address = IpAddress.valueOf("192.168.30.1");
177 Host host3 = new DefaultHost(ProviderId.NONE, HostId.NONE,
178 MacAddress.valueOf("00:00:00:00:00:03"), VlanId.NONE,
179 new HostLocation(SW3_ETH1, 1),
180 Sets.newHashSet(host3Address));
181
182 expect(hostService.getHostsByIp(host3Address))
183 .andReturn(Sets.newHashSet(host3)).anyTimes();
184 hostService.startMonitoringIp(host3Address);
185 expectLastCall().anyTimes();
186
187
188 replay(hostService);
189 }
190
191 /**
192 * This method tests the behavior of intent Synchronizer.
193 *
194 * @throws TestUtilsException
195 */
196 @Test
197 public void testIntentSync() throws TestUtilsException {
198
199 //
200 // Construct routes and intents.
201 // This test simulates the following cases during the master change
202 // time interval:
203 // 1. RouteEntry1 did not change and the intent also did not change.
204 // 2. RouteEntry2 was deleted, but the intent was not deleted.
205 // 3. RouteEntry3 was newly added, and the intent was also submitted.
206 // 4. RouteEntry4 was updated to RouteEntry4Update, and the intent was
207 // also updated to a new one.
208 // 5. RouteEntry5 did not change, but its intent id changed.
209 // 6. RouteEntry6 was newly added, but the intent was not submitted.
210 //
211 RouteEntry routeEntry1 = new RouteEntry(
212 IpPrefix.valueOf("1.1.1.0/24"),
213 IpAddress.valueOf("192.168.10.1"));
214
215 RouteEntry routeEntry2 = new RouteEntry(
216 IpPrefix.valueOf("2.2.2.0/24"),
217 IpAddress.valueOf("192.168.20.1"));
218
219 RouteEntry routeEntry3 = new RouteEntry(
220 IpPrefix.valueOf("3.3.3.0/24"),
221 IpAddress.valueOf("192.168.30.1"));
222
223 RouteEntry routeEntry4 = new RouteEntry(
224 IpPrefix.valueOf("4.4.4.0/24"),
225 IpAddress.valueOf("192.168.30.1"));
226
227 RouteEntry routeEntry4Update = new RouteEntry(
228 IpPrefix.valueOf("4.4.4.0/24"),
229 IpAddress.valueOf("192.168.20.1"));
230
231 RouteEntry routeEntry5 = new RouteEntry(
232 IpPrefix.valueOf("5.5.5.0/24"),
233 IpAddress.valueOf("192.168.10.1"));
234
235 RouteEntry routeEntry6 = new RouteEntry(
236 IpPrefix.valueOf("6.6.6.0/24"),
237 IpAddress.valueOf("192.168.10.1"));
238
Jonathan Hartec2df012014-10-23 16:40:24 -0700239 RouteEntry routeEntry7 = new RouteEntry(
240 IpPrefix.valueOf("7.7.7.0/24"),
241 IpAddress.valueOf("192.168.10.1"));
242
Pingpingf5d90932014-10-27 10:50:04 -0700243 MultiPointToSinglePointIntent intent1 = intentBuilder(
244 routeEntry1.prefix(), "00:00:00:00:00:01", SW1_ETH1);
245 MultiPointToSinglePointIntent intent2 = intentBuilder(
246 routeEntry2.prefix(), "00:00:00:00:00:02", SW2_ETH1);
247 MultiPointToSinglePointIntent intent3 = intentBuilder(
248 routeEntry3.prefix(), "00:00:00:00:00:03", SW3_ETH1);
249 MultiPointToSinglePointIntent intent4 = intentBuilder(
250 routeEntry4.prefix(), "00:00:00:00:00:03", SW3_ETH1);
251 MultiPointToSinglePointIntent intent4Update = intentBuilder(
252 routeEntry4Update.prefix(), "00:00:00:00:00:02", SW2_ETH1);
253 MultiPointToSinglePointIntent intent5 = intentBuilder(
254 routeEntry5.prefix(), "00:00:00:00:00:01", SW1_ETH1);
Jonathan Hartec2df012014-10-23 16:40:24 -0700255 MultiPointToSinglePointIntent intent7 = intentBuilder(
256 routeEntry7.prefix(), "00:00:00:00:00:01", SW1_ETH1);
Pingpingf5d90932014-10-27 10:50:04 -0700257
258 // Compose a intent, which is equal to intent5 but the id is different.
259 MultiPointToSinglePointIntent intent5New =
260 staticIntentBuilder(intent5, routeEntry5, "00:00:00:00:00:01");
261 assertTrue(TestUtils.callMethod(router,
262 "compareMultiPointToSinglePointIntents",
263 new Class<?>[] {MultiPointToSinglePointIntent.class,
264 MultiPointToSinglePointIntent.class},
265 intent5, intent5New).equals(true));
Jonathan Hartec2df012014-10-23 16:40:24 -0700266 assertFalse(intent5.equals(intent5New));
Pingpingf5d90932014-10-27 10:50:04 -0700267
268 MultiPointToSinglePointIntent intent6 = intentBuilder(
269 routeEntry6.prefix(), "00:00:00:00:00:01", SW1_ETH1);
270
271 // Set up the bgpRoutes and pushedRouteIntents fields in Router class
272 InvertedRadixTree<RouteEntry> bgpRoutes =
273 new ConcurrentInvertedRadixTree<>(
274 new DefaultByteArrayNodeFactory());
275 bgpRoutes.put(RouteEntry.createBinaryString(routeEntry1.prefix()),
276 routeEntry1);
277 bgpRoutes.put(RouteEntry.createBinaryString(routeEntry3.prefix()),
278 routeEntry3);
279 bgpRoutes.put(RouteEntry.createBinaryString(routeEntry4Update.prefix()),
280 routeEntry4Update);
281 bgpRoutes.put(RouteEntry.createBinaryString(routeEntry5.prefix()),
282 routeEntry5);
283 bgpRoutes.put(RouteEntry.createBinaryString(routeEntry6.prefix()),
284 routeEntry6);
Jonathan Hartec2df012014-10-23 16:40:24 -0700285 bgpRoutes.put(RouteEntry.createBinaryString(routeEntry7.prefix()),
286 routeEntry7);
Pingpingf5d90932014-10-27 10:50:04 -0700287 TestUtils.setField(router, "bgpRoutes", bgpRoutes);
288
289 ConcurrentHashMap<IpPrefix, MultiPointToSinglePointIntent>
290 pushedRouteIntents = new ConcurrentHashMap<>();
291 pushedRouteIntents.put(routeEntry1.prefix(), intent1);
292 pushedRouteIntents.put(routeEntry3.prefix(), intent3);
293 pushedRouteIntents.put(routeEntry4Update.prefix(), intent4Update);
294 pushedRouteIntents.put(routeEntry5.prefix(), intent5New);
295 pushedRouteIntents.put(routeEntry6.prefix(), intent6);
Jonathan Hartec2df012014-10-23 16:40:24 -0700296 pushedRouteIntents.put(routeEntry7.prefix(), intent7);
Pingpingf5d90932014-10-27 10:50:04 -0700297 TestUtils.setField(router, "pushedRouteIntents", pushedRouteIntents);
298
299 // Set up expectation
300 reset(intentService);
301 Set<Intent> intents = new HashSet<Intent>();
302 intents.add(intent1);
Jonathan Hartec2df012014-10-23 16:40:24 -0700303 expect(intentService.getIntentState(intent1.id()))
304 .andReturn(IntentState.INSTALLED).anyTimes();
Pingpingf5d90932014-10-27 10:50:04 -0700305 intents.add(intent2);
Jonathan Hartec2df012014-10-23 16:40:24 -0700306 expect(intentService.getIntentState(intent2.id()))
307 .andReturn(IntentState.INSTALLED).anyTimes();
Pingpingf5d90932014-10-27 10:50:04 -0700308 intents.add(intent4);
Jonathan Hartec2df012014-10-23 16:40:24 -0700309 expect(intentService.getIntentState(intent4.id()))
310 .andReturn(IntentState.INSTALLED).anyTimes();
Pingpingf5d90932014-10-27 10:50:04 -0700311 intents.add(intent5);
Jonathan Hartec2df012014-10-23 16:40:24 -0700312 expect(intentService.getIntentState(intent5.id()))
313 .andReturn(IntentState.INSTALLED).anyTimes();
314 intents.add(intent7);
315 expect(intentService.getIntentState(intent7.id()))
316 .andReturn(IntentState.WITHDRAWING).anyTimes();
Pingpingf5d90932014-10-27 10:50:04 -0700317 expect(intentService.getIntents()).andReturn(intents).anyTimes();
318
319 intentService.withdraw(intent2);
320 intentService.submit(intent3);
321 intentService.withdraw(intent4);
322 intentService.submit(intent4Update);
323 intentService.submit(intent6);
Jonathan Hartec2df012014-10-23 16:40:24 -0700324 intentService.submit(intent7);
Pingpingf5d90932014-10-27 10:50:04 -0700325 replay(intentService);
326
327 // Start the test
328 router.leaderChanged(true);
329 TestUtils.callMethod(router, "syncIntents", new Class<?>[] {});
330
331 // Verify
Jonathan Hartec2df012014-10-23 16:40:24 -0700332 assertEquals(router.getRoutes().size(), 6);
Pingpingf5d90932014-10-27 10:50:04 -0700333 assertTrue(router.getRoutes().contains(routeEntry1));
334 assertTrue(router.getRoutes().contains(routeEntry3));
335 assertTrue(router.getRoutes().contains(routeEntry4Update));
336 assertTrue(router.getRoutes().contains(routeEntry5));
337 assertTrue(router.getRoutes().contains(routeEntry6));
338
Jonathan Hartec2df012014-10-23 16:40:24 -0700339 assertEquals(router.getPushedRouteIntents().size(), 6);
Pingpingf5d90932014-10-27 10:50:04 -0700340 assertTrue(router.getPushedRouteIntents().contains(intent1));
341 assertTrue(router.getPushedRouteIntents().contains(intent3));
342 assertTrue(router.getPushedRouteIntents().contains(intent4Update));
343 assertTrue(router.getPushedRouteIntents().contains(intent5));
344 assertTrue(router.getPushedRouteIntents().contains(intent6));
345
346 verify(intentService);
347 }
348
349 /**
350 * MultiPointToSinglePointIntent builder.
351 *
352 * @param ipPrefix the ipPrefix to match
353 * @param nextHopMacAddress to which the destination MAC address in packet
354 * should be rewritten
355 * @param egressPoint to which packets should be sent
356 * @return the constructed MultiPointToSinglePointIntent
357 */
358 private MultiPointToSinglePointIntent intentBuilder(IpPrefix ipPrefix,
359 String nextHopMacAddress, ConnectPoint egressPoint) {
360
361 TrafficSelector.Builder selectorBuilder =
362 DefaultTrafficSelector.builder();
363 selectorBuilder.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipPrefix);
364
365 TrafficTreatment.Builder treatmentBuilder =
366 DefaultTrafficTreatment.builder();
367 treatmentBuilder.setEthDst(MacAddress.valueOf(nextHopMacAddress));
368
369 Set<ConnectPoint> ingressPoints = new HashSet<ConnectPoint>();
370 for (Interface intf : interfaceService.getInterfaces()) {
371 if (!intf.equals(interfaceService.getInterface(egressPoint))) {
372 ConnectPoint srcPort = intf.connectPoint();
373 ingressPoints.add(srcPort);
374 }
375 }
376 MultiPointToSinglePointIntent intent =
377 new MultiPointToSinglePointIntent(APPID,
378 selectorBuilder.build(), treatmentBuilder.build(),
379 ingressPoints, egressPoint);
380 return intent;
381 }
382
383 /**
384 * A static MultiPointToSinglePointIntent builder, the returned intent is
385 * equal to the input intent except that the id is different.
386 *
387 *
388 * @param intent the intent to be used for building a new intent
389 * @param routeEntry the relative routeEntry of the intent
390 * @return the newly constructed MultiPointToSinglePointIntent
391 * @throws TestUtilsException
392 */
393 private MultiPointToSinglePointIntent staticIntentBuilder(
394 MultiPointToSinglePointIntent intent, RouteEntry routeEntry,
395 String nextHopMacAddress) throws TestUtilsException {
396
397 // Use a different egress ConnectPoint with that in intent
398 // to generate a different id
399 MultiPointToSinglePointIntent intentNew = intentBuilder(
400 routeEntry.prefix(), nextHopMacAddress, SW2_ETH1);
401 TestUtils.setField(intentNew, "egressPoint", intent.egressPoint());
402 TestUtils.setField(intentNew,
403 "ingressPoints", intent.ingressPoints());
404 return intentNew;
405 }
406}