blob: 6f238736093e46e98c28237a00d25a4401560f5b [file] [log] [blame]
Pier Ventre5c4a0762016-11-21 10:28:13 -08001/*
2 * Copyright 2016-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.net.intent.impl.compiler;
18
19import com.google.common.collect.ImmutableSet;
20import org.hamcrest.core.Is;
Pier Ventre5c4a0762016-11-21 10:28:13 -080021import org.junit.Before;
22import org.junit.Test;
23import org.onlab.packet.Ethernet;
24import org.onlab.packet.MplsLabel;
25import org.onlab.packet.VlanId;
26import org.onosproject.cfg.ComponentConfigAdapter;
27import org.onosproject.core.CoreService;
Thomas Szyrkowiec770093f2017-05-05 13:06:53 +020028import org.onosproject.net.DeviceId;
Pier Ventre5c4a0762016-11-21 10:28:13 -080029import org.onosproject.net.FilteredConnectPoint;
Thomas Szyrkowiec770093f2017-05-05 13:06:53 +020030import org.onosproject.net.domain.DomainService;
Pier Ventre5c4a0762016-11-21 10:28:13 -080031import org.onosproject.net.flow.DefaultTrafficSelector;
32import org.onosproject.net.flow.DefaultTrafficTreatment;
33import org.onosproject.net.flow.FlowRule;
34import org.onosproject.net.flow.criteria.MplsCriterion;
35import org.onosproject.net.flow.criteria.VlanIdCriterion;
36import org.onosproject.net.intent.FlowRuleIntent;
37import org.onosproject.net.intent.Intent;
38import org.onosproject.net.intent.IntentExtensionService;
39import org.onosproject.net.intent.LinkCollectionIntent;
40import org.onosproject.net.resource.MockResourceService;
41
42import java.util.Collection;
43import java.util.Collections;
44import java.util.List;
45import java.util.stream.Collectors;
46
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070047import static org.easymock.EasyMock.*;
Pier Ventre5c4a0762016-11-21 10:28:13 -080048import static org.hamcrest.MatcherAssert.assertThat;
49import static org.hamcrest.Matchers.hasSize;
50import static org.hamcrest.core.Is.is;
51import static org.onosproject.net.NetTestTools.APP_ID;
Thomas Szyrkowiec770093f2017-05-05 13:06:53 +020052import static org.onosproject.net.domain.DomainId.LOCAL;
Pier Ventre5c4a0762016-11-21 10:28:13 -080053import static org.onosproject.net.flow.criteria.Criterion.Type.MPLS_LABEL;
54import static org.onosproject.net.flow.criteria.Criterion.Type.VLAN_VID;
Thomas Szyrkowiec770093f2017-05-05 13:06:53 +020055import static org.onosproject.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction;
Pier Ventre5c4a0762016-11-21 10:28:13 -080056
57/**
58 * This set of tests are meant to test the proper compilation
59 * of p2p intents.
60 */
61public class LinkCollectionIntentCompilerP2PTest extends AbstractLinkCollectionTest {
62
63 @Before
64 public void setUp() {
65 sut = new LinkCollectionIntentCompiler();
66 coreService = createMock(CoreService.class);
67 expect(coreService.registerApplication("org.onosproject.net.intent"))
68 .andReturn(appId);
69 sut.coreService = coreService;
70
Thomas Szyrkowiec770093f2017-05-05 13:06:53 +020071 domainService = createMock(DomainService.class);
72 expect(domainService.getDomain(anyObject(DeviceId.class))).andReturn(LOCAL).anyTimes();
73 sut.domainService = domainService;
74
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070075 super.setUp();
Pier Ventre5c4a0762016-11-21 10:28:13 -080076
77 intentExtensionService = createMock(IntentExtensionService.class);
78 intentExtensionService.registerCompiler(LinkCollectionIntent.class, sut);
79 intentExtensionService.unregisterCompiler(LinkCollectionIntent.class);
80
81 registrator = new IntentConfigurableRegistrator();
82 registrator.extensionService = intentExtensionService;
83 registrator.cfgService = new ComponentConfigAdapter();
84 registrator.activate();
85
86 sut.registrator = registrator;
87 sut.resourceService = new MockResourceService();
88
Yi Tseng84c5a3d2017-04-14 16:42:59 -070089 LinkCollectionCompiler.optimizeInstructions = false;
Pier Ventre5c4a0762016-11-21 10:28:13 -080090 LinkCollectionCompiler.copyTtl = false;
91
Thomas Szyrkowiec770093f2017-05-05 13:06:53 +020092 replay(coreService, domainService, intentExtensionService);
Pier Ventre5c4a0762016-11-21 10:28:13 -080093 }
94
Pier Ventre5c4a0762016-11-21 10:28:13 -080095 /**
96 * We test the proper compilation of p2p with
97 * trivial selector and trivial treatment.
98 */
99 @Test
100 public void testCompilationTrivialForP2P() {
101
102 intent = LinkCollectionIntent.builder()
103 .appId(APP_ID)
104 .selector(selector)
105 .treatment(treatment)
106 .applyTreatmentOnEgress(true)
107 .links(p2pLinks)
108 .filteredIngressPoints(ImmutableSet.of(
109 new FilteredConnectPoint(d1p10)
110 ))
111 .filteredEgressPoints(ImmutableSet.of(
112 new FilteredConnectPoint(d3p0)
113 ))
114 .build();
115
116 sut.activate();
117
118 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
119 assertThat(compiled, hasSize(1));
120
121 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
122 assertThat(rules, hasSize(3));
123
124 Collection<FlowRule> rulesS1 = rules.stream()
125 .filter(rule -> rule.deviceId().equals(d1p0.deviceId()))
126 .collect(Collectors.toSet());
127 assertThat(rulesS1, hasSize(1));
128 FlowRule ruleS1 = rulesS1.iterator().next();
129 assertThat(ruleS1.selector(), Is.is(
130 DefaultTrafficSelector
131 .builder()
132 .matchInPort(d1p10.port())
133 .build()
134 ));
135 assertThat(ruleS1.treatment(), Is.is(
136 DefaultTrafficTreatment
137 .builder()
138 .setOutput(d1p0.port())
139 .build()
140 ));
141
142 Collection<FlowRule> rulesS2 = rules.stream()
143 .filter(rule -> rule.deviceId().equals(d2p0.deviceId()))
144 .collect(Collectors.toSet());
145 assertThat(rulesS2, hasSize(1));
146 FlowRule ruleS2 = rulesS2.iterator().next();
147 assertThat(ruleS2.selector(), Is.is(
148 DefaultTrafficSelector
149 .builder()
150 .matchInPort(d2p0.port())
151 .build()
152 ));
153 assertThat(ruleS2.treatment(), Is.is(
154 DefaultTrafficTreatment
155 .builder()
156 .setOutput(d2p1.port())
157 .build()
158 ));
159
160
161 Collection<FlowRule> rulesS3 = rules.stream()
162 .filter(rule -> rule.deviceId().equals(d3p1.deviceId()))
163 .collect(Collectors.toSet());
164 assertThat(rulesS3, hasSize(1));
165 FlowRule ruleS3 = rulesS3.iterator().next();
166 assertThat(ruleS3.selector(), Is.is(
167 DefaultTrafficSelector
168 .builder()
169 .matchInPort(d3p1.port())
170 .build()
171 ));
172 assertThat(ruleS3.treatment(), Is.is(
173 DefaultTrafficTreatment
174 .builder()
175 .setOutput(d3p0.port())
176 .build()
177 ));
178
179 sut.deactivate();
180
181 }
182
183 /**
184 * We test the proper compilation of p2p with
185 * trivial selector, trivial treatment and
186 * filtered points.
187 */
188 @Test
189 public void testCompilationFilteredPointForP2P() {
190
191 intent = LinkCollectionIntent.builder()
192 .appId(APP_ID)
193 .selector(selector)
194 .treatment(treatment)
195 .applyTreatmentOnEgress(true)
196 .links(p2pLinks)
197 .filteredIngressPoints(ImmutableSet.of(
198 new FilteredConnectPoint(d1p10, vlan100Selector)
199 ))
200 .filteredEgressPoints(ImmutableSet.of(
201 new FilteredConnectPoint(d3p0, mpls200Selector)
202 ))
203 .build();
204
205 sut.activate();
206
207 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
208 assertThat(compiled, hasSize(1));
209
210 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
211 assertThat(rules, hasSize(3));
212
213 Collection<FlowRule> rulesS1 = rules.stream()
214 .filter(rule -> rule.deviceId().equals(d1p0.deviceId()))
215 .collect(Collectors.toSet());
216 assertThat(rulesS1, hasSize(1));
217 FlowRule ruleS1 = rulesS1.iterator().next();
218 assertThat(ruleS1.selector(), Is.is(
219 DefaultTrafficSelector
220 .builder(vlan100Selector)
221 .matchInPort(d1p10.port())
222 .build()
223 ));
224 assertThat(ruleS1.treatment(), Is.is(
225 DefaultTrafficTreatment
226 .builder()
227 .setOutput(d1p0.port())
228 .build()
229 ));
230
231 Collection<FlowRule> rulesS2 = rules.stream()
232 .filter(rule -> rule.deviceId().equals(d2p0.deviceId()))
233 .collect(Collectors.toSet());
234 assertThat(rulesS2, hasSize(1));
235 FlowRule ruleS2 = rulesS2.iterator().next();
236 assertThat(ruleS2.selector(), Is.is(
237 DefaultTrafficSelector
238 .builder(vlan100Selector)
239 .matchInPort(d2p0.port())
240 .build()
241 ));
242 assertThat(ruleS2.treatment(), Is.is(
243 DefaultTrafficTreatment
244 .builder()
245 .setOutput(d2p1.port())
246 .build()
247 ));
248
249
250 Collection<FlowRule> rulesS3 = rules.stream()
251 .filter(rule -> rule.deviceId().equals(d3p1.deviceId()))
252 .collect(Collectors.toSet());
253 assertThat(rulesS3, hasSize(1));
254 FlowRule ruleS3 = rulesS3.iterator().next();
255 assertThat(ruleS3.selector(), Is.is(
256 DefaultTrafficSelector
257 .builder(vlan100Selector)
258 .matchInPort(d3p1.port())
259 .build()
260 ));
261 assertThat(ruleS3.treatment(), Is.is(
262 DefaultTrafficTreatment
263 .builder()
264 .popVlan()
265 .pushMpls()
266 .setMpls(((MplsCriterion) mpls200Selector.getCriterion(MPLS_LABEL)).label())
267 .setOutput(d3p0.port())
268 .build()
269 ));
270
271 sut.deactivate();
272
273 }
274
275 /**
276 * We test the proper compilation of p2p with
277 * selector, treatment and filtered points.
278 */
279 @Test
280 public void testCompilationNonTrivialForP2P() {
281
282 intent = LinkCollectionIntent.builder()
283 .appId(APP_ID)
284 .selector(ipPrefixSelector)
285 .treatment(ethDstTreatment)
286 .applyTreatmentOnEgress(true)
287 .links(p2pLinks)
288 .filteredIngressPoints(ImmutableSet.of(
289 new FilteredConnectPoint(d1p10, vlan100Selector)
290 ))
291 .filteredEgressPoints(ImmutableSet.of(
292 new FilteredConnectPoint(d3p0, mpls200Selector)
293 ))
294 .build();
295
296 sut.activate();
297
298 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
299 assertThat(compiled, hasSize(1));
300
301 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
302 assertThat(rules, hasSize(3));
303
304 Collection<FlowRule> rulesS1 = rules.stream()
305 .filter(rule -> rule.deviceId().equals(d1p0.deviceId()))
306 .collect(Collectors.toSet());
307 assertThat(rulesS1, hasSize(1));
308 FlowRule ruleS1 = rulesS1.iterator().next();
309 assertThat(ruleS1.selector(), Is.is(
310 DefaultTrafficSelector
311 .builder(ipPrefixSelector)
312 .matchInPort(d1p10.port())
313 .matchVlanId(((VlanIdCriterion) vlan100Selector.getCriterion(VLAN_VID)).vlanId())
314 .build()
315 ));
316 assertThat(ruleS1.treatment(), Is.is(
317 DefaultTrafficTreatment
318 .builder()
319 .setOutput(d1p0.port())
320 .build()
321 ));
322
323 Collection<FlowRule> rulesS2 = rules.stream()
324 .filter(rule -> rule.deviceId().equals(d2p0.deviceId()))
325 .collect(Collectors.toSet());
326 assertThat(rulesS2, hasSize(1));
327 FlowRule ruleS2 = rulesS2.iterator().next();
328 assertThat(ruleS2.selector(), Is.is(
329 DefaultTrafficSelector
330 .builder(ipPrefixSelector)
331 .matchInPort(d2p0.port())
332 .matchVlanId(((VlanIdCriterion) vlan100Selector.getCriterion(VLAN_VID)).vlanId())
333 .build()
334 ));
335 assertThat(ruleS2.treatment(), Is.is(
336 DefaultTrafficTreatment
337 .builder()
338 .setOutput(d2p1.port())
339 .build()
340 ));
341
342
343 Collection<FlowRule> rulesS3 = rules.stream()
344 .filter(rule -> rule.deviceId().equals(d3p1.deviceId()))
345 .collect(Collectors.toSet());
346 assertThat(rulesS3, hasSize(1));
347 FlowRule ruleS3 = rulesS3.iterator().next();
348 assertThat(ruleS3.selector(), Is.is(
349 DefaultTrafficSelector
350 .builder(ipPrefixSelector)
351 .matchInPort(d3p1.port())
352 .matchVlanId(((VlanIdCriterion) vlan100Selector.getCriterion(VLAN_VID)).vlanId())
353 .build()
354 ));
355 assertThat(ruleS3.treatment(), Is.is(
356 DefaultTrafficTreatment
357 .builder()
358 .setEthDst(((ModEtherInstruction) ethDstTreatment
359 .allInstructions()
360 .stream()
361 .filter(instruction -> instruction instanceof ModEtherInstruction)
362 .findFirst().get()).mac())
363 .popVlan()
364 .pushMpls()
365 .setMpls(((MplsCriterion) mpls200Selector.getCriterion(MPLS_LABEL)).label())
366 .setOutput(d3p0.port())
367 .build()
368 ));
369
370 sut.deactivate();
371
372 }
373
374 /**
375 * We test the proper compilation of p2p with the VLAN
376 * encapsulation and trivial filtered points.
377 */
378 @Test
379 public void testVlanEncapsulationForP2P() {
380
381 intent = LinkCollectionIntent.builder()
382 .appId(APP_ID)
383 .selector(selector)
384 .treatment(treatment)
385 .constraints(constraintsForVlan)
386 .links(linksForMp2Sp)
387 .filteredIngressPoints(ImmutableSet.of(new FilteredConnectPoint(d1p10, vlan100Selector)))
388 .filteredEgressPoints(ImmutableSet.of(new FilteredConnectPoint(d3p10, mpls200Selector)))
389 .build();
390
391 sut.activate();
392 /*
393 * We use the FIRST_FIT to simplify tests.
394 */
395 LinkCollectionCompiler.labelAllocator.setLabelSelection(LABEL_SELECTION);
396
397 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
398 assertThat(compiled, hasSize(1));
399
400 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
401 assertThat(rules, hasSize(3));
402
403 Collection<FlowRule> rulesS1 = rules.stream()
404 .filter(rule -> rule.deviceId().equals(d1p0.deviceId()))
405 .collect(Collectors.toSet());
406 assertThat(rulesS1, hasSize(1));
407 FlowRule ruleS1 = rulesS1.iterator().next();
408 assertThat(ruleS1.selector(), is(
409 DefaultTrafficSelector
410 .builder(vlan100Selector)
411 .matchInPort(d1p10.port())
412 .build()
413 ));
414 assertThat(ruleS1.treatment(), is(
415 DefaultTrafficTreatment
416 .builder()
417 .setVlanId(VlanId.vlanId(LABEL))
418 .setOutput(d1p0.port())
419 .build()
420 ));
421
422 Collection<FlowRule> rulesS2 = rules.stream()
423 .filter(rule -> rule.deviceId().equals(d2p0.deviceId()))
424 .collect(Collectors.toSet());
425 assertThat(rulesS2, hasSize(1));
426 FlowRule ruleS2 = rulesS2.iterator().next();
427 assertThat(ruleS2.selector(), is(
428 DefaultTrafficSelector
429 .builder()
430 .matchInPort(d2p0.port())
431 .matchVlanId(VlanId.vlanId(LABEL))
432 .build()
433 ));
434 assertThat(ruleS2.treatment(), is(
435 DefaultTrafficTreatment
436 .builder()
437 .setVlanId(VlanId.vlanId(LABEL))
438 .setOutput(d2p1.port())
439 .build()
440 ));
441
442 Collection<FlowRule> rulesS3 = rules.stream()
443 .filter(rule -> rule.deviceId().equals(d3p0.deviceId()))
444 .collect(Collectors.toSet());
445 assertThat(rulesS3, hasSize(1));
446 FlowRule ruleS3 = rulesS3.iterator().next();
447 assertThat(ruleS3.selector(), is(
448 DefaultTrafficSelector
449 .builder()
450 .matchInPort(d3p0.port())
451 .matchVlanId(VlanId.vlanId(LABEL))
452 .build()
453 ));
454 assertThat(ruleS3.treatment(), is(
455 DefaultTrafficTreatment
456 .builder()
457 .popVlan()
458 .pushMpls()
459 .setMpls(((MplsCriterion) mpls200Selector.getCriterion(MPLS_LABEL)).label())
460 .setOutput(d3p10.port())
461 .build()
462 ));
463
464 sut.deactivate();
465
466 }
467
468
469 /**
470 * We test the proper compilation of p2p with the MPLS
471 * encapsulation and trivial filtered points.
472 */
473 @Test
474 public void testMplsEncapsulationForP2P() {
475
476 intent = LinkCollectionIntent.builder()
477 .appId(APP_ID)
478 .selector(selector)
479 .treatment(treatment)
480 .constraints(constraintsForMPLS)
481 .links(linksForMp2Sp)
482 .filteredIngressPoints(ImmutableSet.of(new FilteredConnectPoint(d1p10, vlan100Selector)))
483 .filteredEgressPoints(ImmutableSet.of(new FilteredConnectPoint(d3p10, mpls200Selector)))
484 .build();
485
486 sut.activate();
487 /*
488 * We use the FIRST_FIT to simplify tests.
489 */
490 LinkCollectionCompiler.labelAllocator.setLabelSelection(LABEL_SELECTION);
491
492 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
493 assertThat(compiled, hasSize(1));
494
495 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
496 assertThat(rules, hasSize(3));
497
498 Collection<FlowRule> rulesS1 = rules.stream()
499 .filter(rule -> rule.deviceId().equals(d1p0.deviceId()))
500 .collect(Collectors.toSet());
501 assertThat(rulesS1, hasSize(1));
502 FlowRule ruleS1 = rulesS1.iterator().next();
503 assertThat(ruleS1.selector(), is(
504 DefaultTrafficSelector
505 .builder(vlan100Selector)
506 .matchInPort(d1p10.port())
507 .build()
508 ));
509 assertThat(ruleS1.treatment(), is(
510 DefaultTrafficTreatment
511 .builder()
512 .popVlan()
513 .pushMpls()
514 .setMpls(MplsLabel.mplsLabel(LABEL))
515 .setOutput(d1p0.port())
516 .build()
517 ));
518
519 Collection<FlowRule> rulesS2 = rules.stream()
520 .filter(rule -> rule.deviceId().equals(d2p0.deviceId()))
521 .collect(Collectors.toSet());
522 assertThat(rulesS2, hasSize(1));
523 FlowRule ruleS2 = rulesS2.iterator().next();
524 assertThat(ruleS2.selector(), is(
525 DefaultTrafficSelector
526 .builder()
527 .matchInPort(d2p0.port())
528 .matchMplsLabel(MplsLabel.mplsLabel(LABEL))
529 .matchEthType(Ethernet.MPLS_UNICAST)
530 .build()
531 ));
532 assertThat(ruleS2.treatment(), is(
533 DefaultTrafficTreatment
534 .builder()
535 .setMpls(MplsLabel.mplsLabel(LABEL))
536 .setOutput(d2p1.port())
537 .build()
538 ));
539
540 Collection<FlowRule> rulesS3 = rules.stream()
541 .filter(rule -> rule.deviceId().equals(d3p0.deviceId()))
542 .collect(Collectors.toSet());
543 assertThat(rulesS3, hasSize(1));
544 FlowRule ruleS3 = rulesS3.iterator().next();
545 assertThat(ruleS3.selector(), is(
546 DefaultTrafficSelector
547 .builder()
548 .matchInPort(d3p0.port())
549 .matchMplsLabel(MplsLabel.mplsLabel(LABEL))
550 .matchEthType(Ethernet.MPLS_UNICAST)
551 .build()
552 ));
553 assertThat(ruleS3.treatment(), is(
554 DefaultTrafficTreatment
555 .builder()
556 .setMpls(((MplsCriterion) mpls200Selector.getCriterion(MPLS_LABEL)).label())
557 .setOutput(d3p10.port())
558 .build()
559 ));
560
561 sut.deactivate();
562
563 }
564
565 /**
566 * We test the proper compilation of p2p with the VLAN
567 * encapsulation, filtered points, selector and treatment.
568 */
569 @Test
570 public void testVlanEncapsulationNonTrivialForP2P() {
571
572 intent = LinkCollectionIntent.builder()
573 .appId(APP_ID)
574 .selector(ipPrefixSelector)
575 .treatment(ethDstTreatment)
576 .constraints(constraintsForVlan)
577 .links(linksForMp2Sp)
578 .filteredIngressPoints(ImmutableSet.of(new FilteredConnectPoint(d1p10, vlan100Selector)))
579 .filteredEgressPoints(ImmutableSet.of(new FilteredConnectPoint(d3p10, mpls69Selector)))
580 .build();
581
582 sut.activate();
583 /*
584 * We use the FIRST_FIT to simplify tests.
585 */
586 LinkCollectionCompiler.labelAllocator.setLabelSelection(LABEL_SELECTION);
587
588 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
589 assertThat(compiled, hasSize(1));
590
591 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
592 assertThat(rules, hasSize(3));
593
594 Collection<FlowRule> rulesS1 = rules.stream()
595 .filter(rule -> rule.deviceId().equals(d1p0.deviceId()))
596 .collect(Collectors.toSet());
597 assertThat(rulesS1, hasSize(1));
598 FlowRule ruleS1 = rulesS1.iterator().next();
599 assertThat(ruleS1.selector(), is(
600 DefaultTrafficSelector
601 .builder(ipPrefixSelector)
602 .matchInPort(d1p10.port())
603 .matchVlanId(((VlanIdCriterion) vlan100Selector.getCriterion(VLAN_VID)).vlanId())
604 .build()
605 ));
606 assertThat(ruleS1.treatment(), is(
607 DefaultTrafficTreatment
608 .builder()
609 .setVlanId(VlanId.vlanId(LABEL))
610 .setOutput(d1p0.port())
611 .build()
612 ));
613
614 Collection<FlowRule> rulesS2 = rules.stream()
615 .filter(rule -> rule.deviceId().equals(d2p0.deviceId()))
616 .collect(Collectors.toSet());
617 assertThat(rulesS2, hasSize(1));
618 FlowRule ruleS2 = rulesS2.iterator().next();
619 assertThat(ruleS2.selector(), is(
620 DefaultTrafficSelector
621 .builder()
622 .matchInPort(d2p0.port())
623 .matchVlanId(VlanId.vlanId(LABEL))
624 .build()
625 ));
626 assertThat(ruleS2.treatment(), is(
627 DefaultTrafficTreatment
628 .builder()
629 .setVlanId(VlanId.vlanId(LABEL))
630 .setOutput(d2p1.port())
631 .build()
632 ));
633
634 Collection<FlowRule> rulesS3 = rules.stream()
635 .filter(rule -> rule.deviceId().equals(d3p0.deviceId()))
636 .collect(Collectors.toSet());
637 assertThat(rulesS3, hasSize(1));
638 FlowRule ruleS3 = rulesS3.iterator().next();
639 assertThat(ruleS3.selector(), is(
640 DefaultTrafficSelector
641 .builder()
642 .matchInPort(d3p0.port())
643 .matchVlanId(VlanId.vlanId(LABEL))
644 .build()
645 ));
646 assertThat(ruleS3.treatment(), is(
647 DefaultTrafficTreatment
648 .builder()
649 .setEthDst(((ModEtherInstruction) ethDstTreatment
650 .allInstructions()
651 .stream()
652 .filter(instruction -> instruction instanceof ModEtherInstruction)
653 .findFirst().get()).mac())
654 .popVlan()
655 .pushMpls()
656 .setMpls(((MplsCriterion) mpls69Selector.getCriterion(MPLS_LABEL)).label())
657 .setOutput(d3p10.port())
658 .build()
659 ));
660
661 sut.deactivate();
662
663 }
664
665 /**
666 * We test the proper compilation of p2p with the MPLS
667 * encapsulation, filtered points, selector and treatment.
668 */
669 @Test
670 public void testMplsEncapsulationNonTrivialForP2P() {
671
672 intent = LinkCollectionIntent.builder()
673 .appId(APP_ID)
674 .selector(ipPrefixSelector)
675 .treatment(ethDstTreatment)
676 .constraints(constraintsForMPLS)
677 .links(linksForMp2Sp)
678 .filteredIngressPoints(ImmutableSet.of(new FilteredConnectPoint(d1p10, vlan100Selector)))
679 .filteredEgressPoints(ImmutableSet.of(new FilteredConnectPoint(d3p10, mpls69Selector)))
680 .build();
681
682 sut.activate();
683 /*
684 * We use the FIRST_FIT to simplify tests.
685 */
686 LinkCollectionCompiler.labelAllocator.setLabelSelection(LABEL_SELECTION);
687
688 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
689 assertThat(compiled, hasSize(1));
690
691 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
692 assertThat(rules, hasSize(3));
693
694 Collection<FlowRule> rulesS1 = rules.stream()
695 .filter(rule -> rule.deviceId().equals(d1p0.deviceId()))
696 .collect(Collectors.toSet());
697 assertThat(rulesS1, hasSize(1));
698 FlowRule ruleS1 = rulesS1.iterator().next();
699 assertThat(ruleS1.selector(), is(
700 DefaultTrafficSelector
701 .builder(ipPrefixSelector)
702 .matchInPort(d1p10.port())
703 .matchVlanId(((VlanIdCriterion) vlan100Selector.getCriterion(VLAN_VID)).vlanId())
704 .build()
705 ));
706 assertThat(ruleS1.treatment(), is(
707 DefaultTrafficTreatment
708 .builder()
709 .popVlan()
710 .pushMpls()
711 .setMpls(MplsLabel.mplsLabel(LABEL))
712 .setOutput(d1p0.port())
713 .build()
714 ));
715
716 Collection<FlowRule> rulesS2 = rules.stream()
717 .filter(rule -> rule.deviceId().equals(d2p0.deviceId()))
718 .collect(Collectors.toSet());
719 assertThat(rulesS2, hasSize(1));
720 FlowRule ruleS2 = rulesS2.iterator().next();
721 assertThat(ruleS2.selector(), is(
722 DefaultTrafficSelector
723 .builder()
724 .matchInPort(d2p0.port())
725 .matchMplsLabel(MplsLabel.mplsLabel(LABEL))
726 .matchEthType(Ethernet.MPLS_UNICAST)
727 .build()
728 ));
729 assertThat(ruleS2.treatment(), is(
730 DefaultTrafficTreatment
731 .builder()
732 .setMpls(MplsLabel.mplsLabel(LABEL))
733 .setOutput(d2p1.port())
734 .build()
735 ));
736
737 Collection<FlowRule> rulesS3 = rules.stream()
738 .filter(rule -> rule.deviceId().equals(d3p0.deviceId()))
739 .collect(Collectors.toSet());
740 assertThat(rulesS3, hasSize(1));
741 FlowRule ruleS3 = rulesS3.iterator().next();
742 assertThat(ruleS3.selector(), is(
743 DefaultTrafficSelector
744 .builder()
745 .matchInPort(d3p0.port())
746 .matchMplsLabel(MplsLabel.mplsLabel(LABEL))
747 .matchEthType(Ethernet.MPLS_UNICAST)
748 .build()
749 ));
750 assertThat(ruleS3.treatment(), is(
751 DefaultTrafficTreatment
752 .builder()
753 .setEthDst(((ModEtherInstruction) ethDstTreatment
754 .allInstructions()
755 .stream()
756 .filter(instruction -> instruction instanceof ModEtherInstruction)
757 .findFirst().get()).mac())
758 .setMpls(((MplsCriterion) mpls69Selector.getCriterion(MPLS_LABEL)).label())
759 .setOutput(d3p10.port())
760 .build()
761 ));
762
763 sut.deactivate();
764
765 }
766
767}