blob: 9af9c76942262aa47ba23bf8e59aad378ee941e8 [file] [log] [blame]
Luca Pretee4a5e1a2016-09-07 17:01:22 -07001/*
2 * Copyright 2015-present 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 */
16
17package org.onosproject.sdnip;
18
Luca Prete83bac342016-12-06 19:42:05 -080019import com.fasterxml.jackson.databind.JsonNode;
20import com.fasterxml.jackson.databind.ObjectMapper;
Luca Pretee4a5e1a2016-09-07 17:01:22 -070021import com.google.common.collect.Lists;
22import com.google.common.collect.Sets;
23import org.junit.Before;
24import org.junit.Test;
25import 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;
32import org.onosproject.TestApplicationId;
33import org.onosproject.core.ApplicationId;
34import org.onosproject.core.CoreServiceAdapter;
35import org.onosproject.incubator.net.intf.Interface;
36import org.onosproject.incubator.net.intf.InterfaceEvent;
37import org.onosproject.incubator.net.intf.InterfaceListener;
38import org.onosproject.incubator.net.intf.InterfaceService;
39import org.onosproject.incubator.net.intf.InterfaceServiceAdapter;
40import org.onosproject.incubator.net.routing.ResolvedRoute;
41import org.onosproject.incubator.net.routing.RouteEvent;
42import org.onosproject.incubator.net.routing.RouteListener;
43import org.onosproject.incubator.net.routing.RouteServiceAdapter;
44import org.onosproject.net.ConnectPoint;
45import org.onosproject.net.DeviceId;
Luca Prete83bac342016-12-06 19:42:05 -080046import org.onosproject.net.EncapsulationType;
Luca Pretee4a5e1a2016-09-07 17:01:22 -070047import org.onosproject.net.FilteredConnectPoint;
48import org.onosproject.net.PortNumber;
Luca Prete83bac342016-12-06 19:42:05 -080049import org.onosproject.net.config.Config;
50import org.onosproject.net.config.ConfigApplyDelegate;
51import org.onosproject.net.config.NetworkConfigListener;
52import org.onosproject.net.config.NetworkConfigServiceAdapter;
Luca Pretee4a5e1a2016-09-07 17:01:22 -070053import org.onosproject.net.flow.DefaultTrafficSelector;
54import org.onosproject.net.flow.DefaultTrafficTreatment;
55import org.onosproject.net.flow.TrafficSelector;
56import org.onosproject.net.flow.TrafficTreatment;
57import org.onosproject.net.host.InterfaceIpAddress;
58import org.onosproject.net.intent.AbstractIntentTest;
59import org.onosproject.net.intent.Key;
60import org.onosproject.net.intent.MultiPointToSinglePointIntent;
61import org.onosproject.routing.IntentSynchronizationService;
Luca Prete83bac342016-12-06 19:42:05 -080062import org.onosproject.routing.RoutingService;
63import org.onosproject.routing.config.BgpConfig;
64import org.onosproject.sdnip.config.SdnIpConfig;
Luca Pretee4a5e1a2016-09-07 17:01:22 -070065
66import java.util.Collections;
67import java.util.List;
68import java.util.Set;
69
70import static org.easymock.EasyMock.anyObject;
71import static org.easymock.EasyMock.createMock;
72import static org.easymock.EasyMock.expect;
73import static org.easymock.EasyMock.expectLastCall;
74import static org.easymock.EasyMock.replay;
75import static org.easymock.EasyMock.reset;
76import static org.easymock.EasyMock.verify;
77import static org.onosproject.routing.TestIntentServiceHelper.eqExceptId;
78
79/**
80 * Unit tests for SdnIpFib.
81 */
82public class SdnIpFibTest extends AbstractIntentTest {
83
84 private InterfaceService interfaceService;
85
86 private static final ConnectPoint SW1_ETH1 = new ConnectPoint(
87 DeviceId.deviceId("of:0000000000000001"),
88 PortNumber.portNumber(1));
89
90 private static final ConnectPoint SW2_ETH1 = new ConnectPoint(
91 DeviceId.deviceId("of:0000000000000002"),
92 PortNumber.portNumber(1));
93
94 private static final ConnectPoint SW3_ETH1 = new ConnectPoint(
95 DeviceId.deviceId("of:0000000000000003"),
96 PortNumber.portNumber(1));
97
98 private static final ConnectPoint SW4_ETH1 = new ConnectPoint(
99 DeviceId.deviceId("of:0000000000000004"),
100 PortNumber.portNumber(1));
101
102 private static final MacAddress MAC1 = MacAddress.valueOf("00:00:00:00:00:01");
103 private static final MacAddress MAC2 = MacAddress.valueOf("00:00:00:00:00:02");
104 private static final MacAddress MAC3 = MacAddress.valueOf("00:00:00:00:00:03");
105 private static final MacAddress MAC4 = MacAddress.valueOf("00:00:00:00:00:04");
106
107 private static final VlanId NO_VLAN = VlanId.NONE;
108 private static final VlanId VLAN10 = VlanId.vlanId(Short.valueOf("10"));
109 private static final VlanId VLAN20 = VlanId.vlanId(Short.valueOf("20"));
110
111 private static final InterfaceIpAddress IIP1 =
112 InterfaceIpAddress.valueOf("192.168.10.101/24");
113 private static final InterfaceIpAddress IIP2 =
114 InterfaceIpAddress.valueOf("192.168.20.101/24");
115 private static final InterfaceIpAddress IIP3 =
116 InterfaceIpAddress.valueOf("192.168.30.101/24");
117 private static final InterfaceIpAddress IIP4 =
118 InterfaceIpAddress.valueOf("192.168.40.101/24");
119
120 private static final IpAddress IP1 = Ip4Address.valueOf("192.168.10.1");
121 private static final IpAddress IP2 = Ip4Address.valueOf("192.168.20.1");
122 private static final IpAddress IP3 = Ip4Address.valueOf("192.168.30.1");
123
124 private static final IpPrefix PREFIX1 = Ip4Prefix.valueOf("1.1.1.0/24");
125 private static final IpPrefix PREFIX2 = Ip4Prefix.valueOf("1.1.2.0/24");
126
127 private SdnIpFib sdnipFib;
128 private IntentSynchronizationService intentSynchronizer;
129 private final Set<Interface> interfaces = Sets.newHashSet();
130
131 private static final ApplicationId APPID = TestApplicationId.create("SDNIP");
132
133 private RouteListener routeListener;
134 private InterfaceListener interfaceListener;
135
136 @Before
137 public void setUp() throws Exception {
138 super.setUp();
139
140 interfaceService = createMock(InterfaceService.class);
141
142 interfaceService.addListener(anyObject(InterfaceListener.class));
143 expectLastCall().andDelegateTo(new InterfaceServiceDelegate());
144
145 // These will set expectations on routingConfig and interfaceService
146 setUpInterfaceService();
147
148 replay(interfaceService);
149
150 intentSynchronizer = createMock(IntentSynchronizationService.class);
151
152 sdnipFib = new SdnIpFib();
153 sdnipFib.routeService = new TestRouteService();
154 sdnipFib.coreService = new TestCoreService();
Luca Prete83bac342016-12-06 19:42:05 -0800155 sdnipFib.networkConfigService = new TestNetworkConfigService();
Luca Pretee4a5e1a2016-09-07 17:01:22 -0700156 sdnipFib.interfaceService = interfaceService;
157 sdnipFib.intentSynchronizer = intentSynchronizer;
158
159 sdnipFib.activate();
160 }
161
162 /**
163 * Sets up the interface service.
164 */
165 private void setUpInterfaceService() {
166 List<InterfaceIpAddress> iIps1 = Lists.newArrayList();
167 iIps1.add(IIP1);
168 Interface sw1Eth1 = new Interface("sw1-eth1", SW1_ETH1, iIps1, MAC1, VLAN10);
169 interfaces.add(sw1Eth1);
170
171 List<InterfaceIpAddress> iIps2 = Lists.newArrayList();
172 iIps2.add(IIP2);
173 Interface sw2Eth1 = new Interface("sw2-eth1", SW2_ETH1, iIps2, MAC2, VLAN20);
174 interfaces.add(sw2Eth1);
175
176 List<InterfaceIpAddress> iIps3 = Lists.newArrayList();
177 iIps3.add(IIP3);
178 Interface sw3Eth1 = new Interface("sw3-eth1", SW3_ETH1, iIps3, MAC3, NO_VLAN);
179 interfaces.add(sw3Eth1);
180
181 expect(interfaceService.getInterfacesByPort(SW1_ETH1)).andReturn(
182 Collections.singleton(sw1Eth1)).anyTimes();
183 expect(interfaceService.getMatchingInterface(IP1))
184 .andReturn(sw1Eth1).anyTimes();
185 expect(interfaceService.getInterfacesByPort(SW2_ETH1)).andReturn(
186 Collections.singleton(sw2Eth1)).anyTimes();
187 expect(interfaceService.getMatchingInterface(IP2))
188 .andReturn(sw2Eth1).anyTimes();
189 expect(interfaceService.getInterfacesByPort(SW3_ETH1)).andReturn(
190 Collections.singleton(sw3Eth1)).anyTimes();
191 expect(interfaceService.getMatchingInterface(IP3))
192 .andReturn(sw3Eth1).anyTimes();
193 expect(interfaceService.getInterfaces()).andReturn(interfaces).anyTimes();
194 }
195
196 /**
197 * Tests adding a route. The egress interface has no VLAN configured.
198 *
199 * We verify that the synchronizer records the correct state and that the
200 * correct intent is submitted to the IntentService.
201 */
202 @Test
203 public void testRouteAddToNoVlan() {
204 // Build the expected route
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700205 ResolvedRoute route = new ResolvedRoute(PREFIX1, IP3, MAC3, SW3_ETH1);
Luca Pretee4a5e1a2016-09-07 17:01:22 -0700206
207 MultiPointToSinglePointIntent intent =
208 createIntentToThreeSrcOneTwo(PREFIX1);
209
210 // Setup the expected intents
211 intentSynchronizer.submit(eqExceptId(intent));
212 replay(intentSynchronizer);
213
214 // Send in the added event
215 routeListener.event(new RouteEvent(RouteEvent.Type.ROUTE_ADDED, route));
216
217 verify(intentSynchronizer);
218 }
219
220 /**
221 * Tests adding a route. The egress interface has a VLAN configured.
222 *
223 * We verify that the synchronizer records the correct state and that the
224 * correct intent is submitted to the IntentService.
225 */
226 @Test
227 public void testRouteAddToVlan() {
228 // Build the expected route
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700229 ResolvedRoute route = new ResolvedRoute(PREFIX2, IP1, MAC1, SW1_ETH1);
Luca Pretee4a5e1a2016-09-07 17:01:22 -0700230
231 MultiPointToSinglePointIntent intent = createIntentToOne(PREFIX2);
232
233 // Setup the expected intents
234 intentSynchronizer.submit(eqExceptId(intent));
235 replay(intentSynchronizer);
236
237 // Send in the added event
238 routeListener.event(new RouteEvent(RouteEvent.Type.ROUTE_ADDED, route));
239
240 verify(intentSynchronizer);
241 }
242
243 /**
244 * Tests updating a route.
245 *
246 * We first add a route from a next-hop with no vlan. We then announce the
247 * same route from another next-hop with a vlan.
248 *
249 * We verify that the synchronizer records the correct state and that the
250 * correct intent is submitted to the IntentService.
251 */
252 @Test
253 public void testRouteUpdatesToVlan() {
254 // Add a route first to a destination with no VLAN
255 testRouteAddToNoVlan();
256
257 // Build the new route entries for prefix1 and prefix2
Charles Chane4d13102016-11-08 15:38:44 -0800258 ResolvedRoute oldRoutePrefixOne = new ResolvedRoute(PREFIX1, IP3, MAC3, SW3_ETH1);
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700259 ResolvedRoute routePrefixOne = new ResolvedRoute(PREFIX1, IP1, MAC1, SW1_ETH1);
Luca Pretee4a5e1a2016-09-07 17:01:22 -0700260
261 // Create the new expected intents
262 MultiPointToSinglePointIntent newPrefixOneIntent = createIntentToOne(PREFIX1);
263
264 // Set up test expectation
265 reset(intentSynchronizer);
266
267 // Setup the expected intents
268 intentSynchronizer.submit(eqExceptId(newPrefixOneIntent));
269 replay(intentSynchronizer);
270
271 // Send in the update events
272 routeListener.event(new RouteEvent(RouteEvent.Type.ROUTE_UPDATED,
Charles Chane4d13102016-11-08 15:38:44 -0800273 routePrefixOne, oldRoutePrefixOne));
Luca Pretee4a5e1a2016-09-07 17:01:22 -0700274
275 verify(intentSynchronizer);
276 }
277
278 /**
279 * Tests updating a route.
280 *
281 * We first add a route from a next-hop with a vlan. We then announce the
282 * same route from another next-hop with no vlan.
283 *
284 * We verify that the synchronizer records the correct state and that the
285 * correct intent is submitted to the IntentService.
286 */
287 @Test
288 public void testRouteUpdatesToNoVlan() {
289 // Add a route first to a destination with no VLAN
290 testRouteAddToVlan();
291
292 // Build the new route entries for prefix1 and prefix2
Charles Chane4d13102016-11-08 15:38:44 -0800293 ResolvedRoute oldRoutePrefix = new ResolvedRoute(PREFIX2, IP1, MAC1, SW1_ETH1);
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700294 ResolvedRoute routePrefix = new ResolvedRoute(PREFIX2, IP3, MAC3, SW3_ETH1);
Luca Pretee4a5e1a2016-09-07 17:01:22 -0700295
296 // Create the new expected intents
297 MultiPointToSinglePointIntent newPrefixIntent =
298 createIntentToThreeSrcOneTwo(PREFIX2);
299
300 // Set up test expectation
301 reset(intentSynchronizer);
302
303 // Setup the expected intents
304 intentSynchronizer.submit(eqExceptId(newPrefixIntent));
305 replay(intentSynchronizer);
306
307 // Send in the update events
308 routeListener.event(new RouteEvent(RouteEvent.Type.ROUTE_UPDATED,
Charles Chane4d13102016-11-08 15:38:44 -0800309 routePrefix, oldRoutePrefix));
Luca Pretee4a5e1a2016-09-07 17:01:22 -0700310
311 verify(intentSynchronizer);
312 }
313
314 /**
315 * Tests deleting a route.
316 *
317 * We verify that the synchronizer records the correct state and that the
318 * correct intent is withdrawn from the IntentService.
319 */
320 @Test
321 public void testRouteDelete() {
322 // Add a route first
323 testRouteAddToNoVlan();
324
325 // Construct the existing route entry
Charles Chane4d13102016-11-08 15:38:44 -0800326 ResolvedRoute route = new ResolvedRoute(PREFIX1, IP3, MAC3, SW3_ETH1);
Luca Pretee4a5e1a2016-09-07 17:01:22 -0700327
328 // Create existing intent
329 MultiPointToSinglePointIntent removedIntent =
330 createIntentToThreeSrcOneTwo(PREFIX1);
331
332 // Set up expectation
333 reset(intentSynchronizer);
334 // Setup the expected intents
335 intentSynchronizer.withdraw(eqExceptId(removedIntent));
336 replay(intentSynchronizer);
337
338 // Send in the removed event
339 routeListener.event(new RouteEvent(RouteEvent.Type.ROUTE_REMOVED, route));
340
341 verify(intentSynchronizer);
342 }
343
344 /**
345 * Tests adding a new interface.
346 *
347 * We verify that the synchronizer records the correct state and that the
348 * correct intent is withdrawn from the IntentService.
349 */
350 @Test
351 public void testAddInterface() {
352 // Add a route first
353 testRouteAddToNoVlan();
354
355 // Create the new expected intent
356 MultiPointToSinglePointIntent addedIntent =
357 createIntentToThreeSrcOneTwoFour(PREFIX1);
358
359 reset(intentSynchronizer);
360
361 intentSynchronizer.submit(eqExceptId(addedIntent));
362 expectLastCall().once();
363
364 replay(intentSynchronizer);
365
366 // Create the new interface and add notify it
367 Interface intf = new Interface("sw4-eth1", SW4_ETH1,
368 Collections.singletonList(IIP4),
369 MAC4, NO_VLAN);
370 InterfaceEvent intfEvent =
371 new InterfaceEvent(InterfaceEvent.Type.INTERFACE_ADDED, intf);
372
373 interfaceListener.event(intfEvent);
374
375 verify(intentSynchronizer);
376 }
377
378 /**
379 * Tests removing an existing interface.
380 *
381 * We first push an intent with destination sw3 and source sw1 and sw2. We
382 * then remove the ingress interface on sw1.
383 *
384 * We verify that the synchronizer records the correct state and that the
385 * correct intent is withdrawn from the IntentService.
386 */
387 @Test
388 public void testRemoveIngressInterface() {
389 // Add a route first
390 testRouteAddToNoVlan();
391
392 // Create the new expected intent
393 MultiPointToSinglePointIntent remainingIntent =
394 createIntentToThreeSrcTwo(PREFIX1);
395
396 reset(intentSynchronizer);
397
398 intentSynchronizer.submit(eqExceptId(remainingIntent));
399 expectLastCall().once();
400
401 replay(intentSynchronizer);
402
403 // Define the existing ingress interface and remove it
404 Interface intf = new Interface("sw1-eth1", SW1_ETH1,
405 Collections.singletonList(IIP1),
406 MAC1, VLAN10);
407 InterfaceEvent intfEvent =
408 new InterfaceEvent(InterfaceEvent.Type.INTERFACE_REMOVED, intf);
409 interfaceListener.event(intfEvent);
410
411 verify(intentSynchronizer);
412 }
413
414 /**
415 * Tests removing an existing egress interface.
416 *
417 * We first push an intent with destination sw3 and source sw1 and sw2. We
418 * then remove the egress interface on sw3.
419 *
420 * We verify that the synchronizer records the correct state and that the
421 * correct intent is withdrawn from the IntentService.
422 */
423 @Test
424 public void testRemoveEgressInterface() {
425 // Add a route first
426 testRouteAddToNoVlan();
427
428 // Create existing intent
429 MultiPointToSinglePointIntent removedIntent =
430 createIntentToThreeSrcOneTwo(PREFIX1);
431
432 // Set up expectation
433 reset(intentSynchronizer);
434 // Setup the expected intents
435 intentSynchronizer.withdraw(eqExceptId(removedIntent));
436 replay(intentSynchronizer);
437
438 // Define the existing egress interface and remove it
439 Interface intf = new Interface("sw3-eth1", SW3_ETH1,
440 Collections.singletonList(IIP3),
441 MAC3, VlanId.NONE);
442 InterfaceEvent intfEvent =
443 new InterfaceEvent(InterfaceEvent.Type.INTERFACE_REMOVED, intf);
444 interfaceListener.event(intfEvent);
445
446 verify(intentSynchronizer);
447 }
448
449 /*
450 * Builds a MultiPointToSinglePointIntent with dest sw1 (VLAN Id) and src
451 * sw2, sw3.
452 */
453 private MultiPointToSinglePointIntent createIntentToOne(IpPrefix ipPrefix) {
454 // Build the expected treatment
455 TrafficTreatment.Builder treatmentBuilder =
456 DefaultTrafficTreatment.builder();
457 treatmentBuilder.setEthDst(MAC1);
458
459 // Build the expected egress FilteredConnectPoint
460 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
461 selector.matchVlanId(VLAN10);
462 FilteredConnectPoint egressFilteredCP =
463 new FilteredConnectPoint(SW1_ETH1, selector.build());
464
465 // Build the expected selectors
466 Set<FilteredConnectPoint> ingressFilteredCPs = Sets.newHashSet();
467
468 // Build the expected ingress FilteredConnectPoint for sw2
469 selector = DefaultTrafficSelector.builder();
470 selector.matchVlanId(VLAN20);
471 selector.matchEthType(Ethernet.TYPE_IPV4);
472 selector.matchIPDst(ipPrefix);
473 FilteredConnectPoint ingressFilteredCP =
474 new FilteredConnectPoint(SW2_ETH1, selector.build());
475 ingressFilteredCPs.add(ingressFilteredCP);
476
477 // Build the expected ingress FilteredConnectPoint for sw3
478 selector = DefaultTrafficSelector.builder();
479 selector.matchEthType(Ethernet.TYPE_IPV4);
480 selector.matchIPDst(ipPrefix);
481 ingressFilteredCP = new FilteredConnectPoint(SW3_ETH1, selector.build());
482 ingressFilteredCPs.add(ingressFilteredCP);
483
484 // Build the expected intent
485 MultiPointToSinglePointIntent intent =
486 MultiPointToSinglePointIntent.builder()
487 .appId(APPID)
488 .key(Key.of(ipPrefix.toString(), APPID))
489 .filteredIngressPoints(ingressFilteredCPs)
490 .filteredEgressPoint(egressFilteredCP)
491 .treatment(treatmentBuilder.build())
492 .constraints(SdnIpFib.CONSTRAINTS)
493 .build();
494
495 return intent;
496 }
497
498 /*
499 * Builds a MultiPointToSinglePointIntent with dest sw3 (no VLAN Id) and src
500 * sw1, sw2.
501 */
502 private MultiPointToSinglePointIntent createIntentToThreeSrcOneTwo(IpPrefix ipPrefix) {
503 // Build the expected treatment
504 TrafficTreatment.Builder treatmentBuilder =
505 DefaultTrafficTreatment.builder();
506 treatmentBuilder.setEthDst(MAC3);
507
508 // Build the expected egress FilteredConnectPoint
509 FilteredConnectPoint egressFilteredCP = new FilteredConnectPoint(SW3_ETH1);
510
511 // Build the expected selectors
512 Set<FilteredConnectPoint> ingressFilteredCPs = Sets.newHashSet();
513
514 // Build the expected ingress FilteredConnectPoint for sw1
515 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
516 selector.matchVlanId(VLAN10);
517 selector.matchEthType(Ethernet.TYPE_IPV4);
518 selector.matchIPDst(ipPrefix);
519 FilteredConnectPoint ingressFilteredCP =
520 new FilteredConnectPoint(SW1_ETH1, selector.build());
521 ingressFilteredCPs.add(ingressFilteredCP);
522
523 // Build the expected ingress FilteredConnectPoint for sw2
524 selector = DefaultTrafficSelector.builder();
525 selector.matchVlanId(VLAN20);
526 selector.matchEthType(Ethernet.TYPE_IPV4);
527 selector.matchIPDst(ipPrefix);
528 ingressFilteredCP = new FilteredConnectPoint(SW2_ETH1, selector.build());
529 ingressFilteredCPs.add(ingressFilteredCP);
530
531 // Build the expected intent
532 MultiPointToSinglePointIntent intent =
533 MultiPointToSinglePointIntent.builder()
534 .appId(APPID)
535 .key(Key.of(ipPrefix.toString(), APPID))
536 .filteredIngressPoints(ingressFilteredCPs)
537 .filteredEgressPoint(egressFilteredCP)
538 .treatment(treatmentBuilder.build())
539 .constraints(SdnIpFib.CONSTRAINTS)
540 .build();
541
542 return intent;
543 }
544
545 /*
546 * Builds a MultiPointToSinglePointIntent with dest sw3 (no VLAN Id) and src
547 * sw2.
548 */
549 private MultiPointToSinglePointIntent createIntentToThreeSrcTwo(IpPrefix ipPrefix) {
550 // Build the expected treatment
551 TrafficTreatment.Builder treatmentBuilder =
552 DefaultTrafficTreatment.builder();
553 treatmentBuilder.setEthDst(MAC3);
554
555 // Build the expected egress FilteredConnectPoint
556 FilteredConnectPoint egressFilteredCP = new FilteredConnectPoint(SW3_ETH1);
557
558 // Build the expected ingress FilteredConnectPoint for sw2
559 Set<FilteredConnectPoint> ingressFilteredCPs = Sets.newHashSet();
560 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
561 selector.matchVlanId(VLAN20);
562 selector.matchEthType(Ethernet.TYPE_IPV4);
563 selector.matchIPDst(ipPrefix);
564 FilteredConnectPoint ingressFilteredCP =
565 new FilteredConnectPoint(SW2_ETH1, selector.build());
566 ingressFilteredCPs.add(ingressFilteredCP);
567
568 // Build the expected intent
569 MultiPointToSinglePointIntent intent =
570 MultiPointToSinglePointIntent.builder()
571 .appId(APPID)
572 .key(Key.of(ipPrefix.toString(), APPID))
573 .filteredIngressPoints(ingressFilteredCPs)
574 .filteredEgressPoint(egressFilteredCP)
575 .treatment(treatmentBuilder.build())
576 .constraints(SdnIpFib.CONSTRAINTS)
577 .build();
578
579 return intent;
580 }
581
582 /*
583 * Builds a MultiPointToSinglePointIntent with dest sw3 (no VLAN Id) and src
584 * sw1, sw2, sw4.
585 */
586 private MultiPointToSinglePointIntent createIntentToThreeSrcOneTwoFour(IpPrefix ipPrefix) {
587 // Build the expected treatment
588 TrafficTreatment.Builder treatmentBuilder =
589 DefaultTrafficTreatment.builder();
590 treatmentBuilder.setEthDst(MAC3);
591
592 // Build the expected egress FilteredConnectPoint
593 FilteredConnectPoint egressFilteredCP = new FilteredConnectPoint(SW3_ETH1);
594
595 // Build the expected selectors
596 Set<FilteredConnectPoint> ingressFilteredCPs = Sets.newHashSet();
597
598 // Build the expected ingress FilteredConnectPoint for sw1
599 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
600 selector.matchVlanId(VLAN10);
601 selector.matchEthType(Ethernet.TYPE_IPV4);
602 selector.matchIPDst(ipPrefix);
603 FilteredConnectPoint ingressFilteredCP =
604 new FilteredConnectPoint(SW1_ETH1, selector.build());
605 ingressFilteredCPs.add(ingressFilteredCP);
606
607 // Build the expected ingress FilteredConnectPoint for sw2
608 selector = DefaultTrafficSelector.builder();
609 selector.matchVlanId(VLAN20);
610 selector.matchEthType(Ethernet.TYPE_IPV4);
611 selector.matchIPDst(ipPrefix);
612 ingressFilteredCP = new FilteredConnectPoint(SW2_ETH1, selector.build());
613 ingressFilteredCPs.add(ingressFilteredCP);
614
615 // Build the expected ingress FilteredConnectPoint for sw4
616 selector = DefaultTrafficSelector.builder();
617 selector.matchEthType(Ethernet.TYPE_IPV4);
618 selector.matchIPDst(ipPrefix);
619 ingressFilteredCP = new FilteredConnectPoint(SW4_ETH1, selector.build());
620 ingressFilteredCPs.add(ingressFilteredCP);
621
622 // Build the expected intent
623 MultiPointToSinglePointIntent intent =
624 MultiPointToSinglePointIntent.builder()
625 .appId(APPID)
626 .key(Key.of(ipPrefix.toString(), APPID))
627 .filteredIngressPoints(ingressFilteredCPs)
628 .filteredEgressPoint(egressFilteredCP)
629 .treatment(treatmentBuilder.build())
630 .constraints(SdnIpFib.CONSTRAINTS)
631 .build();
632
633 return intent;
634 }
635
636 private class TestCoreService extends CoreServiceAdapter {
637 @Override
638 public ApplicationId getAppId(String name) {
639 return APPID;
640 }
641 }
642
643 private class TestRouteService extends RouteServiceAdapter {
644 @Override
645 public void addListener(RouteListener routeListener) {
646 SdnIpFibTest.this.routeListener = routeListener;
647 }
648 }
649
Luca Prete83bac342016-12-06 19:42:05 -0800650 private class TestNetworkConfigService extends NetworkConfigServiceAdapter {
651 /**
652 * Returns an empty BGP network configuration to be able to correctly
653 * return the encapsulation parameter when needed.
654 *
655 * @return an empty BGP network configuration object
656 */
657 @Override
658 public <S, C extends Config<S>> C getConfig(S subject, Class<C> configClass) {
659 ApplicationId appId =
660 new TestApplicationId(SdnIp.SDN_IP_APP);
661
662 ObjectMapper mapper = new ObjectMapper();
663 ConfigApplyDelegate delegate = new MockCfgDelegate();
664 JsonNode emptyTree = new ObjectMapper().createObjectNode();
665
666 SdnIpConfig sdnIpConfig = new SdnIpConfig();
667
668 sdnIpConfig.init(appId, "sdnip-test", emptyTree, mapper, delegate);
669
670 return (C) sdnIpConfig;
671 }
672 }
673
Luca Pretee4a5e1a2016-09-07 17:01:22 -0700674 private class InterfaceServiceDelegate extends InterfaceServiceAdapter {
675 @Override
676 public void addListener(InterfaceListener listener) {
677 SdnIpFibTest.this.interfaceListener = listener;
678 }
679 }
Luca Prete83bac342016-12-06 19:42:05 -0800680
681 private class MockCfgDelegate implements ConfigApplyDelegate {
682 @Override
683 public void onApply(@SuppressWarnings("rawtypes") Config config) {
684 config.apply();
685 }
686 }
Luca Pretee4a5e1a2016-09-07 17:01:22 -0700687}