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